json.hpp 895 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677
  1. // __ _____ _____ _____
  2. // __| | __| | | | JSON for Modern C++
  3. // | | |__ | | | | | | version 3.11.2
  4. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5. //
  6. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  7. // SPDX-License-Identifier: MIT
  8. /****************************************************************************\
  9. * Note on documentation: The source files contain links to the online *
  10. * documentation of the public API at https://json.nlohmann.me. This URL *
  11. * contains the most recent documentation and should also be applicable to *
  12. * previous versions; documentation for deprecated functions is not *
  13. * removed, but marked deprecated. See "Generate documentation" section in *
  14. * file docs/README.md. *
  15. \****************************************************************************/
  16. #ifndef INCLUDE_NLOHMANN_JSON_HPP_
  17. #define INCLUDE_NLOHMANN_JSON_HPP_
  18. #include <algorithm> // all_of, find, for_each
  19. #include <cstddef> // nullptr_t, ptrdiff_t, size_t
  20. #include <functional> // hash, less
  21. #include <initializer_list> // initializer_list
  22. #ifndef JSON_NO_IO
  23. #include <iosfwd> // istream, ostream
  24. #endif // JSON_NO_IO
  25. #include <iterator> // random_access_iterator_tag
  26. #include <memory> // unique_ptr
  27. #include <string> // string, stoi, to_string
  28. #include <utility> // declval, forward, move, pair, swap
  29. #include <vector> // vector
  30. // #include <nlohmann/adl_serializer.hpp>
  31. // __ _____ _____ _____
  32. // __| | __| | | | JSON for Modern C++
  33. // | | |__ | | | | | | version 3.11.2
  34. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  35. //
  36. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  37. // SPDX-License-Identifier: MIT
  38. #include <utility>
  39. // #include <nlohmann/detail/abi_macros.hpp>
  40. // __ _____ _____ _____
  41. // __| | __| | | | JSON for Modern C++
  42. // | | |__ | | | | | | version 3.11.2
  43. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  44. //
  45. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  46. // SPDX-License-Identifier: MIT
  47. // This file contains all macro definitions affecting or depending on the ABI
  48. #ifndef JSON_SKIP_LIBRARY_VERSION_CHECK
  49. #if defined(NLOHMANN_JSON_VERSION_MAJOR) && defined(NLOHMANN_JSON_VERSION_MINOR) && defined(NLOHMANN_JSON_VERSION_PATCH)
  50. #if NLOHMANN_JSON_VERSION_MAJOR != 3 || NLOHMANN_JSON_VERSION_MINOR != 11 || NLOHMANN_JSON_VERSION_PATCH != 2
  51. #warning "Already included a different version of the library!"
  52. #endif
  53. #endif
  54. #endif
  55. #define NLOHMANN_JSON_VERSION_MAJOR 3 // NOLINT(modernize-macro-to-enum)
  56. #define NLOHMANN_JSON_VERSION_MINOR 11 // NOLINT(modernize-macro-to-enum)
  57. #define NLOHMANN_JSON_VERSION_PATCH 2 // NOLINT(modernize-macro-to-enum)
  58. #ifndef JSON_DIAGNOSTICS
  59. #define JSON_DIAGNOSTICS 0
  60. #endif
  61. #ifndef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  62. #define JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON 0
  63. #endif
  64. #if JSON_DIAGNOSTICS
  65. #define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS _diag
  66. #else
  67. #define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS
  68. #endif
  69. #if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  70. #define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON _ldvcmp
  71. #else
  72. #define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON
  73. #endif
  74. #ifndef NLOHMANN_JSON_NAMESPACE_NO_VERSION
  75. #define NLOHMANN_JSON_NAMESPACE_NO_VERSION 0
  76. #endif
  77. // Construct the namespace ABI tags component
  78. #define NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b) json_abi ## a ## b
  79. #define NLOHMANN_JSON_ABI_TAGS_CONCAT(a, b) \
  80. NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b)
  81. #define NLOHMANN_JSON_ABI_TAGS \
  82. NLOHMANN_JSON_ABI_TAGS_CONCAT( \
  83. NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS, \
  84. NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON)
  85. // Construct the namespace version component
  86. #define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch) \
  87. _v ## major ## _ ## minor ## _ ## patch
  88. #define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(major, minor, patch) \
  89. NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch)
  90. #if NLOHMANN_JSON_NAMESPACE_NO_VERSION
  91. #define NLOHMANN_JSON_NAMESPACE_VERSION
  92. #else
  93. #define NLOHMANN_JSON_NAMESPACE_VERSION \
  94. NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(NLOHMANN_JSON_VERSION_MAJOR, \
  95. NLOHMANN_JSON_VERSION_MINOR, \
  96. NLOHMANN_JSON_VERSION_PATCH)
  97. #endif
  98. // Combine namespace components
  99. #define NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b) a ## b
  100. #define NLOHMANN_JSON_NAMESPACE_CONCAT(a, b) \
  101. NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b)
  102. #ifndef NLOHMANN_JSON_NAMESPACE
  103. #define NLOHMANN_JSON_NAMESPACE \
  104. nlohmann::NLOHMANN_JSON_NAMESPACE_CONCAT( \
  105. NLOHMANN_JSON_ABI_TAGS, \
  106. NLOHMANN_JSON_NAMESPACE_VERSION)
  107. #endif
  108. #ifndef NLOHMANN_JSON_NAMESPACE_BEGIN
  109. #define NLOHMANN_JSON_NAMESPACE_BEGIN \
  110. namespace nlohmann \
  111. { \
  112. inline namespace NLOHMANN_JSON_NAMESPACE_CONCAT( \
  113. NLOHMANN_JSON_ABI_TAGS, \
  114. NLOHMANN_JSON_NAMESPACE_VERSION) \
  115. {
  116. #endif
  117. #ifndef NLOHMANN_JSON_NAMESPACE_END
  118. #define NLOHMANN_JSON_NAMESPACE_END \
  119. } /* namespace (inline namespace) NOLINT(readability/namespace) */ \
  120. } // namespace nlohmann
  121. #endif
  122. // #include <nlohmann/detail/conversions/from_json.hpp>
  123. // __ _____ _____ _____
  124. // __| | __| | | | JSON for Modern C++
  125. // | | |__ | | | | | | version 3.11.2
  126. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  127. //
  128. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  129. // SPDX-License-Identifier: MIT
  130. #include <algorithm> // transform
  131. #include <array> // array
  132. #include <forward_list> // forward_list
  133. #include <iterator> // inserter, front_inserter, end
  134. #include <map> // map
  135. #include <string> // string
  136. #include <tuple> // tuple, make_tuple
  137. #include <type_traits> // is_arithmetic, is_same, is_enum, underlying_type, is_convertible
  138. #include <unordered_map> // unordered_map
  139. #include <utility> // pair, declval
  140. #include <valarray> // valarray
  141. // #include <nlohmann/detail/exceptions.hpp>
  142. // __ _____ _____ _____
  143. // __| | __| | | | JSON for Modern C++
  144. // | | |__ | | | | | | version 3.11.2
  145. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  146. //
  147. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  148. // SPDX-License-Identifier: MIT
  149. #include <cstddef> // nullptr_t
  150. #include <exception> // exception
  151. #if JSON_DIAGNOSTICS
  152. #include <numeric> // accumulate
  153. #endif
  154. #include <stdexcept> // runtime_error
  155. #include <string> // to_string
  156. #include <vector> // vector
  157. // #include <nlohmann/detail/value_t.hpp>
  158. // __ _____ _____ _____
  159. // __| | __| | | | JSON for Modern C++
  160. // | | |__ | | | | | | version 3.11.2
  161. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  162. //
  163. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  164. // SPDX-License-Identifier: MIT
  165. #include <array> // array
  166. #include <cstddef> // size_t
  167. #include <cstdint> // uint8_t
  168. #include <string> // string
  169. // #include <nlohmann/detail/macro_scope.hpp>
  170. // __ _____ _____ _____
  171. // __| | __| | | | JSON for Modern C++
  172. // | | |__ | | | | | | version 3.11.2
  173. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  174. //
  175. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  176. // SPDX-License-Identifier: MIT
  177. #include <utility> // declval, pair
  178. // #include <nlohmann/detail/meta/detected.hpp>
  179. // __ _____ _____ _____
  180. // __| | __| | | | JSON for Modern C++
  181. // | | |__ | | | | | | version 3.11.2
  182. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  183. //
  184. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  185. // SPDX-License-Identifier: MIT
  186. #include <type_traits>
  187. // #include <nlohmann/detail/meta/void_t.hpp>
  188. // __ _____ _____ _____
  189. // __| | __| | | | JSON for Modern C++
  190. // | | |__ | | | | | | version 3.11.2
  191. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  192. //
  193. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  194. // SPDX-License-Identifier: MIT
  195. // #include <nlohmann/detail/abi_macros.hpp>
  196. NLOHMANN_JSON_NAMESPACE_BEGIN
  197. namespace detail
  198. {
  199. template<typename ...Ts> struct make_void
  200. {
  201. using type = void;
  202. };
  203. template<typename ...Ts> using void_t = typename make_void<Ts...>::type;
  204. } // namespace detail
  205. NLOHMANN_JSON_NAMESPACE_END
  206. NLOHMANN_JSON_NAMESPACE_BEGIN
  207. namespace detail
  208. {
  209. // https://en.cppreference.com/w/cpp/experimental/is_detected
  210. struct nonesuch
  211. {
  212. nonesuch() = delete;
  213. ~nonesuch() = delete;
  214. nonesuch(nonesuch const&) = delete;
  215. nonesuch(nonesuch const&&) = delete;
  216. void operator=(nonesuch const&) = delete;
  217. void operator=(nonesuch&&) = delete;
  218. };
  219. template<class Default,
  220. class AlwaysVoid,
  221. template<class...> class Op,
  222. class... Args>
  223. struct detector
  224. {
  225. using value_t = std::false_type;
  226. using type = Default;
  227. };
  228. template<class Default, template<class...> class Op, class... Args>
  229. struct detector<Default, void_t<Op<Args...>>, Op, Args...>
  230. {
  231. using value_t = std::true_type;
  232. using type = Op<Args...>;
  233. };
  234. template<template<class...> class Op, class... Args>
  235. using is_detected = typename detector<nonesuch, void, Op, Args...>::value_t;
  236. template<template<class...> class Op, class... Args>
  237. struct is_detected_lazy : is_detected<Op, Args...> { };
  238. template<template<class...> class Op, class... Args>
  239. using detected_t = typename detector<nonesuch, void, Op, Args...>::type;
  240. template<class Default, template<class...> class Op, class... Args>
  241. using detected_or = detector<Default, void, Op, Args...>;
  242. template<class Default, template<class...> class Op, class... Args>
  243. using detected_or_t = typename detected_or<Default, Op, Args...>::type;
  244. template<class Expected, template<class...> class Op, class... Args>
  245. using is_detected_exact = std::is_same<Expected, detected_t<Op, Args...>>;
  246. template<class To, template<class...> class Op, class... Args>
  247. using is_detected_convertible =
  248. std::is_convertible<detected_t<Op, Args...>, To>;
  249. } // namespace detail
  250. NLOHMANN_JSON_NAMESPACE_END
  251. // #include <nlohmann/thirdparty/hedley/hedley.hpp>
  252. // __ _____ _____ _____
  253. // __| | __| | | | JSON for Modern C++
  254. // | | |__ | | | | | | version 3.11.2
  255. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  256. //
  257. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  258. // SPDX-FileCopyrightText: 2016-2021 Evan Nemerson <evan@nemerson.com>
  259. // SPDX-License-Identifier: MIT
  260. /* Hedley - https://nemequ.github.io/hedley
  261. * Created by Evan Nemerson <evan@nemerson.com>
  262. */
  263. #if !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < 15)
  264. #if defined(JSON_HEDLEY_VERSION)
  265. #undef JSON_HEDLEY_VERSION
  266. #endif
  267. #define JSON_HEDLEY_VERSION 15
  268. #if defined(JSON_HEDLEY_STRINGIFY_EX)
  269. #undef JSON_HEDLEY_STRINGIFY_EX
  270. #endif
  271. #define JSON_HEDLEY_STRINGIFY_EX(x) #x
  272. #if defined(JSON_HEDLEY_STRINGIFY)
  273. #undef JSON_HEDLEY_STRINGIFY
  274. #endif
  275. #define JSON_HEDLEY_STRINGIFY(x) JSON_HEDLEY_STRINGIFY_EX(x)
  276. #if defined(JSON_HEDLEY_CONCAT_EX)
  277. #undef JSON_HEDLEY_CONCAT_EX
  278. #endif
  279. #define JSON_HEDLEY_CONCAT_EX(a,b) a##b
  280. #if defined(JSON_HEDLEY_CONCAT)
  281. #undef JSON_HEDLEY_CONCAT
  282. #endif
  283. #define JSON_HEDLEY_CONCAT(a,b) JSON_HEDLEY_CONCAT_EX(a,b)
  284. #if defined(JSON_HEDLEY_CONCAT3_EX)
  285. #undef JSON_HEDLEY_CONCAT3_EX
  286. #endif
  287. #define JSON_HEDLEY_CONCAT3_EX(a,b,c) a##b##c
  288. #if defined(JSON_HEDLEY_CONCAT3)
  289. #undef JSON_HEDLEY_CONCAT3
  290. #endif
  291. #define JSON_HEDLEY_CONCAT3(a,b,c) JSON_HEDLEY_CONCAT3_EX(a,b,c)
  292. #if defined(JSON_HEDLEY_VERSION_ENCODE)
  293. #undef JSON_HEDLEY_VERSION_ENCODE
  294. #endif
  295. #define JSON_HEDLEY_VERSION_ENCODE(major,minor,revision) (((major) * 1000000) + ((minor) * 1000) + (revision))
  296. #if defined(JSON_HEDLEY_VERSION_DECODE_MAJOR)
  297. #undef JSON_HEDLEY_VERSION_DECODE_MAJOR
  298. #endif
  299. #define JSON_HEDLEY_VERSION_DECODE_MAJOR(version) ((version) / 1000000)
  300. #if defined(JSON_HEDLEY_VERSION_DECODE_MINOR)
  301. #undef JSON_HEDLEY_VERSION_DECODE_MINOR
  302. #endif
  303. #define JSON_HEDLEY_VERSION_DECODE_MINOR(version) (((version) % 1000000) / 1000)
  304. #if defined(JSON_HEDLEY_VERSION_DECODE_REVISION)
  305. #undef JSON_HEDLEY_VERSION_DECODE_REVISION
  306. #endif
  307. #define JSON_HEDLEY_VERSION_DECODE_REVISION(version) ((version) % 1000)
  308. #if defined(JSON_HEDLEY_GNUC_VERSION)
  309. #undef JSON_HEDLEY_GNUC_VERSION
  310. #endif
  311. #if defined(__GNUC__) && defined(__GNUC_PATCHLEVEL__)
  312. #define JSON_HEDLEY_GNUC_VERSION JSON_HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
  313. #elif defined(__GNUC__)
  314. #define JSON_HEDLEY_GNUC_VERSION JSON_HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, 0)
  315. #endif
  316. #if defined(JSON_HEDLEY_GNUC_VERSION_CHECK)
  317. #undef JSON_HEDLEY_GNUC_VERSION_CHECK
  318. #endif
  319. #if defined(JSON_HEDLEY_GNUC_VERSION)
  320. #define JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_GNUC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  321. #else
  322. #define JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) (0)
  323. #endif
  324. #if defined(JSON_HEDLEY_MSVC_VERSION)
  325. #undef JSON_HEDLEY_MSVC_VERSION
  326. #endif
  327. #if defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 140000000) && !defined(__ICL)
  328. #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 10000000, (_MSC_FULL_VER % 10000000) / 100000, (_MSC_FULL_VER % 100000) / 100)
  329. #elif defined(_MSC_FULL_VER) && !defined(__ICL)
  330. #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 1000000, (_MSC_FULL_VER % 1000000) / 10000, (_MSC_FULL_VER % 10000) / 10)
  331. #elif defined(_MSC_VER) && !defined(__ICL)
  332. #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_VER / 100, _MSC_VER % 100, 0)
  333. #endif
  334. #if defined(JSON_HEDLEY_MSVC_VERSION_CHECK)
  335. #undef JSON_HEDLEY_MSVC_VERSION_CHECK
  336. #endif
  337. #if !defined(JSON_HEDLEY_MSVC_VERSION)
  338. #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (0)
  339. #elif defined(_MSC_VER) && (_MSC_VER >= 1400)
  340. #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 10000000) + (minor * 100000) + (patch)))
  341. #elif defined(_MSC_VER) && (_MSC_VER >= 1200)
  342. #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 1000000) + (minor * 10000) + (patch)))
  343. #else
  344. #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_VER >= ((major * 100) + (minor)))
  345. #endif
  346. #if defined(JSON_HEDLEY_INTEL_VERSION)
  347. #undef JSON_HEDLEY_INTEL_VERSION
  348. #endif
  349. #if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) && !defined(__ICL)
  350. #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, __INTEL_COMPILER_UPDATE)
  351. #elif defined(__INTEL_COMPILER) && !defined(__ICL)
  352. #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, 0)
  353. #endif
  354. #if defined(JSON_HEDLEY_INTEL_VERSION_CHECK)
  355. #undef JSON_HEDLEY_INTEL_VERSION_CHECK
  356. #endif
  357. #if defined(JSON_HEDLEY_INTEL_VERSION)
  358. #define JSON_HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_INTEL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  359. #else
  360. #define JSON_HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (0)
  361. #endif
  362. #if defined(JSON_HEDLEY_INTEL_CL_VERSION)
  363. #undef JSON_HEDLEY_INTEL_CL_VERSION
  364. #endif
  365. #if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) && defined(__ICL)
  366. #define JSON_HEDLEY_INTEL_CL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER, __INTEL_COMPILER_UPDATE, 0)
  367. #endif
  368. #if defined(JSON_HEDLEY_INTEL_CL_VERSION_CHECK)
  369. #undef JSON_HEDLEY_INTEL_CL_VERSION_CHECK
  370. #endif
  371. #if defined(JSON_HEDLEY_INTEL_CL_VERSION)
  372. #define JSON_HEDLEY_INTEL_CL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_INTEL_CL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  373. #else
  374. #define JSON_HEDLEY_INTEL_CL_VERSION_CHECK(major,minor,patch) (0)
  375. #endif
  376. #if defined(JSON_HEDLEY_PGI_VERSION)
  377. #undef JSON_HEDLEY_PGI_VERSION
  378. #endif
  379. #if defined(__PGI) && defined(__PGIC__) && defined(__PGIC_MINOR__) && defined(__PGIC_PATCHLEVEL__)
  380. #define JSON_HEDLEY_PGI_VERSION JSON_HEDLEY_VERSION_ENCODE(__PGIC__, __PGIC_MINOR__, __PGIC_PATCHLEVEL__)
  381. #endif
  382. #if defined(JSON_HEDLEY_PGI_VERSION_CHECK)
  383. #undef JSON_HEDLEY_PGI_VERSION_CHECK
  384. #endif
  385. #if defined(JSON_HEDLEY_PGI_VERSION)
  386. #define JSON_HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_PGI_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  387. #else
  388. #define JSON_HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (0)
  389. #endif
  390. #if defined(JSON_HEDLEY_SUNPRO_VERSION)
  391. #undef JSON_HEDLEY_SUNPRO_VERSION
  392. #endif
  393. #if defined(__SUNPRO_C) && (__SUNPRO_C > 0x1000)
  394. #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((((__SUNPRO_C >> 16) & 0xf) * 10) + ((__SUNPRO_C >> 12) & 0xf), (((__SUNPRO_C >> 8) & 0xf) * 10) + ((__SUNPRO_C >> 4) & 0xf), (__SUNPRO_C & 0xf) * 10)
  395. #elif defined(__SUNPRO_C)
  396. #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((__SUNPRO_C >> 8) & 0xf, (__SUNPRO_C >> 4) & 0xf, (__SUNPRO_C) & 0xf)
  397. #elif defined(__SUNPRO_CC) && (__SUNPRO_CC > 0x1000)
  398. #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((((__SUNPRO_CC >> 16) & 0xf) * 10) + ((__SUNPRO_CC >> 12) & 0xf), (((__SUNPRO_CC >> 8) & 0xf) * 10) + ((__SUNPRO_CC >> 4) & 0xf), (__SUNPRO_CC & 0xf) * 10)
  399. #elif defined(__SUNPRO_CC)
  400. #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((__SUNPRO_CC >> 8) & 0xf, (__SUNPRO_CC >> 4) & 0xf, (__SUNPRO_CC) & 0xf)
  401. #endif
  402. #if defined(JSON_HEDLEY_SUNPRO_VERSION_CHECK)
  403. #undef JSON_HEDLEY_SUNPRO_VERSION_CHECK
  404. #endif
  405. #if defined(JSON_HEDLEY_SUNPRO_VERSION)
  406. #define JSON_HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_SUNPRO_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  407. #else
  408. #define JSON_HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) (0)
  409. #endif
  410. #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION)
  411. #undef JSON_HEDLEY_EMSCRIPTEN_VERSION
  412. #endif
  413. #if defined(__EMSCRIPTEN__)
  414. #define JSON_HEDLEY_EMSCRIPTEN_VERSION JSON_HEDLEY_VERSION_ENCODE(__EMSCRIPTEN_major__, __EMSCRIPTEN_minor__, __EMSCRIPTEN_tiny__)
  415. #endif
  416. #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK)
  417. #undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK
  418. #endif
  419. #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION)
  420. #define JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_EMSCRIPTEN_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  421. #else
  422. #define JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) (0)
  423. #endif
  424. #if defined(JSON_HEDLEY_ARM_VERSION)
  425. #undef JSON_HEDLEY_ARM_VERSION
  426. #endif
  427. #if defined(__CC_ARM) && defined(__ARMCOMPILER_VERSION)
  428. #define JSON_HEDLEY_ARM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ARMCOMPILER_VERSION / 1000000, (__ARMCOMPILER_VERSION % 1000000) / 10000, (__ARMCOMPILER_VERSION % 10000) / 100)
  429. #elif defined(__CC_ARM) && defined(__ARMCC_VERSION)
  430. #define JSON_HEDLEY_ARM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ARMCC_VERSION / 1000000, (__ARMCC_VERSION % 1000000) / 10000, (__ARMCC_VERSION % 10000) / 100)
  431. #endif
  432. #if defined(JSON_HEDLEY_ARM_VERSION_CHECK)
  433. #undef JSON_HEDLEY_ARM_VERSION_CHECK
  434. #endif
  435. #if defined(JSON_HEDLEY_ARM_VERSION)
  436. #define JSON_HEDLEY_ARM_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_ARM_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  437. #else
  438. #define JSON_HEDLEY_ARM_VERSION_CHECK(major,minor,patch) (0)
  439. #endif
  440. #if defined(JSON_HEDLEY_IBM_VERSION)
  441. #undef JSON_HEDLEY_IBM_VERSION
  442. #endif
  443. #if defined(__ibmxl__)
  444. #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ibmxl_version__, __ibmxl_release__, __ibmxl_modification__)
  445. #elif defined(__xlC__) && defined(__xlC_ver__)
  446. #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__xlC__ >> 8, __xlC__ & 0xff, (__xlC_ver__ >> 8) & 0xff)
  447. #elif defined(__xlC__)
  448. #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__xlC__ >> 8, __xlC__ & 0xff, 0)
  449. #endif
  450. #if defined(JSON_HEDLEY_IBM_VERSION_CHECK)
  451. #undef JSON_HEDLEY_IBM_VERSION_CHECK
  452. #endif
  453. #if defined(JSON_HEDLEY_IBM_VERSION)
  454. #define JSON_HEDLEY_IBM_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_IBM_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  455. #else
  456. #define JSON_HEDLEY_IBM_VERSION_CHECK(major,minor,patch) (0)
  457. #endif
  458. #if defined(JSON_HEDLEY_TI_VERSION)
  459. #undef JSON_HEDLEY_TI_VERSION
  460. #endif
  461. #if \
  462. defined(__TI_COMPILER_VERSION__) && \
  463. ( \
  464. defined(__TMS470__) || defined(__TI_ARM__) || \
  465. defined(__MSP430__) || \
  466. defined(__TMS320C2000__) \
  467. )
  468. #if (__TI_COMPILER_VERSION__ >= 16000000)
  469. #define JSON_HEDLEY_TI_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  470. #endif
  471. #endif
  472. #if defined(JSON_HEDLEY_TI_VERSION_CHECK)
  473. #undef JSON_HEDLEY_TI_VERSION_CHECK
  474. #endif
  475. #if defined(JSON_HEDLEY_TI_VERSION)
  476. #define JSON_HEDLEY_TI_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  477. #else
  478. #define JSON_HEDLEY_TI_VERSION_CHECK(major,minor,patch) (0)
  479. #endif
  480. #if defined(JSON_HEDLEY_TI_CL2000_VERSION)
  481. #undef JSON_HEDLEY_TI_CL2000_VERSION
  482. #endif
  483. #if defined(__TI_COMPILER_VERSION__) && defined(__TMS320C2000__)
  484. #define JSON_HEDLEY_TI_CL2000_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  485. #endif
  486. #if defined(JSON_HEDLEY_TI_CL2000_VERSION_CHECK)
  487. #undef JSON_HEDLEY_TI_CL2000_VERSION_CHECK
  488. #endif
  489. #if defined(JSON_HEDLEY_TI_CL2000_VERSION)
  490. #define JSON_HEDLEY_TI_CL2000_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL2000_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  491. #else
  492. #define JSON_HEDLEY_TI_CL2000_VERSION_CHECK(major,minor,patch) (0)
  493. #endif
  494. #if defined(JSON_HEDLEY_TI_CL430_VERSION)
  495. #undef JSON_HEDLEY_TI_CL430_VERSION
  496. #endif
  497. #if defined(__TI_COMPILER_VERSION__) && defined(__MSP430__)
  498. #define JSON_HEDLEY_TI_CL430_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  499. #endif
  500. #if defined(JSON_HEDLEY_TI_CL430_VERSION_CHECK)
  501. #undef JSON_HEDLEY_TI_CL430_VERSION_CHECK
  502. #endif
  503. #if defined(JSON_HEDLEY_TI_CL430_VERSION)
  504. #define JSON_HEDLEY_TI_CL430_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL430_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  505. #else
  506. #define JSON_HEDLEY_TI_CL430_VERSION_CHECK(major,minor,patch) (0)
  507. #endif
  508. #if defined(JSON_HEDLEY_TI_ARMCL_VERSION)
  509. #undef JSON_HEDLEY_TI_ARMCL_VERSION
  510. #endif
  511. #if defined(__TI_COMPILER_VERSION__) && (defined(__TMS470__) || defined(__TI_ARM__))
  512. #define JSON_HEDLEY_TI_ARMCL_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  513. #endif
  514. #if defined(JSON_HEDLEY_TI_ARMCL_VERSION_CHECK)
  515. #undef JSON_HEDLEY_TI_ARMCL_VERSION_CHECK
  516. #endif
  517. #if defined(JSON_HEDLEY_TI_ARMCL_VERSION)
  518. #define JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_ARMCL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  519. #else
  520. #define JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(major,minor,patch) (0)
  521. #endif
  522. #if defined(JSON_HEDLEY_TI_CL6X_VERSION)
  523. #undef JSON_HEDLEY_TI_CL6X_VERSION
  524. #endif
  525. #if defined(__TI_COMPILER_VERSION__) && defined(__TMS320C6X__)
  526. #define JSON_HEDLEY_TI_CL6X_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  527. #endif
  528. #if defined(JSON_HEDLEY_TI_CL6X_VERSION_CHECK)
  529. #undef JSON_HEDLEY_TI_CL6X_VERSION_CHECK
  530. #endif
  531. #if defined(JSON_HEDLEY_TI_CL6X_VERSION)
  532. #define JSON_HEDLEY_TI_CL6X_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL6X_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  533. #else
  534. #define JSON_HEDLEY_TI_CL6X_VERSION_CHECK(major,minor,patch) (0)
  535. #endif
  536. #if defined(JSON_HEDLEY_TI_CL7X_VERSION)
  537. #undef JSON_HEDLEY_TI_CL7X_VERSION
  538. #endif
  539. #if defined(__TI_COMPILER_VERSION__) && defined(__C7000__)
  540. #define JSON_HEDLEY_TI_CL7X_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  541. #endif
  542. #if defined(JSON_HEDLEY_TI_CL7X_VERSION_CHECK)
  543. #undef JSON_HEDLEY_TI_CL7X_VERSION_CHECK
  544. #endif
  545. #if defined(JSON_HEDLEY_TI_CL7X_VERSION)
  546. #define JSON_HEDLEY_TI_CL7X_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL7X_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  547. #else
  548. #define JSON_HEDLEY_TI_CL7X_VERSION_CHECK(major,minor,patch) (0)
  549. #endif
  550. #if defined(JSON_HEDLEY_TI_CLPRU_VERSION)
  551. #undef JSON_HEDLEY_TI_CLPRU_VERSION
  552. #endif
  553. #if defined(__TI_COMPILER_VERSION__) && defined(__PRU__)
  554. #define JSON_HEDLEY_TI_CLPRU_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  555. #endif
  556. #if defined(JSON_HEDLEY_TI_CLPRU_VERSION_CHECK)
  557. #undef JSON_HEDLEY_TI_CLPRU_VERSION_CHECK
  558. #endif
  559. #if defined(JSON_HEDLEY_TI_CLPRU_VERSION)
  560. #define JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CLPRU_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  561. #else
  562. #define JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(major,minor,patch) (0)
  563. #endif
  564. #if defined(JSON_HEDLEY_CRAY_VERSION)
  565. #undef JSON_HEDLEY_CRAY_VERSION
  566. #endif
  567. #if defined(_CRAYC)
  568. #if defined(_RELEASE_PATCHLEVEL)
  569. #define JSON_HEDLEY_CRAY_VERSION JSON_HEDLEY_VERSION_ENCODE(_RELEASE_MAJOR, _RELEASE_MINOR, _RELEASE_PATCHLEVEL)
  570. #else
  571. #define JSON_HEDLEY_CRAY_VERSION JSON_HEDLEY_VERSION_ENCODE(_RELEASE_MAJOR, _RELEASE_MINOR, 0)
  572. #endif
  573. #endif
  574. #if defined(JSON_HEDLEY_CRAY_VERSION_CHECK)
  575. #undef JSON_HEDLEY_CRAY_VERSION_CHECK
  576. #endif
  577. #if defined(JSON_HEDLEY_CRAY_VERSION)
  578. #define JSON_HEDLEY_CRAY_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_CRAY_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  579. #else
  580. #define JSON_HEDLEY_CRAY_VERSION_CHECK(major,minor,patch) (0)
  581. #endif
  582. #if defined(JSON_HEDLEY_IAR_VERSION)
  583. #undef JSON_HEDLEY_IAR_VERSION
  584. #endif
  585. #if defined(__IAR_SYSTEMS_ICC__)
  586. #if __VER__ > 1000
  587. #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE((__VER__ / 1000000), ((__VER__ / 1000) % 1000), (__VER__ % 1000))
  588. #else
  589. #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE(__VER__ / 100, __VER__ % 100, 0)
  590. #endif
  591. #endif
  592. #if defined(JSON_HEDLEY_IAR_VERSION_CHECK)
  593. #undef JSON_HEDLEY_IAR_VERSION_CHECK
  594. #endif
  595. #if defined(JSON_HEDLEY_IAR_VERSION)
  596. #define JSON_HEDLEY_IAR_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_IAR_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  597. #else
  598. #define JSON_HEDLEY_IAR_VERSION_CHECK(major,minor,patch) (0)
  599. #endif
  600. #if defined(JSON_HEDLEY_TINYC_VERSION)
  601. #undef JSON_HEDLEY_TINYC_VERSION
  602. #endif
  603. #if defined(__TINYC__)
  604. #define JSON_HEDLEY_TINYC_VERSION JSON_HEDLEY_VERSION_ENCODE(__TINYC__ / 1000, (__TINYC__ / 100) % 10, __TINYC__ % 100)
  605. #endif
  606. #if defined(JSON_HEDLEY_TINYC_VERSION_CHECK)
  607. #undef JSON_HEDLEY_TINYC_VERSION_CHECK
  608. #endif
  609. #if defined(JSON_HEDLEY_TINYC_VERSION)
  610. #define JSON_HEDLEY_TINYC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TINYC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  611. #else
  612. #define JSON_HEDLEY_TINYC_VERSION_CHECK(major,minor,patch) (0)
  613. #endif
  614. #if defined(JSON_HEDLEY_DMC_VERSION)
  615. #undef JSON_HEDLEY_DMC_VERSION
  616. #endif
  617. #if defined(__DMC__)
  618. #define JSON_HEDLEY_DMC_VERSION JSON_HEDLEY_VERSION_ENCODE(__DMC__ >> 8, (__DMC__ >> 4) & 0xf, __DMC__ & 0xf)
  619. #endif
  620. #if defined(JSON_HEDLEY_DMC_VERSION_CHECK)
  621. #undef JSON_HEDLEY_DMC_VERSION_CHECK
  622. #endif
  623. #if defined(JSON_HEDLEY_DMC_VERSION)
  624. #define JSON_HEDLEY_DMC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_DMC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  625. #else
  626. #define JSON_HEDLEY_DMC_VERSION_CHECK(major,minor,patch) (0)
  627. #endif
  628. #if defined(JSON_HEDLEY_COMPCERT_VERSION)
  629. #undef JSON_HEDLEY_COMPCERT_VERSION
  630. #endif
  631. #if defined(__COMPCERT_VERSION__)
  632. #define JSON_HEDLEY_COMPCERT_VERSION JSON_HEDLEY_VERSION_ENCODE(__COMPCERT_VERSION__ / 10000, (__COMPCERT_VERSION__ / 100) % 100, __COMPCERT_VERSION__ % 100)
  633. #endif
  634. #if defined(JSON_HEDLEY_COMPCERT_VERSION_CHECK)
  635. #undef JSON_HEDLEY_COMPCERT_VERSION_CHECK
  636. #endif
  637. #if defined(JSON_HEDLEY_COMPCERT_VERSION)
  638. #define JSON_HEDLEY_COMPCERT_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_COMPCERT_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  639. #else
  640. #define JSON_HEDLEY_COMPCERT_VERSION_CHECK(major,minor,patch) (0)
  641. #endif
  642. #if defined(JSON_HEDLEY_PELLES_VERSION)
  643. #undef JSON_HEDLEY_PELLES_VERSION
  644. #endif
  645. #if defined(__POCC__)
  646. #define JSON_HEDLEY_PELLES_VERSION JSON_HEDLEY_VERSION_ENCODE(__POCC__ / 100, __POCC__ % 100, 0)
  647. #endif
  648. #if defined(JSON_HEDLEY_PELLES_VERSION_CHECK)
  649. #undef JSON_HEDLEY_PELLES_VERSION_CHECK
  650. #endif
  651. #if defined(JSON_HEDLEY_PELLES_VERSION)
  652. #define JSON_HEDLEY_PELLES_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_PELLES_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  653. #else
  654. #define JSON_HEDLEY_PELLES_VERSION_CHECK(major,minor,patch) (0)
  655. #endif
  656. #if defined(JSON_HEDLEY_MCST_LCC_VERSION)
  657. #undef JSON_HEDLEY_MCST_LCC_VERSION
  658. #endif
  659. #if defined(__LCC__) && defined(__LCC_MINOR__)
  660. #define JSON_HEDLEY_MCST_LCC_VERSION JSON_HEDLEY_VERSION_ENCODE(__LCC__ / 100, __LCC__ % 100, __LCC_MINOR__)
  661. #endif
  662. #if defined(JSON_HEDLEY_MCST_LCC_VERSION_CHECK)
  663. #undef JSON_HEDLEY_MCST_LCC_VERSION_CHECK
  664. #endif
  665. #if defined(JSON_HEDLEY_MCST_LCC_VERSION)
  666. #define JSON_HEDLEY_MCST_LCC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_MCST_LCC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  667. #else
  668. #define JSON_HEDLEY_MCST_LCC_VERSION_CHECK(major,minor,patch) (0)
  669. #endif
  670. #if defined(JSON_HEDLEY_GCC_VERSION)
  671. #undef JSON_HEDLEY_GCC_VERSION
  672. #endif
  673. #if \
  674. defined(JSON_HEDLEY_GNUC_VERSION) && \
  675. !defined(__clang__) && \
  676. !defined(JSON_HEDLEY_INTEL_VERSION) && \
  677. !defined(JSON_HEDLEY_PGI_VERSION) && \
  678. !defined(JSON_HEDLEY_ARM_VERSION) && \
  679. !defined(JSON_HEDLEY_CRAY_VERSION) && \
  680. !defined(JSON_HEDLEY_TI_VERSION) && \
  681. !defined(JSON_HEDLEY_TI_ARMCL_VERSION) && \
  682. !defined(JSON_HEDLEY_TI_CL430_VERSION) && \
  683. !defined(JSON_HEDLEY_TI_CL2000_VERSION) && \
  684. !defined(JSON_HEDLEY_TI_CL6X_VERSION) && \
  685. !defined(JSON_HEDLEY_TI_CL7X_VERSION) && \
  686. !defined(JSON_HEDLEY_TI_CLPRU_VERSION) && \
  687. !defined(__COMPCERT__) && \
  688. !defined(JSON_HEDLEY_MCST_LCC_VERSION)
  689. #define JSON_HEDLEY_GCC_VERSION JSON_HEDLEY_GNUC_VERSION
  690. #endif
  691. #if defined(JSON_HEDLEY_GCC_VERSION_CHECK)
  692. #undef JSON_HEDLEY_GCC_VERSION_CHECK
  693. #endif
  694. #if defined(JSON_HEDLEY_GCC_VERSION)
  695. #define JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_GCC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  696. #else
  697. #define JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (0)
  698. #endif
  699. #if defined(JSON_HEDLEY_HAS_ATTRIBUTE)
  700. #undef JSON_HEDLEY_HAS_ATTRIBUTE
  701. #endif
  702. #if \
  703. defined(__has_attribute) && \
  704. ( \
  705. (!defined(JSON_HEDLEY_IAR_VERSION) || JSON_HEDLEY_IAR_VERSION_CHECK(8,5,9)) \
  706. )
  707. # define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) __has_attribute(attribute)
  708. #else
  709. # define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) (0)
  710. #endif
  711. #if defined(JSON_HEDLEY_GNUC_HAS_ATTRIBUTE)
  712. #undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE
  713. #endif
  714. #if defined(__has_attribute)
  715. #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
  716. #else
  717. #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  718. #endif
  719. #if defined(JSON_HEDLEY_GCC_HAS_ATTRIBUTE)
  720. #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE
  721. #endif
  722. #if defined(__has_attribute)
  723. #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
  724. #else
  725. #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  726. #endif
  727. #if defined(JSON_HEDLEY_HAS_CPP_ATTRIBUTE)
  728. #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE
  729. #endif
  730. #if \
  731. defined(__has_cpp_attribute) && \
  732. defined(__cplusplus) && \
  733. (!defined(JSON_HEDLEY_SUNPRO_VERSION) || JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0))
  734. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) __has_cpp_attribute(attribute)
  735. #else
  736. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) (0)
  737. #endif
  738. #if defined(JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS)
  739. #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS
  740. #endif
  741. #if !defined(__cplusplus) || !defined(__has_cpp_attribute)
  742. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) (0)
  743. #elif \
  744. !defined(JSON_HEDLEY_PGI_VERSION) && \
  745. !defined(JSON_HEDLEY_IAR_VERSION) && \
  746. (!defined(JSON_HEDLEY_SUNPRO_VERSION) || JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0)) && \
  747. (!defined(JSON_HEDLEY_MSVC_VERSION) || JSON_HEDLEY_MSVC_VERSION_CHECK(19,20,0))
  748. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) JSON_HEDLEY_HAS_CPP_ATTRIBUTE(ns::attribute)
  749. #else
  750. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) (0)
  751. #endif
  752. #if defined(JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE)
  753. #undef JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE
  754. #endif
  755. #if defined(__has_cpp_attribute) && defined(__cplusplus)
  756. #define JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) __has_cpp_attribute(attribute)
  757. #else
  758. #define JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  759. #endif
  760. #if defined(JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE)
  761. #undef JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE
  762. #endif
  763. #if defined(__has_cpp_attribute) && defined(__cplusplus)
  764. #define JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) __has_cpp_attribute(attribute)
  765. #else
  766. #define JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  767. #endif
  768. #if defined(JSON_HEDLEY_HAS_BUILTIN)
  769. #undef JSON_HEDLEY_HAS_BUILTIN
  770. #endif
  771. #if defined(__has_builtin)
  772. #define JSON_HEDLEY_HAS_BUILTIN(builtin) __has_builtin(builtin)
  773. #else
  774. #define JSON_HEDLEY_HAS_BUILTIN(builtin) (0)
  775. #endif
  776. #if defined(JSON_HEDLEY_GNUC_HAS_BUILTIN)
  777. #undef JSON_HEDLEY_GNUC_HAS_BUILTIN
  778. #endif
  779. #if defined(__has_builtin)
  780. #define JSON_HEDLEY_GNUC_HAS_BUILTIN(builtin,major,minor,patch) __has_builtin(builtin)
  781. #else
  782. #define JSON_HEDLEY_GNUC_HAS_BUILTIN(builtin,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  783. #endif
  784. #if defined(JSON_HEDLEY_GCC_HAS_BUILTIN)
  785. #undef JSON_HEDLEY_GCC_HAS_BUILTIN
  786. #endif
  787. #if defined(__has_builtin)
  788. #define JSON_HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) __has_builtin(builtin)
  789. #else
  790. #define JSON_HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  791. #endif
  792. #if defined(JSON_HEDLEY_HAS_FEATURE)
  793. #undef JSON_HEDLEY_HAS_FEATURE
  794. #endif
  795. #if defined(__has_feature)
  796. #define JSON_HEDLEY_HAS_FEATURE(feature) __has_feature(feature)
  797. #else
  798. #define JSON_HEDLEY_HAS_FEATURE(feature) (0)
  799. #endif
  800. #if defined(JSON_HEDLEY_GNUC_HAS_FEATURE)
  801. #undef JSON_HEDLEY_GNUC_HAS_FEATURE
  802. #endif
  803. #if defined(__has_feature)
  804. #define JSON_HEDLEY_GNUC_HAS_FEATURE(feature,major,minor,patch) __has_feature(feature)
  805. #else
  806. #define JSON_HEDLEY_GNUC_HAS_FEATURE(feature,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  807. #endif
  808. #if defined(JSON_HEDLEY_GCC_HAS_FEATURE)
  809. #undef JSON_HEDLEY_GCC_HAS_FEATURE
  810. #endif
  811. #if defined(__has_feature)
  812. #define JSON_HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) __has_feature(feature)
  813. #else
  814. #define JSON_HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  815. #endif
  816. #if defined(JSON_HEDLEY_HAS_EXTENSION)
  817. #undef JSON_HEDLEY_HAS_EXTENSION
  818. #endif
  819. #if defined(__has_extension)
  820. #define JSON_HEDLEY_HAS_EXTENSION(extension) __has_extension(extension)
  821. #else
  822. #define JSON_HEDLEY_HAS_EXTENSION(extension) (0)
  823. #endif
  824. #if defined(JSON_HEDLEY_GNUC_HAS_EXTENSION)
  825. #undef JSON_HEDLEY_GNUC_HAS_EXTENSION
  826. #endif
  827. #if defined(__has_extension)
  828. #define JSON_HEDLEY_GNUC_HAS_EXTENSION(extension,major,minor,patch) __has_extension(extension)
  829. #else
  830. #define JSON_HEDLEY_GNUC_HAS_EXTENSION(extension,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  831. #endif
  832. #if defined(JSON_HEDLEY_GCC_HAS_EXTENSION)
  833. #undef JSON_HEDLEY_GCC_HAS_EXTENSION
  834. #endif
  835. #if defined(__has_extension)
  836. #define JSON_HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) __has_extension(extension)
  837. #else
  838. #define JSON_HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  839. #endif
  840. #if defined(JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE)
  841. #undef JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE
  842. #endif
  843. #if defined(__has_declspec_attribute)
  844. #define JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) __has_declspec_attribute(attribute)
  845. #else
  846. #define JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) (0)
  847. #endif
  848. #if defined(JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE)
  849. #undef JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE
  850. #endif
  851. #if defined(__has_declspec_attribute)
  852. #define JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) __has_declspec_attribute(attribute)
  853. #else
  854. #define JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  855. #endif
  856. #if defined(JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE)
  857. #undef JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE
  858. #endif
  859. #if defined(__has_declspec_attribute)
  860. #define JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) __has_declspec_attribute(attribute)
  861. #else
  862. #define JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  863. #endif
  864. #if defined(JSON_HEDLEY_HAS_WARNING)
  865. #undef JSON_HEDLEY_HAS_WARNING
  866. #endif
  867. #if defined(__has_warning)
  868. #define JSON_HEDLEY_HAS_WARNING(warning) __has_warning(warning)
  869. #else
  870. #define JSON_HEDLEY_HAS_WARNING(warning) (0)
  871. #endif
  872. #if defined(JSON_HEDLEY_GNUC_HAS_WARNING)
  873. #undef JSON_HEDLEY_GNUC_HAS_WARNING
  874. #endif
  875. #if defined(__has_warning)
  876. #define JSON_HEDLEY_GNUC_HAS_WARNING(warning,major,minor,patch) __has_warning(warning)
  877. #else
  878. #define JSON_HEDLEY_GNUC_HAS_WARNING(warning,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  879. #endif
  880. #if defined(JSON_HEDLEY_GCC_HAS_WARNING)
  881. #undef JSON_HEDLEY_GCC_HAS_WARNING
  882. #endif
  883. #if defined(__has_warning)
  884. #define JSON_HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) __has_warning(warning)
  885. #else
  886. #define JSON_HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  887. #endif
  888. #if \
  889. (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
  890. defined(__clang__) || \
  891. JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \
  892. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  893. JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \
  894. JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \
  895. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  896. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  897. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,7,0) || \
  898. JSON_HEDLEY_TI_CL430_VERSION_CHECK(2,0,1) || \
  899. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,1,0) || \
  900. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,0,0) || \
  901. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  902. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  903. JSON_HEDLEY_CRAY_VERSION_CHECK(5,0,0) || \
  904. JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,17) || \
  905. JSON_HEDLEY_SUNPRO_VERSION_CHECK(8,0,0) || \
  906. (JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) && defined(__C99_PRAGMA_OPERATOR))
  907. #define JSON_HEDLEY_PRAGMA(value) _Pragma(#value)
  908. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
  909. #define JSON_HEDLEY_PRAGMA(value) __pragma(value)
  910. #else
  911. #define JSON_HEDLEY_PRAGMA(value)
  912. #endif
  913. #if defined(JSON_HEDLEY_DIAGNOSTIC_PUSH)
  914. #undef JSON_HEDLEY_DIAGNOSTIC_PUSH
  915. #endif
  916. #if defined(JSON_HEDLEY_DIAGNOSTIC_POP)
  917. #undef JSON_HEDLEY_DIAGNOSTIC_POP
  918. #endif
  919. #if defined(__clang__)
  920. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("clang diagnostic push")
  921. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("clang diagnostic pop")
  922. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  923. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)")
  924. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)")
  925. #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0)
  926. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push")
  927. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("GCC diagnostic pop")
  928. #elif \
  929. JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) || \
  930. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  931. #define JSON_HEDLEY_DIAGNOSTIC_PUSH __pragma(warning(push))
  932. #define JSON_HEDLEY_DIAGNOSTIC_POP __pragma(warning(pop))
  933. #elif JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0)
  934. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("push")
  935. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("pop")
  936. #elif \
  937. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  938. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  939. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,4,0) || \
  940. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,1,0) || \
  941. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  942. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0)
  943. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("diag_push")
  944. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("diag_pop")
  945. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,90,0)
  946. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)")
  947. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)")
  948. #else
  949. #define JSON_HEDLEY_DIAGNOSTIC_PUSH
  950. #define JSON_HEDLEY_DIAGNOSTIC_POP
  951. #endif
  952. /* JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_ is for
  953. HEDLEY INTERNAL USE ONLY. API subject to change without notice. */
  954. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_)
  955. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_
  956. #endif
  957. #if defined(__cplusplus)
  958. # if JSON_HEDLEY_HAS_WARNING("-Wc++98-compat")
  959. # if JSON_HEDLEY_HAS_WARNING("-Wc++17-extensions")
  960. # if JSON_HEDLEY_HAS_WARNING("-Wc++1z-extensions")
  961. # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
  962. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  963. _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
  964. _Pragma("clang diagnostic ignored \"-Wc++17-extensions\"") \
  965. _Pragma("clang diagnostic ignored \"-Wc++1z-extensions\"") \
  966. xpr \
  967. JSON_HEDLEY_DIAGNOSTIC_POP
  968. # else
  969. # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
  970. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  971. _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
  972. _Pragma("clang diagnostic ignored \"-Wc++17-extensions\"") \
  973. xpr \
  974. JSON_HEDLEY_DIAGNOSTIC_POP
  975. # endif
  976. # else
  977. # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
  978. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  979. _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
  980. xpr \
  981. JSON_HEDLEY_DIAGNOSTIC_POP
  982. # endif
  983. # endif
  984. #endif
  985. #if !defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_)
  986. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(x) x
  987. #endif
  988. #if defined(JSON_HEDLEY_CONST_CAST)
  989. #undef JSON_HEDLEY_CONST_CAST
  990. #endif
  991. #if defined(__cplusplus)
  992. # define JSON_HEDLEY_CONST_CAST(T, expr) (const_cast<T>(expr))
  993. #elif \
  994. JSON_HEDLEY_HAS_WARNING("-Wcast-qual") || \
  995. JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0) || \
  996. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  997. # define JSON_HEDLEY_CONST_CAST(T, expr) (__extension__ ({ \
  998. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  999. JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL \
  1000. ((T) (expr)); \
  1001. JSON_HEDLEY_DIAGNOSTIC_POP \
  1002. }))
  1003. #else
  1004. # define JSON_HEDLEY_CONST_CAST(T, expr) ((T) (expr))
  1005. #endif
  1006. #if defined(JSON_HEDLEY_REINTERPRET_CAST)
  1007. #undef JSON_HEDLEY_REINTERPRET_CAST
  1008. #endif
  1009. #if defined(__cplusplus)
  1010. #define JSON_HEDLEY_REINTERPRET_CAST(T, expr) (reinterpret_cast<T>(expr))
  1011. #else
  1012. #define JSON_HEDLEY_REINTERPRET_CAST(T, expr) ((T) (expr))
  1013. #endif
  1014. #if defined(JSON_HEDLEY_STATIC_CAST)
  1015. #undef JSON_HEDLEY_STATIC_CAST
  1016. #endif
  1017. #if defined(__cplusplus)
  1018. #define JSON_HEDLEY_STATIC_CAST(T, expr) (static_cast<T>(expr))
  1019. #else
  1020. #define JSON_HEDLEY_STATIC_CAST(T, expr) ((T) (expr))
  1021. #endif
  1022. #if defined(JSON_HEDLEY_CPP_CAST)
  1023. #undef JSON_HEDLEY_CPP_CAST
  1024. #endif
  1025. #if defined(__cplusplus)
  1026. # if JSON_HEDLEY_HAS_WARNING("-Wold-style-cast")
  1027. # define JSON_HEDLEY_CPP_CAST(T, expr) \
  1028. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  1029. _Pragma("clang diagnostic ignored \"-Wold-style-cast\"") \
  1030. ((T) (expr)) \
  1031. JSON_HEDLEY_DIAGNOSTIC_POP
  1032. # elif JSON_HEDLEY_IAR_VERSION_CHECK(8,3,0)
  1033. # define JSON_HEDLEY_CPP_CAST(T, expr) \
  1034. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  1035. _Pragma("diag_suppress=Pe137") \
  1036. JSON_HEDLEY_DIAGNOSTIC_POP
  1037. # else
  1038. # define JSON_HEDLEY_CPP_CAST(T, expr) ((T) (expr))
  1039. # endif
  1040. #else
  1041. # define JSON_HEDLEY_CPP_CAST(T, expr) (expr)
  1042. #endif
  1043. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED)
  1044. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
  1045. #endif
  1046. #if JSON_HEDLEY_HAS_WARNING("-Wdeprecated-declarations")
  1047. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"")
  1048. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  1049. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warning(disable:1478 1786)")
  1050. #elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1051. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:1478 1786))
  1052. #elif JSON_HEDLEY_PGI_VERSION_CHECK(20,7,0)
  1053. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1216,1444,1445")
  1054. #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
  1055. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1444")
  1056. #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0)
  1057. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
  1058. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
  1059. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:4996))
  1060. #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1061. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1444")
  1062. #elif \
  1063. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1064. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1065. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1066. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1067. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1068. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1069. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1070. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1071. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1072. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1073. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0)
  1074. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1291,1718")
  1075. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) && !defined(__cplusplus)
  1076. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("error_messages(off,E_DEPRECATED_ATT,E_DEPRECATED_ATT_MESS)")
  1077. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) && defined(__cplusplus)
  1078. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("error_messages(off,symdeprecated,symdeprecated2)")
  1079. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1080. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress=Pe1444,Pe1215")
  1081. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,90,0)
  1082. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warn(disable:2241)")
  1083. #else
  1084. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
  1085. #endif
  1086. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS)
  1087. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
  1088. #endif
  1089. #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
  1090. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("clang diagnostic ignored \"-Wunknown-pragmas\"")
  1091. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  1092. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("warning(disable:161)")
  1093. #elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1094. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS __pragma(warning(disable:161))
  1095. #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
  1096. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 1675")
  1097. #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0)
  1098. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("GCC diagnostic ignored \"-Wunknown-pragmas\"")
  1099. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
  1100. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS __pragma(warning(disable:4068))
  1101. #elif \
  1102. JSON_HEDLEY_TI_VERSION_CHECK(16,9,0) || \
  1103. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) || \
  1104. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1105. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0)
  1106. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163")
  1107. #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0)
  1108. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163")
  1109. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1110. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress=Pe161")
  1111. #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1112. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 161")
  1113. #else
  1114. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
  1115. #endif
  1116. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES)
  1117. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
  1118. #endif
  1119. #if JSON_HEDLEY_HAS_WARNING("-Wunknown-attributes")
  1120. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("clang diagnostic ignored \"-Wunknown-attributes\"")
  1121. #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0)
  1122. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
  1123. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(17,0,0)
  1124. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("warning(disable:1292)")
  1125. #elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1126. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES __pragma(warning(disable:1292))
  1127. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,0)
  1128. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES __pragma(warning(disable:5030))
  1129. #elif JSON_HEDLEY_PGI_VERSION_CHECK(20,7,0)
  1130. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097,1098")
  1131. #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
  1132. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097")
  1133. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,14,0) && defined(__cplusplus)
  1134. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("error_messages(off,attrskipunsup)")
  1135. #elif \
  1136. JSON_HEDLEY_TI_VERSION_CHECK(18,1,0) || \
  1137. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,3,0) || \
  1138. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0)
  1139. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1173")
  1140. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1141. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress=Pe1097")
  1142. #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1143. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097")
  1144. #else
  1145. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
  1146. #endif
  1147. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL)
  1148. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
  1149. #endif
  1150. #if JSON_HEDLEY_HAS_WARNING("-Wcast-qual")
  1151. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("clang diagnostic ignored \"-Wcast-qual\"")
  1152. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  1153. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("warning(disable:2203 2331)")
  1154. #elif JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0)
  1155. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("GCC diagnostic ignored \"-Wcast-qual\"")
  1156. #else
  1157. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
  1158. #endif
  1159. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION)
  1160. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
  1161. #endif
  1162. #if JSON_HEDLEY_HAS_WARNING("-Wunused-function")
  1163. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("clang diagnostic ignored \"-Wunused-function\"")
  1164. #elif JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0)
  1165. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("GCC diagnostic ignored \"-Wunused-function\"")
  1166. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(1,0,0)
  1167. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION __pragma(warning(disable:4505))
  1168. #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1169. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("diag_suppress 3142")
  1170. #else
  1171. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
  1172. #endif
  1173. #if defined(JSON_HEDLEY_DEPRECATED)
  1174. #undef JSON_HEDLEY_DEPRECATED
  1175. #endif
  1176. #if defined(JSON_HEDLEY_DEPRECATED_FOR)
  1177. #undef JSON_HEDLEY_DEPRECATED_FOR
  1178. #endif
  1179. #if \
  1180. JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
  1181. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1182. #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated("Since " # since))
  1183. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated("Since " #since "; use " #replacement))
  1184. #elif \
  1185. (JSON_HEDLEY_HAS_EXTENSION(attribute_deprecated_with_message) && !defined(JSON_HEDLEY_IAR_VERSION)) || \
  1186. JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \
  1187. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1188. JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) || \
  1189. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) || \
  1190. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1191. JSON_HEDLEY_TI_VERSION_CHECK(18,1,0) || \
  1192. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(18,1,0) || \
  1193. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,3,0) || \
  1194. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1195. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0) || \
  1196. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1197. #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__("Since " #since)))
  1198. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__("Since " #since "; use " #replacement)))
  1199. #elif defined(__cplusplus) && (__cplusplus >= 201402L)
  1200. #define JSON_HEDLEY_DEPRECATED(since) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since)]])
  1201. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since "; use " #replacement)]])
  1202. #elif \
  1203. JSON_HEDLEY_HAS_ATTRIBUTE(deprecated) || \
  1204. JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
  1205. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1206. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1207. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1208. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1209. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1210. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1211. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1212. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1213. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1214. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1215. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1216. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1217. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \
  1218. JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
  1219. #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__))
  1220. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__))
  1221. #elif \
  1222. JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
  1223. JSON_HEDLEY_PELLES_VERSION_CHECK(6,50,0) || \
  1224. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1225. #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated)
  1226. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated)
  1227. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1228. #define JSON_HEDLEY_DEPRECATED(since) _Pragma("deprecated")
  1229. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) _Pragma("deprecated")
  1230. #else
  1231. #define JSON_HEDLEY_DEPRECATED(since)
  1232. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement)
  1233. #endif
  1234. #if defined(JSON_HEDLEY_UNAVAILABLE)
  1235. #undef JSON_HEDLEY_UNAVAILABLE
  1236. #endif
  1237. #if \
  1238. JSON_HEDLEY_HAS_ATTRIBUTE(warning) || \
  1239. JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0) || \
  1240. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1241. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1242. #define JSON_HEDLEY_UNAVAILABLE(available_since) __attribute__((__warning__("Not available until " #available_since)))
  1243. #else
  1244. #define JSON_HEDLEY_UNAVAILABLE(available_since)
  1245. #endif
  1246. #if defined(JSON_HEDLEY_WARN_UNUSED_RESULT)
  1247. #undef JSON_HEDLEY_WARN_UNUSED_RESULT
  1248. #endif
  1249. #if defined(JSON_HEDLEY_WARN_UNUSED_RESULT_MSG)
  1250. #undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG
  1251. #endif
  1252. #if \
  1253. JSON_HEDLEY_HAS_ATTRIBUTE(warn_unused_result) || \
  1254. JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
  1255. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1256. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1257. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1258. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1259. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1260. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1261. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1262. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1263. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1264. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1265. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1266. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1267. (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)) || \
  1268. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1269. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1270. #define JSON_HEDLEY_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
  1271. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) __attribute__((__warn_unused_result__))
  1272. #elif (JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard) >= 201907L)
  1273. #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
  1274. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard(msg)]])
  1275. #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard)
  1276. #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
  1277. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
  1278. #elif defined(_Check_return_) /* SAL */
  1279. #define JSON_HEDLEY_WARN_UNUSED_RESULT _Check_return_
  1280. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) _Check_return_
  1281. #else
  1282. #define JSON_HEDLEY_WARN_UNUSED_RESULT
  1283. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg)
  1284. #endif
  1285. #if defined(JSON_HEDLEY_SENTINEL)
  1286. #undef JSON_HEDLEY_SENTINEL
  1287. #endif
  1288. #if \
  1289. JSON_HEDLEY_HAS_ATTRIBUTE(sentinel) || \
  1290. JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
  1291. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1292. JSON_HEDLEY_ARM_VERSION_CHECK(5,4,0) || \
  1293. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1294. #define JSON_HEDLEY_SENTINEL(position) __attribute__((__sentinel__(position)))
  1295. #else
  1296. #define JSON_HEDLEY_SENTINEL(position)
  1297. #endif
  1298. #if defined(JSON_HEDLEY_NO_RETURN)
  1299. #undef JSON_HEDLEY_NO_RETURN
  1300. #endif
  1301. #if JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1302. #define JSON_HEDLEY_NO_RETURN __noreturn
  1303. #elif \
  1304. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1305. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1306. #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__))
  1307. #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
  1308. #define JSON_HEDLEY_NO_RETURN _Noreturn
  1309. #elif defined(__cplusplus) && (__cplusplus >= 201103L)
  1310. #define JSON_HEDLEY_NO_RETURN JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[noreturn]])
  1311. #elif \
  1312. JSON_HEDLEY_HAS_ATTRIBUTE(noreturn) || \
  1313. JSON_HEDLEY_GCC_VERSION_CHECK(3,2,0) || \
  1314. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1315. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1316. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1317. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1318. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1319. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1320. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1321. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1322. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1323. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1324. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1325. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1326. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1327. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1328. JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
  1329. #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__))
  1330. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
  1331. #define JSON_HEDLEY_NO_RETURN _Pragma("does_not_return")
  1332. #elif \
  1333. JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
  1334. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1335. #define JSON_HEDLEY_NO_RETURN __declspec(noreturn)
  1336. #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,0,0) && defined(__cplusplus)
  1337. #define JSON_HEDLEY_NO_RETURN _Pragma("FUNC_NEVER_RETURNS;")
  1338. #elif JSON_HEDLEY_COMPCERT_VERSION_CHECK(3,2,0)
  1339. #define JSON_HEDLEY_NO_RETURN __attribute((noreturn))
  1340. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(9,0,0)
  1341. #define JSON_HEDLEY_NO_RETURN __declspec(noreturn)
  1342. #else
  1343. #define JSON_HEDLEY_NO_RETURN
  1344. #endif
  1345. #if defined(JSON_HEDLEY_NO_ESCAPE)
  1346. #undef JSON_HEDLEY_NO_ESCAPE
  1347. #endif
  1348. #if JSON_HEDLEY_HAS_ATTRIBUTE(noescape)
  1349. #define JSON_HEDLEY_NO_ESCAPE __attribute__((__noescape__))
  1350. #else
  1351. #define JSON_HEDLEY_NO_ESCAPE
  1352. #endif
  1353. #if defined(JSON_HEDLEY_UNREACHABLE)
  1354. #undef JSON_HEDLEY_UNREACHABLE
  1355. #endif
  1356. #if defined(JSON_HEDLEY_UNREACHABLE_RETURN)
  1357. #undef JSON_HEDLEY_UNREACHABLE_RETURN
  1358. #endif
  1359. #if defined(JSON_HEDLEY_ASSUME)
  1360. #undef JSON_HEDLEY_ASSUME
  1361. #endif
  1362. #if \
  1363. JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
  1364. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1365. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1366. #define JSON_HEDLEY_ASSUME(expr) __assume(expr)
  1367. #elif JSON_HEDLEY_HAS_BUILTIN(__builtin_assume)
  1368. #define JSON_HEDLEY_ASSUME(expr) __builtin_assume(expr)
  1369. #elif \
  1370. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
  1371. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0)
  1372. #if defined(__cplusplus)
  1373. #define JSON_HEDLEY_ASSUME(expr) std::_nassert(expr)
  1374. #else
  1375. #define JSON_HEDLEY_ASSUME(expr) _nassert(expr)
  1376. #endif
  1377. #endif
  1378. #if \
  1379. (JSON_HEDLEY_HAS_BUILTIN(__builtin_unreachable) && (!defined(JSON_HEDLEY_ARM_VERSION))) || \
  1380. JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \
  1381. JSON_HEDLEY_PGI_VERSION_CHECK(18,10,0) || \
  1382. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1383. JSON_HEDLEY_IBM_VERSION_CHECK(13,1,5) || \
  1384. JSON_HEDLEY_CRAY_VERSION_CHECK(10,0,0) || \
  1385. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1386. #define JSON_HEDLEY_UNREACHABLE() __builtin_unreachable()
  1387. #elif defined(JSON_HEDLEY_ASSUME)
  1388. #define JSON_HEDLEY_UNREACHABLE() JSON_HEDLEY_ASSUME(0)
  1389. #endif
  1390. #if !defined(JSON_HEDLEY_ASSUME)
  1391. #if defined(JSON_HEDLEY_UNREACHABLE)
  1392. #define JSON_HEDLEY_ASSUME(expr) JSON_HEDLEY_STATIC_CAST(void, ((expr) ? 1 : (JSON_HEDLEY_UNREACHABLE(), 1)))
  1393. #else
  1394. #define JSON_HEDLEY_ASSUME(expr) JSON_HEDLEY_STATIC_CAST(void, expr)
  1395. #endif
  1396. #endif
  1397. #if defined(JSON_HEDLEY_UNREACHABLE)
  1398. #if \
  1399. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
  1400. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0)
  1401. #define JSON_HEDLEY_UNREACHABLE_RETURN(value) return (JSON_HEDLEY_STATIC_CAST(void, JSON_HEDLEY_ASSUME(0)), (value))
  1402. #else
  1403. #define JSON_HEDLEY_UNREACHABLE_RETURN(value) JSON_HEDLEY_UNREACHABLE()
  1404. #endif
  1405. #else
  1406. #define JSON_HEDLEY_UNREACHABLE_RETURN(value) return (value)
  1407. #endif
  1408. #if !defined(JSON_HEDLEY_UNREACHABLE)
  1409. #define JSON_HEDLEY_UNREACHABLE() JSON_HEDLEY_ASSUME(0)
  1410. #endif
  1411. JSON_HEDLEY_DIAGNOSTIC_PUSH
  1412. #if JSON_HEDLEY_HAS_WARNING("-Wpedantic")
  1413. #pragma clang diagnostic ignored "-Wpedantic"
  1414. #endif
  1415. #if JSON_HEDLEY_HAS_WARNING("-Wc++98-compat-pedantic") && defined(__cplusplus)
  1416. #pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
  1417. #endif
  1418. #if JSON_HEDLEY_GCC_HAS_WARNING("-Wvariadic-macros",4,0,0)
  1419. #if defined(__clang__)
  1420. #pragma clang diagnostic ignored "-Wvariadic-macros"
  1421. #elif defined(JSON_HEDLEY_GCC_VERSION)
  1422. #pragma GCC diagnostic ignored "-Wvariadic-macros"
  1423. #endif
  1424. #endif
  1425. #if defined(JSON_HEDLEY_NON_NULL)
  1426. #undef JSON_HEDLEY_NON_NULL
  1427. #endif
  1428. #if \
  1429. JSON_HEDLEY_HAS_ATTRIBUTE(nonnull) || \
  1430. JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
  1431. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1432. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0)
  1433. #define JSON_HEDLEY_NON_NULL(...) __attribute__((__nonnull__(__VA_ARGS__)))
  1434. #else
  1435. #define JSON_HEDLEY_NON_NULL(...)
  1436. #endif
  1437. JSON_HEDLEY_DIAGNOSTIC_POP
  1438. #if defined(JSON_HEDLEY_PRINTF_FORMAT)
  1439. #undef JSON_HEDLEY_PRINTF_FORMAT
  1440. #endif
  1441. #if defined(__MINGW32__) && JSON_HEDLEY_GCC_HAS_ATTRIBUTE(format,4,4,0) && !defined(__USE_MINGW_ANSI_STDIO)
  1442. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(ms_printf, string_idx, first_to_check)))
  1443. #elif defined(__MINGW32__) && JSON_HEDLEY_GCC_HAS_ATTRIBUTE(format,4,4,0) && defined(__USE_MINGW_ANSI_STDIO)
  1444. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(gnu_printf, string_idx, first_to_check)))
  1445. #elif \
  1446. JSON_HEDLEY_HAS_ATTRIBUTE(format) || \
  1447. JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
  1448. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1449. JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) || \
  1450. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1451. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1452. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1453. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1454. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1455. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1456. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1457. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1458. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1459. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1460. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1461. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1462. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1463. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(__printf__, string_idx, first_to_check)))
  1464. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(6,0,0)
  1465. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __declspec(vaformat(printf,string_idx,first_to_check))
  1466. #else
  1467. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check)
  1468. #endif
  1469. #if defined(JSON_HEDLEY_CONSTEXPR)
  1470. #undef JSON_HEDLEY_CONSTEXPR
  1471. #endif
  1472. #if defined(__cplusplus)
  1473. #if __cplusplus >= 201103L
  1474. #define JSON_HEDLEY_CONSTEXPR JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(constexpr)
  1475. #endif
  1476. #endif
  1477. #if !defined(JSON_HEDLEY_CONSTEXPR)
  1478. #define JSON_HEDLEY_CONSTEXPR
  1479. #endif
  1480. #if defined(JSON_HEDLEY_PREDICT)
  1481. #undef JSON_HEDLEY_PREDICT
  1482. #endif
  1483. #if defined(JSON_HEDLEY_LIKELY)
  1484. #undef JSON_HEDLEY_LIKELY
  1485. #endif
  1486. #if defined(JSON_HEDLEY_UNLIKELY)
  1487. #undef JSON_HEDLEY_UNLIKELY
  1488. #endif
  1489. #if defined(JSON_HEDLEY_UNPREDICTABLE)
  1490. #undef JSON_HEDLEY_UNPREDICTABLE
  1491. #endif
  1492. #if JSON_HEDLEY_HAS_BUILTIN(__builtin_unpredictable)
  1493. #define JSON_HEDLEY_UNPREDICTABLE(expr) __builtin_unpredictable((expr))
  1494. #endif
  1495. #if \
  1496. (JSON_HEDLEY_HAS_BUILTIN(__builtin_expect_with_probability) && !defined(JSON_HEDLEY_PGI_VERSION)) || \
  1497. JSON_HEDLEY_GCC_VERSION_CHECK(9,0,0) || \
  1498. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1499. # define JSON_HEDLEY_PREDICT(expr, value, probability) __builtin_expect_with_probability( (expr), (value), (probability))
  1500. # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) __builtin_expect_with_probability(!!(expr), 1 , (probability))
  1501. # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) __builtin_expect_with_probability(!!(expr), 0 , (probability))
  1502. # define JSON_HEDLEY_LIKELY(expr) __builtin_expect (!!(expr), 1 )
  1503. # define JSON_HEDLEY_UNLIKELY(expr) __builtin_expect (!!(expr), 0 )
  1504. #elif \
  1505. (JSON_HEDLEY_HAS_BUILTIN(__builtin_expect) && !defined(JSON_HEDLEY_INTEL_CL_VERSION)) || \
  1506. JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \
  1507. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1508. (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)) || \
  1509. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1510. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1511. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1512. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,7,0) || \
  1513. JSON_HEDLEY_TI_CL430_VERSION_CHECK(3,1,0) || \
  1514. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,1,0) || \
  1515. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
  1516. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1517. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1518. JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,27) || \
  1519. JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \
  1520. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1521. # define JSON_HEDLEY_PREDICT(expr, expected, probability) \
  1522. (((probability) >= 0.9) ? __builtin_expect((expr), (expected)) : (JSON_HEDLEY_STATIC_CAST(void, expected), (expr)))
  1523. # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) \
  1524. (__extension__ ({ \
  1525. double hedley_probability_ = (probability); \
  1526. ((hedley_probability_ >= 0.9) ? __builtin_expect(!!(expr), 1) : ((hedley_probability_ <= 0.1) ? __builtin_expect(!!(expr), 0) : !!(expr))); \
  1527. }))
  1528. # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) \
  1529. (__extension__ ({ \
  1530. double hedley_probability_ = (probability); \
  1531. ((hedley_probability_ >= 0.9) ? __builtin_expect(!!(expr), 0) : ((hedley_probability_ <= 0.1) ? __builtin_expect(!!(expr), 1) : !!(expr))); \
  1532. }))
  1533. # define JSON_HEDLEY_LIKELY(expr) __builtin_expect(!!(expr), 1)
  1534. # define JSON_HEDLEY_UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  1535. #else
  1536. # define JSON_HEDLEY_PREDICT(expr, expected, probability) (JSON_HEDLEY_STATIC_CAST(void, expected), (expr))
  1537. # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) (!!(expr))
  1538. # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) (!!(expr))
  1539. # define JSON_HEDLEY_LIKELY(expr) (!!(expr))
  1540. # define JSON_HEDLEY_UNLIKELY(expr) (!!(expr))
  1541. #endif
  1542. #if !defined(JSON_HEDLEY_UNPREDICTABLE)
  1543. #define JSON_HEDLEY_UNPREDICTABLE(expr) JSON_HEDLEY_PREDICT(expr, 1, 0.5)
  1544. #endif
  1545. #if defined(JSON_HEDLEY_MALLOC)
  1546. #undef JSON_HEDLEY_MALLOC
  1547. #endif
  1548. #if \
  1549. JSON_HEDLEY_HAS_ATTRIBUTE(malloc) || \
  1550. JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
  1551. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1552. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1553. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1554. JSON_HEDLEY_IBM_VERSION_CHECK(12,1,0) || \
  1555. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1556. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1557. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1558. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1559. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1560. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1561. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1562. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1563. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1564. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1565. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1566. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1567. #define JSON_HEDLEY_MALLOC __attribute__((__malloc__))
  1568. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
  1569. #define JSON_HEDLEY_MALLOC _Pragma("returns_new_memory")
  1570. #elif \
  1571. JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
  1572. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1573. #define JSON_HEDLEY_MALLOC __declspec(restrict)
  1574. #else
  1575. #define JSON_HEDLEY_MALLOC
  1576. #endif
  1577. #if defined(JSON_HEDLEY_PURE)
  1578. #undef JSON_HEDLEY_PURE
  1579. #endif
  1580. #if \
  1581. JSON_HEDLEY_HAS_ATTRIBUTE(pure) || \
  1582. JSON_HEDLEY_GCC_VERSION_CHECK(2,96,0) || \
  1583. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1584. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1585. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1586. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1587. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1588. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1589. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1590. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1591. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1592. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1593. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1594. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1595. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1596. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1597. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1598. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1599. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1600. # define JSON_HEDLEY_PURE __attribute__((__pure__))
  1601. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
  1602. # define JSON_HEDLEY_PURE _Pragma("does_not_write_global_data")
  1603. #elif defined(__cplusplus) && \
  1604. ( \
  1605. JSON_HEDLEY_TI_CL430_VERSION_CHECK(2,0,1) || \
  1606. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0) || \
  1607. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) \
  1608. )
  1609. # define JSON_HEDLEY_PURE _Pragma("FUNC_IS_PURE;")
  1610. #else
  1611. # define JSON_HEDLEY_PURE
  1612. #endif
  1613. #if defined(JSON_HEDLEY_CONST)
  1614. #undef JSON_HEDLEY_CONST
  1615. #endif
  1616. #if \
  1617. JSON_HEDLEY_HAS_ATTRIBUTE(const) || \
  1618. JSON_HEDLEY_GCC_VERSION_CHECK(2,5,0) || \
  1619. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1620. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1621. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1622. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1623. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1624. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1625. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1626. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1627. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1628. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1629. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1630. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1631. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1632. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1633. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1634. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1635. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1636. #define JSON_HEDLEY_CONST __attribute__((__const__))
  1637. #elif \
  1638. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
  1639. #define JSON_HEDLEY_CONST _Pragma("no_side_effect")
  1640. #else
  1641. #define JSON_HEDLEY_CONST JSON_HEDLEY_PURE
  1642. #endif
  1643. #if defined(JSON_HEDLEY_RESTRICT)
  1644. #undef JSON_HEDLEY_RESTRICT
  1645. #endif
  1646. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && !defined(__cplusplus)
  1647. #define JSON_HEDLEY_RESTRICT restrict
  1648. #elif \
  1649. JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
  1650. JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
  1651. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1652. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \
  1653. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1654. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1655. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1656. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1657. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,4) || \
  1658. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,1,0) || \
  1659. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1660. (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,14,0) && defined(__cplusplus)) || \
  1661. JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \
  1662. defined(__clang__) || \
  1663. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1664. #define JSON_HEDLEY_RESTRICT __restrict
  1665. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,3,0) && !defined(__cplusplus)
  1666. #define JSON_HEDLEY_RESTRICT _Restrict
  1667. #else
  1668. #define JSON_HEDLEY_RESTRICT
  1669. #endif
  1670. #if defined(JSON_HEDLEY_INLINE)
  1671. #undef JSON_HEDLEY_INLINE
  1672. #endif
  1673. #if \
  1674. (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
  1675. (defined(__cplusplus) && (__cplusplus >= 199711L))
  1676. #define JSON_HEDLEY_INLINE inline
  1677. #elif \
  1678. defined(JSON_HEDLEY_GCC_VERSION) || \
  1679. JSON_HEDLEY_ARM_VERSION_CHECK(6,2,0)
  1680. #define JSON_HEDLEY_INLINE __inline__
  1681. #elif \
  1682. JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \
  1683. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \
  1684. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1685. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,1,0) || \
  1686. JSON_HEDLEY_TI_CL430_VERSION_CHECK(3,1,0) || \
  1687. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
  1688. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) || \
  1689. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1690. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1691. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1692. #define JSON_HEDLEY_INLINE __inline
  1693. #else
  1694. #define JSON_HEDLEY_INLINE
  1695. #endif
  1696. #if defined(JSON_HEDLEY_ALWAYS_INLINE)
  1697. #undef JSON_HEDLEY_ALWAYS_INLINE
  1698. #endif
  1699. #if \
  1700. JSON_HEDLEY_HAS_ATTRIBUTE(always_inline) || \
  1701. JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
  1702. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1703. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1704. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1705. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1706. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1707. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1708. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1709. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1710. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1711. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1712. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1713. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1714. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1715. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1716. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1717. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \
  1718. JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
  1719. # define JSON_HEDLEY_ALWAYS_INLINE __attribute__((__always_inline__)) JSON_HEDLEY_INLINE
  1720. #elif \
  1721. JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \
  1722. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1723. # define JSON_HEDLEY_ALWAYS_INLINE __forceinline
  1724. #elif defined(__cplusplus) && \
  1725. ( \
  1726. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1727. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1728. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1729. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
  1730. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1731. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) \
  1732. )
  1733. # define JSON_HEDLEY_ALWAYS_INLINE _Pragma("FUNC_ALWAYS_INLINE;")
  1734. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1735. # define JSON_HEDLEY_ALWAYS_INLINE _Pragma("inline=forced")
  1736. #else
  1737. # define JSON_HEDLEY_ALWAYS_INLINE JSON_HEDLEY_INLINE
  1738. #endif
  1739. #if defined(JSON_HEDLEY_NEVER_INLINE)
  1740. #undef JSON_HEDLEY_NEVER_INLINE
  1741. #endif
  1742. #if \
  1743. JSON_HEDLEY_HAS_ATTRIBUTE(noinline) || \
  1744. JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
  1745. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1746. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1747. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1748. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1749. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1750. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1751. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1752. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1753. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1754. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1755. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1756. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1757. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1758. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1759. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1760. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \
  1761. JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
  1762. #define JSON_HEDLEY_NEVER_INLINE __attribute__((__noinline__))
  1763. #elif \
  1764. JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
  1765. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1766. #define JSON_HEDLEY_NEVER_INLINE __declspec(noinline)
  1767. #elif JSON_HEDLEY_PGI_VERSION_CHECK(10,2,0)
  1768. #define JSON_HEDLEY_NEVER_INLINE _Pragma("noinline")
  1769. #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,0,0) && defined(__cplusplus)
  1770. #define JSON_HEDLEY_NEVER_INLINE _Pragma("FUNC_CANNOT_INLINE;")
  1771. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1772. #define JSON_HEDLEY_NEVER_INLINE _Pragma("inline=never")
  1773. #elif JSON_HEDLEY_COMPCERT_VERSION_CHECK(3,2,0)
  1774. #define JSON_HEDLEY_NEVER_INLINE __attribute((noinline))
  1775. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(9,0,0)
  1776. #define JSON_HEDLEY_NEVER_INLINE __declspec(noinline)
  1777. #else
  1778. #define JSON_HEDLEY_NEVER_INLINE
  1779. #endif
  1780. #if defined(JSON_HEDLEY_PRIVATE)
  1781. #undef JSON_HEDLEY_PRIVATE
  1782. #endif
  1783. #if defined(JSON_HEDLEY_PUBLIC)
  1784. #undef JSON_HEDLEY_PUBLIC
  1785. #endif
  1786. #if defined(JSON_HEDLEY_IMPORT)
  1787. #undef JSON_HEDLEY_IMPORT
  1788. #endif
  1789. #if defined(_WIN32) || defined(__CYGWIN__)
  1790. # define JSON_HEDLEY_PRIVATE
  1791. # define JSON_HEDLEY_PUBLIC __declspec(dllexport)
  1792. # define JSON_HEDLEY_IMPORT __declspec(dllimport)
  1793. #else
  1794. # if \
  1795. JSON_HEDLEY_HAS_ATTRIBUTE(visibility) || \
  1796. JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
  1797. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1798. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1799. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1800. JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
  1801. ( \
  1802. defined(__TI_EABI__) && \
  1803. ( \
  1804. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1805. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) \
  1806. ) \
  1807. ) || \
  1808. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1809. # define JSON_HEDLEY_PRIVATE __attribute__((__visibility__("hidden")))
  1810. # define JSON_HEDLEY_PUBLIC __attribute__((__visibility__("default")))
  1811. # else
  1812. # define JSON_HEDLEY_PRIVATE
  1813. # define JSON_HEDLEY_PUBLIC
  1814. # endif
  1815. # define JSON_HEDLEY_IMPORT extern
  1816. #endif
  1817. #if defined(JSON_HEDLEY_NO_THROW)
  1818. #undef JSON_HEDLEY_NO_THROW
  1819. #endif
  1820. #if \
  1821. JSON_HEDLEY_HAS_ATTRIBUTE(nothrow) || \
  1822. JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
  1823. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1824. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1825. #define JSON_HEDLEY_NO_THROW __attribute__((__nothrow__))
  1826. #elif \
  1827. JSON_HEDLEY_MSVC_VERSION_CHECK(13,1,0) || \
  1828. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \
  1829. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0)
  1830. #define JSON_HEDLEY_NO_THROW __declspec(nothrow)
  1831. #else
  1832. #define JSON_HEDLEY_NO_THROW
  1833. #endif
  1834. #if defined(JSON_HEDLEY_FALL_THROUGH)
  1835. #undef JSON_HEDLEY_FALL_THROUGH
  1836. #endif
  1837. #if \
  1838. JSON_HEDLEY_HAS_ATTRIBUTE(fallthrough) || \
  1839. JSON_HEDLEY_GCC_VERSION_CHECK(7,0,0) || \
  1840. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1841. #define JSON_HEDLEY_FALL_THROUGH __attribute__((__fallthrough__))
  1842. #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(clang,fallthrough)
  1843. #define JSON_HEDLEY_FALL_THROUGH JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[clang::fallthrough]])
  1844. #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE(fallthrough)
  1845. #define JSON_HEDLEY_FALL_THROUGH JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[fallthrough]])
  1846. #elif defined(__fallthrough) /* SAL */
  1847. #define JSON_HEDLEY_FALL_THROUGH __fallthrough
  1848. #else
  1849. #define JSON_HEDLEY_FALL_THROUGH
  1850. #endif
  1851. #if defined(JSON_HEDLEY_RETURNS_NON_NULL)
  1852. #undef JSON_HEDLEY_RETURNS_NON_NULL
  1853. #endif
  1854. #if \
  1855. JSON_HEDLEY_HAS_ATTRIBUTE(returns_nonnull) || \
  1856. JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) || \
  1857. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1858. #define JSON_HEDLEY_RETURNS_NON_NULL __attribute__((__returns_nonnull__))
  1859. #elif defined(_Ret_notnull_) /* SAL */
  1860. #define JSON_HEDLEY_RETURNS_NON_NULL _Ret_notnull_
  1861. #else
  1862. #define JSON_HEDLEY_RETURNS_NON_NULL
  1863. #endif
  1864. #if defined(JSON_HEDLEY_ARRAY_PARAM)
  1865. #undef JSON_HEDLEY_ARRAY_PARAM
  1866. #endif
  1867. #if \
  1868. defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
  1869. !defined(__STDC_NO_VLA__) && \
  1870. !defined(__cplusplus) && \
  1871. !defined(JSON_HEDLEY_PGI_VERSION) && \
  1872. !defined(JSON_HEDLEY_TINYC_VERSION)
  1873. #define JSON_HEDLEY_ARRAY_PARAM(name) (name)
  1874. #else
  1875. #define JSON_HEDLEY_ARRAY_PARAM(name)
  1876. #endif
  1877. #if defined(JSON_HEDLEY_IS_CONSTANT)
  1878. #undef JSON_HEDLEY_IS_CONSTANT
  1879. #endif
  1880. #if defined(JSON_HEDLEY_REQUIRE_CONSTEXPR)
  1881. #undef JSON_HEDLEY_REQUIRE_CONSTEXPR
  1882. #endif
  1883. /* JSON_HEDLEY_IS_CONSTEXPR_ is for
  1884. HEDLEY INTERNAL USE ONLY. API subject to change without notice. */
  1885. #if defined(JSON_HEDLEY_IS_CONSTEXPR_)
  1886. #undef JSON_HEDLEY_IS_CONSTEXPR_
  1887. #endif
  1888. #if \
  1889. JSON_HEDLEY_HAS_BUILTIN(__builtin_constant_p) || \
  1890. JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
  1891. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1892. JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,19) || \
  1893. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1894. JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
  1895. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
  1896. (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) && !defined(__cplusplus)) || \
  1897. JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \
  1898. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1899. #define JSON_HEDLEY_IS_CONSTANT(expr) __builtin_constant_p(expr)
  1900. #endif
  1901. #if !defined(__cplusplus)
  1902. # if \
  1903. JSON_HEDLEY_HAS_BUILTIN(__builtin_types_compatible_p) || \
  1904. JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
  1905. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1906. JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
  1907. JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \
  1908. JSON_HEDLEY_ARM_VERSION_CHECK(5,4,0) || \
  1909. JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,24)
  1910. #if defined(__INTPTR_TYPE__)
  1911. #define JSON_HEDLEY_IS_CONSTEXPR_(expr) __builtin_types_compatible_p(__typeof__((1 ? (void*) ((__INTPTR_TYPE__) ((expr) * 0)) : (int*) 0)), int*)
  1912. #else
  1913. #include <stdint.h>
  1914. #define JSON_HEDLEY_IS_CONSTEXPR_(expr) __builtin_types_compatible_p(__typeof__((1 ? (void*) ((intptr_t) ((expr) * 0)) : (int*) 0)), int*)
  1915. #endif
  1916. # elif \
  1917. ( \
  1918. defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) && \
  1919. !defined(JSON_HEDLEY_SUNPRO_VERSION) && \
  1920. !defined(JSON_HEDLEY_PGI_VERSION) && \
  1921. !defined(JSON_HEDLEY_IAR_VERSION)) || \
  1922. (JSON_HEDLEY_HAS_EXTENSION(c_generic_selections) && !defined(JSON_HEDLEY_IAR_VERSION)) || \
  1923. JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) || \
  1924. JSON_HEDLEY_INTEL_VERSION_CHECK(17,0,0) || \
  1925. JSON_HEDLEY_IBM_VERSION_CHECK(12,1,0) || \
  1926. JSON_HEDLEY_ARM_VERSION_CHECK(5,3,0)
  1927. #if defined(__INTPTR_TYPE__)
  1928. #define JSON_HEDLEY_IS_CONSTEXPR_(expr) _Generic((1 ? (void*) ((__INTPTR_TYPE__) ((expr) * 0)) : (int*) 0), int*: 1, void*: 0)
  1929. #else
  1930. #include <stdint.h>
  1931. #define JSON_HEDLEY_IS_CONSTEXPR_(expr) _Generic((1 ? (void*) ((intptr_t) * 0) : (int*) 0), int*: 1, void*: 0)
  1932. #endif
  1933. # elif \
  1934. defined(JSON_HEDLEY_GCC_VERSION) || \
  1935. defined(JSON_HEDLEY_INTEL_VERSION) || \
  1936. defined(JSON_HEDLEY_TINYC_VERSION) || \
  1937. defined(JSON_HEDLEY_TI_ARMCL_VERSION) || \
  1938. JSON_HEDLEY_TI_CL430_VERSION_CHECK(18,12,0) || \
  1939. defined(JSON_HEDLEY_TI_CL2000_VERSION) || \
  1940. defined(JSON_HEDLEY_TI_CL6X_VERSION) || \
  1941. defined(JSON_HEDLEY_TI_CL7X_VERSION) || \
  1942. defined(JSON_HEDLEY_TI_CLPRU_VERSION) || \
  1943. defined(__clang__)
  1944. # define JSON_HEDLEY_IS_CONSTEXPR_(expr) ( \
  1945. sizeof(void) != \
  1946. sizeof(*( \
  1947. 1 ? \
  1948. ((void*) ((expr) * 0L) ) : \
  1949. ((struct { char v[sizeof(void) * 2]; } *) 1) \
  1950. ) \
  1951. ) \
  1952. )
  1953. # endif
  1954. #endif
  1955. #if defined(JSON_HEDLEY_IS_CONSTEXPR_)
  1956. #if !defined(JSON_HEDLEY_IS_CONSTANT)
  1957. #define JSON_HEDLEY_IS_CONSTANT(expr) JSON_HEDLEY_IS_CONSTEXPR_(expr)
  1958. #endif
  1959. #define JSON_HEDLEY_REQUIRE_CONSTEXPR(expr) (JSON_HEDLEY_IS_CONSTEXPR_(expr) ? (expr) : (-1))
  1960. #else
  1961. #if !defined(JSON_HEDLEY_IS_CONSTANT)
  1962. #define JSON_HEDLEY_IS_CONSTANT(expr) (0)
  1963. #endif
  1964. #define JSON_HEDLEY_REQUIRE_CONSTEXPR(expr) (expr)
  1965. #endif
  1966. #if defined(JSON_HEDLEY_BEGIN_C_DECLS)
  1967. #undef JSON_HEDLEY_BEGIN_C_DECLS
  1968. #endif
  1969. #if defined(JSON_HEDLEY_END_C_DECLS)
  1970. #undef JSON_HEDLEY_END_C_DECLS
  1971. #endif
  1972. #if defined(JSON_HEDLEY_C_DECL)
  1973. #undef JSON_HEDLEY_C_DECL
  1974. #endif
  1975. #if defined(__cplusplus)
  1976. #define JSON_HEDLEY_BEGIN_C_DECLS extern "C" {
  1977. #define JSON_HEDLEY_END_C_DECLS }
  1978. #define JSON_HEDLEY_C_DECL extern "C"
  1979. #else
  1980. #define JSON_HEDLEY_BEGIN_C_DECLS
  1981. #define JSON_HEDLEY_END_C_DECLS
  1982. #define JSON_HEDLEY_C_DECL
  1983. #endif
  1984. #if defined(JSON_HEDLEY_STATIC_ASSERT)
  1985. #undef JSON_HEDLEY_STATIC_ASSERT
  1986. #endif
  1987. #if \
  1988. !defined(__cplusplus) && ( \
  1989. (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)) || \
  1990. (JSON_HEDLEY_HAS_FEATURE(c_static_assert) && !defined(JSON_HEDLEY_INTEL_CL_VERSION)) || \
  1991. JSON_HEDLEY_GCC_VERSION_CHECK(6,0,0) || \
  1992. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1993. defined(_Static_assert) \
  1994. )
  1995. # define JSON_HEDLEY_STATIC_ASSERT(expr, message) _Static_assert(expr, message)
  1996. #elif \
  1997. (defined(__cplusplus) && (__cplusplus >= 201103L)) || \
  1998. JSON_HEDLEY_MSVC_VERSION_CHECK(16,0,0) || \
  1999. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  2000. # define JSON_HEDLEY_STATIC_ASSERT(expr, message) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(static_assert(expr, message))
  2001. #else
  2002. # define JSON_HEDLEY_STATIC_ASSERT(expr, message)
  2003. #endif
  2004. #if defined(JSON_HEDLEY_NULL)
  2005. #undef JSON_HEDLEY_NULL
  2006. #endif
  2007. #if defined(__cplusplus)
  2008. #if __cplusplus >= 201103L
  2009. #define JSON_HEDLEY_NULL JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(nullptr)
  2010. #elif defined(NULL)
  2011. #define JSON_HEDLEY_NULL NULL
  2012. #else
  2013. #define JSON_HEDLEY_NULL JSON_HEDLEY_STATIC_CAST(void*, 0)
  2014. #endif
  2015. #elif defined(NULL)
  2016. #define JSON_HEDLEY_NULL NULL
  2017. #else
  2018. #define JSON_HEDLEY_NULL ((void*) 0)
  2019. #endif
  2020. #if defined(JSON_HEDLEY_MESSAGE)
  2021. #undef JSON_HEDLEY_MESSAGE
  2022. #endif
  2023. #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
  2024. # define JSON_HEDLEY_MESSAGE(msg) \
  2025. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  2026. JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS \
  2027. JSON_HEDLEY_PRAGMA(message msg) \
  2028. JSON_HEDLEY_DIAGNOSTIC_POP
  2029. #elif \
  2030. JSON_HEDLEY_GCC_VERSION_CHECK(4,4,0) || \
  2031. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  2032. # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message msg)
  2033. #elif JSON_HEDLEY_CRAY_VERSION_CHECK(5,0,0)
  2034. # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(_CRI message msg)
  2035. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  2036. # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message(msg))
  2037. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,0,0)
  2038. # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message(msg))
  2039. #else
  2040. # define JSON_HEDLEY_MESSAGE(msg)
  2041. #endif
  2042. #if defined(JSON_HEDLEY_WARNING)
  2043. #undef JSON_HEDLEY_WARNING
  2044. #endif
  2045. #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
  2046. # define JSON_HEDLEY_WARNING(msg) \
  2047. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  2048. JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS \
  2049. JSON_HEDLEY_PRAGMA(clang warning msg) \
  2050. JSON_HEDLEY_DIAGNOSTIC_POP
  2051. #elif \
  2052. JSON_HEDLEY_GCC_VERSION_CHECK(4,8,0) || \
  2053. JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \
  2054. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  2055. # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(GCC warning msg)
  2056. #elif \
  2057. JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) || \
  2058. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  2059. # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(message(msg))
  2060. #else
  2061. # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_MESSAGE(msg)
  2062. #endif
  2063. #if defined(JSON_HEDLEY_REQUIRE)
  2064. #undef JSON_HEDLEY_REQUIRE
  2065. #endif
  2066. #if defined(JSON_HEDLEY_REQUIRE_MSG)
  2067. #undef JSON_HEDLEY_REQUIRE_MSG
  2068. #endif
  2069. #if JSON_HEDLEY_HAS_ATTRIBUTE(diagnose_if)
  2070. # if JSON_HEDLEY_HAS_WARNING("-Wgcc-compat")
  2071. # define JSON_HEDLEY_REQUIRE(expr) \
  2072. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  2073. _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \
  2074. __attribute__((diagnose_if(!(expr), #expr, "error"))) \
  2075. JSON_HEDLEY_DIAGNOSTIC_POP
  2076. # define JSON_HEDLEY_REQUIRE_MSG(expr,msg) \
  2077. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  2078. _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \
  2079. __attribute__((diagnose_if(!(expr), msg, "error"))) \
  2080. JSON_HEDLEY_DIAGNOSTIC_POP
  2081. # else
  2082. # define JSON_HEDLEY_REQUIRE(expr) __attribute__((diagnose_if(!(expr), #expr, "error")))
  2083. # define JSON_HEDLEY_REQUIRE_MSG(expr,msg) __attribute__((diagnose_if(!(expr), msg, "error")))
  2084. # endif
  2085. #else
  2086. # define JSON_HEDLEY_REQUIRE(expr)
  2087. # define JSON_HEDLEY_REQUIRE_MSG(expr,msg)
  2088. #endif
  2089. #if defined(JSON_HEDLEY_FLAGS)
  2090. #undef JSON_HEDLEY_FLAGS
  2091. #endif
  2092. #if JSON_HEDLEY_HAS_ATTRIBUTE(flag_enum) && (!defined(__cplusplus) || JSON_HEDLEY_HAS_WARNING("-Wbitfield-enum-conversion"))
  2093. #define JSON_HEDLEY_FLAGS __attribute__((__flag_enum__))
  2094. #else
  2095. #define JSON_HEDLEY_FLAGS
  2096. #endif
  2097. #if defined(JSON_HEDLEY_FLAGS_CAST)
  2098. #undef JSON_HEDLEY_FLAGS_CAST
  2099. #endif
  2100. #if JSON_HEDLEY_INTEL_VERSION_CHECK(19,0,0)
  2101. # define JSON_HEDLEY_FLAGS_CAST(T, expr) (__extension__ ({ \
  2102. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  2103. _Pragma("warning(disable:188)") \
  2104. ((T) (expr)); \
  2105. JSON_HEDLEY_DIAGNOSTIC_POP \
  2106. }))
  2107. #else
  2108. # define JSON_HEDLEY_FLAGS_CAST(T, expr) JSON_HEDLEY_STATIC_CAST(T, expr)
  2109. #endif
  2110. #if defined(JSON_HEDLEY_EMPTY_BASES)
  2111. #undef JSON_HEDLEY_EMPTY_BASES
  2112. #endif
  2113. #if \
  2114. (JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,23918) && !JSON_HEDLEY_MSVC_VERSION_CHECK(20,0,0)) || \
  2115. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  2116. #define JSON_HEDLEY_EMPTY_BASES __declspec(empty_bases)
  2117. #else
  2118. #define JSON_HEDLEY_EMPTY_BASES
  2119. #endif
  2120. /* Remaining macros are deprecated. */
  2121. #if defined(JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK)
  2122. #undef JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK
  2123. #endif
  2124. #if defined(__clang__)
  2125. #define JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) (0)
  2126. #else
  2127. #define JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  2128. #endif
  2129. #if defined(JSON_HEDLEY_CLANG_HAS_ATTRIBUTE)
  2130. #undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE
  2131. #endif
  2132. #define JSON_HEDLEY_CLANG_HAS_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
  2133. #if defined(JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE)
  2134. #undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE
  2135. #endif
  2136. #define JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute)
  2137. #if defined(JSON_HEDLEY_CLANG_HAS_BUILTIN)
  2138. #undef JSON_HEDLEY_CLANG_HAS_BUILTIN
  2139. #endif
  2140. #define JSON_HEDLEY_CLANG_HAS_BUILTIN(builtin) JSON_HEDLEY_HAS_BUILTIN(builtin)
  2141. #if defined(JSON_HEDLEY_CLANG_HAS_FEATURE)
  2142. #undef JSON_HEDLEY_CLANG_HAS_FEATURE
  2143. #endif
  2144. #define JSON_HEDLEY_CLANG_HAS_FEATURE(feature) JSON_HEDLEY_HAS_FEATURE(feature)
  2145. #if defined(JSON_HEDLEY_CLANG_HAS_EXTENSION)
  2146. #undef JSON_HEDLEY_CLANG_HAS_EXTENSION
  2147. #endif
  2148. #define JSON_HEDLEY_CLANG_HAS_EXTENSION(extension) JSON_HEDLEY_HAS_EXTENSION(extension)
  2149. #if defined(JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE)
  2150. #undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE
  2151. #endif
  2152. #define JSON_HEDLEY_CLANG_HAS_DECLSPEC_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute)
  2153. #if defined(JSON_HEDLEY_CLANG_HAS_WARNING)
  2154. #undef JSON_HEDLEY_CLANG_HAS_WARNING
  2155. #endif
  2156. #define JSON_HEDLEY_CLANG_HAS_WARNING(warning) JSON_HEDLEY_HAS_WARNING(warning)
  2157. #endif /* !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < X) */
  2158. // This file contains all internal macro definitions (except those affecting ABI)
  2159. // You MUST include macro_unscope.hpp at the end of json.hpp to undef all of them
  2160. // #include <nlohmann/detail/abi_macros.hpp>
  2161. // exclude unsupported compilers
  2162. #if !defined(JSON_SKIP_UNSUPPORTED_COMPILER_CHECK)
  2163. #if defined(__clang__)
  2164. #if (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) < 30400
  2165. #error "unsupported Clang version - see https://github.com/nlohmann/json#supported-compilers"
  2166. #endif
  2167. #elif defined(__GNUC__) && !(defined(__ICC) || defined(__INTEL_COMPILER))
  2168. #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40800
  2169. #error "unsupported GCC version - see https://github.com/nlohmann/json#supported-compilers"
  2170. #endif
  2171. #endif
  2172. #endif
  2173. // C++ language standard detection
  2174. // if the user manually specified the used c++ version this is skipped
  2175. #if !defined(JSON_HAS_CPP_20) && !defined(JSON_HAS_CPP_17) && !defined(JSON_HAS_CPP_14) && !defined(JSON_HAS_CPP_11)
  2176. #if (defined(__cplusplus) && __cplusplus >= 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L)
  2177. #define JSON_HAS_CPP_20
  2178. #define JSON_HAS_CPP_17
  2179. #define JSON_HAS_CPP_14
  2180. #elif (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464
  2181. #define JSON_HAS_CPP_17
  2182. #define JSON_HAS_CPP_14
  2183. #elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1)
  2184. #define JSON_HAS_CPP_14
  2185. #endif
  2186. // the cpp 11 flag is always specified because it is the minimal required version
  2187. #define JSON_HAS_CPP_11
  2188. #endif
  2189. #ifdef __has_include
  2190. #if __has_include(<version>)
  2191. #include <version>
  2192. #endif
  2193. #endif
  2194. #if !defined(JSON_HAS_FILESYSTEM) && !defined(JSON_HAS_EXPERIMENTAL_FILESYSTEM)
  2195. #ifdef JSON_HAS_CPP_17
  2196. #if defined(__cpp_lib_filesystem)
  2197. #define JSON_HAS_FILESYSTEM 1
  2198. #elif defined(__cpp_lib_experimental_filesystem)
  2199. #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
  2200. #elif !defined(__has_include)
  2201. #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
  2202. #elif __has_include(<filesystem>)
  2203. #define JSON_HAS_FILESYSTEM 1
  2204. #elif __has_include(<experimental/filesystem>)
  2205. #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
  2206. #endif
  2207. // std::filesystem does not work on MinGW GCC 8: https://sourceforge.net/p/mingw-w64/bugs/737/
  2208. #if defined(__MINGW32__) && defined(__GNUC__) && __GNUC__ == 8
  2209. #undef JSON_HAS_FILESYSTEM
  2210. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2211. #endif
  2212. // no filesystem support before GCC 8: https://en.cppreference.com/w/cpp/compiler_support
  2213. #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 8
  2214. #undef JSON_HAS_FILESYSTEM
  2215. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2216. #endif
  2217. // no filesystem support before Clang 7: https://en.cppreference.com/w/cpp/compiler_support
  2218. #if defined(__clang_major__) && __clang_major__ < 7
  2219. #undef JSON_HAS_FILESYSTEM
  2220. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2221. #endif
  2222. // no filesystem support before MSVC 19.14: https://en.cppreference.com/w/cpp/compiler_support
  2223. #if defined(_MSC_VER) && _MSC_VER < 1914
  2224. #undef JSON_HAS_FILESYSTEM
  2225. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2226. #endif
  2227. // no filesystem support before iOS 13
  2228. #if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED < 130000
  2229. #undef JSON_HAS_FILESYSTEM
  2230. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2231. #endif
  2232. // no filesystem support before macOS Catalina
  2233. #if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED < 101500
  2234. #undef JSON_HAS_FILESYSTEM
  2235. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2236. #endif
  2237. #endif
  2238. #endif
  2239. #ifndef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2240. #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 0
  2241. #endif
  2242. #ifndef JSON_HAS_FILESYSTEM
  2243. #define JSON_HAS_FILESYSTEM 0
  2244. #endif
  2245. #ifndef JSON_HAS_THREE_WAY_COMPARISON
  2246. #if defined(__cpp_impl_three_way_comparison) && __cpp_impl_three_way_comparison >= 201907L \
  2247. && defined(__cpp_lib_three_way_comparison) && __cpp_lib_three_way_comparison >= 201907L
  2248. #define JSON_HAS_THREE_WAY_COMPARISON 1
  2249. #else
  2250. #define JSON_HAS_THREE_WAY_COMPARISON 0
  2251. #endif
  2252. #endif
  2253. #ifndef JSON_HAS_RANGES
  2254. // ranges header shipping in GCC 11.1.0 (released 2021-04-27) has syntax error
  2255. #if defined(__GLIBCXX__) && __GLIBCXX__ == 20210427
  2256. #define JSON_HAS_RANGES 0
  2257. #elif defined(__cpp_lib_ranges)
  2258. #define JSON_HAS_RANGES 1
  2259. #else
  2260. #define JSON_HAS_RANGES 0
  2261. #endif
  2262. #endif
  2263. #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. /*!
  2520. @brief macro
  2521. @def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE
  2522. @since version 3.9.0
  2523. */
  2524. #define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Type, ...) \
  2525. 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__)) } \
  2526. 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__)) }
  2527. #define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Type, ...) \
  2528. 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__)) } \
  2529. 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__)) }
  2530. // inspired from https://stackoverflow.com/a/26745591
  2531. // allows to call any std function as if (e.g. with begin):
  2532. // using std::begin; begin(x);
  2533. //
  2534. // it allows using the detected idiom to retrieve the return type
  2535. // of such an expression
  2536. #define NLOHMANN_CAN_CALL_STD_FUNC_IMPL(std_name) \
  2537. namespace detail { \
  2538. using std::std_name; \
  2539. \
  2540. template<typename... T> \
  2541. using result_of_##std_name = decltype(std_name(std::declval<T>()...)); \
  2542. } \
  2543. \
  2544. namespace detail2 { \
  2545. struct std_name##_tag \
  2546. { \
  2547. }; \
  2548. \
  2549. template<typename... T> \
  2550. std_name##_tag std_name(T&&...); \
  2551. \
  2552. template<typename... T> \
  2553. using result_of_##std_name = decltype(std_name(std::declval<T>()...)); \
  2554. \
  2555. template<typename... T> \
  2556. struct would_call_std_##std_name \
  2557. { \
  2558. static constexpr auto const value = ::nlohmann::detail:: \
  2559. is_detected_exact<std_name##_tag, result_of_##std_name, T...>::value; \
  2560. }; \
  2561. } /* namespace detail2 */ \
  2562. \
  2563. template<typename... T> \
  2564. struct would_call_std_##std_name : detail2::would_call_std_##std_name<T...> \
  2565. { \
  2566. }
  2567. #ifndef JSON_USE_IMPLICIT_CONVERSIONS
  2568. #define JSON_USE_IMPLICIT_CONVERSIONS 1
  2569. #endif
  2570. #if JSON_USE_IMPLICIT_CONVERSIONS
  2571. #define JSON_EXPLICIT
  2572. #else
  2573. #define JSON_EXPLICIT explicit
  2574. #endif
  2575. #ifndef JSON_DISABLE_ENUM_SERIALIZATION
  2576. #define JSON_DISABLE_ENUM_SERIALIZATION 0
  2577. #endif
  2578. #ifndef JSON_USE_GLOBAL_UDLS
  2579. #define JSON_USE_GLOBAL_UDLS 1
  2580. #endif
  2581. #if JSON_HAS_THREE_WAY_COMPARISON
  2582. #include <compare> // partial_ordering
  2583. #endif
  2584. NLOHMANN_JSON_NAMESPACE_BEGIN
  2585. namespace detail
  2586. {
  2587. ///////////////////////////
  2588. // JSON type enumeration //
  2589. ///////////////////////////
  2590. /*!
  2591. @brief the JSON type enumeration
  2592. This enumeration collects the different JSON types. It is internally used to
  2593. distinguish the stored values, and the functions @ref basic_json::is_null(),
  2594. @ref basic_json::is_object(), @ref basic_json::is_array(),
  2595. @ref basic_json::is_string(), @ref basic_json::is_boolean(),
  2596. @ref basic_json::is_number() (with @ref basic_json::is_number_integer(),
  2597. @ref basic_json::is_number_unsigned(), and @ref basic_json::is_number_float()),
  2598. @ref basic_json::is_discarded(), @ref basic_json::is_primitive(), and
  2599. @ref basic_json::is_structured() rely on it.
  2600. @note There are three enumeration entries (number_integer, number_unsigned, and
  2601. number_float), because the library distinguishes these three types for numbers:
  2602. @ref basic_json::number_unsigned_t is used for unsigned integers,
  2603. @ref basic_json::number_integer_t is used for signed integers, and
  2604. @ref basic_json::number_float_t is used for floating-point numbers or to
  2605. approximate integers which do not fit in the limits of their respective type.
  2606. @sa see @ref basic_json::basic_json(const value_t value_type) -- create a JSON
  2607. value with the default value for a given type
  2608. @since version 1.0.0
  2609. */
  2610. enum class value_t : std::uint8_t
  2611. {
  2612. null, ///< null value
  2613. object, ///< object (unordered set of name/value pairs)
  2614. array, ///< array (ordered collection of values)
  2615. string, ///< string value
  2616. boolean, ///< boolean value
  2617. number_integer, ///< number value (signed integer)
  2618. number_unsigned, ///< number value (unsigned integer)
  2619. number_float, ///< number value (floating-point)
  2620. binary, ///< binary array (ordered collection of bytes)
  2621. discarded ///< discarded by the parser callback function
  2622. };
  2623. /*!
  2624. @brief comparison operator for JSON types
  2625. Returns an ordering that is similar to Python:
  2626. - order: null < boolean < number < object < array < string < binary
  2627. - furthermore, each type is not smaller than itself
  2628. - discarded values are not comparable
  2629. - binary is represented as a b"" string in python and directly comparable to a
  2630. string; however, making a binary array directly comparable with a string would
  2631. be surprising behavior in a JSON file.
  2632. @since version 1.0.0
  2633. */
  2634. #if JSON_HAS_THREE_WAY_COMPARISON
  2635. inline std::partial_ordering operator<=>(const value_t lhs, const value_t rhs) noexcept // *NOPAD*
  2636. #else
  2637. inline bool operator<(const value_t lhs, const value_t rhs) noexcept
  2638. #endif
  2639. {
  2640. static constexpr std::array<std::uint8_t, 9> order = {{
  2641. 0 /* null */, 3 /* object */, 4 /* array */, 5 /* string */,
  2642. 1 /* boolean */, 2 /* integer */, 2 /* unsigned */, 2 /* float */,
  2643. 6 /* binary */
  2644. }
  2645. };
  2646. const auto l_index = static_cast<std::size_t>(lhs);
  2647. const auto r_index = static_cast<std::size_t>(rhs);
  2648. #if JSON_HAS_THREE_WAY_COMPARISON
  2649. if (l_index < order.size() && r_index < order.size())
  2650. {
  2651. return order[l_index] <=> order[r_index]; // *NOPAD*
  2652. }
  2653. return std::partial_ordering::unordered;
  2654. #else
  2655. return l_index < order.size() && r_index < order.size() && order[l_index] < order[r_index];
  2656. #endif
  2657. }
  2658. // GCC selects the built-in operator< over an operator rewritten from
  2659. // a user-defined spaceship operator
  2660. // Clang, MSVC, and ICC select the rewritten candidate
  2661. // (see GCC bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105200)
  2662. #if JSON_HAS_THREE_WAY_COMPARISON && defined(__GNUC__)
  2663. inline bool operator<(const value_t lhs, const value_t rhs) noexcept
  2664. {
  2665. return std::is_lt(lhs <=> rhs); // *NOPAD*
  2666. }
  2667. #endif
  2668. } // namespace detail
  2669. NLOHMANN_JSON_NAMESPACE_END
  2670. // #include <nlohmann/detail/string_escape.hpp>
  2671. // __ _____ _____ _____
  2672. // __| | __| | | | JSON for Modern C++
  2673. // | | |__ | | | | | | version 3.11.2
  2674. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2675. //
  2676. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  2677. // SPDX-License-Identifier: MIT
  2678. // #include <nlohmann/detail/abi_macros.hpp>
  2679. NLOHMANN_JSON_NAMESPACE_BEGIN
  2680. namespace detail
  2681. {
  2682. /*!
  2683. @brief replace all occurrences of a substring by another string
  2684. @param[in,out] s the string to manipulate; changed so that all
  2685. occurrences of @a f are replaced with @a t
  2686. @param[in] f the substring to replace with @a t
  2687. @param[in] t the string to replace @a f
  2688. @pre The search string @a f must not be empty. **This precondition is
  2689. enforced with an assertion.**
  2690. @since version 2.0.0
  2691. */
  2692. template<typename StringType>
  2693. inline void replace_substring(StringType& s, const StringType& f,
  2694. const StringType& t)
  2695. {
  2696. JSON_ASSERT(!f.empty());
  2697. for (auto pos = s.find(f); // find first occurrence of f
  2698. pos != StringType::npos; // make sure f was found
  2699. s.replace(pos, f.size(), t), // replace with t, and
  2700. pos = s.find(f, pos + t.size())) // find next occurrence of f
  2701. {}
  2702. }
  2703. /*!
  2704. * @brief string escaping as described in RFC 6901 (Sect. 4)
  2705. * @param[in] s string to escape
  2706. * @return escaped string
  2707. *
  2708. * Note the order of escaping "~" to "~0" and "/" to "~1" is important.
  2709. */
  2710. template<typename StringType>
  2711. inline StringType escape(StringType s)
  2712. {
  2713. replace_substring(s, StringType{"~"}, StringType{"~0"});
  2714. replace_substring(s, StringType{"/"}, StringType{"~1"});
  2715. return s;
  2716. }
  2717. /*!
  2718. * @brief string unescaping as described in RFC 6901 (Sect. 4)
  2719. * @param[in] s string to unescape
  2720. * @return unescaped string
  2721. *
  2722. * Note the order of escaping "~1" to "/" and "~0" to "~" is important.
  2723. */
  2724. template<typename StringType>
  2725. static void unescape(StringType& s)
  2726. {
  2727. replace_substring(s, StringType{"~1"}, StringType{"/"});
  2728. replace_substring(s, StringType{"~0"}, StringType{"~"});
  2729. }
  2730. } // namespace detail
  2731. NLOHMANN_JSON_NAMESPACE_END
  2732. // #include <nlohmann/detail/input/position_t.hpp>
  2733. // __ _____ _____ _____
  2734. // __| | __| | | | JSON for Modern C++
  2735. // | | |__ | | | | | | version 3.11.2
  2736. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2737. //
  2738. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  2739. // SPDX-License-Identifier: MIT
  2740. #include <cstddef> // size_t
  2741. // #include <nlohmann/detail/abi_macros.hpp>
  2742. NLOHMANN_JSON_NAMESPACE_BEGIN
  2743. namespace detail
  2744. {
  2745. /// struct to capture the start position of the current token
  2746. struct position_t
  2747. {
  2748. /// the total number of characters read
  2749. std::size_t chars_read_total = 0;
  2750. /// the number of characters read in the current line
  2751. std::size_t chars_read_current_line = 0;
  2752. /// the number of lines read
  2753. std::size_t lines_read = 0;
  2754. /// conversion to size_t to preserve SAX interface
  2755. constexpr operator size_t() const
  2756. {
  2757. return chars_read_total;
  2758. }
  2759. };
  2760. } // namespace detail
  2761. NLOHMANN_JSON_NAMESPACE_END
  2762. // #include <nlohmann/detail/macro_scope.hpp>
  2763. // #include <nlohmann/detail/meta/cpp_future.hpp>
  2764. // __ _____ _____ _____
  2765. // __| | __| | | | JSON for Modern C++
  2766. // | | |__ | | | | | | version 3.11.2
  2767. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2768. //
  2769. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  2770. // SPDX-FileCopyrightText: 2018 The Abseil Authors
  2771. // SPDX-License-Identifier: MIT
  2772. #include <array> // array
  2773. #include <cstddef> // size_t
  2774. #include <type_traits> // conditional, enable_if, false_type, integral_constant, is_constructible, is_integral, is_same, remove_cv, remove_reference, true_type
  2775. #include <utility> // index_sequence, make_index_sequence, index_sequence_for
  2776. // #include <nlohmann/detail/macro_scope.hpp>
  2777. NLOHMANN_JSON_NAMESPACE_BEGIN
  2778. namespace detail
  2779. {
  2780. template<typename T>
  2781. using uncvref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
  2782. #ifdef JSON_HAS_CPP_14
  2783. // the following utilities are natively available in C++14
  2784. using std::enable_if_t;
  2785. using std::index_sequence;
  2786. using std::make_index_sequence;
  2787. using std::index_sequence_for;
  2788. #else
  2789. // alias templates to reduce boilerplate
  2790. template<bool B, typename T = void>
  2791. using enable_if_t = typename std::enable_if<B, T>::type;
  2792. // The following code is taken from https://github.com/abseil/abseil-cpp/blob/10cb35e459f5ecca5b2ff107635da0bfa41011b4/absl/utility/utility.h
  2793. // which is part of Google Abseil (https://github.com/abseil/abseil-cpp), licensed under the Apache License 2.0.
  2794. //// START OF CODE FROM GOOGLE ABSEIL
  2795. // integer_sequence
  2796. //
  2797. // Class template representing a compile-time integer sequence. An instantiation
  2798. // of `integer_sequence<T, Ints...>` has a sequence of integers encoded in its
  2799. // type through its template arguments (which is a common need when
  2800. // working with C++11 variadic templates). `absl::integer_sequence` is designed
  2801. // to be a drop-in replacement for C++14's `std::integer_sequence`.
  2802. //
  2803. // Example:
  2804. //
  2805. // template< class T, T... Ints >
  2806. // void user_function(integer_sequence<T, Ints...>);
  2807. //
  2808. // int main()
  2809. // {
  2810. // // user_function's `T` will be deduced to `int` and `Ints...`
  2811. // // will be deduced to `0, 1, 2, 3, 4`.
  2812. // user_function(make_integer_sequence<int, 5>());
  2813. // }
  2814. template <typename T, T... Ints>
  2815. struct integer_sequence
  2816. {
  2817. using value_type = T;
  2818. static constexpr std::size_t size() noexcept
  2819. {
  2820. return sizeof...(Ints);
  2821. }
  2822. };
  2823. // index_sequence
  2824. //
  2825. // A helper template for an `integer_sequence` of `size_t`,
  2826. // `absl::index_sequence` is designed to be a drop-in replacement for C++14's
  2827. // `std::index_sequence`.
  2828. template <size_t... Ints>
  2829. using index_sequence = integer_sequence<size_t, Ints...>;
  2830. namespace utility_internal
  2831. {
  2832. template <typename Seq, size_t SeqSize, size_t Rem>
  2833. struct Extend;
  2834. // Note that SeqSize == sizeof...(Ints). It's passed explicitly for efficiency.
  2835. template <typename T, T... Ints, size_t SeqSize>
  2836. struct Extend<integer_sequence<T, Ints...>, SeqSize, 0>
  2837. {
  2838. using type = integer_sequence < T, Ints..., (Ints + SeqSize)... >;
  2839. };
  2840. template <typename T, T... Ints, size_t SeqSize>
  2841. struct Extend<integer_sequence<T, Ints...>, SeqSize, 1>
  2842. {
  2843. using type = integer_sequence < T, Ints..., (Ints + SeqSize)..., 2 * SeqSize >;
  2844. };
  2845. // Recursion helper for 'make_integer_sequence<T, N>'.
  2846. // 'Gen<T, N>::type' is an alias for 'integer_sequence<T, 0, 1, ... N-1>'.
  2847. template <typename T, size_t N>
  2848. struct Gen
  2849. {
  2850. using type =
  2851. typename Extend < typename Gen < T, N / 2 >::type, N / 2, N % 2 >::type;
  2852. };
  2853. template <typename T>
  2854. struct Gen<T, 0>
  2855. {
  2856. using type = integer_sequence<T>;
  2857. };
  2858. } // namespace utility_internal
  2859. // Compile-time sequences of integers
  2860. // make_integer_sequence
  2861. //
  2862. // This template alias is equivalent to
  2863. // `integer_sequence<int, 0, 1, ..., N-1>`, and is designed to be a drop-in
  2864. // replacement for C++14's `std::make_integer_sequence`.
  2865. template <typename T, T N>
  2866. using make_integer_sequence = typename utility_internal::Gen<T, N>::type;
  2867. // make_index_sequence
  2868. //
  2869. // This template alias is equivalent to `index_sequence<0, 1, ..., N-1>`,
  2870. // and is designed to be a drop-in replacement for C++14's
  2871. // `std::make_index_sequence`.
  2872. template <size_t N>
  2873. using make_index_sequence = make_integer_sequence<size_t, N>;
  2874. // index_sequence_for
  2875. //
  2876. // Converts a typename pack into an index sequence of the same length, and
  2877. // is designed to be a drop-in replacement for C++14's
  2878. // `std::index_sequence_for()`
  2879. template <typename... Ts>
  2880. using index_sequence_for = make_index_sequence<sizeof...(Ts)>;
  2881. //// END OF CODE FROM GOOGLE ABSEIL
  2882. #endif
  2883. // dispatch utility (taken from ranges-v3)
  2884. template<unsigned N> struct priority_tag : priority_tag < N - 1 > {};
  2885. template<> struct priority_tag<0> {};
  2886. // taken from ranges-v3
  2887. template<typename T>
  2888. struct static_const
  2889. {
  2890. static JSON_INLINE_VARIABLE constexpr T value{};
  2891. };
  2892. #ifndef JSON_HAS_CPP_17
  2893. template<typename T>
  2894. constexpr T static_const<T>::value;
  2895. #endif
  2896. template<typename T, typename... Args>
  2897. inline constexpr std::array<T, sizeof...(Args)> make_array(Args&& ... args)
  2898. {
  2899. return std::array<T, sizeof...(Args)> {{static_cast<T>(std::forward<Args>(args))...}};
  2900. }
  2901. } // namespace detail
  2902. NLOHMANN_JSON_NAMESPACE_END
  2903. // #include <nlohmann/detail/meta/type_traits.hpp>
  2904. // __ _____ _____ _____
  2905. // __| | __| | | | JSON for Modern C++
  2906. // | | |__ | | | | | | version 3.11.2
  2907. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2908. //
  2909. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  2910. // SPDX-License-Identifier: MIT
  2911. #include <limits> // numeric_limits
  2912. #include <type_traits> // false_type, is_constructible, is_integral, is_same, true_type
  2913. #include <utility> // declval
  2914. #include <tuple> // tuple
  2915. // #include <nlohmann/detail/iterators/iterator_traits.hpp>
  2916. // __ _____ _____ _____
  2917. // __| | __| | | | JSON for Modern C++
  2918. // | | |__ | | | | | | version 3.11.2
  2919. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2920. //
  2921. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  2922. // SPDX-License-Identifier: MIT
  2923. #include <iterator> // random_access_iterator_tag
  2924. // #include <nlohmann/detail/abi_macros.hpp>
  2925. // #include <nlohmann/detail/meta/void_t.hpp>
  2926. // #include <nlohmann/detail/meta/cpp_future.hpp>
  2927. NLOHMANN_JSON_NAMESPACE_BEGIN
  2928. namespace detail
  2929. {
  2930. template<typename It, typename = void>
  2931. struct iterator_types {};
  2932. template<typename It>
  2933. struct iterator_types <
  2934. It,
  2935. void_t<typename It::difference_type, typename It::value_type, typename It::pointer,
  2936. typename It::reference, typename It::iterator_category >>
  2937. {
  2938. using difference_type = typename It::difference_type;
  2939. using value_type = typename It::value_type;
  2940. using pointer = typename It::pointer;
  2941. using reference = typename It::reference;
  2942. using iterator_category = typename It::iterator_category;
  2943. };
  2944. // This is required as some compilers implement std::iterator_traits in a way that
  2945. // doesn't work with SFINAE. See https://github.com/nlohmann/json/issues/1341.
  2946. template<typename T, typename = void>
  2947. struct iterator_traits
  2948. {
  2949. };
  2950. template<typename T>
  2951. struct iterator_traits < T, enable_if_t < !std::is_pointer<T>::value >>
  2952. : iterator_types<T>
  2953. {
  2954. };
  2955. template<typename T>
  2956. struct iterator_traits<T*, enable_if_t<std::is_object<T>::value>>
  2957. {
  2958. using iterator_category = std::random_access_iterator_tag;
  2959. using value_type = T;
  2960. using difference_type = ptrdiff_t;
  2961. using pointer = T*;
  2962. using reference = T&;
  2963. };
  2964. } // namespace detail
  2965. NLOHMANN_JSON_NAMESPACE_END
  2966. // #include <nlohmann/detail/macro_scope.hpp>
  2967. // #include <nlohmann/detail/meta/call_std/begin.hpp>
  2968. // __ _____ _____ _____
  2969. // __| | __| | | | JSON for Modern C++
  2970. // | | |__ | | | | | | version 3.11.2
  2971. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2972. //
  2973. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  2974. // SPDX-License-Identifier: MIT
  2975. // #include <nlohmann/detail/macro_scope.hpp>
  2976. NLOHMANN_JSON_NAMESPACE_BEGIN
  2977. NLOHMANN_CAN_CALL_STD_FUNC_IMPL(begin);
  2978. NLOHMANN_JSON_NAMESPACE_END
  2979. // #include <nlohmann/detail/meta/call_std/end.hpp>
  2980. // __ _____ _____ _____
  2981. // __| | __| | | | JSON for Modern C++
  2982. // | | |__ | | | | | | version 3.11.2
  2983. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2984. //
  2985. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  2986. // SPDX-License-Identifier: MIT
  2987. // #include <nlohmann/detail/macro_scope.hpp>
  2988. NLOHMANN_JSON_NAMESPACE_BEGIN
  2989. NLOHMANN_CAN_CALL_STD_FUNC_IMPL(end);
  2990. NLOHMANN_JSON_NAMESPACE_END
  2991. // #include <nlohmann/detail/meta/cpp_future.hpp>
  2992. // #include <nlohmann/detail/meta/detected.hpp>
  2993. // #include <nlohmann/json_fwd.hpp>
  2994. // __ _____ _____ _____
  2995. // __| | __| | | | JSON for Modern C++
  2996. // | | |__ | | | | | | version 3.11.2
  2997. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2998. //
  2999. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  3000. // SPDX-License-Identifier: MIT
  3001. #ifndef INCLUDE_NLOHMANN_JSON_FWD_HPP_
  3002. #define INCLUDE_NLOHMANN_JSON_FWD_HPP_
  3003. #include <cstdint> // int64_t, uint64_t
  3004. #include <map> // map
  3005. #include <memory> // allocator
  3006. #include <string> // string
  3007. #include <vector> // vector
  3008. // #include <nlohmann/detail/abi_macros.hpp>
  3009. /*!
  3010. @brief namespace for Niels Lohmann
  3011. @see https://github.com/nlohmann
  3012. @since version 1.0.0
  3013. */
  3014. NLOHMANN_JSON_NAMESPACE_BEGIN
  3015. /*!
  3016. @brief default JSONSerializer template argument
  3017. This serializer ignores the template arguments and uses ADL
  3018. ([argument-dependent lookup](https://en.cppreference.com/w/cpp/language/adl))
  3019. for serialization.
  3020. */
  3021. template<typename T = void, typename SFINAE = void>
  3022. struct adl_serializer;
  3023. /// a class to store JSON values
  3024. /// @sa https://json.nlohmann.me/api/basic_json/
  3025. template<template<typename U, typename V, typename... Args> class ObjectType =
  3026. std::map,
  3027. template<typename U, typename... Args> class ArrayType = std::vector,
  3028. class StringType = std::string, class BooleanType = bool,
  3029. class NumberIntegerType = std::int64_t,
  3030. class NumberUnsignedType = std::uint64_t,
  3031. class NumberFloatType = double,
  3032. template<typename U> class AllocatorType = std::allocator,
  3033. template<typename T, typename SFINAE = void> class JSONSerializer =
  3034. adl_serializer,
  3035. class BinaryType = std::vector<std::uint8_t>, // cppcheck-suppress syntaxError
  3036. class CustomBaseClass = void>
  3037. class basic_json;
  3038. /// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document
  3039. /// @sa https://json.nlohmann.me/api/json_pointer/
  3040. template<typename RefStringType>
  3041. class json_pointer;
  3042. /*!
  3043. @brief default specialization
  3044. @sa https://json.nlohmann.me/api/json/
  3045. */
  3046. using json = basic_json<>;
  3047. /// @brief a minimal map-like container that preserves insertion order
  3048. /// @sa https://json.nlohmann.me/api/ordered_map/
  3049. template<class Key, class T, class IgnoredLess, class Allocator>
  3050. struct ordered_map;
  3051. /// @brief specialization that maintains the insertion order of object keys
  3052. /// @sa https://json.nlohmann.me/api/ordered_json/
  3053. using ordered_json = basic_json<nlohmann::ordered_map>;
  3054. NLOHMANN_JSON_NAMESPACE_END
  3055. #endif // INCLUDE_NLOHMANN_JSON_FWD_HPP_
  3056. NLOHMANN_JSON_NAMESPACE_BEGIN
  3057. /*!
  3058. @brief detail namespace with internal helper functions
  3059. This namespace collects functions that should not be exposed,
  3060. implementations of some @ref basic_json methods, and meta-programming helpers.
  3061. @since version 2.1.0
  3062. */
  3063. namespace detail
  3064. {
  3065. /////////////
  3066. // helpers //
  3067. /////////////
  3068. // Note to maintainers:
  3069. //
  3070. // Every trait in this file expects a non CV-qualified type.
  3071. // The only exceptions are in the 'aliases for detected' section
  3072. // (i.e. those of the form: decltype(T::member_function(std::declval<T>())))
  3073. //
  3074. // In this case, T has to be properly CV-qualified to constraint the function arguments
  3075. // (e.g. to_json(BasicJsonType&, const T&))
  3076. template<typename> struct is_basic_json : std::false_type {};
  3077. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  3078. struct is_basic_json<NLOHMANN_BASIC_JSON_TPL> : std::true_type {};
  3079. // used by exceptions create() member functions
  3080. // true_type for pointer to possibly cv-qualified basic_json or std::nullptr_t
  3081. // false_type otherwise
  3082. template<typename BasicJsonContext>
  3083. struct is_basic_json_context :
  3084. std::integral_constant < bool,
  3085. is_basic_json<typename std::remove_cv<typename std::remove_pointer<BasicJsonContext>::type>::type>::value
  3086. || std::is_same<BasicJsonContext, std::nullptr_t>::value >
  3087. {};
  3088. //////////////////////
  3089. // json_ref helpers //
  3090. //////////////////////
  3091. template<typename>
  3092. class json_ref;
  3093. template<typename>
  3094. struct is_json_ref : std::false_type {};
  3095. template<typename T>
  3096. struct is_json_ref<json_ref<T>> : std::true_type {};
  3097. //////////////////////////
  3098. // aliases for detected //
  3099. //////////////////////////
  3100. template<typename T>
  3101. using mapped_type_t = typename T::mapped_type;
  3102. template<typename T>
  3103. using key_type_t = typename T::key_type;
  3104. template<typename T>
  3105. using value_type_t = typename T::value_type;
  3106. template<typename T>
  3107. using difference_type_t = typename T::difference_type;
  3108. template<typename T>
  3109. using pointer_t = typename T::pointer;
  3110. template<typename T>
  3111. using reference_t = typename T::reference;
  3112. template<typename T>
  3113. using iterator_category_t = typename T::iterator_category;
  3114. template<typename T, typename... Args>
  3115. using to_json_function = decltype(T::to_json(std::declval<Args>()...));
  3116. template<typename T, typename... Args>
  3117. using from_json_function = decltype(T::from_json(std::declval<Args>()...));
  3118. template<typename T, typename U>
  3119. using get_template_function = decltype(std::declval<T>().template get<U>());
  3120. // trait checking if JSONSerializer<T>::from_json(json const&, udt&) exists
  3121. template<typename BasicJsonType, typename T, typename = void>
  3122. struct has_from_json : std::false_type {};
  3123. // trait checking if j.get<T> is valid
  3124. // use this trait instead of std::is_constructible or std::is_convertible,
  3125. // both rely on, or make use of implicit conversions, and thus fail when T
  3126. // has several constructors/operator= (see https://github.com/nlohmann/json/issues/958)
  3127. template <typename BasicJsonType, typename T>
  3128. struct is_getable
  3129. {
  3130. static constexpr bool value = is_detected<get_template_function, const BasicJsonType&, T>::value;
  3131. };
  3132. template<typename BasicJsonType, typename T>
  3133. struct has_from_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
  3134. {
  3135. using serializer = typename BasicJsonType::template json_serializer<T, void>;
  3136. static constexpr bool value =
  3137. is_detected_exact<void, from_json_function, serializer,
  3138. const BasicJsonType&, T&>::value;
  3139. };
  3140. // This trait checks if JSONSerializer<T>::from_json(json const&) exists
  3141. // this overload is used for non-default-constructible user-defined-types
  3142. template<typename BasicJsonType, typename T, typename = void>
  3143. struct has_non_default_from_json : std::false_type {};
  3144. template<typename BasicJsonType, typename T>
  3145. struct has_non_default_from_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
  3146. {
  3147. using serializer = typename BasicJsonType::template json_serializer<T, void>;
  3148. static constexpr bool value =
  3149. is_detected_exact<T, from_json_function, serializer,
  3150. const BasicJsonType&>::value;
  3151. };
  3152. // This trait checks if BasicJsonType::json_serializer<T>::to_json exists
  3153. // Do not evaluate the trait when T is a basic_json type, to avoid template instantiation infinite recursion.
  3154. template<typename BasicJsonType, typename T, typename = void>
  3155. struct has_to_json : std::false_type {};
  3156. template<typename BasicJsonType, typename T>
  3157. struct has_to_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
  3158. {
  3159. using serializer = typename BasicJsonType::template json_serializer<T, void>;
  3160. static constexpr bool value =
  3161. is_detected_exact<void, to_json_function, serializer, BasicJsonType&,
  3162. T>::value;
  3163. };
  3164. template<typename T>
  3165. using detect_key_compare = typename T::key_compare;
  3166. template<typename T>
  3167. struct has_key_compare : std::integral_constant<bool, is_detected<detect_key_compare, T>::value> {};
  3168. // obtains the actual object key comparator
  3169. template<typename BasicJsonType>
  3170. struct actual_object_comparator
  3171. {
  3172. using object_t = typename BasicJsonType::object_t;
  3173. using object_comparator_t = typename BasicJsonType::default_object_comparator_t;
  3174. using type = typename std::conditional < has_key_compare<object_t>::value,
  3175. typename object_t::key_compare, object_comparator_t>::type;
  3176. };
  3177. template<typename BasicJsonType>
  3178. using actual_object_comparator_t = typename actual_object_comparator<BasicJsonType>::type;
  3179. ///////////////////
  3180. // is_ functions //
  3181. ///////////////////
  3182. // https://en.cppreference.com/w/cpp/types/conjunction
  3183. template<class...> struct conjunction : std::true_type { };
  3184. template<class B> struct conjunction<B> : B { };
  3185. template<class B, class... Bn>
  3186. struct conjunction<B, Bn...>
  3187. : std::conditional<static_cast<bool>(B::value), conjunction<Bn...>, B>::type {};
  3188. // https://en.cppreference.com/w/cpp/types/negation
  3189. template<class B> struct negation : std::integral_constant < bool, !B::value > { };
  3190. // Reimplementation of is_constructible and is_default_constructible, due to them being broken for
  3191. // std::pair and std::tuple until LWG 2367 fix (see https://cplusplus.github.io/LWG/lwg-defects.html#2367).
  3192. // This causes compile errors in e.g. clang 3.5 or gcc 4.9.
  3193. template <typename T>
  3194. struct is_default_constructible : std::is_default_constructible<T> {};
  3195. template <typename T1, typename T2>
  3196. struct is_default_constructible<std::pair<T1, T2>>
  3197. : conjunction<is_default_constructible<T1>, is_default_constructible<T2>> {};
  3198. template <typename T1, typename T2>
  3199. struct is_default_constructible<const std::pair<T1, T2>>
  3200. : conjunction<is_default_constructible<T1>, is_default_constructible<T2>> {};
  3201. template <typename... Ts>
  3202. struct is_default_constructible<std::tuple<Ts...>>
  3203. : conjunction<is_default_constructible<Ts>...> {};
  3204. template <typename... Ts>
  3205. struct is_default_constructible<const std::tuple<Ts...>>
  3206. : conjunction<is_default_constructible<Ts>...> {};
  3207. template <typename T, typename... Args>
  3208. struct is_constructible : std::is_constructible<T, Args...> {};
  3209. template <typename T1, typename T2>
  3210. struct is_constructible<std::pair<T1, T2>> : is_default_constructible<std::pair<T1, T2>> {};
  3211. template <typename T1, typename T2>
  3212. struct is_constructible<const std::pair<T1, T2>> : is_default_constructible<const std::pair<T1, T2>> {};
  3213. template <typename... Ts>
  3214. struct is_constructible<std::tuple<Ts...>> : is_default_constructible<std::tuple<Ts...>> {};
  3215. template <typename... Ts>
  3216. struct is_constructible<const std::tuple<Ts...>> : is_default_constructible<const std::tuple<Ts...>> {};
  3217. template<typename T, typename = void>
  3218. struct is_iterator_traits : std::false_type {};
  3219. template<typename T>
  3220. struct is_iterator_traits<iterator_traits<T>>
  3221. {
  3222. private:
  3223. using traits = iterator_traits<T>;
  3224. public:
  3225. static constexpr auto value =
  3226. is_detected<value_type_t, traits>::value &&
  3227. is_detected<difference_type_t, traits>::value &&
  3228. is_detected<pointer_t, traits>::value &&
  3229. is_detected<iterator_category_t, traits>::value &&
  3230. is_detected<reference_t, traits>::value;
  3231. };
  3232. template<typename T>
  3233. struct is_range
  3234. {
  3235. private:
  3236. using t_ref = typename std::add_lvalue_reference<T>::type;
  3237. using iterator = detected_t<result_of_begin, t_ref>;
  3238. using sentinel = detected_t<result_of_end, t_ref>;
  3239. // to be 100% correct, it should use https://en.cppreference.com/w/cpp/iterator/input_or_output_iterator
  3240. // and https://en.cppreference.com/w/cpp/iterator/sentinel_for
  3241. // but reimplementing these would be too much work, as a lot of other concepts are used underneath
  3242. static constexpr auto is_iterator_begin =
  3243. is_iterator_traits<iterator_traits<iterator>>::value;
  3244. public:
  3245. static constexpr bool value = !std::is_same<iterator, nonesuch>::value && !std::is_same<sentinel, nonesuch>::value && is_iterator_begin;
  3246. };
  3247. template<typename R>
  3248. using iterator_t = enable_if_t<is_range<R>::value, result_of_begin<decltype(std::declval<R&>())>>;
  3249. template<typename T>
  3250. using range_value_t = value_type_t<iterator_traits<iterator_t<T>>>;
  3251. // The following implementation of is_complete_type is taken from
  3252. // https://blogs.msdn.microsoft.com/vcblog/2015/12/02/partial-support-for-expression-sfinae-in-vs-2015-update-1/
  3253. // and is written by Xiang Fan who agreed to using it in this library.
  3254. template<typename T, typename = void>
  3255. struct is_complete_type : std::false_type {};
  3256. template<typename T>
  3257. struct is_complete_type<T, decltype(void(sizeof(T)))> : std::true_type {};
  3258. template<typename BasicJsonType, typename CompatibleObjectType,
  3259. typename = void>
  3260. struct is_compatible_object_type_impl : std::false_type {};
  3261. template<typename BasicJsonType, typename CompatibleObjectType>
  3262. struct is_compatible_object_type_impl <
  3263. BasicJsonType, CompatibleObjectType,
  3264. enable_if_t < is_detected<mapped_type_t, CompatibleObjectType>::value&&
  3265. is_detected<key_type_t, CompatibleObjectType>::value >>
  3266. {
  3267. using object_t = typename BasicJsonType::object_t;
  3268. // macOS's is_constructible does not play well with nonesuch...
  3269. static constexpr bool value =
  3270. is_constructible<typename object_t::key_type,
  3271. typename CompatibleObjectType::key_type>::value &&
  3272. is_constructible<typename object_t::mapped_type,
  3273. typename CompatibleObjectType::mapped_type>::value;
  3274. };
  3275. template<typename BasicJsonType, typename CompatibleObjectType>
  3276. struct is_compatible_object_type
  3277. : is_compatible_object_type_impl<BasicJsonType, CompatibleObjectType> {};
  3278. template<typename BasicJsonType, typename ConstructibleObjectType,
  3279. typename = void>
  3280. struct is_constructible_object_type_impl : std::false_type {};
  3281. template<typename BasicJsonType, typename ConstructibleObjectType>
  3282. struct is_constructible_object_type_impl <
  3283. BasicJsonType, ConstructibleObjectType,
  3284. enable_if_t < is_detected<mapped_type_t, ConstructibleObjectType>::value&&
  3285. is_detected<key_type_t, ConstructibleObjectType>::value >>
  3286. {
  3287. using object_t = typename BasicJsonType::object_t;
  3288. static constexpr bool value =
  3289. (is_default_constructible<ConstructibleObjectType>::value &&
  3290. (std::is_move_assignable<ConstructibleObjectType>::value ||
  3291. std::is_copy_assignable<ConstructibleObjectType>::value) &&
  3292. (is_constructible<typename ConstructibleObjectType::key_type,
  3293. typename object_t::key_type>::value &&
  3294. std::is_same <
  3295. typename object_t::mapped_type,
  3296. typename ConstructibleObjectType::mapped_type >::value)) ||
  3297. (has_from_json<BasicJsonType,
  3298. typename ConstructibleObjectType::mapped_type>::value ||
  3299. has_non_default_from_json <
  3300. BasicJsonType,
  3301. typename ConstructibleObjectType::mapped_type >::value);
  3302. };
  3303. template<typename BasicJsonType, typename ConstructibleObjectType>
  3304. struct is_constructible_object_type
  3305. : is_constructible_object_type_impl<BasicJsonType,
  3306. ConstructibleObjectType> {};
  3307. template<typename BasicJsonType, typename CompatibleStringType>
  3308. struct is_compatible_string_type
  3309. {
  3310. static constexpr auto value =
  3311. is_constructible<typename BasicJsonType::string_t, CompatibleStringType>::value;
  3312. };
  3313. template<typename BasicJsonType, typename ConstructibleStringType>
  3314. struct is_constructible_string_type
  3315. {
  3316. // launder type through decltype() to fix compilation failure on ICPC
  3317. #ifdef __INTEL_COMPILER
  3318. using laundered_type = decltype(std::declval<ConstructibleStringType>());
  3319. #else
  3320. using laundered_type = ConstructibleStringType;
  3321. #endif
  3322. static constexpr auto value =
  3323. conjunction <
  3324. is_constructible<laundered_type, typename BasicJsonType::string_t>,
  3325. is_detected_exact<typename BasicJsonType::string_t::value_type,
  3326. value_type_t, laundered_type >>::value;
  3327. };
  3328. template<typename BasicJsonType, typename CompatibleArrayType, typename = void>
  3329. struct is_compatible_array_type_impl : std::false_type {};
  3330. template<typename BasicJsonType, typename CompatibleArrayType>
  3331. struct is_compatible_array_type_impl <
  3332. BasicJsonType, CompatibleArrayType,
  3333. enable_if_t <
  3334. is_detected<iterator_t, CompatibleArrayType>::value&&
  3335. is_iterator_traits<iterator_traits<detected_t<iterator_t, CompatibleArrayType>>>::value&&
  3336. // special case for types like std::filesystem::path whose iterator's value_type are themselves
  3337. // c.f. https://github.com/nlohmann/json/pull/3073
  3338. !std::is_same<CompatibleArrayType, detected_t<range_value_t, CompatibleArrayType>>::value >>
  3339. {
  3340. static constexpr bool value =
  3341. is_constructible<BasicJsonType,
  3342. range_value_t<CompatibleArrayType>>::value;
  3343. };
  3344. template<typename BasicJsonType, typename CompatibleArrayType>
  3345. struct is_compatible_array_type
  3346. : is_compatible_array_type_impl<BasicJsonType, CompatibleArrayType> {};
  3347. template<typename BasicJsonType, typename ConstructibleArrayType, typename = void>
  3348. struct is_constructible_array_type_impl : std::false_type {};
  3349. template<typename BasicJsonType, typename ConstructibleArrayType>
  3350. struct is_constructible_array_type_impl <
  3351. BasicJsonType, ConstructibleArrayType,
  3352. enable_if_t<std::is_same<ConstructibleArrayType,
  3353. typename BasicJsonType::value_type>::value >>
  3354. : std::true_type {};
  3355. template<typename BasicJsonType, typename ConstructibleArrayType>
  3356. struct is_constructible_array_type_impl <
  3357. BasicJsonType, ConstructibleArrayType,
  3358. enable_if_t < !std::is_same<ConstructibleArrayType,
  3359. typename BasicJsonType::value_type>::value&&
  3360. !is_compatible_string_type<BasicJsonType, ConstructibleArrayType>::value&&
  3361. is_default_constructible<ConstructibleArrayType>::value&&
  3362. (std::is_move_assignable<ConstructibleArrayType>::value ||
  3363. std::is_copy_assignable<ConstructibleArrayType>::value)&&
  3364. is_detected<iterator_t, ConstructibleArrayType>::value&&
  3365. is_iterator_traits<iterator_traits<detected_t<iterator_t, ConstructibleArrayType>>>::value&&
  3366. is_detected<range_value_t, ConstructibleArrayType>::value&&
  3367. // special case for types like std::filesystem::path whose iterator's value_type are themselves
  3368. // c.f. https://github.com/nlohmann/json/pull/3073
  3369. !std::is_same<ConstructibleArrayType, detected_t<range_value_t, ConstructibleArrayType>>::value&&
  3370. is_complete_type <
  3371. detected_t<range_value_t, ConstructibleArrayType >>::value >>
  3372. {
  3373. using value_type = range_value_t<ConstructibleArrayType>;
  3374. static constexpr bool value =
  3375. std::is_same<value_type,
  3376. typename BasicJsonType::array_t::value_type>::value ||
  3377. has_from_json<BasicJsonType,
  3378. value_type>::value ||
  3379. has_non_default_from_json <
  3380. BasicJsonType,
  3381. value_type >::value;
  3382. };
  3383. template<typename BasicJsonType, typename ConstructibleArrayType>
  3384. struct is_constructible_array_type
  3385. : is_constructible_array_type_impl<BasicJsonType, ConstructibleArrayType> {};
  3386. template<typename RealIntegerType, typename CompatibleNumberIntegerType,
  3387. typename = void>
  3388. struct is_compatible_integer_type_impl : std::false_type {};
  3389. template<typename RealIntegerType, typename CompatibleNumberIntegerType>
  3390. struct is_compatible_integer_type_impl <
  3391. RealIntegerType, CompatibleNumberIntegerType,
  3392. enable_if_t < std::is_integral<RealIntegerType>::value&&
  3393. std::is_integral<CompatibleNumberIntegerType>::value&&
  3394. !std::is_same<bool, CompatibleNumberIntegerType>::value >>
  3395. {
  3396. // is there an assert somewhere on overflows?
  3397. using RealLimits = std::numeric_limits<RealIntegerType>;
  3398. using CompatibleLimits = std::numeric_limits<CompatibleNumberIntegerType>;
  3399. static constexpr auto value =
  3400. is_constructible<RealIntegerType,
  3401. CompatibleNumberIntegerType>::value &&
  3402. CompatibleLimits::is_integer &&
  3403. RealLimits::is_signed == CompatibleLimits::is_signed;
  3404. };
  3405. template<typename RealIntegerType, typename CompatibleNumberIntegerType>
  3406. struct is_compatible_integer_type
  3407. : is_compatible_integer_type_impl<RealIntegerType,
  3408. CompatibleNumberIntegerType> {};
  3409. template<typename BasicJsonType, typename CompatibleType, typename = void>
  3410. struct is_compatible_type_impl: std::false_type {};
  3411. template<typename BasicJsonType, typename CompatibleType>
  3412. struct is_compatible_type_impl <
  3413. BasicJsonType, CompatibleType,
  3414. enable_if_t<is_complete_type<CompatibleType>::value >>
  3415. {
  3416. static constexpr bool value =
  3417. has_to_json<BasicJsonType, CompatibleType>::value;
  3418. };
  3419. template<typename BasicJsonType, typename CompatibleType>
  3420. struct is_compatible_type
  3421. : is_compatible_type_impl<BasicJsonType, CompatibleType> {};
  3422. template<typename T1, typename T2>
  3423. struct is_constructible_tuple : std::false_type {};
  3424. template<typename T1, typename... Args>
  3425. struct is_constructible_tuple<T1, std::tuple<Args...>> : conjunction<is_constructible<T1, Args>...> {};
  3426. template<typename BasicJsonType, typename T>
  3427. struct is_json_iterator_of : std::false_type {};
  3428. template<typename BasicJsonType>
  3429. struct is_json_iterator_of<BasicJsonType, typename BasicJsonType::iterator> : std::true_type {};
  3430. template<typename BasicJsonType>
  3431. struct is_json_iterator_of<BasicJsonType, typename BasicJsonType::const_iterator> : std::true_type
  3432. {};
  3433. // checks if a given type T is a template specialization of Primary
  3434. template<template <typename...> class Primary, typename T>
  3435. struct is_specialization_of : std::false_type {};
  3436. template<template <typename...> class Primary, typename... Args>
  3437. struct is_specialization_of<Primary, Primary<Args...>> : std::true_type {};
  3438. template<typename T>
  3439. using is_json_pointer = is_specialization_of<::nlohmann::json_pointer, uncvref_t<T>>;
  3440. // checks if A and B are comparable using Compare functor
  3441. template<typename Compare, typename A, typename B, typename = void>
  3442. struct is_comparable : std::false_type {};
  3443. template<typename Compare, typename A, typename B>
  3444. struct is_comparable<Compare, A, B, void_t<
  3445. decltype(std::declval<Compare>()(std::declval<A>(), std::declval<B>())),
  3446. decltype(std::declval<Compare>()(std::declval<B>(), std::declval<A>()))
  3447. >> : std::true_type {};
  3448. template<typename T>
  3449. using detect_is_transparent = typename T::is_transparent;
  3450. // type trait to check if KeyType can be used as object key (without a BasicJsonType)
  3451. // see is_usable_as_basic_json_key_type below
  3452. template<typename Comparator, typename ObjectKeyType, typename KeyTypeCVRef, bool RequireTransparentComparator = true,
  3453. bool ExcludeObjectKeyType = RequireTransparentComparator, typename KeyType = uncvref_t<KeyTypeCVRef>>
  3454. using is_usable_as_key_type = typename std::conditional <
  3455. is_comparable<Comparator, ObjectKeyType, KeyTypeCVRef>::value
  3456. && !(ExcludeObjectKeyType && std::is_same<KeyType,
  3457. ObjectKeyType>::value)
  3458. && (!RequireTransparentComparator
  3459. || is_detected <detect_is_transparent, Comparator>::value)
  3460. && !is_json_pointer<KeyType>::value,
  3461. std::true_type,
  3462. std::false_type >::type;
  3463. // type trait to check if KeyType can be used as object key
  3464. // true if:
  3465. // - KeyType is comparable with BasicJsonType::object_t::key_type
  3466. // - if ExcludeObjectKeyType is true, KeyType is not BasicJsonType::object_t::key_type
  3467. // - the comparator is transparent or RequireTransparentComparator is false
  3468. // - KeyType is not a JSON iterator or json_pointer
  3469. template<typename BasicJsonType, typename KeyTypeCVRef, bool RequireTransparentComparator = true,
  3470. bool ExcludeObjectKeyType = RequireTransparentComparator, typename KeyType = uncvref_t<KeyTypeCVRef>>
  3471. using is_usable_as_basic_json_key_type = typename std::conditional <
  3472. is_usable_as_key_type<typename BasicJsonType::object_comparator_t,
  3473. typename BasicJsonType::object_t::key_type, KeyTypeCVRef,
  3474. RequireTransparentComparator, ExcludeObjectKeyType>::value
  3475. && !is_json_iterator_of<BasicJsonType, KeyType>::value,
  3476. std::true_type,
  3477. std::false_type >::type;
  3478. template<typename ObjectType, typename KeyType>
  3479. using detect_erase_with_key_type = decltype(std::declval<ObjectType&>().erase(std::declval<KeyType>()));
  3480. // type trait to check if object_t has an erase() member functions accepting KeyType
  3481. template<typename BasicJsonType, typename KeyType>
  3482. using has_erase_with_key_type = typename std::conditional <
  3483. is_detected <
  3484. detect_erase_with_key_type,
  3485. typename BasicJsonType::object_t, KeyType >::value,
  3486. std::true_type,
  3487. std::false_type >::type;
  3488. // a naive helper to check if a type is an ordered_map (exploits the fact that
  3489. // ordered_map inherits capacity() from std::vector)
  3490. template <typename T>
  3491. struct is_ordered_map
  3492. {
  3493. using one = char;
  3494. struct two
  3495. {
  3496. char x[2]; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  3497. };
  3498. template <typename C> static one test( decltype(&C::capacity) ) ;
  3499. template <typename C> static two test(...);
  3500. enum { value = sizeof(test<T>(nullptr)) == sizeof(char) }; // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  3501. };
  3502. // to avoid useless casts (see https://github.com/nlohmann/json/issues/2893#issuecomment-889152324)
  3503. template < typename T, typename U, enable_if_t < !std::is_same<T, U>::value, int > = 0 >
  3504. T conditional_static_cast(U value)
  3505. {
  3506. return static_cast<T>(value);
  3507. }
  3508. template<typename T, typename U, enable_if_t<std::is_same<T, U>::value, int> = 0>
  3509. T conditional_static_cast(U value)
  3510. {
  3511. return value;
  3512. }
  3513. template<typename... Types>
  3514. using all_integral = conjunction<std::is_integral<Types>...>;
  3515. template<typename... Types>
  3516. using all_signed = conjunction<std::is_signed<Types>...>;
  3517. template<typename... Types>
  3518. using all_unsigned = conjunction<std::is_unsigned<Types>...>;
  3519. // there's a disjunction trait in another PR; replace when merged
  3520. template<typename... Types>
  3521. using same_sign = std::integral_constant < bool,
  3522. all_signed<Types...>::value || all_unsigned<Types...>::value >;
  3523. template<typename OfType, typename T>
  3524. using never_out_of_range = std::integral_constant < bool,
  3525. (std::is_signed<OfType>::value && (sizeof(T) < sizeof(OfType)))
  3526. || (same_sign<OfType, T>::value && sizeof(OfType) == sizeof(T)) >;
  3527. template<typename OfType, typename T,
  3528. bool OfTypeSigned = std::is_signed<OfType>::value,
  3529. bool TSigned = std::is_signed<T>::value>
  3530. struct value_in_range_of_impl2;
  3531. template<typename OfType, typename T>
  3532. struct value_in_range_of_impl2<OfType, T, false, false>
  3533. {
  3534. static constexpr bool test(T val)
  3535. {
  3536. using CommonType = typename std::common_type<OfType, T>::type;
  3537. return static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
  3538. }
  3539. };
  3540. template<typename OfType, typename T>
  3541. struct value_in_range_of_impl2<OfType, T, true, false>
  3542. {
  3543. static constexpr bool test(T val)
  3544. {
  3545. using CommonType = typename std::common_type<OfType, T>::type;
  3546. return static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
  3547. }
  3548. };
  3549. template<typename OfType, typename T>
  3550. struct value_in_range_of_impl2<OfType, T, false, true>
  3551. {
  3552. static constexpr bool test(T val)
  3553. {
  3554. using CommonType = typename std::common_type<OfType, T>::type;
  3555. return val >= 0 && static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
  3556. }
  3557. };
  3558. template<typename OfType, typename T>
  3559. struct value_in_range_of_impl2<OfType, T, true, true>
  3560. {
  3561. static constexpr bool test(T val)
  3562. {
  3563. using CommonType = typename std::common_type<OfType, T>::type;
  3564. return static_cast<CommonType>(val) >= static_cast<CommonType>((std::numeric_limits<OfType>::min)())
  3565. && static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
  3566. }
  3567. };
  3568. template<typename OfType, typename T,
  3569. bool NeverOutOfRange = never_out_of_range<OfType, T>::value,
  3570. typename = detail::enable_if_t<all_integral<OfType, T>::value>>
  3571. struct value_in_range_of_impl1;
  3572. template<typename OfType, typename T>
  3573. struct value_in_range_of_impl1<OfType, T, false>
  3574. {
  3575. static constexpr bool test(T val)
  3576. {
  3577. return value_in_range_of_impl2<OfType, T>::test(val);
  3578. }
  3579. };
  3580. template<typename OfType, typename T>
  3581. struct value_in_range_of_impl1<OfType, T, true>
  3582. {
  3583. static constexpr bool test(T /*val*/)
  3584. {
  3585. return true;
  3586. }
  3587. };
  3588. template<typename OfType, typename T>
  3589. inline constexpr bool value_in_range_of(T val)
  3590. {
  3591. return value_in_range_of_impl1<OfType, T>::test(val);
  3592. }
  3593. template<bool Value>
  3594. using bool_constant = std::integral_constant<bool, Value>;
  3595. ///////////////////////////////////////////////////////////////////////////////
  3596. // is_c_string
  3597. ///////////////////////////////////////////////////////////////////////////////
  3598. namespace impl
  3599. {
  3600. template<typename T>
  3601. inline constexpr bool is_c_string()
  3602. {
  3603. using TUnExt = typename std::remove_extent<T>::type;
  3604. using TUnCVExt = typename std::remove_cv<TUnExt>::type;
  3605. using TUnPtr = typename std::remove_pointer<T>::type;
  3606. using TUnCVPtr = typename std::remove_cv<TUnPtr>::type;
  3607. return
  3608. (std::is_array<T>::value && std::is_same<TUnCVExt, char>::value)
  3609. || (std::is_pointer<T>::value && std::is_same<TUnCVPtr, char>::value);
  3610. }
  3611. } // namespace impl
  3612. // checks whether T is a [cv] char */[cv] char[] C string
  3613. template<typename T>
  3614. struct is_c_string : bool_constant<impl::is_c_string<T>()> {};
  3615. template<typename T>
  3616. using is_c_string_uncvref = is_c_string<uncvref_t<T>>;
  3617. ///////////////////////////////////////////////////////////////////////////////
  3618. // is_transparent
  3619. ///////////////////////////////////////////////////////////////////////////////
  3620. namespace impl
  3621. {
  3622. template<typename T>
  3623. inline constexpr bool is_transparent()
  3624. {
  3625. return is_detected<detect_is_transparent, T>::value;
  3626. }
  3627. } // namespace impl
  3628. // checks whether T has a member named is_transparent
  3629. template<typename T>
  3630. struct is_transparent : bool_constant<impl::is_transparent<T>()> {};
  3631. ///////////////////////////////////////////////////////////////////////////////
  3632. } // namespace detail
  3633. NLOHMANN_JSON_NAMESPACE_END
  3634. // #include <nlohmann/detail/string_concat.hpp>
  3635. // __ _____ _____ _____
  3636. // __| | __| | | | JSON for Modern C++
  3637. // | | |__ | | | | | | version 3.11.2
  3638. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  3639. //
  3640. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  3641. // SPDX-License-Identifier: MIT
  3642. #include <cstring> // strlen
  3643. #include <string> // string
  3644. #include <utility> // forward
  3645. // #include <nlohmann/detail/meta/cpp_future.hpp>
  3646. // #include <nlohmann/detail/meta/detected.hpp>
  3647. NLOHMANN_JSON_NAMESPACE_BEGIN
  3648. namespace detail
  3649. {
  3650. inline std::size_t concat_length()
  3651. {
  3652. return 0;
  3653. }
  3654. template<typename... Args>
  3655. inline std::size_t concat_length(const char* cstr, const Args& ... rest);
  3656. template<typename StringType, typename... Args>
  3657. inline std::size_t concat_length(const StringType& str, const Args& ... rest);
  3658. template<typename... Args>
  3659. inline std::size_t concat_length(const char /*c*/, const Args& ... rest)
  3660. {
  3661. return 1 + concat_length(rest...);
  3662. }
  3663. template<typename... Args>
  3664. inline std::size_t concat_length(const char* cstr, const Args& ... rest)
  3665. {
  3666. // cppcheck-suppress ignoredReturnValue
  3667. return ::strlen(cstr) + concat_length(rest...);
  3668. }
  3669. template<typename StringType, typename... Args>
  3670. inline std::size_t concat_length(const StringType& str, const Args& ... rest)
  3671. {
  3672. return str.size() + concat_length(rest...);
  3673. }
  3674. template<typename OutStringType>
  3675. inline void concat_into(OutStringType& /*out*/)
  3676. {}
  3677. template<typename StringType, typename Arg>
  3678. using string_can_append = decltype(std::declval<StringType&>().append(std::declval < Arg && > ()));
  3679. template<typename StringType, typename Arg>
  3680. using detect_string_can_append = is_detected<string_can_append, StringType, Arg>;
  3681. template<typename StringType, typename Arg>
  3682. using string_can_append_op = decltype(std::declval<StringType&>() += std::declval < Arg && > ());
  3683. template<typename StringType, typename Arg>
  3684. using detect_string_can_append_op = is_detected<string_can_append_op, StringType, Arg>;
  3685. template<typename StringType, typename Arg>
  3686. using string_can_append_iter = decltype(std::declval<StringType&>().append(std::declval<const Arg&>().begin(), std::declval<const Arg&>().end()));
  3687. template<typename StringType, typename Arg>
  3688. using detect_string_can_append_iter = is_detected<string_can_append_iter, StringType, Arg>;
  3689. template<typename StringType, typename Arg>
  3690. using string_can_append_data = decltype(std::declval<StringType&>().append(std::declval<const Arg&>().data(), std::declval<const Arg&>().size()));
  3691. template<typename StringType, typename Arg>
  3692. using detect_string_can_append_data = is_detected<string_can_append_data, StringType, Arg>;
  3693. template < typename OutStringType, typename Arg, typename... Args,
  3694. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3695. && detect_string_can_append_op<OutStringType, Arg>::value, int > = 0 >
  3696. inline void concat_into(OutStringType& out, Arg && arg, Args && ... rest);
  3697. template < typename OutStringType, typename Arg, typename... Args,
  3698. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3699. && !detect_string_can_append_op<OutStringType, Arg>::value
  3700. && detect_string_can_append_iter<OutStringType, Arg>::value, int > = 0 >
  3701. inline void concat_into(OutStringType& out, const Arg& arg, Args && ... rest);
  3702. template < typename OutStringType, typename Arg, typename... Args,
  3703. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3704. && !detect_string_can_append_op<OutStringType, Arg>::value
  3705. && !detect_string_can_append_iter<OutStringType, Arg>::value
  3706. && detect_string_can_append_data<OutStringType, Arg>::value, int > = 0 >
  3707. inline void concat_into(OutStringType& out, const Arg& arg, Args && ... rest);
  3708. template<typename OutStringType, typename Arg, typename... Args,
  3709. enable_if_t<detect_string_can_append<OutStringType, Arg>::value, int> = 0>
  3710. inline void concat_into(OutStringType& out, Arg && arg, Args && ... rest)
  3711. {
  3712. out.append(std::forward<Arg>(arg));
  3713. concat_into(out, std::forward<Args>(rest)...);
  3714. }
  3715. template < typename OutStringType, typename Arg, typename... Args,
  3716. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3717. && detect_string_can_append_op<OutStringType, Arg>::value, int > >
  3718. inline void concat_into(OutStringType& out, Arg&& arg, Args&& ... rest)
  3719. {
  3720. out += std::forward<Arg>(arg);
  3721. concat_into(out, std::forward<Args>(rest)...);
  3722. }
  3723. template < typename OutStringType, typename Arg, typename... Args,
  3724. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3725. && !detect_string_can_append_op<OutStringType, Arg>::value
  3726. && detect_string_can_append_iter<OutStringType, Arg>::value, int > >
  3727. inline void concat_into(OutStringType& out, const Arg& arg, Args&& ... rest)
  3728. {
  3729. out.append(arg.begin(), arg.end());
  3730. concat_into(out, std::forward<Args>(rest)...);
  3731. }
  3732. template < typename OutStringType, typename Arg, typename... Args,
  3733. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3734. && !detect_string_can_append_op<OutStringType, Arg>::value
  3735. && !detect_string_can_append_iter<OutStringType, Arg>::value
  3736. && detect_string_can_append_data<OutStringType, Arg>::value, int > >
  3737. inline void concat_into(OutStringType& out, const Arg& arg, Args&& ... rest)
  3738. {
  3739. out.append(arg.data(), arg.size());
  3740. concat_into(out, std::forward<Args>(rest)...);
  3741. }
  3742. template<typename OutStringType = std::string, typename... Args>
  3743. inline OutStringType concat(Args && ... args)
  3744. {
  3745. OutStringType str;
  3746. str.reserve(concat_length(args...));
  3747. concat_into(str, std::forward<Args>(args)...);
  3748. return str;
  3749. }
  3750. } // namespace detail
  3751. NLOHMANN_JSON_NAMESPACE_END
  3752. NLOHMANN_JSON_NAMESPACE_BEGIN
  3753. namespace detail
  3754. {
  3755. ////////////////
  3756. // exceptions //
  3757. ////////////////
  3758. /// @brief general exception of the @ref basic_json class
  3759. /// @sa https://json.nlohmann.me/api/basic_json/exception/
  3760. class exception : public std::exception
  3761. {
  3762. public:
  3763. /// returns the explanatory string
  3764. const char* what() const noexcept override
  3765. {
  3766. return m.what();
  3767. }
  3768. /// the id of the exception
  3769. const int id; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes)
  3770. protected:
  3771. JSON_HEDLEY_NON_NULL(3)
  3772. exception(int id_, const char* what_arg) : id(id_), m(what_arg) {} // NOLINT(bugprone-throw-keyword-missing)
  3773. static std::string name(const std::string& ename, int id_)
  3774. {
  3775. return concat("[json.exception.", ename, '.', std::to_string(id_), "] ");
  3776. }
  3777. static std::string diagnostics(std::nullptr_t /*leaf_element*/)
  3778. {
  3779. return "";
  3780. }
  3781. template<typename BasicJsonType>
  3782. static std::string diagnostics(const BasicJsonType* leaf_element)
  3783. {
  3784. #if JSON_DIAGNOSTICS
  3785. std::vector<std::string> tokens;
  3786. for (const auto* current = leaf_element; current != nullptr && current->m_parent != nullptr; current = current->m_parent)
  3787. {
  3788. switch (current->m_parent->type())
  3789. {
  3790. case value_t::array:
  3791. {
  3792. for (std::size_t i = 0; i < current->m_parent->m_data.m_value.array->size(); ++i)
  3793. {
  3794. if (&current->m_parent->m_data.m_value.array->operator[](i) == current)
  3795. {
  3796. tokens.emplace_back(std::to_string(i));
  3797. break;
  3798. }
  3799. }
  3800. break;
  3801. }
  3802. case value_t::object:
  3803. {
  3804. for (const auto& element : *current->m_parent->m_data.m_value.object)
  3805. {
  3806. if (&element.second == current)
  3807. {
  3808. tokens.emplace_back(element.first.c_str());
  3809. break;
  3810. }
  3811. }
  3812. break;
  3813. }
  3814. case value_t::null: // LCOV_EXCL_LINE
  3815. case value_t::string: // LCOV_EXCL_LINE
  3816. case value_t::boolean: // LCOV_EXCL_LINE
  3817. case value_t::number_integer: // LCOV_EXCL_LINE
  3818. case value_t::number_unsigned: // LCOV_EXCL_LINE
  3819. case value_t::number_float: // LCOV_EXCL_LINE
  3820. case value_t::binary: // LCOV_EXCL_LINE
  3821. case value_t::discarded: // LCOV_EXCL_LINE
  3822. default: // LCOV_EXCL_LINE
  3823. break; // LCOV_EXCL_LINE
  3824. }
  3825. }
  3826. if (tokens.empty())
  3827. {
  3828. return "";
  3829. }
  3830. auto str = std::accumulate(tokens.rbegin(), tokens.rend(), std::string{},
  3831. [](const std::string & a, const std::string & b)
  3832. {
  3833. return concat(a, '/', detail::escape(b));
  3834. });
  3835. return concat('(', str, ") ");
  3836. #else
  3837. static_cast<void>(leaf_element);
  3838. return "";
  3839. #endif
  3840. }
  3841. private:
  3842. /// an exception object as storage for error messages
  3843. std::runtime_error m;
  3844. };
  3845. /// @brief exception indicating a parse error
  3846. /// @sa https://json.nlohmann.me/api/basic_json/parse_error/
  3847. class parse_error : public exception
  3848. {
  3849. public:
  3850. /*!
  3851. @brief create a parse error exception
  3852. @param[in] id_ the id of the exception
  3853. @param[in] pos the position where the error occurred (or with
  3854. chars_read_total=0 if the position cannot be
  3855. determined)
  3856. @param[in] what_arg the explanatory string
  3857. @return parse_error object
  3858. */
  3859. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3860. static parse_error create(int id_, const position_t& pos, const std::string& what_arg, BasicJsonContext context)
  3861. {
  3862. const std::string w = concat(exception::name("parse_error", id_), "parse error",
  3863. position_string(pos), ": ", exception::diagnostics(context), what_arg);
  3864. return {id_, pos.chars_read_total, w.c_str()};
  3865. }
  3866. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3867. static parse_error create(int id_, std::size_t byte_, const std::string& what_arg, BasicJsonContext context)
  3868. {
  3869. const std::string w = concat(exception::name("parse_error", id_), "parse error",
  3870. (byte_ != 0 ? (concat(" at byte ", std::to_string(byte_))) : ""),
  3871. ": ", exception::diagnostics(context), what_arg);
  3872. return {id_, byte_, w.c_str()};
  3873. }
  3874. /*!
  3875. @brief byte index of the parse error
  3876. The byte index of the last read character in the input file.
  3877. @note For an input with n bytes, 1 is the index of the first character and
  3878. n+1 is the index of the terminating null byte or the end of file.
  3879. This also holds true when reading a byte vector (CBOR or MessagePack).
  3880. */
  3881. const std::size_t byte;
  3882. private:
  3883. parse_error(int id_, std::size_t byte_, const char* what_arg)
  3884. : exception(id_, what_arg), byte(byte_) {}
  3885. static std::string position_string(const position_t& pos)
  3886. {
  3887. return concat(" at line ", std::to_string(pos.lines_read + 1),
  3888. ", column ", std::to_string(pos.chars_read_current_line));
  3889. }
  3890. };
  3891. /// @brief exception indicating errors with iterators
  3892. /// @sa https://json.nlohmann.me/api/basic_json/invalid_iterator/
  3893. class invalid_iterator : public exception
  3894. {
  3895. public:
  3896. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3897. static invalid_iterator create(int id_, const std::string& what_arg, BasicJsonContext context)
  3898. {
  3899. const std::string w = concat(exception::name("invalid_iterator", id_), exception::diagnostics(context), what_arg);
  3900. return {id_, w.c_str()};
  3901. }
  3902. private:
  3903. JSON_HEDLEY_NON_NULL(3)
  3904. invalid_iterator(int id_, const char* what_arg)
  3905. : exception(id_, what_arg) {}
  3906. };
  3907. /// @brief exception indicating executing a member function with a wrong type
  3908. /// @sa https://json.nlohmann.me/api/basic_json/type_error/
  3909. class type_error : public exception
  3910. {
  3911. public:
  3912. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3913. static type_error create(int id_, const std::string& what_arg, BasicJsonContext context)
  3914. {
  3915. const std::string w = concat(exception::name("type_error", id_), exception::diagnostics(context), what_arg);
  3916. return {id_, w.c_str()};
  3917. }
  3918. private:
  3919. JSON_HEDLEY_NON_NULL(3)
  3920. type_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
  3921. };
  3922. /// @brief exception indicating access out of the defined range
  3923. /// @sa https://json.nlohmann.me/api/basic_json/out_of_range/
  3924. class out_of_range : public exception
  3925. {
  3926. public:
  3927. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3928. static out_of_range create(int id_, const std::string& what_arg, BasicJsonContext context)
  3929. {
  3930. const std::string w = concat(exception::name("out_of_range", id_), exception::diagnostics(context), what_arg);
  3931. return {id_, w.c_str()};
  3932. }
  3933. private:
  3934. JSON_HEDLEY_NON_NULL(3)
  3935. out_of_range(int id_, const char* what_arg) : exception(id_, what_arg) {}
  3936. };
  3937. /// @brief exception indicating other library errors
  3938. /// @sa https://json.nlohmann.me/api/basic_json/other_error/
  3939. class other_error : public exception
  3940. {
  3941. public:
  3942. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3943. static other_error create(int id_, const std::string& what_arg, BasicJsonContext context)
  3944. {
  3945. const std::string w = concat(exception::name("other_error", id_), exception::diagnostics(context), what_arg);
  3946. return {id_, w.c_str()};
  3947. }
  3948. private:
  3949. JSON_HEDLEY_NON_NULL(3)
  3950. other_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
  3951. };
  3952. } // namespace detail
  3953. NLOHMANN_JSON_NAMESPACE_END
  3954. // #include <nlohmann/detail/macro_scope.hpp>
  3955. // #include <nlohmann/detail/meta/cpp_future.hpp>
  3956. // #include <nlohmann/detail/meta/identity_tag.hpp>
  3957. // __ _____ _____ _____
  3958. // __| | __| | | | JSON for Modern C++
  3959. // | | |__ | | | | | | version 3.11.2
  3960. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  3961. //
  3962. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  3963. // SPDX-License-Identifier: MIT
  3964. // #include <nlohmann/detail/abi_macros.hpp>
  3965. NLOHMANN_JSON_NAMESPACE_BEGIN
  3966. namespace detail
  3967. {
  3968. // dispatching helper struct
  3969. template <class T> struct identity_tag {};
  3970. } // namespace detail
  3971. NLOHMANN_JSON_NAMESPACE_END
  3972. // #include <nlohmann/detail/meta/std_fs.hpp>
  3973. // __ _____ _____ _____
  3974. // __| | __| | | | JSON for Modern C++
  3975. // | | |__ | | | | | | version 3.11.2
  3976. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  3977. //
  3978. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  3979. // SPDX-License-Identifier: MIT
  3980. // #include <nlohmann/detail/macro_scope.hpp>
  3981. #if JSON_HAS_EXPERIMENTAL_FILESYSTEM
  3982. #include <experimental/filesystem>
  3983. NLOHMANN_JSON_NAMESPACE_BEGIN
  3984. namespace detail
  3985. {
  3986. namespace std_fs = std::experimental::filesystem;
  3987. } // namespace detail
  3988. NLOHMANN_JSON_NAMESPACE_END
  3989. #elif JSON_HAS_FILESYSTEM
  3990. #include <filesystem>
  3991. NLOHMANN_JSON_NAMESPACE_BEGIN
  3992. namespace detail
  3993. {
  3994. namespace std_fs = std::filesystem;
  3995. } // namespace detail
  3996. NLOHMANN_JSON_NAMESPACE_END
  3997. #endif
  3998. // #include <nlohmann/detail/meta/type_traits.hpp>
  3999. // #include <nlohmann/detail/string_concat.hpp>
  4000. // #include <nlohmann/detail/value_t.hpp>
  4001. NLOHMANN_JSON_NAMESPACE_BEGIN
  4002. namespace detail
  4003. {
  4004. template<typename BasicJsonType>
  4005. inline void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
  4006. {
  4007. if (JSON_HEDLEY_UNLIKELY(!j.is_null()))
  4008. {
  4009. JSON_THROW(type_error::create(302, concat("type must be null, but is ", j.type_name()), &j));
  4010. }
  4011. n = nullptr;
  4012. }
  4013. // overloads for basic_json template parameters
  4014. template < typename BasicJsonType, typename ArithmeticType,
  4015. enable_if_t < std::is_arithmetic<ArithmeticType>::value&&
  4016. !std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
  4017. int > = 0 >
  4018. void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val)
  4019. {
  4020. switch (static_cast<value_t>(j))
  4021. {
  4022. case value_t::number_unsigned:
  4023. {
  4024. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
  4025. break;
  4026. }
  4027. case value_t::number_integer:
  4028. {
  4029. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
  4030. break;
  4031. }
  4032. case value_t::number_float:
  4033. {
  4034. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
  4035. break;
  4036. }
  4037. case value_t::null:
  4038. case value_t::object:
  4039. case value_t::array:
  4040. case value_t::string:
  4041. case value_t::boolean:
  4042. case value_t::binary:
  4043. case value_t::discarded:
  4044. default:
  4045. JSON_THROW(type_error::create(302, concat("type must be number, but is ", j.type_name()), &j));
  4046. }
  4047. }
  4048. template<typename BasicJsonType>
  4049. inline void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
  4050. {
  4051. if (JSON_HEDLEY_UNLIKELY(!j.is_boolean()))
  4052. {
  4053. JSON_THROW(type_error::create(302, concat("type must be boolean, but is ", j.type_name()), &j));
  4054. }
  4055. b = *j.template get_ptr<const typename BasicJsonType::boolean_t*>();
  4056. }
  4057. template<typename BasicJsonType>
  4058. inline void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s)
  4059. {
  4060. if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
  4061. {
  4062. JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
  4063. }
  4064. s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
  4065. }
  4066. template <
  4067. typename BasicJsonType, typename StringType,
  4068. enable_if_t <
  4069. std::is_assignable<StringType&, const typename BasicJsonType::string_t>::value
  4070. && is_detected_exact<typename BasicJsonType::string_t::value_type, value_type_t, StringType>::value
  4071. && !std::is_same<typename BasicJsonType::string_t, StringType>::value
  4072. && !is_json_ref<StringType>::value, int > = 0 >
  4073. inline void from_json(const BasicJsonType& j, StringType& s)
  4074. {
  4075. if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
  4076. {
  4077. JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
  4078. }
  4079. s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
  4080. }
  4081. template<typename BasicJsonType>
  4082. inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val)
  4083. {
  4084. get_arithmetic_value(j, val);
  4085. }
  4086. template<typename BasicJsonType>
  4087. inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_unsigned_t& val)
  4088. {
  4089. get_arithmetic_value(j, val);
  4090. }
  4091. template<typename BasicJsonType>
  4092. inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& val)
  4093. {
  4094. get_arithmetic_value(j, val);
  4095. }
  4096. #if !JSON_DISABLE_ENUM_SERIALIZATION
  4097. template<typename BasicJsonType, typename EnumType,
  4098. enable_if_t<std::is_enum<EnumType>::value, int> = 0>
  4099. inline void from_json(const BasicJsonType& j, EnumType& e)
  4100. {
  4101. typename std::underlying_type<EnumType>::type val;
  4102. get_arithmetic_value(j, val);
  4103. e = static_cast<EnumType>(val);
  4104. }
  4105. #endif // JSON_DISABLE_ENUM_SERIALIZATION
  4106. // forward_list doesn't have an insert method
  4107. template<typename BasicJsonType, typename T, typename Allocator,
  4108. enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
  4109. inline void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
  4110. {
  4111. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4112. {
  4113. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4114. }
  4115. l.clear();
  4116. std::transform(j.rbegin(), j.rend(),
  4117. std::front_inserter(l), [](const BasicJsonType & i)
  4118. {
  4119. return i.template get<T>();
  4120. });
  4121. }
  4122. // valarray doesn't have an insert method
  4123. template<typename BasicJsonType, typename T,
  4124. enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
  4125. inline void from_json(const BasicJsonType& j, std::valarray<T>& l)
  4126. {
  4127. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4128. {
  4129. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4130. }
  4131. l.resize(j.size());
  4132. std::transform(j.begin(), j.end(), std::begin(l),
  4133. [](const BasicJsonType & elem)
  4134. {
  4135. return elem.template get<T>();
  4136. });
  4137. }
  4138. template<typename BasicJsonType, typename T, std::size_t N>
  4139. auto from_json(const BasicJsonType& j, T (&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  4140. -> decltype(j.template get<T>(), void())
  4141. {
  4142. for (std::size_t i = 0; i < N; ++i)
  4143. {
  4144. arr[i] = j.at(i).template get<T>();
  4145. }
  4146. }
  4147. template<typename BasicJsonType>
  4148. inline void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType::array_t& arr, priority_tag<3> /*unused*/)
  4149. {
  4150. arr = *j.template get_ptr<const typename BasicJsonType::array_t*>();
  4151. }
  4152. template<typename BasicJsonType, typename T, std::size_t N>
  4153. auto from_json_array_impl(const BasicJsonType& j, std::array<T, N>& arr,
  4154. priority_tag<2> /*unused*/)
  4155. -> decltype(j.template get<T>(), void())
  4156. {
  4157. for (std::size_t i = 0; i < N; ++i)
  4158. {
  4159. arr[i] = j.at(i).template get<T>();
  4160. }
  4161. }
  4162. template<typename BasicJsonType, typename ConstructibleArrayType,
  4163. enable_if_t<
  4164. std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
  4165. int> = 0>
  4166. auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, priority_tag<1> /*unused*/)
  4167. -> decltype(
  4168. arr.reserve(std::declval<typename ConstructibleArrayType::size_type>()),
  4169. j.template get<typename ConstructibleArrayType::value_type>(),
  4170. void())
  4171. {
  4172. using std::end;
  4173. ConstructibleArrayType ret;
  4174. ret.reserve(j.size());
  4175. std::transform(j.begin(), j.end(),
  4176. std::inserter(ret, end(ret)), [](const BasicJsonType & i)
  4177. {
  4178. // get<BasicJsonType>() returns *this, this won't call a from_json
  4179. // method when value_type is BasicJsonType
  4180. return i.template get<typename ConstructibleArrayType::value_type>();
  4181. });
  4182. arr = std::move(ret);
  4183. }
  4184. template<typename BasicJsonType, typename ConstructibleArrayType,
  4185. enable_if_t<
  4186. std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
  4187. int> = 0>
  4188. inline void from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr,
  4189. priority_tag<0> /*unused*/)
  4190. {
  4191. using std::end;
  4192. ConstructibleArrayType ret;
  4193. std::transform(
  4194. j.begin(), j.end(), std::inserter(ret, end(ret)),
  4195. [](const BasicJsonType & i)
  4196. {
  4197. // get<BasicJsonType>() returns *this, this won't call a from_json
  4198. // method when value_type is BasicJsonType
  4199. return i.template get<typename ConstructibleArrayType::value_type>();
  4200. });
  4201. arr = std::move(ret);
  4202. }
  4203. template < typename BasicJsonType, typename ConstructibleArrayType,
  4204. enable_if_t <
  4205. is_constructible_array_type<BasicJsonType, ConstructibleArrayType>::value&&
  4206. !is_constructible_object_type<BasicJsonType, ConstructibleArrayType>::value&&
  4207. !is_constructible_string_type<BasicJsonType, ConstructibleArrayType>::value&&
  4208. !std::is_same<ConstructibleArrayType, typename BasicJsonType::binary_t>::value&&
  4209. !is_basic_json<ConstructibleArrayType>::value,
  4210. int > = 0 >
  4211. auto from_json(const BasicJsonType& j, ConstructibleArrayType& arr)
  4212. -> decltype(from_json_array_impl(j, arr, priority_tag<3> {}),
  4213. j.template get<typename ConstructibleArrayType::value_type>(),
  4214. void())
  4215. {
  4216. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4217. {
  4218. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4219. }
  4220. from_json_array_impl(j, arr, priority_tag<3> {});
  4221. }
  4222. template < typename BasicJsonType, typename T, std::size_t... Idx >
  4223. std::array<T, sizeof...(Idx)> from_json_inplace_array_impl(BasicJsonType&& j,
  4224. identity_tag<std::array<T, sizeof...(Idx)>> /*unused*/, index_sequence<Idx...> /*unused*/)
  4225. {
  4226. return { { std::forward<BasicJsonType>(j).at(Idx).template get<T>()... } };
  4227. }
  4228. template < typename BasicJsonType, typename T, std::size_t N >
  4229. auto from_json(BasicJsonType&& j, identity_tag<std::array<T, N>> tag)
  4230. -> decltype(from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {}))
  4231. {
  4232. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4233. {
  4234. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4235. }
  4236. return from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {});
  4237. }
  4238. template<typename BasicJsonType>
  4239. inline void from_json(const BasicJsonType& j, typename BasicJsonType::binary_t& bin)
  4240. {
  4241. if (JSON_HEDLEY_UNLIKELY(!j.is_binary()))
  4242. {
  4243. JSON_THROW(type_error::create(302, concat("type must be binary, but is ", j.type_name()), &j));
  4244. }
  4245. bin = *j.template get_ptr<const typename BasicJsonType::binary_t*>();
  4246. }
  4247. template<typename BasicJsonType, typename ConstructibleObjectType,
  4248. enable_if_t<is_constructible_object_type<BasicJsonType, ConstructibleObjectType>::value, int> = 0>
  4249. inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
  4250. {
  4251. if (JSON_HEDLEY_UNLIKELY(!j.is_object()))
  4252. {
  4253. JSON_THROW(type_error::create(302, concat("type must be object, but is ", j.type_name()), &j));
  4254. }
  4255. ConstructibleObjectType ret;
  4256. const auto* inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
  4257. using value_type = typename ConstructibleObjectType::value_type;
  4258. std::transform(
  4259. inner_object->begin(), inner_object->end(),
  4260. std::inserter(ret, ret.begin()),
  4261. [](typename BasicJsonType::object_t::value_type const & p)
  4262. {
  4263. return value_type(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
  4264. });
  4265. obj = std::move(ret);
  4266. }
  4267. // overload for arithmetic types, not chosen for basic_json template arguments
  4268. // (BooleanType, etc..); note: Is it really necessary to provide explicit
  4269. // overloads for boolean_t etc. in case of a custom BooleanType which is not
  4270. // an arithmetic type?
  4271. template < typename BasicJsonType, typename ArithmeticType,
  4272. enable_if_t <
  4273. std::is_arithmetic<ArithmeticType>::value&&
  4274. !std::is_same<ArithmeticType, typename BasicJsonType::number_unsigned_t>::value&&
  4275. !std::is_same<ArithmeticType, typename BasicJsonType::number_integer_t>::value&&
  4276. !std::is_same<ArithmeticType, typename BasicJsonType::number_float_t>::value&&
  4277. !std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
  4278. int > = 0 >
  4279. inline void from_json(const BasicJsonType& j, ArithmeticType& val)
  4280. {
  4281. switch (static_cast<value_t>(j))
  4282. {
  4283. case value_t::number_unsigned:
  4284. {
  4285. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
  4286. break;
  4287. }
  4288. case value_t::number_integer:
  4289. {
  4290. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
  4291. break;
  4292. }
  4293. case value_t::number_float:
  4294. {
  4295. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
  4296. break;
  4297. }
  4298. case value_t::boolean:
  4299. {
  4300. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::boolean_t*>());
  4301. break;
  4302. }
  4303. case value_t::null:
  4304. case value_t::object:
  4305. case value_t::array:
  4306. case value_t::string:
  4307. case value_t::binary:
  4308. case value_t::discarded:
  4309. default:
  4310. JSON_THROW(type_error::create(302, concat("type must be number, but is ", j.type_name()), &j));
  4311. }
  4312. }
  4313. template<typename BasicJsonType, typename... Args, std::size_t... Idx>
  4314. std::tuple<Args...> from_json_tuple_impl_base(BasicJsonType&& j, index_sequence<Idx...> /*unused*/)
  4315. {
  4316. return std::make_tuple(std::forward<BasicJsonType>(j).at(Idx).template get<Args>()...);
  4317. }
  4318. template < typename BasicJsonType, class A1, class A2 >
  4319. std::pair<A1, A2> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::pair<A1, A2>> /*unused*/, priority_tag<0> /*unused*/)
  4320. {
  4321. return {std::forward<BasicJsonType>(j).at(0).template get<A1>(),
  4322. std::forward<BasicJsonType>(j).at(1).template get<A2>()};
  4323. }
  4324. template<typename BasicJsonType, typename A1, typename A2>
  4325. inline void from_json_tuple_impl(BasicJsonType&& j, std::pair<A1, A2>& p, priority_tag<1> /*unused*/)
  4326. {
  4327. p = from_json_tuple_impl(std::forward<BasicJsonType>(j), identity_tag<std::pair<A1, A2>> {}, priority_tag<0> {});
  4328. }
  4329. template<typename BasicJsonType, typename... Args>
  4330. std::tuple<Args...> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::tuple<Args...>> /*unused*/, priority_tag<2> /*unused*/)
  4331. {
  4332. return from_json_tuple_impl_base<BasicJsonType, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
  4333. }
  4334. template<typename BasicJsonType, typename... Args>
  4335. inline void from_json_tuple_impl(BasicJsonType&& j, std::tuple<Args...>& t, priority_tag<3> /*unused*/)
  4336. {
  4337. t = from_json_tuple_impl_base<BasicJsonType, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
  4338. }
  4339. template<typename BasicJsonType, typename TupleRelated>
  4340. auto from_json(BasicJsonType&& j, TupleRelated&& t)
  4341. -> decltype(from_json_tuple_impl(std::forward<BasicJsonType>(j), std::forward<TupleRelated>(t), priority_tag<3> {}))
  4342. {
  4343. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4344. {
  4345. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4346. }
  4347. return from_json_tuple_impl(std::forward<BasicJsonType>(j), std::forward<TupleRelated>(t), priority_tag<3> {});
  4348. }
  4349. template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator,
  4350. typename = enable_if_t < !std::is_constructible <
  4351. typename BasicJsonType::string_t, Key >::value >>
  4352. inline void from_json(const BasicJsonType& j, std::map<Key, Value, Compare, Allocator>& m)
  4353. {
  4354. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4355. {
  4356. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4357. }
  4358. m.clear();
  4359. for (const auto& p : j)
  4360. {
  4361. if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
  4362. {
  4363. JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &j));
  4364. }
  4365. m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
  4366. }
  4367. }
  4368. template < typename BasicJsonType, typename Key, typename Value, typename Hash, typename KeyEqual, typename Allocator,
  4369. typename = enable_if_t < !std::is_constructible <
  4370. typename BasicJsonType::string_t, Key >::value >>
  4371. inline void from_json(const BasicJsonType& j, std::unordered_map<Key, Value, Hash, KeyEqual, Allocator>& m)
  4372. {
  4373. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4374. {
  4375. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4376. }
  4377. m.clear();
  4378. for (const auto& p : j)
  4379. {
  4380. if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
  4381. {
  4382. JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &j));
  4383. }
  4384. m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
  4385. }
  4386. }
  4387. #if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
  4388. template<typename BasicJsonType>
  4389. inline void from_json(const BasicJsonType& j, std_fs::path& p)
  4390. {
  4391. if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
  4392. {
  4393. JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
  4394. }
  4395. p = *j.template get_ptr<const typename BasicJsonType::string_t*>();
  4396. }
  4397. #endif
  4398. struct from_json_fn
  4399. {
  4400. template<typename BasicJsonType, typename T>
  4401. auto operator()(const BasicJsonType& j, T&& val) const
  4402. noexcept(noexcept(from_json(j, std::forward<T>(val))))
  4403. -> decltype(from_json(j, std::forward<T>(val)))
  4404. {
  4405. return from_json(j, std::forward<T>(val));
  4406. }
  4407. };
  4408. } // namespace detail
  4409. #ifndef JSON_HAS_CPP_17
  4410. /// namespace to hold default `from_json` function
  4411. /// to see why this is required:
  4412. /// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
  4413. namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces)
  4414. {
  4415. #endif
  4416. JSON_INLINE_VARIABLE constexpr const auto& from_json = // NOLINT(misc-definitions-in-headers)
  4417. detail::static_const<detail::from_json_fn>::value;
  4418. #ifndef JSON_HAS_CPP_17
  4419. } // namespace
  4420. #endif
  4421. NLOHMANN_JSON_NAMESPACE_END
  4422. // #include <nlohmann/detail/conversions/to_json.hpp>
  4423. // __ _____ _____ _____
  4424. // __| | __| | | | JSON for Modern C++
  4425. // | | |__ | | | | | | version 3.11.2
  4426. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  4427. //
  4428. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  4429. // SPDX-License-Identifier: MIT
  4430. #include <algorithm> // copy
  4431. #include <iterator> // begin, end
  4432. #include <string> // string
  4433. #include <tuple> // tuple, get
  4434. #include <type_traits> // is_same, is_constructible, is_floating_point, is_enum, underlying_type
  4435. #include <utility> // move, forward, declval, pair
  4436. #include <valarray> // valarray
  4437. #include <vector> // vector
  4438. // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
  4439. // __ _____ _____ _____
  4440. // __| | __| | | | JSON for Modern C++
  4441. // | | |__ | | | | | | version 3.11.2
  4442. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  4443. //
  4444. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  4445. // SPDX-License-Identifier: MIT
  4446. #include <cstddef> // size_t
  4447. #include <iterator> // input_iterator_tag
  4448. #include <string> // string, to_string
  4449. #include <tuple> // tuple_size, get, tuple_element
  4450. #include <utility> // move
  4451. #if JSON_HAS_RANGES
  4452. #include <ranges> // enable_borrowed_range
  4453. #endif
  4454. // #include <nlohmann/detail/abi_macros.hpp>
  4455. // #include <nlohmann/detail/meta/type_traits.hpp>
  4456. // #include <nlohmann/detail/value_t.hpp>
  4457. NLOHMANN_JSON_NAMESPACE_BEGIN
  4458. namespace detail
  4459. {
  4460. template<typename string_type>
  4461. void int_to_string( string_type& target, std::size_t value )
  4462. {
  4463. // For ADL
  4464. using std::to_string;
  4465. target = to_string(value);
  4466. }
  4467. template<typename IteratorType> class iteration_proxy_value
  4468. {
  4469. public:
  4470. using difference_type = std::ptrdiff_t;
  4471. using value_type = iteration_proxy_value;
  4472. using pointer = value_type *;
  4473. using reference = value_type &;
  4474. using iterator_category = std::input_iterator_tag;
  4475. using string_type = typename std::remove_cv< typename std::remove_reference<decltype( std::declval<IteratorType>().key() ) >::type >::type;
  4476. private:
  4477. /// the iterator
  4478. IteratorType anchor{};
  4479. /// an index for arrays (used to create key names)
  4480. std::size_t array_index = 0;
  4481. /// last stringified array index
  4482. mutable std::size_t array_index_last = 0;
  4483. /// a string representation of the array index
  4484. mutable string_type array_index_str = "0";
  4485. /// an empty string (to return a reference for primitive values)
  4486. string_type empty_str{};
  4487. public:
  4488. explicit iteration_proxy_value() = default;
  4489. explicit iteration_proxy_value(IteratorType it, std::size_t array_index_ = 0)
  4490. noexcept(std::is_nothrow_move_constructible<IteratorType>::value
  4491. && std::is_nothrow_default_constructible<string_type>::value)
  4492. : anchor(std::move(it))
  4493. , array_index(array_index_)
  4494. {}
  4495. iteration_proxy_value(iteration_proxy_value const&) = default;
  4496. iteration_proxy_value& operator=(iteration_proxy_value const&) = default;
  4497. // older GCCs are a bit fussy and require explicit noexcept specifiers on defaulted functions
  4498. iteration_proxy_value(iteration_proxy_value&&)
  4499. noexcept(std::is_nothrow_move_constructible<IteratorType>::value
  4500. && std::is_nothrow_move_constructible<string_type>::value) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor,cppcoreguidelines-noexcept-move-operations)
  4501. iteration_proxy_value& operator=(iteration_proxy_value&&)
  4502. noexcept(std::is_nothrow_move_assignable<IteratorType>::value
  4503. && std::is_nothrow_move_assignable<string_type>::value) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor,cppcoreguidelines-noexcept-move-operations)
  4504. ~iteration_proxy_value() = default;
  4505. /// dereference operator (needed for range-based for)
  4506. const iteration_proxy_value& operator*() const
  4507. {
  4508. return *this;
  4509. }
  4510. /// increment operator (needed for range-based for)
  4511. iteration_proxy_value& operator++()
  4512. {
  4513. ++anchor;
  4514. ++array_index;
  4515. return *this;
  4516. }
  4517. iteration_proxy_value operator++(int)& // NOLINT(cert-dcl21-cpp)
  4518. {
  4519. auto tmp = iteration_proxy_value(anchor, array_index);
  4520. ++anchor;
  4521. ++array_index;
  4522. return tmp;
  4523. }
  4524. /// equality operator (needed for InputIterator)
  4525. bool operator==(const iteration_proxy_value& o) const
  4526. {
  4527. return anchor == o.anchor;
  4528. }
  4529. /// inequality operator (needed for range-based for)
  4530. bool operator!=(const iteration_proxy_value& o) const
  4531. {
  4532. return anchor != o.anchor;
  4533. }
  4534. /// return key of the iterator
  4535. const string_type& key() const
  4536. {
  4537. JSON_ASSERT(anchor.m_object != nullptr);
  4538. switch (anchor.m_object->type())
  4539. {
  4540. // use integer array index as key
  4541. case value_t::array:
  4542. {
  4543. if (array_index != array_index_last)
  4544. {
  4545. int_to_string( array_index_str, array_index );
  4546. array_index_last = array_index;
  4547. }
  4548. return array_index_str;
  4549. }
  4550. // use key from the object
  4551. case value_t::object:
  4552. return anchor.key();
  4553. // use an empty key for all primitive types
  4554. case value_t::null:
  4555. case value_t::string:
  4556. case value_t::boolean:
  4557. case value_t::number_integer:
  4558. case value_t::number_unsigned:
  4559. case value_t::number_float:
  4560. case value_t::binary:
  4561. case value_t::discarded:
  4562. default:
  4563. return empty_str;
  4564. }
  4565. }
  4566. /// return value of the iterator
  4567. typename IteratorType::reference value() const
  4568. {
  4569. return anchor.value();
  4570. }
  4571. };
  4572. /// proxy class for the items() function
  4573. template<typename IteratorType> class iteration_proxy
  4574. {
  4575. private:
  4576. /// the container to iterate
  4577. typename IteratorType::pointer container = nullptr;
  4578. public:
  4579. explicit iteration_proxy() = default;
  4580. /// construct iteration proxy from a container
  4581. explicit iteration_proxy(typename IteratorType::reference cont) noexcept
  4582. : container(&cont) {}
  4583. iteration_proxy(iteration_proxy const&) = default;
  4584. iteration_proxy& operator=(iteration_proxy const&) = default;
  4585. iteration_proxy(iteration_proxy&&) noexcept = default;
  4586. iteration_proxy& operator=(iteration_proxy&&) noexcept = default;
  4587. ~iteration_proxy() = default;
  4588. /// return iterator begin (needed for range-based for)
  4589. iteration_proxy_value<IteratorType> begin() const noexcept
  4590. {
  4591. return iteration_proxy_value<IteratorType>(container->begin());
  4592. }
  4593. /// return iterator end (needed for range-based for)
  4594. iteration_proxy_value<IteratorType> end() const noexcept
  4595. {
  4596. return iteration_proxy_value<IteratorType>(container->end());
  4597. }
  4598. };
  4599. // Structured Bindings Support
  4600. // For further reference see https://blog.tartanllama.xyz/structured-bindings/
  4601. // And see https://github.com/nlohmann/json/pull/1391
  4602. template<std::size_t N, typename IteratorType, enable_if_t<N == 0, int> = 0>
  4603. auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.key())
  4604. {
  4605. return i.key();
  4606. }
  4607. // Structured Bindings Support
  4608. // For further reference see https://blog.tartanllama.xyz/structured-bindings/
  4609. // And see https://github.com/nlohmann/json/pull/1391
  4610. template<std::size_t N, typename IteratorType, enable_if_t<N == 1, int> = 0>
  4611. auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.value())
  4612. {
  4613. return i.value();
  4614. }
  4615. } // namespace detail
  4616. NLOHMANN_JSON_NAMESPACE_END
  4617. // The Addition to the STD Namespace is required to add
  4618. // Structured Bindings Support to the iteration_proxy_value class
  4619. // For further reference see https://blog.tartanllama.xyz/structured-bindings/
  4620. // And see https://github.com/nlohmann/json/pull/1391
  4621. namespace std
  4622. {
  4623. #if defined(__clang__)
  4624. // Fix: https://github.com/nlohmann/json/issues/1401
  4625. #pragma clang diagnostic push
  4626. #pragma clang diagnostic ignored "-Wmismatched-tags"
  4627. #endif
  4628. template<typename IteratorType>
  4629. class tuple_size<::nlohmann::detail::iteration_proxy_value<IteratorType>> // NOLINT(cert-dcl58-cpp)
  4630. : public std::integral_constant<std::size_t, 2> {};
  4631. template<std::size_t N, typename IteratorType>
  4632. class tuple_element<N, ::nlohmann::detail::iteration_proxy_value<IteratorType >> // NOLINT(cert-dcl58-cpp)
  4633. {
  4634. public:
  4635. using type = decltype(
  4636. get<N>(std::declval <
  4637. ::nlohmann::detail::iteration_proxy_value<IteratorType >> ()));
  4638. };
  4639. #if defined(__clang__)
  4640. #pragma clang diagnostic pop
  4641. #endif
  4642. } // namespace std
  4643. #if JSON_HAS_RANGES
  4644. template <typename IteratorType>
  4645. inline constexpr bool ::std::ranges::enable_borrowed_range<::nlohmann::detail::iteration_proxy<IteratorType>> = true;
  4646. #endif
  4647. // #include <nlohmann/detail/macro_scope.hpp>
  4648. // #include <nlohmann/detail/meta/cpp_future.hpp>
  4649. // #include <nlohmann/detail/meta/std_fs.hpp>
  4650. // #include <nlohmann/detail/meta/type_traits.hpp>
  4651. // #include <nlohmann/detail/value_t.hpp>
  4652. NLOHMANN_JSON_NAMESPACE_BEGIN
  4653. namespace detail
  4654. {
  4655. //////////////////
  4656. // constructors //
  4657. //////////////////
  4658. /*
  4659. * Note all external_constructor<>::construct functions need to call
  4660. * j.m_data.m_value.destroy(j.m_data.m_type) to avoid a memory leak in case j contains an
  4661. * allocated value (e.g., a string). See bug issue
  4662. * https://github.com/nlohmann/json/issues/2865 for more information.
  4663. */
  4664. template<value_t> struct external_constructor;
  4665. template<>
  4666. struct external_constructor<value_t::boolean>
  4667. {
  4668. template<typename BasicJsonType>
  4669. static void construct(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept
  4670. {
  4671. j.m_data.m_value.destroy(j.m_data.m_type);
  4672. j.m_data.m_type = value_t::boolean;
  4673. j.m_data.m_value = b;
  4674. j.assert_invariant();
  4675. }
  4676. };
  4677. template<>
  4678. struct external_constructor<value_t::string>
  4679. {
  4680. template<typename BasicJsonType>
  4681. static void construct(BasicJsonType& j, const typename BasicJsonType::string_t& s)
  4682. {
  4683. j.m_data.m_value.destroy(j.m_data.m_type);
  4684. j.m_data.m_type = value_t::string;
  4685. j.m_data.m_value = s;
  4686. j.assert_invariant();
  4687. }
  4688. template<typename BasicJsonType>
  4689. static void construct(BasicJsonType& j, typename BasicJsonType::string_t&& s)
  4690. {
  4691. j.m_data.m_value.destroy(j.m_data.m_type);
  4692. j.m_data.m_type = value_t::string;
  4693. j.m_data.m_value = std::move(s);
  4694. j.assert_invariant();
  4695. }
  4696. template < typename BasicJsonType, typename CompatibleStringType,
  4697. enable_if_t < !std::is_same<CompatibleStringType, typename BasicJsonType::string_t>::value,
  4698. int > = 0 >
  4699. static void construct(BasicJsonType& j, const CompatibleStringType& str)
  4700. {
  4701. j.m_data.m_value.destroy(j.m_data.m_type);
  4702. j.m_data.m_type = value_t::string;
  4703. j.m_data.m_value.string = j.template create<typename BasicJsonType::string_t>(str);
  4704. j.assert_invariant();
  4705. }
  4706. };
  4707. template<>
  4708. struct external_constructor<value_t::binary>
  4709. {
  4710. template<typename BasicJsonType>
  4711. static void construct(BasicJsonType& j, const typename BasicJsonType::binary_t& b)
  4712. {
  4713. j.m_data.m_value.destroy(j.m_data.m_type);
  4714. j.m_data.m_type = value_t::binary;
  4715. j.m_data.m_value = typename BasicJsonType::binary_t(b);
  4716. j.assert_invariant();
  4717. }
  4718. template<typename BasicJsonType>
  4719. static void construct(BasicJsonType& j, typename BasicJsonType::binary_t&& b)
  4720. {
  4721. j.m_data.m_value.destroy(j.m_data.m_type);
  4722. j.m_data.m_type = value_t::binary;
  4723. j.m_data.m_value = typename BasicJsonType::binary_t(std::move(b));
  4724. j.assert_invariant();
  4725. }
  4726. };
  4727. template<>
  4728. struct external_constructor<value_t::number_float>
  4729. {
  4730. template<typename BasicJsonType>
  4731. static void construct(BasicJsonType& j, typename BasicJsonType::number_float_t val) noexcept
  4732. {
  4733. j.m_data.m_value.destroy(j.m_data.m_type);
  4734. j.m_data.m_type = value_t::number_float;
  4735. j.m_data.m_value = val;
  4736. j.assert_invariant();
  4737. }
  4738. };
  4739. template<>
  4740. struct external_constructor<value_t::number_unsigned>
  4741. {
  4742. template<typename BasicJsonType>
  4743. static void construct(BasicJsonType& j, typename BasicJsonType::number_unsigned_t val) noexcept
  4744. {
  4745. j.m_data.m_value.destroy(j.m_data.m_type);
  4746. j.m_data.m_type = value_t::number_unsigned;
  4747. j.m_data.m_value = val;
  4748. j.assert_invariant();
  4749. }
  4750. };
  4751. template<>
  4752. struct external_constructor<value_t::number_integer>
  4753. {
  4754. template<typename BasicJsonType>
  4755. static void construct(BasicJsonType& j, typename BasicJsonType::number_integer_t val) noexcept
  4756. {
  4757. j.m_data.m_value.destroy(j.m_data.m_type);
  4758. j.m_data.m_type = value_t::number_integer;
  4759. j.m_data.m_value = val;
  4760. j.assert_invariant();
  4761. }
  4762. };
  4763. template<>
  4764. struct external_constructor<value_t::array>
  4765. {
  4766. template<typename BasicJsonType>
  4767. static void construct(BasicJsonType& j, const typename BasicJsonType::array_t& arr)
  4768. {
  4769. j.m_data.m_value.destroy(j.m_data.m_type);
  4770. j.m_data.m_type = value_t::array;
  4771. j.m_data.m_value = arr;
  4772. j.set_parents();
  4773. j.assert_invariant();
  4774. }
  4775. template<typename BasicJsonType>
  4776. static void construct(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
  4777. {
  4778. j.m_data.m_value.destroy(j.m_data.m_type);
  4779. j.m_data.m_type = value_t::array;
  4780. j.m_data.m_value = std::move(arr);
  4781. j.set_parents();
  4782. j.assert_invariant();
  4783. }
  4784. template < typename BasicJsonType, typename CompatibleArrayType,
  4785. enable_if_t < !std::is_same<CompatibleArrayType, typename BasicJsonType::array_t>::value,
  4786. int > = 0 >
  4787. static void construct(BasicJsonType& j, const CompatibleArrayType& arr)
  4788. {
  4789. using std::begin;
  4790. using std::end;
  4791. j.m_data.m_value.destroy(j.m_data.m_type);
  4792. j.m_data.m_type = value_t::array;
  4793. j.m_data.m_value.array = j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
  4794. j.set_parents();
  4795. j.assert_invariant();
  4796. }
  4797. template<typename BasicJsonType>
  4798. static void construct(BasicJsonType& j, const std::vector<bool>& arr)
  4799. {
  4800. j.m_data.m_value.destroy(j.m_data.m_type);
  4801. j.m_data.m_type = value_t::array;
  4802. j.m_data.m_value = value_t::array;
  4803. j.m_data.m_value.array->reserve(arr.size());
  4804. for (const bool x : arr)
  4805. {
  4806. j.m_data.m_value.array->push_back(x);
  4807. j.set_parent(j.m_data.m_value.array->back());
  4808. }
  4809. j.assert_invariant();
  4810. }
  4811. template<typename BasicJsonType, typename T,
  4812. enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
  4813. static void construct(BasicJsonType& j, const std::valarray<T>& arr)
  4814. {
  4815. j.m_data.m_value.destroy(j.m_data.m_type);
  4816. j.m_data.m_type = value_t::array;
  4817. j.m_data.m_value = value_t::array;
  4818. j.m_data.m_value.array->resize(arr.size());
  4819. if (arr.size() > 0)
  4820. {
  4821. std::copy(std::begin(arr), std::end(arr), j.m_data.m_value.array->begin());
  4822. }
  4823. j.set_parents();
  4824. j.assert_invariant();
  4825. }
  4826. };
  4827. template<>
  4828. struct external_constructor<value_t::object>
  4829. {
  4830. template<typename BasicJsonType>
  4831. static void construct(BasicJsonType& j, const typename BasicJsonType::object_t& obj)
  4832. {
  4833. j.m_data.m_value.destroy(j.m_data.m_type);
  4834. j.m_data.m_type = value_t::object;
  4835. j.m_data.m_value = obj;
  4836. j.set_parents();
  4837. j.assert_invariant();
  4838. }
  4839. template<typename BasicJsonType>
  4840. static void construct(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
  4841. {
  4842. j.m_data.m_value.destroy(j.m_data.m_type);
  4843. j.m_data.m_type = value_t::object;
  4844. j.m_data.m_value = std::move(obj);
  4845. j.set_parents();
  4846. j.assert_invariant();
  4847. }
  4848. template < typename BasicJsonType, typename CompatibleObjectType,
  4849. enable_if_t < !std::is_same<CompatibleObjectType, typename BasicJsonType::object_t>::value, int > = 0 >
  4850. static void construct(BasicJsonType& j, const CompatibleObjectType& obj)
  4851. {
  4852. using std::begin;
  4853. using std::end;
  4854. j.m_data.m_value.destroy(j.m_data.m_type);
  4855. j.m_data.m_type = value_t::object;
  4856. j.m_data.m_value.object = j.template create<typename BasicJsonType::object_t>(begin(obj), end(obj));
  4857. j.set_parents();
  4858. j.assert_invariant();
  4859. }
  4860. };
  4861. /////////////
  4862. // to_json //
  4863. /////////////
  4864. template<typename BasicJsonType, typename T,
  4865. enable_if_t<std::is_same<T, typename BasicJsonType::boolean_t>::value, int> = 0>
  4866. inline void to_json(BasicJsonType& j, T b) noexcept
  4867. {
  4868. external_constructor<value_t::boolean>::construct(j, b);
  4869. }
  4870. template < typename BasicJsonType, typename BoolRef,
  4871. enable_if_t <
  4872. ((std::is_same<std::vector<bool>::reference, BoolRef>::value
  4873. && !std::is_same <std::vector<bool>::reference, typename BasicJsonType::boolean_t&>::value)
  4874. || (std::is_same<std::vector<bool>::const_reference, BoolRef>::value
  4875. && !std::is_same <detail::uncvref_t<std::vector<bool>::const_reference>,
  4876. typename BasicJsonType::boolean_t >::value))
  4877. && std::is_convertible<const BoolRef&, typename BasicJsonType::boolean_t>::value, int > = 0 >
  4878. inline void to_json(BasicJsonType& j, const BoolRef& b) noexcept
  4879. {
  4880. external_constructor<value_t::boolean>::construct(j, static_cast<typename BasicJsonType::boolean_t>(b));
  4881. }
  4882. template<typename BasicJsonType, typename CompatibleString,
  4883. enable_if_t<std::is_constructible<typename BasicJsonType::string_t, CompatibleString>::value, int> = 0>
  4884. inline void to_json(BasicJsonType& j, const CompatibleString& s)
  4885. {
  4886. external_constructor<value_t::string>::construct(j, s);
  4887. }
  4888. template<typename BasicJsonType>
  4889. inline void to_json(BasicJsonType& j, typename BasicJsonType::string_t&& s)
  4890. {
  4891. external_constructor<value_t::string>::construct(j, std::move(s));
  4892. }
  4893. template<typename BasicJsonType, typename FloatType,
  4894. enable_if_t<std::is_floating_point<FloatType>::value, int> = 0>
  4895. inline void to_json(BasicJsonType& j, FloatType val) noexcept
  4896. {
  4897. external_constructor<value_t::number_float>::construct(j, static_cast<typename BasicJsonType::number_float_t>(val));
  4898. }
  4899. template<typename BasicJsonType, typename CompatibleNumberUnsignedType,
  4900. enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_unsigned_t, CompatibleNumberUnsignedType>::value, int> = 0>
  4901. inline void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val) noexcept
  4902. {
  4903. external_constructor<value_t::number_unsigned>::construct(j, static_cast<typename BasicJsonType::number_unsigned_t>(val));
  4904. }
  4905. template<typename BasicJsonType, typename CompatibleNumberIntegerType,
  4906. enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_integer_t, CompatibleNumberIntegerType>::value, int> = 0>
  4907. inline void to_json(BasicJsonType& j, CompatibleNumberIntegerType val) noexcept
  4908. {
  4909. external_constructor<value_t::number_integer>::construct(j, static_cast<typename BasicJsonType::number_integer_t>(val));
  4910. }
  4911. #if !JSON_DISABLE_ENUM_SERIALIZATION
  4912. template<typename BasicJsonType, typename EnumType,
  4913. enable_if_t<std::is_enum<EnumType>::value, int> = 0>
  4914. inline void to_json(BasicJsonType& j, EnumType e) noexcept
  4915. {
  4916. using underlying_type = typename std::underlying_type<EnumType>::type;
  4917. external_constructor<value_t::number_integer>::construct(j, static_cast<underlying_type>(e));
  4918. }
  4919. #endif // JSON_DISABLE_ENUM_SERIALIZATION
  4920. template<typename BasicJsonType>
  4921. inline void to_json(BasicJsonType& j, const std::vector<bool>& e)
  4922. {
  4923. external_constructor<value_t::array>::construct(j, e);
  4924. }
  4925. template < typename BasicJsonType, typename CompatibleArrayType,
  4926. enable_if_t < is_compatible_array_type<BasicJsonType,
  4927. CompatibleArrayType>::value&&
  4928. !is_compatible_object_type<BasicJsonType, CompatibleArrayType>::value&&
  4929. !is_compatible_string_type<BasicJsonType, CompatibleArrayType>::value&&
  4930. !std::is_same<typename BasicJsonType::binary_t, CompatibleArrayType>::value&&
  4931. !is_basic_json<CompatibleArrayType>::value,
  4932. int > = 0 >
  4933. inline void to_json(BasicJsonType& j, const CompatibleArrayType& arr)
  4934. {
  4935. external_constructor<value_t::array>::construct(j, arr);
  4936. }
  4937. template<typename BasicJsonType>
  4938. inline void to_json(BasicJsonType& j, const typename BasicJsonType::binary_t& bin)
  4939. {
  4940. external_constructor<value_t::binary>::construct(j, bin);
  4941. }
  4942. template<typename BasicJsonType, typename T,
  4943. enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
  4944. inline void to_json(BasicJsonType& j, const std::valarray<T>& arr)
  4945. {
  4946. external_constructor<value_t::array>::construct(j, std::move(arr));
  4947. }
  4948. template<typename BasicJsonType>
  4949. inline void to_json(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
  4950. {
  4951. external_constructor<value_t::array>::construct(j, std::move(arr));
  4952. }
  4953. template < typename BasicJsonType, typename CompatibleObjectType,
  4954. enable_if_t < is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value&& !is_basic_json<CompatibleObjectType>::value, int > = 0 >
  4955. inline void to_json(BasicJsonType& j, const CompatibleObjectType& obj)
  4956. {
  4957. external_constructor<value_t::object>::construct(j, obj);
  4958. }
  4959. template<typename BasicJsonType>
  4960. inline void to_json(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
  4961. {
  4962. external_constructor<value_t::object>::construct(j, std::move(obj));
  4963. }
  4964. template <
  4965. typename BasicJsonType, typename T, std::size_t N,
  4966. enable_if_t < !std::is_constructible<typename BasicJsonType::string_t,
  4967. const T(&)[N]>::value, // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  4968. int > = 0 >
  4969. inline void to_json(BasicJsonType& j, const T(&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  4970. {
  4971. external_constructor<value_t::array>::construct(j, arr);
  4972. }
  4973. template < typename BasicJsonType, typename T1, typename T2, enable_if_t < std::is_constructible<BasicJsonType, T1>::value&& std::is_constructible<BasicJsonType, T2>::value, int > = 0 >
  4974. inline void to_json(BasicJsonType& j, const std::pair<T1, T2>& p)
  4975. {
  4976. j = { p.first, p.second };
  4977. }
  4978. // for https://github.com/nlohmann/json/pull/1134
  4979. template<typename BasicJsonType, typename T,
  4980. enable_if_t<std::is_same<T, iteration_proxy_value<typename BasicJsonType::iterator>>::value, int> = 0>
  4981. inline void to_json(BasicJsonType& j, const T& b)
  4982. {
  4983. j = { {b.key(), b.value()} };
  4984. }
  4985. template<typename BasicJsonType, typename Tuple, std::size_t... Idx>
  4986. inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence<Idx...> /*unused*/)
  4987. {
  4988. j = { std::get<Idx>(t)... };
  4989. }
  4990. template<typename BasicJsonType, typename T, enable_if_t<is_constructible_tuple<BasicJsonType, T>::value, int > = 0>
  4991. inline void to_json(BasicJsonType& j, const T& t)
  4992. {
  4993. to_json_tuple_impl(j, t, make_index_sequence<std::tuple_size<T>::value> {});
  4994. }
  4995. #if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
  4996. template<typename BasicJsonType>
  4997. inline void to_json(BasicJsonType& j, const std_fs::path& p)
  4998. {
  4999. j = p.string();
  5000. }
  5001. #endif
  5002. struct to_json_fn
  5003. {
  5004. template<typename BasicJsonType, typename T>
  5005. auto operator()(BasicJsonType& j, T&& val) const noexcept(noexcept(to_json(j, std::forward<T>(val))))
  5006. -> decltype(to_json(j, std::forward<T>(val)), void())
  5007. {
  5008. return to_json(j, std::forward<T>(val));
  5009. }
  5010. };
  5011. } // namespace detail
  5012. #ifndef JSON_HAS_CPP_17
  5013. /// namespace to hold default `to_json` function
  5014. /// to see why this is required:
  5015. /// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
  5016. namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces)
  5017. {
  5018. #endif
  5019. JSON_INLINE_VARIABLE constexpr const auto& to_json = // NOLINT(misc-definitions-in-headers)
  5020. detail::static_const<detail::to_json_fn>::value;
  5021. #ifndef JSON_HAS_CPP_17
  5022. } // namespace
  5023. #endif
  5024. NLOHMANN_JSON_NAMESPACE_END
  5025. // #include <nlohmann/detail/meta/identity_tag.hpp>
  5026. NLOHMANN_JSON_NAMESPACE_BEGIN
  5027. /// @sa https://json.nlohmann.me/api/adl_serializer/
  5028. template<typename ValueType, typename>
  5029. struct adl_serializer
  5030. {
  5031. /// @brief convert a JSON value to any value type
  5032. /// @sa https://json.nlohmann.me/api/adl_serializer/from_json/
  5033. template<typename BasicJsonType, typename TargetType = ValueType>
  5034. static auto from_json(BasicJsonType && j, TargetType& val) noexcept(
  5035. noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), val)))
  5036. -> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), val), void())
  5037. {
  5038. ::nlohmann::from_json(std::forward<BasicJsonType>(j), val);
  5039. }
  5040. /// @brief convert a JSON value to any value type
  5041. /// @sa https://json.nlohmann.me/api/adl_serializer/from_json/
  5042. template<typename BasicJsonType, typename TargetType = ValueType>
  5043. static auto from_json(BasicJsonType && j) noexcept(
  5044. noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {})))
  5045. -> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {}))
  5046. {
  5047. return ::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {});
  5048. }
  5049. /// @brief convert any value type to a JSON value
  5050. /// @sa https://json.nlohmann.me/api/adl_serializer/to_json/
  5051. template<typename BasicJsonType, typename TargetType = ValueType>
  5052. static auto to_json(BasicJsonType& j, TargetType && val) noexcept(
  5053. noexcept(::nlohmann::to_json(j, std::forward<TargetType>(val))))
  5054. -> decltype(::nlohmann::to_json(j, std::forward<TargetType>(val)), void())
  5055. {
  5056. ::nlohmann::to_json(j, std::forward<TargetType>(val));
  5057. }
  5058. };
  5059. NLOHMANN_JSON_NAMESPACE_END
  5060. // #include <nlohmann/byte_container_with_subtype.hpp>
  5061. // __ _____ _____ _____
  5062. // __| | __| | | | JSON for Modern C++
  5063. // | | |__ | | | | | | version 3.11.2
  5064. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5065. //
  5066. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  5067. // SPDX-License-Identifier: MIT
  5068. #include <cstdint> // uint8_t, uint64_t
  5069. #include <tuple> // tie
  5070. #include <utility> // move
  5071. // #include <nlohmann/detail/abi_macros.hpp>
  5072. NLOHMANN_JSON_NAMESPACE_BEGIN
  5073. /// @brief an internal type for a backed binary type
  5074. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/
  5075. template<typename BinaryType>
  5076. class byte_container_with_subtype : public BinaryType
  5077. {
  5078. public:
  5079. using container_type = BinaryType;
  5080. using subtype_type = std::uint64_t;
  5081. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5082. byte_container_with_subtype() noexcept(noexcept(container_type()))
  5083. : container_type()
  5084. {}
  5085. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5086. byte_container_with_subtype(const container_type& b) noexcept(noexcept(container_type(b)))
  5087. : container_type(b)
  5088. {}
  5089. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5090. byte_container_with_subtype(container_type&& b) noexcept(noexcept(container_type(std::move(b))))
  5091. : container_type(std::move(b))
  5092. {}
  5093. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5094. byte_container_with_subtype(const container_type& b, subtype_type subtype_) noexcept(noexcept(container_type(b)))
  5095. : container_type(b)
  5096. , m_subtype(subtype_)
  5097. , m_has_subtype(true)
  5098. {}
  5099. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5100. byte_container_with_subtype(container_type&& b, subtype_type subtype_) noexcept(noexcept(container_type(std::move(b))))
  5101. : container_type(std::move(b))
  5102. , m_subtype(subtype_)
  5103. , m_has_subtype(true)
  5104. {}
  5105. bool operator==(const byte_container_with_subtype& rhs) const
  5106. {
  5107. return std::tie(static_cast<const BinaryType&>(*this), m_subtype, m_has_subtype) ==
  5108. std::tie(static_cast<const BinaryType&>(rhs), rhs.m_subtype, rhs.m_has_subtype);
  5109. }
  5110. bool operator!=(const byte_container_with_subtype& rhs) const
  5111. {
  5112. return !(rhs == *this);
  5113. }
  5114. /// @brief sets the binary subtype
  5115. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/set_subtype/
  5116. void set_subtype(subtype_type subtype_) noexcept
  5117. {
  5118. m_subtype = subtype_;
  5119. m_has_subtype = true;
  5120. }
  5121. /// @brief return the binary subtype
  5122. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/subtype/
  5123. constexpr subtype_type subtype() const noexcept
  5124. {
  5125. return m_has_subtype ? m_subtype : static_cast<subtype_type>(-1);
  5126. }
  5127. /// @brief return whether the value has a subtype
  5128. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/has_subtype/
  5129. constexpr bool has_subtype() const noexcept
  5130. {
  5131. return m_has_subtype;
  5132. }
  5133. /// @brief clears the binary subtype
  5134. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/clear_subtype/
  5135. void clear_subtype() noexcept
  5136. {
  5137. m_subtype = 0;
  5138. m_has_subtype = false;
  5139. }
  5140. private:
  5141. subtype_type m_subtype = 0;
  5142. bool m_has_subtype = false;
  5143. };
  5144. NLOHMANN_JSON_NAMESPACE_END
  5145. // #include <nlohmann/detail/conversions/from_json.hpp>
  5146. // #include <nlohmann/detail/conversions/to_json.hpp>
  5147. // #include <nlohmann/detail/exceptions.hpp>
  5148. // #include <nlohmann/detail/hash.hpp>
  5149. // __ _____ _____ _____
  5150. // __| | __| | | | JSON for Modern C++
  5151. // | | |__ | | | | | | version 3.11.2
  5152. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5153. //
  5154. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  5155. // SPDX-License-Identifier: MIT
  5156. #include <cstdint> // uint8_t
  5157. #include <cstddef> // size_t
  5158. #include <functional> // hash
  5159. // #include <nlohmann/detail/abi_macros.hpp>
  5160. // #include <nlohmann/detail/value_t.hpp>
  5161. NLOHMANN_JSON_NAMESPACE_BEGIN
  5162. namespace detail
  5163. {
  5164. // boost::hash_combine
  5165. inline std::size_t combine(std::size_t seed, std::size_t h) noexcept
  5166. {
  5167. seed ^= h + 0x9e3779b9 + (seed << 6U) + (seed >> 2U);
  5168. return seed;
  5169. }
  5170. /*!
  5171. @brief hash a JSON value
  5172. The hash function tries to rely on std::hash where possible. Furthermore, the
  5173. type of the JSON value is taken into account to have different hash values for
  5174. null, 0, 0U, and false, etc.
  5175. @tparam BasicJsonType basic_json specialization
  5176. @param j JSON value to hash
  5177. @return hash value of j
  5178. */
  5179. template<typename BasicJsonType>
  5180. std::size_t hash(const BasicJsonType& j)
  5181. {
  5182. using string_t = typename BasicJsonType::string_t;
  5183. using number_integer_t = typename BasicJsonType::number_integer_t;
  5184. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  5185. using number_float_t = typename BasicJsonType::number_float_t;
  5186. const auto type = static_cast<std::size_t>(j.type());
  5187. switch (j.type())
  5188. {
  5189. case BasicJsonType::value_t::null:
  5190. case BasicJsonType::value_t::discarded:
  5191. {
  5192. return combine(type, 0);
  5193. }
  5194. case BasicJsonType::value_t::object:
  5195. {
  5196. auto seed = combine(type, j.size());
  5197. for (const auto& element : j.items())
  5198. {
  5199. const auto h = std::hash<string_t> {}(element.key());
  5200. seed = combine(seed, h);
  5201. seed = combine(seed, hash(element.value()));
  5202. }
  5203. return seed;
  5204. }
  5205. case BasicJsonType::value_t::array:
  5206. {
  5207. auto seed = combine(type, j.size());
  5208. for (const auto& element : j)
  5209. {
  5210. seed = combine(seed, hash(element));
  5211. }
  5212. return seed;
  5213. }
  5214. case BasicJsonType::value_t::string:
  5215. {
  5216. const auto h = std::hash<string_t> {}(j.template get_ref<const string_t&>());
  5217. return combine(type, h);
  5218. }
  5219. case BasicJsonType::value_t::boolean:
  5220. {
  5221. const auto h = std::hash<bool> {}(j.template get<bool>());
  5222. return combine(type, h);
  5223. }
  5224. case BasicJsonType::value_t::number_integer:
  5225. {
  5226. const auto h = std::hash<number_integer_t> {}(j.template get<number_integer_t>());
  5227. return combine(type, h);
  5228. }
  5229. case BasicJsonType::value_t::number_unsigned:
  5230. {
  5231. const auto h = std::hash<number_unsigned_t> {}(j.template get<number_unsigned_t>());
  5232. return combine(type, h);
  5233. }
  5234. case BasicJsonType::value_t::number_float:
  5235. {
  5236. const auto h = std::hash<number_float_t> {}(j.template get<number_float_t>());
  5237. return combine(type, h);
  5238. }
  5239. case BasicJsonType::value_t::binary:
  5240. {
  5241. auto seed = combine(type, j.get_binary().size());
  5242. const auto h = std::hash<bool> {}(j.get_binary().has_subtype());
  5243. seed = combine(seed, h);
  5244. seed = combine(seed, static_cast<std::size_t>(j.get_binary().subtype()));
  5245. for (const auto byte : j.get_binary())
  5246. {
  5247. seed = combine(seed, std::hash<std::uint8_t> {}(byte));
  5248. }
  5249. return seed;
  5250. }
  5251. default: // LCOV_EXCL_LINE
  5252. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  5253. return 0; // LCOV_EXCL_LINE
  5254. }
  5255. }
  5256. } // namespace detail
  5257. NLOHMANN_JSON_NAMESPACE_END
  5258. // #include <nlohmann/detail/input/binary_reader.hpp>
  5259. // __ _____ _____ _____
  5260. // __| | __| | | | JSON for Modern C++
  5261. // | | |__ | | | | | | version 3.11.2
  5262. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5263. //
  5264. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  5265. // SPDX-License-Identifier: MIT
  5266. #include <algorithm> // generate_n
  5267. #include <array> // array
  5268. #include <cmath> // ldexp
  5269. #include <cstddef> // size_t
  5270. #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
  5271. #include <cstdio> // snprintf
  5272. #include <cstring> // memcpy
  5273. #include <iterator> // back_inserter
  5274. #include <limits> // numeric_limits
  5275. #include <string> // char_traits, string
  5276. #include <utility> // make_pair, move
  5277. #include <vector> // vector
  5278. // #include <nlohmann/detail/exceptions.hpp>
  5279. // #include <nlohmann/detail/input/input_adapters.hpp>
  5280. // __ _____ _____ _____
  5281. // __| | __| | | | JSON for Modern C++
  5282. // | | |__ | | | | | | version 3.11.2
  5283. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5284. //
  5285. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  5286. // SPDX-License-Identifier: MIT
  5287. #include <array> // array
  5288. #include <cstddef> // size_t
  5289. #include <cstring> // strlen
  5290. #include <iterator> // begin, end, iterator_traits, random_access_iterator_tag, distance, next
  5291. #include <memory> // shared_ptr, make_shared, addressof
  5292. #include <numeric> // accumulate
  5293. #include <string> // string, char_traits
  5294. #include <type_traits> // enable_if, is_base_of, is_pointer, is_integral, remove_pointer
  5295. #include <utility> // pair, declval
  5296. #ifndef JSON_NO_IO
  5297. #include <cstdio> // FILE *
  5298. #include <istream> // istream
  5299. #endif // JSON_NO_IO
  5300. // #include <nlohmann/detail/iterators/iterator_traits.hpp>
  5301. // #include <nlohmann/detail/macro_scope.hpp>
  5302. NLOHMANN_JSON_NAMESPACE_BEGIN
  5303. namespace detail
  5304. {
  5305. /// the supported input formats
  5306. enum class input_format_t { json, cbor, msgpack, ubjson, bson, bjdata };
  5307. ////////////////////
  5308. // input adapters //
  5309. ////////////////////
  5310. #ifndef JSON_NO_IO
  5311. /*!
  5312. Input adapter for stdio file access. This adapter read only 1 byte and do not use any
  5313. buffer. This adapter is a very low level adapter.
  5314. */
  5315. class file_input_adapter
  5316. {
  5317. public:
  5318. using char_type = char;
  5319. JSON_HEDLEY_NON_NULL(2)
  5320. explicit file_input_adapter(std::FILE* f) noexcept
  5321. : m_file(f)
  5322. {
  5323. JSON_ASSERT(m_file != nullptr);
  5324. }
  5325. // make class move-only
  5326. file_input_adapter(const file_input_adapter&) = delete;
  5327. file_input_adapter(file_input_adapter&&) noexcept = default;
  5328. file_input_adapter& operator=(const file_input_adapter&) = delete;
  5329. file_input_adapter& operator=(file_input_adapter&&) = delete;
  5330. ~file_input_adapter() = default;
  5331. std::char_traits<char>::int_type get_character() noexcept
  5332. {
  5333. return std::fgetc(m_file);
  5334. }
  5335. private:
  5336. /// the file pointer to read from
  5337. std::FILE* m_file;
  5338. };
  5339. /*!
  5340. Input adapter for a (caching) istream. Ignores a UFT Byte Order Mark at
  5341. beginning of input. Does not support changing the underlying std::streambuf
  5342. in mid-input. Maintains underlying std::istream and std::streambuf to support
  5343. subsequent use of standard std::istream operations to process any input
  5344. characters following those used in parsing the JSON input. Clears the
  5345. std::istream flags; any input errors (e.g., EOF) will be detected by the first
  5346. subsequent call for input from the std::istream.
  5347. */
  5348. class input_stream_adapter
  5349. {
  5350. public:
  5351. using char_type = char;
  5352. ~input_stream_adapter()
  5353. {
  5354. // clear stream flags; we use underlying streambuf I/O, do not
  5355. // maintain ifstream flags, except eof
  5356. if (is != nullptr)
  5357. {
  5358. is->clear(is->rdstate() & std::ios::eofbit);
  5359. }
  5360. }
  5361. explicit input_stream_adapter(std::istream& i)
  5362. : is(&i), sb(i.rdbuf())
  5363. {}
  5364. // delete because of pointer members
  5365. input_stream_adapter(const input_stream_adapter&) = delete;
  5366. input_stream_adapter& operator=(input_stream_adapter&) = delete;
  5367. input_stream_adapter& operator=(input_stream_adapter&&) = delete;
  5368. input_stream_adapter(input_stream_adapter&& rhs) noexcept
  5369. : is(rhs.is), sb(rhs.sb)
  5370. {
  5371. rhs.is = nullptr;
  5372. rhs.sb = nullptr;
  5373. }
  5374. // std::istream/std::streambuf use std::char_traits<char>::to_int_type, to
  5375. // ensure that std::char_traits<char>::eof() and the character 0xFF do not
  5376. // end up as the same value, e.g. 0xFFFFFFFF.
  5377. std::char_traits<char>::int_type get_character()
  5378. {
  5379. auto res = sb->sbumpc();
  5380. // set eof manually, as we don't use the istream interface.
  5381. if (JSON_HEDLEY_UNLIKELY(res == std::char_traits<char>::eof()))
  5382. {
  5383. is->clear(is->rdstate() | std::ios::eofbit);
  5384. }
  5385. return res;
  5386. }
  5387. private:
  5388. /// the associated input stream
  5389. std::istream* is = nullptr;
  5390. std::streambuf* sb = nullptr;
  5391. };
  5392. #endif // JSON_NO_IO
  5393. // General-purpose iterator-based adapter. It might not be as fast as
  5394. // theoretically possible for some containers, but it is extremely versatile.
  5395. template<typename IteratorType>
  5396. class iterator_input_adapter
  5397. {
  5398. public:
  5399. using char_type = typename std::iterator_traits<IteratorType>::value_type;
  5400. iterator_input_adapter(IteratorType first, IteratorType last)
  5401. : current(std::move(first)), end(std::move(last))
  5402. {}
  5403. typename std::char_traits<char_type>::int_type get_character()
  5404. {
  5405. if (JSON_HEDLEY_LIKELY(current != end))
  5406. {
  5407. auto result = std::char_traits<char_type>::to_int_type(*current);
  5408. std::advance(current, 1);
  5409. return result;
  5410. }
  5411. return std::char_traits<char_type>::eof();
  5412. }
  5413. private:
  5414. IteratorType current;
  5415. IteratorType end;
  5416. template<typename BaseInputAdapter, size_t T>
  5417. friend struct wide_string_input_helper;
  5418. bool empty() const
  5419. {
  5420. return current == end;
  5421. }
  5422. };
  5423. template<typename BaseInputAdapter, size_t T>
  5424. struct wide_string_input_helper;
  5425. template<typename BaseInputAdapter>
  5426. struct wide_string_input_helper<BaseInputAdapter, 4>
  5427. {
  5428. // UTF-32
  5429. static void fill_buffer(BaseInputAdapter& input,
  5430. std::array<std::char_traits<char>::int_type, 4>& utf8_bytes,
  5431. size_t& utf8_bytes_index,
  5432. size_t& utf8_bytes_filled)
  5433. {
  5434. utf8_bytes_index = 0;
  5435. if (JSON_HEDLEY_UNLIKELY(input.empty()))
  5436. {
  5437. utf8_bytes[0] = std::char_traits<char>::eof();
  5438. utf8_bytes_filled = 1;
  5439. }
  5440. else
  5441. {
  5442. // get the current character
  5443. const auto wc = input.get_character();
  5444. // UTF-32 to UTF-8 encoding
  5445. if (wc < 0x80)
  5446. {
  5447. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  5448. utf8_bytes_filled = 1;
  5449. }
  5450. else if (wc <= 0x7FF)
  5451. {
  5452. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xC0u | ((static_cast<unsigned int>(wc) >> 6u) & 0x1Fu));
  5453. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5454. utf8_bytes_filled = 2;
  5455. }
  5456. else if (wc <= 0xFFFF)
  5457. {
  5458. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xE0u | ((static_cast<unsigned int>(wc) >> 12u) & 0x0Fu));
  5459. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
  5460. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5461. utf8_bytes_filled = 3;
  5462. }
  5463. else if (wc <= 0x10FFFF)
  5464. {
  5465. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xF0u | ((static_cast<unsigned int>(wc) >> 18u) & 0x07u));
  5466. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 12u) & 0x3Fu));
  5467. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
  5468. utf8_bytes[3] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5469. utf8_bytes_filled = 4;
  5470. }
  5471. else
  5472. {
  5473. // unknown character
  5474. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  5475. utf8_bytes_filled = 1;
  5476. }
  5477. }
  5478. }
  5479. };
  5480. template<typename BaseInputAdapter>
  5481. struct wide_string_input_helper<BaseInputAdapter, 2>
  5482. {
  5483. // UTF-16
  5484. static void fill_buffer(BaseInputAdapter& input,
  5485. std::array<std::char_traits<char>::int_type, 4>& utf8_bytes,
  5486. size_t& utf8_bytes_index,
  5487. size_t& utf8_bytes_filled)
  5488. {
  5489. utf8_bytes_index = 0;
  5490. if (JSON_HEDLEY_UNLIKELY(input.empty()))
  5491. {
  5492. utf8_bytes[0] = std::char_traits<char>::eof();
  5493. utf8_bytes_filled = 1;
  5494. }
  5495. else
  5496. {
  5497. // get the current character
  5498. const auto wc = input.get_character();
  5499. // UTF-16 to UTF-8 encoding
  5500. if (wc < 0x80)
  5501. {
  5502. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  5503. utf8_bytes_filled = 1;
  5504. }
  5505. else if (wc <= 0x7FF)
  5506. {
  5507. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xC0u | ((static_cast<unsigned int>(wc) >> 6u)));
  5508. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5509. utf8_bytes_filled = 2;
  5510. }
  5511. else if (0xD800 > wc || wc >= 0xE000)
  5512. {
  5513. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xE0u | ((static_cast<unsigned int>(wc) >> 12u)));
  5514. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
  5515. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5516. utf8_bytes_filled = 3;
  5517. }
  5518. else
  5519. {
  5520. if (JSON_HEDLEY_UNLIKELY(!input.empty()))
  5521. {
  5522. const auto wc2 = static_cast<unsigned int>(input.get_character());
  5523. const auto charcode = 0x10000u + (((static_cast<unsigned int>(wc) & 0x3FFu) << 10u) | (wc2 & 0x3FFu));
  5524. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xF0u | (charcode >> 18u));
  5525. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((charcode >> 12u) & 0x3Fu));
  5526. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | ((charcode >> 6u) & 0x3Fu));
  5527. utf8_bytes[3] = static_cast<std::char_traits<char>::int_type>(0x80u | (charcode & 0x3Fu));
  5528. utf8_bytes_filled = 4;
  5529. }
  5530. else
  5531. {
  5532. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  5533. utf8_bytes_filled = 1;
  5534. }
  5535. }
  5536. }
  5537. }
  5538. };
  5539. // Wraps another input adapter to convert wide character types into individual bytes.
  5540. template<typename BaseInputAdapter, typename WideCharType>
  5541. class wide_string_input_adapter
  5542. {
  5543. public:
  5544. using char_type = char;
  5545. wide_string_input_adapter(BaseInputAdapter base)
  5546. : base_adapter(base) {}
  5547. typename std::char_traits<char>::int_type get_character() noexcept
  5548. {
  5549. // check if buffer needs to be filled
  5550. if (utf8_bytes_index == utf8_bytes_filled)
  5551. {
  5552. fill_buffer<sizeof(WideCharType)>();
  5553. JSON_ASSERT(utf8_bytes_filled > 0);
  5554. JSON_ASSERT(utf8_bytes_index == 0);
  5555. }
  5556. // use buffer
  5557. JSON_ASSERT(utf8_bytes_filled > 0);
  5558. JSON_ASSERT(utf8_bytes_index < utf8_bytes_filled);
  5559. return utf8_bytes[utf8_bytes_index++];
  5560. }
  5561. private:
  5562. BaseInputAdapter base_adapter;
  5563. template<size_t T>
  5564. void fill_buffer()
  5565. {
  5566. wide_string_input_helper<BaseInputAdapter, T>::fill_buffer(base_adapter, utf8_bytes, utf8_bytes_index, utf8_bytes_filled);
  5567. }
  5568. /// a buffer for UTF-8 bytes
  5569. std::array<std::char_traits<char>::int_type, 4> utf8_bytes = {{0, 0, 0, 0}};
  5570. /// index to the utf8_codes array for the next valid byte
  5571. std::size_t utf8_bytes_index = 0;
  5572. /// number of valid bytes in the utf8_codes array
  5573. std::size_t utf8_bytes_filled = 0;
  5574. };
  5575. template<typename IteratorType, typename Enable = void>
  5576. struct iterator_input_adapter_factory
  5577. {
  5578. using iterator_type = IteratorType;
  5579. using char_type = typename std::iterator_traits<iterator_type>::value_type;
  5580. using adapter_type = iterator_input_adapter<iterator_type>;
  5581. static adapter_type create(IteratorType first, IteratorType last)
  5582. {
  5583. return adapter_type(std::move(first), std::move(last));
  5584. }
  5585. };
  5586. template<typename T>
  5587. struct is_iterator_of_multibyte
  5588. {
  5589. using value_type = typename std::iterator_traits<T>::value_type;
  5590. enum
  5591. {
  5592. value = sizeof(value_type) > 1
  5593. };
  5594. };
  5595. template<typename IteratorType>
  5596. struct iterator_input_adapter_factory<IteratorType, enable_if_t<is_iterator_of_multibyte<IteratorType>::value>>
  5597. {
  5598. using iterator_type = IteratorType;
  5599. using char_type = typename std::iterator_traits<iterator_type>::value_type;
  5600. using base_adapter_type = iterator_input_adapter<iterator_type>;
  5601. using adapter_type = wide_string_input_adapter<base_adapter_type, char_type>;
  5602. static adapter_type create(IteratorType first, IteratorType last)
  5603. {
  5604. return adapter_type(base_adapter_type(std::move(first), std::move(last)));
  5605. }
  5606. };
  5607. // General purpose iterator-based input
  5608. template<typename IteratorType>
  5609. typename iterator_input_adapter_factory<IteratorType>::adapter_type input_adapter(IteratorType first, IteratorType last)
  5610. {
  5611. using factory_type = iterator_input_adapter_factory<IteratorType>;
  5612. return factory_type::create(first, last);
  5613. }
  5614. // Convenience shorthand from container to iterator
  5615. // Enables ADL on begin(container) and end(container)
  5616. // Encloses the using declarations in namespace for not to leak them to outside scope
  5617. namespace container_input_adapter_factory_impl
  5618. {
  5619. using std::begin;
  5620. using std::end;
  5621. template<typename ContainerType, typename Enable = void>
  5622. struct container_input_adapter_factory {};
  5623. template<typename ContainerType>
  5624. struct container_input_adapter_factory< ContainerType,
  5625. void_t<decltype(begin(std::declval<ContainerType>()), end(std::declval<ContainerType>()))>>
  5626. {
  5627. using adapter_type = decltype(input_adapter(begin(std::declval<ContainerType>()), end(std::declval<ContainerType>())));
  5628. static adapter_type create(const ContainerType& container)
  5629. {
  5630. return input_adapter(begin(container), end(container));
  5631. }
  5632. };
  5633. } // namespace container_input_adapter_factory_impl
  5634. template<typename ContainerType>
  5635. typename container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::adapter_type input_adapter(const ContainerType& container)
  5636. {
  5637. return container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::create(container);
  5638. }
  5639. #ifndef JSON_NO_IO
  5640. // Special cases with fast paths
  5641. inline file_input_adapter input_adapter(std::FILE* file)
  5642. {
  5643. return file_input_adapter(file);
  5644. }
  5645. inline input_stream_adapter input_adapter(std::istream& stream)
  5646. {
  5647. return input_stream_adapter(stream);
  5648. }
  5649. inline input_stream_adapter input_adapter(std::istream&& stream)
  5650. {
  5651. return input_stream_adapter(stream);
  5652. }
  5653. #endif // JSON_NO_IO
  5654. using contiguous_bytes_input_adapter = decltype(input_adapter(std::declval<const char*>(), std::declval<const char*>()));
  5655. // Null-delimited strings, and the like.
  5656. template < typename CharT,
  5657. typename std::enable_if <
  5658. std::is_pointer<CharT>::value&&
  5659. !std::is_array<CharT>::value&&
  5660. std::is_integral<typename std::remove_pointer<CharT>::type>::value&&
  5661. sizeof(typename std::remove_pointer<CharT>::type) == 1,
  5662. int >::type = 0 >
  5663. contiguous_bytes_input_adapter input_adapter(CharT b)
  5664. {
  5665. auto length = std::strlen(reinterpret_cast<const char*>(b));
  5666. const auto* ptr = reinterpret_cast<const char*>(b);
  5667. return input_adapter(ptr, ptr + length);
  5668. }
  5669. template<typename T, std::size_t N>
  5670. 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)
  5671. {
  5672. return input_adapter(array, array + N);
  5673. }
  5674. // This class only handles inputs of input_buffer_adapter type.
  5675. // It's required so that expressions like {ptr, len} can be implicitly cast
  5676. // to the correct adapter.
  5677. class span_input_adapter
  5678. {
  5679. public:
  5680. template < typename CharT,
  5681. typename std::enable_if <
  5682. std::is_pointer<CharT>::value&&
  5683. std::is_integral<typename std::remove_pointer<CharT>::type>::value&&
  5684. sizeof(typename std::remove_pointer<CharT>::type) == 1,
  5685. int >::type = 0 >
  5686. span_input_adapter(CharT b, std::size_t l)
  5687. : ia(reinterpret_cast<const char*>(b), reinterpret_cast<const char*>(b) + l) {}
  5688. template<class IteratorType,
  5689. typename std::enable_if<
  5690. std::is_same<typename iterator_traits<IteratorType>::iterator_category, std::random_access_iterator_tag>::value,
  5691. int>::type = 0>
  5692. span_input_adapter(IteratorType first, IteratorType last)
  5693. : ia(input_adapter(first, last)) {}
  5694. contiguous_bytes_input_adapter&& get()
  5695. {
  5696. return std::move(ia); // NOLINT(hicpp-move-const-arg,performance-move-const-arg)
  5697. }
  5698. private:
  5699. contiguous_bytes_input_adapter ia;
  5700. };
  5701. } // namespace detail
  5702. NLOHMANN_JSON_NAMESPACE_END
  5703. // #include <nlohmann/detail/input/json_sax.hpp>
  5704. // __ _____ _____ _____
  5705. // __| | __| | | | JSON for Modern C++
  5706. // | | |__ | | | | | | version 3.11.2
  5707. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5708. //
  5709. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  5710. // SPDX-License-Identifier: MIT
  5711. #include <cstddef>
  5712. #include <string> // string
  5713. #include <utility> // move
  5714. #include <vector> // vector
  5715. // #include <nlohmann/detail/exceptions.hpp>
  5716. // #include <nlohmann/detail/macro_scope.hpp>
  5717. // #include <nlohmann/detail/string_concat.hpp>
  5718. NLOHMANN_JSON_NAMESPACE_BEGIN
  5719. /*!
  5720. @brief SAX interface
  5721. This class describes the SAX interface used by @ref nlohmann::json::sax_parse.
  5722. Each function is called in different situations while the input is parsed. The
  5723. boolean return value informs the parser whether to continue processing the
  5724. input.
  5725. */
  5726. template<typename BasicJsonType>
  5727. struct json_sax
  5728. {
  5729. using number_integer_t = typename BasicJsonType::number_integer_t;
  5730. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  5731. using number_float_t = typename BasicJsonType::number_float_t;
  5732. using string_t = typename BasicJsonType::string_t;
  5733. using binary_t = typename BasicJsonType::binary_t;
  5734. /*!
  5735. @brief a null value was read
  5736. @return whether parsing should proceed
  5737. */
  5738. virtual bool null() = 0;
  5739. /*!
  5740. @brief a boolean value was read
  5741. @param[in] val boolean value
  5742. @return whether parsing should proceed
  5743. */
  5744. virtual bool boolean(bool val) = 0;
  5745. /*!
  5746. @brief an integer number was read
  5747. @param[in] val integer value
  5748. @return whether parsing should proceed
  5749. */
  5750. virtual bool number_integer(number_integer_t val) = 0;
  5751. /*!
  5752. @brief an unsigned integer number was read
  5753. @param[in] val unsigned integer value
  5754. @return whether parsing should proceed
  5755. */
  5756. virtual bool number_unsigned(number_unsigned_t val) = 0;
  5757. /*!
  5758. @brief a floating-point number was read
  5759. @param[in] val floating-point value
  5760. @param[in] s raw token value
  5761. @return whether parsing should proceed
  5762. */
  5763. virtual bool number_float(number_float_t val, const string_t& s) = 0;
  5764. /*!
  5765. @brief a string value was read
  5766. @param[in] val string value
  5767. @return whether parsing should proceed
  5768. @note It is safe to move the passed string value.
  5769. */
  5770. virtual bool string(string_t& val) = 0;
  5771. /*!
  5772. @brief a binary value was read
  5773. @param[in] val binary value
  5774. @return whether parsing should proceed
  5775. @note It is safe to move the passed binary value.
  5776. */
  5777. virtual bool binary(binary_t& val) = 0;
  5778. /*!
  5779. @brief the beginning of an object was read
  5780. @param[in] elements number of object elements or -1 if unknown
  5781. @return whether parsing should proceed
  5782. @note binary formats may report the number of elements
  5783. */
  5784. virtual bool start_object(std::size_t elements) = 0;
  5785. /*!
  5786. @brief an object key was read
  5787. @param[in] val object key
  5788. @return whether parsing should proceed
  5789. @note It is safe to move the passed string.
  5790. */
  5791. virtual bool key(string_t& val) = 0;
  5792. /*!
  5793. @brief the end of an object was read
  5794. @return whether parsing should proceed
  5795. */
  5796. virtual bool end_object() = 0;
  5797. /*!
  5798. @brief the beginning of an array was read
  5799. @param[in] elements number of array elements or -1 if unknown
  5800. @return whether parsing should proceed
  5801. @note binary formats may report the number of elements
  5802. */
  5803. virtual bool start_array(std::size_t elements) = 0;
  5804. /*!
  5805. @brief the end of an array was read
  5806. @return whether parsing should proceed
  5807. */
  5808. virtual bool end_array() = 0;
  5809. /*!
  5810. @brief a parse error occurred
  5811. @param[in] position the position in the input where the error occurs
  5812. @param[in] last_token the last read token
  5813. @param[in] ex an exception object describing the error
  5814. @return whether parsing should proceed (must return false)
  5815. */
  5816. virtual bool parse_error(std::size_t position,
  5817. const std::string& last_token,
  5818. const detail::exception& ex) = 0;
  5819. json_sax() = default;
  5820. json_sax(const json_sax&) = default;
  5821. json_sax(json_sax&&) noexcept = default;
  5822. json_sax& operator=(const json_sax&) = default;
  5823. json_sax& operator=(json_sax&&) noexcept = default;
  5824. virtual ~json_sax() = default;
  5825. };
  5826. namespace detail
  5827. {
  5828. /*!
  5829. @brief SAX implementation to create a JSON value from SAX events
  5830. This class implements the @ref json_sax interface and processes the SAX events
  5831. to create a JSON value which makes it basically a DOM parser. The structure or
  5832. hierarchy of the JSON value is managed by the stack `ref_stack` which contains
  5833. a pointer to the respective array or object for each recursion depth.
  5834. After successful parsing, the value that is passed by reference to the
  5835. constructor contains the parsed value.
  5836. @tparam BasicJsonType the JSON type
  5837. */
  5838. template<typename BasicJsonType>
  5839. class json_sax_dom_parser
  5840. {
  5841. public:
  5842. using number_integer_t = typename BasicJsonType::number_integer_t;
  5843. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  5844. using number_float_t = typename BasicJsonType::number_float_t;
  5845. using string_t = typename BasicJsonType::string_t;
  5846. using binary_t = typename BasicJsonType::binary_t;
  5847. /*!
  5848. @param[in,out] r reference to a JSON value that is manipulated while
  5849. parsing
  5850. @param[in] allow_exceptions_ whether parse errors yield exceptions
  5851. */
  5852. explicit json_sax_dom_parser(BasicJsonType& r, const bool allow_exceptions_ = true)
  5853. : root(r), allow_exceptions(allow_exceptions_)
  5854. {}
  5855. // make class move-only
  5856. json_sax_dom_parser(const json_sax_dom_parser&) = delete;
  5857. json_sax_dom_parser(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  5858. json_sax_dom_parser& operator=(const json_sax_dom_parser&) = delete;
  5859. json_sax_dom_parser& operator=(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  5860. ~json_sax_dom_parser() = default;
  5861. bool null()
  5862. {
  5863. handle_value(nullptr);
  5864. return true;
  5865. }
  5866. bool boolean(bool val)
  5867. {
  5868. handle_value(val);
  5869. return true;
  5870. }
  5871. bool number_integer(number_integer_t val)
  5872. {
  5873. handle_value(val);
  5874. return true;
  5875. }
  5876. bool number_unsigned(number_unsigned_t val)
  5877. {
  5878. handle_value(val);
  5879. return true;
  5880. }
  5881. bool number_float(number_float_t val, const string_t& /*unused*/)
  5882. {
  5883. handle_value(val);
  5884. return true;
  5885. }
  5886. bool string(string_t& val)
  5887. {
  5888. handle_value(val);
  5889. return true;
  5890. }
  5891. bool binary(binary_t& val)
  5892. {
  5893. handle_value(std::move(val));
  5894. return true;
  5895. }
  5896. bool start_object(std::size_t len)
  5897. {
  5898. ref_stack.push_back(handle_value(BasicJsonType::value_t::object));
  5899. if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
  5900. {
  5901. JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
  5902. }
  5903. return true;
  5904. }
  5905. bool key(string_t& val)
  5906. {
  5907. JSON_ASSERT(!ref_stack.empty());
  5908. JSON_ASSERT(ref_stack.back()->is_object());
  5909. // add null at given key and store the reference for later
  5910. object_element = &(ref_stack.back()->m_data.m_value.object->operator[](val));
  5911. return true;
  5912. }
  5913. bool end_object()
  5914. {
  5915. JSON_ASSERT(!ref_stack.empty());
  5916. JSON_ASSERT(ref_stack.back()->is_object());
  5917. ref_stack.back()->set_parents();
  5918. ref_stack.pop_back();
  5919. return true;
  5920. }
  5921. bool start_array(std::size_t len)
  5922. {
  5923. ref_stack.push_back(handle_value(BasicJsonType::value_t::array));
  5924. if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
  5925. {
  5926. JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
  5927. }
  5928. return true;
  5929. }
  5930. bool end_array()
  5931. {
  5932. JSON_ASSERT(!ref_stack.empty());
  5933. JSON_ASSERT(ref_stack.back()->is_array());
  5934. ref_stack.back()->set_parents();
  5935. ref_stack.pop_back();
  5936. return true;
  5937. }
  5938. template<class Exception>
  5939. bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
  5940. const Exception& ex)
  5941. {
  5942. errored = true;
  5943. static_cast<void>(ex);
  5944. if (allow_exceptions)
  5945. {
  5946. JSON_THROW(ex);
  5947. }
  5948. return false;
  5949. }
  5950. constexpr bool is_errored() const
  5951. {
  5952. return errored;
  5953. }
  5954. private:
  5955. /*!
  5956. @invariant If the ref stack is empty, then the passed value will be the new
  5957. root.
  5958. @invariant If the ref stack contains a value, then it is an array or an
  5959. object to which we can add elements
  5960. */
  5961. template<typename Value>
  5962. JSON_HEDLEY_RETURNS_NON_NULL
  5963. BasicJsonType* handle_value(Value&& v)
  5964. {
  5965. if (ref_stack.empty())
  5966. {
  5967. root = BasicJsonType(std::forward<Value>(v));
  5968. return &root;
  5969. }
  5970. JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
  5971. if (ref_stack.back()->is_array())
  5972. {
  5973. ref_stack.back()->m_data.m_value.array->emplace_back(std::forward<Value>(v));
  5974. return &(ref_stack.back()->m_data.m_value.array->back());
  5975. }
  5976. JSON_ASSERT(ref_stack.back()->is_object());
  5977. JSON_ASSERT(object_element);
  5978. *object_element = BasicJsonType(std::forward<Value>(v));
  5979. return object_element;
  5980. }
  5981. /// the parsed JSON value
  5982. BasicJsonType& root;
  5983. /// stack to model hierarchy of values
  5984. std::vector<BasicJsonType*> ref_stack {};
  5985. /// helper to hold the reference for the next object element
  5986. BasicJsonType* object_element = nullptr;
  5987. /// whether a syntax error occurred
  5988. bool errored = false;
  5989. /// whether to throw exceptions in case of errors
  5990. const bool allow_exceptions = true;
  5991. };
  5992. template<typename BasicJsonType>
  5993. class json_sax_dom_callback_parser
  5994. {
  5995. public:
  5996. using number_integer_t = typename BasicJsonType::number_integer_t;
  5997. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  5998. using number_float_t = typename BasicJsonType::number_float_t;
  5999. using string_t = typename BasicJsonType::string_t;
  6000. using binary_t = typename BasicJsonType::binary_t;
  6001. using parser_callback_t = typename BasicJsonType::parser_callback_t;
  6002. using parse_event_t = typename BasicJsonType::parse_event_t;
  6003. json_sax_dom_callback_parser(BasicJsonType& r,
  6004. const parser_callback_t cb,
  6005. const bool allow_exceptions_ = true)
  6006. : root(r), callback(cb), allow_exceptions(allow_exceptions_)
  6007. {
  6008. keep_stack.push_back(true);
  6009. }
  6010. // make class move-only
  6011. json_sax_dom_callback_parser(const json_sax_dom_callback_parser&) = delete;
  6012. json_sax_dom_callback_parser(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  6013. json_sax_dom_callback_parser& operator=(const json_sax_dom_callback_parser&) = delete;
  6014. json_sax_dom_callback_parser& operator=(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  6015. ~json_sax_dom_callback_parser() = default;
  6016. bool null()
  6017. {
  6018. handle_value(nullptr);
  6019. return true;
  6020. }
  6021. bool boolean(bool val)
  6022. {
  6023. handle_value(val);
  6024. return true;
  6025. }
  6026. bool number_integer(number_integer_t val)
  6027. {
  6028. handle_value(val);
  6029. return true;
  6030. }
  6031. bool number_unsigned(number_unsigned_t val)
  6032. {
  6033. handle_value(val);
  6034. return true;
  6035. }
  6036. bool number_float(number_float_t val, const string_t& /*unused*/)
  6037. {
  6038. handle_value(val);
  6039. return true;
  6040. }
  6041. bool string(string_t& val)
  6042. {
  6043. handle_value(val);
  6044. return true;
  6045. }
  6046. bool binary(binary_t& val)
  6047. {
  6048. handle_value(std::move(val));
  6049. return true;
  6050. }
  6051. bool start_object(std::size_t len)
  6052. {
  6053. // check callback for object start
  6054. const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::object_start, discarded);
  6055. keep_stack.push_back(keep);
  6056. auto val = handle_value(BasicJsonType::value_t::object, true);
  6057. ref_stack.push_back(val.second);
  6058. // check object limit
  6059. if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
  6060. {
  6061. JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
  6062. }
  6063. return true;
  6064. }
  6065. bool key(string_t& val)
  6066. {
  6067. BasicJsonType k = BasicJsonType(val);
  6068. // check callback for key
  6069. const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::key, k);
  6070. key_keep_stack.push_back(keep);
  6071. // add discarded value at given key and store the reference for later
  6072. if (keep && ref_stack.back())
  6073. {
  6074. object_element = &(ref_stack.back()->m_data.m_value.object->operator[](val) = discarded);
  6075. }
  6076. return true;
  6077. }
  6078. bool end_object()
  6079. {
  6080. if (ref_stack.back())
  6081. {
  6082. if (!callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back()))
  6083. {
  6084. // discard object
  6085. *ref_stack.back() = discarded;
  6086. }
  6087. else
  6088. {
  6089. ref_stack.back()->set_parents();
  6090. }
  6091. }
  6092. JSON_ASSERT(!ref_stack.empty());
  6093. JSON_ASSERT(!keep_stack.empty());
  6094. ref_stack.pop_back();
  6095. keep_stack.pop_back();
  6096. if (!ref_stack.empty() && ref_stack.back() && ref_stack.back()->is_structured())
  6097. {
  6098. // remove discarded value
  6099. for (auto it = ref_stack.back()->begin(); it != ref_stack.back()->end(); ++it)
  6100. {
  6101. if (it->is_discarded())
  6102. {
  6103. ref_stack.back()->erase(it);
  6104. break;
  6105. }
  6106. }
  6107. }
  6108. return true;
  6109. }
  6110. bool start_array(std::size_t len)
  6111. {
  6112. const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::array_start, discarded);
  6113. keep_stack.push_back(keep);
  6114. auto val = handle_value(BasicJsonType::value_t::array, true);
  6115. ref_stack.push_back(val.second);
  6116. // check array limit
  6117. if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
  6118. {
  6119. JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
  6120. }
  6121. return true;
  6122. }
  6123. bool end_array()
  6124. {
  6125. bool keep = true;
  6126. if (ref_stack.back())
  6127. {
  6128. keep = callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::array_end, *ref_stack.back());
  6129. if (keep)
  6130. {
  6131. ref_stack.back()->set_parents();
  6132. }
  6133. else
  6134. {
  6135. // discard array
  6136. *ref_stack.back() = discarded;
  6137. }
  6138. }
  6139. JSON_ASSERT(!ref_stack.empty());
  6140. JSON_ASSERT(!keep_stack.empty());
  6141. ref_stack.pop_back();
  6142. keep_stack.pop_back();
  6143. // remove discarded value
  6144. if (!keep && !ref_stack.empty() && ref_stack.back()->is_array())
  6145. {
  6146. ref_stack.back()->m_data.m_value.array->pop_back();
  6147. }
  6148. return true;
  6149. }
  6150. template<class Exception>
  6151. bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
  6152. const Exception& ex)
  6153. {
  6154. errored = true;
  6155. static_cast<void>(ex);
  6156. if (allow_exceptions)
  6157. {
  6158. JSON_THROW(ex);
  6159. }
  6160. return false;
  6161. }
  6162. constexpr bool is_errored() const
  6163. {
  6164. return errored;
  6165. }
  6166. private:
  6167. /*!
  6168. @param[in] v value to add to the JSON value we build during parsing
  6169. @param[in] skip_callback whether we should skip calling the callback
  6170. function; this is required after start_array() and
  6171. start_object() SAX events, because otherwise we would call the
  6172. callback function with an empty array or object, respectively.
  6173. @invariant If the ref stack is empty, then the passed value will be the new
  6174. root.
  6175. @invariant If the ref stack contains a value, then it is an array or an
  6176. object to which we can add elements
  6177. @return pair of boolean (whether value should be kept) and pointer (to the
  6178. passed value in the ref_stack hierarchy; nullptr if not kept)
  6179. */
  6180. template<typename Value>
  6181. std::pair<bool, BasicJsonType*> handle_value(Value&& v, const bool skip_callback = false)
  6182. {
  6183. JSON_ASSERT(!keep_stack.empty());
  6184. // do not handle this value if we know it would be added to a discarded
  6185. // container
  6186. if (!keep_stack.back())
  6187. {
  6188. return {false, nullptr};
  6189. }
  6190. // create value
  6191. auto value = BasicJsonType(std::forward<Value>(v));
  6192. // check callback
  6193. const bool keep = skip_callback || callback(static_cast<int>(ref_stack.size()), parse_event_t::value, value);
  6194. // do not handle this value if we just learnt it shall be discarded
  6195. if (!keep)
  6196. {
  6197. return {false, nullptr};
  6198. }
  6199. if (ref_stack.empty())
  6200. {
  6201. root = std::move(value);
  6202. return {true, & root};
  6203. }
  6204. // skip this value if we already decided to skip the parent
  6205. // (https://github.com/nlohmann/json/issues/971#issuecomment-413678360)
  6206. if (!ref_stack.back())
  6207. {
  6208. return {false, nullptr};
  6209. }
  6210. // we now only expect arrays and objects
  6211. JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
  6212. // array
  6213. if (ref_stack.back()->is_array())
  6214. {
  6215. ref_stack.back()->m_data.m_value.array->emplace_back(std::move(value));
  6216. return {true, & (ref_stack.back()->m_data.m_value.array->back())};
  6217. }
  6218. // object
  6219. JSON_ASSERT(ref_stack.back()->is_object());
  6220. // check if we should store an element for the current key
  6221. JSON_ASSERT(!key_keep_stack.empty());
  6222. const bool store_element = key_keep_stack.back();
  6223. key_keep_stack.pop_back();
  6224. if (!store_element)
  6225. {
  6226. return {false, nullptr};
  6227. }
  6228. JSON_ASSERT(object_element);
  6229. *object_element = std::move(value);
  6230. return {true, object_element};
  6231. }
  6232. /// the parsed JSON value
  6233. BasicJsonType& root;
  6234. /// stack to model hierarchy of values
  6235. std::vector<BasicJsonType*> ref_stack {};
  6236. /// stack to manage which values to keep
  6237. std::vector<bool> keep_stack {};
  6238. /// stack to manage which object keys to keep
  6239. std::vector<bool> key_keep_stack {};
  6240. /// helper to hold the reference for the next object element
  6241. BasicJsonType* object_element = nullptr;
  6242. /// whether a syntax error occurred
  6243. bool errored = false;
  6244. /// callback function
  6245. const parser_callback_t callback = nullptr;
  6246. /// whether to throw exceptions in case of errors
  6247. const bool allow_exceptions = true;
  6248. /// a discarded value for the callback
  6249. BasicJsonType discarded = BasicJsonType::value_t::discarded;
  6250. };
  6251. template<typename BasicJsonType>
  6252. class json_sax_acceptor
  6253. {
  6254. public:
  6255. using number_integer_t = typename BasicJsonType::number_integer_t;
  6256. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  6257. using number_float_t = typename BasicJsonType::number_float_t;
  6258. using string_t = typename BasicJsonType::string_t;
  6259. using binary_t = typename BasicJsonType::binary_t;
  6260. bool null()
  6261. {
  6262. return true;
  6263. }
  6264. bool boolean(bool /*unused*/)
  6265. {
  6266. return true;
  6267. }
  6268. bool number_integer(number_integer_t /*unused*/)
  6269. {
  6270. return true;
  6271. }
  6272. bool number_unsigned(number_unsigned_t /*unused*/)
  6273. {
  6274. return true;
  6275. }
  6276. bool number_float(number_float_t /*unused*/, const string_t& /*unused*/)
  6277. {
  6278. return true;
  6279. }
  6280. bool string(string_t& /*unused*/)
  6281. {
  6282. return true;
  6283. }
  6284. bool binary(binary_t& /*unused*/)
  6285. {
  6286. return true;
  6287. }
  6288. bool start_object(std::size_t /*unused*/ = static_cast<std::size_t>(-1))
  6289. {
  6290. return true;
  6291. }
  6292. bool key(string_t& /*unused*/)
  6293. {
  6294. return true;
  6295. }
  6296. bool end_object()
  6297. {
  6298. return true;
  6299. }
  6300. bool start_array(std::size_t /*unused*/ = static_cast<std::size_t>(-1))
  6301. {
  6302. return true;
  6303. }
  6304. bool end_array()
  6305. {
  6306. return true;
  6307. }
  6308. bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const detail::exception& /*unused*/)
  6309. {
  6310. return false;
  6311. }
  6312. };
  6313. } // namespace detail
  6314. NLOHMANN_JSON_NAMESPACE_END
  6315. // #include <nlohmann/detail/input/lexer.hpp>
  6316. // __ _____ _____ _____
  6317. // __| | __| | | | JSON for Modern C++
  6318. // | | |__ | | | | | | version 3.11.2
  6319. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  6320. //
  6321. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  6322. // SPDX-License-Identifier: MIT
  6323. #include <array> // array
  6324. #include <clocale> // localeconv
  6325. #include <cstddef> // size_t
  6326. #include <cstdio> // snprintf
  6327. #include <cstdlib> // strtof, strtod, strtold, strtoll, strtoull
  6328. #include <initializer_list> // initializer_list
  6329. #include <string> // char_traits, string
  6330. #include <utility> // move
  6331. #include <vector> // vector
  6332. // #include <nlohmann/detail/input/input_adapters.hpp>
  6333. // #include <nlohmann/detail/input/position_t.hpp>
  6334. // #include <nlohmann/detail/macro_scope.hpp>
  6335. NLOHMANN_JSON_NAMESPACE_BEGIN
  6336. namespace detail
  6337. {
  6338. ///////////
  6339. // lexer //
  6340. ///////////
  6341. template<typename BasicJsonType>
  6342. class lexer_base
  6343. {
  6344. public:
  6345. /// token types for the parser
  6346. enum class token_type
  6347. {
  6348. uninitialized, ///< indicating the scanner is uninitialized
  6349. literal_true, ///< the `true` literal
  6350. literal_false, ///< the `false` literal
  6351. literal_null, ///< the `null` literal
  6352. value_string, ///< a string -- use get_string() for actual value
  6353. value_unsigned, ///< an unsigned integer -- use get_number_unsigned() for actual value
  6354. value_integer, ///< a signed integer -- use get_number_integer() for actual value
  6355. value_float, ///< an floating point number -- use get_number_float() for actual value
  6356. begin_array, ///< the character for array begin `[`
  6357. begin_object, ///< the character for object begin `{`
  6358. end_array, ///< the character for array end `]`
  6359. end_object, ///< the character for object end `}`
  6360. name_separator, ///< the name separator `:`
  6361. value_separator, ///< the value separator `,`
  6362. parse_error, ///< indicating a parse error
  6363. end_of_input, ///< indicating the end of the input buffer
  6364. literal_or_value ///< a literal or the begin of a value (only for diagnostics)
  6365. };
  6366. /// return name of values of type token_type (only used for errors)
  6367. JSON_HEDLEY_RETURNS_NON_NULL
  6368. JSON_HEDLEY_CONST
  6369. static const char* token_type_name(const token_type t) noexcept
  6370. {
  6371. switch (t)
  6372. {
  6373. case token_type::uninitialized:
  6374. return "<uninitialized>";
  6375. case token_type::literal_true:
  6376. return "true literal";
  6377. case token_type::literal_false:
  6378. return "false literal";
  6379. case token_type::literal_null:
  6380. return "null literal";
  6381. case token_type::value_string:
  6382. return "string literal";
  6383. case token_type::value_unsigned:
  6384. case token_type::value_integer:
  6385. case token_type::value_float:
  6386. return "number literal";
  6387. case token_type::begin_array:
  6388. return "'['";
  6389. case token_type::begin_object:
  6390. return "'{'";
  6391. case token_type::end_array:
  6392. return "']'";
  6393. case token_type::end_object:
  6394. return "'}'";
  6395. case token_type::name_separator:
  6396. return "':'";
  6397. case token_type::value_separator:
  6398. return "','";
  6399. case token_type::parse_error:
  6400. return "<parse error>";
  6401. case token_type::end_of_input:
  6402. return "end of input";
  6403. case token_type::literal_or_value:
  6404. return "'[', '{', or a literal";
  6405. // LCOV_EXCL_START
  6406. default: // catch non-enum values
  6407. return "unknown token";
  6408. // LCOV_EXCL_STOP
  6409. }
  6410. }
  6411. };
  6412. /*!
  6413. @brief lexical analysis
  6414. This class organizes the lexical analysis during JSON deserialization.
  6415. */
  6416. template<typename BasicJsonType, typename InputAdapterType>
  6417. class lexer : public lexer_base<BasicJsonType>
  6418. {
  6419. using number_integer_t = typename BasicJsonType::number_integer_t;
  6420. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  6421. using number_float_t = typename BasicJsonType::number_float_t;
  6422. using string_t = typename BasicJsonType::string_t;
  6423. using char_type = typename InputAdapterType::char_type;
  6424. using char_int_type = typename std::char_traits<char_type>::int_type;
  6425. public:
  6426. using token_type = typename lexer_base<BasicJsonType>::token_type;
  6427. explicit lexer(InputAdapterType&& adapter, bool ignore_comments_ = false) noexcept
  6428. : ia(std::move(adapter))
  6429. , ignore_comments(ignore_comments_)
  6430. , decimal_point_char(static_cast<char_int_type>(get_decimal_point()))
  6431. {}
  6432. // delete because of pointer members
  6433. lexer(const lexer&) = delete;
  6434. lexer(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  6435. lexer& operator=(lexer&) = delete;
  6436. lexer& operator=(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  6437. ~lexer() = default;
  6438. private:
  6439. /////////////////////
  6440. // locales
  6441. /////////////////////
  6442. /// return the locale-dependent decimal point
  6443. JSON_HEDLEY_PURE
  6444. static char get_decimal_point() noexcept
  6445. {
  6446. const auto* loc = localeconv();
  6447. JSON_ASSERT(loc != nullptr);
  6448. return (loc->decimal_point == nullptr) ? '.' : *(loc->decimal_point);
  6449. }
  6450. /////////////////////
  6451. // scan functions
  6452. /////////////////////
  6453. /*!
  6454. @brief get codepoint from 4 hex characters following `\u`
  6455. For input "\u c1 c2 c3 c4" the codepoint is:
  6456. (c1 * 0x1000) + (c2 * 0x0100) + (c3 * 0x0010) + c4
  6457. = (c1 << 12) + (c2 << 8) + (c3 << 4) + (c4 << 0)
  6458. Furthermore, the possible characters '0'..'9', 'A'..'F', and 'a'..'f'
  6459. must be converted to the integers 0x0..0x9, 0xA..0xF, 0xA..0xF, resp. The
  6460. conversion is done by subtracting the offset (0x30, 0x37, and 0x57)
  6461. between the ASCII value of the character and the desired integer value.
  6462. @return codepoint (0x0000..0xFFFF) or -1 in case of an error (e.g. EOF or
  6463. non-hex character)
  6464. */
  6465. int get_codepoint()
  6466. {
  6467. // this function only makes sense after reading `\u`
  6468. JSON_ASSERT(current == 'u');
  6469. int codepoint = 0;
  6470. const auto factors = { 12u, 8u, 4u, 0u };
  6471. for (const auto factor : factors)
  6472. {
  6473. get();
  6474. if (current >= '0' && current <= '9')
  6475. {
  6476. codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x30u) << factor);
  6477. }
  6478. else if (current >= 'A' && current <= 'F')
  6479. {
  6480. codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x37u) << factor);
  6481. }
  6482. else if (current >= 'a' && current <= 'f')
  6483. {
  6484. codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x57u) << factor);
  6485. }
  6486. else
  6487. {
  6488. return -1;
  6489. }
  6490. }
  6491. JSON_ASSERT(0x0000 <= codepoint && codepoint <= 0xFFFF);
  6492. return codepoint;
  6493. }
  6494. /*!
  6495. @brief check if the next byte(s) are inside a given range
  6496. Adds the current byte and, for each passed range, reads a new byte and
  6497. checks if it is inside the range. If a violation was detected, set up an
  6498. error message and return false. Otherwise, return true.
  6499. @param[in] ranges list of integers; interpreted as list of pairs of
  6500. inclusive lower and upper bound, respectively
  6501. @pre The passed list @a ranges must have 2, 4, or 6 elements; that is,
  6502. 1, 2, or 3 pairs. This precondition is enforced by an assertion.
  6503. @return true if and only if no range violation was detected
  6504. */
  6505. bool next_byte_in_range(std::initializer_list<char_int_type> ranges)
  6506. {
  6507. JSON_ASSERT(ranges.size() == 2 || ranges.size() == 4 || ranges.size() == 6);
  6508. add(current);
  6509. for (auto range = ranges.begin(); range != ranges.end(); ++range)
  6510. {
  6511. get();
  6512. if (JSON_HEDLEY_LIKELY(*range <= current && current <= *(++range))) // NOLINT(bugprone-inc-dec-in-conditions)
  6513. {
  6514. add(current);
  6515. }
  6516. else
  6517. {
  6518. error_message = "invalid string: ill-formed UTF-8 byte";
  6519. return false;
  6520. }
  6521. }
  6522. return true;
  6523. }
  6524. /*!
  6525. @brief scan a string literal
  6526. This function scans a string according to Sect. 7 of RFC 8259. While
  6527. scanning, bytes are escaped and copied into buffer token_buffer. Then the
  6528. function returns successfully, token_buffer is *not* null-terminated (as it
  6529. may contain \0 bytes), and token_buffer.size() is the number of bytes in the
  6530. string.
  6531. @return token_type::value_string if string could be successfully scanned,
  6532. token_type::parse_error otherwise
  6533. @note In case of errors, variable error_message contains a textual
  6534. description.
  6535. */
  6536. token_type scan_string()
  6537. {
  6538. // reset token_buffer (ignore opening quote)
  6539. reset();
  6540. // we entered the function by reading an open quote
  6541. JSON_ASSERT(current == '\"');
  6542. while (true)
  6543. {
  6544. // get next character
  6545. switch (get())
  6546. {
  6547. // end of file while parsing string
  6548. case std::char_traits<char_type>::eof():
  6549. {
  6550. error_message = "invalid string: missing closing quote";
  6551. return token_type::parse_error;
  6552. }
  6553. // closing quote
  6554. case '\"':
  6555. {
  6556. return token_type::value_string;
  6557. }
  6558. // escapes
  6559. case '\\':
  6560. {
  6561. switch (get())
  6562. {
  6563. // quotation mark
  6564. case '\"':
  6565. add('\"');
  6566. break;
  6567. // reverse solidus
  6568. case '\\':
  6569. add('\\');
  6570. break;
  6571. // solidus
  6572. case '/':
  6573. add('/');
  6574. break;
  6575. // backspace
  6576. case 'b':
  6577. add('\b');
  6578. break;
  6579. // form feed
  6580. case 'f':
  6581. add('\f');
  6582. break;
  6583. // line feed
  6584. case 'n':
  6585. add('\n');
  6586. break;
  6587. // carriage return
  6588. case 'r':
  6589. add('\r');
  6590. break;
  6591. // tab
  6592. case 't':
  6593. add('\t');
  6594. break;
  6595. // unicode escapes
  6596. case 'u':
  6597. {
  6598. const int codepoint1 = get_codepoint();
  6599. int codepoint = codepoint1; // start with codepoint1
  6600. if (JSON_HEDLEY_UNLIKELY(codepoint1 == -1))
  6601. {
  6602. error_message = "invalid string: '\\u' must be followed by 4 hex digits";
  6603. return token_type::parse_error;
  6604. }
  6605. // check if code point is a high surrogate
  6606. if (0xD800 <= codepoint1 && codepoint1 <= 0xDBFF)
  6607. {
  6608. // expect next \uxxxx entry
  6609. if (JSON_HEDLEY_LIKELY(get() == '\\' && get() == 'u'))
  6610. {
  6611. const int codepoint2 = get_codepoint();
  6612. if (JSON_HEDLEY_UNLIKELY(codepoint2 == -1))
  6613. {
  6614. error_message = "invalid string: '\\u' must be followed by 4 hex digits";
  6615. return token_type::parse_error;
  6616. }
  6617. // check if codepoint2 is a low surrogate
  6618. if (JSON_HEDLEY_LIKELY(0xDC00 <= codepoint2 && codepoint2 <= 0xDFFF))
  6619. {
  6620. // overwrite codepoint
  6621. codepoint = static_cast<int>(
  6622. // high surrogate occupies the most significant 22 bits
  6623. (static_cast<unsigned int>(codepoint1) << 10u)
  6624. // low surrogate occupies the least significant 15 bits
  6625. + static_cast<unsigned int>(codepoint2)
  6626. // there is still the 0xD800, 0xDC00 and 0x10000 noise
  6627. // in the result, so we have to subtract with:
  6628. // (0xD800 << 10) + DC00 - 0x10000 = 0x35FDC00
  6629. - 0x35FDC00u);
  6630. }
  6631. else
  6632. {
  6633. error_message = "invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF";
  6634. return token_type::parse_error;
  6635. }
  6636. }
  6637. else
  6638. {
  6639. error_message = "invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF";
  6640. return token_type::parse_error;
  6641. }
  6642. }
  6643. else
  6644. {
  6645. if (JSON_HEDLEY_UNLIKELY(0xDC00 <= codepoint1 && codepoint1 <= 0xDFFF))
  6646. {
  6647. error_message = "invalid string: surrogate U+DC00..U+DFFF must follow U+D800..U+DBFF";
  6648. return token_type::parse_error;
  6649. }
  6650. }
  6651. // result of the above calculation yields a proper codepoint
  6652. JSON_ASSERT(0x00 <= codepoint && codepoint <= 0x10FFFF);
  6653. // translate codepoint into bytes
  6654. if (codepoint < 0x80)
  6655. {
  6656. // 1-byte characters: 0xxxxxxx (ASCII)
  6657. add(static_cast<char_int_type>(codepoint));
  6658. }
  6659. else if (codepoint <= 0x7FF)
  6660. {
  6661. // 2-byte characters: 110xxxxx 10xxxxxx
  6662. add(static_cast<char_int_type>(0xC0u | (static_cast<unsigned int>(codepoint) >> 6u)));
  6663. add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
  6664. }
  6665. else if (codepoint <= 0xFFFF)
  6666. {
  6667. // 3-byte characters: 1110xxxx 10xxxxxx 10xxxxxx
  6668. add(static_cast<char_int_type>(0xE0u | (static_cast<unsigned int>(codepoint) >> 12u)));
  6669. add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 6u) & 0x3Fu)));
  6670. add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
  6671. }
  6672. else
  6673. {
  6674. // 4-byte characters: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  6675. add(static_cast<char_int_type>(0xF0u | (static_cast<unsigned int>(codepoint) >> 18u)));
  6676. add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 12u) & 0x3Fu)));
  6677. add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 6u) & 0x3Fu)));
  6678. add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
  6679. }
  6680. break;
  6681. }
  6682. // other characters after escape
  6683. default:
  6684. error_message = "invalid string: forbidden character after backslash";
  6685. return token_type::parse_error;
  6686. }
  6687. break;
  6688. }
  6689. // invalid control characters
  6690. case 0x00:
  6691. {
  6692. error_message = "invalid string: control character U+0000 (NUL) must be escaped to \\u0000";
  6693. return token_type::parse_error;
  6694. }
  6695. case 0x01:
  6696. {
  6697. error_message = "invalid string: control character U+0001 (SOH) must be escaped to \\u0001";
  6698. return token_type::parse_error;
  6699. }
  6700. case 0x02:
  6701. {
  6702. error_message = "invalid string: control character U+0002 (STX) must be escaped to \\u0002";
  6703. return token_type::parse_error;
  6704. }
  6705. case 0x03:
  6706. {
  6707. error_message = "invalid string: control character U+0003 (ETX) must be escaped to \\u0003";
  6708. return token_type::parse_error;
  6709. }
  6710. case 0x04:
  6711. {
  6712. error_message = "invalid string: control character U+0004 (EOT) must be escaped to \\u0004";
  6713. return token_type::parse_error;
  6714. }
  6715. case 0x05:
  6716. {
  6717. error_message = "invalid string: control character U+0005 (ENQ) must be escaped to \\u0005";
  6718. return token_type::parse_error;
  6719. }
  6720. case 0x06:
  6721. {
  6722. error_message = "invalid string: control character U+0006 (ACK) must be escaped to \\u0006";
  6723. return token_type::parse_error;
  6724. }
  6725. case 0x07:
  6726. {
  6727. error_message = "invalid string: control character U+0007 (BEL) must be escaped to \\u0007";
  6728. return token_type::parse_error;
  6729. }
  6730. case 0x08:
  6731. {
  6732. error_message = "invalid string: control character U+0008 (BS) must be escaped to \\u0008 or \\b";
  6733. return token_type::parse_error;
  6734. }
  6735. case 0x09:
  6736. {
  6737. error_message = "invalid string: control character U+0009 (HT) must be escaped to \\u0009 or \\t";
  6738. return token_type::parse_error;
  6739. }
  6740. case 0x0A:
  6741. {
  6742. error_message = "invalid string: control character U+000A (LF) must be escaped to \\u000A or \\n";
  6743. return token_type::parse_error;
  6744. }
  6745. case 0x0B:
  6746. {
  6747. error_message = "invalid string: control character U+000B (VT) must be escaped to \\u000B";
  6748. return token_type::parse_error;
  6749. }
  6750. case 0x0C:
  6751. {
  6752. error_message = "invalid string: control character U+000C (FF) must be escaped to \\u000C or \\f";
  6753. return token_type::parse_error;
  6754. }
  6755. case 0x0D:
  6756. {
  6757. error_message = "invalid string: control character U+000D (CR) must be escaped to \\u000D or \\r";
  6758. return token_type::parse_error;
  6759. }
  6760. case 0x0E:
  6761. {
  6762. error_message = "invalid string: control character U+000E (SO) must be escaped to \\u000E";
  6763. return token_type::parse_error;
  6764. }
  6765. case 0x0F:
  6766. {
  6767. error_message = "invalid string: control character U+000F (SI) must be escaped to \\u000F";
  6768. return token_type::parse_error;
  6769. }
  6770. case 0x10:
  6771. {
  6772. error_message = "invalid string: control character U+0010 (DLE) must be escaped to \\u0010";
  6773. return token_type::parse_error;
  6774. }
  6775. case 0x11:
  6776. {
  6777. error_message = "invalid string: control character U+0011 (DC1) must be escaped to \\u0011";
  6778. return token_type::parse_error;
  6779. }
  6780. case 0x12:
  6781. {
  6782. error_message = "invalid string: control character U+0012 (DC2) must be escaped to \\u0012";
  6783. return token_type::parse_error;
  6784. }
  6785. case 0x13:
  6786. {
  6787. error_message = "invalid string: control character U+0013 (DC3) must be escaped to \\u0013";
  6788. return token_type::parse_error;
  6789. }
  6790. case 0x14:
  6791. {
  6792. error_message = "invalid string: control character U+0014 (DC4) must be escaped to \\u0014";
  6793. return token_type::parse_error;
  6794. }
  6795. case 0x15:
  6796. {
  6797. error_message = "invalid string: control character U+0015 (NAK) must be escaped to \\u0015";
  6798. return token_type::parse_error;
  6799. }
  6800. case 0x16:
  6801. {
  6802. error_message = "invalid string: control character U+0016 (SYN) must be escaped to \\u0016";
  6803. return token_type::parse_error;
  6804. }
  6805. case 0x17:
  6806. {
  6807. error_message = "invalid string: control character U+0017 (ETB) must be escaped to \\u0017";
  6808. return token_type::parse_error;
  6809. }
  6810. case 0x18:
  6811. {
  6812. error_message = "invalid string: control character U+0018 (CAN) must be escaped to \\u0018";
  6813. return token_type::parse_error;
  6814. }
  6815. case 0x19:
  6816. {
  6817. error_message = "invalid string: control character U+0019 (EM) must be escaped to \\u0019";
  6818. return token_type::parse_error;
  6819. }
  6820. case 0x1A:
  6821. {
  6822. error_message = "invalid string: control character U+001A (SUB) must be escaped to \\u001A";
  6823. return token_type::parse_error;
  6824. }
  6825. case 0x1B:
  6826. {
  6827. error_message = "invalid string: control character U+001B (ESC) must be escaped to \\u001B";
  6828. return token_type::parse_error;
  6829. }
  6830. case 0x1C:
  6831. {
  6832. error_message = "invalid string: control character U+001C (FS) must be escaped to \\u001C";
  6833. return token_type::parse_error;
  6834. }
  6835. case 0x1D:
  6836. {
  6837. error_message = "invalid string: control character U+001D (GS) must be escaped to \\u001D";
  6838. return token_type::parse_error;
  6839. }
  6840. case 0x1E:
  6841. {
  6842. error_message = "invalid string: control character U+001E (RS) must be escaped to \\u001E";
  6843. return token_type::parse_error;
  6844. }
  6845. case 0x1F:
  6846. {
  6847. error_message = "invalid string: control character U+001F (US) must be escaped to \\u001F";
  6848. return token_type::parse_error;
  6849. }
  6850. // U+0020..U+007F (except U+0022 (quote) and U+005C (backspace))
  6851. case 0x20:
  6852. case 0x21:
  6853. case 0x23:
  6854. case 0x24:
  6855. case 0x25:
  6856. case 0x26:
  6857. case 0x27:
  6858. case 0x28:
  6859. case 0x29:
  6860. case 0x2A:
  6861. case 0x2B:
  6862. case 0x2C:
  6863. case 0x2D:
  6864. case 0x2E:
  6865. case 0x2F:
  6866. case 0x30:
  6867. case 0x31:
  6868. case 0x32:
  6869. case 0x33:
  6870. case 0x34:
  6871. case 0x35:
  6872. case 0x36:
  6873. case 0x37:
  6874. case 0x38:
  6875. case 0x39:
  6876. case 0x3A:
  6877. case 0x3B:
  6878. case 0x3C:
  6879. case 0x3D:
  6880. case 0x3E:
  6881. case 0x3F:
  6882. case 0x40:
  6883. case 0x41:
  6884. case 0x42:
  6885. case 0x43:
  6886. case 0x44:
  6887. case 0x45:
  6888. case 0x46:
  6889. case 0x47:
  6890. case 0x48:
  6891. case 0x49:
  6892. case 0x4A:
  6893. case 0x4B:
  6894. case 0x4C:
  6895. case 0x4D:
  6896. case 0x4E:
  6897. case 0x4F:
  6898. case 0x50:
  6899. case 0x51:
  6900. case 0x52:
  6901. case 0x53:
  6902. case 0x54:
  6903. case 0x55:
  6904. case 0x56:
  6905. case 0x57:
  6906. case 0x58:
  6907. case 0x59:
  6908. case 0x5A:
  6909. case 0x5B:
  6910. case 0x5D:
  6911. case 0x5E:
  6912. case 0x5F:
  6913. case 0x60:
  6914. case 0x61:
  6915. case 0x62:
  6916. case 0x63:
  6917. case 0x64:
  6918. case 0x65:
  6919. case 0x66:
  6920. case 0x67:
  6921. case 0x68:
  6922. case 0x69:
  6923. case 0x6A:
  6924. case 0x6B:
  6925. case 0x6C:
  6926. case 0x6D:
  6927. case 0x6E:
  6928. case 0x6F:
  6929. case 0x70:
  6930. case 0x71:
  6931. case 0x72:
  6932. case 0x73:
  6933. case 0x74:
  6934. case 0x75:
  6935. case 0x76:
  6936. case 0x77:
  6937. case 0x78:
  6938. case 0x79:
  6939. case 0x7A:
  6940. case 0x7B:
  6941. case 0x7C:
  6942. case 0x7D:
  6943. case 0x7E:
  6944. case 0x7F:
  6945. {
  6946. add(current);
  6947. break;
  6948. }
  6949. // U+0080..U+07FF: bytes C2..DF 80..BF
  6950. case 0xC2:
  6951. case 0xC3:
  6952. case 0xC4:
  6953. case 0xC5:
  6954. case 0xC6:
  6955. case 0xC7:
  6956. case 0xC8:
  6957. case 0xC9:
  6958. case 0xCA:
  6959. case 0xCB:
  6960. case 0xCC:
  6961. case 0xCD:
  6962. case 0xCE:
  6963. case 0xCF:
  6964. case 0xD0:
  6965. case 0xD1:
  6966. case 0xD2:
  6967. case 0xD3:
  6968. case 0xD4:
  6969. case 0xD5:
  6970. case 0xD6:
  6971. case 0xD7:
  6972. case 0xD8:
  6973. case 0xD9:
  6974. case 0xDA:
  6975. case 0xDB:
  6976. case 0xDC:
  6977. case 0xDD:
  6978. case 0xDE:
  6979. case 0xDF:
  6980. {
  6981. if (JSON_HEDLEY_UNLIKELY(!next_byte_in_range({0x80, 0xBF})))
  6982. {
  6983. return token_type::parse_error;
  6984. }
  6985. break;
  6986. }
  6987. // U+0800..U+0FFF: bytes E0 A0..BF 80..BF
  6988. case 0xE0:
  6989. {
  6990. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0xA0, 0xBF, 0x80, 0xBF}))))
  6991. {
  6992. return token_type::parse_error;
  6993. }
  6994. break;
  6995. }
  6996. // U+1000..U+CFFF: bytes E1..EC 80..BF 80..BF
  6997. // U+E000..U+FFFF: bytes EE..EF 80..BF 80..BF
  6998. case 0xE1:
  6999. case 0xE2:
  7000. case 0xE3:
  7001. case 0xE4:
  7002. case 0xE5:
  7003. case 0xE6:
  7004. case 0xE7:
  7005. case 0xE8:
  7006. case 0xE9:
  7007. case 0xEA:
  7008. case 0xEB:
  7009. case 0xEC:
  7010. case 0xEE:
  7011. case 0xEF:
  7012. {
  7013. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0xBF, 0x80, 0xBF}))))
  7014. {
  7015. return token_type::parse_error;
  7016. }
  7017. break;
  7018. }
  7019. // U+D000..U+D7FF: bytes ED 80..9F 80..BF
  7020. case 0xED:
  7021. {
  7022. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0x9F, 0x80, 0xBF}))))
  7023. {
  7024. return token_type::parse_error;
  7025. }
  7026. break;
  7027. }
  7028. // U+10000..U+3FFFF F0 90..BF 80..BF 80..BF
  7029. case 0xF0:
  7030. {
  7031. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x90, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
  7032. {
  7033. return token_type::parse_error;
  7034. }
  7035. break;
  7036. }
  7037. // U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF
  7038. case 0xF1:
  7039. case 0xF2:
  7040. case 0xF3:
  7041. {
  7042. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
  7043. {
  7044. return token_type::parse_error;
  7045. }
  7046. break;
  7047. }
  7048. // U+100000..U+10FFFF F4 80..8F 80..BF 80..BF
  7049. case 0xF4:
  7050. {
  7051. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0x8F, 0x80, 0xBF, 0x80, 0xBF}))))
  7052. {
  7053. return token_type::parse_error;
  7054. }
  7055. break;
  7056. }
  7057. // remaining bytes (80..C1 and F5..FF) are ill-formed
  7058. default:
  7059. {
  7060. error_message = "invalid string: ill-formed UTF-8 byte";
  7061. return token_type::parse_error;
  7062. }
  7063. }
  7064. }
  7065. }
  7066. /*!
  7067. * @brief scan a comment
  7068. * @return whether comment could be scanned successfully
  7069. */
  7070. bool scan_comment()
  7071. {
  7072. switch (get())
  7073. {
  7074. // single-line comments skip input until a newline or EOF is read
  7075. case '/':
  7076. {
  7077. while (true)
  7078. {
  7079. switch (get())
  7080. {
  7081. case '\n':
  7082. case '\r':
  7083. case std::char_traits<char_type>::eof():
  7084. case '\0':
  7085. return true;
  7086. default:
  7087. break;
  7088. }
  7089. }
  7090. }
  7091. // multi-line comments skip input until */ is read
  7092. case '*':
  7093. {
  7094. while (true)
  7095. {
  7096. switch (get())
  7097. {
  7098. case std::char_traits<char_type>::eof():
  7099. case '\0':
  7100. {
  7101. error_message = "invalid comment; missing closing '*/'";
  7102. return false;
  7103. }
  7104. case '*':
  7105. {
  7106. switch (get())
  7107. {
  7108. case '/':
  7109. return true;
  7110. default:
  7111. {
  7112. unget();
  7113. continue;
  7114. }
  7115. }
  7116. }
  7117. default:
  7118. continue;
  7119. }
  7120. }
  7121. }
  7122. // unexpected character after reading '/'
  7123. default:
  7124. {
  7125. error_message = "invalid comment; expecting '/' or '*' after '/'";
  7126. return false;
  7127. }
  7128. }
  7129. }
  7130. JSON_HEDLEY_NON_NULL(2)
  7131. static void strtof(float& f, const char* str, char** endptr) noexcept
  7132. {
  7133. f = std::strtof(str, endptr);
  7134. }
  7135. JSON_HEDLEY_NON_NULL(2)
  7136. static void strtof(double& f, const char* str, char** endptr) noexcept
  7137. {
  7138. f = std::strtod(str, endptr);
  7139. }
  7140. JSON_HEDLEY_NON_NULL(2)
  7141. static void strtof(long double& f, const char* str, char** endptr) noexcept
  7142. {
  7143. f = std::strtold(str, endptr);
  7144. }
  7145. /*!
  7146. @brief scan a number literal
  7147. This function scans a string according to Sect. 6 of RFC 8259.
  7148. The function is realized with a deterministic finite state machine derived
  7149. from the grammar described in RFC 8259. Starting in state "init", the
  7150. input is read and used to determined the next state. Only state "done"
  7151. accepts the number. State "error" is a trap state to model errors. In the
  7152. table below, "anything" means any character but the ones listed before.
  7153. state | 0 | 1-9 | e E | + | - | . | anything
  7154. ---------|----------|----------|----------|---------|---------|----------|-----------
  7155. init | zero | any1 | [error] | [error] | minus | [error] | [error]
  7156. minus | zero | any1 | [error] | [error] | [error] | [error] | [error]
  7157. zero | done | done | exponent | done | done | decimal1 | done
  7158. any1 | any1 | any1 | exponent | done | done | decimal1 | done
  7159. decimal1 | decimal2 | decimal2 | [error] | [error] | [error] | [error] | [error]
  7160. decimal2 | decimal2 | decimal2 | exponent | done | done | done | done
  7161. exponent | any2 | any2 | [error] | sign | sign | [error] | [error]
  7162. sign | any2 | any2 | [error] | [error] | [error] | [error] | [error]
  7163. any2 | any2 | any2 | done | done | done | done | done
  7164. The state machine is realized with one label per state (prefixed with
  7165. "scan_number_") and `goto` statements between them. The state machine
  7166. contains cycles, but any cycle can be left when EOF is read. Therefore,
  7167. the function is guaranteed to terminate.
  7168. During scanning, the read bytes are stored in token_buffer. This string is
  7169. then converted to a signed integer, an unsigned integer, or a
  7170. floating-point number.
  7171. @return token_type::value_unsigned, token_type::value_integer, or
  7172. token_type::value_float if number could be successfully scanned,
  7173. token_type::parse_error otherwise
  7174. @note The scanner is independent of the current locale. Internally, the
  7175. locale's decimal point is used instead of `.` to work with the
  7176. locale-dependent converters.
  7177. */
  7178. token_type scan_number() // lgtm [cpp/use-of-goto]
  7179. {
  7180. // reset token_buffer to store the number's bytes
  7181. reset();
  7182. // the type of the parsed number; initially set to unsigned; will be
  7183. // changed if minus sign, decimal point or exponent is read
  7184. token_type number_type = token_type::value_unsigned;
  7185. // state (init): we just found out we need to scan a number
  7186. switch (current)
  7187. {
  7188. case '-':
  7189. {
  7190. add(current);
  7191. goto scan_number_minus;
  7192. }
  7193. case '0':
  7194. {
  7195. add(current);
  7196. goto scan_number_zero;
  7197. }
  7198. case '1':
  7199. case '2':
  7200. case '3':
  7201. case '4':
  7202. case '5':
  7203. case '6':
  7204. case '7':
  7205. case '8':
  7206. case '9':
  7207. {
  7208. add(current);
  7209. goto scan_number_any1;
  7210. }
  7211. // all other characters are rejected outside scan_number()
  7212. default: // LCOV_EXCL_LINE
  7213. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  7214. }
  7215. scan_number_minus:
  7216. // state: we just parsed a leading minus sign
  7217. number_type = token_type::value_integer;
  7218. switch (get())
  7219. {
  7220. case '0':
  7221. {
  7222. add(current);
  7223. goto scan_number_zero;
  7224. }
  7225. case '1':
  7226. case '2':
  7227. case '3':
  7228. case '4':
  7229. case '5':
  7230. case '6':
  7231. case '7':
  7232. case '8':
  7233. case '9':
  7234. {
  7235. add(current);
  7236. goto scan_number_any1;
  7237. }
  7238. default:
  7239. {
  7240. error_message = "invalid number; expected digit after '-'";
  7241. return token_type::parse_error;
  7242. }
  7243. }
  7244. scan_number_zero:
  7245. // state: we just parse a zero (maybe with a leading minus sign)
  7246. switch (get())
  7247. {
  7248. case '.':
  7249. {
  7250. add(decimal_point_char);
  7251. goto scan_number_decimal1;
  7252. }
  7253. case 'e':
  7254. case 'E':
  7255. {
  7256. add(current);
  7257. goto scan_number_exponent;
  7258. }
  7259. default:
  7260. goto scan_number_done;
  7261. }
  7262. scan_number_any1:
  7263. // state: we just parsed a number 0-9 (maybe with a leading minus sign)
  7264. switch (get())
  7265. {
  7266. case '0':
  7267. case '1':
  7268. case '2':
  7269. case '3':
  7270. case '4':
  7271. case '5':
  7272. case '6':
  7273. case '7':
  7274. case '8':
  7275. case '9':
  7276. {
  7277. add(current);
  7278. goto scan_number_any1;
  7279. }
  7280. case '.':
  7281. {
  7282. add(decimal_point_char);
  7283. goto scan_number_decimal1;
  7284. }
  7285. case 'e':
  7286. case 'E':
  7287. {
  7288. add(current);
  7289. goto scan_number_exponent;
  7290. }
  7291. default:
  7292. goto scan_number_done;
  7293. }
  7294. scan_number_decimal1:
  7295. // state: we just parsed a decimal point
  7296. number_type = token_type::value_float;
  7297. switch (get())
  7298. {
  7299. case '0':
  7300. case '1':
  7301. case '2':
  7302. case '3':
  7303. case '4':
  7304. case '5':
  7305. case '6':
  7306. case '7':
  7307. case '8':
  7308. case '9':
  7309. {
  7310. add(current);
  7311. goto scan_number_decimal2;
  7312. }
  7313. default:
  7314. {
  7315. error_message = "invalid number; expected digit after '.'";
  7316. return token_type::parse_error;
  7317. }
  7318. }
  7319. scan_number_decimal2:
  7320. // we just parsed at least one number after a decimal point
  7321. switch (get())
  7322. {
  7323. case '0':
  7324. case '1':
  7325. case '2':
  7326. case '3':
  7327. case '4':
  7328. case '5':
  7329. case '6':
  7330. case '7':
  7331. case '8':
  7332. case '9':
  7333. {
  7334. add(current);
  7335. goto scan_number_decimal2;
  7336. }
  7337. case 'e':
  7338. case 'E':
  7339. {
  7340. add(current);
  7341. goto scan_number_exponent;
  7342. }
  7343. default:
  7344. goto scan_number_done;
  7345. }
  7346. scan_number_exponent:
  7347. // we just parsed an exponent
  7348. number_type = token_type::value_float;
  7349. switch (get())
  7350. {
  7351. case '+':
  7352. case '-':
  7353. {
  7354. add(current);
  7355. goto scan_number_sign;
  7356. }
  7357. case '0':
  7358. case '1':
  7359. case '2':
  7360. case '3':
  7361. case '4':
  7362. case '5':
  7363. case '6':
  7364. case '7':
  7365. case '8':
  7366. case '9':
  7367. {
  7368. add(current);
  7369. goto scan_number_any2;
  7370. }
  7371. default:
  7372. {
  7373. error_message =
  7374. "invalid number; expected '+', '-', or digit after exponent";
  7375. return token_type::parse_error;
  7376. }
  7377. }
  7378. scan_number_sign:
  7379. // we just parsed an exponent sign
  7380. switch (get())
  7381. {
  7382. case '0':
  7383. case '1':
  7384. case '2':
  7385. case '3':
  7386. case '4':
  7387. case '5':
  7388. case '6':
  7389. case '7':
  7390. case '8':
  7391. case '9':
  7392. {
  7393. add(current);
  7394. goto scan_number_any2;
  7395. }
  7396. default:
  7397. {
  7398. error_message = "invalid number; expected digit after exponent sign";
  7399. return token_type::parse_error;
  7400. }
  7401. }
  7402. scan_number_any2:
  7403. // we just parsed a number after the exponent or exponent sign
  7404. switch (get())
  7405. {
  7406. case '0':
  7407. case '1':
  7408. case '2':
  7409. case '3':
  7410. case '4':
  7411. case '5':
  7412. case '6':
  7413. case '7':
  7414. case '8':
  7415. case '9':
  7416. {
  7417. add(current);
  7418. goto scan_number_any2;
  7419. }
  7420. default:
  7421. goto scan_number_done;
  7422. }
  7423. scan_number_done:
  7424. // unget the character after the number (we only read it to know that
  7425. // we are done scanning a number)
  7426. unget();
  7427. char* endptr = nullptr; // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  7428. errno = 0;
  7429. // try to parse integers first and fall back to floats
  7430. if (number_type == token_type::value_unsigned)
  7431. {
  7432. const auto x = std::strtoull(token_buffer.data(), &endptr, 10);
  7433. // we checked the number format before
  7434. JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
  7435. if (errno == 0)
  7436. {
  7437. value_unsigned = static_cast<number_unsigned_t>(x);
  7438. if (value_unsigned == x)
  7439. {
  7440. return token_type::value_unsigned;
  7441. }
  7442. }
  7443. }
  7444. else if (number_type == token_type::value_integer)
  7445. {
  7446. const auto x = std::strtoll(token_buffer.data(), &endptr, 10);
  7447. // we checked the number format before
  7448. JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
  7449. if (errno == 0)
  7450. {
  7451. value_integer = static_cast<number_integer_t>(x);
  7452. if (value_integer == x)
  7453. {
  7454. return token_type::value_integer;
  7455. }
  7456. }
  7457. }
  7458. // this code is reached if we parse a floating-point number or if an
  7459. // integer conversion above failed
  7460. strtof(value_float, token_buffer.data(), &endptr);
  7461. // we checked the number format before
  7462. JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
  7463. return token_type::value_float;
  7464. }
  7465. /*!
  7466. @param[in] literal_text the literal text to expect
  7467. @param[in] length the length of the passed literal text
  7468. @param[in] return_type the token type to return on success
  7469. */
  7470. JSON_HEDLEY_NON_NULL(2)
  7471. token_type scan_literal(const char_type* literal_text, const std::size_t length,
  7472. token_type return_type)
  7473. {
  7474. JSON_ASSERT(std::char_traits<char_type>::to_char_type(current) == literal_text[0]);
  7475. for (std::size_t i = 1; i < length; ++i)
  7476. {
  7477. if (JSON_HEDLEY_UNLIKELY(std::char_traits<char_type>::to_char_type(get()) != literal_text[i]))
  7478. {
  7479. error_message = "invalid literal";
  7480. return token_type::parse_error;
  7481. }
  7482. }
  7483. return return_type;
  7484. }
  7485. /////////////////////
  7486. // input management
  7487. /////////////////////
  7488. /// reset token_buffer; current character is beginning of token
  7489. void reset() noexcept
  7490. {
  7491. token_buffer.clear();
  7492. token_string.clear();
  7493. token_string.push_back(std::char_traits<char_type>::to_char_type(current));
  7494. }
  7495. /*
  7496. @brief get next character from the input
  7497. This function provides the interface to the used input adapter. It does
  7498. not throw in case the input reached EOF, but returns a
  7499. `std::char_traits<char>::eof()` in that case. Stores the scanned characters
  7500. for use in error messages.
  7501. @return character read from the input
  7502. */
  7503. char_int_type get()
  7504. {
  7505. ++position.chars_read_total;
  7506. ++position.chars_read_current_line;
  7507. if (next_unget)
  7508. {
  7509. // just reset the next_unget variable and work with current
  7510. next_unget = false;
  7511. }
  7512. else
  7513. {
  7514. current = ia.get_character();
  7515. }
  7516. if (JSON_HEDLEY_LIKELY(current != std::char_traits<char_type>::eof()))
  7517. {
  7518. token_string.push_back(std::char_traits<char_type>::to_char_type(current));
  7519. }
  7520. if (current == '\n')
  7521. {
  7522. ++position.lines_read;
  7523. position.chars_read_current_line = 0;
  7524. }
  7525. return current;
  7526. }
  7527. /*!
  7528. @brief unget current character (read it again on next get)
  7529. We implement unget by setting variable next_unget to true. The input is not
  7530. changed - we just simulate ungetting by modifying chars_read_total,
  7531. chars_read_current_line, and token_string. The next call to get() will
  7532. behave as if the unget character is read again.
  7533. */
  7534. void unget()
  7535. {
  7536. next_unget = true;
  7537. --position.chars_read_total;
  7538. // in case we "unget" a newline, we have to also decrement the lines_read
  7539. if (position.chars_read_current_line == 0)
  7540. {
  7541. if (position.lines_read > 0)
  7542. {
  7543. --position.lines_read;
  7544. }
  7545. }
  7546. else
  7547. {
  7548. --position.chars_read_current_line;
  7549. }
  7550. if (JSON_HEDLEY_LIKELY(current != std::char_traits<char_type>::eof()))
  7551. {
  7552. JSON_ASSERT(!token_string.empty());
  7553. token_string.pop_back();
  7554. }
  7555. }
  7556. /// add a character to token_buffer
  7557. void add(char_int_type c)
  7558. {
  7559. token_buffer.push_back(static_cast<typename string_t::value_type>(c));
  7560. }
  7561. public:
  7562. /////////////////////
  7563. // value getters
  7564. /////////////////////
  7565. /// return integer value
  7566. constexpr number_integer_t get_number_integer() const noexcept
  7567. {
  7568. return value_integer;
  7569. }
  7570. /// return unsigned integer value
  7571. constexpr number_unsigned_t get_number_unsigned() const noexcept
  7572. {
  7573. return value_unsigned;
  7574. }
  7575. /// return floating-point value
  7576. constexpr number_float_t get_number_float() const noexcept
  7577. {
  7578. return value_float;
  7579. }
  7580. /// return current string value (implicitly resets the token; useful only once)
  7581. string_t& get_string()
  7582. {
  7583. return token_buffer;
  7584. }
  7585. /////////////////////
  7586. // diagnostics
  7587. /////////////////////
  7588. /// return position of last read token
  7589. constexpr position_t get_position() const noexcept
  7590. {
  7591. return position;
  7592. }
  7593. /// return the last read token (for errors only). Will never contain EOF
  7594. /// (an arbitrary value that is not a valid char value, often -1), because
  7595. /// 255 may legitimately occur. May contain NUL, which should be escaped.
  7596. std::string get_token_string() const
  7597. {
  7598. // escape control characters
  7599. std::string result;
  7600. for (const auto c : token_string)
  7601. {
  7602. if (static_cast<unsigned char>(c) <= '\x1F')
  7603. {
  7604. // escape control characters
  7605. std::array<char, 9> cs{{}};
  7606. static_cast<void>((std::snprintf)(cs.data(), cs.size(), "<U+%.4X>", static_cast<unsigned char>(c))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  7607. result += cs.data();
  7608. }
  7609. else
  7610. {
  7611. // add character as is
  7612. result.push_back(static_cast<std::string::value_type>(c));
  7613. }
  7614. }
  7615. return result;
  7616. }
  7617. /// return syntax error message
  7618. JSON_HEDLEY_RETURNS_NON_NULL
  7619. constexpr const char* get_error_message() const noexcept
  7620. {
  7621. return error_message;
  7622. }
  7623. /////////////////////
  7624. // actual scanner
  7625. /////////////////////
  7626. /*!
  7627. @brief skip the UTF-8 byte order mark
  7628. @return true iff there is no BOM or the correct BOM has been skipped
  7629. */
  7630. bool skip_bom()
  7631. {
  7632. if (get() == 0xEF)
  7633. {
  7634. // check if we completely parse the BOM
  7635. return get() == 0xBB && get() == 0xBF;
  7636. }
  7637. // the first character is not the beginning of the BOM; unget it to
  7638. // process is later
  7639. unget();
  7640. return true;
  7641. }
  7642. void skip_whitespace()
  7643. {
  7644. do
  7645. {
  7646. get();
  7647. }
  7648. while (current == ' ' || current == '\t' || current == '\n' || current == '\r');
  7649. }
  7650. token_type scan()
  7651. {
  7652. // initially, skip the BOM
  7653. if (position.chars_read_total == 0 && !skip_bom())
  7654. {
  7655. error_message = "invalid BOM; must be 0xEF 0xBB 0xBF if given";
  7656. return token_type::parse_error;
  7657. }
  7658. // read next character and ignore whitespace
  7659. skip_whitespace();
  7660. // ignore comments
  7661. while (ignore_comments && current == '/')
  7662. {
  7663. if (!scan_comment())
  7664. {
  7665. return token_type::parse_error;
  7666. }
  7667. // skip following whitespace
  7668. skip_whitespace();
  7669. }
  7670. switch (current)
  7671. {
  7672. // structural characters
  7673. case '[':
  7674. return token_type::begin_array;
  7675. case ']':
  7676. return token_type::end_array;
  7677. case '{':
  7678. return token_type::begin_object;
  7679. case '}':
  7680. return token_type::end_object;
  7681. case ':':
  7682. return token_type::name_separator;
  7683. case ',':
  7684. return token_type::value_separator;
  7685. // literals
  7686. case 't':
  7687. {
  7688. 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')}};
  7689. return scan_literal(true_literal.data(), true_literal.size(), token_type::literal_true);
  7690. }
  7691. case 'f':
  7692. {
  7693. 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')}};
  7694. return scan_literal(false_literal.data(), false_literal.size(), token_type::literal_false);
  7695. }
  7696. case 'n':
  7697. {
  7698. 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')}};
  7699. return scan_literal(null_literal.data(), null_literal.size(), token_type::literal_null);
  7700. }
  7701. // string
  7702. case '\"':
  7703. return scan_string();
  7704. // number
  7705. case '-':
  7706. case '0':
  7707. case '1':
  7708. case '2':
  7709. case '3':
  7710. case '4':
  7711. case '5':
  7712. case '6':
  7713. case '7':
  7714. case '8':
  7715. case '9':
  7716. return scan_number();
  7717. // end of input (the null byte is needed when parsing from
  7718. // string literals)
  7719. case '\0':
  7720. case std::char_traits<char_type>::eof():
  7721. return token_type::end_of_input;
  7722. // error
  7723. default:
  7724. error_message = "invalid literal";
  7725. return token_type::parse_error;
  7726. }
  7727. }
  7728. private:
  7729. /// input adapter
  7730. InputAdapterType ia;
  7731. /// whether comments should be ignored (true) or signaled as errors (false)
  7732. const bool ignore_comments = false;
  7733. /// the current character
  7734. char_int_type current = std::char_traits<char_type>::eof();
  7735. /// whether the next get() call should just return current
  7736. bool next_unget = false;
  7737. /// the start position of the current token
  7738. position_t position {};
  7739. /// raw input token string (for error messages)
  7740. std::vector<char_type> token_string {};
  7741. /// buffer for variable-length tokens (numbers, strings)
  7742. string_t token_buffer {};
  7743. /// a description of occurred lexer errors
  7744. const char* error_message = "";
  7745. // number values
  7746. number_integer_t value_integer = 0;
  7747. number_unsigned_t value_unsigned = 0;
  7748. number_float_t value_float = 0;
  7749. /// the decimal point
  7750. const char_int_type decimal_point_char = '.';
  7751. };
  7752. } // namespace detail
  7753. NLOHMANN_JSON_NAMESPACE_END
  7754. // #include <nlohmann/detail/macro_scope.hpp>
  7755. // #include <nlohmann/detail/meta/is_sax.hpp>
  7756. // __ _____ _____ _____
  7757. // __| | __| | | | JSON for Modern C++
  7758. // | | |__ | | | | | | version 3.11.2
  7759. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  7760. //
  7761. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  7762. // SPDX-License-Identifier: MIT
  7763. #include <cstdint> // size_t
  7764. #include <utility> // declval
  7765. #include <string> // string
  7766. // #include <nlohmann/detail/abi_macros.hpp>
  7767. // #include <nlohmann/detail/meta/detected.hpp>
  7768. // #include <nlohmann/detail/meta/type_traits.hpp>
  7769. NLOHMANN_JSON_NAMESPACE_BEGIN
  7770. namespace detail
  7771. {
  7772. template<typename T>
  7773. using null_function_t = decltype(std::declval<T&>().null());
  7774. template<typename T>
  7775. using boolean_function_t =
  7776. decltype(std::declval<T&>().boolean(std::declval<bool>()));
  7777. template<typename T, typename Integer>
  7778. using number_integer_function_t =
  7779. decltype(std::declval<T&>().number_integer(std::declval<Integer>()));
  7780. template<typename T, typename Unsigned>
  7781. using number_unsigned_function_t =
  7782. decltype(std::declval<T&>().number_unsigned(std::declval<Unsigned>()));
  7783. template<typename T, typename Float, typename String>
  7784. using number_float_function_t = decltype(std::declval<T&>().number_float(
  7785. std::declval<Float>(), std::declval<const String&>()));
  7786. template<typename T, typename String>
  7787. using string_function_t =
  7788. decltype(std::declval<T&>().string(std::declval<String&>()));
  7789. template<typename T, typename Binary>
  7790. using binary_function_t =
  7791. decltype(std::declval<T&>().binary(std::declval<Binary&>()));
  7792. template<typename T>
  7793. using start_object_function_t =
  7794. decltype(std::declval<T&>().start_object(std::declval<std::size_t>()));
  7795. template<typename T, typename String>
  7796. using key_function_t =
  7797. decltype(std::declval<T&>().key(std::declval<String&>()));
  7798. template<typename T>
  7799. using end_object_function_t = decltype(std::declval<T&>().end_object());
  7800. template<typename T>
  7801. using start_array_function_t =
  7802. decltype(std::declval<T&>().start_array(std::declval<std::size_t>()));
  7803. template<typename T>
  7804. using end_array_function_t = decltype(std::declval<T&>().end_array());
  7805. template<typename T, typename Exception>
  7806. using parse_error_function_t = decltype(std::declval<T&>().parse_error(
  7807. std::declval<std::size_t>(), std::declval<const std::string&>(),
  7808. std::declval<const Exception&>()));
  7809. template<typename SAX, typename BasicJsonType>
  7810. struct is_sax
  7811. {
  7812. private:
  7813. static_assert(is_basic_json<BasicJsonType>::value,
  7814. "BasicJsonType must be of type basic_json<...>");
  7815. using number_integer_t = typename BasicJsonType::number_integer_t;
  7816. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  7817. using number_float_t = typename BasicJsonType::number_float_t;
  7818. using string_t = typename BasicJsonType::string_t;
  7819. using binary_t = typename BasicJsonType::binary_t;
  7820. using exception_t = typename BasicJsonType::exception;
  7821. public:
  7822. static constexpr bool value =
  7823. is_detected_exact<bool, null_function_t, SAX>::value &&
  7824. is_detected_exact<bool, boolean_function_t, SAX>::value &&
  7825. is_detected_exact<bool, number_integer_function_t, SAX, number_integer_t>::value &&
  7826. is_detected_exact<bool, number_unsigned_function_t, SAX, number_unsigned_t>::value &&
  7827. is_detected_exact<bool, number_float_function_t, SAX, number_float_t, string_t>::value &&
  7828. is_detected_exact<bool, string_function_t, SAX, string_t>::value &&
  7829. is_detected_exact<bool, binary_function_t, SAX, binary_t>::value &&
  7830. is_detected_exact<bool, start_object_function_t, SAX>::value &&
  7831. is_detected_exact<bool, key_function_t, SAX, string_t>::value &&
  7832. is_detected_exact<bool, end_object_function_t, SAX>::value &&
  7833. is_detected_exact<bool, start_array_function_t, SAX>::value &&
  7834. is_detected_exact<bool, end_array_function_t, SAX>::value &&
  7835. is_detected_exact<bool, parse_error_function_t, SAX, exception_t>::value;
  7836. };
  7837. template<typename SAX, typename BasicJsonType>
  7838. struct is_sax_static_asserts
  7839. {
  7840. private:
  7841. static_assert(is_basic_json<BasicJsonType>::value,
  7842. "BasicJsonType must be of type basic_json<...>");
  7843. using number_integer_t = typename BasicJsonType::number_integer_t;
  7844. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  7845. using number_float_t = typename BasicJsonType::number_float_t;
  7846. using string_t = typename BasicJsonType::string_t;
  7847. using binary_t = typename BasicJsonType::binary_t;
  7848. using exception_t = typename BasicJsonType::exception;
  7849. public:
  7850. static_assert(is_detected_exact<bool, null_function_t, SAX>::value,
  7851. "Missing/invalid function: bool null()");
  7852. static_assert(is_detected_exact<bool, boolean_function_t, SAX>::value,
  7853. "Missing/invalid function: bool boolean(bool)");
  7854. static_assert(is_detected_exact<bool, boolean_function_t, SAX>::value,
  7855. "Missing/invalid function: bool boolean(bool)");
  7856. static_assert(
  7857. is_detected_exact<bool, number_integer_function_t, SAX,
  7858. number_integer_t>::value,
  7859. "Missing/invalid function: bool number_integer(number_integer_t)");
  7860. static_assert(
  7861. is_detected_exact<bool, number_unsigned_function_t, SAX,
  7862. number_unsigned_t>::value,
  7863. "Missing/invalid function: bool number_unsigned(number_unsigned_t)");
  7864. static_assert(is_detected_exact<bool, number_float_function_t, SAX,
  7865. number_float_t, string_t>::value,
  7866. "Missing/invalid function: bool number_float(number_float_t, const string_t&)");
  7867. static_assert(
  7868. is_detected_exact<bool, string_function_t, SAX, string_t>::value,
  7869. "Missing/invalid function: bool string(string_t&)");
  7870. static_assert(
  7871. is_detected_exact<bool, binary_function_t, SAX, binary_t>::value,
  7872. "Missing/invalid function: bool binary(binary_t&)");
  7873. static_assert(is_detected_exact<bool, start_object_function_t, SAX>::value,
  7874. "Missing/invalid function: bool start_object(std::size_t)");
  7875. static_assert(is_detected_exact<bool, key_function_t, SAX, string_t>::value,
  7876. "Missing/invalid function: bool key(string_t&)");
  7877. static_assert(is_detected_exact<bool, end_object_function_t, SAX>::value,
  7878. "Missing/invalid function: bool end_object()");
  7879. static_assert(is_detected_exact<bool, start_array_function_t, SAX>::value,
  7880. "Missing/invalid function: bool start_array(std::size_t)");
  7881. static_assert(is_detected_exact<bool, end_array_function_t, SAX>::value,
  7882. "Missing/invalid function: bool end_array()");
  7883. static_assert(
  7884. is_detected_exact<bool, parse_error_function_t, SAX, exception_t>::value,
  7885. "Missing/invalid function: bool parse_error(std::size_t, const "
  7886. "std::string&, const exception&)");
  7887. };
  7888. } // namespace detail
  7889. NLOHMANN_JSON_NAMESPACE_END
  7890. // #include <nlohmann/detail/meta/type_traits.hpp>
  7891. // #include <nlohmann/detail/string_concat.hpp>
  7892. // #include <nlohmann/detail/value_t.hpp>
  7893. NLOHMANN_JSON_NAMESPACE_BEGIN
  7894. namespace detail
  7895. {
  7896. /// how to treat CBOR tags
  7897. enum class cbor_tag_handler_t
  7898. {
  7899. error, ///< throw a parse_error exception in case of a tag
  7900. ignore, ///< ignore tags
  7901. store ///< store tags as binary type
  7902. };
  7903. /*!
  7904. @brief determine system byte order
  7905. @return true if and only if system's byte order is little endian
  7906. @note from https://stackoverflow.com/a/1001328/266378
  7907. */
  7908. static inline bool little_endianness(int num = 1) noexcept
  7909. {
  7910. return *reinterpret_cast<char*>(&num) == 1;
  7911. }
  7912. ///////////////////
  7913. // binary reader //
  7914. ///////////////////
  7915. /*!
  7916. @brief deserialization of CBOR, MessagePack, and UBJSON values
  7917. */
  7918. template<typename BasicJsonType, typename InputAdapterType, typename SAX = json_sax_dom_parser<BasicJsonType>>
  7919. class binary_reader
  7920. {
  7921. using number_integer_t = typename BasicJsonType::number_integer_t;
  7922. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  7923. using number_float_t = typename BasicJsonType::number_float_t;
  7924. using string_t = typename BasicJsonType::string_t;
  7925. using binary_t = typename BasicJsonType::binary_t;
  7926. using json_sax_t = SAX;
  7927. using char_type = typename InputAdapterType::char_type;
  7928. using char_int_type = typename std::char_traits<char_type>::int_type;
  7929. public:
  7930. /*!
  7931. @brief create a binary reader
  7932. @param[in] adapter input adapter to read from
  7933. */
  7934. explicit binary_reader(InputAdapterType&& adapter, const input_format_t format = input_format_t::json) noexcept : ia(std::move(adapter)), input_format(format)
  7935. {
  7936. (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
  7937. }
  7938. // make class move-only
  7939. binary_reader(const binary_reader&) = delete;
  7940. binary_reader(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  7941. binary_reader& operator=(const binary_reader&) = delete;
  7942. binary_reader& operator=(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  7943. ~binary_reader() = default;
  7944. /*!
  7945. @param[in] format the binary format to parse
  7946. @param[in] sax_ a SAX event processor
  7947. @param[in] strict whether to expect the input to be consumed completed
  7948. @param[in] tag_handler how to treat CBOR tags
  7949. @return whether parsing was successful
  7950. */
  7951. JSON_HEDLEY_NON_NULL(3)
  7952. bool sax_parse(const input_format_t format,
  7953. json_sax_t* sax_,
  7954. const bool strict = true,
  7955. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  7956. {
  7957. sax = sax_;
  7958. bool result = false;
  7959. switch (format)
  7960. {
  7961. case input_format_t::bson:
  7962. result = parse_bson_internal();
  7963. break;
  7964. case input_format_t::cbor:
  7965. result = parse_cbor_internal(true, tag_handler);
  7966. break;
  7967. case input_format_t::msgpack:
  7968. result = parse_msgpack_internal();
  7969. break;
  7970. case input_format_t::ubjson:
  7971. case input_format_t::bjdata:
  7972. result = parse_ubjson_internal();
  7973. break;
  7974. case input_format_t::json: // LCOV_EXCL_LINE
  7975. default: // LCOV_EXCL_LINE
  7976. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  7977. }
  7978. // strict mode: next byte must be EOF
  7979. if (result && strict)
  7980. {
  7981. if (input_format == input_format_t::ubjson || input_format == input_format_t::bjdata)
  7982. {
  7983. get_ignore_noop();
  7984. }
  7985. else
  7986. {
  7987. get();
  7988. }
  7989. if (JSON_HEDLEY_UNLIKELY(current != std::char_traits<char_type>::eof()))
  7990. {
  7991. return sax->parse_error(chars_read, get_token_string(), parse_error::create(110, chars_read,
  7992. exception_message(input_format, concat("expected end of input; last byte: 0x", get_token_string()), "value"), nullptr));
  7993. }
  7994. }
  7995. return result;
  7996. }
  7997. private:
  7998. //////////
  7999. // BSON //
  8000. //////////
  8001. /*!
  8002. @brief Reads in a BSON-object and passes it to the SAX-parser.
  8003. @return whether a valid BSON-value was passed to the SAX parser
  8004. */
  8005. bool parse_bson_internal()
  8006. {
  8007. std::int32_t document_size{};
  8008. get_number<std::int32_t, true>(input_format_t::bson, document_size);
  8009. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast<std::size_t>(-1))))
  8010. {
  8011. return false;
  8012. }
  8013. if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/false)))
  8014. {
  8015. return false;
  8016. }
  8017. return sax->end_object();
  8018. }
  8019. /*!
  8020. @brief Parses a C-style string from the BSON input.
  8021. @param[in,out] result A reference to the string variable where the read
  8022. string is to be stored.
  8023. @return `true` if the \x00-byte indicating the end of the string was
  8024. encountered before the EOF; false` indicates an unexpected EOF.
  8025. */
  8026. bool get_bson_cstr(string_t& result)
  8027. {
  8028. auto out = std::back_inserter(result);
  8029. while (true)
  8030. {
  8031. get();
  8032. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "cstring")))
  8033. {
  8034. return false;
  8035. }
  8036. if (current == 0x00)
  8037. {
  8038. return true;
  8039. }
  8040. *out++ = static_cast<typename string_t::value_type>(current);
  8041. }
  8042. }
  8043. /*!
  8044. @brief Parses a zero-terminated string of length @a len from the BSON
  8045. input.
  8046. @param[in] len The length (including the zero-byte at the end) of the
  8047. string to be read.
  8048. @param[in,out] result A reference to the string variable where the read
  8049. string is to be stored.
  8050. @tparam NumberType The type of the length @a len
  8051. @pre len >= 1
  8052. @return `true` if the string was successfully parsed
  8053. */
  8054. template<typename NumberType>
  8055. bool get_bson_string(const NumberType len, string_t& result)
  8056. {
  8057. if (JSON_HEDLEY_UNLIKELY(len < 1))
  8058. {
  8059. auto last_token = get_token_string();
  8060. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  8061. exception_message(input_format_t::bson, concat("string length must be at least 1, is ", std::to_string(len)), "string"), nullptr));
  8062. }
  8063. return get_string(input_format_t::bson, len - static_cast<NumberType>(1), result) && get() != std::char_traits<char_type>::eof();
  8064. }
  8065. /*!
  8066. @brief Parses a byte array input of length @a len from the BSON input.
  8067. @param[in] len The length of the byte array to be read.
  8068. @param[in,out] result A reference to the binary variable where the read
  8069. array is to be stored.
  8070. @tparam NumberType The type of the length @a len
  8071. @pre len >= 0
  8072. @return `true` if the byte array was successfully parsed
  8073. */
  8074. template<typename NumberType>
  8075. bool get_bson_binary(const NumberType len, binary_t& result)
  8076. {
  8077. if (JSON_HEDLEY_UNLIKELY(len < 0))
  8078. {
  8079. auto last_token = get_token_string();
  8080. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  8081. exception_message(input_format_t::bson, concat("byte array length cannot be negative, is ", std::to_string(len)), "binary"), nullptr));
  8082. }
  8083. // All BSON binary values have a subtype
  8084. std::uint8_t subtype{};
  8085. get_number<std::uint8_t>(input_format_t::bson, subtype);
  8086. result.set_subtype(subtype);
  8087. return get_binary(input_format_t::bson, len, result);
  8088. }
  8089. /*!
  8090. @brief Read a BSON document element of the given @a element_type.
  8091. @param[in] element_type The BSON element type, c.f. http://bsonspec.org/spec.html
  8092. @param[in] element_type_parse_position The position in the input stream,
  8093. where the `element_type` was read.
  8094. @warning Not all BSON element types are supported yet. An unsupported
  8095. @a element_type will give rise to a parse_error.114:
  8096. Unsupported BSON record type 0x...
  8097. @return whether a valid BSON-object/array was passed to the SAX parser
  8098. */
  8099. bool parse_bson_element_internal(const char_int_type element_type,
  8100. const std::size_t element_type_parse_position)
  8101. {
  8102. switch (element_type)
  8103. {
  8104. case 0x01: // double
  8105. {
  8106. double number{};
  8107. return get_number<double, true>(input_format_t::bson, number) && sax->number_float(static_cast<number_float_t>(number), "");
  8108. }
  8109. case 0x02: // string
  8110. {
  8111. std::int32_t len{};
  8112. string_t value;
  8113. return get_number<std::int32_t, true>(input_format_t::bson, len) && get_bson_string(len, value) && sax->string(value);
  8114. }
  8115. case 0x03: // object
  8116. {
  8117. return parse_bson_internal();
  8118. }
  8119. case 0x04: // array
  8120. {
  8121. return parse_bson_array();
  8122. }
  8123. case 0x05: // binary
  8124. {
  8125. std::int32_t len{};
  8126. binary_t value;
  8127. return get_number<std::int32_t, true>(input_format_t::bson, len) && get_bson_binary(len, value) && sax->binary(value);
  8128. }
  8129. case 0x08: // boolean
  8130. {
  8131. return sax->boolean(get() != 0);
  8132. }
  8133. case 0x0A: // null
  8134. {
  8135. return sax->null();
  8136. }
  8137. case 0x10: // int32
  8138. {
  8139. std::int32_t value{};
  8140. return get_number<std::int32_t, true>(input_format_t::bson, value) && sax->number_integer(value);
  8141. }
  8142. case 0x12: // int64
  8143. {
  8144. std::int64_t value{};
  8145. return get_number<std::int64_t, true>(input_format_t::bson, value) && sax->number_integer(value);
  8146. }
  8147. default: // anything else not supported (yet)
  8148. {
  8149. std::array<char, 3> cr{{}};
  8150. static_cast<void>((std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(element_type))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  8151. const std::string cr_str{cr.data()};
  8152. return sax->parse_error(element_type_parse_position, cr_str,
  8153. parse_error::create(114, element_type_parse_position, concat("Unsupported BSON record type 0x", cr_str), nullptr));
  8154. }
  8155. }
  8156. }
  8157. /*!
  8158. @brief Read a BSON element list (as specified in the BSON-spec)
  8159. The same binary layout is used for objects and arrays, hence it must be
  8160. indicated with the argument @a is_array which one is expected
  8161. (true --> array, false --> object).
  8162. @param[in] is_array Determines if the element list being read is to be
  8163. treated as an object (@a is_array == false), or as an
  8164. array (@a is_array == true).
  8165. @return whether a valid BSON-object/array was passed to the SAX parser
  8166. */
  8167. bool parse_bson_element_list(const bool is_array)
  8168. {
  8169. string_t key;
  8170. while (auto element_type = get())
  8171. {
  8172. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "element list")))
  8173. {
  8174. return false;
  8175. }
  8176. const std::size_t element_type_parse_position = chars_read;
  8177. if (JSON_HEDLEY_UNLIKELY(!get_bson_cstr(key)))
  8178. {
  8179. return false;
  8180. }
  8181. if (!is_array && !sax->key(key))
  8182. {
  8183. return false;
  8184. }
  8185. if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_internal(element_type, element_type_parse_position)))
  8186. {
  8187. return false;
  8188. }
  8189. // get_bson_cstr only appends
  8190. key.clear();
  8191. }
  8192. return true;
  8193. }
  8194. /*!
  8195. @brief Reads an array from the BSON input and passes it to the SAX-parser.
  8196. @return whether a valid BSON-array was passed to the SAX parser
  8197. */
  8198. bool parse_bson_array()
  8199. {
  8200. std::int32_t document_size{};
  8201. get_number<std::int32_t, true>(input_format_t::bson, document_size);
  8202. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
  8203. {
  8204. return false;
  8205. }
  8206. if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/true)))
  8207. {
  8208. return false;
  8209. }
  8210. return sax->end_array();
  8211. }
  8212. //////////
  8213. // CBOR //
  8214. //////////
  8215. /*!
  8216. @param[in] get_char whether a new character should be retrieved from the
  8217. input (true) or whether the last read character should
  8218. be considered instead (false)
  8219. @param[in] tag_handler how CBOR tags should be treated
  8220. @return whether a valid CBOR value was passed to the SAX parser
  8221. */
  8222. bool parse_cbor_internal(const bool get_char,
  8223. const cbor_tag_handler_t tag_handler)
  8224. {
  8225. switch (get_char ? get() : current)
  8226. {
  8227. // EOF
  8228. case std::char_traits<char_type>::eof():
  8229. return unexpect_eof(input_format_t::cbor, "value");
  8230. // Integer 0x00..0x17 (0..23)
  8231. case 0x00:
  8232. case 0x01:
  8233. case 0x02:
  8234. case 0x03:
  8235. case 0x04:
  8236. case 0x05:
  8237. case 0x06:
  8238. case 0x07:
  8239. case 0x08:
  8240. case 0x09:
  8241. case 0x0A:
  8242. case 0x0B:
  8243. case 0x0C:
  8244. case 0x0D:
  8245. case 0x0E:
  8246. case 0x0F:
  8247. case 0x10:
  8248. case 0x11:
  8249. case 0x12:
  8250. case 0x13:
  8251. case 0x14:
  8252. case 0x15:
  8253. case 0x16:
  8254. case 0x17:
  8255. return sax->number_unsigned(static_cast<number_unsigned_t>(current));
  8256. case 0x18: // Unsigned integer (one-byte uint8_t follows)
  8257. {
  8258. std::uint8_t number{};
  8259. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  8260. }
  8261. case 0x19: // Unsigned integer (two-byte uint16_t follows)
  8262. {
  8263. std::uint16_t number{};
  8264. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  8265. }
  8266. case 0x1A: // Unsigned integer (four-byte uint32_t follows)
  8267. {
  8268. std::uint32_t number{};
  8269. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  8270. }
  8271. case 0x1B: // Unsigned integer (eight-byte uint64_t follows)
  8272. {
  8273. std::uint64_t number{};
  8274. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  8275. }
  8276. // Negative integer -1-0x00..-1-0x17 (-1..-24)
  8277. case 0x20:
  8278. case 0x21:
  8279. case 0x22:
  8280. case 0x23:
  8281. case 0x24:
  8282. case 0x25:
  8283. case 0x26:
  8284. case 0x27:
  8285. case 0x28:
  8286. case 0x29:
  8287. case 0x2A:
  8288. case 0x2B:
  8289. case 0x2C:
  8290. case 0x2D:
  8291. case 0x2E:
  8292. case 0x2F:
  8293. case 0x30:
  8294. case 0x31:
  8295. case 0x32:
  8296. case 0x33:
  8297. case 0x34:
  8298. case 0x35:
  8299. case 0x36:
  8300. case 0x37:
  8301. return sax->number_integer(static_cast<std::int8_t>(0x20 - 1 - current));
  8302. case 0x38: // Negative integer (one-byte uint8_t follows)
  8303. {
  8304. std::uint8_t number{};
  8305. return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
  8306. }
  8307. case 0x39: // Negative integer -1-n (two-byte uint16_t follows)
  8308. {
  8309. std::uint16_t number{};
  8310. return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
  8311. }
  8312. case 0x3A: // Negative integer -1-n (four-byte uint32_t follows)
  8313. {
  8314. std::uint32_t number{};
  8315. return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
  8316. }
  8317. case 0x3B: // Negative integer -1-n (eight-byte uint64_t follows)
  8318. {
  8319. std::uint64_t number{};
  8320. return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1)
  8321. - static_cast<number_integer_t>(number));
  8322. }
  8323. // Binary data (0x00..0x17 bytes follow)
  8324. case 0x40:
  8325. case 0x41:
  8326. case 0x42:
  8327. case 0x43:
  8328. case 0x44:
  8329. case 0x45:
  8330. case 0x46:
  8331. case 0x47:
  8332. case 0x48:
  8333. case 0x49:
  8334. case 0x4A:
  8335. case 0x4B:
  8336. case 0x4C:
  8337. case 0x4D:
  8338. case 0x4E:
  8339. case 0x4F:
  8340. case 0x50:
  8341. case 0x51:
  8342. case 0x52:
  8343. case 0x53:
  8344. case 0x54:
  8345. case 0x55:
  8346. case 0x56:
  8347. case 0x57:
  8348. case 0x58: // Binary data (one-byte uint8_t for n follows)
  8349. case 0x59: // Binary data (two-byte uint16_t for n follow)
  8350. case 0x5A: // Binary data (four-byte uint32_t for n follow)
  8351. case 0x5B: // Binary data (eight-byte uint64_t for n follow)
  8352. case 0x5F: // Binary data (indefinite length)
  8353. {
  8354. binary_t b;
  8355. return get_cbor_binary(b) && sax->binary(b);
  8356. }
  8357. // UTF-8 string (0x00..0x17 bytes follow)
  8358. case 0x60:
  8359. case 0x61:
  8360. case 0x62:
  8361. case 0x63:
  8362. case 0x64:
  8363. case 0x65:
  8364. case 0x66:
  8365. case 0x67:
  8366. case 0x68:
  8367. case 0x69:
  8368. case 0x6A:
  8369. case 0x6B:
  8370. case 0x6C:
  8371. case 0x6D:
  8372. case 0x6E:
  8373. case 0x6F:
  8374. case 0x70:
  8375. case 0x71:
  8376. case 0x72:
  8377. case 0x73:
  8378. case 0x74:
  8379. case 0x75:
  8380. case 0x76:
  8381. case 0x77:
  8382. case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
  8383. case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
  8384. case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
  8385. case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
  8386. case 0x7F: // UTF-8 string (indefinite length)
  8387. {
  8388. string_t s;
  8389. return get_cbor_string(s) && sax->string(s);
  8390. }
  8391. // array (0x00..0x17 data items follow)
  8392. case 0x80:
  8393. case 0x81:
  8394. case 0x82:
  8395. case 0x83:
  8396. case 0x84:
  8397. case 0x85:
  8398. case 0x86:
  8399. case 0x87:
  8400. case 0x88:
  8401. case 0x89:
  8402. case 0x8A:
  8403. case 0x8B:
  8404. case 0x8C:
  8405. case 0x8D:
  8406. case 0x8E:
  8407. case 0x8F:
  8408. case 0x90:
  8409. case 0x91:
  8410. case 0x92:
  8411. case 0x93:
  8412. case 0x94:
  8413. case 0x95:
  8414. case 0x96:
  8415. case 0x97:
  8416. return get_cbor_array(
  8417. conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
  8418. case 0x98: // array (one-byte uint8_t for n follows)
  8419. {
  8420. std::uint8_t len{};
  8421. return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
  8422. }
  8423. case 0x99: // array (two-byte uint16_t for n follow)
  8424. {
  8425. std::uint16_t len{};
  8426. return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
  8427. }
  8428. case 0x9A: // array (four-byte uint32_t for n follow)
  8429. {
  8430. std::uint32_t len{};
  8431. return get_number(input_format_t::cbor, len) && get_cbor_array(conditional_static_cast<std::size_t>(len), tag_handler);
  8432. }
  8433. case 0x9B: // array (eight-byte uint64_t for n follow)
  8434. {
  8435. std::uint64_t len{};
  8436. return get_number(input_format_t::cbor, len) && get_cbor_array(conditional_static_cast<std::size_t>(len), tag_handler);
  8437. }
  8438. case 0x9F: // array (indefinite length)
  8439. return get_cbor_array(static_cast<std::size_t>(-1), tag_handler);
  8440. // map (0x00..0x17 pairs of data items follow)
  8441. case 0xA0:
  8442. case 0xA1:
  8443. case 0xA2:
  8444. case 0xA3:
  8445. case 0xA4:
  8446. case 0xA5:
  8447. case 0xA6:
  8448. case 0xA7:
  8449. case 0xA8:
  8450. case 0xA9:
  8451. case 0xAA:
  8452. case 0xAB:
  8453. case 0xAC:
  8454. case 0xAD:
  8455. case 0xAE:
  8456. case 0xAF:
  8457. case 0xB0:
  8458. case 0xB1:
  8459. case 0xB2:
  8460. case 0xB3:
  8461. case 0xB4:
  8462. case 0xB5:
  8463. case 0xB6:
  8464. case 0xB7:
  8465. return get_cbor_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
  8466. case 0xB8: // map (one-byte uint8_t for n follows)
  8467. {
  8468. std::uint8_t len{};
  8469. return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
  8470. }
  8471. case 0xB9: // map (two-byte uint16_t for n follow)
  8472. {
  8473. std::uint16_t len{};
  8474. return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
  8475. }
  8476. case 0xBA: // map (four-byte uint32_t for n follow)
  8477. {
  8478. std::uint32_t len{};
  8479. return get_number(input_format_t::cbor, len) && get_cbor_object(conditional_static_cast<std::size_t>(len), tag_handler);
  8480. }
  8481. case 0xBB: // map (eight-byte uint64_t for n follow)
  8482. {
  8483. std::uint64_t len{};
  8484. return get_number(input_format_t::cbor, len) && get_cbor_object(conditional_static_cast<std::size_t>(len), tag_handler);
  8485. }
  8486. case 0xBF: // map (indefinite length)
  8487. return get_cbor_object(static_cast<std::size_t>(-1), tag_handler);
  8488. case 0xC6: // tagged item
  8489. case 0xC7:
  8490. case 0xC8:
  8491. case 0xC9:
  8492. case 0xCA:
  8493. case 0xCB:
  8494. case 0xCC:
  8495. case 0xCD:
  8496. case 0xCE:
  8497. case 0xCF:
  8498. case 0xD0:
  8499. case 0xD1:
  8500. case 0xD2:
  8501. case 0xD3:
  8502. case 0xD4:
  8503. case 0xD8: // tagged item (1 bytes follow)
  8504. case 0xD9: // tagged item (2 bytes follow)
  8505. case 0xDA: // tagged item (4 bytes follow)
  8506. case 0xDB: // tagged item (8 bytes follow)
  8507. {
  8508. switch (tag_handler)
  8509. {
  8510. case cbor_tag_handler_t::error:
  8511. {
  8512. auto last_token = get_token_string();
  8513. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  8514. exception_message(input_format_t::cbor, concat("invalid byte: 0x", last_token), "value"), nullptr));
  8515. }
  8516. case cbor_tag_handler_t::ignore:
  8517. {
  8518. // ignore binary subtype
  8519. switch (current)
  8520. {
  8521. case 0xD8:
  8522. {
  8523. std::uint8_t subtype_to_ignore{};
  8524. get_number(input_format_t::cbor, subtype_to_ignore);
  8525. break;
  8526. }
  8527. case 0xD9:
  8528. {
  8529. std::uint16_t subtype_to_ignore{};
  8530. get_number(input_format_t::cbor, subtype_to_ignore);
  8531. break;
  8532. }
  8533. case 0xDA:
  8534. {
  8535. std::uint32_t subtype_to_ignore{};
  8536. get_number(input_format_t::cbor, subtype_to_ignore);
  8537. break;
  8538. }
  8539. case 0xDB:
  8540. {
  8541. std::uint64_t subtype_to_ignore{};
  8542. get_number(input_format_t::cbor, subtype_to_ignore);
  8543. break;
  8544. }
  8545. default:
  8546. break;
  8547. }
  8548. return parse_cbor_internal(true, tag_handler);
  8549. }
  8550. case cbor_tag_handler_t::store:
  8551. {
  8552. binary_t b;
  8553. // use binary subtype and store in binary container
  8554. switch (current)
  8555. {
  8556. case 0xD8:
  8557. {
  8558. std::uint8_t subtype{};
  8559. get_number(input_format_t::cbor, subtype);
  8560. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  8561. break;
  8562. }
  8563. case 0xD9:
  8564. {
  8565. std::uint16_t subtype{};
  8566. get_number(input_format_t::cbor, subtype);
  8567. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  8568. break;
  8569. }
  8570. case 0xDA:
  8571. {
  8572. std::uint32_t subtype{};
  8573. get_number(input_format_t::cbor, subtype);
  8574. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  8575. break;
  8576. }
  8577. case 0xDB:
  8578. {
  8579. std::uint64_t subtype{};
  8580. get_number(input_format_t::cbor, subtype);
  8581. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  8582. break;
  8583. }
  8584. default:
  8585. return parse_cbor_internal(true, tag_handler);
  8586. }
  8587. get();
  8588. return get_cbor_binary(b) && sax->binary(b);
  8589. }
  8590. default: // LCOV_EXCL_LINE
  8591. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  8592. return false; // LCOV_EXCL_LINE
  8593. }
  8594. }
  8595. case 0xF4: // false
  8596. return sax->boolean(false);
  8597. case 0xF5: // true
  8598. return sax->boolean(true);
  8599. case 0xF6: // null
  8600. return sax->null();
  8601. case 0xF9: // Half-Precision Float (two-byte IEEE 754)
  8602. {
  8603. const auto byte1_raw = get();
  8604. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number")))
  8605. {
  8606. return false;
  8607. }
  8608. const auto byte2_raw = get();
  8609. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number")))
  8610. {
  8611. return false;
  8612. }
  8613. const auto byte1 = static_cast<unsigned char>(byte1_raw);
  8614. const auto byte2 = static_cast<unsigned char>(byte2_raw);
  8615. // code from RFC 7049, Appendix D, Figure 3:
  8616. // As half-precision floating-point numbers were only added
  8617. // to IEEE 754 in 2008, today's programming platforms often
  8618. // still only have limited support for them. It is very
  8619. // easy to include at least decoding support for them even
  8620. // without such support. An example of a small decoder for
  8621. // half-precision floating-point numbers in the C language
  8622. // is shown in Fig. 3.
  8623. const auto half = static_cast<unsigned int>((byte1 << 8u) + byte2);
  8624. const double val = [&half]
  8625. {
  8626. const int exp = (half >> 10u) & 0x1Fu;
  8627. const unsigned int mant = half & 0x3FFu;
  8628. JSON_ASSERT(0 <= exp&& exp <= 32);
  8629. JSON_ASSERT(mant <= 1024);
  8630. switch (exp)
  8631. {
  8632. case 0:
  8633. return std::ldexp(mant, -24);
  8634. case 31:
  8635. return (mant == 0)
  8636. ? std::numeric_limits<double>::infinity()
  8637. : std::numeric_limits<double>::quiet_NaN();
  8638. default:
  8639. return std::ldexp(mant + 1024, exp - 25);
  8640. }
  8641. }();
  8642. return sax->number_float((half & 0x8000u) != 0
  8643. ? static_cast<number_float_t>(-val)
  8644. : static_cast<number_float_t>(val), "");
  8645. }
  8646. case 0xFA: // Single-Precision Float (four-byte IEEE 754)
  8647. {
  8648. float number{};
  8649. return get_number(input_format_t::cbor, number) && sax->number_float(static_cast<number_float_t>(number), "");
  8650. }
  8651. case 0xFB: // Double-Precision Float (eight-byte IEEE 754)
  8652. {
  8653. double number{};
  8654. return get_number(input_format_t::cbor, number) && sax->number_float(static_cast<number_float_t>(number), "");
  8655. }
  8656. default: // anything else (0xFF is handled inside the other types)
  8657. {
  8658. auto last_token = get_token_string();
  8659. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  8660. exception_message(input_format_t::cbor, concat("invalid byte: 0x", last_token), "value"), nullptr));
  8661. }
  8662. }
  8663. }
  8664. /*!
  8665. @brief reads a CBOR string
  8666. This function first reads starting bytes to determine the expected
  8667. string length and then copies this number of bytes into a string.
  8668. Additionally, CBOR's strings with indefinite lengths are supported.
  8669. @param[out] result created string
  8670. @return whether string creation completed
  8671. */
  8672. bool get_cbor_string(string_t& result)
  8673. {
  8674. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "string")))
  8675. {
  8676. return false;
  8677. }
  8678. switch (current)
  8679. {
  8680. // UTF-8 string (0x00..0x17 bytes follow)
  8681. case 0x60:
  8682. case 0x61:
  8683. case 0x62:
  8684. case 0x63:
  8685. case 0x64:
  8686. case 0x65:
  8687. case 0x66:
  8688. case 0x67:
  8689. case 0x68:
  8690. case 0x69:
  8691. case 0x6A:
  8692. case 0x6B:
  8693. case 0x6C:
  8694. case 0x6D:
  8695. case 0x6E:
  8696. case 0x6F:
  8697. case 0x70:
  8698. case 0x71:
  8699. case 0x72:
  8700. case 0x73:
  8701. case 0x74:
  8702. case 0x75:
  8703. case 0x76:
  8704. case 0x77:
  8705. {
  8706. return get_string(input_format_t::cbor, static_cast<unsigned int>(current) & 0x1Fu, result);
  8707. }
  8708. case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
  8709. {
  8710. std::uint8_t len{};
  8711. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  8712. }
  8713. case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
  8714. {
  8715. std::uint16_t len{};
  8716. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  8717. }
  8718. case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
  8719. {
  8720. std::uint32_t len{};
  8721. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  8722. }
  8723. case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
  8724. {
  8725. std::uint64_t len{};
  8726. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  8727. }
  8728. case 0x7F: // UTF-8 string (indefinite length)
  8729. {
  8730. while (get() != 0xFF)
  8731. {
  8732. string_t chunk;
  8733. if (!get_cbor_string(chunk))
  8734. {
  8735. return false;
  8736. }
  8737. result.append(chunk);
  8738. }
  8739. return true;
  8740. }
  8741. default:
  8742. {
  8743. auto last_token = get_token_string();
  8744. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
  8745. exception_message(input_format_t::cbor, concat("expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x", last_token), "string"), nullptr));
  8746. }
  8747. }
  8748. }
  8749. /*!
  8750. @brief reads a CBOR byte array
  8751. This function first reads starting bytes to determine the expected
  8752. byte array length and then copies this number of bytes into the byte array.
  8753. Additionally, CBOR's byte arrays with indefinite lengths are supported.
  8754. @param[out] result created byte array
  8755. @return whether byte array creation completed
  8756. */
  8757. bool get_cbor_binary(binary_t& result)
  8758. {
  8759. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "binary")))
  8760. {
  8761. return false;
  8762. }
  8763. switch (current)
  8764. {
  8765. // Binary data (0x00..0x17 bytes follow)
  8766. case 0x40:
  8767. case 0x41:
  8768. case 0x42:
  8769. case 0x43:
  8770. case 0x44:
  8771. case 0x45:
  8772. case 0x46:
  8773. case 0x47:
  8774. case 0x48:
  8775. case 0x49:
  8776. case 0x4A:
  8777. case 0x4B:
  8778. case 0x4C:
  8779. case 0x4D:
  8780. case 0x4E:
  8781. case 0x4F:
  8782. case 0x50:
  8783. case 0x51:
  8784. case 0x52:
  8785. case 0x53:
  8786. case 0x54:
  8787. case 0x55:
  8788. case 0x56:
  8789. case 0x57:
  8790. {
  8791. return get_binary(input_format_t::cbor, static_cast<unsigned int>(current) & 0x1Fu, result);
  8792. }
  8793. case 0x58: // Binary data (one-byte uint8_t for n follows)
  8794. {
  8795. std::uint8_t len{};
  8796. return get_number(input_format_t::cbor, len) &&
  8797. get_binary(input_format_t::cbor, len, result);
  8798. }
  8799. case 0x59: // Binary data (two-byte uint16_t for n follow)
  8800. {
  8801. std::uint16_t len{};
  8802. return get_number(input_format_t::cbor, len) &&
  8803. get_binary(input_format_t::cbor, len, result);
  8804. }
  8805. case 0x5A: // Binary data (four-byte uint32_t for n follow)
  8806. {
  8807. std::uint32_t len{};
  8808. return get_number(input_format_t::cbor, len) &&
  8809. get_binary(input_format_t::cbor, len, result);
  8810. }
  8811. case 0x5B: // Binary data (eight-byte uint64_t for n follow)
  8812. {
  8813. std::uint64_t len{};
  8814. return get_number(input_format_t::cbor, len) &&
  8815. get_binary(input_format_t::cbor, len, result);
  8816. }
  8817. case 0x5F: // Binary data (indefinite length)
  8818. {
  8819. while (get() != 0xFF)
  8820. {
  8821. binary_t chunk;
  8822. if (!get_cbor_binary(chunk))
  8823. {
  8824. return false;
  8825. }
  8826. result.insert(result.end(), chunk.begin(), chunk.end());
  8827. }
  8828. return true;
  8829. }
  8830. default:
  8831. {
  8832. auto last_token = get_token_string();
  8833. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
  8834. 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));
  8835. }
  8836. }
  8837. }
  8838. /*!
  8839. @param[in] len the length of the array or static_cast<std::size_t>(-1) for an
  8840. array of indefinite size
  8841. @param[in] tag_handler how CBOR tags should be treated
  8842. @return whether array creation completed
  8843. */
  8844. bool get_cbor_array(const std::size_t len,
  8845. const cbor_tag_handler_t tag_handler)
  8846. {
  8847. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
  8848. {
  8849. return false;
  8850. }
  8851. if (len != static_cast<std::size_t>(-1))
  8852. {
  8853. for (std::size_t i = 0; i < len; ++i)
  8854. {
  8855. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
  8856. {
  8857. return false;
  8858. }
  8859. }
  8860. }
  8861. else
  8862. {
  8863. while (get() != 0xFF)
  8864. {
  8865. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(false, tag_handler)))
  8866. {
  8867. return false;
  8868. }
  8869. }
  8870. }
  8871. return sax->end_array();
  8872. }
  8873. /*!
  8874. @param[in] len the length of the object or static_cast<std::size_t>(-1) for an
  8875. object of indefinite size
  8876. @param[in] tag_handler how CBOR tags should be treated
  8877. @return whether object creation completed
  8878. */
  8879. bool get_cbor_object(const std::size_t len,
  8880. const cbor_tag_handler_t tag_handler)
  8881. {
  8882. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
  8883. {
  8884. return false;
  8885. }
  8886. if (len != 0)
  8887. {
  8888. string_t key;
  8889. if (len != static_cast<std::size_t>(-1))
  8890. {
  8891. for (std::size_t i = 0; i < len; ++i)
  8892. {
  8893. get();
  8894. if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
  8895. {
  8896. return false;
  8897. }
  8898. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
  8899. {
  8900. return false;
  8901. }
  8902. key.clear();
  8903. }
  8904. }
  8905. else
  8906. {
  8907. while (get() != 0xFF)
  8908. {
  8909. if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
  8910. {
  8911. return false;
  8912. }
  8913. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
  8914. {
  8915. return false;
  8916. }
  8917. key.clear();
  8918. }
  8919. }
  8920. }
  8921. return sax->end_object();
  8922. }
  8923. /////////////
  8924. // MsgPack //
  8925. /////////////
  8926. /*!
  8927. @return whether a valid MessagePack value was passed to the SAX parser
  8928. */
  8929. bool parse_msgpack_internal()
  8930. {
  8931. switch (get())
  8932. {
  8933. // EOF
  8934. case std::char_traits<char_type>::eof():
  8935. return unexpect_eof(input_format_t::msgpack, "value");
  8936. // positive fixint
  8937. case 0x00:
  8938. case 0x01:
  8939. case 0x02:
  8940. case 0x03:
  8941. case 0x04:
  8942. case 0x05:
  8943. case 0x06:
  8944. case 0x07:
  8945. case 0x08:
  8946. case 0x09:
  8947. case 0x0A:
  8948. case 0x0B:
  8949. case 0x0C:
  8950. case 0x0D:
  8951. case 0x0E:
  8952. case 0x0F:
  8953. case 0x10:
  8954. case 0x11:
  8955. case 0x12:
  8956. case 0x13:
  8957. case 0x14:
  8958. case 0x15:
  8959. case 0x16:
  8960. case 0x17:
  8961. case 0x18:
  8962. case 0x19:
  8963. case 0x1A:
  8964. case 0x1B:
  8965. case 0x1C:
  8966. case 0x1D:
  8967. case 0x1E:
  8968. case 0x1F:
  8969. case 0x20:
  8970. case 0x21:
  8971. case 0x22:
  8972. case 0x23:
  8973. case 0x24:
  8974. case 0x25:
  8975. case 0x26:
  8976. case 0x27:
  8977. case 0x28:
  8978. case 0x29:
  8979. case 0x2A:
  8980. case 0x2B:
  8981. case 0x2C:
  8982. case 0x2D:
  8983. case 0x2E:
  8984. case 0x2F:
  8985. case 0x30:
  8986. case 0x31:
  8987. case 0x32:
  8988. case 0x33:
  8989. case 0x34:
  8990. case 0x35:
  8991. case 0x36:
  8992. case 0x37:
  8993. case 0x38:
  8994. case 0x39:
  8995. case 0x3A:
  8996. case 0x3B:
  8997. case 0x3C:
  8998. case 0x3D:
  8999. case 0x3E:
  9000. case 0x3F:
  9001. case 0x40:
  9002. case 0x41:
  9003. case 0x42:
  9004. case 0x43:
  9005. case 0x44:
  9006. case 0x45:
  9007. case 0x46:
  9008. case 0x47:
  9009. case 0x48:
  9010. case 0x49:
  9011. case 0x4A:
  9012. case 0x4B:
  9013. case 0x4C:
  9014. case 0x4D:
  9015. case 0x4E:
  9016. case 0x4F:
  9017. case 0x50:
  9018. case 0x51:
  9019. case 0x52:
  9020. case 0x53:
  9021. case 0x54:
  9022. case 0x55:
  9023. case 0x56:
  9024. case 0x57:
  9025. case 0x58:
  9026. case 0x59:
  9027. case 0x5A:
  9028. case 0x5B:
  9029. case 0x5C:
  9030. case 0x5D:
  9031. case 0x5E:
  9032. case 0x5F:
  9033. case 0x60:
  9034. case 0x61:
  9035. case 0x62:
  9036. case 0x63:
  9037. case 0x64:
  9038. case 0x65:
  9039. case 0x66:
  9040. case 0x67:
  9041. case 0x68:
  9042. case 0x69:
  9043. case 0x6A:
  9044. case 0x6B:
  9045. case 0x6C:
  9046. case 0x6D:
  9047. case 0x6E:
  9048. case 0x6F:
  9049. case 0x70:
  9050. case 0x71:
  9051. case 0x72:
  9052. case 0x73:
  9053. case 0x74:
  9054. case 0x75:
  9055. case 0x76:
  9056. case 0x77:
  9057. case 0x78:
  9058. case 0x79:
  9059. case 0x7A:
  9060. case 0x7B:
  9061. case 0x7C:
  9062. case 0x7D:
  9063. case 0x7E:
  9064. case 0x7F:
  9065. return sax->number_unsigned(static_cast<number_unsigned_t>(current));
  9066. // fixmap
  9067. case 0x80:
  9068. case 0x81:
  9069. case 0x82:
  9070. case 0x83:
  9071. case 0x84:
  9072. case 0x85:
  9073. case 0x86:
  9074. case 0x87:
  9075. case 0x88:
  9076. case 0x89:
  9077. case 0x8A:
  9078. case 0x8B:
  9079. case 0x8C:
  9080. case 0x8D:
  9081. case 0x8E:
  9082. case 0x8F:
  9083. return get_msgpack_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
  9084. // fixarray
  9085. case 0x90:
  9086. case 0x91:
  9087. case 0x92:
  9088. case 0x93:
  9089. case 0x94:
  9090. case 0x95:
  9091. case 0x96:
  9092. case 0x97:
  9093. case 0x98:
  9094. case 0x99:
  9095. case 0x9A:
  9096. case 0x9B:
  9097. case 0x9C:
  9098. case 0x9D:
  9099. case 0x9E:
  9100. case 0x9F:
  9101. return get_msgpack_array(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
  9102. // fixstr
  9103. case 0xA0:
  9104. case 0xA1:
  9105. case 0xA2:
  9106. case 0xA3:
  9107. case 0xA4:
  9108. case 0xA5:
  9109. case 0xA6:
  9110. case 0xA7:
  9111. case 0xA8:
  9112. case 0xA9:
  9113. case 0xAA:
  9114. case 0xAB:
  9115. case 0xAC:
  9116. case 0xAD:
  9117. case 0xAE:
  9118. case 0xAF:
  9119. case 0xB0:
  9120. case 0xB1:
  9121. case 0xB2:
  9122. case 0xB3:
  9123. case 0xB4:
  9124. case 0xB5:
  9125. case 0xB6:
  9126. case 0xB7:
  9127. case 0xB8:
  9128. case 0xB9:
  9129. case 0xBA:
  9130. case 0xBB:
  9131. case 0xBC:
  9132. case 0xBD:
  9133. case 0xBE:
  9134. case 0xBF:
  9135. case 0xD9: // str 8
  9136. case 0xDA: // str 16
  9137. case 0xDB: // str 32
  9138. {
  9139. string_t s;
  9140. return get_msgpack_string(s) && sax->string(s);
  9141. }
  9142. case 0xC0: // nil
  9143. return sax->null();
  9144. case 0xC2: // false
  9145. return sax->boolean(false);
  9146. case 0xC3: // true
  9147. return sax->boolean(true);
  9148. case 0xC4: // bin 8
  9149. case 0xC5: // bin 16
  9150. case 0xC6: // bin 32
  9151. case 0xC7: // ext 8
  9152. case 0xC8: // ext 16
  9153. case 0xC9: // ext 32
  9154. case 0xD4: // fixext 1
  9155. case 0xD5: // fixext 2
  9156. case 0xD6: // fixext 4
  9157. case 0xD7: // fixext 8
  9158. case 0xD8: // fixext 16
  9159. {
  9160. binary_t b;
  9161. return get_msgpack_binary(b) && sax->binary(b);
  9162. }
  9163. case 0xCA: // float 32
  9164. {
  9165. float number{};
  9166. return get_number(input_format_t::msgpack, number) && sax->number_float(static_cast<number_float_t>(number), "");
  9167. }
  9168. case 0xCB: // float 64
  9169. {
  9170. double number{};
  9171. return get_number(input_format_t::msgpack, number) && sax->number_float(static_cast<number_float_t>(number), "");
  9172. }
  9173. case 0xCC: // uint 8
  9174. {
  9175. std::uint8_t number{};
  9176. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  9177. }
  9178. case 0xCD: // uint 16
  9179. {
  9180. std::uint16_t number{};
  9181. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  9182. }
  9183. case 0xCE: // uint 32
  9184. {
  9185. std::uint32_t number{};
  9186. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  9187. }
  9188. case 0xCF: // uint 64
  9189. {
  9190. std::uint64_t number{};
  9191. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  9192. }
  9193. case 0xD0: // int 8
  9194. {
  9195. std::int8_t number{};
  9196. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  9197. }
  9198. case 0xD1: // int 16
  9199. {
  9200. std::int16_t number{};
  9201. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  9202. }
  9203. case 0xD2: // int 32
  9204. {
  9205. std::int32_t number{};
  9206. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  9207. }
  9208. case 0xD3: // int 64
  9209. {
  9210. std::int64_t number{};
  9211. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  9212. }
  9213. case 0xDC: // array 16
  9214. {
  9215. std::uint16_t len{};
  9216. return get_number(input_format_t::msgpack, len) && get_msgpack_array(static_cast<std::size_t>(len));
  9217. }
  9218. case 0xDD: // array 32
  9219. {
  9220. std::uint32_t len{};
  9221. return get_number(input_format_t::msgpack, len) && get_msgpack_array(conditional_static_cast<std::size_t>(len));
  9222. }
  9223. case 0xDE: // map 16
  9224. {
  9225. std::uint16_t len{};
  9226. return get_number(input_format_t::msgpack, len) && get_msgpack_object(static_cast<std::size_t>(len));
  9227. }
  9228. case 0xDF: // map 32
  9229. {
  9230. std::uint32_t len{};
  9231. return get_number(input_format_t::msgpack, len) && get_msgpack_object(conditional_static_cast<std::size_t>(len));
  9232. }
  9233. // negative fixint
  9234. case 0xE0:
  9235. case 0xE1:
  9236. case 0xE2:
  9237. case 0xE3:
  9238. case 0xE4:
  9239. case 0xE5:
  9240. case 0xE6:
  9241. case 0xE7:
  9242. case 0xE8:
  9243. case 0xE9:
  9244. case 0xEA:
  9245. case 0xEB:
  9246. case 0xEC:
  9247. case 0xED:
  9248. case 0xEE:
  9249. case 0xEF:
  9250. case 0xF0:
  9251. case 0xF1:
  9252. case 0xF2:
  9253. case 0xF3:
  9254. case 0xF4:
  9255. case 0xF5:
  9256. case 0xF6:
  9257. case 0xF7:
  9258. case 0xF8:
  9259. case 0xF9:
  9260. case 0xFA:
  9261. case 0xFB:
  9262. case 0xFC:
  9263. case 0xFD:
  9264. case 0xFE:
  9265. case 0xFF:
  9266. return sax->number_integer(static_cast<std::int8_t>(current));
  9267. default: // anything else
  9268. {
  9269. auto last_token = get_token_string();
  9270. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  9271. exception_message(input_format_t::msgpack, concat("invalid byte: 0x", last_token), "value"), nullptr));
  9272. }
  9273. }
  9274. }
  9275. /*!
  9276. @brief reads a MessagePack string
  9277. This function first reads starting bytes to determine the expected
  9278. string length and then copies this number of bytes into a string.
  9279. @param[out] result created string
  9280. @return whether string creation completed
  9281. */
  9282. bool get_msgpack_string(string_t& result)
  9283. {
  9284. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::msgpack, "string")))
  9285. {
  9286. return false;
  9287. }
  9288. switch (current)
  9289. {
  9290. // fixstr
  9291. case 0xA0:
  9292. case 0xA1:
  9293. case 0xA2:
  9294. case 0xA3:
  9295. case 0xA4:
  9296. case 0xA5:
  9297. case 0xA6:
  9298. case 0xA7:
  9299. case 0xA8:
  9300. case 0xA9:
  9301. case 0xAA:
  9302. case 0xAB:
  9303. case 0xAC:
  9304. case 0xAD:
  9305. case 0xAE:
  9306. case 0xAF:
  9307. case 0xB0:
  9308. case 0xB1:
  9309. case 0xB2:
  9310. case 0xB3:
  9311. case 0xB4:
  9312. case 0xB5:
  9313. case 0xB6:
  9314. case 0xB7:
  9315. case 0xB8:
  9316. case 0xB9:
  9317. case 0xBA:
  9318. case 0xBB:
  9319. case 0xBC:
  9320. case 0xBD:
  9321. case 0xBE:
  9322. case 0xBF:
  9323. {
  9324. return get_string(input_format_t::msgpack, static_cast<unsigned int>(current) & 0x1Fu, result);
  9325. }
  9326. case 0xD9: // str 8
  9327. {
  9328. std::uint8_t len{};
  9329. return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
  9330. }
  9331. case 0xDA: // str 16
  9332. {
  9333. std::uint16_t len{};
  9334. return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
  9335. }
  9336. case 0xDB: // str 32
  9337. {
  9338. std::uint32_t len{};
  9339. return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
  9340. }
  9341. default:
  9342. {
  9343. auto last_token = get_token_string();
  9344. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
  9345. exception_message(input_format_t::msgpack, concat("expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x", last_token), "string"), nullptr));
  9346. }
  9347. }
  9348. }
  9349. /*!
  9350. @brief reads a MessagePack byte array
  9351. This function first reads starting bytes to determine the expected
  9352. byte array length and then copies this number of bytes into a byte array.
  9353. @param[out] result created byte array
  9354. @return whether byte array creation completed
  9355. */
  9356. bool get_msgpack_binary(binary_t& result)
  9357. {
  9358. // helper function to set the subtype
  9359. auto assign_and_return_true = [&result](std::int8_t subtype)
  9360. {
  9361. result.set_subtype(static_cast<std::uint8_t>(subtype));
  9362. return true;
  9363. };
  9364. switch (current)
  9365. {
  9366. case 0xC4: // bin 8
  9367. {
  9368. std::uint8_t len{};
  9369. return get_number(input_format_t::msgpack, len) &&
  9370. get_binary(input_format_t::msgpack, len, result);
  9371. }
  9372. case 0xC5: // bin 16
  9373. {
  9374. std::uint16_t len{};
  9375. return get_number(input_format_t::msgpack, len) &&
  9376. get_binary(input_format_t::msgpack, len, result);
  9377. }
  9378. case 0xC6: // bin 32
  9379. {
  9380. std::uint32_t len{};
  9381. return get_number(input_format_t::msgpack, len) &&
  9382. get_binary(input_format_t::msgpack, len, result);
  9383. }
  9384. case 0xC7: // ext 8
  9385. {
  9386. std::uint8_t len{};
  9387. std::int8_t subtype{};
  9388. return get_number(input_format_t::msgpack, len) &&
  9389. get_number(input_format_t::msgpack, subtype) &&
  9390. get_binary(input_format_t::msgpack, len, result) &&
  9391. assign_and_return_true(subtype);
  9392. }
  9393. case 0xC8: // ext 16
  9394. {
  9395. std::uint16_t len{};
  9396. std::int8_t subtype{};
  9397. return get_number(input_format_t::msgpack, len) &&
  9398. get_number(input_format_t::msgpack, subtype) &&
  9399. get_binary(input_format_t::msgpack, len, result) &&
  9400. assign_and_return_true(subtype);
  9401. }
  9402. case 0xC9: // ext 32
  9403. {
  9404. std::uint32_t len{};
  9405. std::int8_t subtype{};
  9406. return get_number(input_format_t::msgpack, len) &&
  9407. get_number(input_format_t::msgpack, subtype) &&
  9408. get_binary(input_format_t::msgpack, len, result) &&
  9409. assign_and_return_true(subtype);
  9410. }
  9411. case 0xD4: // fixext 1
  9412. {
  9413. std::int8_t subtype{};
  9414. return get_number(input_format_t::msgpack, subtype) &&
  9415. get_binary(input_format_t::msgpack, 1, result) &&
  9416. assign_and_return_true(subtype);
  9417. }
  9418. case 0xD5: // fixext 2
  9419. {
  9420. std::int8_t subtype{};
  9421. return get_number(input_format_t::msgpack, subtype) &&
  9422. get_binary(input_format_t::msgpack, 2, result) &&
  9423. assign_and_return_true(subtype);
  9424. }
  9425. case 0xD6: // fixext 4
  9426. {
  9427. std::int8_t subtype{};
  9428. return get_number(input_format_t::msgpack, subtype) &&
  9429. get_binary(input_format_t::msgpack, 4, result) &&
  9430. assign_and_return_true(subtype);
  9431. }
  9432. case 0xD7: // fixext 8
  9433. {
  9434. std::int8_t subtype{};
  9435. return get_number(input_format_t::msgpack, subtype) &&
  9436. get_binary(input_format_t::msgpack, 8, result) &&
  9437. assign_and_return_true(subtype);
  9438. }
  9439. case 0xD8: // fixext 16
  9440. {
  9441. std::int8_t subtype{};
  9442. return get_number(input_format_t::msgpack, subtype) &&
  9443. get_binary(input_format_t::msgpack, 16, result) &&
  9444. assign_and_return_true(subtype);
  9445. }
  9446. default: // LCOV_EXCL_LINE
  9447. return false; // LCOV_EXCL_LINE
  9448. }
  9449. }
  9450. /*!
  9451. @param[in] len the length of the array
  9452. @return whether array creation completed
  9453. */
  9454. bool get_msgpack_array(const std::size_t len)
  9455. {
  9456. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
  9457. {
  9458. return false;
  9459. }
  9460. for (std::size_t i = 0; i < len; ++i)
  9461. {
  9462. if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
  9463. {
  9464. return false;
  9465. }
  9466. }
  9467. return sax->end_array();
  9468. }
  9469. /*!
  9470. @param[in] len the length of the object
  9471. @return whether object creation completed
  9472. */
  9473. bool get_msgpack_object(const std::size_t len)
  9474. {
  9475. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
  9476. {
  9477. return false;
  9478. }
  9479. string_t key;
  9480. for (std::size_t i = 0; i < len; ++i)
  9481. {
  9482. get();
  9483. if (JSON_HEDLEY_UNLIKELY(!get_msgpack_string(key) || !sax->key(key)))
  9484. {
  9485. return false;
  9486. }
  9487. if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
  9488. {
  9489. return false;
  9490. }
  9491. key.clear();
  9492. }
  9493. return sax->end_object();
  9494. }
  9495. ////////////
  9496. // UBJSON //
  9497. ////////////
  9498. /*!
  9499. @param[in] get_char whether a new character should be retrieved from the
  9500. input (true, default) or whether the last read
  9501. character should be considered instead
  9502. @return whether a valid UBJSON value was passed to the SAX parser
  9503. */
  9504. bool parse_ubjson_internal(const bool get_char = true)
  9505. {
  9506. return get_ubjson_value(get_char ? get_ignore_noop() : current);
  9507. }
  9508. /*!
  9509. @brief reads a UBJSON string
  9510. This function is either called after reading the 'S' byte explicitly
  9511. indicating a string, or in case of an object key where the 'S' byte can be
  9512. left out.
  9513. @param[out] result created string
  9514. @param[in] get_char whether a new character should be retrieved from the
  9515. input (true, default) or whether the last read
  9516. character should be considered instead
  9517. @return whether string creation completed
  9518. */
  9519. bool get_ubjson_string(string_t& result, const bool get_char = true)
  9520. {
  9521. if (get_char)
  9522. {
  9523. get(); // TODO(niels): may we ignore N here?
  9524. }
  9525. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "value")))
  9526. {
  9527. return false;
  9528. }
  9529. switch (current)
  9530. {
  9531. case 'U':
  9532. {
  9533. std::uint8_t len{};
  9534. return get_number(input_format, len) && get_string(input_format, len, result);
  9535. }
  9536. case 'i':
  9537. {
  9538. std::int8_t len{};
  9539. return get_number(input_format, len) && get_string(input_format, len, result);
  9540. }
  9541. case 'I':
  9542. {
  9543. std::int16_t len{};
  9544. return get_number(input_format, len) && get_string(input_format, len, result);
  9545. }
  9546. case 'l':
  9547. {
  9548. std::int32_t len{};
  9549. return get_number(input_format, len) && get_string(input_format, len, result);
  9550. }
  9551. case 'L':
  9552. {
  9553. std::int64_t len{};
  9554. return get_number(input_format, len) && get_string(input_format, len, result);
  9555. }
  9556. case 'u':
  9557. {
  9558. if (input_format != input_format_t::bjdata)
  9559. {
  9560. break;
  9561. }
  9562. std::uint16_t len{};
  9563. return get_number(input_format, len) && get_string(input_format, len, result);
  9564. }
  9565. case 'm':
  9566. {
  9567. if (input_format != input_format_t::bjdata)
  9568. {
  9569. break;
  9570. }
  9571. std::uint32_t len{};
  9572. return get_number(input_format, len) && get_string(input_format, len, result);
  9573. }
  9574. case 'M':
  9575. {
  9576. if (input_format != input_format_t::bjdata)
  9577. {
  9578. break;
  9579. }
  9580. std::uint64_t len{};
  9581. return get_number(input_format, len) && get_string(input_format, len, result);
  9582. }
  9583. default:
  9584. break;
  9585. }
  9586. auto last_token = get_token_string();
  9587. std::string message;
  9588. if (input_format != input_format_t::bjdata)
  9589. {
  9590. message = "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token;
  9591. }
  9592. else
  9593. {
  9594. message = "expected length type specification (U, i, u, I, m, l, M, L); last byte: 0x" + last_token;
  9595. }
  9596. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "string"), nullptr));
  9597. }
  9598. /*!
  9599. @param[out] dim an integer vector storing the ND array dimensions
  9600. @return whether reading ND array size vector is successful
  9601. */
  9602. bool get_ubjson_ndarray_size(std::vector<size_t>& dim)
  9603. {
  9604. std::pair<std::size_t, char_int_type> size_and_type;
  9605. size_t dimlen = 0;
  9606. bool no_ndarray = true;
  9607. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type, no_ndarray)))
  9608. {
  9609. return false;
  9610. }
  9611. if (size_and_type.first != npos)
  9612. {
  9613. if (size_and_type.second != 0)
  9614. {
  9615. if (size_and_type.second != 'N')
  9616. {
  9617. for (std::size_t i = 0; i < size_and_type.first; ++i)
  9618. {
  9619. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray, size_and_type.second)))
  9620. {
  9621. return false;
  9622. }
  9623. dim.push_back(dimlen);
  9624. }
  9625. }
  9626. }
  9627. else
  9628. {
  9629. for (std::size_t i = 0; i < size_and_type.first; ++i)
  9630. {
  9631. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray)))
  9632. {
  9633. return false;
  9634. }
  9635. dim.push_back(dimlen);
  9636. }
  9637. }
  9638. }
  9639. else
  9640. {
  9641. while (current != ']')
  9642. {
  9643. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray, current)))
  9644. {
  9645. return false;
  9646. }
  9647. dim.push_back(dimlen);
  9648. get_ignore_noop();
  9649. }
  9650. }
  9651. return true;
  9652. }
  9653. /*!
  9654. @param[out] result determined size
  9655. @param[in,out] is_ndarray for input, `true` means already inside an ndarray vector
  9656. or ndarray dimension is not allowed; `false` means ndarray
  9657. is allowed; for output, `true` means an ndarray is found;
  9658. is_ndarray can only return `true` when its initial value
  9659. is `false`
  9660. @param[in] prefix type marker if already read, otherwise set to 0
  9661. @return whether size determination completed
  9662. */
  9663. bool get_ubjson_size_value(std::size_t& result, bool& is_ndarray, char_int_type prefix = 0)
  9664. {
  9665. if (prefix == 0)
  9666. {
  9667. prefix = get_ignore_noop();
  9668. }
  9669. switch (prefix)
  9670. {
  9671. case 'U':
  9672. {
  9673. std::uint8_t number{};
  9674. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9675. {
  9676. return false;
  9677. }
  9678. result = static_cast<std::size_t>(number);
  9679. return true;
  9680. }
  9681. case 'i':
  9682. {
  9683. std::int8_t number{};
  9684. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9685. {
  9686. return false;
  9687. }
  9688. if (number < 0)
  9689. {
  9690. return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
  9691. exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
  9692. }
  9693. result = static_cast<std::size_t>(number); // NOLINT(bugprone-signed-char-misuse,cert-str34-c): number is not a char
  9694. return true;
  9695. }
  9696. case 'I':
  9697. {
  9698. std::int16_t number{};
  9699. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9700. {
  9701. return false;
  9702. }
  9703. if (number < 0)
  9704. {
  9705. return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
  9706. exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
  9707. }
  9708. result = static_cast<std::size_t>(number);
  9709. return true;
  9710. }
  9711. case 'l':
  9712. {
  9713. std::int32_t number{};
  9714. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9715. {
  9716. return false;
  9717. }
  9718. if (number < 0)
  9719. {
  9720. return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
  9721. exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
  9722. }
  9723. result = static_cast<std::size_t>(number);
  9724. return true;
  9725. }
  9726. case 'L':
  9727. {
  9728. std::int64_t number{};
  9729. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9730. {
  9731. return false;
  9732. }
  9733. if (number < 0)
  9734. {
  9735. return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
  9736. exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
  9737. }
  9738. if (!value_in_range_of<std::size_t>(number))
  9739. {
  9740. return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408,
  9741. exception_message(input_format, "integer value overflow", "size"), nullptr));
  9742. }
  9743. result = static_cast<std::size_t>(number);
  9744. return true;
  9745. }
  9746. case 'u':
  9747. {
  9748. if (input_format != input_format_t::bjdata)
  9749. {
  9750. break;
  9751. }
  9752. std::uint16_t number{};
  9753. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9754. {
  9755. return false;
  9756. }
  9757. result = static_cast<std::size_t>(number);
  9758. return true;
  9759. }
  9760. case 'm':
  9761. {
  9762. if (input_format != input_format_t::bjdata)
  9763. {
  9764. break;
  9765. }
  9766. std::uint32_t number{};
  9767. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9768. {
  9769. return false;
  9770. }
  9771. result = conditional_static_cast<std::size_t>(number);
  9772. return true;
  9773. }
  9774. case 'M':
  9775. {
  9776. if (input_format != input_format_t::bjdata)
  9777. {
  9778. break;
  9779. }
  9780. std::uint64_t number{};
  9781. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9782. {
  9783. return false;
  9784. }
  9785. if (!value_in_range_of<std::size_t>(number))
  9786. {
  9787. return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408,
  9788. exception_message(input_format, "integer value overflow", "size"), nullptr));
  9789. }
  9790. result = detail::conditional_static_cast<std::size_t>(number);
  9791. return true;
  9792. }
  9793. case '[':
  9794. {
  9795. if (input_format != input_format_t::bjdata)
  9796. {
  9797. break;
  9798. }
  9799. if (is_ndarray) // ndarray dimensional vector can only contain integers, and can not embed another array
  9800. {
  9801. 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));
  9802. }
  9803. std::vector<size_t> dim;
  9804. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_ndarray_size(dim)))
  9805. {
  9806. return false;
  9807. }
  9808. if (dim.size() == 1 || (dim.size() == 2 && dim.at(0) == 1)) // return normal array size if 1D row vector
  9809. {
  9810. result = dim.at(dim.size() - 1);
  9811. return true;
  9812. }
  9813. if (!dim.empty()) // if ndarray, convert to an object in JData annotated array format
  9814. {
  9815. for (auto i : dim) // test if any dimension in an ndarray is 0, if so, return a 1D empty container
  9816. {
  9817. if ( i == 0 )
  9818. {
  9819. result = 0;
  9820. return true;
  9821. }
  9822. }
  9823. string_t key = "_ArraySize_";
  9824. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(3) || !sax->key(key) || !sax->start_array(dim.size())))
  9825. {
  9826. return false;
  9827. }
  9828. result = 1;
  9829. for (auto i : dim)
  9830. {
  9831. result *= i;
  9832. 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()
  9833. {
  9834. 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));
  9835. }
  9836. if (JSON_HEDLEY_UNLIKELY(!sax->number_unsigned(static_cast<number_unsigned_t>(i))))
  9837. {
  9838. return false;
  9839. }
  9840. }
  9841. is_ndarray = true;
  9842. return sax->end_array();
  9843. }
  9844. result = 0;
  9845. return true;
  9846. }
  9847. default:
  9848. break;
  9849. }
  9850. auto last_token = get_token_string();
  9851. std::string message;
  9852. if (input_format != input_format_t::bjdata)
  9853. {
  9854. message = "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token;
  9855. }
  9856. else
  9857. {
  9858. message = "expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x" + last_token;
  9859. }
  9860. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "size"), nullptr));
  9861. }
  9862. /*!
  9863. @brief determine the type and size for a container
  9864. In the optimized UBJSON format, a type and a size can be provided to allow
  9865. for a more compact representation.
  9866. @param[out] result pair of the size and the type
  9867. @param[in] inside_ndarray whether the parser is parsing an ND array dimensional vector
  9868. @return whether pair creation completed
  9869. */
  9870. bool get_ubjson_size_type(std::pair<std::size_t, char_int_type>& result, bool inside_ndarray = false)
  9871. {
  9872. result.first = npos; // size
  9873. result.second = 0; // type
  9874. bool is_ndarray = false;
  9875. get_ignore_noop();
  9876. if (current == '$')
  9877. {
  9878. result.second = get(); // must not ignore 'N', because 'N' maybe the type
  9879. if (input_format == input_format_t::bjdata
  9880. && JSON_HEDLEY_UNLIKELY(std::binary_search(bjd_optimized_type_markers.begin(), bjd_optimized_type_markers.end(), result.second)))
  9881. {
  9882. auto last_token = get_token_string();
  9883. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  9884. exception_message(input_format, concat("marker 0x", last_token, " is not a permitted optimized array type"), "type"), nullptr));
  9885. }
  9886. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "type")))
  9887. {
  9888. return false;
  9889. }
  9890. get_ignore_noop();
  9891. if (JSON_HEDLEY_UNLIKELY(current != '#'))
  9892. {
  9893. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "value")))
  9894. {
  9895. return false;
  9896. }
  9897. auto last_token = get_token_string();
  9898. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  9899. exception_message(input_format, concat("expected '#' after type information; last byte: 0x", last_token), "size"), nullptr));
  9900. }
  9901. const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
  9902. if (input_format == input_format_t::bjdata && is_ndarray)
  9903. {
  9904. if (inside_ndarray)
  9905. {
  9906. return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
  9907. exception_message(input_format, "ndarray can not be recursive", "size"), nullptr));
  9908. }
  9909. result.second |= (1 << 8); // use bit 8 to indicate ndarray, all UBJSON and BJData markers should be ASCII letters
  9910. }
  9911. return is_error;
  9912. }
  9913. if (current == '#')
  9914. {
  9915. const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
  9916. if (input_format == input_format_t::bjdata && is_ndarray)
  9917. {
  9918. return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
  9919. exception_message(input_format, "ndarray requires both type and size", "size"), nullptr));
  9920. }
  9921. return is_error;
  9922. }
  9923. return true;
  9924. }
  9925. /*!
  9926. @param prefix the previously read or set type prefix
  9927. @return whether value creation completed
  9928. */
  9929. bool get_ubjson_value(const char_int_type prefix)
  9930. {
  9931. switch (prefix)
  9932. {
  9933. case std::char_traits<char_type>::eof(): // EOF
  9934. return unexpect_eof(input_format, "value");
  9935. case 'T': // true
  9936. return sax->boolean(true);
  9937. case 'F': // false
  9938. return sax->boolean(false);
  9939. case 'Z': // null
  9940. return sax->null();
  9941. case 'U':
  9942. {
  9943. std::uint8_t number{};
  9944. return get_number(input_format, number) && sax->number_unsigned(number);
  9945. }
  9946. case 'i':
  9947. {
  9948. std::int8_t number{};
  9949. return get_number(input_format, number) && sax->number_integer(number);
  9950. }
  9951. case 'I':
  9952. {
  9953. std::int16_t number{};
  9954. return get_number(input_format, number) && sax->number_integer(number);
  9955. }
  9956. case 'l':
  9957. {
  9958. std::int32_t number{};
  9959. return get_number(input_format, number) && sax->number_integer(number);
  9960. }
  9961. case 'L':
  9962. {
  9963. std::int64_t number{};
  9964. return get_number(input_format, number) && sax->number_integer(number);
  9965. }
  9966. case 'u':
  9967. {
  9968. if (input_format != input_format_t::bjdata)
  9969. {
  9970. break;
  9971. }
  9972. std::uint16_t number{};
  9973. return get_number(input_format, number) && sax->number_unsigned(number);
  9974. }
  9975. case 'm':
  9976. {
  9977. if (input_format != input_format_t::bjdata)
  9978. {
  9979. break;
  9980. }
  9981. std::uint32_t number{};
  9982. return get_number(input_format, number) && sax->number_unsigned(number);
  9983. }
  9984. case 'M':
  9985. {
  9986. if (input_format != input_format_t::bjdata)
  9987. {
  9988. break;
  9989. }
  9990. std::uint64_t number{};
  9991. return get_number(input_format, number) && sax->number_unsigned(number);
  9992. }
  9993. case 'h':
  9994. {
  9995. if (input_format != input_format_t::bjdata)
  9996. {
  9997. break;
  9998. }
  9999. const auto byte1_raw = get();
  10000. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
  10001. {
  10002. return false;
  10003. }
  10004. const auto byte2_raw = get();
  10005. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
  10006. {
  10007. return false;
  10008. }
  10009. const auto byte1 = static_cast<unsigned char>(byte1_raw);
  10010. const auto byte2 = static_cast<unsigned char>(byte2_raw);
  10011. // code from RFC 7049, Appendix D, Figure 3:
  10012. // As half-precision floating-point numbers were only added
  10013. // to IEEE 754 in 2008, today's programming platforms often
  10014. // still only have limited support for them. It is very
  10015. // easy to include at least decoding support for them even
  10016. // without such support. An example of a small decoder for
  10017. // half-precision floating-point numbers in the C language
  10018. // is shown in Fig. 3.
  10019. const auto half = static_cast<unsigned int>((byte2 << 8u) + byte1);
  10020. const double val = [&half]
  10021. {
  10022. const int exp = (half >> 10u) & 0x1Fu;
  10023. const unsigned int mant = half & 0x3FFu;
  10024. JSON_ASSERT(0 <= exp&& exp <= 32);
  10025. JSON_ASSERT(mant <= 1024);
  10026. switch (exp)
  10027. {
  10028. case 0:
  10029. return std::ldexp(mant, -24);
  10030. case 31:
  10031. return (mant == 0)
  10032. ? std::numeric_limits<double>::infinity()
  10033. : std::numeric_limits<double>::quiet_NaN();
  10034. default:
  10035. return std::ldexp(mant + 1024, exp - 25);
  10036. }
  10037. }();
  10038. return sax->number_float((half & 0x8000u) != 0
  10039. ? static_cast<number_float_t>(-val)
  10040. : static_cast<number_float_t>(val), "");
  10041. }
  10042. case 'd':
  10043. {
  10044. float number{};
  10045. return get_number(input_format, number) && sax->number_float(static_cast<number_float_t>(number), "");
  10046. }
  10047. case 'D':
  10048. {
  10049. double number{};
  10050. return get_number(input_format, number) && sax->number_float(static_cast<number_float_t>(number), "");
  10051. }
  10052. case 'H':
  10053. {
  10054. return get_ubjson_high_precision_number();
  10055. }
  10056. case 'C': // char
  10057. {
  10058. get();
  10059. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "char")))
  10060. {
  10061. return false;
  10062. }
  10063. if (JSON_HEDLEY_UNLIKELY(current > 127))
  10064. {
  10065. auto last_token = get_token_string();
  10066. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
  10067. exception_message(input_format, concat("byte after 'C' must be in range 0x00..0x7F; last byte: 0x", last_token), "char"), nullptr));
  10068. }
  10069. string_t s(1, static_cast<typename string_t::value_type>(current));
  10070. return sax->string(s);
  10071. }
  10072. case 'S': // string
  10073. {
  10074. string_t s;
  10075. return get_ubjson_string(s) && sax->string(s);
  10076. }
  10077. case '[': // array
  10078. return get_ubjson_array();
  10079. case '{': // object
  10080. return get_ubjson_object();
  10081. default: // anything else
  10082. break;
  10083. }
  10084. auto last_token = get_token_string();
  10085. 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));
  10086. }
  10087. /*!
  10088. @return whether array creation completed
  10089. */
  10090. bool get_ubjson_array()
  10091. {
  10092. std::pair<std::size_t, char_int_type> size_and_type;
  10093. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type)))
  10094. {
  10095. return false;
  10096. }
  10097. // 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):
  10098. // {"_ArrayType_" : "typeid", "_ArraySize_" : [n1, n2, ...], "_ArrayData_" : [v1, v2, ...]}
  10099. if (input_format == input_format_t::bjdata && size_and_type.first != npos && (size_and_type.second & (1 << 8)) != 0)
  10100. {
  10101. 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
  10102. 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)
  10103. {
  10104. return p.first < t;
  10105. });
  10106. string_t key = "_ArrayType_";
  10107. if (JSON_HEDLEY_UNLIKELY(it == bjd_types_map.end() || it->first != size_and_type.second))
  10108. {
  10109. auto last_token = get_token_string();
  10110. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  10111. exception_message(input_format, "invalid byte: 0x" + last_token, "type"), nullptr));
  10112. }
  10113. string_t type = it->second; // sax->string() takes a reference
  10114. if (JSON_HEDLEY_UNLIKELY(!sax->key(key) || !sax->string(type)))
  10115. {
  10116. return false;
  10117. }
  10118. if (size_and_type.second == 'C')
  10119. {
  10120. size_and_type.second = 'U';
  10121. }
  10122. key = "_ArrayData_";
  10123. if (JSON_HEDLEY_UNLIKELY(!sax->key(key) || !sax->start_array(size_and_type.first) ))
  10124. {
  10125. return false;
  10126. }
  10127. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10128. {
  10129. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
  10130. {
  10131. return false;
  10132. }
  10133. }
  10134. return (sax->end_array() && sax->end_object());
  10135. }
  10136. if (size_and_type.first != npos)
  10137. {
  10138. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(size_and_type.first)))
  10139. {
  10140. return false;
  10141. }
  10142. if (size_and_type.second != 0)
  10143. {
  10144. if (size_and_type.second != 'N')
  10145. {
  10146. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10147. {
  10148. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
  10149. {
  10150. return false;
  10151. }
  10152. }
  10153. }
  10154. }
  10155. else
  10156. {
  10157. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10158. {
  10159. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
  10160. {
  10161. return false;
  10162. }
  10163. }
  10164. }
  10165. }
  10166. else
  10167. {
  10168. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
  10169. {
  10170. return false;
  10171. }
  10172. while (current != ']')
  10173. {
  10174. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal(false)))
  10175. {
  10176. return false;
  10177. }
  10178. get_ignore_noop();
  10179. }
  10180. }
  10181. return sax->end_array();
  10182. }
  10183. /*!
  10184. @return whether object creation completed
  10185. */
  10186. bool get_ubjson_object()
  10187. {
  10188. std::pair<std::size_t, char_int_type> size_and_type;
  10189. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type)))
  10190. {
  10191. return false;
  10192. }
  10193. // do not accept ND-array size in objects in BJData
  10194. if (input_format == input_format_t::bjdata && size_and_type.first != npos && (size_and_type.second & (1 << 8)) != 0)
  10195. {
  10196. auto last_token = get_token_string();
  10197. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  10198. exception_message(input_format, "BJData object does not support ND-array size in optimized format", "object"), nullptr));
  10199. }
  10200. string_t key;
  10201. if (size_and_type.first != npos)
  10202. {
  10203. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(size_and_type.first)))
  10204. {
  10205. return false;
  10206. }
  10207. if (size_and_type.second != 0)
  10208. {
  10209. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10210. {
  10211. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
  10212. {
  10213. return false;
  10214. }
  10215. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
  10216. {
  10217. return false;
  10218. }
  10219. key.clear();
  10220. }
  10221. }
  10222. else
  10223. {
  10224. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10225. {
  10226. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
  10227. {
  10228. return false;
  10229. }
  10230. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
  10231. {
  10232. return false;
  10233. }
  10234. key.clear();
  10235. }
  10236. }
  10237. }
  10238. else
  10239. {
  10240. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast<std::size_t>(-1))))
  10241. {
  10242. return false;
  10243. }
  10244. while (current != '}')
  10245. {
  10246. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key, false) || !sax->key(key)))
  10247. {
  10248. return false;
  10249. }
  10250. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
  10251. {
  10252. return false;
  10253. }
  10254. get_ignore_noop();
  10255. key.clear();
  10256. }
  10257. }
  10258. return sax->end_object();
  10259. }
  10260. // Note, no reader for UBJSON binary types is implemented because they do
  10261. // not exist
  10262. bool get_ubjson_high_precision_number()
  10263. {
  10264. // get size of following number string
  10265. std::size_t size{};
  10266. bool no_ndarray = true;
  10267. auto res = get_ubjson_size_value(size, no_ndarray);
  10268. if (JSON_HEDLEY_UNLIKELY(!res))
  10269. {
  10270. return res;
  10271. }
  10272. // get number string
  10273. std::vector<char> number_vector;
  10274. for (std::size_t i = 0; i < size; ++i)
  10275. {
  10276. get();
  10277. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
  10278. {
  10279. return false;
  10280. }
  10281. number_vector.push_back(static_cast<char>(current));
  10282. }
  10283. // parse number string
  10284. using ia_type = decltype(detail::input_adapter(number_vector));
  10285. auto number_lexer = detail::lexer<BasicJsonType, ia_type>(detail::input_adapter(number_vector), false);
  10286. const auto result_number = number_lexer.scan();
  10287. const auto number_string = number_lexer.get_token_string();
  10288. const auto result_remainder = number_lexer.scan();
  10289. using token_type = typename detail::lexer_base<BasicJsonType>::token_type;
  10290. if (JSON_HEDLEY_UNLIKELY(result_remainder != token_type::end_of_input))
  10291. {
  10292. return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read,
  10293. exception_message(input_format, concat("invalid number text: ", number_lexer.get_token_string()), "high-precision number"), nullptr));
  10294. }
  10295. switch (result_number)
  10296. {
  10297. case token_type::value_integer:
  10298. return sax->number_integer(number_lexer.get_number_integer());
  10299. case token_type::value_unsigned:
  10300. return sax->number_unsigned(number_lexer.get_number_unsigned());
  10301. case token_type::value_float:
  10302. return sax->number_float(number_lexer.get_number_float(), std::move(number_string));
  10303. case token_type::uninitialized:
  10304. case token_type::literal_true:
  10305. case token_type::literal_false:
  10306. case token_type::literal_null:
  10307. case token_type::value_string:
  10308. case token_type::begin_array:
  10309. case token_type::begin_object:
  10310. case token_type::end_array:
  10311. case token_type::end_object:
  10312. case token_type::name_separator:
  10313. case token_type::value_separator:
  10314. case token_type::parse_error:
  10315. case token_type::end_of_input:
  10316. case token_type::literal_or_value:
  10317. default:
  10318. return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read,
  10319. exception_message(input_format, concat("invalid number text: ", number_lexer.get_token_string()), "high-precision number"), nullptr));
  10320. }
  10321. }
  10322. ///////////////////////
  10323. // Utility functions //
  10324. ///////////////////////
  10325. /*!
  10326. @brief get next character from the input
  10327. This function provides the interface to the used input adapter. It does
  10328. not throw in case the input reached EOF, but returns a -'ve valued
  10329. `std::char_traits<char_type>::eof()` in that case.
  10330. @return character read from the input
  10331. */
  10332. char_int_type get()
  10333. {
  10334. ++chars_read;
  10335. return current = ia.get_character();
  10336. }
  10337. /*!
  10338. @return character read from the input after ignoring all 'N' entries
  10339. */
  10340. char_int_type get_ignore_noop()
  10341. {
  10342. do
  10343. {
  10344. get();
  10345. }
  10346. while (current == 'N');
  10347. return current;
  10348. }
  10349. /*
  10350. @brief read a number from the input
  10351. @tparam NumberType the type of the number
  10352. @param[in] format the current format (for diagnostics)
  10353. @param[out] result number of type @a NumberType
  10354. @return whether conversion completed
  10355. @note This function needs to respect the system's endianness, because
  10356. bytes in CBOR, MessagePack, and UBJSON are stored in network order
  10357. (big endian) and therefore need reordering on little endian systems.
  10358. On the other hand, BSON and BJData use little endian and should reorder
  10359. on big endian systems.
  10360. */
  10361. template<typename NumberType, bool InputIsLittleEndian = false>
  10362. bool get_number(const input_format_t format, NumberType& result)
  10363. {
  10364. // step 1: read input into array with system's byte order
  10365. std::array<std::uint8_t, sizeof(NumberType)> vec{};
  10366. for (std::size_t i = 0; i < sizeof(NumberType); ++i)
  10367. {
  10368. get();
  10369. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "number")))
  10370. {
  10371. return false;
  10372. }
  10373. // reverse byte order prior to conversion if necessary
  10374. if (is_little_endian != (InputIsLittleEndian || format == input_format_t::bjdata))
  10375. {
  10376. vec[sizeof(NumberType) - i - 1] = static_cast<std::uint8_t>(current);
  10377. }
  10378. else
  10379. {
  10380. vec[i] = static_cast<std::uint8_t>(current); // LCOV_EXCL_LINE
  10381. }
  10382. }
  10383. // step 2: convert array into number of type T and return
  10384. std::memcpy(&result, vec.data(), sizeof(NumberType));
  10385. return true;
  10386. }
  10387. /*!
  10388. @brief create a string by reading characters from the input
  10389. @tparam NumberType the type of the number
  10390. @param[in] format the current format (for diagnostics)
  10391. @param[in] len number of characters to read
  10392. @param[out] result string created by reading @a len bytes
  10393. @return whether string creation completed
  10394. @note We can not reserve @a len bytes for the result, because @a len
  10395. may be too large. Usually, @ref unexpect_eof() detects the end of
  10396. the input before we run out of string memory.
  10397. */
  10398. template<typename NumberType>
  10399. bool get_string(const input_format_t format,
  10400. const NumberType len,
  10401. string_t& result)
  10402. {
  10403. bool success = true;
  10404. for (NumberType i = 0; i < len; i++)
  10405. {
  10406. get();
  10407. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "string")))
  10408. {
  10409. success = false;
  10410. break;
  10411. }
  10412. result.push_back(static_cast<typename string_t::value_type>(current));
  10413. }
  10414. return success;
  10415. }
  10416. /*!
  10417. @brief create a byte array by reading bytes from the input
  10418. @tparam NumberType the type of the number
  10419. @param[in] format the current format (for diagnostics)
  10420. @param[in] len number of bytes to read
  10421. @param[out] result byte array created by reading @a len bytes
  10422. @return whether byte array creation completed
  10423. @note We can not reserve @a len bytes for the result, because @a len
  10424. may be too large. Usually, @ref unexpect_eof() detects the end of
  10425. the input before we run out of memory.
  10426. */
  10427. template<typename NumberType>
  10428. bool get_binary(const input_format_t format,
  10429. const NumberType len,
  10430. binary_t& result)
  10431. {
  10432. bool success = true;
  10433. for (NumberType i = 0; i < len; i++)
  10434. {
  10435. get();
  10436. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "binary")))
  10437. {
  10438. success = false;
  10439. break;
  10440. }
  10441. result.push_back(static_cast<std::uint8_t>(current));
  10442. }
  10443. return success;
  10444. }
  10445. /*!
  10446. @param[in] format the current format (for diagnostics)
  10447. @param[in] context further context information (for diagnostics)
  10448. @return whether the last read character is not EOF
  10449. */
  10450. JSON_HEDLEY_NON_NULL(3)
  10451. bool unexpect_eof(const input_format_t format, const char* context) const
  10452. {
  10453. if (JSON_HEDLEY_UNLIKELY(current == std::char_traits<char_type>::eof()))
  10454. {
  10455. return sax->parse_error(chars_read, "<end of file>",
  10456. parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), nullptr));
  10457. }
  10458. return true;
  10459. }
  10460. /*!
  10461. @return a string representation of the last read byte
  10462. */
  10463. std::string get_token_string() const
  10464. {
  10465. std::array<char, 3> cr{{}};
  10466. static_cast<void>((std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(current))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  10467. return std::string{cr.data()};
  10468. }
  10469. /*!
  10470. @param[in] format the current format
  10471. @param[in] detail a detailed error message
  10472. @param[in] context further context information
  10473. @return a message string to use in the parse_error exceptions
  10474. */
  10475. std::string exception_message(const input_format_t format,
  10476. const std::string& detail,
  10477. const std::string& context) const
  10478. {
  10479. std::string error_msg = "syntax error while parsing ";
  10480. switch (format)
  10481. {
  10482. case input_format_t::cbor:
  10483. error_msg += "CBOR";
  10484. break;
  10485. case input_format_t::msgpack:
  10486. error_msg += "MessagePack";
  10487. break;
  10488. case input_format_t::ubjson:
  10489. error_msg += "UBJSON";
  10490. break;
  10491. case input_format_t::bson:
  10492. error_msg += "BSON";
  10493. break;
  10494. case input_format_t::bjdata:
  10495. error_msg += "BJData";
  10496. break;
  10497. case input_format_t::json: // LCOV_EXCL_LINE
  10498. default: // LCOV_EXCL_LINE
  10499. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  10500. }
  10501. return concat(error_msg, ' ', context, ": ", detail);
  10502. }
  10503. private:
  10504. static JSON_INLINE_VARIABLE constexpr std::size_t npos = static_cast<std::size_t>(-1);
  10505. /// input adapter
  10506. InputAdapterType ia;
  10507. /// the current character
  10508. char_int_type current = std::char_traits<char_type>::eof();
  10509. /// the number of characters read
  10510. std::size_t chars_read = 0;
  10511. /// whether we can assume little endianness
  10512. const bool is_little_endian = little_endianness();
  10513. /// input format
  10514. const input_format_t input_format = input_format_t::json;
  10515. /// the SAX parser
  10516. json_sax_t* sax = nullptr;
  10517. // excluded markers in bjdata optimized type
  10518. #define JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_ \
  10519. make_array<char_int_type>('F', 'H', 'N', 'S', 'T', 'Z', '[', '{')
  10520. #define JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_ \
  10521. make_array<bjd_type>( \
  10522. bjd_type{'C', "char"}, \
  10523. bjd_type{'D', "double"}, \
  10524. bjd_type{'I', "int16"}, \
  10525. bjd_type{'L', "int64"}, \
  10526. bjd_type{'M', "uint64"}, \
  10527. bjd_type{'U', "uint8"}, \
  10528. bjd_type{'d', "single"}, \
  10529. bjd_type{'i', "int8"}, \
  10530. bjd_type{'l', "int32"}, \
  10531. bjd_type{'m', "uint32"}, \
  10532. bjd_type{'u', "uint16"})
  10533. JSON_PRIVATE_UNLESS_TESTED:
  10534. // lookup tables
  10535. // NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
  10536. const decltype(JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_) bjd_optimized_type_markers =
  10537. JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_;
  10538. using bjd_type = std::pair<char_int_type, string_t>;
  10539. // NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
  10540. const decltype(JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_) bjd_types_map =
  10541. JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_;
  10542. #undef JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_
  10543. #undef JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_
  10544. };
  10545. #ifndef JSON_HAS_CPP_17
  10546. template<typename BasicJsonType, typename InputAdapterType, typename SAX>
  10547. constexpr std::size_t binary_reader<BasicJsonType, InputAdapterType, SAX>::npos;
  10548. #endif
  10549. } // namespace detail
  10550. NLOHMANN_JSON_NAMESPACE_END
  10551. // #include <nlohmann/detail/input/input_adapters.hpp>
  10552. // #include <nlohmann/detail/input/lexer.hpp>
  10553. // #include <nlohmann/detail/input/parser.hpp>
  10554. // __ _____ _____ _____
  10555. // __| | __| | | | JSON for Modern C++
  10556. // | | |__ | | | | | | version 3.11.2
  10557. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  10558. //
  10559. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  10560. // SPDX-License-Identifier: MIT
  10561. #include <cmath> // isfinite
  10562. #include <cstdint> // uint8_t
  10563. #include <functional> // function
  10564. #include <string> // string
  10565. #include <utility> // move
  10566. #include <vector> // vector
  10567. // #include <nlohmann/detail/exceptions.hpp>
  10568. // #include <nlohmann/detail/input/input_adapters.hpp>
  10569. // #include <nlohmann/detail/input/json_sax.hpp>
  10570. // #include <nlohmann/detail/input/lexer.hpp>
  10571. // #include <nlohmann/detail/macro_scope.hpp>
  10572. // #include <nlohmann/detail/meta/is_sax.hpp>
  10573. // #include <nlohmann/detail/string_concat.hpp>
  10574. // #include <nlohmann/detail/value_t.hpp>
  10575. NLOHMANN_JSON_NAMESPACE_BEGIN
  10576. namespace detail
  10577. {
  10578. ////////////
  10579. // parser //
  10580. ////////////
  10581. enum class parse_event_t : std::uint8_t
  10582. {
  10583. /// the parser read `{` and started to process a JSON object
  10584. object_start,
  10585. /// the parser read `}` and finished processing a JSON object
  10586. object_end,
  10587. /// the parser read `[` and started to process a JSON array
  10588. array_start,
  10589. /// the parser read `]` and finished processing a JSON array
  10590. array_end,
  10591. /// the parser read a key of a value in an object
  10592. key,
  10593. /// the parser finished reading a JSON value
  10594. value
  10595. };
  10596. template<typename BasicJsonType>
  10597. using parser_callback_t =
  10598. std::function<bool(int /*depth*/, parse_event_t /*event*/, BasicJsonType& /*parsed*/)>;
  10599. /*!
  10600. @brief syntax analysis
  10601. This class implements a recursive descent parser.
  10602. */
  10603. template<typename BasicJsonType, typename InputAdapterType>
  10604. class parser
  10605. {
  10606. using number_integer_t = typename BasicJsonType::number_integer_t;
  10607. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  10608. using number_float_t = typename BasicJsonType::number_float_t;
  10609. using string_t = typename BasicJsonType::string_t;
  10610. using lexer_t = lexer<BasicJsonType, InputAdapterType>;
  10611. using token_type = typename lexer_t::token_type;
  10612. public:
  10613. /// a parser reading from an input adapter
  10614. explicit parser(InputAdapterType&& adapter,
  10615. const parser_callback_t<BasicJsonType> cb = nullptr,
  10616. const bool allow_exceptions_ = true,
  10617. const bool skip_comments = false)
  10618. : callback(cb)
  10619. , m_lexer(std::move(adapter), skip_comments)
  10620. , allow_exceptions(allow_exceptions_)
  10621. {
  10622. // read first token
  10623. get_token();
  10624. }
  10625. /*!
  10626. @brief public parser interface
  10627. @param[in] strict whether to expect the last token to be EOF
  10628. @param[in,out] result parsed JSON value
  10629. @throw parse_error.101 in case of an unexpected token
  10630. @throw parse_error.102 if to_unicode fails or surrogate error
  10631. @throw parse_error.103 if to_unicode fails
  10632. */
  10633. void parse(const bool strict, BasicJsonType& result)
  10634. {
  10635. if (callback)
  10636. {
  10637. json_sax_dom_callback_parser<BasicJsonType> sdp(result, callback, allow_exceptions);
  10638. sax_parse_internal(&sdp);
  10639. // in strict mode, input must be completely read
  10640. if (strict && (get_token() != token_type::end_of_input))
  10641. {
  10642. sdp.parse_error(m_lexer.get_position(),
  10643. m_lexer.get_token_string(),
  10644. parse_error::create(101, m_lexer.get_position(),
  10645. exception_message(token_type::end_of_input, "value"), nullptr));
  10646. }
  10647. // in case of an error, return discarded value
  10648. if (sdp.is_errored())
  10649. {
  10650. result = value_t::discarded;
  10651. return;
  10652. }
  10653. // set top-level value to null if it was discarded by the callback
  10654. // function
  10655. if (result.is_discarded())
  10656. {
  10657. result = nullptr;
  10658. }
  10659. }
  10660. else
  10661. {
  10662. json_sax_dom_parser<BasicJsonType> sdp(result, allow_exceptions);
  10663. sax_parse_internal(&sdp);
  10664. // in strict mode, input must be completely read
  10665. if (strict && (get_token() != token_type::end_of_input))
  10666. {
  10667. sdp.parse_error(m_lexer.get_position(),
  10668. m_lexer.get_token_string(),
  10669. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), nullptr));
  10670. }
  10671. // in case of an error, return discarded value
  10672. if (sdp.is_errored())
  10673. {
  10674. result = value_t::discarded;
  10675. return;
  10676. }
  10677. }
  10678. result.assert_invariant();
  10679. }
  10680. /*!
  10681. @brief public accept interface
  10682. @param[in] strict whether to expect the last token to be EOF
  10683. @return whether the input is a proper JSON text
  10684. */
  10685. bool accept(const bool strict = true)
  10686. {
  10687. json_sax_acceptor<BasicJsonType> sax_acceptor;
  10688. return sax_parse(&sax_acceptor, strict);
  10689. }
  10690. template<typename SAX>
  10691. JSON_HEDLEY_NON_NULL(2)
  10692. bool sax_parse(SAX* sax, const bool strict = true)
  10693. {
  10694. (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
  10695. const bool result = sax_parse_internal(sax);
  10696. // strict mode: next byte must be EOF
  10697. if (result && strict && (get_token() != token_type::end_of_input))
  10698. {
  10699. return sax->parse_error(m_lexer.get_position(),
  10700. m_lexer.get_token_string(),
  10701. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), nullptr));
  10702. }
  10703. return result;
  10704. }
  10705. private:
  10706. template<typename SAX>
  10707. JSON_HEDLEY_NON_NULL(2)
  10708. bool sax_parse_internal(SAX* sax)
  10709. {
  10710. // stack to remember the hierarchy of structured values we are parsing
  10711. // true = array; false = object
  10712. std::vector<bool> states;
  10713. // value to avoid a goto (see comment where set to true)
  10714. bool skip_to_state_evaluation = false;
  10715. while (true)
  10716. {
  10717. if (!skip_to_state_evaluation)
  10718. {
  10719. // invariant: get_token() was called before each iteration
  10720. switch (last_token)
  10721. {
  10722. case token_type::begin_object:
  10723. {
  10724. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast<std::size_t>(-1))))
  10725. {
  10726. return false;
  10727. }
  10728. // closing } -> we are done
  10729. if (get_token() == token_type::end_object)
  10730. {
  10731. if (JSON_HEDLEY_UNLIKELY(!sax->end_object()))
  10732. {
  10733. return false;
  10734. }
  10735. break;
  10736. }
  10737. // parse key
  10738. if (JSON_HEDLEY_UNLIKELY(last_token != token_type::value_string))
  10739. {
  10740. return sax->parse_error(m_lexer.get_position(),
  10741. m_lexer.get_token_string(),
  10742. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), nullptr));
  10743. }
  10744. if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string())))
  10745. {
  10746. return false;
  10747. }
  10748. // parse separator (:)
  10749. if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
  10750. {
  10751. return sax->parse_error(m_lexer.get_position(),
  10752. m_lexer.get_token_string(),
  10753. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), nullptr));
  10754. }
  10755. // remember we are now inside an object
  10756. states.push_back(false);
  10757. // parse values
  10758. get_token();
  10759. continue;
  10760. }
  10761. case token_type::begin_array:
  10762. {
  10763. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
  10764. {
  10765. return false;
  10766. }
  10767. // closing ] -> we are done
  10768. if (get_token() == token_type::end_array)
  10769. {
  10770. if (JSON_HEDLEY_UNLIKELY(!sax->end_array()))
  10771. {
  10772. return false;
  10773. }
  10774. break;
  10775. }
  10776. // remember we are now inside an array
  10777. states.push_back(true);
  10778. // parse values (no need to call get_token)
  10779. continue;
  10780. }
  10781. case token_type::value_float:
  10782. {
  10783. const auto res = m_lexer.get_number_float();
  10784. if (JSON_HEDLEY_UNLIKELY(!std::isfinite(res)))
  10785. {
  10786. return sax->parse_error(m_lexer.get_position(),
  10787. m_lexer.get_token_string(),
  10788. out_of_range::create(406, concat("number overflow parsing '", m_lexer.get_token_string(), '\''), nullptr));
  10789. }
  10790. if (JSON_HEDLEY_UNLIKELY(!sax->number_float(res, m_lexer.get_string())))
  10791. {
  10792. return false;
  10793. }
  10794. break;
  10795. }
  10796. case token_type::literal_false:
  10797. {
  10798. if (JSON_HEDLEY_UNLIKELY(!sax->boolean(false)))
  10799. {
  10800. return false;
  10801. }
  10802. break;
  10803. }
  10804. case token_type::literal_null:
  10805. {
  10806. if (JSON_HEDLEY_UNLIKELY(!sax->null()))
  10807. {
  10808. return false;
  10809. }
  10810. break;
  10811. }
  10812. case token_type::literal_true:
  10813. {
  10814. if (JSON_HEDLEY_UNLIKELY(!sax->boolean(true)))
  10815. {
  10816. return false;
  10817. }
  10818. break;
  10819. }
  10820. case token_type::value_integer:
  10821. {
  10822. if (JSON_HEDLEY_UNLIKELY(!sax->number_integer(m_lexer.get_number_integer())))
  10823. {
  10824. return false;
  10825. }
  10826. break;
  10827. }
  10828. case token_type::value_string:
  10829. {
  10830. if (JSON_HEDLEY_UNLIKELY(!sax->string(m_lexer.get_string())))
  10831. {
  10832. return false;
  10833. }
  10834. break;
  10835. }
  10836. case token_type::value_unsigned:
  10837. {
  10838. if (JSON_HEDLEY_UNLIKELY(!sax->number_unsigned(m_lexer.get_number_unsigned())))
  10839. {
  10840. return false;
  10841. }
  10842. break;
  10843. }
  10844. case token_type::parse_error:
  10845. {
  10846. // using "uninitialized" to avoid "expected" message
  10847. return sax->parse_error(m_lexer.get_position(),
  10848. m_lexer.get_token_string(),
  10849. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::uninitialized, "value"), nullptr));
  10850. }
  10851. case token_type::uninitialized:
  10852. case token_type::end_array:
  10853. case token_type::end_object:
  10854. case token_type::name_separator:
  10855. case token_type::value_separator:
  10856. case token_type::end_of_input:
  10857. case token_type::literal_or_value:
  10858. default: // the last token was unexpected
  10859. {
  10860. return sax->parse_error(m_lexer.get_position(),
  10861. m_lexer.get_token_string(),
  10862. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::literal_or_value, "value"), nullptr));
  10863. }
  10864. }
  10865. }
  10866. else
  10867. {
  10868. skip_to_state_evaluation = false;
  10869. }
  10870. // we reached this line after we successfully parsed a value
  10871. if (states.empty())
  10872. {
  10873. // empty stack: we reached the end of the hierarchy: done
  10874. return true;
  10875. }
  10876. if (states.back()) // array
  10877. {
  10878. // comma -> next value
  10879. if (get_token() == token_type::value_separator)
  10880. {
  10881. // parse a new value
  10882. get_token();
  10883. continue;
  10884. }
  10885. // closing ]
  10886. if (JSON_HEDLEY_LIKELY(last_token == token_type::end_array))
  10887. {
  10888. if (JSON_HEDLEY_UNLIKELY(!sax->end_array()))
  10889. {
  10890. return false;
  10891. }
  10892. // We are done with this array. Before we can parse a
  10893. // new value, we need to evaluate the new state first.
  10894. // By setting skip_to_state_evaluation to false, we
  10895. // are effectively jumping to the beginning of this if.
  10896. JSON_ASSERT(!states.empty());
  10897. states.pop_back();
  10898. skip_to_state_evaluation = true;
  10899. continue;
  10900. }
  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::end_array, "array"), nullptr));
  10904. }
  10905. // states.back() is false -> object
  10906. // comma -> next value
  10907. if (get_token() == token_type::value_separator)
  10908. {
  10909. // parse key
  10910. if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::value_string))
  10911. {
  10912. return sax->parse_error(m_lexer.get_position(),
  10913. m_lexer.get_token_string(),
  10914. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), nullptr));
  10915. }
  10916. if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string())))
  10917. {
  10918. return false;
  10919. }
  10920. // parse separator (:)
  10921. if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
  10922. {
  10923. return sax->parse_error(m_lexer.get_position(),
  10924. m_lexer.get_token_string(),
  10925. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), nullptr));
  10926. }
  10927. // parse values
  10928. get_token();
  10929. continue;
  10930. }
  10931. // closing }
  10932. if (JSON_HEDLEY_LIKELY(last_token == token_type::end_object))
  10933. {
  10934. if (JSON_HEDLEY_UNLIKELY(!sax->end_object()))
  10935. {
  10936. return false;
  10937. }
  10938. // We are done with this object. Before we can parse a
  10939. // new value, we need to evaluate the new state first.
  10940. // By setting skip_to_state_evaluation to false, we
  10941. // are effectively jumping to the beginning of this if.
  10942. JSON_ASSERT(!states.empty());
  10943. states.pop_back();
  10944. skip_to_state_evaluation = true;
  10945. continue;
  10946. }
  10947. return sax->parse_error(m_lexer.get_position(),
  10948. m_lexer.get_token_string(),
  10949. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_object, "object"), nullptr));
  10950. }
  10951. }
  10952. /// get next token from lexer
  10953. token_type get_token()
  10954. {
  10955. return last_token = m_lexer.scan();
  10956. }
  10957. std::string exception_message(const token_type expected, const std::string& context)
  10958. {
  10959. std::string error_msg = "syntax error ";
  10960. if (!context.empty())
  10961. {
  10962. error_msg += concat("while parsing ", context, ' ');
  10963. }
  10964. error_msg += "- ";
  10965. if (last_token == token_type::parse_error)
  10966. {
  10967. error_msg += concat(m_lexer.get_error_message(), "; last read: '",
  10968. m_lexer.get_token_string(), '\'');
  10969. }
  10970. else
  10971. {
  10972. error_msg += concat("unexpected ", lexer_t::token_type_name(last_token));
  10973. }
  10974. if (expected != token_type::uninitialized)
  10975. {
  10976. error_msg += concat("; expected ", lexer_t::token_type_name(expected));
  10977. }
  10978. return error_msg;
  10979. }
  10980. private:
  10981. /// callback function
  10982. const parser_callback_t<BasicJsonType> callback = nullptr;
  10983. /// the type of the last read token
  10984. token_type last_token = token_type::uninitialized;
  10985. /// the lexer
  10986. lexer_t m_lexer;
  10987. /// whether to throw exceptions in case of errors
  10988. const bool allow_exceptions = true;
  10989. };
  10990. } // namespace detail
  10991. NLOHMANN_JSON_NAMESPACE_END
  10992. // #include <nlohmann/detail/iterators/internal_iterator.hpp>
  10993. // __ _____ _____ _____
  10994. // __| | __| | | | JSON for Modern C++
  10995. // | | |__ | | | | | | version 3.11.2
  10996. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  10997. //
  10998. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  10999. // SPDX-License-Identifier: MIT
  11000. // #include <nlohmann/detail/abi_macros.hpp>
  11001. // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
  11002. // __ _____ _____ _____
  11003. // __| | __| | | | JSON for Modern C++
  11004. // | | |__ | | | | | | version 3.11.2
  11005. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  11006. //
  11007. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  11008. // SPDX-License-Identifier: MIT
  11009. #include <cstddef> // ptrdiff_t
  11010. #include <limits> // numeric_limits
  11011. // #include <nlohmann/detail/macro_scope.hpp>
  11012. NLOHMANN_JSON_NAMESPACE_BEGIN
  11013. namespace detail
  11014. {
  11015. /*
  11016. @brief an iterator for primitive JSON types
  11017. This class models an iterator for primitive JSON types (boolean, number,
  11018. string). It's only purpose is to allow the iterator/const_iterator classes
  11019. to "iterate" over primitive values. Internally, the iterator is modeled by
  11020. a `difference_type` variable. Value begin_value (`0`) models the begin,
  11021. end_value (`1`) models past the end.
  11022. */
  11023. class primitive_iterator_t
  11024. {
  11025. private:
  11026. using difference_type = std::ptrdiff_t;
  11027. static constexpr difference_type begin_value = 0;
  11028. static constexpr difference_type end_value = begin_value + 1;
  11029. JSON_PRIVATE_UNLESS_TESTED:
  11030. /// iterator as signed integer type
  11031. difference_type m_it = (std::numeric_limits<std::ptrdiff_t>::min)();
  11032. public:
  11033. constexpr difference_type get_value() const noexcept
  11034. {
  11035. return m_it;
  11036. }
  11037. /// set iterator to a defined beginning
  11038. void set_begin() noexcept
  11039. {
  11040. m_it = begin_value;
  11041. }
  11042. /// set iterator to a defined past the end
  11043. void set_end() noexcept
  11044. {
  11045. m_it = end_value;
  11046. }
  11047. /// return whether the iterator can be dereferenced
  11048. constexpr bool is_begin() const noexcept
  11049. {
  11050. return m_it == begin_value;
  11051. }
  11052. /// return whether the iterator is at end
  11053. constexpr bool is_end() const noexcept
  11054. {
  11055. return m_it == end_value;
  11056. }
  11057. friend constexpr bool operator==(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
  11058. {
  11059. return lhs.m_it == rhs.m_it;
  11060. }
  11061. friend constexpr bool operator<(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
  11062. {
  11063. return lhs.m_it < rhs.m_it;
  11064. }
  11065. primitive_iterator_t operator+(difference_type n) noexcept
  11066. {
  11067. auto result = *this;
  11068. result += n;
  11069. return result;
  11070. }
  11071. friend constexpr difference_type operator-(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
  11072. {
  11073. return lhs.m_it - rhs.m_it;
  11074. }
  11075. primitive_iterator_t& operator++() noexcept
  11076. {
  11077. ++m_it;
  11078. return *this;
  11079. }
  11080. primitive_iterator_t operator++(int)& noexcept // NOLINT(cert-dcl21-cpp)
  11081. {
  11082. auto result = *this;
  11083. ++m_it;
  11084. return result;
  11085. }
  11086. primitive_iterator_t& operator--() noexcept
  11087. {
  11088. --m_it;
  11089. return *this;
  11090. }
  11091. primitive_iterator_t operator--(int)& noexcept // NOLINT(cert-dcl21-cpp)
  11092. {
  11093. auto result = *this;
  11094. --m_it;
  11095. return result;
  11096. }
  11097. primitive_iterator_t& operator+=(difference_type n) noexcept
  11098. {
  11099. m_it += n;
  11100. return *this;
  11101. }
  11102. primitive_iterator_t& operator-=(difference_type n) noexcept
  11103. {
  11104. m_it -= n;
  11105. return *this;
  11106. }
  11107. };
  11108. } // namespace detail
  11109. NLOHMANN_JSON_NAMESPACE_END
  11110. NLOHMANN_JSON_NAMESPACE_BEGIN
  11111. namespace detail
  11112. {
  11113. /*!
  11114. @brief an iterator value
  11115. @note This structure could easily be a union, but MSVC currently does not allow
  11116. unions members with complex constructors, see https://github.com/nlohmann/json/pull/105.
  11117. */
  11118. template<typename BasicJsonType> struct internal_iterator
  11119. {
  11120. /// iterator for JSON objects
  11121. typename BasicJsonType::object_t::iterator object_iterator {};
  11122. /// iterator for JSON arrays
  11123. typename BasicJsonType::array_t::iterator array_iterator {};
  11124. /// generic iterator for all other types
  11125. primitive_iterator_t primitive_iterator {};
  11126. };
  11127. } // namespace detail
  11128. NLOHMANN_JSON_NAMESPACE_END
  11129. // #include <nlohmann/detail/iterators/iter_impl.hpp>
  11130. // __ _____ _____ _____
  11131. // __| | __| | | | JSON for Modern C++
  11132. // | | |__ | | | | | | version 3.11.2
  11133. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  11134. //
  11135. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  11136. // SPDX-License-Identifier: MIT
  11137. #include <iterator> // iterator, random_access_iterator_tag, bidirectional_iterator_tag, advance, next
  11138. #include <type_traits> // conditional, is_const, remove_const
  11139. // #include <nlohmann/detail/exceptions.hpp>
  11140. // #include <nlohmann/detail/iterators/internal_iterator.hpp>
  11141. // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
  11142. // #include <nlohmann/detail/macro_scope.hpp>
  11143. // #include <nlohmann/detail/meta/cpp_future.hpp>
  11144. // #include <nlohmann/detail/meta/type_traits.hpp>
  11145. // #include <nlohmann/detail/value_t.hpp>
  11146. NLOHMANN_JSON_NAMESPACE_BEGIN
  11147. namespace detail
  11148. {
  11149. // forward declare, to be able to friend it later on
  11150. template<typename IteratorType> class iteration_proxy;
  11151. template<typename IteratorType> class iteration_proxy_value;
  11152. /*!
  11153. @brief a template for a bidirectional iterator for the @ref basic_json class
  11154. This class implements a both iterators (iterator and const_iterator) for the
  11155. @ref basic_json class.
  11156. @note An iterator is called *initialized* when a pointer to a JSON value has
  11157. been set (e.g., by a constructor or a copy assignment). If the iterator is
  11158. default-constructed, it is *uninitialized* and most methods are undefined.
  11159. **The library uses assertions to detect calls on uninitialized iterators.**
  11160. @requirement The class satisfies the following concept requirements:
  11161. -
  11162. [BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
  11163. The iterator that can be moved can be moved in both directions (i.e.
  11164. incremented and decremented).
  11165. @since version 1.0.0, simplified in version 2.0.9, change to bidirectional
  11166. iterators in version 3.0.0 (see https://github.com/nlohmann/json/issues/593)
  11167. */
  11168. template<typename BasicJsonType>
  11169. class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions)
  11170. {
  11171. /// the iterator with BasicJsonType of different const-ness
  11172. using other_iter_impl = iter_impl<typename std::conditional<std::is_const<BasicJsonType>::value, typename std::remove_const<BasicJsonType>::type, const BasicJsonType>::type>;
  11173. /// allow basic_json to access private members
  11174. friend other_iter_impl;
  11175. friend BasicJsonType;
  11176. friend iteration_proxy<iter_impl>;
  11177. friend iteration_proxy_value<iter_impl>;
  11178. using object_t = typename BasicJsonType::object_t;
  11179. using array_t = typename BasicJsonType::array_t;
  11180. // make sure BasicJsonType is basic_json or const basic_json
  11181. static_assert(is_basic_json<typename std::remove_const<BasicJsonType>::type>::value,
  11182. "iter_impl only accepts (const) basic_json");
  11183. // superficial check for the LegacyBidirectionalIterator named requirement
  11184. static_assert(std::is_base_of<std::bidirectional_iterator_tag, std::bidirectional_iterator_tag>::value
  11185. && std::is_base_of<std::bidirectional_iterator_tag, typename std::iterator_traits<typename array_t::iterator>::iterator_category>::value,
  11186. "basic_json iterator assumes array and object type iterators satisfy the LegacyBidirectionalIterator named requirement.");
  11187. public:
  11188. /// The std::iterator class template (used as a base class to provide typedefs) is deprecated in C++17.
  11189. /// The C++ Standard has never required user-defined iterators to derive from std::iterator.
  11190. /// A user-defined iterator should provide publicly accessible typedefs named
  11191. /// iterator_category, value_type, difference_type, pointer, and reference.
  11192. /// Note that value_type is required to be non-const, even for constant iterators.
  11193. using iterator_category = std::bidirectional_iterator_tag;
  11194. /// the type of the values when the iterator is dereferenced
  11195. using value_type = typename BasicJsonType::value_type;
  11196. /// a type to represent differences between iterators
  11197. using difference_type = typename BasicJsonType::difference_type;
  11198. /// defines a pointer to the type iterated over (value_type)
  11199. using pointer = typename std::conditional<std::is_const<BasicJsonType>::value,
  11200. typename BasicJsonType::const_pointer,
  11201. typename BasicJsonType::pointer>::type;
  11202. /// defines a reference to the type iterated over (value_type)
  11203. using reference =
  11204. typename std::conditional<std::is_const<BasicJsonType>::value,
  11205. typename BasicJsonType::const_reference,
  11206. typename BasicJsonType::reference>::type;
  11207. iter_impl() = default;
  11208. ~iter_impl() = default;
  11209. iter_impl(iter_impl&&) noexcept = default;
  11210. iter_impl& operator=(iter_impl&&) noexcept = default;
  11211. /*!
  11212. @brief constructor for a given JSON instance
  11213. @param[in] object pointer to a JSON object for this iterator
  11214. @pre object != nullptr
  11215. @post The iterator is initialized; i.e. `m_object != nullptr`.
  11216. */
  11217. explicit iter_impl(pointer object) noexcept : m_object(object)
  11218. {
  11219. JSON_ASSERT(m_object != nullptr);
  11220. switch (m_object->m_data.m_type)
  11221. {
  11222. case value_t::object:
  11223. {
  11224. m_it.object_iterator = typename object_t::iterator();
  11225. break;
  11226. }
  11227. case value_t::array:
  11228. {
  11229. m_it.array_iterator = typename array_t::iterator();
  11230. break;
  11231. }
  11232. case value_t::null:
  11233. case value_t::string:
  11234. case value_t::boolean:
  11235. case value_t::number_integer:
  11236. case value_t::number_unsigned:
  11237. case value_t::number_float:
  11238. case value_t::binary:
  11239. case value_t::discarded:
  11240. default:
  11241. {
  11242. m_it.primitive_iterator = primitive_iterator_t();
  11243. break;
  11244. }
  11245. }
  11246. }
  11247. /*!
  11248. @note The conventional copy constructor and copy assignment are implicitly
  11249. defined. Combined with the following converting constructor and
  11250. assignment, they support: (1) copy from iterator to iterator, (2)
  11251. copy from const iterator to const iterator, and (3) conversion from
  11252. iterator to const iterator. However conversion from const iterator
  11253. to iterator is not defined.
  11254. */
  11255. /*!
  11256. @brief const copy constructor
  11257. @param[in] other const iterator to copy from
  11258. @note This copy constructor had to be defined explicitly to circumvent a bug
  11259. occurring on msvc v19.0 compiler (VS 2015) debug build. For more
  11260. information refer to: https://github.com/nlohmann/json/issues/1608
  11261. */
  11262. iter_impl(const iter_impl<const BasicJsonType>& other) noexcept
  11263. : m_object(other.m_object), m_it(other.m_it)
  11264. {}
  11265. /*!
  11266. @brief converting assignment
  11267. @param[in] other const iterator to copy from
  11268. @return const/non-const iterator
  11269. @note It is not checked whether @a other is initialized.
  11270. */
  11271. iter_impl& operator=(const iter_impl<const BasicJsonType>& other) noexcept
  11272. {
  11273. if (&other != this)
  11274. {
  11275. m_object = other.m_object;
  11276. m_it = other.m_it;
  11277. }
  11278. return *this;
  11279. }
  11280. /*!
  11281. @brief converting constructor
  11282. @param[in] other non-const iterator to copy from
  11283. @note It is not checked whether @a other is initialized.
  11284. */
  11285. iter_impl(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept
  11286. : m_object(other.m_object), m_it(other.m_it)
  11287. {}
  11288. /*!
  11289. @brief converting assignment
  11290. @param[in] other non-const iterator to copy from
  11291. @return const/non-const iterator
  11292. @note It is not checked whether @a other is initialized.
  11293. */
  11294. iter_impl& operator=(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept // NOLINT(cert-oop54-cpp)
  11295. {
  11296. m_object = other.m_object;
  11297. m_it = other.m_it;
  11298. return *this;
  11299. }
  11300. JSON_PRIVATE_UNLESS_TESTED:
  11301. /*!
  11302. @brief set the iterator to the first value
  11303. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11304. */
  11305. void set_begin() noexcept
  11306. {
  11307. JSON_ASSERT(m_object != nullptr);
  11308. switch (m_object->m_data.m_type)
  11309. {
  11310. case value_t::object:
  11311. {
  11312. m_it.object_iterator = m_object->m_data.m_value.object->begin();
  11313. break;
  11314. }
  11315. case value_t::array:
  11316. {
  11317. m_it.array_iterator = m_object->m_data.m_value.array->begin();
  11318. break;
  11319. }
  11320. case value_t::null:
  11321. {
  11322. // set to end so begin()==end() is true: null is empty
  11323. m_it.primitive_iterator.set_end();
  11324. break;
  11325. }
  11326. case value_t::string:
  11327. case value_t::boolean:
  11328. case value_t::number_integer:
  11329. case value_t::number_unsigned:
  11330. case value_t::number_float:
  11331. case value_t::binary:
  11332. case value_t::discarded:
  11333. default:
  11334. {
  11335. m_it.primitive_iterator.set_begin();
  11336. break;
  11337. }
  11338. }
  11339. }
  11340. /*!
  11341. @brief set the iterator past the last value
  11342. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11343. */
  11344. void set_end() noexcept
  11345. {
  11346. JSON_ASSERT(m_object != nullptr);
  11347. switch (m_object->m_data.m_type)
  11348. {
  11349. case value_t::object:
  11350. {
  11351. m_it.object_iterator = m_object->m_data.m_value.object->end();
  11352. break;
  11353. }
  11354. case value_t::array:
  11355. {
  11356. m_it.array_iterator = m_object->m_data.m_value.array->end();
  11357. break;
  11358. }
  11359. case value_t::null:
  11360. case value_t::string:
  11361. case value_t::boolean:
  11362. case value_t::number_integer:
  11363. case value_t::number_unsigned:
  11364. case value_t::number_float:
  11365. case value_t::binary:
  11366. case value_t::discarded:
  11367. default:
  11368. {
  11369. m_it.primitive_iterator.set_end();
  11370. break;
  11371. }
  11372. }
  11373. }
  11374. public:
  11375. /*!
  11376. @brief return a reference to the value pointed to by the iterator
  11377. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11378. */
  11379. reference operator*() const
  11380. {
  11381. JSON_ASSERT(m_object != nullptr);
  11382. switch (m_object->m_data.m_type)
  11383. {
  11384. case value_t::object:
  11385. {
  11386. JSON_ASSERT(m_it.object_iterator != m_object->m_data.m_value.object->end());
  11387. return m_it.object_iterator->second;
  11388. }
  11389. case value_t::array:
  11390. {
  11391. JSON_ASSERT(m_it.array_iterator != m_object->m_data.m_value.array->end());
  11392. return *m_it.array_iterator;
  11393. }
  11394. case value_t::null:
  11395. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  11396. case value_t::string:
  11397. case value_t::boolean:
  11398. case value_t::number_integer:
  11399. case value_t::number_unsigned:
  11400. case value_t::number_float:
  11401. case value_t::binary:
  11402. case value_t::discarded:
  11403. default:
  11404. {
  11405. if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin()))
  11406. {
  11407. return *m_object;
  11408. }
  11409. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  11410. }
  11411. }
  11412. }
  11413. /*!
  11414. @brief dereference the iterator
  11415. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11416. */
  11417. pointer operator->() const
  11418. {
  11419. JSON_ASSERT(m_object != nullptr);
  11420. switch (m_object->m_data.m_type)
  11421. {
  11422. case value_t::object:
  11423. {
  11424. JSON_ASSERT(m_it.object_iterator != m_object->m_data.m_value.object->end());
  11425. return &(m_it.object_iterator->second);
  11426. }
  11427. case value_t::array:
  11428. {
  11429. JSON_ASSERT(m_it.array_iterator != m_object->m_data.m_value.array->end());
  11430. return &*m_it.array_iterator;
  11431. }
  11432. case value_t::null:
  11433. case value_t::string:
  11434. case value_t::boolean:
  11435. case value_t::number_integer:
  11436. case value_t::number_unsigned:
  11437. case value_t::number_float:
  11438. case value_t::binary:
  11439. case value_t::discarded:
  11440. default:
  11441. {
  11442. if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin()))
  11443. {
  11444. return m_object;
  11445. }
  11446. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  11447. }
  11448. }
  11449. }
  11450. /*!
  11451. @brief post-increment (it++)
  11452. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11453. */
  11454. iter_impl operator++(int)& // NOLINT(cert-dcl21-cpp)
  11455. {
  11456. auto result = *this;
  11457. ++(*this);
  11458. return result;
  11459. }
  11460. /*!
  11461. @brief pre-increment (++it)
  11462. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11463. */
  11464. iter_impl& operator++()
  11465. {
  11466. JSON_ASSERT(m_object != nullptr);
  11467. switch (m_object->m_data.m_type)
  11468. {
  11469. case value_t::object:
  11470. {
  11471. std::advance(m_it.object_iterator, 1);
  11472. break;
  11473. }
  11474. case value_t::array:
  11475. {
  11476. std::advance(m_it.array_iterator, 1);
  11477. break;
  11478. }
  11479. case value_t::null:
  11480. case value_t::string:
  11481. case value_t::boolean:
  11482. case value_t::number_integer:
  11483. case value_t::number_unsigned:
  11484. case value_t::number_float:
  11485. case value_t::binary:
  11486. case value_t::discarded:
  11487. default:
  11488. {
  11489. ++m_it.primitive_iterator;
  11490. break;
  11491. }
  11492. }
  11493. return *this;
  11494. }
  11495. /*!
  11496. @brief post-decrement (it--)
  11497. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11498. */
  11499. iter_impl operator--(int)& // NOLINT(cert-dcl21-cpp)
  11500. {
  11501. auto result = *this;
  11502. --(*this);
  11503. return result;
  11504. }
  11505. /*!
  11506. @brief pre-decrement (--it)
  11507. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11508. */
  11509. iter_impl& operator--()
  11510. {
  11511. JSON_ASSERT(m_object != nullptr);
  11512. switch (m_object->m_data.m_type)
  11513. {
  11514. case value_t::object:
  11515. {
  11516. std::advance(m_it.object_iterator, -1);
  11517. break;
  11518. }
  11519. case value_t::array:
  11520. {
  11521. std::advance(m_it.array_iterator, -1);
  11522. break;
  11523. }
  11524. case value_t::null:
  11525. case value_t::string:
  11526. case value_t::boolean:
  11527. case value_t::number_integer:
  11528. case value_t::number_unsigned:
  11529. case value_t::number_float:
  11530. case value_t::binary:
  11531. case value_t::discarded:
  11532. default:
  11533. {
  11534. --m_it.primitive_iterator;
  11535. break;
  11536. }
  11537. }
  11538. return *this;
  11539. }
  11540. /*!
  11541. @brief comparison: equal
  11542. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11543. */
  11544. 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 >
  11545. bool operator==(const IterImpl& other) const
  11546. {
  11547. // if objects are not the same, the comparison is undefined
  11548. if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
  11549. {
  11550. JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", m_object));
  11551. }
  11552. JSON_ASSERT(m_object != nullptr);
  11553. switch (m_object->m_data.m_type)
  11554. {
  11555. case value_t::object:
  11556. return (m_it.object_iterator == other.m_it.object_iterator);
  11557. case value_t::array:
  11558. return (m_it.array_iterator == other.m_it.array_iterator);
  11559. case value_t::null:
  11560. case value_t::string:
  11561. case value_t::boolean:
  11562. case value_t::number_integer:
  11563. case value_t::number_unsigned:
  11564. case value_t::number_float:
  11565. case value_t::binary:
  11566. case value_t::discarded:
  11567. default:
  11568. return (m_it.primitive_iterator == other.m_it.primitive_iterator);
  11569. }
  11570. }
  11571. /*!
  11572. @brief comparison: not equal
  11573. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11574. */
  11575. 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 >
  11576. bool operator!=(const IterImpl& other) const
  11577. {
  11578. return !operator==(other);
  11579. }
  11580. /*!
  11581. @brief comparison: smaller
  11582. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11583. */
  11584. bool operator<(const iter_impl& other) const
  11585. {
  11586. // if objects are not the same, the comparison is undefined
  11587. if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
  11588. {
  11589. JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", m_object));
  11590. }
  11591. JSON_ASSERT(m_object != nullptr);
  11592. switch (m_object->m_data.m_type)
  11593. {
  11594. case value_t::object:
  11595. JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators", m_object));
  11596. case value_t::array:
  11597. return (m_it.array_iterator < other.m_it.array_iterator);
  11598. case value_t::null:
  11599. case value_t::string:
  11600. case value_t::boolean:
  11601. case value_t::number_integer:
  11602. case value_t::number_unsigned:
  11603. case value_t::number_float:
  11604. case value_t::binary:
  11605. case value_t::discarded:
  11606. default:
  11607. return (m_it.primitive_iterator < other.m_it.primitive_iterator);
  11608. }
  11609. }
  11610. /*!
  11611. @brief comparison: less than or equal
  11612. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11613. */
  11614. bool operator<=(const iter_impl& other) const
  11615. {
  11616. return !other.operator < (*this);
  11617. }
  11618. /*!
  11619. @brief comparison: greater than
  11620. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11621. */
  11622. bool operator>(const iter_impl& other) const
  11623. {
  11624. return !operator<=(other);
  11625. }
  11626. /*!
  11627. @brief comparison: greater than or equal
  11628. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11629. */
  11630. bool operator>=(const iter_impl& other) const
  11631. {
  11632. return !operator<(other);
  11633. }
  11634. /*!
  11635. @brief add to iterator
  11636. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11637. */
  11638. iter_impl& operator+=(difference_type i)
  11639. {
  11640. JSON_ASSERT(m_object != nullptr);
  11641. switch (m_object->m_data.m_type)
  11642. {
  11643. case value_t::object:
  11644. JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", m_object));
  11645. case value_t::array:
  11646. {
  11647. std::advance(m_it.array_iterator, i);
  11648. break;
  11649. }
  11650. case value_t::null:
  11651. case value_t::string:
  11652. case value_t::boolean:
  11653. case value_t::number_integer:
  11654. case value_t::number_unsigned:
  11655. case value_t::number_float:
  11656. case value_t::binary:
  11657. case value_t::discarded:
  11658. default:
  11659. {
  11660. m_it.primitive_iterator += i;
  11661. break;
  11662. }
  11663. }
  11664. return *this;
  11665. }
  11666. /*!
  11667. @brief subtract from iterator
  11668. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11669. */
  11670. iter_impl& operator-=(difference_type i)
  11671. {
  11672. return operator+=(-i);
  11673. }
  11674. /*!
  11675. @brief add to iterator
  11676. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11677. */
  11678. iter_impl operator+(difference_type i) const
  11679. {
  11680. auto result = *this;
  11681. result += i;
  11682. return result;
  11683. }
  11684. /*!
  11685. @brief addition of distance and iterator
  11686. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11687. */
  11688. friend iter_impl operator+(difference_type i, const iter_impl& it)
  11689. {
  11690. auto result = it;
  11691. result += i;
  11692. return result;
  11693. }
  11694. /*!
  11695. @brief subtract from iterator
  11696. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11697. */
  11698. iter_impl operator-(difference_type i) const
  11699. {
  11700. auto result = *this;
  11701. result -= i;
  11702. return result;
  11703. }
  11704. /*!
  11705. @brief return difference
  11706. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11707. */
  11708. difference_type operator-(const iter_impl& other) const
  11709. {
  11710. JSON_ASSERT(m_object != nullptr);
  11711. switch (m_object->m_data.m_type)
  11712. {
  11713. case value_t::object:
  11714. JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", m_object));
  11715. case value_t::array:
  11716. return m_it.array_iterator - other.m_it.array_iterator;
  11717. case value_t::null:
  11718. case value_t::string:
  11719. case value_t::boolean:
  11720. case value_t::number_integer:
  11721. case value_t::number_unsigned:
  11722. case value_t::number_float:
  11723. case value_t::binary:
  11724. case value_t::discarded:
  11725. default:
  11726. return m_it.primitive_iterator - other.m_it.primitive_iterator;
  11727. }
  11728. }
  11729. /*!
  11730. @brief access to successor
  11731. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11732. */
  11733. reference operator[](difference_type n) const
  11734. {
  11735. JSON_ASSERT(m_object != nullptr);
  11736. switch (m_object->m_data.m_type)
  11737. {
  11738. case value_t::object:
  11739. JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators", m_object));
  11740. case value_t::array:
  11741. return *std::next(m_it.array_iterator, n);
  11742. case value_t::null:
  11743. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  11744. case value_t::string:
  11745. case value_t::boolean:
  11746. case value_t::number_integer:
  11747. case value_t::number_unsigned:
  11748. case value_t::number_float:
  11749. case value_t::binary:
  11750. case value_t::discarded:
  11751. default:
  11752. {
  11753. if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.get_value() == -n))
  11754. {
  11755. return *m_object;
  11756. }
  11757. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  11758. }
  11759. }
  11760. }
  11761. /*!
  11762. @brief return the key of an object iterator
  11763. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11764. */
  11765. const typename object_t::key_type& key() const
  11766. {
  11767. JSON_ASSERT(m_object != nullptr);
  11768. if (JSON_HEDLEY_LIKELY(m_object->is_object()))
  11769. {
  11770. return m_it.object_iterator->first;
  11771. }
  11772. JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators", m_object));
  11773. }
  11774. /*!
  11775. @brief return the value of an iterator
  11776. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11777. */
  11778. reference value() const
  11779. {
  11780. return operator*();
  11781. }
  11782. JSON_PRIVATE_UNLESS_TESTED:
  11783. /// associated JSON instance
  11784. pointer m_object = nullptr;
  11785. /// the actual iterator of the associated instance
  11786. internal_iterator<typename std::remove_const<BasicJsonType>::type> m_it {};
  11787. };
  11788. } // namespace detail
  11789. NLOHMANN_JSON_NAMESPACE_END
  11790. // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
  11791. // #include <nlohmann/detail/iterators/json_reverse_iterator.hpp>
  11792. // __ _____ _____ _____
  11793. // __| | __| | | | JSON for Modern C++
  11794. // | | |__ | | | | | | version 3.11.2
  11795. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  11796. //
  11797. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  11798. // SPDX-License-Identifier: MIT
  11799. #include <cstddef> // ptrdiff_t
  11800. #include <iterator> // reverse_iterator
  11801. #include <utility> // declval
  11802. // #include <nlohmann/detail/abi_macros.hpp>
  11803. NLOHMANN_JSON_NAMESPACE_BEGIN
  11804. namespace detail
  11805. {
  11806. //////////////////////
  11807. // reverse_iterator //
  11808. //////////////////////
  11809. /*!
  11810. @brief a template for a reverse iterator class
  11811. @tparam Base the base iterator type to reverse. Valid types are @ref
  11812. iterator (to create @ref reverse_iterator) and @ref const_iterator (to
  11813. create @ref const_reverse_iterator).
  11814. @requirement The class satisfies the following concept requirements:
  11815. -
  11816. [BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
  11817. The iterator that can be moved can be moved in both directions (i.e.
  11818. incremented and decremented).
  11819. - [OutputIterator](https://en.cppreference.com/w/cpp/named_req/OutputIterator):
  11820. It is possible to write to the pointed-to element (only if @a Base is
  11821. @ref iterator).
  11822. @since version 1.0.0
  11823. */
  11824. template<typename Base>
  11825. class json_reverse_iterator : public std::reverse_iterator<Base>
  11826. {
  11827. public:
  11828. using difference_type = std::ptrdiff_t;
  11829. /// shortcut to the reverse iterator adapter
  11830. using base_iterator = std::reverse_iterator<Base>;
  11831. /// the reference type for the pointed-to element
  11832. using reference = typename Base::reference;
  11833. /// create reverse iterator from iterator
  11834. explicit json_reverse_iterator(const typename base_iterator::iterator_type& it) noexcept
  11835. : base_iterator(it) {}
  11836. /// create reverse iterator from base class
  11837. explicit json_reverse_iterator(const base_iterator& it) noexcept : base_iterator(it) {}
  11838. /// post-increment (it++)
  11839. json_reverse_iterator operator++(int)& // NOLINT(cert-dcl21-cpp)
  11840. {
  11841. return static_cast<json_reverse_iterator>(base_iterator::operator++(1));
  11842. }
  11843. /// pre-increment (++it)
  11844. json_reverse_iterator& operator++()
  11845. {
  11846. return static_cast<json_reverse_iterator&>(base_iterator::operator++());
  11847. }
  11848. /// post-decrement (it--)
  11849. json_reverse_iterator operator--(int)& // NOLINT(cert-dcl21-cpp)
  11850. {
  11851. return static_cast<json_reverse_iterator>(base_iterator::operator--(1));
  11852. }
  11853. /// pre-decrement (--it)
  11854. json_reverse_iterator& operator--()
  11855. {
  11856. return static_cast<json_reverse_iterator&>(base_iterator::operator--());
  11857. }
  11858. /// add to iterator
  11859. json_reverse_iterator& operator+=(difference_type i)
  11860. {
  11861. return static_cast<json_reverse_iterator&>(base_iterator::operator+=(i));
  11862. }
  11863. /// add to iterator
  11864. json_reverse_iterator operator+(difference_type i) const
  11865. {
  11866. return static_cast<json_reverse_iterator>(base_iterator::operator+(i));
  11867. }
  11868. /// subtract from iterator
  11869. json_reverse_iterator operator-(difference_type i) const
  11870. {
  11871. return static_cast<json_reverse_iterator>(base_iterator::operator-(i));
  11872. }
  11873. /// return difference
  11874. difference_type operator-(const json_reverse_iterator& other) const
  11875. {
  11876. return base_iterator(*this) - base_iterator(other);
  11877. }
  11878. /// access to successor
  11879. reference operator[](difference_type n) const
  11880. {
  11881. return *(this->operator+(n));
  11882. }
  11883. /// return the key of an object iterator
  11884. auto key() const -> decltype(std::declval<Base>().key())
  11885. {
  11886. auto it = --this->base();
  11887. return it.key();
  11888. }
  11889. /// return the value of an iterator
  11890. reference value() const
  11891. {
  11892. auto it = --this->base();
  11893. return it.operator * ();
  11894. }
  11895. };
  11896. } // namespace detail
  11897. NLOHMANN_JSON_NAMESPACE_END
  11898. // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
  11899. // #include <nlohmann/detail/json_custom_base_class.hpp>
  11900. #include <type_traits> // conditional, is_same
  11901. // #include <nlohmann/detail/abi_macros.hpp>
  11902. NLOHMANN_JSON_NAMESPACE_BEGIN
  11903. namespace detail
  11904. {
  11905. /*!
  11906. @brief Default base class of the @ref basic_json class.
  11907. So that the correct implementations of the copy / move ctors / assign operators
  11908. of @ref basic_json do not require complex case distinctions
  11909. (no base class / custom base class used as customization point),
  11910. @ref basic_json always has a base class.
  11911. By default, this class is used because it is empty and thus has no effect
  11912. on the behavior of @ref basic_json.
  11913. */
  11914. struct json_default_base {};
  11915. template<class T>
  11916. using json_base_class = typename std::conditional <
  11917. std::is_same<T, void>::value,
  11918. json_default_base,
  11919. T
  11920. >::type;
  11921. } // namespace detail
  11922. NLOHMANN_JSON_NAMESPACE_END
  11923. // #include <nlohmann/detail/json_pointer.hpp>
  11924. // __ _____ _____ _____
  11925. // __| | __| | | | JSON for Modern C++
  11926. // | | |__ | | | | | | version 3.11.2
  11927. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  11928. //
  11929. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  11930. // SPDX-License-Identifier: MIT
  11931. #include <algorithm> // all_of
  11932. #include <cctype> // isdigit
  11933. #include <cerrno> // errno, ERANGE
  11934. #include <cstdlib> // strtoull
  11935. #ifndef JSON_NO_IO
  11936. #include <iosfwd> // ostream
  11937. #endif // JSON_NO_IO
  11938. #include <limits> // max
  11939. #include <numeric> // accumulate
  11940. #include <string> // string
  11941. #include <utility> // move
  11942. #include <vector> // vector
  11943. // #include <nlohmann/detail/exceptions.hpp>
  11944. // #include <nlohmann/detail/macro_scope.hpp>
  11945. // #include <nlohmann/detail/string_concat.hpp>
  11946. // #include <nlohmann/detail/string_escape.hpp>
  11947. // #include <nlohmann/detail/value_t.hpp>
  11948. NLOHMANN_JSON_NAMESPACE_BEGIN
  11949. /// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document
  11950. /// @sa https://json.nlohmann.me/api/json_pointer/
  11951. template<typename RefStringType>
  11952. class json_pointer
  11953. {
  11954. // allow basic_json to access private members
  11955. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  11956. friend class basic_json;
  11957. template<typename>
  11958. friend class json_pointer;
  11959. template<typename T>
  11960. struct string_t_helper
  11961. {
  11962. using type = T;
  11963. };
  11964. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  11965. struct string_t_helper<NLOHMANN_BASIC_JSON_TPL>
  11966. {
  11967. using type = StringType;
  11968. };
  11969. public:
  11970. // for backwards compatibility accept BasicJsonType
  11971. using string_t = typename string_t_helper<RefStringType>::type;
  11972. /// @brief create JSON pointer
  11973. /// @sa https://json.nlohmann.me/api/json_pointer/json_pointer/
  11974. explicit json_pointer(const string_t& s = "")
  11975. : reference_tokens(split(s))
  11976. {}
  11977. /// @brief return a string representation of the JSON pointer
  11978. /// @sa https://json.nlohmann.me/api/json_pointer/to_string/
  11979. string_t to_string() const
  11980. {
  11981. return std::accumulate(reference_tokens.begin(), reference_tokens.end(),
  11982. string_t{},
  11983. [](const string_t& a, const string_t& b)
  11984. {
  11985. return detail::concat(a, '/', detail::escape(b));
  11986. });
  11987. }
  11988. /// @brief return a string representation of the JSON pointer
  11989. /// @sa https://json.nlohmann.me/api/json_pointer/operator_string/
  11990. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, to_string())
  11991. operator string_t() const
  11992. {
  11993. return to_string();
  11994. }
  11995. #ifndef JSON_NO_IO
  11996. /// @brief write string representation of the JSON pointer to stream
  11997. /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
  11998. friend std::ostream& operator<<(std::ostream& o, const json_pointer& ptr)
  11999. {
  12000. o << ptr.to_string();
  12001. return o;
  12002. }
  12003. #endif
  12004. /// @brief append another JSON pointer at the end of this JSON pointer
  12005. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
  12006. json_pointer& operator/=(const json_pointer& ptr)
  12007. {
  12008. reference_tokens.insert(reference_tokens.end(),
  12009. ptr.reference_tokens.begin(),
  12010. ptr.reference_tokens.end());
  12011. return *this;
  12012. }
  12013. /// @brief append an unescaped reference token at the end of this JSON pointer
  12014. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
  12015. json_pointer& operator/=(string_t token)
  12016. {
  12017. push_back(std::move(token));
  12018. return *this;
  12019. }
  12020. /// @brief append an array index at the end of this JSON pointer
  12021. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
  12022. json_pointer& operator/=(std::size_t array_idx)
  12023. {
  12024. return *this /= std::to_string(array_idx);
  12025. }
  12026. /// @brief create a new JSON pointer by appending the right JSON pointer at the end of the left JSON pointer
  12027. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
  12028. friend json_pointer operator/(const json_pointer& lhs,
  12029. const json_pointer& rhs)
  12030. {
  12031. return json_pointer(lhs) /= rhs;
  12032. }
  12033. /// @brief create a new JSON pointer by appending the unescaped token at the end of the JSON pointer
  12034. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
  12035. friend json_pointer operator/(const json_pointer& lhs, string_t token) // NOLINT(performance-unnecessary-value-param)
  12036. {
  12037. return json_pointer(lhs) /= std::move(token);
  12038. }
  12039. /// @brief create a new JSON pointer by appending the array-index-token at the end of the JSON pointer
  12040. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
  12041. friend json_pointer operator/(const json_pointer& lhs, std::size_t array_idx)
  12042. {
  12043. return json_pointer(lhs) /= array_idx;
  12044. }
  12045. /// @brief returns the parent of this JSON pointer
  12046. /// @sa https://json.nlohmann.me/api/json_pointer/parent_pointer/
  12047. json_pointer parent_pointer() const
  12048. {
  12049. if (empty())
  12050. {
  12051. return *this;
  12052. }
  12053. json_pointer res = *this;
  12054. res.pop_back();
  12055. return res;
  12056. }
  12057. /// @brief remove last reference token
  12058. /// @sa https://json.nlohmann.me/api/json_pointer/pop_back/
  12059. void pop_back()
  12060. {
  12061. if (JSON_HEDLEY_UNLIKELY(empty()))
  12062. {
  12063. JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
  12064. }
  12065. reference_tokens.pop_back();
  12066. }
  12067. /// @brief return last reference token
  12068. /// @sa https://json.nlohmann.me/api/json_pointer/back/
  12069. const string_t& back() const
  12070. {
  12071. if (JSON_HEDLEY_UNLIKELY(empty()))
  12072. {
  12073. JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
  12074. }
  12075. return reference_tokens.back();
  12076. }
  12077. /// @brief append an unescaped token at the end of the reference pointer
  12078. /// @sa https://json.nlohmann.me/api/json_pointer/push_back/
  12079. void push_back(const string_t& token)
  12080. {
  12081. reference_tokens.push_back(token);
  12082. }
  12083. /// @brief append an unescaped token at the end of the reference pointer
  12084. /// @sa https://json.nlohmann.me/api/json_pointer/push_back/
  12085. void push_back(string_t&& token)
  12086. {
  12087. reference_tokens.push_back(std::move(token));
  12088. }
  12089. /// @brief return whether pointer points to the root document
  12090. /// @sa https://json.nlohmann.me/api/json_pointer/empty/
  12091. bool empty() const noexcept
  12092. {
  12093. return reference_tokens.empty();
  12094. }
  12095. private:
  12096. /*!
  12097. @param[in] s reference token to be converted into an array index
  12098. @return integer representation of @a s
  12099. @throw parse_error.106 if an array index begins with '0'
  12100. @throw parse_error.109 if an array index begins not with a digit
  12101. @throw out_of_range.404 if string @a s could not be converted to an integer
  12102. @throw out_of_range.410 if an array index exceeds size_type
  12103. */
  12104. template<typename BasicJsonType>
  12105. static typename BasicJsonType::size_type array_index(const string_t& s)
  12106. {
  12107. using size_type = typename BasicJsonType::size_type;
  12108. // error condition (cf. RFC 6901, Sect. 4)
  12109. if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && s[0] == '0'))
  12110. {
  12111. JSON_THROW(detail::parse_error::create(106, 0, detail::concat("array index '", s, "' must not begin with '0'"), nullptr));
  12112. }
  12113. // error condition (cf. RFC 6901, Sect. 4)
  12114. if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && !(s[0] >= '1' && s[0] <= '9')))
  12115. {
  12116. JSON_THROW(detail::parse_error::create(109, 0, detail::concat("array index '", s, "' is not a number"), nullptr));
  12117. }
  12118. const char* p = s.c_str();
  12119. char* p_end = nullptr;
  12120. errno = 0; // strtoull doesn't reset errno
  12121. const unsigned long long res = std::strtoull(p, &p_end, 10); // NOLINT(runtime/int)
  12122. if (p == p_end // invalid input or empty string
  12123. || errno == ERANGE // out of range
  12124. || JSON_HEDLEY_UNLIKELY(static_cast<std::size_t>(p_end - p) != s.size())) // incomplete read
  12125. {
  12126. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", s, "'"), nullptr));
  12127. }
  12128. // only triggered on special platforms (like 32bit), see also
  12129. // https://github.com/nlohmann/json/pull/2203
  12130. if (res >= static_cast<unsigned long long>((std::numeric_limits<size_type>::max)())) // NOLINT(runtime/int)
  12131. {
  12132. JSON_THROW(detail::out_of_range::create(410, detail::concat("array index ", s, " exceeds size_type"), nullptr)); // LCOV_EXCL_LINE
  12133. }
  12134. return static_cast<size_type>(res);
  12135. }
  12136. JSON_PRIVATE_UNLESS_TESTED:
  12137. json_pointer top() const
  12138. {
  12139. if (JSON_HEDLEY_UNLIKELY(empty()))
  12140. {
  12141. JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
  12142. }
  12143. json_pointer result = *this;
  12144. result.reference_tokens = {reference_tokens[0]};
  12145. return result;
  12146. }
  12147. private:
  12148. /*!
  12149. @brief create and return a reference to the pointed to value
  12150. @complexity Linear in the number of reference tokens.
  12151. @throw parse_error.109 if array index is not a number
  12152. @throw type_error.313 if value cannot be unflattened
  12153. */
  12154. template<typename BasicJsonType>
  12155. BasicJsonType& get_and_create(BasicJsonType& j) const
  12156. {
  12157. auto* result = &j;
  12158. // in case no reference tokens exist, return a reference to the JSON value
  12159. // j which will be overwritten by a primitive value
  12160. for (const auto& reference_token : reference_tokens)
  12161. {
  12162. switch (result->type())
  12163. {
  12164. case detail::value_t::null:
  12165. {
  12166. if (reference_token == "0")
  12167. {
  12168. // start a new array if reference token is 0
  12169. result = &result->operator[](0);
  12170. }
  12171. else
  12172. {
  12173. // start a new object otherwise
  12174. result = &result->operator[](reference_token);
  12175. }
  12176. break;
  12177. }
  12178. case detail::value_t::object:
  12179. {
  12180. // create an entry in the object
  12181. result = &result->operator[](reference_token);
  12182. break;
  12183. }
  12184. case detail::value_t::array:
  12185. {
  12186. // create an entry in the array
  12187. result = &result->operator[](array_index<BasicJsonType>(reference_token));
  12188. break;
  12189. }
  12190. /*
  12191. The following code is only reached if there exists a reference
  12192. token _and_ the current value is primitive. In this case, we have
  12193. an error situation, because primitive values may only occur as
  12194. single value; that is, with an empty list of reference tokens.
  12195. */
  12196. case detail::value_t::string:
  12197. case detail::value_t::boolean:
  12198. case detail::value_t::number_integer:
  12199. case detail::value_t::number_unsigned:
  12200. case detail::value_t::number_float:
  12201. case detail::value_t::binary:
  12202. case detail::value_t::discarded:
  12203. default:
  12204. JSON_THROW(detail::type_error::create(313, "invalid value to unflatten", &j));
  12205. }
  12206. }
  12207. return *result;
  12208. }
  12209. /*!
  12210. @brief return a reference to the pointed to value
  12211. @note This version does not throw if a value is not present, but tries to
  12212. create nested values instead. For instance, calling this function
  12213. with pointer `"/this/that"` on a null value is equivalent to calling
  12214. `operator[]("this").operator[]("that")` on that value, effectively
  12215. changing the null value to an object.
  12216. @param[in] ptr a JSON value
  12217. @return reference to the JSON value pointed to by the JSON pointer
  12218. @complexity Linear in the length of the JSON pointer.
  12219. @throw parse_error.106 if an array index begins with '0'
  12220. @throw parse_error.109 if an array index was not a number
  12221. @throw out_of_range.404 if the JSON pointer can not be resolved
  12222. */
  12223. template<typename BasicJsonType>
  12224. BasicJsonType& get_unchecked(BasicJsonType* ptr) const
  12225. {
  12226. for (const auto& reference_token : reference_tokens)
  12227. {
  12228. // convert null values to arrays or objects before continuing
  12229. if (ptr->is_null())
  12230. {
  12231. // check if reference token is a number
  12232. const bool nums =
  12233. std::all_of(reference_token.begin(), reference_token.end(),
  12234. [](const unsigned char x)
  12235. {
  12236. return std::isdigit(x);
  12237. });
  12238. // change value to array for numbers or "-" or to object otherwise
  12239. *ptr = (nums || reference_token == "-")
  12240. ? detail::value_t::array
  12241. : detail::value_t::object;
  12242. }
  12243. switch (ptr->type())
  12244. {
  12245. case detail::value_t::object:
  12246. {
  12247. // use unchecked object access
  12248. ptr = &ptr->operator[](reference_token);
  12249. break;
  12250. }
  12251. case detail::value_t::array:
  12252. {
  12253. if (reference_token == "-")
  12254. {
  12255. // explicitly treat "-" as index beyond the end
  12256. ptr = &ptr->operator[](ptr->m_data.m_value.array->size());
  12257. }
  12258. else
  12259. {
  12260. // convert array index to number; unchecked access
  12261. ptr = &ptr->operator[](array_index<BasicJsonType>(reference_token));
  12262. }
  12263. break;
  12264. }
  12265. case detail::value_t::null:
  12266. case detail::value_t::string:
  12267. case detail::value_t::boolean:
  12268. case detail::value_t::number_integer:
  12269. case detail::value_t::number_unsigned:
  12270. case detail::value_t::number_float:
  12271. case detail::value_t::binary:
  12272. case detail::value_t::discarded:
  12273. default:
  12274. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
  12275. }
  12276. }
  12277. return *ptr;
  12278. }
  12279. /*!
  12280. @throw parse_error.106 if an array index begins with '0'
  12281. @throw parse_error.109 if an array index was not a number
  12282. @throw out_of_range.402 if the array index '-' is used
  12283. @throw out_of_range.404 if the JSON pointer can not be resolved
  12284. */
  12285. template<typename BasicJsonType>
  12286. BasicJsonType& get_checked(BasicJsonType* ptr) const
  12287. {
  12288. for (const auto& reference_token : reference_tokens)
  12289. {
  12290. switch (ptr->type())
  12291. {
  12292. case detail::value_t::object:
  12293. {
  12294. // note: at performs range check
  12295. ptr = &ptr->at(reference_token);
  12296. break;
  12297. }
  12298. case detail::value_t::array:
  12299. {
  12300. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  12301. {
  12302. // "-" always fails the range check
  12303. JSON_THROW(detail::out_of_range::create(402, detail::concat(
  12304. "array index '-' (", std::to_string(ptr->m_data.m_value.array->size()),
  12305. ") is out of range"), ptr));
  12306. }
  12307. // note: at performs range check
  12308. ptr = &ptr->at(array_index<BasicJsonType>(reference_token));
  12309. break;
  12310. }
  12311. case detail::value_t::null:
  12312. case detail::value_t::string:
  12313. case detail::value_t::boolean:
  12314. case detail::value_t::number_integer:
  12315. case detail::value_t::number_unsigned:
  12316. case detail::value_t::number_float:
  12317. case detail::value_t::binary:
  12318. case detail::value_t::discarded:
  12319. default:
  12320. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
  12321. }
  12322. }
  12323. return *ptr;
  12324. }
  12325. /*!
  12326. @brief return a const reference to the pointed to value
  12327. @param[in] ptr a JSON value
  12328. @return const reference to the JSON value pointed to by the JSON
  12329. pointer
  12330. @throw parse_error.106 if an array index begins with '0'
  12331. @throw parse_error.109 if an array index was not a number
  12332. @throw out_of_range.402 if the array index '-' is used
  12333. @throw out_of_range.404 if the JSON pointer can not be resolved
  12334. */
  12335. template<typename BasicJsonType>
  12336. const BasicJsonType& get_unchecked(const BasicJsonType* ptr) const
  12337. {
  12338. for (const auto& reference_token : reference_tokens)
  12339. {
  12340. switch (ptr->type())
  12341. {
  12342. case detail::value_t::object:
  12343. {
  12344. // use unchecked object access
  12345. ptr = &ptr->operator[](reference_token);
  12346. break;
  12347. }
  12348. case detail::value_t::array:
  12349. {
  12350. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  12351. {
  12352. // "-" cannot be used for const access
  12353. 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));
  12354. }
  12355. // use unchecked array access
  12356. ptr = &ptr->operator[](array_index<BasicJsonType>(reference_token));
  12357. break;
  12358. }
  12359. case detail::value_t::null:
  12360. case detail::value_t::string:
  12361. case detail::value_t::boolean:
  12362. case detail::value_t::number_integer:
  12363. case detail::value_t::number_unsigned:
  12364. case detail::value_t::number_float:
  12365. case detail::value_t::binary:
  12366. case detail::value_t::discarded:
  12367. default:
  12368. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
  12369. }
  12370. }
  12371. return *ptr;
  12372. }
  12373. /*!
  12374. @throw parse_error.106 if an array index begins with '0'
  12375. @throw parse_error.109 if an array index was not a number
  12376. @throw out_of_range.402 if the array index '-' is used
  12377. @throw out_of_range.404 if the JSON pointer can not be resolved
  12378. */
  12379. template<typename BasicJsonType>
  12380. const BasicJsonType& get_checked(const BasicJsonType* ptr) const
  12381. {
  12382. for (const auto& reference_token : reference_tokens)
  12383. {
  12384. switch (ptr->type())
  12385. {
  12386. case detail::value_t::object:
  12387. {
  12388. // note: at performs range check
  12389. ptr = &ptr->at(reference_token);
  12390. break;
  12391. }
  12392. case detail::value_t::array:
  12393. {
  12394. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  12395. {
  12396. // "-" always fails the range check
  12397. JSON_THROW(detail::out_of_range::create(402, detail::concat(
  12398. "array index '-' (", std::to_string(ptr->m_data.m_value.array->size()),
  12399. ") is out of range"), ptr));
  12400. }
  12401. // note: at performs range check
  12402. ptr = &ptr->at(array_index<BasicJsonType>(reference_token));
  12403. break;
  12404. }
  12405. case detail::value_t::null:
  12406. case detail::value_t::string:
  12407. case detail::value_t::boolean:
  12408. case detail::value_t::number_integer:
  12409. case detail::value_t::number_unsigned:
  12410. case detail::value_t::number_float:
  12411. case detail::value_t::binary:
  12412. case detail::value_t::discarded:
  12413. default:
  12414. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
  12415. }
  12416. }
  12417. return *ptr;
  12418. }
  12419. /*!
  12420. @throw parse_error.106 if an array index begins with '0'
  12421. @throw parse_error.109 if an array index was not a number
  12422. */
  12423. template<typename BasicJsonType>
  12424. bool contains(const BasicJsonType* ptr) const
  12425. {
  12426. for (const auto& reference_token : reference_tokens)
  12427. {
  12428. switch (ptr->type())
  12429. {
  12430. case detail::value_t::object:
  12431. {
  12432. if (!ptr->contains(reference_token))
  12433. {
  12434. // we did not find the key in the object
  12435. return false;
  12436. }
  12437. ptr = &ptr->operator[](reference_token);
  12438. break;
  12439. }
  12440. case detail::value_t::array:
  12441. {
  12442. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  12443. {
  12444. // "-" always fails the range check
  12445. return false;
  12446. }
  12447. if (JSON_HEDLEY_UNLIKELY(reference_token.size() == 1 && !("0" <= reference_token && reference_token <= "9")))
  12448. {
  12449. // invalid char
  12450. return false;
  12451. }
  12452. if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1))
  12453. {
  12454. if (JSON_HEDLEY_UNLIKELY(!('1' <= reference_token[0] && reference_token[0] <= '9')))
  12455. {
  12456. // first char should be between '1' and '9'
  12457. return false;
  12458. }
  12459. for (std::size_t i = 1; i < reference_token.size(); i++)
  12460. {
  12461. if (JSON_HEDLEY_UNLIKELY(!('0' <= reference_token[i] && reference_token[i] <= '9')))
  12462. {
  12463. // other char should be between '0' and '9'
  12464. return false;
  12465. }
  12466. }
  12467. }
  12468. const auto idx = array_index<BasicJsonType>(reference_token);
  12469. if (idx >= ptr->size())
  12470. {
  12471. // index out of range
  12472. return false;
  12473. }
  12474. ptr = &ptr->operator[](idx);
  12475. break;
  12476. }
  12477. case detail::value_t::null:
  12478. case detail::value_t::string:
  12479. case detail::value_t::boolean:
  12480. case detail::value_t::number_integer:
  12481. case detail::value_t::number_unsigned:
  12482. case detail::value_t::number_float:
  12483. case detail::value_t::binary:
  12484. case detail::value_t::discarded:
  12485. default:
  12486. {
  12487. // we do not expect primitive values if there is still a
  12488. // reference token to process
  12489. return false;
  12490. }
  12491. }
  12492. }
  12493. // no reference token left means we found a primitive value
  12494. return true;
  12495. }
  12496. /*!
  12497. @brief split the string input to reference tokens
  12498. @note This function is only called by the json_pointer constructor.
  12499. All exceptions below are documented there.
  12500. @throw parse_error.107 if the pointer is not empty or begins with '/'
  12501. @throw parse_error.108 if character '~' is not followed by '0' or '1'
  12502. */
  12503. static std::vector<string_t> split(const string_t& reference_string)
  12504. {
  12505. std::vector<string_t> result;
  12506. // special case: empty reference string -> no reference tokens
  12507. if (reference_string.empty())
  12508. {
  12509. return result;
  12510. }
  12511. // check if nonempty reference string begins with slash
  12512. if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/'))
  12513. {
  12514. JSON_THROW(detail::parse_error::create(107, 1, detail::concat("JSON pointer must be empty or begin with '/' - was: '", reference_string, "'"), nullptr));
  12515. }
  12516. // extract the reference tokens:
  12517. // - slash: position of the last read slash (or end of string)
  12518. // - start: position after the previous slash
  12519. for (
  12520. // search for the first slash after the first character
  12521. std::size_t slash = reference_string.find_first_of('/', 1),
  12522. // set the beginning of the first reference token
  12523. start = 1;
  12524. // we can stop if start == 0 (if slash == string_t::npos)
  12525. start != 0;
  12526. // set the beginning of the next reference token
  12527. // (will eventually be 0 if slash == string_t::npos)
  12528. start = (slash == string_t::npos) ? 0 : slash + 1,
  12529. // find next slash
  12530. slash = reference_string.find_first_of('/', start))
  12531. {
  12532. // use the text between the beginning of the reference token
  12533. // (start) and the last slash (slash).
  12534. auto reference_token = reference_string.substr(start, slash - start);
  12535. // check reference tokens are properly escaped
  12536. for (std::size_t pos = reference_token.find_first_of('~');
  12537. pos != string_t::npos;
  12538. pos = reference_token.find_first_of('~', pos + 1))
  12539. {
  12540. JSON_ASSERT(reference_token[pos] == '~');
  12541. // ~ must be followed by 0 or 1
  12542. if (JSON_HEDLEY_UNLIKELY(pos == reference_token.size() - 1 ||
  12543. (reference_token[pos + 1] != '0' &&
  12544. reference_token[pos + 1] != '1')))
  12545. {
  12546. JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'", nullptr));
  12547. }
  12548. }
  12549. // finally, store the reference token
  12550. detail::unescape(reference_token);
  12551. result.push_back(reference_token);
  12552. }
  12553. return result;
  12554. }
  12555. private:
  12556. /*!
  12557. @param[in] reference_string the reference string to the current value
  12558. @param[in] value the value to consider
  12559. @param[in,out] result the result object to insert values to
  12560. @note Empty objects or arrays are flattened to `null`.
  12561. */
  12562. template<typename BasicJsonType>
  12563. static void flatten(const string_t& reference_string,
  12564. const BasicJsonType& value,
  12565. BasicJsonType& result)
  12566. {
  12567. switch (value.type())
  12568. {
  12569. case detail::value_t::array:
  12570. {
  12571. if (value.m_data.m_value.array->empty())
  12572. {
  12573. // flatten empty array as null
  12574. result[reference_string] = nullptr;
  12575. }
  12576. else
  12577. {
  12578. // iterate array and use index as reference string
  12579. for (std::size_t i = 0; i < value.m_data.m_value.array->size(); ++i)
  12580. {
  12581. flatten(detail::concat(reference_string, '/', std::to_string(i)),
  12582. value.m_data.m_value.array->operator[](i), result);
  12583. }
  12584. }
  12585. break;
  12586. }
  12587. case detail::value_t::object:
  12588. {
  12589. if (value.m_data.m_value.object->empty())
  12590. {
  12591. // flatten empty object as null
  12592. result[reference_string] = nullptr;
  12593. }
  12594. else
  12595. {
  12596. // iterate object and use keys as reference string
  12597. for (const auto& element : *value.m_data.m_value.object)
  12598. {
  12599. flatten(detail::concat(reference_string, '/', detail::escape(element.first)), element.second, result);
  12600. }
  12601. }
  12602. break;
  12603. }
  12604. case detail::value_t::null:
  12605. case detail::value_t::string:
  12606. case detail::value_t::boolean:
  12607. case detail::value_t::number_integer:
  12608. case detail::value_t::number_unsigned:
  12609. case detail::value_t::number_float:
  12610. case detail::value_t::binary:
  12611. case detail::value_t::discarded:
  12612. default:
  12613. {
  12614. // add primitive value with its reference string
  12615. result[reference_string] = value;
  12616. break;
  12617. }
  12618. }
  12619. }
  12620. /*!
  12621. @param[in] value flattened JSON
  12622. @return unflattened JSON
  12623. @throw parse_error.109 if array index is not a number
  12624. @throw type_error.314 if value is not an object
  12625. @throw type_error.315 if object values are not primitive
  12626. @throw type_error.313 if value cannot be unflattened
  12627. */
  12628. template<typename BasicJsonType>
  12629. static BasicJsonType
  12630. unflatten(const BasicJsonType& value)
  12631. {
  12632. if (JSON_HEDLEY_UNLIKELY(!value.is_object()))
  12633. {
  12634. JSON_THROW(detail::type_error::create(314, "only objects can be unflattened", &value));
  12635. }
  12636. BasicJsonType result;
  12637. // iterate the JSON object values
  12638. for (const auto& element : *value.m_data.m_value.object)
  12639. {
  12640. if (JSON_HEDLEY_UNLIKELY(!element.second.is_primitive()))
  12641. {
  12642. JSON_THROW(detail::type_error::create(315, "values in object must be primitive", &element.second));
  12643. }
  12644. // assign value to reference pointed to by JSON pointer; Note that if
  12645. // the JSON pointer is "" (i.e., points to the whole value), function
  12646. // get_and_create returns a reference to result itself. An assignment
  12647. // will then create a primitive value.
  12648. json_pointer(element.first).get_and_create(result) = element.second;
  12649. }
  12650. return result;
  12651. }
  12652. // can't use conversion operator because of ambiguity
  12653. json_pointer<string_t> convert() const&
  12654. {
  12655. json_pointer<string_t> result;
  12656. result.reference_tokens = reference_tokens;
  12657. return result;
  12658. }
  12659. json_pointer<string_t> convert()&&
  12660. {
  12661. json_pointer<string_t> result;
  12662. result.reference_tokens = std::move(reference_tokens);
  12663. return result;
  12664. }
  12665. public:
  12666. #if JSON_HAS_THREE_WAY_COMPARISON
  12667. /// @brief compares two JSON pointers for equality
  12668. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  12669. template<typename RefStringTypeRhs>
  12670. bool operator==(const json_pointer<RefStringTypeRhs>& rhs) const noexcept
  12671. {
  12672. return reference_tokens == rhs.reference_tokens;
  12673. }
  12674. /// @brief compares JSON pointer and string for equality
  12675. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  12676. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer))
  12677. bool operator==(const string_t& rhs) const
  12678. {
  12679. return *this == json_pointer(rhs);
  12680. }
  12681. /// @brief 3-way compares two JSON pointers
  12682. template<typename RefStringTypeRhs>
  12683. std::strong_ordering operator<=>(const json_pointer<RefStringTypeRhs>& rhs) const noexcept // *NOPAD*
  12684. {
  12685. return reference_tokens <=> rhs.reference_tokens; // *NOPAD*
  12686. }
  12687. #else
  12688. /// @brief compares two JSON pointers for equality
  12689. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  12690. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  12691. // NOLINTNEXTLINE(readability-redundant-declaration)
  12692. friend bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
  12693. const json_pointer<RefStringTypeRhs>& rhs) noexcept;
  12694. /// @brief compares JSON pointer and string for equality
  12695. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  12696. template<typename RefStringTypeLhs, typename StringType>
  12697. // NOLINTNEXTLINE(readability-redundant-declaration)
  12698. friend bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
  12699. const StringType& rhs);
  12700. /// @brief compares string and JSON pointer for equality
  12701. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  12702. template<typename RefStringTypeRhs, typename StringType>
  12703. // NOLINTNEXTLINE(readability-redundant-declaration)
  12704. friend bool operator==(const StringType& lhs,
  12705. const json_pointer<RefStringTypeRhs>& rhs);
  12706. /// @brief compares two JSON pointers for inequality
  12707. /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/
  12708. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  12709. // NOLINTNEXTLINE(readability-redundant-declaration)
  12710. friend bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
  12711. const json_pointer<RefStringTypeRhs>& rhs) noexcept;
  12712. /// @brief compares JSON pointer and string for inequality
  12713. /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/
  12714. template<typename RefStringTypeLhs, typename StringType>
  12715. // NOLINTNEXTLINE(readability-redundant-declaration)
  12716. friend bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
  12717. const StringType& rhs);
  12718. /// @brief compares string and JSON pointer for inequality
  12719. /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/
  12720. template<typename RefStringTypeRhs, typename StringType>
  12721. // NOLINTNEXTLINE(readability-redundant-declaration)
  12722. friend bool operator!=(const StringType& lhs,
  12723. const json_pointer<RefStringTypeRhs>& rhs);
  12724. /// @brief compares two JSON pointer for less-than
  12725. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  12726. // NOLINTNEXTLINE(readability-redundant-declaration)
  12727. friend bool operator<(const json_pointer<RefStringTypeLhs>& lhs,
  12728. const json_pointer<RefStringTypeRhs>& rhs) noexcept;
  12729. #endif
  12730. private:
  12731. /// the reference tokens
  12732. std::vector<string_t> reference_tokens;
  12733. };
  12734. #if !JSON_HAS_THREE_WAY_COMPARISON
  12735. // functions cannot be defined inside class due to ODR violations
  12736. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  12737. inline bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
  12738. const json_pointer<RefStringTypeRhs>& rhs) noexcept
  12739. {
  12740. return lhs.reference_tokens == rhs.reference_tokens;
  12741. }
  12742. template<typename RefStringTypeLhs,
  12743. typename StringType = typename json_pointer<RefStringTypeLhs>::string_t>
  12744. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer, json_pointer))
  12745. inline bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
  12746. const StringType& rhs)
  12747. {
  12748. return lhs == json_pointer<RefStringTypeLhs>(rhs);
  12749. }
  12750. template<typename RefStringTypeRhs,
  12751. typename StringType = typename json_pointer<RefStringTypeRhs>::string_t>
  12752. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer, json_pointer))
  12753. inline bool operator==(const StringType& lhs,
  12754. const json_pointer<RefStringTypeRhs>& rhs)
  12755. {
  12756. return json_pointer<RefStringTypeRhs>(lhs) == rhs;
  12757. }
  12758. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  12759. inline bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
  12760. const json_pointer<RefStringTypeRhs>& rhs) noexcept
  12761. {
  12762. return !(lhs == rhs);
  12763. }
  12764. template<typename RefStringTypeLhs,
  12765. typename StringType = typename json_pointer<RefStringTypeLhs>::string_t>
  12766. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator!=(json_pointer, json_pointer))
  12767. inline bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
  12768. const StringType& rhs)
  12769. {
  12770. return !(lhs == rhs);
  12771. }
  12772. template<typename RefStringTypeRhs,
  12773. typename StringType = typename json_pointer<RefStringTypeRhs>::string_t>
  12774. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator!=(json_pointer, json_pointer))
  12775. inline bool operator!=(const StringType& lhs,
  12776. const json_pointer<RefStringTypeRhs>& rhs)
  12777. {
  12778. return !(lhs == rhs);
  12779. }
  12780. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  12781. inline bool operator<(const json_pointer<RefStringTypeLhs>& lhs,
  12782. const json_pointer<RefStringTypeRhs>& rhs) noexcept
  12783. {
  12784. return lhs.reference_tokens < rhs.reference_tokens;
  12785. }
  12786. #endif
  12787. NLOHMANN_JSON_NAMESPACE_END
  12788. // #include <nlohmann/detail/json_ref.hpp>
  12789. // __ _____ _____ _____
  12790. // __| | __| | | | JSON for Modern C++
  12791. // | | |__ | | | | | | version 3.11.2
  12792. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  12793. //
  12794. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  12795. // SPDX-License-Identifier: MIT
  12796. #include <initializer_list>
  12797. #include <utility>
  12798. // #include <nlohmann/detail/abi_macros.hpp>
  12799. // #include <nlohmann/detail/meta/type_traits.hpp>
  12800. NLOHMANN_JSON_NAMESPACE_BEGIN
  12801. namespace detail
  12802. {
  12803. template<typename BasicJsonType>
  12804. class json_ref
  12805. {
  12806. public:
  12807. using value_type = BasicJsonType;
  12808. json_ref(value_type&& value)
  12809. : owned_value(std::move(value))
  12810. {}
  12811. json_ref(const value_type& value)
  12812. : value_ref(&value)
  12813. {}
  12814. json_ref(std::initializer_list<json_ref> init)
  12815. : owned_value(init)
  12816. {}
  12817. template <
  12818. class... Args,
  12819. enable_if_t<std::is_constructible<value_type, Args...>::value, int> = 0 >
  12820. json_ref(Args && ... args)
  12821. : owned_value(std::forward<Args>(args)...)
  12822. {}
  12823. // class should be movable only
  12824. json_ref(json_ref&&) noexcept = default;
  12825. json_ref(const json_ref&) = delete;
  12826. json_ref& operator=(const json_ref&) = delete;
  12827. json_ref& operator=(json_ref&&) = delete;
  12828. ~json_ref() = default;
  12829. value_type moved_or_copied() const
  12830. {
  12831. if (value_ref == nullptr)
  12832. {
  12833. return std::move(owned_value);
  12834. }
  12835. return *value_ref;
  12836. }
  12837. value_type const& operator*() const
  12838. {
  12839. return value_ref ? *value_ref : owned_value;
  12840. }
  12841. value_type const* operator->() const
  12842. {
  12843. return &** this;
  12844. }
  12845. private:
  12846. mutable value_type owned_value = nullptr;
  12847. value_type const* value_ref = nullptr;
  12848. };
  12849. } // namespace detail
  12850. NLOHMANN_JSON_NAMESPACE_END
  12851. // #include <nlohmann/detail/macro_scope.hpp>
  12852. // #include <nlohmann/detail/string_concat.hpp>
  12853. // #include <nlohmann/detail/string_escape.hpp>
  12854. // #include <nlohmann/detail/meta/cpp_future.hpp>
  12855. // #include <nlohmann/detail/meta/type_traits.hpp>
  12856. // #include <nlohmann/detail/output/binary_writer.hpp>
  12857. // __ _____ _____ _____
  12858. // __| | __| | | | JSON for Modern C++
  12859. // | | |__ | | | | | | version 3.11.2
  12860. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  12861. //
  12862. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  12863. // SPDX-License-Identifier: MIT
  12864. #include <algorithm> // reverse
  12865. #include <array> // array
  12866. #include <map> // map
  12867. #include <cmath> // isnan, isinf
  12868. #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
  12869. #include <cstring> // memcpy
  12870. #include <limits> // numeric_limits
  12871. #include <string> // string
  12872. #include <utility> // move
  12873. #include <vector> // vector
  12874. // #include <nlohmann/detail/input/binary_reader.hpp>
  12875. // #include <nlohmann/detail/macro_scope.hpp>
  12876. // #include <nlohmann/detail/output/output_adapters.hpp>
  12877. // __ _____ _____ _____
  12878. // __| | __| | | | JSON for Modern C++
  12879. // | | |__ | | | | | | version 3.11.2
  12880. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  12881. //
  12882. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  12883. // SPDX-License-Identifier: MIT
  12884. #include <algorithm> // copy
  12885. #include <cstddef> // size_t
  12886. #include <iterator> // back_inserter
  12887. #include <memory> // shared_ptr, make_shared
  12888. #include <string> // basic_string
  12889. #include <vector> // vector
  12890. #ifndef JSON_NO_IO
  12891. #include <ios> // streamsize
  12892. #include <ostream> // basic_ostream
  12893. #endif // JSON_NO_IO
  12894. // #include <nlohmann/detail/macro_scope.hpp>
  12895. NLOHMANN_JSON_NAMESPACE_BEGIN
  12896. namespace detail
  12897. {
  12898. /// abstract output adapter interface
  12899. template<typename CharType> struct output_adapter_protocol
  12900. {
  12901. virtual void write_character(CharType c) = 0;
  12902. virtual void write_characters(const CharType* s, std::size_t length) = 0;
  12903. virtual ~output_adapter_protocol() = default;
  12904. output_adapter_protocol() = default;
  12905. output_adapter_protocol(const output_adapter_protocol&) = default;
  12906. output_adapter_protocol(output_adapter_protocol&&) noexcept = default;
  12907. output_adapter_protocol& operator=(const output_adapter_protocol&) = default;
  12908. output_adapter_protocol& operator=(output_adapter_protocol&&) noexcept = default;
  12909. };
  12910. /// a type to simplify interfaces
  12911. template<typename CharType>
  12912. using output_adapter_t = std::shared_ptr<output_adapter_protocol<CharType>>;
  12913. /// output adapter for byte vectors
  12914. template<typename CharType, typename AllocatorType = std::allocator<CharType>>
  12915. class output_vector_adapter : public output_adapter_protocol<CharType>
  12916. {
  12917. public:
  12918. explicit output_vector_adapter(std::vector<CharType, AllocatorType>& vec) noexcept
  12919. : v(vec)
  12920. {}
  12921. void write_character(CharType c) override
  12922. {
  12923. v.push_back(c);
  12924. }
  12925. JSON_HEDLEY_NON_NULL(2)
  12926. void write_characters(const CharType* s, std::size_t length) override
  12927. {
  12928. v.insert(v.end(), s, s + length);
  12929. }
  12930. private:
  12931. std::vector<CharType, AllocatorType>& v;
  12932. };
  12933. #ifndef JSON_NO_IO
  12934. /// output adapter for output streams
  12935. template<typename CharType>
  12936. class output_stream_adapter : public output_adapter_protocol<CharType>
  12937. {
  12938. public:
  12939. explicit output_stream_adapter(std::basic_ostream<CharType>& s) noexcept
  12940. : stream(s)
  12941. {}
  12942. void write_character(CharType c) override
  12943. {
  12944. stream.put(c);
  12945. }
  12946. JSON_HEDLEY_NON_NULL(2)
  12947. void write_characters(const CharType* s, std::size_t length) override
  12948. {
  12949. stream.write(s, static_cast<std::streamsize>(length));
  12950. }
  12951. private:
  12952. std::basic_ostream<CharType>& stream;
  12953. };
  12954. #endif // JSON_NO_IO
  12955. /// output adapter for basic_string
  12956. template<typename CharType, typename StringType = std::basic_string<CharType>>
  12957. class output_string_adapter : public output_adapter_protocol<CharType>
  12958. {
  12959. public:
  12960. explicit output_string_adapter(StringType& s) noexcept
  12961. : str(s)
  12962. {}
  12963. void write_character(CharType c) override
  12964. {
  12965. str.push_back(c);
  12966. }
  12967. JSON_HEDLEY_NON_NULL(2)
  12968. void write_characters(const CharType* s, std::size_t length) override
  12969. {
  12970. str.append(s, length);
  12971. }
  12972. private:
  12973. StringType& str;
  12974. };
  12975. template<typename CharType, typename StringType = std::basic_string<CharType>>
  12976. class output_adapter
  12977. {
  12978. public:
  12979. template<typename AllocatorType = std::allocator<CharType>>
  12980. output_adapter(std::vector<CharType, AllocatorType>& vec)
  12981. : oa(std::make_shared<output_vector_adapter<CharType, AllocatorType>>(vec)) {}
  12982. #ifndef JSON_NO_IO
  12983. output_adapter(std::basic_ostream<CharType>& s)
  12984. : oa(std::make_shared<output_stream_adapter<CharType>>(s)) {}
  12985. #endif // JSON_NO_IO
  12986. output_adapter(StringType& s)
  12987. : oa(std::make_shared<output_string_adapter<CharType, StringType>>(s)) {}
  12988. operator output_adapter_t<CharType>()
  12989. {
  12990. return oa;
  12991. }
  12992. private:
  12993. output_adapter_t<CharType> oa = nullptr;
  12994. };
  12995. } // namespace detail
  12996. NLOHMANN_JSON_NAMESPACE_END
  12997. // #include <nlohmann/detail/string_concat.hpp>
  12998. NLOHMANN_JSON_NAMESPACE_BEGIN
  12999. namespace detail
  13000. {
  13001. ///////////////////
  13002. // binary writer //
  13003. ///////////////////
  13004. /*!
  13005. @brief serialization to CBOR and MessagePack values
  13006. */
  13007. template<typename BasicJsonType, typename CharType>
  13008. class binary_writer
  13009. {
  13010. using string_t = typename BasicJsonType::string_t;
  13011. using binary_t = typename BasicJsonType::binary_t;
  13012. using number_float_t = typename BasicJsonType::number_float_t;
  13013. public:
  13014. /*!
  13015. @brief create a binary writer
  13016. @param[in] adapter output adapter to write to
  13017. */
  13018. explicit binary_writer(output_adapter_t<CharType> adapter) : oa(std::move(adapter))
  13019. {
  13020. JSON_ASSERT(oa);
  13021. }
  13022. /*!
  13023. @param[in] j JSON value to serialize
  13024. @pre j.type() == value_t::object
  13025. */
  13026. void write_bson(const BasicJsonType& j)
  13027. {
  13028. switch (j.type())
  13029. {
  13030. case value_t::object:
  13031. {
  13032. write_bson_object(*j.m_data.m_value.object);
  13033. break;
  13034. }
  13035. case value_t::null:
  13036. case value_t::array:
  13037. case value_t::string:
  13038. case value_t::boolean:
  13039. case value_t::number_integer:
  13040. case value_t::number_unsigned:
  13041. case value_t::number_float:
  13042. case value_t::binary:
  13043. case value_t::discarded:
  13044. default:
  13045. {
  13046. JSON_THROW(type_error::create(317, concat("to serialize to BSON, top-level type must be object, but is ", j.type_name()), &j));
  13047. }
  13048. }
  13049. }
  13050. /*!
  13051. @param[in] j JSON value to serialize
  13052. */
  13053. void write_cbor(const BasicJsonType& j)
  13054. {
  13055. switch (j.type())
  13056. {
  13057. case value_t::null:
  13058. {
  13059. oa->write_character(to_char_type(0xF6));
  13060. break;
  13061. }
  13062. case value_t::boolean:
  13063. {
  13064. oa->write_character(j.m_data.m_value.boolean
  13065. ? to_char_type(0xF5)
  13066. : to_char_type(0xF4));
  13067. break;
  13068. }
  13069. case value_t::number_integer:
  13070. {
  13071. if (j.m_data.m_value.number_integer >= 0)
  13072. {
  13073. // CBOR does not differentiate between positive signed
  13074. // integers and unsigned integers. Therefore, we used the
  13075. // code from the value_t::number_unsigned case here.
  13076. if (j.m_data.m_value.number_integer <= 0x17)
  13077. {
  13078. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  13079. }
  13080. else if (j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint8_t>::max)())
  13081. {
  13082. oa->write_character(to_char_type(0x18));
  13083. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  13084. }
  13085. else if (j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint16_t>::max)())
  13086. {
  13087. oa->write_character(to_char_type(0x19));
  13088. write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_integer));
  13089. }
  13090. else if (j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint32_t>::max)())
  13091. {
  13092. oa->write_character(to_char_type(0x1A));
  13093. write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_integer));
  13094. }
  13095. else
  13096. {
  13097. oa->write_character(to_char_type(0x1B));
  13098. write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_integer));
  13099. }
  13100. }
  13101. else
  13102. {
  13103. // The conversions below encode the sign in the first
  13104. // byte, and the value is converted to a positive number.
  13105. const auto positive_number = -1 - j.m_data.m_value.number_integer;
  13106. if (j.m_data.m_value.number_integer >= -24)
  13107. {
  13108. write_number(static_cast<std::uint8_t>(0x20 + positive_number));
  13109. }
  13110. else if (positive_number <= (std::numeric_limits<std::uint8_t>::max)())
  13111. {
  13112. oa->write_character(to_char_type(0x38));
  13113. write_number(static_cast<std::uint8_t>(positive_number));
  13114. }
  13115. else if (positive_number <= (std::numeric_limits<std::uint16_t>::max)())
  13116. {
  13117. oa->write_character(to_char_type(0x39));
  13118. write_number(static_cast<std::uint16_t>(positive_number));
  13119. }
  13120. else if (positive_number <= (std::numeric_limits<std::uint32_t>::max)())
  13121. {
  13122. oa->write_character(to_char_type(0x3A));
  13123. write_number(static_cast<std::uint32_t>(positive_number));
  13124. }
  13125. else
  13126. {
  13127. oa->write_character(to_char_type(0x3B));
  13128. write_number(static_cast<std::uint64_t>(positive_number));
  13129. }
  13130. }
  13131. break;
  13132. }
  13133. case value_t::number_unsigned:
  13134. {
  13135. if (j.m_data.m_value.number_unsigned <= 0x17)
  13136. {
  13137. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_unsigned));
  13138. }
  13139. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
  13140. {
  13141. oa->write_character(to_char_type(0x18));
  13142. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_unsigned));
  13143. }
  13144. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
  13145. {
  13146. oa->write_character(to_char_type(0x19));
  13147. write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_unsigned));
  13148. }
  13149. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
  13150. {
  13151. oa->write_character(to_char_type(0x1A));
  13152. write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_unsigned));
  13153. }
  13154. else
  13155. {
  13156. oa->write_character(to_char_type(0x1B));
  13157. write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_unsigned));
  13158. }
  13159. break;
  13160. }
  13161. case value_t::number_float:
  13162. {
  13163. if (std::isnan(j.m_data.m_value.number_float))
  13164. {
  13165. // NaN is 0xf97e00 in CBOR
  13166. oa->write_character(to_char_type(0xF9));
  13167. oa->write_character(to_char_type(0x7E));
  13168. oa->write_character(to_char_type(0x00));
  13169. }
  13170. else if (std::isinf(j.m_data.m_value.number_float))
  13171. {
  13172. // Infinity is 0xf97c00, -Infinity is 0xf9fc00
  13173. oa->write_character(to_char_type(0xf9));
  13174. oa->write_character(j.m_data.m_value.number_float > 0 ? to_char_type(0x7C) : to_char_type(0xFC));
  13175. oa->write_character(to_char_type(0x00));
  13176. }
  13177. else
  13178. {
  13179. write_compact_float(j.m_data.m_value.number_float, detail::input_format_t::cbor);
  13180. }
  13181. break;
  13182. }
  13183. case value_t::string:
  13184. {
  13185. // step 1: write control byte and the string length
  13186. const auto N = j.m_data.m_value.string->size();
  13187. if (N <= 0x17)
  13188. {
  13189. write_number(static_cast<std::uint8_t>(0x60 + N));
  13190. }
  13191. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13192. {
  13193. oa->write_character(to_char_type(0x78));
  13194. write_number(static_cast<std::uint8_t>(N));
  13195. }
  13196. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13197. {
  13198. oa->write_character(to_char_type(0x79));
  13199. write_number(static_cast<std::uint16_t>(N));
  13200. }
  13201. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13202. {
  13203. oa->write_character(to_char_type(0x7A));
  13204. write_number(static_cast<std::uint32_t>(N));
  13205. }
  13206. // LCOV_EXCL_START
  13207. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  13208. {
  13209. oa->write_character(to_char_type(0x7B));
  13210. write_number(static_cast<std::uint64_t>(N));
  13211. }
  13212. // LCOV_EXCL_STOP
  13213. // step 2: write the string
  13214. oa->write_characters(
  13215. reinterpret_cast<const CharType*>(j.m_data.m_value.string->c_str()),
  13216. j.m_data.m_value.string->size());
  13217. break;
  13218. }
  13219. case value_t::array:
  13220. {
  13221. // step 1: write control byte and the array size
  13222. const auto N = j.m_data.m_value.array->size();
  13223. if (N <= 0x17)
  13224. {
  13225. write_number(static_cast<std::uint8_t>(0x80 + N));
  13226. }
  13227. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13228. {
  13229. oa->write_character(to_char_type(0x98));
  13230. write_number(static_cast<std::uint8_t>(N));
  13231. }
  13232. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13233. {
  13234. oa->write_character(to_char_type(0x99));
  13235. write_number(static_cast<std::uint16_t>(N));
  13236. }
  13237. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13238. {
  13239. oa->write_character(to_char_type(0x9A));
  13240. write_number(static_cast<std::uint32_t>(N));
  13241. }
  13242. // LCOV_EXCL_START
  13243. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  13244. {
  13245. oa->write_character(to_char_type(0x9B));
  13246. write_number(static_cast<std::uint64_t>(N));
  13247. }
  13248. // LCOV_EXCL_STOP
  13249. // step 2: write each element
  13250. for (const auto& el : *j.m_data.m_value.array)
  13251. {
  13252. write_cbor(el);
  13253. }
  13254. break;
  13255. }
  13256. case value_t::binary:
  13257. {
  13258. if (j.m_data.m_value.binary->has_subtype())
  13259. {
  13260. if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint8_t>::max)())
  13261. {
  13262. write_number(static_cast<std::uint8_t>(0xd8));
  13263. write_number(static_cast<std::uint8_t>(j.m_data.m_value.binary->subtype()));
  13264. }
  13265. else if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint16_t>::max)())
  13266. {
  13267. write_number(static_cast<std::uint8_t>(0xd9));
  13268. write_number(static_cast<std::uint16_t>(j.m_data.m_value.binary->subtype()));
  13269. }
  13270. else if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint32_t>::max)())
  13271. {
  13272. write_number(static_cast<std::uint8_t>(0xda));
  13273. write_number(static_cast<std::uint32_t>(j.m_data.m_value.binary->subtype()));
  13274. }
  13275. else if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint64_t>::max)())
  13276. {
  13277. write_number(static_cast<std::uint8_t>(0xdb));
  13278. write_number(static_cast<std::uint64_t>(j.m_data.m_value.binary->subtype()));
  13279. }
  13280. }
  13281. // step 1: write control byte and the binary array size
  13282. const auto N = j.m_data.m_value.binary->size();
  13283. if (N <= 0x17)
  13284. {
  13285. write_number(static_cast<std::uint8_t>(0x40 + N));
  13286. }
  13287. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13288. {
  13289. oa->write_character(to_char_type(0x58));
  13290. write_number(static_cast<std::uint8_t>(N));
  13291. }
  13292. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13293. {
  13294. oa->write_character(to_char_type(0x59));
  13295. write_number(static_cast<std::uint16_t>(N));
  13296. }
  13297. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13298. {
  13299. oa->write_character(to_char_type(0x5A));
  13300. write_number(static_cast<std::uint32_t>(N));
  13301. }
  13302. // LCOV_EXCL_START
  13303. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  13304. {
  13305. oa->write_character(to_char_type(0x5B));
  13306. write_number(static_cast<std::uint64_t>(N));
  13307. }
  13308. // LCOV_EXCL_STOP
  13309. // step 2: write each element
  13310. oa->write_characters(
  13311. reinterpret_cast<const CharType*>(j.m_data.m_value.binary->data()),
  13312. N);
  13313. break;
  13314. }
  13315. case value_t::object:
  13316. {
  13317. // step 1: write control byte and the object size
  13318. const auto N = j.m_data.m_value.object->size();
  13319. if (N <= 0x17)
  13320. {
  13321. write_number(static_cast<std::uint8_t>(0xA0 + N));
  13322. }
  13323. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13324. {
  13325. oa->write_character(to_char_type(0xB8));
  13326. write_number(static_cast<std::uint8_t>(N));
  13327. }
  13328. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13329. {
  13330. oa->write_character(to_char_type(0xB9));
  13331. write_number(static_cast<std::uint16_t>(N));
  13332. }
  13333. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13334. {
  13335. oa->write_character(to_char_type(0xBA));
  13336. write_number(static_cast<std::uint32_t>(N));
  13337. }
  13338. // LCOV_EXCL_START
  13339. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  13340. {
  13341. oa->write_character(to_char_type(0xBB));
  13342. write_number(static_cast<std::uint64_t>(N));
  13343. }
  13344. // LCOV_EXCL_STOP
  13345. // step 2: write each element
  13346. for (const auto& el : *j.m_data.m_value.object)
  13347. {
  13348. write_cbor(el.first);
  13349. write_cbor(el.second);
  13350. }
  13351. break;
  13352. }
  13353. case value_t::discarded:
  13354. default:
  13355. break;
  13356. }
  13357. }
  13358. /*!
  13359. @param[in] j JSON value to serialize
  13360. */
  13361. void write_msgpack(const BasicJsonType& j)
  13362. {
  13363. switch (j.type())
  13364. {
  13365. case value_t::null: // nil
  13366. {
  13367. oa->write_character(to_char_type(0xC0));
  13368. break;
  13369. }
  13370. case value_t::boolean: // true and false
  13371. {
  13372. oa->write_character(j.m_data.m_value.boolean
  13373. ? to_char_type(0xC3)
  13374. : to_char_type(0xC2));
  13375. break;
  13376. }
  13377. case value_t::number_integer:
  13378. {
  13379. if (j.m_data.m_value.number_integer >= 0)
  13380. {
  13381. // MessagePack does not differentiate between positive
  13382. // signed integers and unsigned integers. Therefore, we used
  13383. // the code from the value_t::number_unsigned case here.
  13384. if (j.m_data.m_value.number_unsigned < 128)
  13385. {
  13386. // positive fixnum
  13387. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  13388. }
  13389. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
  13390. {
  13391. // uint 8
  13392. oa->write_character(to_char_type(0xCC));
  13393. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  13394. }
  13395. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
  13396. {
  13397. // uint 16
  13398. oa->write_character(to_char_type(0xCD));
  13399. write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_integer));
  13400. }
  13401. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
  13402. {
  13403. // uint 32
  13404. oa->write_character(to_char_type(0xCE));
  13405. write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_integer));
  13406. }
  13407. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
  13408. {
  13409. // uint 64
  13410. oa->write_character(to_char_type(0xCF));
  13411. write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_integer));
  13412. }
  13413. }
  13414. else
  13415. {
  13416. if (j.m_data.m_value.number_integer >= -32)
  13417. {
  13418. // negative fixnum
  13419. write_number(static_cast<std::int8_t>(j.m_data.m_value.number_integer));
  13420. }
  13421. else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int8_t>::min)() &&
  13422. j.m_data.m_value.number_integer <= (std::numeric_limits<std::int8_t>::max)())
  13423. {
  13424. // int 8
  13425. oa->write_character(to_char_type(0xD0));
  13426. write_number(static_cast<std::int8_t>(j.m_data.m_value.number_integer));
  13427. }
  13428. else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int16_t>::min)() &&
  13429. j.m_data.m_value.number_integer <= (std::numeric_limits<std::int16_t>::max)())
  13430. {
  13431. // int 16
  13432. oa->write_character(to_char_type(0xD1));
  13433. write_number(static_cast<std::int16_t>(j.m_data.m_value.number_integer));
  13434. }
  13435. else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int32_t>::min)() &&
  13436. j.m_data.m_value.number_integer <= (std::numeric_limits<std::int32_t>::max)())
  13437. {
  13438. // int 32
  13439. oa->write_character(to_char_type(0xD2));
  13440. write_number(static_cast<std::int32_t>(j.m_data.m_value.number_integer));
  13441. }
  13442. else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int64_t>::min)() &&
  13443. j.m_data.m_value.number_integer <= (std::numeric_limits<std::int64_t>::max)())
  13444. {
  13445. // int 64
  13446. oa->write_character(to_char_type(0xD3));
  13447. write_number(static_cast<std::int64_t>(j.m_data.m_value.number_integer));
  13448. }
  13449. }
  13450. break;
  13451. }
  13452. case value_t::number_unsigned:
  13453. {
  13454. if (j.m_data.m_value.number_unsigned < 128)
  13455. {
  13456. // positive fixnum
  13457. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  13458. }
  13459. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
  13460. {
  13461. // uint 8
  13462. oa->write_character(to_char_type(0xCC));
  13463. write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
  13464. }
  13465. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
  13466. {
  13467. // uint 16
  13468. oa->write_character(to_char_type(0xCD));
  13469. write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_integer));
  13470. }
  13471. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
  13472. {
  13473. // uint 32
  13474. oa->write_character(to_char_type(0xCE));
  13475. write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_integer));
  13476. }
  13477. else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
  13478. {
  13479. // uint 64
  13480. oa->write_character(to_char_type(0xCF));
  13481. write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_integer));
  13482. }
  13483. break;
  13484. }
  13485. case value_t::number_float:
  13486. {
  13487. write_compact_float(j.m_data.m_value.number_float, detail::input_format_t::msgpack);
  13488. break;
  13489. }
  13490. case value_t::string:
  13491. {
  13492. // step 1: write control byte and the string length
  13493. const auto N = j.m_data.m_value.string->size();
  13494. if (N <= 31)
  13495. {
  13496. // fixstr
  13497. write_number(static_cast<std::uint8_t>(0xA0 | N));
  13498. }
  13499. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13500. {
  13501. // str 8
  13502. oa->write_character(to_char_type(0xD9));
  13503. write_number(static_cast<std::uint8_t>(N));
  13504. }
  13505. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13506. {
  13507. // str 16
  13508. oa->write_character(to_char_type(0xDA));
  13509. write_number(static_cast<std::uint16_t>(N));
  13510. }
  13511. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13512. {
  13513. // str 32
  13514. oa->write_character(to_char_type(0xDB));
  13515. write_number(static_cast<std::uint32_t>(N));
  13516. }
  13517. // step 2: write the string
  13518. oa->write_characters(
  13519. reinterpret_cast<const CharType*>(j.m_data.m_value.string->c_str()),
  13520. j.m_data.m_value.string->size());
  13521. break;
  13522. }
  13523. case value_t::array:
  13524. {
  13525. // step 1: write control byte and the array size
  13526. const auto N = j.m_data.m_value.array->size();
  13527. if (N <= 15)
  13528. {
  13529. // fixarray
  13530. write_number(static_cast<std::uint8_t>(0x90 | N));
  13531. }
  13532. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13533. {
  13534. // array 16
  13535. oa->write_character(to_char_type(0xDC));
  13536. write_number(static_cast<std::uint16_t>(N));
  13537. }
  13538. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13539. {
  13540. // array 32
  13541. oa->write_character(to_char_type(0xDD));
  13542. write_number(static_cast<std::uint32_t>(N));
  13543. }
  13544. // step 2: write each element
  13545. for (const auto& el : *j.m_data.m_value.array)
  13546. {
  13547. write_msgpack(el);
  13548. }
  13549. break;
  13550. }
  13551. case value_t::binary:
  13552. {
  13553. // step 0: determine if the binary type has a set subtype to
  13554. // determine whether or not to use the ext or fixext types
  13555. const bool use_ext = j.m_data.m_value.binary->has_subtype();
  13556. // step 1: write control byte and the byte string length
  13557. const auto N = j.m_data.m_value.binary->size();
  13558. if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13559. {
  13560. std::uint8_t output_type{};
  13561. bool fixed = true;
  13562. if (use_ext)
  13563. {
  13564. switch (N)
  13565. {
  13566. case 1:
  13567. output_type = 0xD4; // fixext 1
  13568. break;
  13569. case 2:
  13570. output_type = 0xD5; // fixext 2
  13571. break;
  13572. case 4:
  13573. output_type = 0xD6; // fixext 4
  13574. break;
  13575. case 8:
  13576. output_type = 0xD7; // fixext 8
  13577. break;
  13578. case 16:
  13579. output_type = 0xD8; // fixext 16
  13580. break;
  13581. default:
  13582. output_type = 0xC7; // ext 8
  13583. fixed = false;
  13584. break;
  13585. }
  13586. }
  13587. else
  13588. {
  13589. output_type = 0xC4; // bin 8
  13590. fixed = false;
  13591. }
  13592. oa->write_character(to_char_type(output_type));
  13593. if (!fixed)
  13594. {
  13595. write_number(static_cast<std::uint8_t>(N));
  13596. }
  13597. }
  13598. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13599. {
  13600. const std::uint8_t output_type = use_ext
  13601. ? 0xC8 // ext 16
  13602. : 0xC5; // bin 16
  13603. oa->write_character(to_char_type(output_type));
  13604. write_number(static_cast<std::uint16_t>(N));
  13605. }
  13606. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13607. {
  13608. const std::uint8_t output_type = use_ext
  13609. ? 0xC9 // ext 32
  13610. : 0xC6; // bin 32
  13611. oa->write_character(to_char_type(output_type));
  13612. write_number(static_cast<std::uint32_t>(N));
  13613. }
  13614. // step 1.5: if this is an ext type, write the subtype
  13615. if (use_ext)
  13616. {
  13617. write_number(static_cast<std::int8_t>(j.m_data.m_value.binary->subtype()));
  13618. }
  13619. // step 2: write the byte string
  13620. oa->write_characters(
  13621. reinterpret_cast<const CharType*>(j.m_data.m_value.binary->data()),
  13622. N);
  13623. break;
  13624. }
  13625. case value_t::object:
  13626. {
  13627. // step 1: write control byte and the object size
  13628. const auto N = j.m_data.m_value.object->size();
  13629. if (N <= 15)
  13630. {
  13631. // fixmap
  13632. write_number(static_cast<std::uint8_t>(0x80 | (N & 0xF)));
  13633. }
  13634. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13635. {
  13636. // map 16
  13637. oa->write_character(to_char_type(0xDE));
  13638. write_number(static_cast<std::uint16_t>(N));
  13639. }
  13640. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13641. {
  13642. // map 32
  13643. oa->write_character(to_char_type(0xDF));
  13644. write_number(static_cast<std::uint32_t>(N));
  13645. }
  13646. // step 2: write each element
  13647. for (const auto& el : *j.m_data.m_value.object)
  13648. {
  13649. write_msgpack(el.first);
  13650. write_msgpack(el.second);
  13651. }
  13652. break;
  13653. }
  13654. case value_t::discarded:
  13655. default:
  13656. break;
  13657. }
  13658. }
  13659. /*!
  13660. @param[in] j JSON value to serialize
  13661. @param[in] use_count whether to use '#' prefixes (optimized format)
  13662. @param[in] use_type whether to use '$' prefixes (optimized format)
  13663. @param[in] add_prefix whether prefixes need to be used for this value
  13664. @param[in] use_bjdata whether write in BJData format, default is false
  13665. */
  13666. void write_ubjson(const BasicJsonType& j, const bool use_count,
  13667. const bool use_type, const bool add_prefix = true,
  13668. const bool use_bjdata = false)
  13669. {
  13670. switch (j.type())
  13671. {
  13672. case value_t::null:
  13673. {
  13674. if (add_prefix)
  13675. {
  13676. oa->write_character(to_char_type('Z'));
  13677. }
  13678. break;
  13679. }
  13680. case value_t::boolean:
  13681. {
  13682. if (add_prefix)
  13683. {
  13684. oa->write_character(j.m_data.m_value.boolean
  13685. ? to_char_type('T')
  13686. : to_char_type('F'));
  13687. }
  13688. break;
  13689. }
  13690. case value_t::number_integer:
  13691. {
  13692. write_number_with_ubjson_prefix(j.m_data.m_value.number_integer, add_prefix, use_bjdata);
  13693. break;
  13694. }
  13695. case value_t::number_unsigned:
  13696. {
  13697. write_number_with_ubjson_prefix(j.m_data.m_value.number_unsigned, add_prefix, use_bjdata);
  13698. break;
  13699. }
  13700. case value_t::number_float:
  13701. {
  13702. write_number_with_ubjson_prefix(j.m_data.m_value.number_float, add_prefix, use_bjdata);
  13703. break;
  13704. }
  13705. case value_t::string:
  13706. {
  13707. if (add_prefix)
  13708. {
  13709. oa->write_character(to_char_type('S'));
  13710. }
  13711. write_number_with_ubjson_prefix(j.m_data.m_value.string->size(), true, use_bjdata);
  13712. oa->write_characters(
  13713. reinterpret_cast<const CharType*>(j.m_data.m_value.string->c_str()),
  13714. j.m_data.m_value.string->size());
  13715. break;
  13716. }
  13717. case value_t::array:
  13718. {
  13719. if (add_prefix)
  13720. {
  13721. oa->write_character(to_char_type('['));
  13722. }
  13723. bool prefix_required = true;
  13724. if (use_type && !j.m_data.m_value.array->empty())
  13725. {
  13726. JSON_ASSERT(use_count);
  13727. const CharType first_prefix = ubjson_prefix(j.front(), use_bjdata);
  13728. const bool same_prefix = std::all_of(j.begin() + 1, j.end(),
  13729. [this, first_prefix, use_bjdata](const BasicJsonType & v)
  13730. {
  13731. return ubjson_prefix(v, use_bjdata) == first_prefix;
  13732. });
  13733. std::vector<CharType> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type
  13734. if (same_prefix && !(use_bjdata && std::find(bjdx.begin(), bjdx.end(), first_prefix) != bjdx.end()))
  13735. {
  13736. prefix_required = false;
  13737. oa->write_character(to_char_type('$'));
  13738. oa->write_character(first_prefix);
  13739. }
  13740. }
  13741. if (use_count)
  13742. {
  13743. oa->write_character(to_char_type('#'));
  13744. write_number_with_ubjson_prefix(j.m_data.m_value.array->size(), true, use_bjdata);
  13745. }
  13746. for (const auto& el : *j.m_data.m_value.array)
  13747. {
  13748. write_ubjson(el, use_count, use_type, prefix_required, use_bjdata);
  13749. }
  13750. if (!use_count)
  13751. {
  13752. oa->write_character(to_char_type(']'));
  13753. }
  13754. break;
  13755. }
  13756. case value_t::binary:
  13757. {
  13758. if (add_prefix)
  13759. {
  13760. oa->write_character(to_char_type('['));
  13761. }
  13762. if (use_type && !j.m_data.m_value.binary->empty())
  13763. {
  13764. JSON_ASSERT(use_count);
  13765. oa->write_character(to_char_type('$'));
  13766. oa->write_character('U');
  13767. }
  13768. if (use_count)
  13769. {
  13770. oa->write_character(to_char_type('#'));
  13771. write_number_with_ubjson_prefix(j.m_data.m_value.binary->size(), true, use_bjdata);
  13772. }
  13773. if (use_type)
  13774. {
  13775. oa->write_characters(
  13776. reinterpret_cast<const CharType*>(j.m_data.m_value.binary->data()),
  13777. j.m_data.m_value.binary->size());
  13778. }
  13779. else
  13780. {
  13781. for (size_t i = 0; i < j.m_data.m_value.binary->size(); ++i)
  13782. {
  13783. oa->write_character(to_char_type('U'));
  13784. oa->write_character(j.m_data.m_value.binary->data()[i]);
  13785. }
  13786. }
  13787. if (!use_count)
  13788. {
  13789. oa->write_character(to_char_type(']'));
  13790. }
  13791. break;
  13792. }
  13793. case value_t::object:
  13794. {
  13795. 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())
  13796. {
  13797. 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)
  13798. {
  13799. break;
  13800. }
  13801. }
  13802. if (add_prefix)
  13803. {
  13804. oa->write_character(to_char_type('{'));
  13805. }
  13806. bool prefix_required = true;
  13807. if (use_type && !j.m_data.m_value.object->empty())
  13808. {
  13809. JSON_ASSERT(use_count);
  13810. const CharType first_prefix = ubjson_prefix(j.front(), use_bjdata);
  13811. const bool same_prefix = std::all_of(j.begin(), j.end(),
  13812. [this, first_prefix, use_bjdata](const BasicJsonType & v)
  13813. {
  13814. return ubjson_prefix(v, use_bjdata) == first_prefix;
  13815. });
  13816. std::vector<CharType> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type
  13817. if (same_prefix && !(use_bjdata && std::find(bjdx.begin(), bjdx.end(), first_prefix) != bjdx.end()))
  13818. {
  13819. prefix_required = false;
  13820. oa->write_character(to_char_type('$'));
  13821. oa->write_character(first_prefix);
  13822. }
  13823. }
  13824. if (use_count)
  13825. {
  13826. oa->write_character(to_char_type('#'));
  13827. write_number_with_ubjson_prefix(j.m_data.m_value.object->size(), true, use_bjdata);
  13828. }
  13829. for (const auto& el : *j.m_data.m_value.object)
  13830. {
  13831. write_number_with_ubjson_prefix(el.first.size(), true, use_bjdata);
  13832. oa->write_characters(
  13833. reinterpret_cast<const CharType*>(el.first.c_str()),
  13834. el.first.size());
  13835. write_ubjson(el.second, use_count, use_type, prefix_required, use_bjdata);
  13836. }
  13837. if (!use_count)
  13838. {
  13839. oa->write_character(to_char_type('}'));
  13840. }
  13841. break;
  13842. }
  13843. case value_t::discarded:
  13844. default:
  13845. break;
  13846. }
  13847. }
  13848. private:
  13849. //////////
  13850. // BSON //
  13851. //////////
  13852. /*!
  13853. @return The size of a BSON document entry header, including the id marker
  13854. and the entry name size (and its null-terminator).
  13855. */
  13856. static std::size_t calc_bson_entry_header_size(const string_t& name, const BasicJsonType& j)
  13857. {
  13858. const auto it = name.find(static_cast<typename string_t::value_type>(0));
  13859. if (JSON_HEDLEY_UNLIKELY(it != BasicJsonType::string_t::npos))
  13860. {
  13861. JSON_THROW(out_of_range::create(409, concat("BSON key cannot contain code point U+0000 (at byte ", std::to_string(it), ")"), &j));
  13862. static_cast<void>(j);
  13863. }
  13864. return /*id*/ 1ul + name.size() + /*zero-terminator*/1u;
  13865. }
  13866. /*!
  13867. @brief Writes the given @a element_type and @a name to the output adapter
  13868. */
  13869. void write_bson_entry_header(const string_t& name,
  13870. const std::uint8_t element_type)
  13871. {
  13872. oa->write_character(to_char_type(element_type)); // boolean
  13873. oa->write_characters(
  13874. reinterpret_cast<const CharType*>(name.c_str()),
  13875. name.size() + 1u);
  13876. }
  13877. /*!
  13878. @brief Writes a BSON element with key @a name and boolean value @a value
  13879. */
  13880. void write_bson_boolean(const string_t& name,
  13881. const bool value)
  13882. {
  13883. write_bson_entry_header(name, 0x08);
  13884. oa->write_character(value ? to_char_type(0x01) : to_char_type(0x00));
  13885. }
  13886. /*!
  13887. @brief Writes a BSON element with key @a name and double value @a value
  13888. */
  13889. void write_bson_double(const string_t& name,
  13890. const double value)
  13891. {
  13892. write_bson_entry_header(name, 0x01);
  13893. write_number<double>(value, true);
  13894. }
  13895. /*!
  13896. @return The size of the BSON-encoded string in @a value
  13897. */
  13898. static std::size_t calc_bson_string_size(const string_t& value)
  13899. {
  13900. return sizeof(std::int32_t) + value.size() + 1ul;
  13901. }
  13902. /*!
  13903. @brief Writes a BSON element with key @a name and string value @a value
  13904. */
  13905. void write_bson_string(const string_t& name,
  13906. const string_t& value)
  13907. {
  13908. write_bson_entry_header(name, 0x02);
  13909. write_number<std::int32_t>(static_cast<std::int32_t>(value.size() + 1ul), true);
  13910. oa->write_characters(
  13911. reinterpret_cast<const CharType*>(value.c_str()),
  13912. value.size() + 1);
  13913. }
  13914. /*!
  13915. @brief Writes a BSON element with key @a name and null value
  13916. */
  13917. void write_bson_null(const string_t& name)
  13918. {
  13919. write_bson_entry_header(name, 0x0A);
  13920. }
  13921. /*!
  13922. @return The size of the BSON-encoded integer @a value
  13923. */
  13924. static std::size_t calc_bson_integer_size(const std::int64_t value)
  13925. {
  13926. return (std::numeric_limits<std::int32_t>::min)() <= value && value <= (std::numeric_limits<std::int32_t>::max)()
  13927. ? sizeof(std::int32_t)
  13928. : sizeof(std::int64_t);
  13929. }
  13930. /*!
  13931. @brief Writes a BSON element with key @a name and integer @a value
  13932. */
  13933. void write_bson_integer(const string_t& name,
  13934. const std::int64_t value)
  13935. {
  13936. if ((std::numeric_limits<std::int32_t>::min)() <= value && value <= (std::numeric_limits<std::int32_t>::max)())
  13937. {
  13938. write_bson_entry_header(name, 0x10); // int32
  13939. write_number<std::int32_t>(static_cast<std::int32_t>(value), true);
  13940. }
  13941. else
  13942. {
  13943. write_bson_entry_header(name, 0x12); // int64
  13944. write_number<std::int64_t>(static_cast<std::int64_t>(value), true);
  13945. }
  13946. }
  13947. /*!
  13948. @return The size of the BSON-encoded unsigned integer in @a j
  13949. */
  13950. static constexpr std::size_t calc_bson_unsigned_size(const std::uint64_t value) noexcept
  13951. {
  13952. return (value <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  13953. ? sizeof(std::int32_t)
  13954. : sizeof(std::int64_t);
  13955. }
  13956. /*!
  13957. @brief Writes a BSON element with key @a name and unsigned @a value
  13958. */
  13959. void write_bson_unsigned(const string_t& name,
  13960. const BasicJsonType& j)
  13961. {
  13962. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  13963. {
  13964. write_bson_entry_header(name, 0x10 /* int32 */);
  13965. write_number<std::int32_t>(static_cast<std::int32_t>(j.m_data.m_value.number_unsigned), true);
  13966. }
  13967. else if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
  13968. {
  13969. write_bson_entry_header(name, 0x12 /* int64 */);
  13970. write_number<std::int64_t>(static_cast<std::int64_t>(j.m_data.m_value.number_unsigned), true);
  13971. }
  13972. else
  13973. {
  13974. 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));
  13975. }
  13976. }
  13977. /*!
  13978. @brief Writes a BSON element with key @a name and object @a value
  13979. */
  13980. void write_bson_object_entry(const string_t& name,
  13981. const typename BasicJsonType::object_t& value)
  13982. {
  13983. write_bson_entry_header(name, 0x03); // object
  13984. write_bson_object(value);
  13985. }
  13986. /*!
  13987. @return The size of the BSON-encoded array @a value
  13988. */
  13989. static std::size_t calc_bson_array_size(const typename BasicJsonType::array_t& value)
  13990. {
  13991. std::size_t array_index = 0ul;
  13992. 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)
  13993. {
  13994. return result + calc_bson_element_size(std::to_string(array_index++), el);
  13995. });
  13996. return sizeof(std::int32_t) + embedded_document_size + 1ul;
  13997. }
  13998. /*!
  13999. @return The size of the BSON-encoded binary array @a value
  14000. */
  14001. static std::size_t calc_bson_binary_size(const typename BasicJsonType::binary_t& value)
  14002. {
  14003. return sizeof(std::int32_t) + value.size() + 1ul;
  14004. }
  14005. /*!
  14006. @brief Writes a BSON element with key @a name and array @a value
  14007. */
  14008. void write_bson_array(const string_t& name,
  14009. const typename BasicJsonType::array_t& value)
  14010. {
  14011. write_bson_entry_header(name, 0x04); // array
  14012. write_number<std::int32_t>(static_cast<std::int32_t>(calc_bson_array_size(value)), true);
  14013. std::size_t array_index = 0ul;
  14014. for (const auto& el : value)
  14015. {
  14016. write_bson_element(std::to_string(array_index++), el);
  14017. }
  14018. oa->write_character(to_char_type(0x00));
  14019. }
  14020. /*!
  14021. @brief Writes a BSON element with key @a name and binary value @a value
  14022. */
  14023. void write_bson_binary(const string_t& name,
  14024. const binary_t& value)
  14025. {
  14026. write_bson_entry_header(name, 0x05);
  14027. write_number<std::int32_t>(static_cast<std::int32_t>(value.size()), true);
  14028. write_number(value.has_subtype() ? static_cast<std::uint8_t>(value.subtype()) : static_cast<std::uint8_t>(0x00));
  14029. oa->write_characters(reinterpret_cast<const CharType*>(value.data()), value.size());
  14030. }
  14031. /*!
  14032. @brief Calculates the size necessary to serialize the JSON value @a j with its @a name
  14033. @return The calculated size for the BSON document entry for @a j with the given @a name.
  14034. */
  14035. static std::size_t calc_bson_element_size(const string_t& name,
  14036. const BasicJsonType& j)
  14037. {
  14038. const auto header_size = calc_bson_entry_header_size(name, j);
  14039. switch (j.type())
  14040. {
  14041. case value_t::object:
  14042. return header_size + calc_bson_object_size(*j.m_data.m_value.object);
  14043. case value_t::array:
  14044. return header_size + calc_bson_array_size(*j.m_data.m_value.array);
  14045. case value_t::binary:
  14046. return header_size + calc_bson_binary_size(*j.m_data.m_value.binary);
  14047. case value_t::boolean:
  14048. return header_size + 1ul;
  14049. case value_t::number_float:
  14050. return header_size + 8ul;
  14051. case value_t::number_integer:
  14052. return header_size + calc_bson_integer_size(j.m_data.m_value.number_integer);
  14053. case value_t::number_unsigned:
  14054. return header_size + calc_bson_unsigned_size(j.m_data.m_value.number_unsigned);
  14055. case value_t::string:
  14056. return header_size + calc_bson_string_size(*j.m_data.m_value.string);
  14057. case value_t::null:
  14058. return header_size + 0ul;
  14059. // LCOV_EXCL_START
  14060. case value_t::discarded:
  14061. default:
  14062. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert)
  14063. return 0ul;
  14064. // LCOV_EXCL_STOP
  14065. }
  14066. }
  14067. /*!
  14068. @brief Serializes the JSON value @a j to BSON and associates it with the
  14069. key @a name.
  14070. @param name The name to associate with the JSON entity @a j within the
  14071. current BSON document
  14072. */
  14073. void write_bson_element(const string_t& name,
  14074. const BasicJsonType& j)
  14075. {
  14076. switch (j.type())
  14077. {
  14078. case value_t::object:
  14079. return write_bson_object_entry(name, *j.m_data.m_value.object);
  14080. case value_t::array:
  14081. return write_bson_array(name, *j.m_data.m_value.array);
  14082. case value_t::binary:
  14083. return write_bson_binary(name, *j.m_data.m_value.binary);
  14084. case value_t::boolean:
  14085. return write_bson_boolean(name, j.m_data.m_value.boolean);
  14086. case value_t::number_float:
  14087. return write_bson_double(name, j.m_data.m_value.number_float);
  14088. case value_t::number_integer:
  14089. return write_bson_integer(name, j.m_data.m_value.number_integer);
  14090. case value_t::number_unsigned:
  14091. return write_bson_unsigned(name, j);
  14092. case value_t::string:
  14093. return write_bson_string(name, *j.m_data.m_value.string);
  14094. case value_t::null:
  14095. return write_bson_null(name);
  14096. // LCOV_EXCL_START
  14097. case value_t::discarded:
  14098. default:
  14099. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert)
  14100. return;
  14101. // LCOV_EXCL_STOP
  14102. }
  14103. }
  14104. /*!
  14105. @brief Calculates the size of the BSON serialization of the given
  14106. JSON-object @a j.
  14107. @param[in] value JSON value to serialize
  14108. @pre value.type() == value_t::object
  14109. */
  14110. static std::size_t calc_bson_object_size(const typename BasicJsonType::object_t& value)
  14111. {
  14112. const std::size_t document_size = std::accumulate(value.begin(), value.end(), static_cast<std::size_t>(0),
  14113. [](size_t result, const typename BasicJsonType::object_t::value_type & el)
  14114. {
  14115. return result += calc_bson_element_size(el.first, el.second);
  14116. });
  14117. return sizeof(std::int32_t) + document_size + 1ul;
  14118. }
  14119. /*!
  14120. @param[in] value JSON value to serialize
  14121. @pre value.type() == value_t::object
  14122. */
  14123. void write_bson_object(const typename BasicJsonType::object_t& value)
  14124. {
  14125. write_number<std::int32_t>(static_cast<std::int32_t>(calc_bson_object_size(value)), true);
  14126. for (const auto& el : value)
  14127. {
  14128. write_bson_element(el.first, el.second);
  14129. }
  14130. oa->write_character(to_char_type(0x00));
  14131. }
  14132. //////////
  14133. // CBOR //
  14134. //////////
  14135. static constexpr CharType get_cbor_float_prefix(float /*unused*/)
  14136. {
  14137. return to_char_type(0xFA); // Single-Precision Float
  14138. }
  14139. static constexpr CharType get_cbor_float_prefix(double /*unused*/)
  14140. {
  14141. return to_char_type(0xFB); // Double-Precision Float
  14142. }
  14143. /////////////
  14144. // MsgPack //
  14145. /////////////
  14146. static constexpr CharType get_msgpack_float_prefix(float /*unused*/)
  14147. {
  14148. return to_char_type(0xCA); // float 32
  14149. }
  14150. static constexpr CharType get_msgpack_float_prefix(double /*unused*/)
  14151. {
  14152. return to_char_type(0xCB); // float 64
  14153. }
  14154. ////////////
  14155. // UBJSON //
  14156. ////////////
  14157. // UBJSON: write number (floating point)
  14158. template<typename NumberType, typename std::enable_if<
  14159. std::is_floating_point<NumberType>::value, int>::type = 0>
  14160. void write_number_with_ubjson_prefix(const NumberType n,
  14161. const bool add_prefix,
  14162. const bool use_bjdata)
  14163. {
  14164. if (add_prefix)
  14165. {
  14166. oa->write_character(get_ubjson_float_prefix(n));
  14167. }
  14168. write_number(n, use_bjdata);
  14169. }
  14170. // UBJSON: write number (unsigned integer)
  14171. template<typename NumberType, typename std::enable_if<
  14172. std::is_unsigned<NumberType>::value, int>::type = 0>
  14173. void write_number_with_ubjson_prefix(const NumberType n,
  14174. const bool add_prefix,
  14175. const bool use_bjdata)
  14176. {
  14177. if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int8_t>::max)()))
  14178. {
  14179. if (add_prefix)
  14180. {
  14181. oa->write_character(to_char_type('i')); // int8
  14182. }
  14183. write_number(static_cast<std::uint8_t>(n), use_bjdata);
  14184. }
  14185. else if (n <= (std::numeric_limits<std::uint8_t>::max)())
  14186. {
  14187. if (add_prefix)
  14188. {
  14189. oa->write_character(to_char_type('U')); // uint8
  14190. }
  14191. write_number(static_cast<std::uint8_t>(n), use_bjdata);
  14192. }
  14193. else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int16_t>::max)()))
  14194. {
  14195. if (add_prefix)
  14196. {
  14197. oa->write_character(to_char_type('I')); // int16
  14198. }
  14199. write_number(static_cast<std::int16_t>(n), use_bjdata);
  14200. }
  14201. else if (use_bjdata && n <= static_cast<uint64_t>((std::numeric_limits<uint16_t>::max)()))
  14202. {
  14203. if (add_prefix)
  14204. {
  14205. oa->write_character(to_char_type('u')); // uint16 - bjdata only
  14206. }
  14207. write_number(static_cast<std::uint16_t>(n), use_bjdata);
  14208. }
  14209. else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  14210. {
  14211. if (add_prefix)
  14212. {
  14213. oa->write_character(to_char_type('l')); // int32
  14214. }
  14215. write_number(static_cast<std::int32_t>(n), use_bjdata);
  14216. }
  14217. else if (use_bjdata && n <= static_cast<uint64_t>((std::numeric_limits<uint32_t>::max)()))
  14218. {
  14219. if (add_prefix)
  14220. {
  14221. oa->write_character(to_char_type('m')); // uint32 - bjdata only
  14222. }
  14223. write_number(static_cast<std::uint32_t>(n), use_bjdata);
  14224. }
  14225. else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
  14226. {
  14227. if (add_prefix)
  14228. {
  14229. oa->write_character(to_char_type('L')); // int64
  14230. }
  14231. write_number(static_cast<std::int64_t>(n), use_bjdata);
  14232. }
  14233. else if (use_bjdata && n <= (std::numeric_limits<uint64_t>::max)())
  14234. {
  14235. if (add_prefix)
  14236. {
  14237. oa->write_character(to_char_type('M')); // uint64 - bjdata only
  14238. }
  14239. write_number(static_cast<std::uint64_t>(n), use_bjdata);
  14240. }
  14241. else
  14242. {
  14243. if (add_prefix)
  14244. {
  14245. oa->write_character(to_char_type('H')); // high-precision number
  14246. }
  14247. const auto number = BasicJsonType(n).dump();
  14248. write_number_with_ubjson_prefix(number.size(), true, use_bjdata);
  14249. for (std::size_t i = 0; i < number.size(); ++i)
  14250. {
  14251. oa->write_character(to_char_type(static_cast<std::uint8_t>(number[i])));
  14252. }
  14253. }
  14254. }
  14255. // UBJSON: write number (signed integer)
  14256. template < typename NumberType, typename std::enable_if <
  14257. std::is_signed<NumberType>::value&&
  14258. !std::is_floating_point<NumberType>::value, int >::type = 0 >
  14259. void write_number_with_ubjson_prefix(const NumberType n,
  14260. const bool add_prefix,
  14261. const bool use_bjdata)
  14262. {
  14263. if ((std::numeric_limits<std::int8_t>::min)() <= n && n <= (std::numeric_limits<std::int8_t>::max)())
  14264. {
  14265. if (add_prefix)
  14266. {
  14267. oa->write_character(to_char_type('i')); // int8
  14268. }
  14269. write_number(static_cast<std::int8_t>(n), use_bjdata);
  14270. }
  14271. 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)()))
  14272. {
  14273. if (add_prefix)
  14274. {
  14275. oa->write_character(to_char_type('U')); // uint8
  14276. }
  14277. write_number(static_cast<std::uint8_t>(n), use_bjdata);
  14278. }
  14279. else if ((std::numeric_limits<std::int16_t>::min)() <= n && n <= (std::numeric_limits<std::int16_t>::max)())
  14280. {
  14281. if (add_prefix)
  14282. {
  14283. oa->write_character(to_char_type('I')); // int16
  14284. }
  14285. write_number(static_cast<std::int16_t>(n), use_bjdata);
  14286. }
  14287. 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)())))
  14288. {
  14289. if (add_prefix)
  14290. {
  14291. oa->write_character(to_char_type('u')); // uint16 - bjdata only
  14292. }
  14293. write_number(static_cast<uint16_t>(n), use_bjdata);
  14294. }
  14295. else if ((std::numeric_limits<std::int32_t>::min)() <= n && n <= (std::numeric_limits<std::int32_t>::max)())
  14296. {
  14297. if (add_prefix)
  14298. {
  14299. oa->write_character(to_char_type('l')); // int32
  14300. }
  14301. write_number(static_cast<std::int32_t>(n), use_bjdata);
  14302. }
  14303. 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)())))
  14304. {
  14305. if (add_prefix)
  14306. {
  14307. oa->write_character(to_char_type('m')); // uint32 - bjdata only
  14308. }
  14309. write_number(static_cast<uint32_t>(n), use_bjdata);
  14310. }
  14311. else if ((std::numeric_limits<std::int64_t>::min)() <= n && n <= (std::numeric_limits<std::int64_t>::max)())
  14312. {
  14313. if (add_prefix)
  14314. {
  14315. oa->write_character(to_char_type('L')); // int64
  14316. }
  14317. write_number(static_cast<std::int64_t>(n), use_bjdata);
  14318. }
  14319. // LCOV_EXCL_START
  14320. else
  14321. {
  14322. if (add_prefix)
  14323. {
  14324. oa->write_character(to_char_type('H')); // high-precision number
  14325. }
  14326. const auto number = BasicJsonType(n).dump();
  14327. write_number_with_ubjson_prefix(number.size(), true, use_bjdata);
  14328. for (std::size_t i = 0; i < number.size(); ++i)
  14329. {
  14330. oa->write_character(to_char_type(static_cast<std::uint8_t>(number[i])));
  14331. }
  14332. }
  14333. // LCOV_EXCL_STOP
  14334. }
  14335. /*!
  14336. @brief determine the type prefix of container values
  14337. */
  14338. CharType ubjson_prefix(const BasicJsonType& j, const bool use_bjdata) const noexcept
  14339. {
  14340. switch (j.type())
  14341. {
  14342. case value_t::null:
  14343. return 'Z';
  14344. case value_t::boolean:
  14345. return j.m_data.m_value.boolean ? 'T' : 'F';
  14346. case value_t::number_integer:
  14347. {
  14348. 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)())
  14349. {
  14350. return 'i';
  14351. }
  14352. 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)())
  14353. {
  14354. return 'U';
  14355. }
  14356. 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)())
  14357. {
  14358. return 'I';
  14359. }
  14360. 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)()))
  14361. {
  14362. return 'u';
  14363. }
  14364. 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)())
  14365. {
  14366. return 'l';
  14367. }
  14368. 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)()))
  14369. {
  14370. return 'm';
  14371. }
  14372. 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)())
  14373. {
  14374. return 'L';
  14375. }
  14376. // anything else is treated as high-precision number
  14377. return 'H'; // LCOV_EXCL_LINE
  14378. }
  14379. case value_t::number_unsigned:
  14380. {
  14381. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int8_t>::max)()))
  14382. {
  14383. return 'i';
  14384. }
  14385. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint8_t>::max)()))
  14386. {
  14387. return 'U';
  14388. }
  14389. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int16_t>::max)()))
  14390. {
  14391. return 'I';
  14392. }
  14393. if (use_bjdata && j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint16_t>::max)()))
  14394. {
  14395. return 'u';
  14396. }
  14397. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  14398. {
  14399. return 'l';
  14400. }
  14401. if (use_bjdata && j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint32_t>::max)()))
  14402. {
  14403. return 'm';
  14404. }
  14405. if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
  14406. {
  14407. return 'L';
  14408. }
  14409. if (use_bjdata && j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
  14410. {
  14411. return 'M';
  14412. }
  14413. // anything else is treated as high-precision number
  14414. return 'H'; // LCOV_EXCL_LINE
  14415. }
  14416. case value_t::number_float:
  14417. return get_ubjson_float_prefix(j.m_data.m_value.number_float);
  14418. case value_t::string:
  14419. return 'S';
  14420. case value_t::array: // fallthrough
  14421. case value_t::binary:
  14422. return '[';
  14423. case value_t::object:
  14424. return '{';
  14425. case value_t::discarded:
  14426. default: // discarded values
  14427. return 'N';
  14428. }
  14429. }
  14430. static constexpr CharType get_ubjson_float_prefix(float /*unused*/)
  14431. {
  14432. return 'd'; // float 32
  14433. }
  14434. static constexpr CharType get_ubjson_float_prefix(double /*unused*/)
  14435. {
  14436. return 'D'; // float 64
  14437. }
  14438. /*!
  14439. @return false if the object is successfully converted to a bjdata ndarray, true if the type or size is invalid
  14440. */
  14441. bool write_bjdata_ndarray(const typename BasicJsonType::object_t& value, const bool use_count, const bool use_type)
  14442. {
  14443. std::map<string_t, CharType> bjdtype = {{"uint8", 'U'}, {"int8", 'i'}, {"uint16", 'u'}, {"int16", 'I'},
  14444. {"uint32", 'm'}, {"int32", 'l'}, {"uint64", 'M'}, {"int64", 'L'}, {"single", 'd'}, {"double", 'D'}, {"char", 'C'}
  14445. };
  14446. string_t key = "_ArrayType_";
  14447. auto it = bjdtype.find(static_cast<string_t>(value.at(key)));
  14448. if (it == bjdtype.end())
  14449. {
  14450. return true;
  14451. }
  14452. CharType dtype = it->second;
  14453. key = "_ArraySize_";
  14454. std::size_t len = (value.at(key).empty() ? 0 : 1);
  14455. for (const auto& el : value.at(key))
  14456. {
  14457. len *= static_cast<std::size_t>(el.m_data.m_value.number_unsigned);
  14458. }
  14459. key = "_ArrayData_";
  14460. if (value.at(key).size() != len)
  14461. {
  14462. return true;
  14463. }
  14464. oa->write_character('[');
  14465. oa->write_character('$');
  14466. oa->write_character(dtype);
  14467. oa->write_character('#');
  14468. key = "_ArraySize_";
  14469. write_ubjson(value.at(key), use_count, use_type, true, true);
  14470. key = "_ArrayData_";
  14471. if (dtype == 'U' || dtype == 'C')
  14472. {
  14473. for (const auto& el : value.at(key))
  14474. {
  14475. write_number(static_cast<std::uint8_t>(el.m_data.m_value.number_unsigned), true);
  14476. }
  14477. }
  14478. else if (dtype == 'i')
  14479. {
  14480. for (const auto& el : value.at(key))
  14481. {
  14482. write_number(static_cast<std::int8_t>(el.m_data.m_value.number_integer), true);
  14483. }
  14484. }
  14485. else if (dtype == 'u')
  14486. {
  14487. for (const auto& el : value.at(key))
  14488. {
  14489. write_number(static_cast<std::uint16_t>(el.m_data.m_value.number_unsigned), true);
  14490. }
  14491. }
  14492. else if (dtype == 'I')
  14493. {
  14494. for (const auto& el : value.at(key))
  14495. {
  14496. write_number(static_cast<std::int16_t>(el.m_data.m_value.number_integer), true);
  14497. }
  14498. }
  14499. else if (dtype == 'm')
  14500. {
  14501. for (const auto& el : value.at(key))
  14502. {
  14503. write_number(static_cast<std::uint32_t>(el.m_data.m_value.number_unsigned), true);
  14504. }
  14505. }
  14506. else if (dtype == 'l')
  14507. {
  14508. for (const auto& el : value.at(key))
  14509. {
  14510. write_number(static_cast<std::int32_t>(el.m_data.m_value.number_integer), true);
  14511. }
  14512. }
  14513. else if (dtype == 'M')
  14514. {
  14515. for (const auto& el : value.at(key))
  14516. {
  14517. write_number(static_cast<std::uint64_t>(el.m_data.m_value.number_unsigned), true);
  14518. }
  14519. }
  14520. else if (dtype == 'L')
  14521. {
  14522. for (const auto& el : value.at(key))
  14523. {
  14524. write_number(static_cast<std::int64_t>(el.m_data.m_value.number_integer), true);
  14525. }
  14526. }
  14527. else if (dtype == 'd')
  14528. {
  14529. for (const auto& el : value.at(key))
  14530. {
  14531. write_number(static_cast<float>(el.m_data.m_value.number_float), true);
  14532. }
  14533. }
  14534. else if (dtype == 'D')
  14535. {
  14536. for (const auto& el : value.at(key))
  14537. {
  14538. write_number(static_cast<double>(el.m_data.m_value.number_float), true);
  14539. }
  14540. }
  14541. return false;
  14542. }
  14543. ///////////////////////
  14544. // Utility functions //
  14545. ///////////////////////
  14546. /*
  14547. @brief write a number to output input
  14548. @param[in] n number of type @a NumberType
  14549. @param[in] OutputIsLittleEndian Set to true if output data is
  14550. required to be little endian
  14551. @tparam NumberType the type of the number
  14552. @note This function needs to respect the system's endianness, because bytes
  14553. in CBOR, MessagePack, and UBJSON are stored in network order (big
  14554. endian) and therefore need reordering on little endian systems.
  14555. On the other hand, BSON and BJData use little endian and should reorder
  14556. on big endian systems.
  14557. */
  14558. template<typename NumberType>
  14559. void write_number(const NumberType n, const bool OutputIsLittleEndian = false)
  14560. {
  14561. // step 1: write number to array of length NumberType
  14562. std::array<CharType, sizeof(NumberType)> vec{};
  14563. std::memcpy(vec.data(), &n, sizeof(NumberType));
  14564. // step 2: write array to output (with possible reordering)
  14565. if (is_little_endian != OutputIsLittleEndian)
  14566. {
  14567. // reverse byte order prior to conversion if necessary
  14568. std::reverse(vec.begin(), vec.end());
  14569. }
  14570. oa->write_characters(vec.data(), sizeof(NumberType));
  14571. }
  14572. void write_compact_float(const number_float_t n, detail::input_format_t format)
  14573. {
  14574. #ifdef __GNUC__
  14575. #pragma GCC diagnostic push
  14576. #pragma GCC diagnostic ignored "-Wfloat-equal"
  14577. #endif
  14578. if (static_cast<double>(n) >= static_cast<double>(std::numeric_limits<float>::lowest()) &&
  14579. static_cast<double>(n) <= static_cast<double>((std::numeric_limits<float>::max)()) &&
  14580. static_cast<double>(static_cast<float>(n)) == static_cast<double>(n))
  14581. {
  14582. oa->write_character(format == detail::input_format_t::cbor
  14583. ? get_cbor_float_prefix(static_cast<float>(n))
  14584. : get_msgpack_float_prefix(static_cast<float>(n)));
  14585. write_number(static_cast<float>(n));
  14586. }
  14587. else
  14588. {
  14589. oa->write_character(format == detail::input_format_t::cbor
  14590. ? get_cbor_float_prefix(n)
  14591. : get_msgpack_float_prefix(n));
  14592. write_number(n);
  14593. }
  14594. #ifdef __GNUC__
  14595. #pragma GCC diagnostic pop
  14596. #endif
  14597. }
  14598. public:
  14599. // The following to_char_type functions are implement the conversion
  14600. // between uint8_t and CharType. In case CharType is not unsigned,
  14601. // such a conversion is required to allow values greater than 128.
  14602. // See <https://github.com/nlohmann/json/issues/1286> for a discussion.
  14603. template < typename C = CharType,
  14604. enable_if_t < std::is_signed<C>::value && std::is_signed<char>::value > * = nullptr >
  14605. static constexpr CharType to_char_type(std::uint8_t x) noexcept
  14606. {
  14607. return *reinterpret_cast<char*>(&x);
  14608. }
  14609. template < typename C = CharType,
  14610. enable_if_t < std::is_signed<C>::value && std::is_unsigned<char>::value > * = nullptr >
  14611. static CharType to_char_type(std::uint8_t x) noexcept
  14612. {
  14613. static_assert(sizeof(std::uint8_t) == sizeof(CharType), "size of CharType must be equal to std::uint8_t");
  14614. static_assert(std::is_trivial<CharType>::value, "CharType must be trivial");
  14615. CharType result;
  14616. std::memcpy(&result, &x, sizeof(x));
  14617. return result;
  14618. }
  14619. template<typename C = CharType,
  14620. enable_if_t<std::is_unsigned<C>::value>* = nullptr>
  14621. static constexpr CharType to_char_type(std::uint8_t x) noexcept
  14622. {
  14623. return x;
  14624. }
  14625. template < typename InputCharType, typename C = CharType,
  14626. enable_if_t <
  14627. std::is_signed<C>::value &&
  14628. std::is_signed<char>::value &&
  14629. std::is_same<char, typename std::remove_cv<InputCharType>::type>::value
  14630. > * = nullptr >
  14631. static constexpr CharType to_char_type(InputCharType x) noexcept
  14632. {
  14633. return x;
  14634. }
  14635. private:
  14636. /// whether we can assume little endianness
  14637. const bool is_little_endian = little_endianness();
  14638. /// the output
  14639. output_adapter_t<CharType> oa = nullptr;
  14640. };
  14641. } // namespace detail
  14642. NLOHMANN_JSON_NAMESPACE_END
  14643. // #include <nlohmann/detail/output/output_adapters.hpp>
  14644. // #include <nlohmann/detail/output/serializer.hpp>
  14645. // __ _____ _____ _____
  14646. // __| | __| | | | JSON for Modern C++
  14647. // | | |__ | | | | | | version 3.11.2
  14648. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  14649. //
  14650. // SPDX-FileCopyrightText: 2008-2009 Björn Hoehrmann <bjoern@hoehrmann.de>
  14651. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  14652. // SPDX-License-Identifier: MIT
  14653. #include <algorithm> // reverse, remove, fill, find, none_of
  14654. #include <array> // array
  14655. #include <clocale> // localeconv, lconv
  14656. #include <cmath> // labs, isfinite, isnan, signbit
  14657. #include <cstddef> // size_t, ptrdiff_t
  14658. #include <cstdint> // uint8_t
  14659. #include <cstdio> // snprintf
  14660. #include <limits> // numeric_limits
  14661. #include <string> // string, char_traits
  14662. #include <iomanip> // setfill, setw
  14663. #include <type_traits> // is_same
  14664. #include <utility> // move
  14665. // #include <nlohmann/detail/conversions/to_chars.hpp>
  14666. // __ _____ _____ _____
  14667. // __| | __| | | | JSON for Modern C++
  14668. // | | |__ | | | | | | version 3.11.2
  14669. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  14670. //
  14671. // SPDX-FileCopyrightText: 2009 Florian Loitsch <https://florian.loitsch.com/>
  14672. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  14673. // SPDX-License-Identifier: MIT
  14674. #include <array> // array
  14675. #include <cmath> // signbit, isfinite
  14676. #include <cstdint> // intN_t, uintN_t
  14677. #include <cstring> // memcpy, memmove
  14678. #include <limits> // numeric_limits
  14679. #include <type_traits> // conditional
  14680. // #include <nlohmann/detail/macro_scope.hpp>
  14681. NLOHMANN_JSON_NAMESPACE_BEGIN
  14682. namespace detail
  14683. {
  14684. /*!
  14685. @brief implements the Grisu2 algorithm for binary to decimal floating-point
  14686. conversion.
  14687. This implementation is a slightly modified version of the reference
  14688. implementation which may be obtained from
  14689. http://florian.loitsch.com/publications (bench.tar.gz).
  14690. The code is distributed under the MIT license, Copyright (c) 2009 Florian Loitsch.
  14691. For a detailed description of the algorithm see:
  14692. [1] Loitsch, "Printing Floating-Point Numbers Quickly and Accurately with
  14693. Integers", Proceedings of the ACM SIGPLAN 2010 Conference on Programming
  14694. Language Design and Implementation, PLDI 2010
  14695. [2] Burger, Dybvig, "Printing Floating-Point Numbers Quickly and Accurately",
  14696. Proceedings of the ACM SIGPLAN 1996 Conference on Programming Language
  14697. Design and Implementation, PLDI 1996
  14698. */
  14699. namespace dtoa_impl
  14700. {
  14701. template<typename Target, typename Source>
  14702. Target reinterpret_bits(const Source source)
  14703. {
  14704. static_assert(sizeof(Target) == sizeof(Source), "size mismatch");
  14705. Target target;
  14706. std::memcpy(&target, &source, sizeof(Source));
  14707. return target;
  14708. }
  14709. struct diyfp // f * 2^e
  14710. {
  14711. static constexpr int kPrecision = 64; // = q
  14712. std::uint64_t f = 0;
  14713. int e = 0;
  14714. constexpr diyfp(std::uint64_t f_, int e_) noexcept : f(f_), e(e_) {}
  14715. /*!
  14716. @brief returns x - y
  14717. @pre x.e == y.e and x.f >= y.f
  14718. */
  14719. static diyfp sub(const diyfp& x, const diyfp& y) noexcept
  14720. {
  14721. JSON_ASSERT(x.e == y.e);
  14722. JSON_ASSERT(x.f >= y.f);
  14723. return {x.f - y.f, x.e};
  14724. }
  14725. /*!
  14726. @brief returns x * y
  14727. @note The result is rounded. (Only the upper q bits are returned.)
  14728. */
  14729. static diyfp mul(const diyfp& x, const diyfp& y) noexcept
  14730. {
  14731. static_assert(kPrecision == 64, "internal error");
  14732. // Computes:
  14733. // f = round((x.f * y.f) / 2^q)
  14734. // e = x.e + y.e + q
  14735. // Emulate the 64-bit * 64-bit multiplication:
  14736. //
  14737. // p = u * v
  14738. // = (u_lo + 2^32 u_hi) (v_lo + 2^32 v_hi)
  14739. // = (u_lo v_lo ) + 2^32 ((u_lo v_hi ) + (u_hi v_lo )) + 2^64 (u_hi v_hi )
  14740. // = (p0 ) + 2^32 ((p1 ) + (p2 )) + 2^64 (p3 )
  14741. // = (p0_lo + 2^32 p0_hi) + 2^32 ((p1_lo + 2^32 p1_hi) + (p2_lo + 2^32 p2_hi)) + 2^64 (p3 )
  14742. // = (p0_lo ) + 2^32 (p0_hi + p1_lo + p2_lo ) + 2^64 (p1_hi + p2_hi + p3)
  14743. // = (p0_lo ) + 2^32 (Q ) + 2^64 (H )
  14744. // = (p0_lo ) + 2^32 (Q_lo + 2^32 Q_hi ) + 2^64 (H )
  14745. //
  14746. // (Since Q might be larger than 2^32 - 1)
  14747. //
  14748. // = (p0_lo + 2^32 Q_lo) + 2^64 (Q_hi + H)
  14749. //
  14750. // (Q_hi + H does not overflow a 64-bit int)
  14751. //
  14752. // = p_lo + 2^64 p_hi
  14753. const std::uint64_t u_lo = x.f & 0xFFFFFFFFu;
  14754. const std::uint64_t u_hi = x.f >> 32u;
  14755. const std::uint64_t v_lo = y.f & 0xFFFFFFFFu;
  14756. const std::uint64_t v_hi = y.f >> 32u;
  14757. const std::uint64_t p0 = u_lo * v_lo;
  14758. const std::uint64_t p1 = u_lo * v_hi;
  14759. const std::uint64_t p2 = u_hi * v_lo;
  14760. const std::uint64_t p3 = u_hi * v_hi;
  14761. const std::uint64_t p0_hi = p0 >> 32u;
  14762. const std::uint64_t p1_lo = p1 & 0xFFFFFFFFu;
  14763. const std::uint64_t p1_hi = p1 >> 32u;
  14764. const std::uint64_t p2_lo = p2 & 0xFFFFFFFFu;
  14765. const std::uint64_t p2_hi = p2 >> 32u;
  14766. std::uint64_t Q = p0_hi + p1_lo + p2_lo;
  14767. // The full product might now be computed as
  14768. //
  14769. // p_hi = p3 + p2_hi + p1_hi + (Q >> 32)
  14770. // p_lo = p0_lo + (Q << 32)
  14771. //
  14772. // But in this particular case here, the full p_lo is not required.
  14773. // Effectively we only need to add the highest bit in p_lo to p_hi (and
  14774. // Q_hi + 1 does not overflow).
  14775. Q += std::uint64_t{1} << (64u - 32u - 1u); // round, ties up
  14776. const std::uint64_t h = p3 + p2_hi + p1_hi + (Q >> 32u);
  14777. return {h, x.e + y.e + 64};
  14778. }
  14779. /*!
  14780. @brief normalize x such that the significand is >= 2^(q-1)
  14781. @pre x.f != 0
  14782. */
  14783. static diyfp normalize(diyfp x) noexcept
  14784. {
  14785. JSON_ASSERT(x.f != 0);
  14786. while ((x.f >> 63u) == 0)
  14787. {
  14788. x.f <<= 1u;
  14789. x.e--;
  14790. }
  14791. return x;
  14792. }
  14793. /*!
  14794. @brief normalize x such that the result has the exponent E
  14795. @pre e >= x.e and the upper e - x.e bits of x.f must be zero.
  14796. */
  14797. static diyfp normalize_to(const diyfp& x, const int target_exponent) noexcept
  14798. {
  14799. const int delta = x.e - target_exponent;
  14800. JSON_ASSERT(delta >= 0);
  14801. JSON_ASSERT(((x.f << delta) >> delta) == x.f);
  14802. return {x.f << delta, target_exponent};
  14803. }
  14804. };
  14805. struct boundaries
  14806. {
  14807. diyfp w;
  14808. diyfp minus;
  14809. diyfp plus;
  14810. };
  14811. /*!
  14812. Compute the (normalized) diyfp representing the input number 'value' and its
  14813. boundaries.
  14814. @pre value must be finite and positive
  14815. */
  14816. template<typename FloatType>
  14817. boundaries compute_boundaries(FloatType value)
  14818. {
  14819. JSON_ASSERT(std::isfinite(value));
  14820. JSON_ASSERT(value > 0);
  14821. // Convert the IEEE representation into a diyfp.
  14822. //
  14823. // If v is denormal:
  14824. // value = 0.F * 2^(1 - bias) = ( F) * 2^(1 - bias - (p-1))
  14825. // If v is normalized:
  14826. // value = 1.F * 2^(E - bias) = (2^(p-1) + F) * 2^(E - bias - (p-1))
  14827. static_assert(std::numeric_limits<FloatType>::is_iec559,
  14828. "internal error: dtoa_short requires an IEEE-754 floating-point implementation");
  14829. constexpr int kPrecision = std::numeric_limits<FloatType>::digits; // = p (includes the hidden bit)
  14830. constexpr int kBias = std::numeric_limits<FloatType>::max_exponent - 1 + (kPrecision - 1);
  14831. constexpr int kMinExp = 1 - kBias;
  14832. constexpr std::uint64_t kHiddenBit = std::uint64_t{1} << (kPrecision - 1); // = 2^(p-1)
  14833. using bits_type = typename std::conditional<kPrecision == 24, std::uint32_t, std::uint64_t >::type;
  14834. const auto bits = static_cast<std::uint64_t>(reinterpret_bits<bits_type>(value));
  14835. const std::uint64_t E = bits >> (kPrecision - 1);
  14836. const std::uint64_t F = bits & (kHiddenBit - 1);
  14837. const bool is_denormal = E == 0;
  14838. const diyfp v = is_denormal
  14839. ? diyfp(F, kMinExp)
  14840. : diyfp(F + kHiddenBit, static_cast<int>(E) - kBias);
  14841. // Compute the boundaries m- and m+ of the floating-point value
  14842. // v = f * 2^e.
  14843. //
  14844. // Determine v- and v+, the floating-point predecessor and successor if v,
  14845. // respectively.
  14846. //
  14847. // v- = v - 2^e if f != 2^(p-1) or e == e_min (A)
  14848. // = v - 2^(e-1) if f == 2^(p-1) and e > e_min (B)
  14849. //
  14850. // v+ = v + 2^e
  14851. //
  14852. // Let m- = (v- + v) / 2 and m+ = (v + v+) / 2. All real numbers _strictly_
  14853. // between m- and m+ round to v, regardless of how the input rounding
  14854. // algorithm breaks ties.
  14855. //
  14856. // ---+-------------+-------------+-------------+-------------+--- (A)
  14857. // v- m- v m+ v+
  14858. //
  14859. // -----------------+------+------+-------------+-------------+--- (B)
  14860. // v- m- v m+ v+
  14861. const bool lower_boundary_is_closer = F == 0 && E > 1;
  14862. const diyfp m_plus = diyfp(2 * v.f + 1, v.e - 1);
  14863. const diyfp m_minus = lower_boundary_is_closer
  14864. ? diyfp(4 * v.f - 1, v.e - 2) // (B)
  14865. : diyfp(2 * v.f - 1, v.e - 1); // (A)
  14866. // Determine the normalized w+ = m+.
  14867. const diyfp w_plus = diyfp::normalize(m_plus);
  14868. // Determine w- = m- such that e_(w-) = e_(w+).
  14869. const diyfp w_minus = diyfp::normalize_to(m_minus, w_plus.e);
  14870. return {diyfp::normalize(v), w_minus, w_plus};
  14871. }
  14872. // Given normalized diyfp w, Grisu needs to find a (normalized) cached
  14873. // power-of-ten c, such that the exponent of the product c * w = f * 2^e lies
  14874. // within a certain range [alpha, gamma] (Definition 3.2 from [1])
  14875. //
  14876. // alpha <= e = e_c + e_w + q <= gamma
  14877. //
  14878. // or
  14879. //
  14880. // f_c * f_w * 2^alpha <= f_c 2^(e_c) * f_w 2^(e_w) * 2^q
  14881. // <= f_c * f_w * 2^gamma
  14882. //
  14883. // Since c and w are normalized, i.e. 2^(q-1) <= f < 2^q, this implies
  14884. //
  14885. // 2^(q-1) * 2^(q-1) * 2^alpha <= c * w * 2^q < 2^q * 2^q * 2^gamma
  14886. //
  14887. // or
  14888. //
  14889. // 2^(q - 2 + alpha) <= c * w < 2^(q + gamma)
  14890. //
  14891. // The choice of (alpha,gamma) determines the size of the table and the form of
  14892. // the digit generation procedure. Using (alpha,gamma)=(-60,-32) works out well
  14893. // in practice:
  14894. //
  14895. // The idea is to cut the number c * w = f * 2^e into two parts, which can be
  14896. // processed independently: An integral part p1, and a fractional part p2:
  14897. //
  14898. // f * 2^e = ( (f div 2^-e) * 2^-e + (f mod 2^-e) ) * 2^e
  14899. // = (f div 2^-e) + (f mod 2^-e) * 2^e
  14900. // = p1 + p2 * 2^e
  14901. //
  14902. // The conversion of p1 into decimal form requires a series of divisions and
  14903. // modulos by (a power of) 10. These operations are faster for 32-bit than for
  14904. // 64-bit integers, so p1 should ideally fit into a 32-bit integer. This can be
  14905. // achieved by choosing
  14906. //
  14907. // -e >= 32 or e <= -32 := gamma
  14908. //
  14909. // In order to convert the fractional part
  14910. //
  14911. // p2 * 2^e = p2 / 2^-e = d[-1] / 10^1 + d[-2] / 10^2 + ...
  14912. //
  14913. // into decimal form, the fraction is repeatedly multiplied by 10 and the digits
  14914. // d[-i] are extracted in order:
  14915. //
  14916. // (10 * p2) div 2^-e = d[-1]
  14917. // (10 * p2) mod 2^-e = d[-2] / 10^1 + ...
  14918. //
  14919. // The multiplication by 10 must not overflow. It is sufficient to choose
  14920. //
  14921. // 10 * p2 < 16 * p2 = 2^4 * p2 <= 2^64.
  14922. //
  14923. // Since p2 = f mod 2^-e < 2^-e,
  14924. //
  14925. // -e <= 60 or e >= -60 := alpha
  14926. constexpr int kAlpha = -60;
  14927. constexpr int kGamma = -32;
  14928. struct cached_power // c = f * 2^e ~= 10^k
  14929. {
  14930. std::uint64_t f;
  14931. int e;
  14932. int k;
  14933. };
  14934. /*!
  14935. For a normalized diyfp w = f * 2^e, this function returns a (normalized) cached
  14936. power-of-ten c = f_c * 2^e_c, such that the exponent of the product w * c
  14937. satisfies (Definition 3.2 from [1])
  14938. alpha <= e_c + e + q <= gamma.
  14939. */
  14940. inline cached_power get_cached_power_for_binary_exponent(int e)
  14941. {
  14942. // Now
  14943. //
  14944. // alpha <= e_c + e + q <= gamma (1)
  14945. // ==> f_c * 2^alpha <= c * 2^e * 2^q
  14946. //
  14947. // and since the c's are normalized, 2^(q-1) <= f_c,
  14948. //
  14949. // ==> 2^(q - 1 + alpha) <= c * 2^(e + q)
  14950. // ==> 2^(alpha - e - 1) <= c
  14951. //
  14952. // If c were an exact power of ten, i.e. c = 10^k, one may determine k as
  14953. //
  14954. // k = ceil( log_10( 2^(alpha - e - 1) ) )
  14955. // = ceil( (alpha - e - 1) * log_10(2) )
  14956. //
  14957. // From the paper:
  14958. // "In theory the result of the procedure could be wrong since c is rounded,
  14959. // and the computation itself is approximated [...]. In practice, however,
  14960. // this simple function is sufficient."
  14961. //
  14962. // For IEEE double precision floating-point numbers converted into
  14963. // normalized diyfp's w = f * 2^e, with q = 64,
  14964. //
  14965. // e >= -1022 (min IEEE exponent)
  14966. // -52 (p - 1)
  14967. // -52 (p - 1, possibly normalize denormal IEEE numbers)
  14968. // -11 (normalize the diyfp)
  14969. // = -1137
  14970. //
  14971. // and
  14972. //
  14973. // e <= +1023 (max IEEE exponent)
  14974. // -52 (p - 1)
  14975. // -11 (normalize the diyfp)
  14976. // = 960
  14977. //
  14978. // This binary exponent range [-1137,960] results in a decimal exponent
  14979. // range [-307,324]. One does not need to store a cached power for each
  14980. // k in this range. For each such k it suffices to find a cached power
  14981. // such that the exponent of the product lies in [alpha,gamma].
  14982. // This implies that the difference of the decimal exponents of adjacent
  14983. // table entries must be less than or equal to
  14984. //
  14985. // floor( (gamma - alpha) * log_10(2) ) = 8.
  14986. //
  14987. // (A smaller distance gamma-alpha would require a larger table.)
  14988. // NB:
  14989. // Actually this function returns c, such that -60 <= e_c + e + 64 <= -34.
  14990. constexpr int kCachedPowersMinDecExp = -300;
  14991. constexpr int kCachedPowersDecStep = 8;
  14992. static constexpr std::array<cached_power, 79> kCachedPowers =
  14993. {
  14994. {
  14995. { 0xAB70FE17C79AC6CA, -1060, -300 },
  14996. { 0xFF77B1FCBEBCDC4F, -1034, -292 },
  14997. { 0xBE5691EF416BD60C, -1007, -284 },
  14998. { 0x8DD01FAD907FFC3C, -980, -276 },
  14999. { 0xD3515C2831559A83, -954, -268 },
  15000. { 0x9D71AC8FADA6C9B5, -927, -260 },
  15001. { 0xEA9C227723EE8BCB, -901, -252 },
  15002. { 0xAECC49914078536D, -874, -244 },
  15003. { 0x823C12795DB6CE57, -847, -236 },
  15004. { 0xC21094364DFB5637, -821, -228 },
  15005. { 0x9096EA6F3848984F, -794, -220 },
  15006. { 0xD77485CB25823AC7, -768, -212 },
  15007. { 0xA086CFCD97BF97F4, -741, -204 },
  15008. { 0xEF340A98172AACE5, -715, -196 },
  15009. { 0xB23867FB2A35B28E, -688, -188 },
  15010. { 0x84C8D4DFD2C63F3B, -661, -180 },
  15011. { 0xC5DD44271AD3CDBA, -635, -172 },
  15012. { 0x936B9FCEBB25C996, -608, -164 },
  15013. { 0xDBAC6C247D62A584, -582, -156 },
  15014. { 0xA3AB66580D5FDAF6, -555, -148 },
  15015. { 0xF3E2F893DEC3F126, -529, -140 },
  15016. { 0xB5B5ADA8AAFF80B8, -502, -132 },
  15017. { 0x87625F056C7C4A8B, -475, -124 },
  15018. { 0xC9BCFF6034C13053, -449, -116 },
  15019. { 0x964E858C91BA2655, -422, -108 },
  15020. { 0xDFF9772470297EBD, -396, -100 },
  15021. { 0xA6DFBD9FB8E5B88F, -369, -92 },
  15022. { 0xF8A95FCF88747D94, -343, -84 },
  15023. { 0xB94470938FA89BCF, -316, -76 },
  15024. { 0x8A08F0F8BF0F156B, -289, -68 },
  15025. { 0xCDB02555653131B6, -263, -60 },
  15026. { 0x993FE2C6D07B7FAC, -236, -52 },
  15027. { 0xE45C10C42A2B3B06, -210, -44 },
  15028. { 0xAA242499697392D3, -183, -36 },
  15029. { 0xFD87B5F28300CA0E, -157, -28 },
  15030. { 0xBCE5086492111AEB, -130, -20 },
  15031. { 0x8CBCCC096F5088CC, -103, -12 },
  15032. { 0xD1B71758E219652C, -77, -4 },
  15033. { 0x9C40000000000000, -50, 4 },
  15034. { 0xE8D4A51000000000, -24, 12 },
  15035. { 0xAD78EBC5AC620000, 3, 20 },
  15036. { 0x813F3978F8940984, 30, 28 },
  15037. { 0xC097CE7BC90715B3, 56, 36 },
  15038. { 0x8F7E32CE7BEA5C70, 83, 44 },
  15039. { 0xD5D238A4ABE98068, 109, 52 },
  15040. { 0x9F4F2726179A2245, 136, 60 },
  15041. { 0xED63A231D4C4FB27, 162, 68 },
  15042. { 0xB0DE65388CC8ADA8, 189, 76 },
  15043. { 0x83C7088E1AAB65DB, 216, 84 },
  15044. { 0xC45D1DF942711D9A, 242, 92 },
  15045. { 0x924D692CA61BE758, 269, 100 },
  15046. { 0xDA01EE641A708DEA, 295, 108 },
  15047. { 0xA26DA3999AEF774A, 322, 116 },
  15048. { 0xF209787BB47D6B85, 348, 124 },
  15049. { 0xB454E4A179DD1877, 375, 132 },
  15050. { 0x865B86925B9BC5C2, 402, 140 },
  15051. { 0xC83553C5C8965D3D, 428, 148 },
  15052. { 0x952AB45CFA97A0B3, 455, 156 },
  15053. { 0xDE469FBD99A05FE3, 481, 164 },
  15054. { 0xA59BC234DB398C25, 508, 172 },
  15055. { 0xF6C69A72A3989F5C, 534, 180 },
  15056. { 0xB7DCBF5354E9BECE, 561, 188 },
  15057. { 0x88FCF317F22241E2, 588, 196 },
  15058. { 0xCC20CE9BD35C78A5, 614, 204 },
  15059. { 0x98165AF37B2153DF, 641, 212 },
  15060. { 0xE2A0B5DC971F303A, 667, 220 },
  15061. { 0xA8D9D1535CE3B396, 694, 228 },
  15062. { 0xFB9B7CD9A4A7443C, 720, 236 },
  15063. { 0xBB764C4CA7A44410, 747, 244 },
  15064. { 0x8BAB8EEFB6409C1A, 774, 252 },
  15065. { 0xD01FEF10A657842C, 800, 260 },
  15066. { 0x9B10A4E5E9913129, 827, 268 },
  15067. { 0xE7109BFBA19C0C9D, 853, 276 },
  15068. { 0xAC2820D9623BF429, 880, 284 },
  15069. { 0x80444B5E7AA7CF85, 907, 292 },
  15070. { 0xBF21E44003ACDD2D, 933, 300 },
  15071. { 0x8E679C2F5E44FF8F, 960, 308 },
  15072. { 0xD433179D9C8CB841, 986, 316 },
  15073. { 0x9E19DB92B4E31BA9, 1013, 324 },
  15074. }
  15075. };
  15076. // This computation gives exactly the same results for k as
  15077. // k = ceil((kAlpha - e - 1) * 0.30102999566398114)
  15078. // for |e| <= 1500, but doesn't require floating-point operations.
  15079. // NB: log_10(2) ~= 78913 / 2^18
  15080. JSON_ASSERT(e >= -1500);
  15081. JSON_ASSERT(e <= 1500);
  15082. const int f = kAlpha - e - 1;
  15083. const int k = (f * 78913) / (1 << 18) + static_cast<int>(f > 0);
  15084. const int index = (-kCachedPowersMinDecExp + k + (kCachedPowersDecStep - 1)) / kCachedPowersDecStep;
  15085. JSON_ASSERT(index >= 0);
  15086. JSON_ASSERT(static_cast<std::size_t>(index) < kCachedPowers.size());
  15087. const cached_power cached = kCachedPowers[static_cast<std::size_t>(index)];
  15088. JSON_ASSERT(kAlpha <= cached.e + e + 64);
  15089. JSON_ASSERT(kGamma >= cached.e + e + 64);
  15090. return cached;
  15091. }
  15092. /*!
  15093. For n != 0, returns k, such that pow10 := 10^(k-1) <= n < 10^k.
  15094. For n == 0, returns 1 and sets pow10 := 1.
  15095. */
  15096. inline int find_largest_pow10(const std::uint32_t n, std::uint32_t& pow10)
  15097. {
  15098. // LCOV_EXCL_START
  15099. if (n >= 1000000000)
  15100. {
  15101. pow10 = 1000000000;
  15102. return 10;
  15103. }
  15104. // LCOV_EXCL_STOP
  15105. if (n >= 100000000)
  15106. {
  15107. pow10 = 100000000;
  15108. return 9;
  15109. }
  15110. if (n >= 10000000)
  15111. {
  15112. pow10 = 10000000;
  15113. return 8;
  15114. }
  15115. if (n >= 1000000)
  15116. {
  15117. pow10 = 1000000;
  15118. return 7;
  15119. }
  15120. if (n >= 100000)
  15121. {
  15122. pow10 = 100000;
  15123. return 6;
  15124. }
  15125. if (n >= 10000)
  15126. {
  15127. pow10 = 10000;
  15128. return 5;
  15129. }
  15130. if (n >= 1000)
  15131. {
  15132. pow10 = 1000;
  15133. return 4;
  15134. }
  15135. if (n >= 100)
  15136. {
  15137. pow10 = 100;
  15138. return 3;
  15139. }
  15140. if (n >= 10)
  15141. {
  15142. pow10 = 10;
  15143. return 2;
  15144. }
  15145. pow10 = 1;
  15146. return 1;
  15147. }
  15148. inline void grisu2_round(char* buf, int len, std::uint64_t dist, std::uint64_t delta,
  15149. std::uint64_t rest, std::uint64_t ten_k)
  15150. {
  15151. JSON_ASSERT(len >= 1);
  15152. JSON_ASSERT(dist <= delta);
  15153. JSON_ASSERT(rest <= delta);
  15154. JSON_ASSERT(ten_k > 0);
  15155. // <--------------------------- delta ---->
  15156. // <---- dist --------->
  15157. // --------------[------------------+-------------------]--------------
  15158. // M- w M+
  15159. //
  15160. // ten_k
  15161. // <------>
  15162. // <---- rest ---->
  15163. // --------------[------------------+----+--------------]--------------
  15164. // w V
  15165. // = buf * 10^k
  15166. //
  15167. // ten_k represents a unit-in-the-last-place in the decimal representation
  15168. // stored in buf.
  15169. // Decrement buf by ten_k while this takes buf closer to w.
  15170. // The tests are written in this order to avoid overflow in unsigned
  15171. // integer arithmetic.
  15172. while (rest < dist
  15173. && delta - rest >= ten_k
  15174. && (rest + ten_k < dist || dist - rest > rest + ten_k - dist))
  15175. {
  15176. JSON_ASSERT(buf[len - 1] != '0');
  15177. buf[len - 1]--;
  15178. rest += ten_k;
  15179. }
  15180. }
  15181. /*!
  15182. Generates V = buffer * 10^decimal_exponent, such that M- <= V <= M+.
  15183. M- and M+ must be normalized and share the same exponent -60 <= e <= -32.
  15184. */
  15185. inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent,
  15186. diyfp M_minus, diyfp w, diyfp M_plus)
  15187. {
  15188. static_assert(kAlpha >= -60, "internal error");
  15189. static_assert(kGamma <= -32, "internal error");
  15190. // Generates the digits (and the exponent) of a decimal floating-point
  15191. // number V = buffer * 10^decimal_exponent in the range [M-, M+]. The diyfp's
  15192. // w, M- and M+ share the same exponent e, which satisfies alpha <= e <= gamma.
  15193. //
  15194. // <--------------------------- delta ---->
  15195. // <---- dist --------->
  15196. // --------------[------------------+-------------------]--------------
  15197. // M- w M+
  15198. //
  15199. // Grisu2 generates the digits of M+ from left to right and stops as soon as
  15200. // V is in [M-,M+].
  15201. JSON_ASSERT(M_plus.e >= kAlpha);
  15202. JSON_ASSERT(M_plus.e <= kGamma);
  15203. std::uint64_t delta = diyfp::sub(M_plus, M_minus).f; // (significand of (M+ - M-), implicit exponent is e)
  15204. std::uint64_t dist = diyfp::sub(M_plus, w ).f; // (significand of (M+ - w ), implicit exponent is e)
  15205. // Split M+ = f * 2^e into two parts p1 and p2 (note: e < 0):
  15206. //
  15207. // M+ = f * 2^e
  15208. // = ((f div 2^-e) * 2^-e + (f mod 2^-e)) * 2^e
  15209. // = ((p1 ) * 2^-e + (p2 )) * 2^e
  15210. // = p1 + p2 * 2^e
  15211. const diyfp one(std::uint64_t{1} << -M_plus.e, M_plus.e);
  15212. 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.)
  15213. std::uint64_t p2 = M_plus.f & (one.f - 1); // p2 = f mod 2^-e
  15214. // 1)
  15215. //
  15216. // Generate the digits of the integral part p1 = d[n-1]...d[1]d[0]
  15217. JSON_ASSERT(p1 > 0);
  15218. std::uint32_t pow10{};
  15219. const int k = find_largest_pow10(p1, pow10);
  15220. // 10^(k-1) <= p1 < 10^k, pow10 = 10^(k-1)
  15221. //
  15222. // p1 = (p1 div 10^(k-1)) * 10^(k-1) + (p1 mod 10^(k-1))
  15223. // = (d[k-1] ) * 10^(k-1) + (p1 mod 10^(k-1))
  15224. //
  15225. // M+ = p1 + p2 * 2^e
  15226. // = d[k-1] * 10^(k-1) + (p1 mod 10^(k-1)) + p2 * 2^e
  15227. // = d[k-1] * 10^(k-1) + ((p1 mod 10^(k-1)) * 2^-e + p2) * 2^e
  15228. // = d[k-1] * 10^(k-1) + ( rest) * 2^e
  15229. //
  15230. // Now generate the digits d[n] of p1 from left to right (n = k-1,...,0)
  15231. //
  15232. // p1 = d[k-1]...d[n] * 10^n + d[n-1]...d[0]
  15233. //
  15234. // but stop as soon as
  15235. //
  15236. // rest * 2^e = (d[n-1]...d[0] * 2^-e + p2) * 2^e <= delta * 2^e
  15237. int n = k;
  15238. while (n > 0)
  15239. {
  15240. // Invariants:
  15241. // M+ = buffer * 10^n + (p1 + p2 * 2^e) (buffer = 0 for n = k)
  15242. // pow10 = 10^(n-1) <= p1 < 10^n
  15243. //
  15244. const std::uint32_t d = p1 / pow10; // d = p1 div 10^(n-1)
  15245. const std::uint32_t r = p1 % pow10; // r = p1 mod 10^(n-1)
  15246. //
  15247. // M+ = buffer * 10^n + (d * 10^(n-1) + r) + p2 * 2^e
  15248. // = (buffer * 10 + d) * 10^(n-1) + (r + p2 * 2^e)
  15249. //
  15250. JSON_ASSERT(d <= 9);
  15251. buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
  15252. //
  15253. // M+ = buffer * 10^(n-1) + (r + p2 * 2^e)
  15254. //
  15255. p1 = r;
  15256. n--;
  15257. //
  15258. // M+ = buffer * 10^n + (p1 + p2 * 2^e)
  15259. // pow10 = 10^n
  15260. //
  15261. // Now check if enough digits have been generated.
  15262. // Compute
  15263. //
  15264. // p1 + p2 * 2^e = (p1 * 2^-e + p2) * 2^e = rest * 2^e
  15265. //
  15266. // Note:
  15267. // Since rest and delta share the same exponent e, it suffices to
  15268. // compare the significands.
  15269. const std::uint64_t rest = (std::uint64_t{p1} << -one.e) + p2;
  15270. if (rest <= delta)
  15271. {
  15272. // V = buffer * 10^n, with M- <= V <= M+.
  15273. decimal_exponent += n;
  15274. // We may now just stop. But instead look if the buffer could be
  15275. // decremented to bring V closer to w.
  15276. //
  15277. // pow10 = 10^n is now 1 ulp in the decimal representation V.
  15278. // The rounding procedure works with diyfp's with an implicit
  15279. // exponent of e.
  15280. //
  15281. // 10^n = (10^n * 2^-e) * 2^e = ulp * 2^e
  15282. //
  15283. const std::uint64_t ten_n = std::uint64_t{pow10} << -one.e;
  15284. grisu2_round(buffer, length, dist, delta, rest, ten_n);
  15285. return;
  15286. }
  15287. pow10 /= 10;
  15288. //
  15289. // pow10 = 10^(n-1) <= p1 < 10^n
  15290. // Invariants restored.
  15291. }
  15292. // 2)
  15293. //
  15294. // The digits of the integral part have been generated:
  15295. //
  15296. // M+ = d[k-1]...d[1]d[0] + p2 * 2^e
  15297. // = buffer + p2 * 2^e
  15298. //
  15299. // Now generate the digits of the fractional part p2 * 2^e.
  15300. //
  15301. // Note:
  15302. // No decimal point is generated: the exponent is adjusted instead.
  15303. //
  15304. // p2 actually represents the fraction
  15305. //
  15306. // p2 * 2^e
  15307. // = p2 / 2^-e
  15308. // = d[-1] / 10^1 + d[-2] / 10^2 + ...
  15309. //
  15310. // Now generate the digits d[-m] of p1 from left to right (m = 1,2,...)
  15311. //
  15312. // p2 * 2^e = d[-1]d[-2]...d[-m] * 10^-m
  15313. // + 10^-m * (d[-m-1] / 10^1 + d[-m-2] / 10^2 + ...)
  15314. //
  15315. // using
  15316. //
  15317. // 10^m * p2 = ((10^m * p2) div 2^-e) * 2^-e + ((10^m * p2) mod 2^-e)
  15318. // = ( d) * 2^-e + ( r)
  15319. //
  15320. // or
  15321. // 10^m * p2 * 2^e = d + r * 2^e
  15322. //
  15323. // i.e.
  15324. //
  15325. // M+ = buffer + p2 * 2^e
  15326. // = buffer + 10^-m * (d + r * 2^e)
  15327. // = (buffer * 10^m + d) * 10^-m + 10^-m * r * 2^e
  15328. //
  15329. // and stop as soon as 10^-m * r * 2^e <= delta * 2^e
  15330. JSON_ASSERT(p2 > delta);
  15331. int m = 0;
  15332. for (;;)
  15333. {
  15334. // Invariant:
  15335. // M+ = buffer * 10^-m + 10^-m * (d[-m-1] / 10 + d[-m-2] / 10^2 + ...) * 2^e
  15336. // = buffer * 10^-m + 10^-m * (p2 ) * 2^e
  15337. // = buffer * 10^-m + 10^-m * (1/10 * (10 * p2) ) * 2^e
  15338. // = buffer * 10^-m + 10^-m * (1/10 * ((10*p2 div 2^-e) * 2^-e + (10*p2 mod 2^-e)) * 2^e
  15339. //
  15340. JSON_ASSERT(p2 <= (std::numeric_limits<std::uint64_t>::max)() / 10);
  15341. p2 *= 10;
  15342. const std::uint64_t d = p2 >> -one.e; // d = (10 * p2) div 2^-e
  15343. const std::uint64_t r = p2 & (one.f - 1); // r = (10 * p2) mod 2^-e
  15344. //
  15345. // M+ = buffer * 10^-m + 10^-m * (1/10 * (d * 2^-e + r) * 2^e
  15346. // = buffer * 10^-m + 10^-m * (1/10 * (d + r * 2^e))
  15347. // = (buffer * 10 + d) * 10^(-m-1) + 10^(-m-1) * r * 2^e
  15348. //
  15349. JSON_ASSERT(d <= 9);
  15350. buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
  15351. //
  15352. // M+ = buffer * 10^(-m-1) + 10^(-m-1) * r * 2^e
  15353. //
  15354. p2 = r;
  15355. m++;
  15356. //
  15357. // M+ = buffer * 10^-m + 10^-m * p2 * 2^e
  15358. // Invariant restored.
  15359. // Check if enough digits have been generated.
  15360. //
  15361. // 10^-m * p2 * 2^e <= delta * 2^e
  15362. // p2 * 2^e <= 10^m * delta * 2^e
  15363. // p2 <= 10^m * delta
  15364. delta *= 10;
  15365. dist *= 10;
  15366. if (p2 <= delta)
  15367. {
  15368. break;
  15369. }
  15370. }
  15371. // V = buffer * 10^-m, with M- <= V <= M+.
  15372. decimal_exponent -= m;
  15373. // 1 ulp in the decimal representation is now 10^-m.
  15374. // Since delta and dist are now scaled by 10^m, we need to do the
  15375. // same with ulp in order to keep the units in sync.
  15376. //
  15377. // 10^m * 10^-m = 1 = 2^-e * 2^e = ten_m * 2^e
  15378. //
  15379. const std::uint64_t ten_m = one.f;
  15380. grisu2_round(buffer, length, dist, delta, p2, ten_m);
  15381. // By construction this algorithm generates the shortest possible decimal
  15382. // number (Loitsch, Theorem 6.2) which rounds back to w.
  15383. // For an input number of precision p, at least
  15384. //
  15385. // N = 1 + ceil(p * log_10(2))
  15386. //
  15387. // decimal digits are sufficient to identify all binary floating-point
  15388. // numbers (Matula, "In-and-Out conversions").
  15389. // This implies that the algorithm does not produce more than N decimal
  15390. // digits.
  15391. //
  15392. // N = 17 for p = 53 (IEEE double precision)
  15393. // N = 9 for p = 24 (IEEE single precision)
  15394. }
  15395. /*!
  15396. v = buf * 10^decimal_exponent
  15397. len is the length of the buffer (number of decimal digits)
  15398. The buffer must be large enough, i.e. >= max_digits10.
  15399. */
  15400. JSON_HEDLEY_NON_NULL(1)
  15401. inline void grisu2(char* buf, int& len, int& decimal_exponent,
  15402. diyfp m_minus, diyfp v, diyfp m_plus)
  15403. {
  15404. JSON_ASSERT(m_plus.e == m_minus.e);
  15405. JSON_ASSERT(m_plus.e == v.e);
  15406. // --------(-----------------------+-----------------------)-------- (A)
  15407. // m- v m+
  15408. //
  15409. // --------------------(-----------+-----------------------)-------- (B)
  15410. // m- v m+
  15411. //
  15412. // First scale v (and m- and m+) such that the exponent is in the range
  15413. // [alpha, gamma].
  15414. const cached_power cached = get_cached_power_for_binary_exponent(m_plus.e);
  15415. const diyfp c_minus_k(cached.f, cached.e); // = c ~= 10^-k
  15416. // The exponent of the products is = v.e + c_minus_k.e + q and is in the range [alpha,gamma]
  15417. const diyfp w = diyfp::mul(v, c_minus_k);
  15418. const diyfp w_minus = diyfp::mul(m_minus, c_minus_k);
  15419. const diyfp w_plus = diyfp::mul(m_plus, c_minus_k);
  15420. // ----(---+---)---------------(---+---)---------------(---+---)----
  15421. // w- w w+
  15422. // = c*m- = c*v = c*m+
  15423. //
  15424. // diyfp::mul rounds its result and c_minus_k is approximated too. w, w- and
  15425. // w+ are now off by a small amount.
  15426. // In fact:
  15427. //
  15428. // w - v * 10^k < 1 ulp
  15429. //
  15430. // To account for this inaccuracy, add resp. subtract 1 ulp.
  15431. //
  15432. // --------+---[---------------(---+---)---------------]---+--------
  15433. // w- M- w M+ w+
  15434. //
  15435. // Now any number in [M-, M+] (bounds included) will round to w when input,
  15436. // regardless of how the input rounding algorithm breaks ties.
  15437. //
  15438. // And digit_gen generates the shortest possible such number in [M-, M+].
  15439. // Note that this does not mean that Grisu2 always generates the shortest
  15440. // possible number in the interval (m-, m+).
  15441. const diyfp M_minus(w_minus.f + 1, w_minus.e);
  15442. const diyfp M_plus (w_plus.f - 1, w_plus.e );
  15443. decimal_exponent = -cached.k; // = -(-k) = k
  15444. grisu2_digit_gen(buf, len, decimal_exponent, M_minus, w, M_plus);
  15445. }
  15446. /*!
  15447. v = buf * 10^decimal_exponent
  15448. len is the length of the buffer (number of decimal digits)
  15449. The buffer must be large enough, i.e. >= max_digits10.
  15450. */
  15451. template<typename FloatType>
  15452. JSON_HEDLEY_NON_NULL(1)
  15453. void grisu2(char* buf, int& len, int& decimal_exponent, FloatType value)
  15454. {
  15455. static_assert(diyfp::kPrecision >= std::numeric_limits<FloatType>::digits + 3,
  15456. "internal error: not enough precision");
  15457. JSON_ASSERT(std::isfinite(value));
  15458. JSON_ASSERT(value > 0);
  15459. // If the neighbors (and boundaries) of 'value' are always computed for double-precision
  15460. // numbers, all float's can be recovered using strtod (and strtof). However, the resulting
  15461. // decimal representations are not exactly "short".
  15462. //
  15463. // The documentation for 'std::to_chars' (https://en.cppreference.com/w/cpp/utility/to_chars)
  15464. // says "value is converted to a string as if by std::sprintf in the default ("C") locale"
  15465. // and since sprintf promotes floats to doubles, I think this is exactly what 'std::to_chars'
  15466. // does.
  15467. // On the other hand, the documentation for 'std::to_chars' requires that "parsing the
  15468. // representation using the corresponding std::from_chars function recovers value exactly". That
  15469. // indicates that single precision floating-point numbers should be recovered using
  15470. // 'std::strtof'.
  15471. //
  15472. // NB: If the neighbors are computed for single-precision numbers, there is a single float
  15473. // (7.0385307e-26f) which can't be recovered using strtod. The resulting double precision
  15474. // value is off by 1 ulp.
  15475. #if 0 // NOLINT(readability-avoid-unconditional-preprocessor-if)
  15476. const boundaries w = compute_boundaries(static_cast<double>(value));
  15477. #else
  15478. const boundaries w = compute_boundaries(value);
  15479. #endif
  15480. grisu2(buf, len, decimal_exponent, w.minus, w.w, w.plus);
  15481. }
  15482. /*!
  15483. @brief appends a decimal representation of e to buf
  15484. @return a pointer to the element following the exponent.
  15485. @pre -1000 < e < 1000
  15486. */
  15487. JSON_HEDLEY_NON_NULL(1)
  15488. JSON_HEDLEY_RETURNS_NON_NULL
  15489. inline char* append_exponent(char* buf, int e)
  15490. {
  15491. JSON_ASSERT(e > -1000);
  15492. JSON_ASSERT(e < 1000);
  15493. if (e < 0)
  15494. {
  15495. e = -e;
  15496. *buf++ = '-';
  15497. }
  15498. else
  15499. {
  15500. *buf++ = '+';
  15501. }
  15502. auto k = static_cast<std::uint32_t>(e);
  15503. if (k < 10)
  15504. {
  15505. // Always print at least two digits in the exponent.
  15506. // This is for compatibility with printf("%g").
  15507. *buf++ = '0';
  15508. *buf++ = static_cast<char>('0' + k);
  15509. }
  15510. else if (k < 100)
  15511. {
  15512. *buf++ = static_cast<char>('0' + k / 10);
  15513. k %= 10;
  15514. *buf++ = static_cast<char>('0' + k);
  15515. }
  15516. else
  15517. {
  15518. *buf++ = static_cast<char>('0' + k / 100);
  15519. k %= 100;
  15520. *buf++ = static_cast<char>('0' + k / 10);
  15521. k %= 10;
  15522. *buf++ = static_cast<char>('0' + k);
  15523. }
  15524. return buf;
  15525. }
  15526. /*!
  15527. @brief prettify v = buf * 10^decimal_exponent
  15528. If v is in the range [10^min_exp, 10^max_exp) it will be printed in fixed-point
  15529. notation. Otherwise it will be printed in exponential notation.
  15530. @pre min_exp < 0
  15531. @pre max_exp > 0
  15532. */
  15533. JSON_HEDLEY_NON_NULL(1)
  15534. JSON_HEDLEY_RETURNS_NON_NULL
  15535. inline char* format_buffer(char* buf, int len, int decimal_exponent,
  15536. int min_exp, int max_exp)
  15537. {
  15538. JSON_ASSERT(min_exp < 0);
  15539. JSON_ASSERT(max_exp > 0);
  15540. const int k = len;
  15541. const int n = len + decimal_exponent;
  15542. // v = buf * 10^(n-k)
  15543. // k is the length of the buffer (number of decimal digits)
  15544. // n is the position of the decimal point relative to the start of the buffer.
  15545. if (k <= n && n <= max_exp)
  15546. {
  15547. // digits[000]
  15548. // len <= max_exp + 2
  15549. std::memset(buf + k, '0', static_cast<size_t>(n) - static_cast<size_t>(k));
  15550. // Make it look like a floating-point number (#362, #378)
  15551. buf[n + 0] = '.';
  15552. buf[n + 1] = '0';
  15553. return buf + (static_cast<size_t>(n) + 2);
  15554. }
  15555. if (0 < n && n <= max_exp)
  15556. {
  15557. // dig.its
  15558. // len <= max_digits10 + 1
  15559. JSON_ASSERT(k > n);
  15560. std::memmove(buf + (static_cast<size_t>(n) + 1), buf + n, static_cast<size_t>(k) - static_cast<size_t>(n));
  15561. buf[n] = '.';
  15562. return buf + (static_cast<size_t>(k) + 1U);
  15563. }
  15564. if (min_exp < n && n <= 0)
  15565. {
  15566. // 0.[000]digits
  15567. // len <= 2 + (-min_exp - 1) + max_digits10
  15568. std::memmove(buf + (2 + static_cast<size_t>(-n)), buf, static_cast<size_t>(k));
  15569. buf[0] = '0';
  15570. buf[1] = '.';
  15571. std::memset(buf + 2, '0', static_cast<size_t>(-n));
  15572. return buf + (2U + static_cast<size_t>(-n) + static_cast<size_t>(k));
  15573. }
  15574. if (k == 1)
  15575. {
  15576. // dE+123
  15577. // len <= 1 + 5
  15578. buf += 1;
  15579. }
  15580. else
  15581. {
  15582. // d.igitsE+123
  15583. // len <= max_digits10 + 1 + 5
  15584. std::memmove(buf + 2, buf + 1, static_cast<size_t>(k) - 1);
  15585. buf[1] = '.';
  15586. buf += 1 + static_cast<size_t>(k);
  15587. }
  15588. *buf++ = 'e';
  15589. return append_exponent(buf, n - 1);
  15590. }
  15591. } // namespace dtoa_impl
  15592. /*!
  15593. @brief generates a decimal representation of the floating-point number value in [first, last).
  15594. The format of the resulting decimal representation is similar to printf's %g
  15595. format. Returns an iterator pointing past-the-end of the decimal representation.
  15596. @note The input number must be finite, i.e. NaN's and Inf's are not supported.
  15597. @note The buffer must be large enough.
  15598. @note The result is NOT null-terminated.
  15599. */
  15600. template<typename FloatType>
  15601. JSON_HEDLEY_NON_NULL(1, 2)
  15602. JSON_HEDLEY_RETURNS_NON_NULL
  15603. char* to_chars(char* first, const char* last, FloatType value)
  15604. {
  15605. static_cast<void>(last); // maybe unused - fix warning
  15606. JSON_ASSERT(std::isfinite(value));
  15607. // Use signbit(value) instead of (value < 0) since signbit works for -0.
  15608. if (std::signbit(value))
  15609. {
  15610. value = -value;
  15611. *first++ = '-';
  15612. }
  15613. #ifdef __GNUC__
  15614. #pragma GCC diagnostic push
  15615. #pragma GCC diagnostic ignored "-Wfloat-equal"
  15616. #endif
  15617. if (value == 0) // +-0
  15618. {
  15619. *first++ = '0';
  15620. // Make it look like a floating-point number (#362, #378)
  15621. *first++ = '.';
  15622. *first++ = '0';
  15623. return first;
  15624. }
  15625. #ifdef __GNUC__
  15626. #pragma GCC diagnostic pop
  15627. #endif
  15628. JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10);
  15629. // Compute v = buffer * 10^decimal_exponent.
  15630. // The decimal digits are stored in the buffer, which needs to be interpreted
  15631. // as an unsigned decimal integer.
  15632. // len is the length of the buffer, i.e. the number of decimal digits.
  15633. int len = 0;
  15634. int decimal_exponent = 0;
  15635. dtoa_impl::grisu2(first, len, decimal_exponent, value);
  15636. JSON_ASSERT(len <= std::numeric_limits<FloatType>::max_digits10);
  15637. // Format the buffer like printf("%.*g", prec, value)
  15638. constexpr int kMinExp = -4;
  15639. // Use digits10 here to increase compatibility with version 2.
  15640. constexpr int kMaxExp = std::numeric_limits<FloatType>::digits10;
  15641. JSON_ASSERT(last - first >= kMaxExp + 2);
  15642. JSON_ASSERT(last - first >= 2 + (-kMinExp - 1) + std::numeric_limits<FloatType>::max_digits10);
  15643. JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10 + 6);
  15644. return dtoa_impl::format_buffer(first, len, decimal_exponent, kMinExp, kMaxExp);
  15645. }
  15646. } // namespace detail
  15647. NLOHMANN_JSON_NAMESPACE_END
  15648. // #include <nlohmann/detail/exceptions.hpp>
  15649. // #include <nlohmann/detail/macro_scope.hpp>
  15650. // #include <nlohmann/detail/meta/cpp_future.hpp>
  15651. // #include <nlohmann/detail/output/binary_writer.hpp>
  15652. // #include <nlohmann/detail/output/output_adapters.hpp>
  15653. // #include <nlohmann/detail/string_concat.hpp>
  15654. // #include <nlohmann/detail/value_t.hpp>
  15655. NLOHMANN_JSON_NAMESPACE_BEGIN
  15656. namespace detail
  15657. {
  15658. ///////////////////
  15659. // serialization //
  15660. ///////////////////
  15661. /// how to treat decoding errors
  15662. enum class error_handler_t
  15663. {
  15664. strict, ///< throw a type_error exception in case of invalid UTF-8
  15665. replace, ///< replace invalid UTF-8 sequences with U+FFFD
  15666. ignore ///< ignore invalid UTF-8 sequences
  15667. };
  15668. template<typename BasicJsonType>
  15669. class serializer
  15670. {
  15671. using string_t = typename BasicJsonType::string_t;
  15672. using number_float_t = typename BasicJsonType::number_float_t;
  15673. using number_integer_t = typename BasicJsonType::number_integer_t;
  15674. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  15675. using binary_char_t = typename BasicJsonType::binary_t::value_type;
  15676. static constexpr std::uint8_t UTF8_ACCEPT = 0;
  15677. static constexpr std::uint8_t UTF8_REJECT = 1;
  15678. public:
  15679. /*!
  15680. @param[in] s output stream to serialize to
  15681. @param[in] ichar indentation character to use
  15682. @param[in] error_handler_ how to react on decoding errors
  15683. */
  15684. serializer(output_adapter_t<char> s, const char ichar,
  15685. error_handler_t error_handler_ = error_handler_t::strict)
  15686. : o(std::move(s))
  15687. , loc(std::localeconv())
  15688. , thousands_sep(loc->thousands_sep == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->thousands_sep)))
  15689. , decimal_point(loc->decimal_point == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->decimal_point)))
  15690. , indent_char(ichar)
  15691. , indent_string(512, indent_char)
  15692. , error_handler(error_handler_)
  15693. {}
  15694. // delete because of pointer members
  15695. serializer(const serializer&) = delete;
  15696. serializer& operator=(const serializer&) = delete;
  15697. serializer(serializer&&) = delete;
  15698. serializer& operator=(serializer&&) = delete;
  15699. ~serializer() = default;
  15700. /*!
  15701. @brief internal implementation of the serialization function
  15702. This function is called by the public member function dump and organizes
  15703. the serialization internally. The indentation level is propagated as
  15704. additional parameter. In case of arrays and objects, the function is
  15705. called recursively.
  15706. - strings and object keys are escaped using `escape_string()`
  15707. - integer numbers are converted implicitly via `operator<<`
  15708. - floating-point numbers are converted to a string using `"%g"` format
  15709. - binary values are serialized as objects containing the subtype and the
  15710. byte array
  15711. @param[in] val value to serialize
  15712. @param[in] pretty_print whether the output shall be pretty-printed
  15713. @param[in] ensure_ascii If @a ensure_ascii is true, all non-ASCII characters
  15714. in the output are escaped with `\uXXXX` sequences, and the result consists
  15715. of ASCII characters only.
  15716. @param[in] indent_step the indent level
  15717. @param[in] current_indent the current indent level (only used internally)
  15718. */
  15719. void dump(const BasicJsonType& val,
  15720. const bool pretty_print,
  15721. const bool ensure_ascii,
  15722. const unsigned int indent_step,
  15723. const unsigned int current_indent = 0)
  15724. {
  15725. switch (val.m_data.m_type)
  15726. {
  15727. case value_t::object:
  15728. {
  15729. if (val.m_data.m_value.object->empty())
  15730. {
  15731. o->write_characters("{}", 2);
  15732. return;
  15733. }
  15734. if (pretty_print)
  15735. {
  15736. o->write_characters("{\n", 2);
  15737. // variable to hold indentation for recursive calls
  15738. const auto new_indent = current_indent + indent_step;
  15739. if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
  15740. {
  15741. indent_string.resize(indent_string.size() * 2, ' ');
  15742. }
  15743. // first n-1 elements
  15744. auto i = val.m_data.m_value.object->cbegin();
  15745. for (std::size_t cnt = 0; cnt < val.m_data.m_value.object->size() - 1; ++cnt, ++i)
  15746. {
  15747. o->write_characters(indent_string.c_str(), new_indent);
  15748. o->write_character('\"');
  15749. dump_escaped(i->first, ensure_ascii);
  15750. o->write_characters("\": ", 3);
  15751. dump(i->second, true, ensure_ascii, indent_step, new_indent);
  15752. o->write_characters(",\n", 2);
  15753. }
  15754. // last element
  15755. JSON_ASSERT(i != val.m_data.m_value.object->cend());
  15756. JSON_ASSERT(std::next(i) == val.m_data.m_value.object->cend());
  15757. o->write_characters(indent_string.c_str(), new_indent);
  15758. o->write_character('\"');
  15759. dump_escaped(i->first, ensure_ascii);
  15760. o->write_characters("\": ", 3);
  15761. dump(i->second, true, ensure_ascii, indent_step, new_indent);
  15762. o->write_character('\n');
  15763. o->write_characters(indent_string.c_str(), current_indent);
  15764. o->write_character('}');
  15765. }
  15766. else
  15767. {
  15768. o->write_character('{');
  15769. // first n-1 elements
  15770. auto i = val.m_data.m_value.object->cbegin();
  15771. for (std::size_t cnt = 0; cnt < val.m_data.m_value.object->size() - 1; ++cnt, ++i)
  15772. {
  15773. o->write_character('\"');
  15774. dump_escaped(i->first, ensure_ascii);
  15775. o->write_characters("\":", 2);
  15776. dump(i->second, false, ensure_ascii, indent_step, current_indent);
  15777. o->write_character(',');
  15778. }
  15779. // last element
  15780. JSON_ASSERT(i != val.m_data.m_value.object->cend());
  15781. JSON_ASSERT(std::next(i) == val.m_data.m_value.object->cend());
  15782. o->write_character('\"');
  15783. dump_escaped(i->first, ensure_ascii);
  15784. o->write_characters("\":", 2);
  15785. dump(i->second, false, ensure_ascii, indent_step, current_indent);
  15786. o->write_character('}');
  15787. }
  15788. return;
  15789. }
  15790. case value_t::array:
  15791. {
  15792. if (val.m_data.m_value.array->empty())
  15793. {
  15794. o->write_characters("[]", 2);
  15795. return;
  15796. }
  15797. if (pretty_print)
  15798. {
  15799. o->write_characters("[\n", 2);
  15800. // variable to hold indentation for recursive calls
  15801. const auto new_indent = current_indent + indent_step;
  15802. if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
  15803. {
  15804. indent_string.resize(indent_string.size() * 2, ' ');
  15805. }
  15806. // first n-1 elements
  15807. for (auto i = val.m_data.m_value.array->cbegin();
  15808. i != val.m_data.m_value.array->cend() - 1; ++i)
  15809. {
  15810. o->write_characters(indent_string.c_str(), new_indent);
  15811. dump(*i, true, ensure_ascii, indent_step, new_indent);
  15812. o->write_characters(",\n", 2);
  15813. }
  15814. // last element
  15815. JSON_ASSERT(!val.m_data.m_value.array->empty());
  15816. o->write_characters(indent_string.c_str(), new_indent);
  15817. dump(val.m_data.m_value.array->back(), true, ensure_ascii, indent_step, new_indent);
  15818. o->write_character('\n');
  15819. o->write_characters(indent_string.c_str(), current_indent);
  15820. o->write_character(']');
  15821. }
  15822. else
  15823. {
  15824. o->write_character('[');
  15825. // first n-1 elements
  15826. for (auto i = val.m_data.m_value.array->cbegin();
  15827. i != val.m_data.m_value.array->cend() - 1; ++i)
  15828. {
  15829. dump(*i, false, ensure_ascii, indent_step, current_indent);
  15830. o->write_character(',');
  15831. }
  15832. // last element
  15833. JSON_ASSERT(!val.m_data.m_value.array->empty());
  15834. dump(val.m_data.m_value.array->back(), false, ensure_ascii, indent_step, current_indent);
  15835. o->write_character(']');
  15836. }
  15837. return;
  15838. }
  15839. case value_t::string:
  15840. {
  15841. o->write_character('\"');
  15842. dump_escaped(*val.m_data.m_value.string, ensure_ascii);
  15843. o->write_character('\"');
  15844. return;
  15845. }
  15846. case value_t::binary:
  15847. {
  15848. if (pretty_print)
  15849. {
  15850. o->write_characters("{\n", 2);
  15851. // variable to hold indentation for recursive calls
  15852. const auto new_indent = current_indent + indent_step;
  15853. if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
  15854. {
  15855. indent_string.resize(indent_string.size() * 2, ' ');
  15856. }
  15857. o->write_characters(indent_string.c_str(), new_indent);
  15858. o->write_characters("\"bytes\": [", 10);
  15859. if (!val.m_data.m_value.binary->empty())
  15860. {
  15861. for (auto i = val.m_data.m_value.binary->cbegin();
  15862. i != val.m_data.m_value.binary->cend() - 1; ++i)
  15863. {
  15864. dump_integer(*i);
  15865. o->write_characters(", ", 2);
  15866. }
  15867. dump_integer(val.m_data.m_value.binary->back());
  15868. }
  15869. o->write_characters("],\n", 3);
  15870. o->write_characters(indent_string.c_str(), new_indent);
  15871. o->write_characters("\"subtype\": ", 11);
  15872. if (val.m_data.m_value.binary->has_subtype())
  15873. {
  15874. dump_integer(val.m_data.m_value.binary->subtype());
  15875. }
  15876. else
  15877. {
  15878. o->write_characters("null", 4);
  15879. }
  15880. o->write_character('\n');
  15881. o->write_characters(indent_string.c_str(), current_indent);
  15882. o->write_character('}');
  15883. }
  15884. else
  15885. {
  15886. o->write_characters("{\"bytes\":[", 10);
  15887. if (!val.m_data.m_value.binary->empty())
  15888. {
  15889. for (auto i = val.m_data.m_value.binary->cbegin();
  15890. i != val.m_data.m_value.binary->cend() - 1; ++i)
  15891. {
  15892. dump_integer(*i);
  15893. o->write_character(',');
  15894. }
  15895. dump_integer(val.m_data.m_value.binary->back());
  15896. }
  15897. o->write_characters("],\"subtype\":", 12);
  15898. if (val.m_data.m_value.binary->has_subtype())
  15899. {
  15900. dump_integer(val.m_data.m_value.binary->subtype());
  15901. o->write_character('}');
  15902. }
  15903. else
  15904. {
  15905. o->write_characters("null}", 5);
  15906. }
  15907. }
  15908. return;
  15909. }
  15910. case value_t::boolean:
  15911. {
  15912. if (val.m_data.m_value.boolean)
  15913. {
  15914. o->write_characters("true", 4);
  15915. }
  15916. else
  15917. {
  15918. o->write_characters("false", 5);
  15919. }
  15920. return;
  15921. }
  15922. case value_t::number_integer:
  15923. {
  15924. dump_integer(val.m_data.m_value.number_integer);
  15925. return;
  15926. }
  15927. case value_t::number_unsigned:
  15928. {
  15929. dump_integer(val.m_data.m_value.number_unsigned);
  15930. return;
  15931. }
  15932. case value_t::number_float:
  15933. {
  15934. dump_float(val.m_data.m_value.number_float);
  15935. return;
  15936. }
  15937. case value_t::discarded:
  15938. {
  15939. o->write_characters("<discarded>", 11);
  15940. return;
  15941. }
  15942. case value_t::null:
  15943. {
  15944. o->write_characters("null", 4);
  15945. return;
  15946. }
  15947. default: // LCOV_EXCL_LINE
  15948. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  15949. }
  15950. }
  15951. JSON_PRIVATE_UNLESS_TESTED:
  15952. /*!
  15953. @brief dump escaped string
  15954. Escape a string by replacing certain special characters by a sequence of an
  15955. escape character (backslash) and another character and other control
  15956. characters by a sequence of "\u" followed by a four-digit hex
  15957. representation. The escaped string is written to output stream @a o.
  15958. @param[in] s the string to escape
  15959. @param[in] ensure_ascii whether to escape non-ASCII characters with
  15960. \uXXXX sequences
  15961. @complexity Linear in the length of string @a s.
  15962. */
  15963. void dump_escaped(const string_t& s, const bool ensure_ascii)
  15964. {
  15965. std::uint32_t codepoint{};
  15966. std::uint8_t state = UTF8_ACCEPT;
  15967. std::size_t bytes = 0; // number of bytes written to string_buffer
  15968. // number of bytes written at the point of the last valid byte
  15969. std::size_t bytes_after_last_accept = 0;
  15970. std::size_t undumped_chars = 0;
  15971. for (std::size_t i = 0; i < s.size(); ++i)
  15972. {
  15973. const auto byte = static_cast<std::uint8_t>(s[i]);
  15974. switch (decode(state, codepoint, byte))
  15975. {
  15976. case UTF8_ACCEPT: // decode found a new code point
  15977. {
  15978. switch (codepoint)
  15979. {
  15980. case 0x08: // backspace
  15981. {
  15982. string_buffer[bytes++] = '\\';
  15983. string_buffer[bytes++] = 'b';
  15984. break;
  15985. }
  15986. case 0x09: // horizontal tab
  15987. {
  15988. string_buffer[bytes++] = '\\';
  15989. string_buffer[bytes++] = 't';
  15990. break;
  15991. }
  15992. case 0x0A: // newline
  15993. {
  15994. string_buffer[bytes++] = '\\';
  15995. string_buffer[bytes++] = 'n';
  15996. break;
  15997. }
  15998. case 0x0C: // formfeed
  15999. {
  16000. string_buffer[bytes++] = '\\';
  16001. string_buffer[bytes++] = 'f';
  16002. break;
  16003. }
  16004. case 0x0D: // carriage return
  16005. {
  16006. string_buffer[bytes++] = '\\';
  16007. string_buffer[bytes++] = 'r';
  16008. break;
  16009. }
  16010. case 0x22: // quotation mark
  16011. {
  16012. string_buffer[bytes++] = '\\';
  16013. string_buffer[bytes++] = '\"';
  16014. break;
  16015. }
  16016. case 0x5C: // reverse solidus
  16017. {
  16018. string_buffer[bytes++] = '\\';
  16019. string_buffer[bytes++] = '\\';
  16020. break;
  16021. }
  16022. default:
  16023. {
  16024. // escape control characters (0x00..0x1F) or, if
  16025. // ensure_ascii parameter is used, non-ASCII characters
  16026. if ((codepoint <= 0x1F) || (ensure_ascii && (codepoint >= 0x7F)))
  16027. {
  16028. if (codepoint <= 0xFFFF)
  16029. {
  16030. // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  16031. static_cast<void>((std::snprintf)(string_buffer.data() + bytes, 7, "\\u%04x",
  16032. static_cast<std::uint16_t>(codepoint)));
  16033. bytes += 6;
  16034. }
  16035. else
  16036. {
  16037. // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  16038. static_cast<void>((std::snprintf)(string_buffer.data() + bytes, 13, "\\u%04x\\u%04x",
  16039. static_cast<std::uint16_t>(0xD7C0u + (codepoint >> 10u)),
  16040. static_cast<std::uint16_t>(0xDC00u + (codepoint & 0x3FFu))));
  16041. bytes += 12;
  16042. }
  16043. }
  16044. else
  16045. {
  16046. // copy byte to buffer (all previous bytes
  16047. // been copied have in default case above)
  16048. string_buffer[bytes++] = s[i];
  16049. }
  16050. break;
  16051. }
  16052. }
  16053. // write buffer and reset index; there must be 13 bytes
  16054. // left, as this is the maximal number of bytes to be
  16055. // written ("\uxxxx\uxxxx\0") for one code point
  16056. if (string_buffer.size() - bytes < 13)
  16057. {
  16058. o->write_characters(string_buffer.data(), bytes);
  16059. bytes = 0;
  16060. }
  16061. // remember the byte position of this accept
  16062. bytes_after_last_accept = bytes;
  16063. undumped_chars = 0;
  16064. break;
  16065. }
  16066. case UTF8_REJECT: // decode found invalid UTF-8 byte
  16067. {
  16068. switch (error_handler)
  16069. {
  16070. case error_handler_t::strict:
  16071. {
  16072. JSON_THROW(type_error::create(316, concat("invalid UTF-8 byte at index ", std::to_string(i), ": 0x", hex_bytes(byte | 0)), nullptr));
  16073. }
  16074. case error_handler_t::ignore:
  16075. case error_handler_t::replace:
  16076. {
  16077. // in case we saw this character the first time, we
  16078. // would like to read it again, because the byte
  16079. // may be OK for itself, but just not OK for the
  16080. // previous sequence
  16081. if (undumped_chars > 0)
  16082. {
  16083. --i;
  16084. }
  16085. // reset length buffer to the last accepted index;
  16086. // thus removing/ignoring the invalid characters
  16087. bytes = bytes_after_last_accept;
  16088. if (error_handler == error_handler_t::replace)
  16089. {
  16090. // add a replacement character
  16091. if (ensure_ascii)
  16092. {
  16093. string_buffer[bytes++] = '\\';
  16094. string_buffer[bytes++] = 'u';
  16095. string_buffer[bytes++] = 'f';
  16096. string_buffer[bytes++] = 'f';
  16097. string_buffer[bytes++] = 'f';
  16098. string_buffer[bytes++] = 'd';
  16099. }
  16100. else
  16101. {
  16102. string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xEF');
  16103. string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xBF');
  16104. string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xBD');
  16105. }
  16106. // write buffer and reset index; there must be 13 bytes
  16107. // left, as this is the maximal number of bytes to be
  16108. // written ("\uxxxx\uxxxx\0") for one code point
  16109. if (string_buffer.size() - bytes < 13)
  16110. {
  16111. o->write_characters(string_buffer.data(), bytes);
  16112. bytes = 0;
  16113. }
  16114. bytes_after_last_accept = bytes;
  16115. }
  16116. undumped_chars = 0;
  16117. // continue processing the string
  16118. state = UTF8_ACCEPT;
  16119. break;
  16120. }
  16121. default: // LCOV_EXCL_LINE
  16122. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  16123. }
  16124. break;
  16125. }
  16126. default: // decode found yet incomplete multi-byte code point
  16127. {
  16128. if (!ensure_ascii)
  16129. {
  16130. // code point will not be escaped - copy byte to buffer
  16131. string_buffer[bytes++] = s[i];
  16132. }
  16133. ++undumped_chars;
  16134. break;
  16135. }
  16136. }
  16137. }
  16138. // we finished processing the string
  16139. if (JSON_HEDLEY_LIKELY(state == UTF8_ACCEPT))
  16140. {
  16141. // write buffer
  16142. if (bytes > 0)
  16143. {
  16144. o->write_characters(string_buffer.data(), bytes);
  16145. }
  16146. }
  16147. else
  16148. {
  16149. // we finish reading, but do not accept: string was incomplete
  16150. switch (error_handler)
  16151. {
  16152. case error_handler_t::strict:
  16153. {
  16154. 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));
  16155. }
  16156. case error_handler_t::ignore:
  16157. {
  16158. // write all accepted bytes
  16159. o->write_characters(string_buffer.data(), bytes_after_last_accept);
  16160. break;
  16161. }
  16162. case error_handler_t::replace:
  16163. {
  16164. // write all accepted bytes
  16165. o->write_characters(string_buffer.data(), bytes_after_last_accept);
  16166. // add a replacement character
  16167. if (ensure_ascii)
  16168. {
  16169. o->write_characters("\\ufffd", 6);
  16170. }
  16171. else
  16172. {
  16173. o->write_characters("\xEF\xBF\xBD", 3);
  16174. }
  16175. break;
  16176. }
  16177. default: // LCOV_EXCL_LINE
  16178. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  16179. }
  16180. }
  16181. }
  16182. private:
  16183. /*!
  16184. @brief count digits
  16185. Count the number of decimal (base 10) digits for an input unsigned integer.
  16186. @param[in] x unsigned integer number to count its digits
  16187. @return number of decimal digits
  16188. */
  16189. inline unsigned int count_digits(number_unsigned_t x) noexcept
  16190. {
  16191. unsigned int n_digits = 1;
  16192. for (;;)
  16193. {
  16194. if (x < 10)
  16195. {
  16196. return n_digits;
  16197. }
  16198. if (x < 100)
  16199. {
  16200. return n_digits + 1;
  16201. }
  16202. if (x < 1000)
  16203. {
  16204. return n_digits + 2;
  16205. }
  16206. if (x < 10000)
  16207. {
  16208. return n_digits + 3;
  16209. }
  16210. x = x / 10000u;
  16211. n_digits += 4;
  16212. }
  16213. }
  16214. /*!
  16215. * @brief convert a byte to a uppercase hex representation
  16216. * @param[in] byte byte to represent
  16217. * @return representation ("00".."FF")
  16218. */
  16219. static std::string hex_bytes(std::uint8_t byte)
  16220. {
  16221. std::string result = "FF";
  16222. constexpr const char* nibble_to_hex = "0123456789ABCDEF";
  16223. result[0] = nibble_to_hex[byte / 16];
  16224. result[1] = nibble_to_hex[byte % 16];
  16225. return result;
  16226. }
  16227. // templates to avoid warnings about useless casts
  16228. template <typename NumberType, enable_if_t<std::is_signed<NumberType>::value, int> = 0>
  16229. bool is_negative_number(NumberType x)
  16230. {
  16231. return x < 0;
  16232. }
  16233. template < typename NumberType, enable_if_t <std::is_unsigned<NumberType>::value, int > = 0 >
  16234. bool is_negative_number(NumberType /*unused*/)
  16235. {
  16236. return false;
  16237. }
  16238. /*!
  16239. @brief dump an integer
  16240. Dump a given integer to output stream @a o. Works internally with
  16241. @a number_buffer.
  16242. @param[in] x integer number (signed or unsigned) to dump
  16243. @tparam NumberType either @a number_integer_t or @a number_unsigned_t
  16244. */
  16245. template < typename NumberType, detail::enable_if_t <
  16246. std::is_integral<NumberType>::value ||
  16247. std::is_same<NumberType, number_unsigned_t>::value ||
  16248. std::is_same<NumberType, number_integer_t>::value ||
  16249. std::is_same<NumberType, binary_char_t>::value,
  16250. int > = 0 >
  16251. void dump_integer(NumberType x)
  16252. {
  16253. static constexpr std::array<std::array<char, 2>, 100> digits_to_99
  16254. {
  16255. {
  16256. {{'0', '0'}}, {{'0', '1'}}, {{'0', '2'}}, {{'0', '3'}}, {{'0', '4'}}, {{'0', '5'}}, {{'0', '6'}}, {{'0', '7'}}, {{'0', '8'}}, {{'0', '9'}},
  16257. {{'1', '0'}}, {{'1', '1'}}, {{'1', '2'}}, {{'1', '3'}}, {{'1', '4'}}, {{'1', '5'}}, {{'1', '6'}}, {{'1', '7'}}, {{'1', '8'}}, {{'1', '9'}},
  16258. {{'2', '0'}}, {{'2', '1'}}, {{'2', '2'}}, {{'2', '3'}}, {{'2', '4'}}, {{'2', '5'}}, {{'2', '6'}}, {{'2', '7'}}, {{'2', '8'}}, {{'2', '9'}},
  16259. {{'3', '0'}}, {{'3', '1'}}, {{'3', '2'}}, {{'3', '3'}}, {{'3', '4'}}, {{'3', '5'}}, {{'3', '6'}}, {{'3', '7'}}, {{'3', '8'}}, {{'3', '9'}},
  16260. {{'4', '0'}}, {{'4', '1'}}, {{'4', '2'}}, {{'4', '3'}}, {{'4', '4'}}, {{'4', '5'}}, {{'4', '6'}}, {{'4', '7'}}, {{'4', '8'}}, {{'4', '9'}},
  16261. {{'5', '0'}}, {{'5', '1'}}, {{'5', '2'}}, {{'5', '3'}}, {{'5', '4'}}, {{'5', '5'}}, {{'5', '6'}}, {{'5', '7'}}, {{'5', '8'}}, {{'5', '9'}},
  16262. {{'6', '0'}}, {{'6', '1'}}, {{'6', '2'}}, {{'6', '3'}}, {{'6', '4'}}, {{'6', '5'}}, {{'6', '6'}}, {{'6', '7'}}, {{'6', '8'}}, {{'6', '9'}},
  16263. {{'7', '0'}}, {{'7', '1'}}, {{'7', '2'}}, {{'7', '3'}}, {{'7', '4'}}, {{'7', '5'}}, {{'7', '6'}}, {{'7', '7'}}, {{'7', '8'}}, {{'7', '9'}},
  16264. {{'8', '0'}}, {{'8', '1'}}, {{'8', '2'}}, {{'8', '3'}}, {{'8', '4'}}, {{'8', '5'}}, {{'8', '6'}}, {{'8', '7'}}, {{'8', '8'}}, {{'8', '9'}},
  16265. {{'9', '0'}}, {{'9', '1'}}, {{'9', '2'}}, {{'9', '3'}}, {{'9', '4'}}, {{'9', '5'}}, {{'9', '6'}}, {{'9', '7'}}, {{'9', '8'}}, {{'9', '9'}},
  16266. }
  16267. };
  16268. // special case for "0"
  16269. if (x == 0)
  16270. {
  16271. o->write_character('0');
  16272. return;
  16273. }
  16274. // use a pointer to fill the buffer
  16275. auto buffer_ptr = number_buffer.begin(); // NOLINT(llvm-qualified-auto,readability-qualified-auto,cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  16276. number_unsigned_t abs_value;
  16277. unsigned int n_chars{};
  16278. if (is_negative_number(x))
  16279. {
  16280. *buffer_ptr = '-';
  16281. abs_value = remove_sign(static_cast<number_integer_t>(x));
  16282. // account one more byte for the minus sign
  16283. n_chars = 1 + count_digits(abs_value);
  16284. }
  16285. else
  16286. {
  16287. abs_value = static_cast<number_unsigned_t>(x);
  16288. n_chars = count_digits(abs_value);
  16289. }
  16290. // spare 1 byte for '\0'
  16291. JSON_ASSERT(n_chars < number_buffer.size() - 1);
  16292. // jump to the end to generate the string from backward,
  16293. // so we later avoid reversing the result
  16294. buffer_ptr += n_chars;
  16295. // Fast int2ascii implementation inspired by "Fastware" talk by Andrei Alexandrescu
  16296. // See: https://www.youtube.com/watch?v=o4-CwDo2zpg
  16297. while (abs_value >= 100)
  16298. {
  16299. const auto digits_index = static_cast<unsigned>((abs_value % 100));
  16300. abs_value /= 100;
  16301. *(--buffer_ptr) = digits_to_99[digits_index][1];
  16302. *(--buffer_ptr) = digits_to_99[digits_index][0];
  16303. }
  16304. if (abs_value >= 10)
  16305. {
  16306. const auto digits_index = static_cast<unsigned>(abs_value);
  16307. *(--buffer_ptr) = digits_to_99[digits_index][1];
  16308. *(--buffer_ptr) = digits_to_99[digits_index][0];
  16309. }
  16310. else
  16311. {
  16312. *(--buffer_ptr) = static_cast<char>('0' + abs_value);
  16313. }
  16314. o->write_characters(number_buffer.data(), n_chars);
  16315. }
  16316. /*!
  16317. @brief dump a floating-point number
  16318. Dump a given floating-point number to output stream @a o. Works internally
  16319. with @a number_buffer.
  16320. @param[in] x floating-point number to dump
  16321. */
  16322. void dump_float(number_float_t x)
  16323. {
  16324. // NaN / inf
  16325. if (!std::isfinite(x))
  16326. {
  16327. o->write_characters("null", 4);
  16328. return;
  16329. }
  16330. // If number_float_t is an IEEE-754 single or double precision number,
  16331. // use the Grisu2 algorithm to produce short numbers which are
  16332. // guaranteed to round-trip, using strtof and strtod, resp.
  16333. //
  16334. // NB: The test below works if <long double> == <double>.
  16335. static constexpr bool is_ieee_single_or_double
  16336. = (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) ||
  16337. (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);
  16338. dump_float(x, std::integral_constant<bool, is_ieee_single_or_double>());
  16339. }
  16340. void dump_float(number_float_t x, std::true_type /*is_ieee_single_or_double*/)
  16341. {
  16342. auto* begin = number_buffer.data();
  16343. auto* end = ::nlohmann::detail::to_chars(begin, begin + number_buffer.size(), x);
  16344. o->write_characters(begin, static_cast<size_t>(end - begin));
  16345. }
  16346. void dump_float(number_float_t x, std::false_type /*is_ieee_single_or_double*/)
  16347. {
  16348. // get number of digits for a float -> text -> float round-trip
  16349. static constexpr auto d = std::numeric_limits<number_float_t>::max_digits10;
  16350. // the actual conversion
  16351. // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  16352. std::ptrdiff_t len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*g", d, x);
  16353. // negative value indicates an error
  16354. JSON_ASSERT(len > 0);
  16355. // check if buffer was large enough
  16356. JSON_ASSERT(static_cast<std::size_t>(len) < number_buffer.size());
  16357. // erase thousands separator
  16358. if (thousands_sep != '\0')
  16359. {
  16360. // NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::remove returns an iterator, see https://github.com/nlohmann/json/issues/3081
  16361. const auto end = std::remove(number_buffer.begin(), number_buffer.begin() + len, thousands_sep);
  16362. std::fill(end, number_buffer.end(), '\0');
  16363. JSON_ASSERT((end - number_buffer.begin()) <= len);
  16364. len = (end - number_buffer.begin());
  16365. }
  16366. // convert decimal point to '.'
  16367. if (decimal_point != '\0' && decimal_point != '.')
  16368. {
  16369. // NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::find returns an iterator, see https://github.com/nlohmann/json/issues/3081
  16370. const auto dec_pos = std::find(number_buffer.begin(), number_buffer.end(), decimal_point);
  16371. if (dec_pos != number_buffer.end())
  16372. {
  16373. *dec_pos = '.';
  16374. }
  16375. }
  16376. o->write_characters(number_buffer.data(), static_cast<std::size_t>(len));
  16377. // determine if we need to append ".0"
  16378. const bool value_is_int_like =
  16379. std::none_of(number_buffer.begin(), number_buffer.begin() + len + 1,
  16380. [](char c)
  16381. {
  16382. return c == '.' || c == 'e';
  16383. });
  16384. if (value_is_int_like)
  16385. {
  16386. o->write_characters(".0", 2);
  16387. }
  16388. }
  16389. /*!
  16390. @brief check whether a string is UTF-8 encoded
  16391. The function checks each byte of a string whether it is UTF-8 encoded. The
  16392. result of the check is stored in the @a state parameter. The function must
  16393. be called initially with state 0 (accept). State 1 means the string must
  16394. be rejected, because the current byte is not allowed. If the string is
  16395. completely processed, but the state is non-zero, the string ended
  16396. prematurely; that is, the last byte indicated more bytes should have
  16397. followed.
  16398. @param[in,out] state the state of the decoding
  16399. @param[in,out] codep codepoint (valid only if resulting state is UTF8_ACCEPT)
  16400. @param[in] byte next byte to decode
  16401. @return new state
  16402. @note The function has been edited: a std::array is used.
  16403. @copyright Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
  16404. @sa http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
  16405. */
  16406. static std::uint8_t decode(std::uint8_t& state, std::uint32_t& codep, const std::uint8_t byte) noexcept
  16407. {
  16408. static const std::array<std::uint8_t, 400> utf8d =
  16409. {
  16410. {
  16411. 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
  16412. 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
  16413. 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
  16414. 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
  16415. 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
  16416. 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
  16417. 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
  16418. 0xA, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x4, 0x3, 0x3, // E0..EF
  16419. 0xB, 0x6, 0x6, 0x6, 0x5, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, // F0..FF
  16420. 0x0, 0x1, 0x2, 0x3, 0x5, 0x8, 0x7, 0x1, 0x1, 0x1, 0x4, 0x6, 0x1, 0x1, 0x1, 0x1, // s0..s0
  16421. 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
  16422. 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
  16423. 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
  16424. 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
  16425. }
  16426. };
  16427. JSON_ASSERT(byte < utf8d.size());
  16428. const std::uint8_t type = utf8d[byte];
  16429. codep = (state != UTF8_ACCEPT)
  16430. ? (byte & 0x3fu) | (codep << 6u)
  16431. : (0xFFu >> type) & (byte);
  16432. const std::size_t index = 256u + static_cast<size_t>(state) * 16u + static_cast<size_t>(type);
  16433. JSON_ASSERT(index < utf8d.size());
  16434. state = utf8d[index];
  16435. return state;
  16436. }
  16437. /*
  16438. * Overload to make the compiler happy while it is instantiating
  16439. * dump_integer for number_unsigned_t.
  16440. * Must never be called.
  16441. */
  16442. number_unsigned_t remove_sign(number_unsigned_t x)
  16443. {
  16444. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  16445. return x; // LCOV_EXCL_LINE
  16446. }
  16447. /*
  16448. * Helper function for dump_integer
  16449. *
  16450. * This function takes a negative signed integer and returns its absolute
  16451. * value as unsigned integer. The plus/minus shuffling is necessary as we can
  16452. * not directly remove the sign of an arbitrary signed integer as the
  16453. * absolute values of INT_MIN and INT_MAX are usually not the same. See
  16454. * #1708 for details.
  16455. */
  16456. inline number_unsigned_t remove_sign(number_integer_t x) noexcept
  16457. {
  16458. JSON_ASSERT(x < 0 && x < (std::numeric_limits<number_integer_t>::max)()); // NOLINT(misc-redundant-expression)
  16459. return static_cast<number_unsigned_t>(-(x + 1)) + 1;
  16460. }
  16461. private:
  16462. /// the output of the serializer
  16463. output_adapter_t<char> o = nullptr;
  16464. /// a (hopefully) large enough character buffer
  16465. std::array<char, 64> number_buffer{{}};
  16466. /// the locale
  16467. const std::lconv* loc = nullptr;
  16468. /// the locale's thousand separator character
  16469. const char thousands_sep = '\0';
  16470. /// the locale's decimal point character
  16471. const char decimal_point = '\0';
  16472. /// string buffer
  16473. std::array<char, 512> string_buffer{{}};
  16474. /// the indentation character
  16475. const char indent_char;
  16476. /// the indentation string
  16477. string_t indent_string;
  16478. /// error_handler how to react on decoding errors
  16479. const error_handler_t error_handler;
  16480. };
  16481. } // namespace detail
  16482. NLOHMANN_JSON_NAMESPACE_END
  16483. // #include <nlohmann/detail/value_t.hpp>
  16484. // #include <nlohmann/json_fwd.hpp>
  16485. // #include <nlohmann/ordered_map.hpp>
  16486. // __ _____ _____ _____
  16487. // __| | __| | | | JSON for Modern C++
  16488. // | | |__ | | | | | | version 3.11.2
  16489. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  16490. //
  16491. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  16492. // SPDX-License-Identifier: MIT
  16493. #include <functional> // equal_to, less
  16494. #include <initializer_list> // initializer_list
  16495. #include <iterator> // input_iterator_tag, iterator_traits
  16496. #include <memory> // allocator
  16497. #include <stdexcept> // for out_of_range
  16498. #include <type_traits> // enable_if, is_convertible
  16499. #include <utility> // pair
  16500. #include <vector> // vector
  16501. // #include <nlohmann/detail/macro_scope.hpp>
  16502. // #include <nlohmann/detail/meta/type_traits.hpp>
  16503. NLOHMANN_JSON_NAMESPACE_BEGIN
  16504. /// ordered_map: a minimal map-like container that preserves insertion order
  16505. /// for use within nlohmann::basic_json<ordered_map>
  16506. template <class Key, class T, class IgnoredLess = std::less<Key>,
  16507. class Allocator = std::allocator<std::pair<const Key, T>>>
  16508. struct ordered_map : std::vector<std::pair<const Key, T>, Allocator>
  16509. {
  16510. using key_type = Key;
  16511. using mapped_type = T;
  16512. using Container = std::vector<std::pair<const Key, T>, Allocator>;
  16513. using iterator = typename Container::iterator;
  16514. using const_iterator = typename Container::const_iterator;
  16515. using size_type = typename Container::size_type;
  16516. using value_type = typename Container::value_type;
  16517. #ifdef JSON_HAS_CPP_14
  16518. using key_compare = std::equal_to<>;
  16519. #else
  16520. using key_compare = std::equal_to<Key>;
  16521. #endif
  16522. // Explicit constructors instead of `using Container::Container`
  16523. // otherwise older compilers choke on it (GCC <= 5.5, xcode <= 9.4)
  16524. ordered_map() noexcept(noexcept(Container())) : Container{} {}
  16525. explicit ordered_map(const Allocator& alloc) noexcept(noexcept(Container(alloc))) : Container{alloc} {}
  16526. template <class It>
  16527. ordered_map(It first, It last, const Allocator& alloc = Allocator())
  16528. : Container{first, last, alloc} {}
  16529. ordered_map(std::initializer_list<value_type> init, const Allocator& alloc = Allocator() )
  16530. : Container{init, alloc} {}
  16531. std::pair<iterator, bool> emplace(const key_type& key, T&& t)
  16532. {
  16533. for (auto it = this->begin(); it != this->end(); ++it)
  16534. {
  16535. if (m_compare(it->first, key))
  16536. {
  16537. return {it, false};
  16538. }
  16539. }
  16540. Container::emplace_back(key, std::forward<T>(t));
  16541. return {std::prev(this->end()), true};
  16542. }
  16543. template<class KeyType, detail::enable_if_t<
  16544. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16545. std::pair<iterator, bool> emplace(KeyType && key, T && t)
  16546. {
  16547. for (auto it = this->begin(); it != this->end(); ++it)
  16548. {
  16549. if (m_compare(it->first, key))
  16550. {
  16551. return {it, false};
  16552. }
  16553. }
  16554. Container::emplace_back(std::forward<KeyType>(key), std::forward<T>(t));
  16555. return {std::prev(this->end()), true};
  16556. }
  16557. T& operator[](const key_type& key)
  16558. {
  16559. return emplace(key, T{}).first->second;
  16560. }
  16561. template<class KeyType, detail::enable_if_t<
  16562. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16563. T & operator[](KeyType && key)
  16564. {
  16565. return emplace(std::forward<KeyType>(key), T{}).first->second;
  16566. }
  16567. const T& operator[](const key_type& key) const
  16568. {
  16569. return at(key);
  16570. }
  16571. template<class KeyType, detail::enable_if_t<
  16572. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16573. const T & operator[](KeyType && key) const
  16574. {
  16575. return at(std::forward<KeyType>(key));
  16576. }
  16577. T& at(const key_type& key)
  16578. {
  16579. for (auto it = this->begin(); it != this->end(); ++it)
  16580. {
  16581. if (m_compare(it->first, key))
  16582. {
  16583. return it->second;
  16584. }
  16585. }
  16586. JSON_THROW(std::out_of_range("key not found"));
  16587. }
  16588. template<class KeyType, detail::enable_if_t<
  16589. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16590. T & at(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
  16591. {
  16592. for (auto it = this->begin(); it != this->end(); ++it)
  16593. {
  16594. if (m_compare(it->first, key))
  16595. {
  16596. return it->second;
  16597. }
  16598. }
  16599. JSON_THROW(std::out_of_range("key not found"));
  16600. }
  16601. const T& at(const key_type& key) const
  16602. {
  16603. for (auto it = this->begin(); it != this->end(); ++it)
  16604. {
  16605. if (m_compare(it->first, key))
  16606. {
  16607. return it->second;
  16608. }
  16609. }
  16610. JSON_THROW(std::out_of_range("key not found"));
  16611. }
  16612. template<class KeyType, detail::enable_if_t<
  16613. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16614. const T & at(KeyType && key) const // NOLINT(cppcoreguidelines-missing-std-forward)
  16615. {
  16616. for (auto it = this->begin(); it != this->end(); ++it)
  16617. {
  16618. if (m_compare(it->first, key))
  16619. {
  16620. return it->second;
  16621. }
  16622. }
  16623. JSON_THROW(std::out_of_range("key not found"));
  16624. }
  16625. size_type erase(const key_type& key)
  16626. {
  16627. for (auto it = this->begin(); it != this->end(); ++it)
  16628. {
  16629. if (m_compare(it->first, key))
  16630. {
  16631. // Since we cannot move const Keys, re-construct them in place
  16632. for (auto next = it; ++next != this->end(); ++it)
  16633. {
  16634. it->~value_type(); // Destroy but keep allocation
  16635. new (&*it) value_type{std::move(*next)};
  16636. }
  16637. Container::pop_back();
  16638. return 1;
  16639. }
  16640. }
  16641. return 0;
  16642. }
  16643. template<class KeyType, detail::enable_if_t<
  16644. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16645. size_type erase(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
  16646. {
  16647. for (auto it = this->begin(); it != this->end(); ++it)
  16648. {
  16649. if (m_compare(it->first, key))
  16650. {
  16651. // Since we cannot move const Keys, re-construct them in place
  16652. for (auto next = it; ++next != this->end(); ++it)
  16653. {
  16654. it->~value_type(); // Destroy but keep allocation
  16655. new (&*it) value_type{std::move(*next)};
  16656. }
  16657. Container::pop_back();
  16658. return 1;
  16659. }
  16660. }
  16661. return 0;
  16662. }
  16663. iterator erase(iterator pos)
  16664. {
  16665. return erase(pos, std::next(pos));
  16666. }
  16667. iterator erase(iterator first, iterator last)
  16668. {
  16669. if (first == last)
  16670. {
  16671. return first;
  16672. }
  16673. const auto elements_affected = std::distance(first, last);
  16674. const auto offset = std::distance(Container::begin(), first);
  16675. // This is the start situation. We need to delete elements_affected
  16676. // elements (3 in this example: e, f, g), and need to return an
  16677. // iterator past the last deleted element (h in this example).
  16678. // Note that offset is the distance from the start of the vector
  16679. // to first. We will need this later.
  16680. // [ a, b, c, d, e, f, g, h, i, j ]
  16681. // ^ ^
  16682. // first last
  16683. // Since we cannot move const Keys, we re-construct them in place.
  16684. // We start at first and re-construct (viz. copy) the elements from
  16685. // the back of the vector. Example for first iteration:
  16686. // ,--------.
  16687. // v | destroy e and re-construct with h
  16688. // [ a, b, c, d, e, f, g, h, i, j ]
  16689. // ^ ^
  16690. // it it + elements_affected
  16691. for (auto it = first; std::next(it, elements_affected) != Container::end(); ++it)
  16692. {
  16693. it->~value_type(); // destroy but keep allocation
  16694. new (&*it) value_type{std::move(*std::next(it, elements_affected))}; // "move" next element to it
  16695. }
  16696. // [ a, b, c, d, h, i, j, h, i, j ]
  16697. // ^ ^
  16698. // first last
  16699. // remove the unneeded elements at the end of the vector
  16700. Container::resize(this->size() - static_cast<size_type>(elements_affected));
  16701. // [ a, b, c, d, h, i, j ]
  16702. // ^ ^
  16703. // first last
  16704. // first is now pointing past the last deleted element, but we cannot
  16705. // use this iterator, because it may have been invalidated by the
  16706. // resize call. Instead, we can return begin() + offset.
  16707. return Container::begin() + offset;
  16708. }
  16709. size_type count(const key_type& key) const
  16710. {
  16711. for (auto it = this->begin(); it != this->end(); ++it)
  16712. {
  16713. if (m_compare(it->first, key))
  16714. {
  16715. return 1;
  16716. }
  16717. }
  16718. return 0;
  16719. }
  16720. template<class KeyType, detail::enable_if_t<
  16721. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16722. size_type count(KeyType && key) const // NOLINT(cppcoreguidelines-missing-std-forward)
  16723. {
  16724. for (auto it = this->begin(); it != this->end(); ++it)
  16725. {
  16726. if (m_compare(it->first, key))
  16727. {
  16728. return 1;
  16729. }
  16730. }
  16731. return 0;
  16732. }
  16733. iterator find(const key_type& key)
  16734. {
  16735. for (auto it = this->begin(); it != this->end(); ++it)
  16736. {
  16737. if (m_compare(it->first, key))
  16738. {
  16739. return it;
  16740. }
  16741. }
  16742. return Container::end();
  16743. }
  16744. template<class KeyType, detail::enable_if_t<
  16745. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16746. iterator find(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
  16747. {
  16748. for (auto it = this->begin(); it != this->end(); ++it)
  16749. {
  16750. if (m_compare(it->first, key))
  16751. {
  16752. return it;
  16753. }
  16754. }
  16755. return Container::end();
  16756. }
  16757. const_iterator find(const key_type& key) const
  16758. {
  16759. for (auto it = this->begin(); it != this->end(); ++it)
  16760. {
  16761. if (m_compare(it->first, key))
  16762. {
  16763. return it;
  16764. }
  16765. }
  16766. return Container::end();
  16767. }
  16768. std::pair<iterator, bool> insert( value_type&& value )
  16769. {
  16770. return emplace(value.first, std::move(value.second));
  16771. }
  16772. std::pair<iterator, bool> insert( const value_type& value )
  16773. {
  16774. for (auto it = this->begin(); it != this->end(); ++it)
  16775. {
  16776. if (m_compare(it->first, value.first))
  16777. {
  16778. return {it, false};
  16779. }
  16780. }
  16781. Container::push_back(value);
  16782. return {--this->end(), true};
  16783. }
  16784. template<typename InputIt>
  16785. using require_input_iter = typename std::enable_if<std::is_convertible<typename std::iterator_traits<InputIt>::iterator_category,
  16786. std::input_iterator_tag>::value>::type;
  16787. template<typename InputIt, typename = require_input_iter<InputIt>>
  16788. void insert(InputIt first, InputIt last)
  16789. {
  16790. for (auto it = first; it != last; ++it)
  16791. {
  16792. insert(*it);
  16793. }
  16794. }
  16795. private:
  16796. JSON_NO_UNIQUE_ADDRESS key_compare m_compare = key_compare();
  16797. };
  16798. NLOHMANN_JSON_NAMESPACE_END
  16799. #if defined(JSON_HAS_CPP_17)
  16800. #if JSON_HAS_STATIC_RTTI
  16801. #include <any>
  16802. #endif
  16803. #include <string_view>
  16804. #endif
  16805. /*!
  16806. @brief namespace for Niels Lohmann
  16807. @see https://github.com/nlohmann
  16808. @since version 1.0.0
  16809. */
  16810. NLOHMANN_JSON_NAMESPACE_BEGIN
  16811. /*!
  16812. @brief a class to store JSON values
  16813. @internal
  16814. @invariant The member variables @a m_value and @a m_type have the following
  16815. relationship:
  16816. - If `m_type == value_t::object`, then `m_value.object != nullptr`.
  16817. - If `m_type == value_t::array`, then `m_value.array != nullptr`.
  16818. - If `m_type == value_t::string`, then `m_value.string != nullptr`.
  16819. The invariants are checked by member function assert_invariant().
  16820. @note ObjectType trick from https://stackoverflow.com/a/9860911
  16821. @endinternal
  16822. @since version 1.0.0
  16823. @nosubgrouping
  16824. */
  16825. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  16826. class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions)
  16827. : public ::nlohmann::detail::json_base_class<CustomBaseClass>
  16828. {
  16829. private:
  16830. template<detail::value_t> friend struct detail::external_constructor;
  16831. template<typename>
  16832. friend class ::nlohmann::json_pointer;
  16833. // can be restored when json_pointer backwards compatibility is removed
  16834. // friend ::nlohmann::json_pointer<StringType>;
  16835. template<typename BasicJsonType, typename InputType>
  16836. friend class ::nlohmann::detail::parser;
  16837. friend ::nlohmann::detail::serializer<basic_json>;
  16838. template<typename BasicJsonType>
  16839. friend class ::nlohmann::detail::iter_impl;
  16840. template<typename BasicJsonType, typename CharType>
  16841. friend class ::nlohmann::detail::binary_writer;
  16842. template<typename BasicJsonType, typename InputType, typename SAX>
  16843. friend class ::nlohmann::detail::binary_reader;
  16844. template<typename BasicJsonType>
  16845. friend class ::nlohmann::detail::json_sax_dom_parser;
  16846. template<typename BasicJsonType>
  16847. friend class ::nlohmann::detail::json_sax_dom_callback_parser;
  16848. friend class ::nlohmann::detail::exception;
  16849. /// workaround type for MSVC
  16850. using basic_json_t = NLOHMANN_BASIC_JSON_TPL;
  16851. using json_base_class_t = ::nlohmann::detail::json_base_class<CustomBaseClass>;
  16852. JSON_PRIVATE_UNLESS_TESTED:
  16853. // convenience aliases for types residing in namespace detail;
  16854. using lexer = ::nlohmann::detail::lexer_base<basic_json>;
  16855. template<typename InputAdapterType>
  16856. static ::nlohmann::detail::parser<basic_json, InputAdapterType> parser(
  16857. InputAdapterType adapter,
  16858. detail::parser_callback_t<basic_json>cb = nullptr,
  16859. const bool allow_exceptions = true,
  16860. const bool ignore_comments = false
  16861. )
  16862. {
  16863. return ::nlohmann::detail::parser<basic_json, InputAdapterType>(std::move(adapter),
  16864. std::move(cb), allow_exceptions, ignore_comments);
  16865. }
  16866. private:
  16867. using primitive_iterator_t = ::nlohmann::detail::primitive_iterator_t;
  16868. template<typename BasicJsonType>
  16869. using internal_iterator = ::nlohmann::detail::internal_iterator<BasicJsonType>;
  16870. template<typename BasicJsonType>
  16871. using iter_impl = ::nlohmann::detail::iter_impl<BasicJsonType>;
  16872. template<typename Iterator>
  16873. using iteration_proxy = ::nlohmann::detail::iteration_proxy<Iterator>;
  16874. template<typename Base> using json_reverse_iterator = ::nlohmann::detail::json_reverse_iterator<Base>;
  16875. template<typename CharType>
  16876. using output_adapter_t = ::nlohmann::detail::output_adapter_t<CharType>;
  16877. template<typename InputType>
  16878. using binary_reader = ::nlohmann::detail::binary_reader<basic_json, InputType>;
  16879. template<typename CharType> using binary_writer = ::nlohmann::detail::binary_writer<basic_json, CharType>;
  16880. JSON_PRIVATE_UNLESS_TESTED:
  16881. using serializer = ::nlohmann::detail::serializer<basic_json>;
  16882. public:
  16883. using value_t = detail::value_t;
  16884. /// JSON Pointer, see @ref nlohmann::json_pointer
  16885. using json_pointer = ::nlohmann::json_pointer<StringType>;
  16886. template<typename T, typename SFINAE>
  16887. using json_serializer = JSONSerializer<T, SFINAE>;
  16888. /// how to treat decoding errors
  16889. using error_handler_t = detail::error_handler_t;
  16890. /// how to treat CBOR tags
  16891. using cbor_tag_handler_t = detail::cbor_tag_handler_t;
  16892. /// helper type for initializer lists of basic_json values
  16893. using initializer_list_t = std::initializer_list<detail::json_ref<basic_json>>;
  16894. using input_format_t = detail::input_format_t;
  16895. /// SAX interface type, see @ref nlohmann::json_sax
  16896. using json_sax_t = json_sax<basic_json>;
  16897. ////////////////
  16898. // exceptions //
  16899. ////////////////
  16900. /// @name exceptions
  16901. /// Classes to implement user-defined exceptions.
  16902. /// @{
  16903. using exception = detail::exception;
  16904. using parse_error = detail::parse_error;
  16905. using invalid_iterator = detail::invalid_iterator;
  16906. using type_error = detail::type_error;
  16907. using out_of_range = detail::out_of_range;
  16908. using other_error = detail::other_error;
  16909. /// @}
  16910. /////////////////////
  16911. // container types //
  16912. /////////////////////
  16913. /// @name container types
  16914. /// The canonic container types to use @ref basic_json like any other STL
  16915. /// container.
  16916. /// @{
  16917. /// the type of elements in a basic_json container
  16918. using value_type = basic_json;
  16919. /// the type of an element reference
  16920. using reference = value_type&;
  16921. /// the type of an element const reference
  16922. using const_reference = const value_type&;
  16923. /// a type to represent differences between iterators
  16924. using difference_type = std::ptrdiff_t;
  16925. /// a type to represent container sizes
  16926. using size_type = std::size_t;
  16927. /// the allocator type
  16928. using allocator_type = AllocatorType<basic_json>;
  16929. /// the type of an element pointer
  16930. using pointer = typename std::allocator_traits<allocator_type>::pointer;
  16931. /// the type of an element const pointer
  16932. using const_pointer = typename std::allocator_traits<allocator_type>::const_pointer;
  16933. /// an iterator for a basic_json container
  16934. using iterator = iter_impl<basic_json>;
  16935. /// a const iterator for a basic_json container
  16936. using const_iterator = iter_impl<const basic_json>;
  16937. /// a reverse iterator for a basic_json container
  16938. using reverse_iterator = json_reverse_iterator<typename basic_json::iterator>;
  16939. /// a const reverse iterator for a basic_json container
  16940. using const_reverse_iterator = json_reverse_iterator<typename basic_json::const_iterator>;
  16941. /// @}
  16942. /// @brief returns the allocator associated with the container
  16943. /// @sa https://json.nlohmann.me/api/basic_json/get_allocator/
  16944. static allocator_type get_allocator()
  16945. {
  16946. return allocator_type();
  16947. }
  16948. /// @brief returns version information on the library
  16949. /// @sa https://json.nlohmann.me/api/basic_json/meta/
  16950. JSON_HEDLEY_WARN_UNUSED_RESULT
  16951. static basic_json meta()
  16952. {
  16953. basic_json result;
  16954. result["copyright"] = "(C) 2013-2022 Niels Lohmann";
  16955. result["name"] = "JSON for Modern C++";
  16956. result["url"] = "https://github.com/nlohmann/json";
  16957. result["version"]["string"] =
  16958. detail::concat(std::to_string(NLOHMANN_JSON_VERSION_MAJOR), '.',
  16959. std::to_string(NLOHMANN_JSON_VERSION_MINOR), '.',
  16960. std::to_string(NLOHMANN_JSON_VERSION_PATCH));
  16961. result["version"]["major"] = NLOHMANN_JSON_VERSION_MAJOR;
  16962. result["version"]["minor"] = NLOHMANN_JSON_VERSION_MINOR;
  16963. result["version"]["patch"] = NLOHMANN_JSON_VERSION_PATCH;
  16964. #ifdef _WIN32
  16965. result["platform"] = "win32";
  16966. #elif defined __linux__
  16967. result["platform"] = "linux";
  16968. #elif defined __APPLE__
  16969. result["platform"] = "apple";
  16970. #elif defined __unix__
  16971. result["platform"] = "unix";
  16972. #else
  16973. result["platform"] = "unknown";
  16974. #endif
  16975. #if defined(__ICC) || defined(__INTEL_COMPILER)
  16976. result["compiler"] = {{"family", "icc"}, {"version", __INTEL_COMPILER}};
  16977. #elif defined(__clang__)
  16978. result["compiler"] = {{"family", "clang"}, {"version", __clang_version__}};
  16979. #elif defined(__GNUC__) || defined(__GNUG__)
  16980. result["compiler"] = {{"family", "gcc"}, {"version", detail::concat(
  16981. std::to_string(__GNUC__), '.',
  16982. std::to_string(__GNUC_MINOR__), '.',
  16983. std::to_string(__GNUC_PATCHLEVEL__))
  16984. }
  16985. };
  16986. #elif defined(__HP_cc) || defined(__HP_aCC)
  16987. result["compiler"] = "hp"
  16988. #elif defined(__IBMCPP__)
  16989. result["compiler"] = {{"family", "ilecpp"}, {"version", __IBMCPP__}};
  16990. #elif defined(_MSC_VER)
  16991. result["compiler"] = {{"family", "msvc"}, {"version", _MSC_VER}};
  16992. #elif defined(__PGI)
  16993. result["compiler"] = {{"family", "pgcpp"}, {"version", __PGI}};
  16994. #elif defined(__SUNPRO_CC)
  16995. result["compiler"] = {{"family", "sunpro"}, {"version", __SUNPRO_CC}};
  16996. #else
  16997. result["compiler"] = {{"family", "unknown"}, {"version", "unknown"}};
  16998. #endif
  16999. #if defined(_MSVC_LANG)
  17000. result["compiler"]["c++"] = std::to_string(_MSVC_LANG);
  17001. #elif defined(__cplusplus)
  17002. result["compiler"]["c++"] = std::to_string(__cplusplus);
  17003. #else
  17004. result["compiler"]["c++"] = "unknown";
  17005. #endif
  17006. return result;
  17007. }
  17008. ///////////////////////////
  17009. // JSON value data types //
  17010. ///////////////////////////
  17011. /// @name JSON value data types
  17012. /// The data types to store a JSON value. These types are derived from
  17013. /// the template arguments passed to class @ref basic_json.
  17014. /// @{
  17015. /// @brief default object key comparator type
  17016. /// The actual object key comparator type (@ref object_comparator_t) may be
  17017. /// different.
  17018. /// @sa https://json.nlohmann.me/api/basic_json/default_object_comparator_t/
  17019. #if defined(JSON_HAS_CPP_14)
  17020. // use of transparent comparator avoids unnecessary repeated construction of temporaries
  17021. // in functions involving lookup by key with types other than object_t::key_type (aka. StringType)
  17022. using default_object_comparator_t = std::less<>;
  17023. #else
  17024. using default_object_comparator_t = std::less<StringType>;
  17025. #endif
  17026. /// @brief a type for an object
  17027. /// @sa https://json.nlohmann.me/api/basic_json/object_t/
  17028. using object_t = ObjectType<StringType,
  17029. basic_json,
  17030. default_object_comparator_t,
  17031. AllocatorType<std::pair<const StringType,
  17032. basic_json>>>;
  17033. /// @brief a type for an array
  17034. /// @sa https://json.nlohmann.me/api/basic_json/array_t/
  17035. using array_t = ArrayType<basic_json, AllocatorType<basic_json>>;
  17036. /// @brief a type for a string
  17037. /// @sa https://json.nlohmann.me/api/basic_json/string_t/
  17038. using string_t = StringType;
  17039. /// @brief a type for a boolean
  17040. /// @sa https://json.nlohmann.me/api/basic_json/boolean_t/
  17041. using boolean_t = BooleanType;
  17042. /// @brief a type for a number (integer)
  17043. /// @sa https://json.nlohmann.me/api/basic_json/number_integer_t/
  17044. using number_integer_t = NumberIntegerType;
  17045. /// @brief a type for a number (unsigned)
  17046. /// @sa https://json.nlohmann.me/api/basic_json/number_unsigned_t/
  17047. using number_unsigned_t = NumberUnsignedType;
  17048. /// @brief a type for a number (floating-point)
  17049. /// @sa https://json.nlohmann.me/api/basic_json/number_float_t/
  17050. using number_float_t = NumberFloatType;
  17051. /// @brief a type for a packed binary type
  17052. /// @sa https://json.nlohmann.me/api/basic_json/binary_t/
  17053. using binary_t = nlohmann::byte_container_with_subtype<BinaryType>;
  17054. /// @brief object key comparator type
  17055. /// @sa https://json.nlohmann.me/api/basic_json/object_comparator_t/
  17056. using object_comparator_t = detail::actual_object_comparator_t<basic_json>;
  17057. /// @}
  17058. private:
  17059. /// helper for exception-safe object creation
  17060. template<typename T, typename... Args>
  17061. JSON_HEDLEY_RETURNS_NON_NULL
  17062. static T* create(Args&& ... args)
  17063. {
  17064. AllocatorType<T> alloc;
  17065. using AllocatorTraits = std::allocator_traits<AllocatorType<T>>;
  17066. auto deleter = [&](T * obj)
  17067. {
  17068. AllocatorTraits::deallocate(alloc, obj, 1);
  17069. };
  17070. std::unique_ptr<T, decltype(deleter)> obj(AllocatorTraits::allocate(alloc, 1), deleter);
  17071. AllocatorTraits::construct(alloc, obj.get(), std::forward<Args>(args)...);
  17072. JSON_ASSERT(obj != nullptr);
  17073. return obj.release();
  17074. }
  17075. ////////////////////////
  17076. // JSON value storage //
  17077. ////////////////////////
  17078. JSON_PRIVATE_UNLESS_TESTED:
  17079. /*!
  17080. @brief a JSON value
  17081. The actual storage for a JSON value of the @ref basic_json class. This
  17082. union combines the different storage types for the JSON value types
  17083. defined in @ref value_t.
  17084. JSON type | value_t type | used type
  17085. --------- | --------------- | ------------------------
  17086. object | object | pointer to @ref object_t
  17087. array | array | pointer to @ref array_t
  17088. string | string | pointer to @ref string_t
  17089. boolean | boolean | @ref boolean_t
  17090. number | number_integer | @ref number_integer_t
  17091. number | number_unsigned | @ref number_unsigned_t
  17092. number | number_float | @ref number_float_t
  17093. binary | binary | pointer to @ref binary_t
  17094. null | null | *no value is stored*
  17095. @note Variable-length types (objects, arrays, and strings) are stored as
  17096. pointers. The size of the union should not exceed 64 bits if the default
  17097. value types are used.
  17098. @since version 1.0.0
  17099. */
  17100. union json_value
  17101. {
  17102. /// object (stored with pointer to save storage)
  17103. object_t* object;
  17104. /// array (stored with pointer to save storage)
  17105. array_t* array;
  17106. /// string (stored with pointer to save storage)
  17107. string_t* string;
  17108. /// binary (stored with pointer to save storage)
  17109. binary_t* binary;
  17110. /// boolean
  17111. boolean_t boolean;
  17112. /// number (integer)
  17113. number_integer_t number_integer;
  17114. /// number (unsigned integer)
  17115. number_unsigned_t number_unsigned;
  17116. /// number (floating-point)
  17117. number_float_t number_float;
  17118. /// default constructor (for null values)
  17119. json_value() = default;
  17120. /// constructor for booleans
  17121. json_value(boolean_t v) noexcept : boolean(v) {}
  17122. /// constructor for numbers (integer)
  17123. json_value(number_integer_t v) noexcept : number_integer(v) {}
  17124. /// constructor for numbers (unsigned)
  17125. json_value(number_unsigned_t v) noexcept : number_unsigned(v) {}
  17126. /// constructor for numbers (floating-point)
  17127. json_value(number_float_t v) noexcept : number_float(v) {}
  17128. /// constructor for empty values of a given type
  17129. json_value(value_t t)
  17130. {
  17131. switch (t)
  17132. {
  17133. case value_t::object:
  17134. {
  17135. object = create<object_t>();
  17136. break;
  17137. }
  17138. case value_t::array:
  17139. {
  17140. array = create<array_t>();
  17141. break;
  17142. }
  17143. case value_t::string:
  17144. {
  17145. string = create<string_t>("");
  17146. break;
  17147. }
  17148. case value_t::binary:
  17149. {
  17150. binary = create<binary_t>();
  17151. break;
  17152. }
  17153. case value_t::boolean:
  17154. {
  17155. boolean = static_cast<boolean_t>(false);
  17156. break;
  17157. }
  17158. case value_t::number_integer:
  17159. {
  17160. number_integer = static_cast<number_integer_t>(0);
  17161. break;
  17162. }
  17163. case value_t::number_unsigned:
  17164. {
  17165. number_unsigned = static_cast<number_unsigned_t>(0);
  17166. break;
  17167. }
  17168. case value_t::number_float:
  17169. {
  17170. number_float = static_cast<number_float_t>(0.0);
  17171. break;
  17172. }
  17173. case value_t::null:
  17174. {
  17175. object = nullptr; // silence warning, see #821
  17176. break;
  17177. }
  17178. case value_t::discarded:
  17179. default:
  17180. {
  17181. object = nullptr; // silence warning, see #821
  17182. if (JSON_HEDLEY_UNLIKELY(t == value_t::null))
  17183. {
  17184. JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.11.2", nullptr)); // LCOV_EXCL_LINE
  17185. }
  17186. break;
  17187. }
  17188. }
  17189. }
  17190. /// constructor for strings
  17191. json_value(const string_t& value) : string(create<string_t>(value)) {}
  17192. /// constructor for rvalue strings
  17193. json_value(string_t&& value) : string(create<string_t>(std::move(value))) {}
  17194. /// constructor for objects
  17195. json_value(const object_t& value) : object(create<object_t>(value)) {}
  17196. /// constructor for rvalue objects
  17197. json_value(object_t&& value) : object(create<object_t>(std::move(value))) {}
  17198. /// constructor for arrays
  17199. json_value(const array_t& value) : array(create<array_t>(value)) {}
  17200. /// constructor for rvalue arrays
  17201. json_value(array_t&& value) : array(create<array_t>(std::move(value))) {}
  17202. /// constructor for binary arrays
  17203. json_value(const typename binary_t::container_type& value) : binary(create<binary_t>(value)) {}
  17204. /// constructor for rvalue binary arrays
  17205. json_value(typename binary_t::container_type&& value) : binary(create<binary_t>(std::move(value))) {}
  17206. /// constructor for binary arrays (internal type)
  17207. json_value(const binary_t& value) : binary(create<binary_t>(value)) {}
  17208. /// constructor for rvalue binary arrays (internal type)
  17209. json_value(binary_t&& value) : binary(create<binary_t>(std::move(value))) {}
  17210. void destroy(value_t t)
  17211. {
  17212. if (
  17213. (t == value_t::object && object == nullptr) ||
  17214. (t == value_t::array && array == nullptr) ||
  17215. (t == value_t::string && string == nullptr) ||
  17216. (t == value_t::binary && binary == nullptr)
  17217. )
  17218. {
  17219. //not initialized (e.g. due to exception in the ctor)
  17220. return;
  17221. }
  17222. if (t == value_t::array || t == value_t::object)
  17223. {
  17224. // flatten the current json_value to a heap-allocated stack
  17225. std::vector<basic_json> stack;
  17226. // move the top-level items to stack
  17227. if (t == value_t::array)
  17228. {
  17229. stack.reserve(array->size());
  17230. std::move(array->begin(), array->end(), std::back_inserter(stack));
  17231. }
  17232. else
  17233. {
  17234. stack.reserve(object->size());
  17235. for (auto&& it : *object)
  17236. {
  17237. stack.push_back(std::move(it.second));
  17238. }
  17239. }
  17240. while (!stack.empty())
  17241. {
  17242. // move the last item to local variable to be processed
  17243. basic_json current_item(std::move(stack.back()));
  17244. stack.pop_back();
  17245. // if current_item is array/object, move
  17246. // its children to the stack to be processed later
  17247. if (current_item.is_array())
  17248. {
  17249. std::move(current_item.m_data.m_value.array->begin(), current_item.m_data.m_value.array->end(), std::back_inserter(stack));
  17250. current_item.m_data.m_value.array->clear();
  17251. }
  17252. else if (current_item.is_object())
  17253. {
  17254. for (auto&& it : *current_item.m_data.m_value.object)
  17255. {
  17256. stack.push_back(std::move(it.second));
  17257. }
  17258. current_item.m_data.m_value.object->clear();
  17259. }
  17260. // it's now safe that current_item get destructed
  17261. // since it doesn't have any children
  17262. }
  17263. }
  17264. switch (t)
  17265. {
  17266. case value_t::object:
  17267. {
  17268. AllocatorType<object_t> alloc;
  17269. std::allocator_traits<decltype(alloc)>::destroy(alloc, object);
  17270. std::allocator_traits<decltype(alloc)>::deallocate(alloc, object, 1);
  17271. break;
  17272. }
  17273. case value_t::array:
  17274. {
  17275. AllocatorType<array_t> alloc;
  17276. std::allocator_traits<decltype(alloc)>::destroy(alloc, array);
  17277. std::allocator_traits<decltype(alloc)>::deallocate(alloc, array, 1);
  17278. break;
  17279. }
  17280. case value_t::string:
  17281. {
  17282. AllocatorType<string_t> alloc;
  17283. std::allocator_traits<decltype(alloc)>::destroy(alloc, string);
  17284. std::allocator_traits<decltype(alloc)>::deallocate(alloc, string, 1);
  17285. break;
  17286. }
  17287. case value_t::binary:
  17288. {
  17289. AllocatorType<binary_t> alloc;
  17290. std::allocator_traits<decltype(alloc)>::destroy(alloc, binary);
  17291. std::allocator_traits<decltype(alloc)>::deallocate(alloc, binary, 1);
  17292. break;
  17293. }
  17294. case value_t::null:
  17295. case value_t::boolean:
  17296. case value_t::number_integer:
  17297. case value_t::number_unsigned:
  17298. case value_t::number_float:
  17299. case value_t::discarded:
  17300. default:
  17301. {
  17302. break;
  17303. }
  17304. }
  17305. }
  17306. };
  17307. private:
  17308. /*!
  17309. @brief checks the class invariants
  17310. This function asserts the class invariants. It needs to be called at the
  17311. end of every constructor to make sure that created objects respect the
  17312. invariant. Furthermore, it has to be called each time the type of a JSON
  17313. value is changed, because the invariant expresses a relationship between
  17314. @a m_type and @a m_value.
  17315. Furthermore, the parent relation is checked for arrays and objects: If
  17316. @a check_parents true and the value is an array or object, then the
  17317. container's elements must have the current value as parent.
  17318. @param[in] check_parents whether the parent relation should be checked.
  17319. The value is true by default and should only be set to false
  17320. during destruction of objects when the invariant does not
  17321. need to hold.
  17322. */
  17323. void assert_invariant(bool check_parents = true) const noexcept
  17324. {
  17325. JSON_ASSERT(m_data.m_type != value_t::object || m_data.m_value.object != nullptr);
  17326. JSON_ASSERT(m_data.m_type != value_t::array || m_data.m_value.array != nullptr);
  17327. JSON_ASSERT(m_data.m_type != value_t::string || m_data.m_value.string != nullptr);
  17328. JSON_ASSERT(m_data.m_type != value_t::binary || m_data.m_value.binary != nullptr);
  17329. #if JSON_DIAGNOSTICS
  17330. JSON_TRY
  17331. {
  17332. // cppcheck-suppress assertWithSideEffect
  17333. JSON_ASSERT(!check_parents || !is_structured() || std::all_of(begin(), end(), [this](const basic_json & j)
  17334. {
  17335. return j.m_parent == this;
  17336. }));
  17337. }
  17338. JSON_CATCH(...) {} // LCOV_EXCL_LINE
  17339. #endif
  17340. static_cast<void>(check_parents);
  17341. }
  17342. void set_parents()
  17343. {
  17344. #if JSON_DIAGNOSTICS
  17345. switch (m_data.m_type)
  17346. {
  17347. case value_t::array:
  17348. {
  17349. for (auto& element : *m_data.m_value.array)
  17350. {
  17351. element.m_parent = this;
  17352. }
  17353. break;
  17354. }
  17355. case value_t::object:
  17356. {
  17357. for (auto& element : *m_data.m_value.object)
  17358. {
  17359. element.second.m_parent = this;
  17360. }
  17361. break;
  17362. }
  17363. case value_t::null:
  17364. case value_t::string:
  17365. case value_t::boolean:
  17366. case value_t::number_integer:
  17367. case value_t::number_unsigned:
  17368. case value_t::number_float:
  17369. case value_t::binary:
  17370. case value_t::discarded:
  17371. default:
  17372. break;
  17373. }
  17374. #endif
  17375. }
  17376. iterator set_parents(iterator it, typename iterator::difference_type count_set_parents)
  17377. {
  17378. #if JSON_DIAGNOSTICS
  17379. for (typename iterator::difference_type i = 0; i < count_set_parents; ++i)
  17380. {
  17381. (it + i)->m_parent = this;
  17382. }
  17383. #else
  17384. static_cast<void>(count_set_parents);
  17385. #endif
  17386. return it;
  17387. }
  17388. reference set_parent(reference j, std::size_t old_capacity = static_cast<std::size_t>(-1))
  17389. {
  17390. #if JSON_DIAGNOSTICS
  17391. if (old_capacity != static_cast<std::size_t>(-1))
  17392. {
  17393. // see https://github.com/nlohmann/json/issues/2838
  17394. JSON_ASSERT(type() == value_t::array);
  17395. if (JSON_HEDLEY_UNLIKELY(m_data.m_value.array->capacity() != old_capacity))
  17396. {
  17397. // capacity has changed: update all parents
  17398. set_parents();
  17399. return j;
  17400. }
  17401. }
  17402. // ordered_json uses a vector internally, so pointers could have
  17403. // been invalidated; see https://github.com/nlohmann/json/issues/2962
  17404. #ifdef JSON_HEDLEY_MSVC_VERSION
  17405. #pragma warning(push )
  17406. #pragma warning(disable : 4127) // ignore warning to replace if with if constexpr
  17407. #endif
  17408. if (detail::is_ordered_map<object_t>::value)
  17409. {
  17410. set_parents();
  17411. return j;
  17412. }
  17413. #ifdef JSON_HEDLEY_MSVC_VERSION
  17414. #pragma warning( pop )
  17415. #endif
  17416. j.m_parent = this;
  17417. #else
  17418. static_cast<void>(j);
  17419. static_cast<void>(old_capacity);
  17420. #endif
  17421. return j;
  17422. }
  17423. public:
  17424. //////////////////////////
  17425. // JSON parser callback //
  17426. //////////////////////////
  17427. /// @brief parser event types
  17428. /// @sa https://json.nlohmann.me/api/basic_json/parse_event_t/
  17429. using parse_event_t = detail::parse_event_t;
  17430. /// @brief per-element parser callback type
  17431. /// @sa https://json.nlohmann.me/api/basic_json/parser_callback_t/
  17432. using parser_callback_t = detail::parser_callback_t<basic_json>;
  17433. //////////////////
  17434. // constructors //
  17435. //////////////////
  17436. /// @name constructors and destructors
  17437. /// Constructors of class @ref basic_json, copy/move constructor, copy
  17438. /// assignment, static functions creating objects, and the destructor.
  17439. /// @{
  17440. /// @brief create an empty value with a given type
  17441. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17442. basic_json(const value_t v)
  17443. : m_data(v)
  17444. {
  17445. assert_invariant();
  17446. }
  17447. /// @brief create a null object
  17448. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17449. basic_json(std::nullptr_t = nullptr) noexcept // NOLINT(bugprone-exception-escape)
  17450. : basic_json(value_t::null)
  17451. {
  17452. assert_invariant();
  17453. }
  17454. /// @brief create a JSON value from compatible types
  17455. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17456. template < typename CompatibleType,
  17457. typename U = detail::uncvref_t<CompatibleType>,
  17458. detail::enable_if_t <
  17459. !detail::is_basic_json<U>::value && detail::is_compatible_type<basic_json_t, U>::value, int > = 0 >
  17460. basic_json(CompatibleType && val) noexcept(noexcept( // NOLINT(bugprone-forwarding-reference-overload,bugprone-exception-escape)
  17461. JSONSerializer<U>::to_json(std::declval<basic_json_t&>(),
  17462. std::forward<CompatibleType>(val))))
  17463. {
  17464. JSONSerializer<U>::to_json(*this, std::forward<CompatibleType>(val));
  17465. set_parents();
  17466. assert_invariant();
  17467. }
  17468. /// @brief create a JSON value from an existing one
  17469. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17470. template < typename BasicJsonType,
  17471. detail::enable_if_t <
  17472. detail::is_basic_json<BasicJsonType>::value&& !std::is_same<basic_json, BasicJsonType>::value, int > = 0 >
  17473. basic_json(const BasicJsonType& val)
  17474. {
  17475. using other_boolean_t = typename BasicJsonType::boolean_t;
  17476. using other_number_float_t = typename BasicJsonType::number_float_t;
  17477. using other_number_integer_t = typename BasicJsonType::number_integer_t;
  17478. using other_number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  17479. using other_string_t = typename BasicJsonType::string_t;
  17480. using other_object_t = typename BasicJsonType::object_t;
  17481. using other_array_t = typename BasicJsonType::array_t;
  17482. using other_binary_t = typename BasicJsonType::binary_t;
  17483. switch (val.type())
  17484. {
  17485. case value_t::boolean:
  17486. JSONSerializer<other_boolean_t>::to_json(*this, val.template get<other_boolean_t>());
  17487. break;
  17488. case value_t::number_float:
  17489. JSONSerializer<other_number_float_t>::to_json(*this, val.template get<other_number_float_t>());
  17490. break;
  17491. case value_t::number_integer:
  17492. JSONSerializer<other_number_integer_t>::to_json(*this, val.template get<other_number_integer_t>());
  17493. break;
  17494. case value_t::number_unsigned:
  17495. JSONSerializer<other_number_unsigned_t>::to_json(*this, val.template get<other_number_unsigned_t>());
  17496. break;
  17497. case value_t::string:
  17498. JSONSerializer<other_string_t>::to_json(*this, val.template get_ref<const other_string_t&>());
  17499. break;
  17500. case value_t::object:
  17501. JSONSerializer<other_object_t>::to_json(*this, val.template get_ref<const other_object_t&>());
  17502. break;
  17503. case value_t::array:
  17504. JSONSerializer<other_array_t>::to_json(*this, val.template get_ref<const other_array_t&>());
  17505. break;
  17506. case value_t::binary:
  17507. JSONSerializer<other_binary_t>::to_json(*this, val.template get_ref<const other_binary_t&>());
  17508. break;
  17509. case value_t::null:
  17510. *this = nullptr;
  17511. break;
  17512. case value_t::discarded:
  17513. m_data.m_type = value_t::discarded;
  17514. break;
  17515. default: // LCOV_EXCL_LINE
  17516. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  17517. }
  17518. JSON_ASSERT(m_data.m_type == val.type());
  17519. set_parents();
  17520. assert_invariant();
  17521. }
  17522. /// @brief create a container (array or object) from an initializer list
  17523. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17524. basic_json(initializer_list_t init,
  17525. bool type_deduction = true,
  17526. value_t manual_type = value_t::array)
  17527. {
  17528. // check if each element is an array with two elements whose first
  17529. // element is a string
  17530. bool is_an_object = std::all_of(init.begin(), init.end(),
  17531. [](const detail::json_ref<basic_json>& element_ref)
  17532. {
  17533. // The cast is to ensure op[size_type] is called, bearing in mind size_type may not be int;
  17534. // (many string types can be constructed from 0 via its null-pointer guise, so we get a
  17535. // broken call to op[key_type], the wrong semantics and a 4804 warning on Windows)
  17536. return element_ref->is_array() && element_ref->size() == 2 && (*element_ref)[static_cast<size_type>(0)].is_string();
  17537. });
  17538. // adjust type if type deduction is not wanted
  17539. if (!type_deduction)
  17540. {
  17541. // if array is wanted, do not create an object though possible
  17542. if (manual_type == value_t::array)
  17543. {
  17544. is_an_object = false;
  17545. }
  17546. // if object is wanted but impossible, throw an exception
  17547. if (JSON_HEDLEY_UNLIKELY(manual_type == value_t::object && !is_an_object))
  17548. {
  17549. JSON_THROW(type_error::create(301, "cannot create object from initializer list", nullptr));
  17550. }
  17551. }
  17552. if (is_an_object)
  17553. {
  17554. // the initializer list is a list of pairs -> create object
  17555. m_data.m_type = value_t::object;
  17556. m_data.m_value = value_t::object;
  17557. for (auto& element_ref : init)
  17558. {
  17559. auto element = element_ref.moved_or_copied();
  17560. m_data.m_value.object->emplace(
  17561. std::move(*((*element.m_data.m_value.array)[0].m_data.m_value.string)),
  17562. std::move((*element.m_data.m_value.array)[1]));
  17563. }
  17564. }
  17565. else
  17566. {
  17567. // the initializer list describes an array -> create array
  17568. m_data.m_type = value_t::array;
  17569. m_data.m_value.array = create<array_t>(init.begin(), init.end());
  17570. }
  17571. set_parents();
  17572. assert_invariant();
  17573. }
  17574. /// @brief explicitly create a binary array (without subtype)
  17575. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  17576. JSON_HEDLEY_WARN_UNUSED_RESULT
  17577. static basic_json binary(const typename binary_t::container_type& init)
  17578. {
  17579. auto res = basic_json();
  17580. res.m_data.m_type = value_t::binary;
  17581. res.m_data.m_value = init;
  17582. return res;
  17583. }
  17584. /// @brief explicitly create a binary array (with subtype)
  17585. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  17586. JSON_HEDLEY_WARN_UNUSED_RESULT
  17587. static basic_json binary(const typename binary_t::container_type& init, typename binary_t::subtype_type subtype)
  17588. {
  17589. auto res = basic_json();
  17590. res.m_data.m_type = value_t::binary;
  17591. res.m_data.m_value = binary_t(init, subtype);
  17592. return res;
  17593. }
  17594. /// @brief explicitly create a binary array
  17595. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  17596. JSON_HEDLEY_WARN_UNUSED_RESULT
  17597. static basic_json binary(typename binary_t::container_type&& init)
  17598. {
  17599. auto res = basic_json();
  17600. res.m_data.m_type = value_t::binary;
  17601. res.m_data.m_value = std::move(init);
  17602. return res;
  17603. }
  17604. /// @brief explicitly create a binary array (with subtype)
  17605. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  17606. JSON_HEDLEY_WARN_UNUSED_RESULT
  17607. static basic_json binary(typename binary_t::container_type&& init, typename binary_t::subtype_type subtype)
  17608. {
  17609. auto res = basic_json();
  17610. res.m_data.m_type = value_t::binary;
  17611. res.m_data.m_value = binary_t(std::move(init), subtype);
  17612. return res;
  17613. }
  17614. /// @brief explicitly create an array from an initializer list
  17615. /// @sa https://json.nlohmann.me/api/basic_json/array/
  17616. JSON_HEDLEY_WARN_UNUSED_RESULT
  17617. static basic_json array(initializer_list_t init = {})
  17618. {
  17619. return basic_json(init, false, value_t::array);
  17620. }
  17621. /// @brief explicitly create an object from an initializer list
  17622. /// @sa https://json.nlohmann.me/api/basic_json/object/
  17623. JSON_HEDLEY_WARN_UNUSED_RESULT
  17624. static basic_json object(initializer_list_t init = {})
  17625. {
  17626. return basic_json(init, false, value_t::object);
  17627. }
  17628. /// @brief construct an array with count copies of given value
  17629. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17630. basic_json(size_type cnt, const basic_json& val):
  17631. m_data{cnt, val}
  17632. {
  17633. set_parents();
  17634. assert_invariant();
  17635. }
  17636. /// @brief construct a JSON container given an iterator range
  17637. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17638. template < class InputIT, typename std::enable_if <
  17639. std::is_same<InputIT, typename basic_json_t::iterator>::value ||
  17640. std::is_same<InputIT, typename basic_json_t::const_iterator>::value, int >::type = 0 >
  17641. basic_json(InputIT first, InputIT last)
  17642. {
  17643. JSON_ASSERT(first.m_object != nullptr);
  17644. JSON_ASSERT(last.m_object != nullptr);
  17645. // make sure iterator fits the current value
  17646. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  17647. {
  17648. JSON_THROW(invalid_iterator::create(201, "iterators are not compatible", nullptr));
  17649. }
  17650. // copy type from first iterator
  17651. m_data.m_type = first.m_object->m_data.m_type;
  17652. // check if iterator range is complete for primitive values
  17653. switch (m_data.m_type)
  17654. {
  17655. case value_t::boolean:
  17656. case value_t::number_float:
  17657. case value_t::number_integer:
  17658. case value_t::number_unsigned:
  17659. case value_t::string:
  17660. {
  17661. if (JSON_HEDLEY_UNLIKELY(!first.m_it.primitive_iterator.is_begin()
  17662. || !last.m_it.primitive_iterator.is_end()))
  17663. {
  17664. JSON_THROW(invalid_iterator::create(204, "iterators out of range", first.m_object));
  17665. }
  17666. break;
  17667. }
  17668. case value_t::null:
  17669. case value_t::object:
  17670. case value_t::array:
  17671. case value_t::binary:
  17672. case value_t::discarded:
  17673. default:
  17674. break;
  17675. }
  17676. switch (m_data.m_type)
  17677. {
  17678. case value_t::number_integer:
  17679. {
  17680. m_data.m_value.number_integer = first.m_object->m_data.m_value.number_integer;
  17681. break;
  17682. }
  17683. case value_t::number_unsigned:
  17684. {
  17685. m_data.m_value.number_unsigned = first.m_object->m_data.m_value.number_unsigned;
  17686. break;
  17687. }
  17688. case value_t::number_float:
  17689. {
  17690. m_data.m_value.number_float = first.m_object->m_data.m_value.number_float;
  17691. break;
  17692. }
  17693. case value_t::boolean:
  17694. {
  17695. m_data.m_value.boolean = first.m_object->m_data.m_value.boolean;
  17696. break;
  17697. }
  17698. case value_t::string:
  17699. {
  17700. m_data.m_value = *first.m_object->m_data.m_value.string;
  17701. break;
  17702. }
  17703. case value_t::object:
  17704. {
  17705. m_data.m_value.object = create<object_t>(first.m_it.object_iterator,
  17706. last.m_it.object_iterator);
  17707. break;
  17708. }
  17709. case value_t::array:
  17710. {
  17711. m_data.m_value.array = create<array_t>(first.m_it.array_iterator,
  17712. last.m_it.array_iterator);
  17713. break;
  17714. }
  17715. case value_t::binary:
  17716. {
  17717. m_data.m_value = *first.m_object->m_data.m_value.binary;
  17718. break;
  17719. }
  17720. case value_t::null:
  17721. case value_t::discarded:
  17722. default:
  17723. JSON_THROW(invalid_iterator::create(206, detail::concat("cannot construct with iterators from ", first.m_object->type_name()), first.m_object));
  17724. }
  17725. set_parents();
  17726. assert_invariant();
  17727. }
  17728. ///////////////////////////////////////
  17729. // other constructors and destructor //
  17730. ///////////////////////////////////////
  17731. template<typename JsonRef,
  17732. detail::enable_if_t<detail::conjunction<detail::is_json_ref<JsonRef>,
  17733. std::is_same<typename JsonRef::value_type, basic_json>>::value, int> = 0 >
  17734. basic_json(const JsonRef& ref) : basic_json(ref.moved_or_copied()) {}
  17735. /// @brief copy constructor
  17736. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17737. basic_json(const basic_json& other)
  17738. : json_base_class_t(other)
  17739. {
  17740. m_data.m_type = other.m_data.m_type;
  17741. // check of passed value is valid
  17742. other.assert_invariant();
  17743. switch (m_data.m_type)
  17744. {
  17745. case value_t::object:
  17746. {
  17747. m_data.m_value = *other.m_data.m_value.object;
  17748. break;
  17749. }
  17750. case value_t::array:
  17751. {
  17752. m_data.m_value = *other.m_data.m_value.array;
  17753. break;
  17754. }
  17755. case value_t::string:
  17756. {
  17757. m_data.m_value = *other.m_data.m_value.string;
  17758. break;
  17759. }
  17760. case value_t::boolean:
  17761. {
  17762. m_data.m_value = other.m_data.m_value.boolean;
  17763. break;
  17764. }
  17765. case value_t::number_integer:
  17766. {
  17767. m_data.m_value = other.m_data.m_value.number_integer;
  17768. break;
  17769. }
  17770. case value_t::number_unsigned:
  17771. {
  17772. m_data.m_value = other.m_data.m_value.number_unsigned;
  17773. break;
  17774. }
  17775. case value_t::number_float:
  17776. {
  17777. m_data.m_value = other.m_data.m_value.number_float;
  17778. break;
  17779. }
  17780. case value_t::binary:
  17781. {
  17782. m_data.m_value = *other.m_data.m_value.binary;
  17783. break;
  17784. }
  17785. case value_t::null:
  17786. case value_t::discarded:
  17787. default:
  17788. break;
  17789. }
  17790. set_parents();
  17791. assert_invariant();
  17792. }
  17793. /// @brief move constructor
  17794. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17795. basic_json(basic_json&& other) noexcept
  17796. : json_base_class_t(std::forward<json_base_class_t>(other)),
  17797. m_data(std::move(other.m_data))
  17798. {
  17799. // check that passed value is valid
  17800. other.assert_invariant(false);
  17801. // invalidate payload
  17802. other.m_data.m_type = value_t::null;
  17803. other.m_data.m_value = {};
  17804. set_parents();
  17805. assert_invariant();
  17806. }
  17807. /// @brief copy assignment
  17808. /// @sa https://json.nlohmann.me/api/basic_json/operator=/
  17809. basic_json& operator=(basic_json other) noexcept (
  17810. std::is_nothrow_move_constructible<value_t>::value&&
  17811. std::is_nothrow_move_assignable<value_t>::value&&
  17812. std::is_nothrow_move_constructible<json_value>::value&&
  17813. std::is_nothrow_move_assignable<json_value>::value&&
  17814. std::is_nothrow_move_assignable<json_base_class_t>::value
  17815. )
  17816. {
  17817. // check that passed value is valid
  17818. other.assert_invariant();
  17819. using std::swap;
  17820. swap(m_data.m_type, other.m_data.m_type);
  17821. swap(m_data.m_value, other.m_data.m_value);
  17822. json_base_class_t::operator=(std::move(other));
  17823. set_parents();
  17824. assert_invariant();
  17825. return *this;
  17826. }
  17827. /// @brief destructor
  17828. /// @sa https://json.nlohmann.me/api/basic_json/~basic_json/
  17829. ~basic_json() noexcept
  17830. {
  17831. assert_invariant(false);
  17832. }
  17833. /// @}
  17834. public:
  17835. ///////////////////////
  17836. // object inspection //
  17837. ///////////////////////
  17838. /// @name object inspection
  17839. /// Functions to inspect the type of a JSON value.
  17840. /// @{
  17841. /// @brief serialization
  17842. /// @sa https://json.nlohmann.me/api/basic_json/dump/
  17843. string_t dump(const int indent = -1,
  17844. const char indent_char = ' ',
  17845. const bool ensure_ascii = false,
  17846. const error_handler_t error_handler = error_handler_t::strict) const
  17847. {
  17848. string_t result;
  17849. serializer s(detail::output_adapter<char, string_t>(result), indent_char, error_handler);
  17850. if (indent >= 0)
  17851. {
  17852. s.dump(*this, true, ensure_ascii, static_cast<unsigned int>(indent));
  17853. }
  17854. else
  17855. {
  17856. s.dump(*this, false, ensure_ascii, 0);
  17857. }
  17858. return result;
  17859. }
  17860. /// @brief return the type of the JSON value (explicit)
  17861. /// @sa https://json.nlohmann.me/api/basic_json/type/
  17862. constexpr value_t type() const noexcept
  17863. {
  17864. return m_data.m_type;
  17865. }
  17866. /// @brief return whether type is primitive
  17867. /// @sa https://json.nlohmann.me/api/basic_json/is_primitive/
  17868. constexpr bool is_primitive() const noexcept
  17869. {
  17870. return is_null() || is_string() || is_boolean() || is_number() || is_binary();
  17871. }
  17872. /// @brief return whether type is structured
  17873. /// @sa https://json.nlohmann.me/api/basic_json/is_structured/
  17874. constexpr bool is_structured() const noexcept
  17875. {
  17876. return is_array() || is_object();
  17877. }
  17878. /// @brief return whether value is null
  17879. /// @sa https://json.nlohmann.me/api/basic_json/is_null/
  17880. constexpr bool is_null() const noexcept
  17881. {
  17882. return m_data.m_type == value_t::null;
  17883. }
  17884. /// @brief return whether value is a boolean
  17885. /// @sa https://json.nlohmann.me/api/basic_json/is_boolean/
  17886. constexpr bool is_boolean() const noexcept
  17887. {
  17888. return m_data.m_type == value_t::boolean;
  17889. }
  17890. /// @brief return whether value is a number
  17891. /// @sa https://json.nlohmann.me/api/basic_json/is_number/
  17892. constexpr bool is_number() const noexcept
  17893. {
  17894. return is_number_integer() || is_number_float();
  17895. }
  17896. /// @brief return whether value is an integer number
  17897. /// @sa https://json.nlohmann.me/api/basic_json/is_number_integer/
  17898. constexpr bool is_number_integer() const noexcept
  17899. {
  17900. return m_data.m_type == value_t::number_integer || m_data.m_type == value_t::number_unsigned;
  17901. }
  17902. /// @brief return whether value is an unsigned integer number
  17903. /// @sa https://json.nlohmann.me/api/basic_json/is_number_unsigned/
  17904. constexpr bool is_number_unsigned() const noexcept
  17905. {
  17906. return m_data.m_type == value_t::number_unsigned;
  17907. }
  17908. /// @brief return whether value is a floating-point number
  17909. /// @sa https://json.nlohmann.me/api/basic_json/is_number_float/
  17910. constexpr bool is_number_float() const noexcept
  17911. {
  17912. return m_data.m_type == value_t::number_float;
  17913. }
  17914. /// @brief return whether value is an object
  17915. /// @sa https://json.nlohmann.me/api/basic_json/is_object/
  17916. constexpr bool is_object() const noexcept
  17917. {
  17918. return m_data.m_type == value_t::object;
  17919. }
  17920. /// @brief return whether value is an array
  17921. /// @sa https://json.nlohmann.me/api/basic_json/is_array/
  17922. constexpr bool is_array() const noexcept
  17923. {
  17924. return m_data.m_type == value_t::array;
  17925. }
  17926. /// @brief return whether value is a string
  17927. /// @sa https://json.nlohmann.me/api/basic_json/is_string/
  17928. constexpr bool is_string() const noexcept
  17929. {
  17930. return m_data.m_type == value_t::string;
  17931. }
  17932. /// @brief return whether value is a binary array
  17933. /// @sa https://json.nlohmann.me/api/basic_json/is_binary/
  17934. constexpr bool is_binary() const noexcept
  17935. {
  17936. return m_data.m_type == value_t::binary;
  17937. }
  17938. /// @brief return whether value is discarded
  17939. /// @sa https://json.nlohmann.me/api/basic_json/is_discarded/
  17940. constexpr bool is_discarded() const noexcept
  17941. {
  17942. return m_data.m_type == value_t::discarded;
  17943. }
  17944. /// @brief return the type of the JSON value (implicit)
  17945. /// @sa https://json.nlohmann.me/api/basic_json/operator_value_t/
  17946. constexpr operator value_t() const noexcept
  17947. {
  17948. return m_data.m_type;
  17949. }
  17950. /// @}
  17951. private:
  17952. //////////////////
  17953. // value access //
  17954. //////////////////
  17955. /// get a boolean (explicit)
  17956. boolean_t get_impl(boolean_t* /*unused*/) const
  17957. {
  17958. if (JSON_HEDLEY_LIKELY(is_boolean()))
  17959. {
  17960. return m_data.m_value.boolean;
  17961. }
  17962. JSON_THROW(type_error::create(302, detail::concat("type must be boolean, but is ", type_name()), this));
  17963. }
  17964. /// get a pointer to the value (object)
  17965. object_t* get_impl_ptr(object_t* /*unused*/) noexcept
  17966. {
  17967. return is_object() ? m_data.m_value.object : nullptr;
  17968. }
  17969. /// get a pointer to the value (object)
  17970. constexpr const object_t* get_impl_ptr(const object_t* /*unused*/) const noexcept
  17971. {
  17972. return is_object() ? m_data.m_value.object : nullptr;
  17973. }
  17974. /// get a pointer to the value (array)
  17975. array_t* get_impl_ptr(array_t* /*unused*/) noexcept
  17976. {
  17977. return is_array() ? m_data.m_value.array : nullptr;
  17978. }
  17979. /// get a pointer to the value (array)
  17980. constexpr const array_t* get_impl_ptr(const array_t* /*unused*/) const noexcept
  17981. {
  17982. return is_array() ? m_data.m_value.array : nullptr;
  17983. }
  17984. /// get a pointer to the value (string)
  17985. string_t* get_impl_ptr(string_t* /*unused*/) noexcept
  17986. {
  17987. return is_string() ? m_data.m_value.string : nullptr;
  17988. }
  17989. /// get a pointer to the value (string)
  17990. constexpr const string_t* get_impl_ptr(const string_t* /*unused*/) const noexcept
  17991. {
  17992. return is_string() ? m_data.m_value.string : nullptr;
  17993. }
  17994. /// get a pointer to the value (boolean)
  17995. boolean_t* get_impl_ptr(boolean_t* /*unused*/) noexcept
  17996. {
  17997. return is_boolean() ? &m_data.m_value.boolean : nullptr;
  17998. }
  17999. /// get a pointer to the value (boolean)
  18000. constexpr const boolean_t* get_impl_ptr(const boolean_t* /*unused*/) const noexcept
  18001. {
  18002. return is_boolean() ? &m_data.m_value.boolean : nullptr;
  18003. }
  18004. /// get a pointer to the value (integer number)
  18005. number_integer_t* get_impl_ptr(number_integer_t* /*unused*/) noexcept
  18006. {
  18007. return is_number_integer() ? &m_data.m_value.number_integer : nullptr;
  18008. }
  18009. /// get a pointer to the value (integer number)
  18010. constexpr const number_integer_t* get_impl_ptr(const number_integer_t* /*unused*/) const noexcept
  18011. {
  18012. return is_number_integer() ? &m_data.m_value.number_integer : nullptr;
  18013. }
  18014. /// get a pointer to the value (unsigned number)
  18015. number_unsigned_t* get_impl_ptr(number_unsigned_t* /*unused*/) noexcept
  18016. {
  18017. return is_number_unsigned() ? &m_data.m_value.number_unsigned : nullptr;
  18018. }
  18019. /// get a pointer to the value (unsigned number)
  18020. constexpr const number_unsigned_t* get_impl_ptr(const number_unsigned_t* /*unused*/) const noexcept
  18021. {
  18022. return is_number_unsigned() ? &m_data.m_value.number_unsigned : nullptr;
  18023. }
  18024. /// get a pointer to the value (floating-point number)
  18025. number_float_t* get_impl_ptr(number_float_t* /*unused*/) noexcept
  18026. {
  18027. return is_number_float() ? &m_data.m_value.number_float : nullptr;
  18028. }
  18029. /// get a pointer to the value (floating-point number)
  18030. constexpr const number_float_t* get_impl_ptr(const number_float_t* /*unused*/) const noexcept
  18031. {
  18032. return is_number_float() ? &m_data.m_value.number_float : nullptr;
  18033. }
  18034. /// get a pointer to the value (binary)
  18035. binary_t* get_impl_ptr(binary_t* /*unused*/) noexcept
  18036. {
  18037. return is_binary() ? m_data.m_value.binary : nullptr;
  18038. }
  18039. /// get a pointer to the value (binary)
  18040. constexpr const binary_t* get_impl_ptr(const binary_t* /*unused*/) const noexcept
  18041. {
  18042. return is_binary() ? m_data.m_value.binary : nullptr;
  18043. }
  18044. /*!
  18045. @brief helper function to implement get_ref()
  18046. This function helps to implement get_ref() without code duplication for
  18047. const and non-const overloads
  18048. @tparam ThisType will be deduced as `basic_json` or `const basic_json`
  18049. @throw type_error.303 if ReferenceType does not match underlying value
  18050. type of the current JSON
  18051. */
  18052. template<typename ReferenceType, typename ThisType>
  18053. static ReferenceType get_ref_impl(ThisType& obj)
  18054. {
  18055. // delegate the call to get_ptr<>()
  18056. auto* ptr = obj.template get_ptr<typename std::add_pointer<ReferenceType>::type>();
  18057. if (JSON_HEDLEY_LIKELY(ptr != nullptr))
  18058. {
  18059. return *ptr;
  18060. }
  18061. JSON_THROW(type_error::create(303, detail::concat("incompatible ReferenceType for get_ref, actual type is ", obj.type_name()), &obj));
  18062. }
  18063. public:
  18064. /// @name value access
  18065. /// Direct access to the stored value of a JSON value.
  18066. /// @{
  18067. /// @brief get a pointer value (implicit)
  18068. /// @sa https://json.nlohmann.me/api/basic_json/get_ptr/
  18069. template<typename PointerType, typename std::enable_if<
  18070. std::is_pointer<PointerType>::value, int>::type = 0>
  18071. auto get_ptr() noexcept -> decltype(std::declval<basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
  18072. {
  18073. // delegate the call to get_impl_ptr<>()
  18074. return get_impl_ptr(static_cast<PointerType>(nullptr));
  18075. }
  18076. /// @brief get a pointer value (implicit)
  18077. /// @sa https://json.nlohmann.me/api/basic_json/get_ptr/
  18078. template < typename PointerType, typename std::enable_if <
  18079. std::is_pointer<PointerType>::value&&
  18080. std::is_const<typename std::remove_pointer<PointerType>::type>::value, int >::type = 0 >
  18081. constexpr auto get_ptr() const noexcept -> decltype(std::declval<const basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
  18082. {
  18083. // delegate the call to get_impl_ptr<>() const
  18084. return get_impl_ptr(static_cast<PointerType>(nullptr));
  18085. }
  18086. private:
  18087. /*!
  18088. @brief get a value (explicit)
  18089. Explicit type conversion between the JSON value and a compatible value
  18090. which is [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)
  18091. and [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible).
  18092. The value is converted by calling the @ref json_serializer<ValueType>
  18093. `from_json()` method.
  18094. The function is equivalent to executing
  18095. @code {.cpp}
  18096. ValueType ret;
  18097. JSONSerializer<ValueType>::from_json(*this, ret);
  18098. return ret;
  18099. @endcode
  18100. This overloads is chosen if:
  18101. - @a ValueType is not @ref basic_json,
  18102. - @ref json_serializer<ValueType> has a `from_json()` method of the form
  18103. `void from_json(const basic_json&, ValueType&)`, and
  18104. - @ref json_serializer<ValueType> does not have a `from_json()` method of
  18105. the form `ValueType from_json(const basic_json&)`
  18106. @tparam ValueType the returned value type
  18107. @return copy of the JSON value, converted to @a ValueType
  18108. @throw what @ref json_serializer<ValueType> `from_json()` method throws
  18109. @liveexample{The example below shows several conversions from JSON values
  18110. to other types. There a few things to note: (1) Floating-point numbers can
  18111. be converted to integers\, (2) A JSON array can be converted to a standard
  18112. `std::vector<short>`\, (3) A JSON object can be converted to C++
  18113. associative containers such as `std::unordered_map<std::string\,
  18114. json>`.,get__ValueType_const}
  18115. @since version 2.1.0
  18116. */
  18117. template < typename ValueType,
  18118. detail::enable_if_t <
  18119. detail::is_default_constructible<ValueType>::value&&
  18120. detail::has_from_json<basic_json_t, ValueType>::value,
  18121. int > = 0 >
  18122. ValueType get_impl(detail::priority_tag<0> /*unused*/) const noexcept(noexcept(
  18123. JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), std::declval<ValueType&>())))
  18124. {
  18125. auto ret = ValueType();
  18126. JSONSerializer<ValueType>::from_json(*this, ret);
  18127. return ret;
  18128. }
  18129. /*!
  18130. @brief get a value (explicit); special case
  18131. Explicit type conversion between the JSON value and a compatible value
  18132. which is **not** [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)
  18133. and **not** [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible).
  18134. The value is converted by calling the @ref json_serializer<ValueType>
  18135. `from_json()` method.
  18136. The function is equivalent to executing
  18137. @code {.cpp}
  18138. return JSONSerializer<ValueType>::from_json(*this);
  18139. @endcode
  18140. This overloads is chosen if:
  18141. - @a ValueType is not @ref basic_json and
  18142. - @ref json_serializer<ValueType> has a `from_json()` method of the form
  18143. `ValueType from_json(const basic_json&)`
  18144. @note If @ref json_serializer<ValueType> has both overloads of
  18145. `from_json()`, this one is chosen.
  18146. @tparam ValueType the returned value type
  18147. @return copy of the JSON value, converted to @a ValueType
  18148. @throw what @ref json_serializer<ValueType> `from_json()` method throws
  18149. @since version 2.1.0
  18150. */
  18151. template < typename ValueType,
  18152. detail::enable_if_t <
  18153. detail::has_non_default_from_json<basic_json_t, ValueType>::value,
  18154. int > = 0 >
  18155. ValueType get_impl(detail::priority_tag<1> /*unused*/) const noexcept(noexcept(
  18156. JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>())))
  18157. {
  18158. return JSONSerializer<ValueType>::from_json(*this);
  18159. }
  18160. /*!
  18161. @brief get special-case overload
  18162. This overloads converts the current @ref basic_json in a different
  18163. @ref basic_json type
  18164. @tparam BasicJsonType == @ref basic_json
  18165. @return a copy of *this, converted into @a BasicJsonType
  18166. @complexity Depending on the implementation of the called `from_json()`
  18167. method.
  18168. @since version 3.2.0
  18169. */
  18170. template < typename BasicJsonType,
  18171. detail::enable_if_t <
  18172. detail::is_basic_json<BasicJsonType>::value,
  18173. int > = 0 >
  18174. BasicJsonType get_impl(detail::priority_tag<2> /*unused*/) const
  18175. {
  18176. return *this;
  18177. }
  18178. /*!
  18179. @brief get special-case overload
  18180. This overloads avoids a lot of template boilerplate, it can be seen as the
  18181. identity method
  18182. @tparam BasicJsonType == @ref basic_json
  18183. @return a copy of *this
  18184. @complexity Constant.
  18185. @since version 2.1.0
  18186. */
  18187. template<typename BasicJsonType,
  18188. detail::enable_if_t<
  18189. std::is_same<BasicJsonType, basic_json_t>::value,
  18190. int> = 0>
  18191. basic_json get_impl(detail::priority_tag<3> /*unused*/) const
  18192. {
  18193. return *this;
  18194. }
  18195. /*!
  18196. @brief get a pointer value (explicit)
  18197. @copydoc get()
  18198. */
  18199. template<typename PointerType,
  18200. detail::enable_if_t<
  18201. std::is_pointer<PointerType>::value,
  18202. int> = 0>
  18203. constexpr auto get_impl(detail::priority_tag<4> /*unused*/) const noexcept
  18204. -> decltype(std::declval<const basic_json_t&>().template get_ptr<PointerType>())
  18205. {
  18206. // delegate the call to get_ptr
  18207. return get_ptr<PointerType>();
  18208. }
  18209. public:
  18210. /*!
  18211. @brief get a (pointer) value (explicit)
  18212. Performs explicit type conversion between the JSON value and a compatible value if required.
  18213. - If the requested type is a pointer to the internally stored JSON value that pointer is returned.
  18214. No copies are made.
  18215. - If the requested type is the current @ref basic_json, or a different @ref basic_json convertible
  18216. from the current @ref basic_json.
  18217. - Otherwise the value is converted by calling the @ref json_serializer<ValueType> `from_json()`
  18218. method.
  18219. @tparam ValueTypeCV the provided value type
  18220. @tparam ValueType the returned value type
  18221. @return copy of the JSON value, converted to @tparam ValueType if necessary
  18222. @throw what @ref json_serializer<ValueType> `from_json()` method throws if conversion is required
  18223. @since version 2.1.0
  18224. */
  18225. template < typename ValueTypeCV, typename ValueType = detail::uncvref_t<ValueTypeCV>>
  18226. #if defined(JSON_HAS_CPP_14)
  18227. constexpr
  18228. #endif
  18229. auto get() const noexcept(
  18230. noexcept(std::declval<const basic_json_t&>().template get_impl<ValueType>(detail::priority_tag<4> {})))
  18231. -> decltype(std::declval<const basic_json_t&>().template get_impl<ValueType>(detail::priority_tag<4> {}))
  18232. {
  18233. // we cannot static_assert on ValueTypeCV being non-const, because
  18234. // there is support for get<const basic_json_t>(), which is why we
  18235. // still need the uncvref
  18236. static_assert(!std::is_reference<ValueTypeCV>::value,
  18237. "get() cannot be used with reference types, you might want to use get_ref()");
  18238. return get_impl<ValueType>(detail::priority_tag<4> {});
  18239. }
  18240. /*!
  18241. @brief get a pointer value (explicit)
  18242. Explicit pointer access to the internally stored JSON value. No copies are
  18243. made.
  18244. @warning The pointer becomes invalid if the underlying JSON object
  18245. changes.
  18246. @tparam PointerType pointer type; must be a pointer to @ref array_t, @ref
  18247. object_t, @ref string_t, @ref boolean_t, @ref number_integer_t,
  18248. @ref number_unsigned_t, or @ref number_float_t.
  18249. @return pointer to the internally stored JSON value if the requested
  18250. pointer type @a PointerType fits to the JSON value; `nullptr` otherwise
  18251. @complexity Constant.
  18252. @liveexample{The example below shows how pointers to internal values of a
  18253. JSON value can be requested. Note that no type conversions are made and a
  18254. `nullptr` is returned if the value and the requested pointer type does not
  18255. match.,get__PointerType}
  18256. @sa see @ref get_ptr() for explicit pointer-member access
  18257. @since version 1.0.0
  18258. */
  18259. template<typename PointerType, typename std::enable_if<
  18260. std::is_pointer<PointerType>::value, int>::type = 0>
  18261. auto get() noexcept -> decltype(std::declval<basic_json_t&>().template get_ptr<PointerType>())
  18262. {
  18263. // delegate the call to get_ptr
  18264. return get_ptr<PointerType>();
  18265. }
  18266. /// @brief get a value (explicit)
  18267. /// @sa https://json.nlohmann.me/api/basic_json/get_to/
  18268. template < typename ValueType,
  18269. detail::enable_if_t <
  18270. !detail::is_basic_json<ValueType>::value&&
  18271. detail::has_from_json<basic_json_t, ValueType>::value,
  18272. int > = 0 >
  18273. ValueType & get_to(ValueType& v) const noexcept(noexcept(
  18274. JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), v)))
  18275. {
  18276. JSONSerializer<ValueType>::from_json(*this, v);
  18277. return v;
  18278. }
  18279. // specialization to allow calling get_to with a basic_json value
  18280. // see https://github.com/nlohmann/json/issues/2175
  18281. template<typename ValueType,
  18282. detail::enable_if_t <
  18283. detail::is_basic_json<ValueType>::value,
  18284. int> = 0>
  18285. ValueType & get_to(ValueType& v) const
  18286. {
  18287. v = *this;
  18288. return v;
  18289. }
  18290. template <
  18291. typename T, std::size_t N,
  18292. typename Array = T (&)[N], // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  18293. detail::enable_if_t <
  18294. detail::has_from_json<basic_json_t, Array>::value, int > = 0 >
  18295. Array get_to(T (&v)[N]) const // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  18296. noexcept(noexcept(JSONSerializer<Array>::from_json(
  18297. std::declval<const basic_json_t&>(), v)))
  18298. {
  18299. JSONSerializer<Array>::from_json(*this, v);
  18300. return v;
  18301. }
  18302. /// @brief get a reference value (implicit)
  18303. /// @sa https://json.nlohmann.me/api/basic_json/get_ref/
  18304. template<typename ReferenceType, typename std::enable_if<
  18305. std::is_reference<ReferenceType>::value, int>::type = 0>
  18306. ReferenceType get_ref()
  18307. {
  18308. // delegate call to get_ref_impl
  18309. return get_ref_impl<ReferenceType>(*this);
  18310. }
  18311. /// @brief get a reference value (implicit)
  18312. /// @sa https://json.nlohmann.me/api/basic_json/get_ref/
  18313. template < typename ReferenceType, typename std::enable_if <
  18314. std::is_reference<ReferenceType>::value&&
  18315. std::is_const<typename std::remove_reference<ReferenceType>::type>::value, int >::type = 0 >
  18316. ReferenceType get_ref() const
  18317. {
  18318. // delegate call to get_ref_impl
  18319. return get_ref_impl<ReferenceType>(*this);
  18320. }
  18321. /*!
  18322. @brief get a value (implicit)
  18323. Implicit type conversion between the JSON value and a compatible value.
  18324. The call is realized by calling @ref get() const.
  18325. @tparam ValueType non-pointer type compatible to the JSON value, for
  18326. instance `int` for JSON integer numbers, `bool` for JSON booleans, or
  18327. `std::vector` types for JSON arrays. The character type of @ref string_t
  18328. as well as an initializer list of this type is excluded to avoid
  18329. ambiguities as these types implicitly convert to `std::string`.
  18330. @return copy of the JSON value, converted to type @a ValueType
  18331. @throw type_error.302 in case passed type @a ValueType is incompatible
  18332. to the JSON value type (e.g., the JSON value is of type boolean, but a
  18333. string is requested); see example below
  18334. @complexity Linear in the size of the JSON value.
  18335. @liveexample{The example below shows several conversions from JSON values
  18336. to other types. There a few things to note: (1) Floating-point numbers can
  18337. be converted to integers\, (2) A JSON array can be converted to a standard
  18338. `std::vector<short>`\, (3) A JSON object can be converted to C++
  18339. associative containers such as `std::unordered_map<std::string\,
  18340. json>`.,operator__ValueType}
  18341. @since version 1.0.0
  18342. */
  18343. template < typename ValueType, typename std::enable_if <
  18344. detail::conjunction <
  18345. detail::negation<std::is_pointer<ValueType>>,
  18346. detail::negation<std::is_same<ValueType, std::nullptr_t>>,
  18347. detail::negation<std::is_same<ValueType, detail::json_ref<basic_json>>>,
  18348. detail::negation<std::is_same<ValueType, typename string_t::value_type>>,
  18349. detail::negation<detail::is_basic_json<ValueType>>,
  18350. detail::negation<std::is_same<ValueType, std::initializer_list<typename string_t::value_type>>>,
  18351. #if defined(JSON_HAS_CPP_17) && (defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER <= 1914))
  18352. detail::negation<std::is_same<ValueType, std::string_view>>,
  18353. #endif
  18354. #if defined(JSON_HAS_CPP_17) && JSON_HAS_STATIC_RTTI
  18355. detail::negation<std::is_same<ValueType, std::any>>,
  18356. #endif
  18357. detail::is_detected_lazy<detail::get_template_function, const basic_json_t&, ValueType>
  18358. >::value, int >::type = 0 >
  18359. JSON_EXPLICIT operator ValueType() const
  18360. {
  18361. // delegate the call to get<>() const
  18362. return get<ValueType>();
  18363. }
  18364. /// @brief get a binary value
  18365. /// @sa https://json.nlohmann.me/api/basic_json/get_binary/
  18366. binary_t& get_binary()
  18367. {
  18368. if (!is_binary())
  18369. {
  18370. JSON_THROW(type_error::create(302, detail::concat("type must be binary, but is ", type_name()), this));
  18371. }
  18372. return *get_ptr<binary_t*>();
  18373. }
  18374. /// @brief get a binary value
  18375. /// @sa https://json.nlohmann.me/api/basic_json/get_binary/
  18376. const binary_t& get_binary() const
  18377. {
  18378. if (!is_binary())
  18379. {
  18380. JSON_THROW(type_error::create(302, detail::concat("type must be binary, but is ", type_name()), this));
  18381. }
  18382. return *get_ptr<const binary_t*>();
  18383. }
  18384. /// @}
  18385. ////////////////////
  18386. // element access //
  18387. ////////////////////
  18388. /// @name element access
  18389. /// Access to the JSON value.
  18390. /// @{
  18391. /// @brief access specified array element with bounds checking
  18392. /// @sa https://json.nlohmann.me/api/basic_json/at/
  18393. reference at(size_type idx)
  18394. {
  18395. // at only works for arrays
  18396. if (JSON_HEDLEY_LIKELY(is_array()))
  18397. {
  18398. JSON_TRY
  18399. {
  18400. return set_parent(m_data.m_value.array->at(idx));
  18401. }
  18402. JSON_CATCH (std::out_of_range&)
  18403. {
  18404. // create better exception explanation
  18405. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
  18406. }
  18407. }
  18408. else
  18409. {
  18410. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  18411. }
  18412. }
  18413. /// @brief access specified array element with bounds checking
  18414. /// @sa https://json.nlohmann.me/api/basic_json/at/
  18415. const_reference at(size_type idx) const
  18416. {
  18417. // at only works for arrays
  18418. if (JSON_HEDLEY_LIKELY(is_array()))
  18419. {
  18420. JSON_TRY
  18421. {
  18422. return m_data.m_value.array->at(idx);
  18423. }
  18424. JSON_CATCH (std::out_of_range&)
  18425. {
  18426. // create better exception explanation
  18427. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
  18428. }
  18429. }
  18430. else
  18431. {
  18432. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  18433. }
  18434. }
  18435. /// @brief access specified object element with bounds checking
  18436. /// @sa https://json.nlohmann.me/api/basic_json/at/
  18437. reference at(const typename object_t::key_type& key)
  18438. {
  18439. // at only works for objects
  18440. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  18441. {
  18442. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  18443. }
  18444. auto it = m_data.m_value.object->find(key);
  18445. if (it == m_data.m_value.object->end())
  18446. {
  18447. JSON_THROW(out_of_range::create(403, detail::concat("key '", key, "' not found"), this));
  18448. }
  18449. return set_parent(it->second);
  18450. }
  18451. /// @brief access specified object element with bounds checking
  18452. /// @sa https://json.nlohmann.me/api/basic_json/at/
  18453. template<class KeyType, detail::enable_if_t<
  18454. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  18455. reference at(KeyType && key)
  18456. {
  18457. // at only works for objects
  18458. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  18459. {
  18460. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  18461. }
  18462. auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
  18463. if (it == m_data.m_value.object->end())
  18464. {
  18465. JSON_THROW(out_of_range::create(403, detail::concat("key '", string_t(std::forward<KeyType>(key)), "' not found"), this));
  18466. }
  18467. return set_parent(it->second);
  18468. }
  18469. /// @brief access specified object element with bounds checking
  18470. /// @sa https://json.nlohmann.me/api/basic_json/at/
  18471. const_reference at(const typename object_t::key_type& key) const
  18472. {
  18473. // at only works for objects
  18474. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  18475. {
  18476. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  18477. }
  18478. auto it = m_data.m_value.object->find(key);
  18479. if (it == m_data.m_value.object->end())
  18480. {
  18481. JSON_THROW(out_of_range::create(403, detail::concat("key '", key, "' not found"), this));
  18482. }
  18483. return it->second;
  18484. }
  18485. /// @brief access specified object element with bounds checking
  18486. /// @sa https://json.nlohmann.me/api/basic_json/at/
  18487. template<class KeyType, detail::enable_if_t<
  18488. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  18489. const_reference at(KeyType && key) const
  18490. {
  18491. // at only works for objects
  18492. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  18493. {
  18494. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  18495. }
  18496. auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
  18497. if (it == m_data.m_value.object->end())
  18498. {
  18499. JSON_THROW(out_of_range::create(403, detail::concat("key '", string_t(std::forward<KeyType>(key)), "' not found"), this));
  18500. }
  18501. return it->second;
  18502. }
  18503. /// @brief access specified array element
  18504. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  18505. reference operator[](size_type idx)
  18506. {
  18507. // implicitly convert null value to an empty array
  18508. if (is_null())
  18509. {
  18510. m_data.m_type = value_t::array;
  18511. m_data.m_value.array = create<array_t>();
  18512. assert_invariant();
  18513. }
  18514. // operator[] only works for arrays
  18515. if (JSON_HEDLEY_LIKELY(is_array()))
  18516. {
  18517. // fill up array with null values if given idx is outside range
  18518. if (idx >= m_data.m_value.array->size())
  18519. {
  18520. #if JSON_DIAGNOSTICS
  18521. // remember array size & capacity before resizing
  18522. const auto old_size = m_data.m_value.array->size();
  18523. const auto old_capacity = m_data.m_value.array->capacity();
  18524. #endif
  18525. m_data.m_value.array->resize(idx + 1);
  18526. #if JSON_DIAGNOSTICS
  18527. if (JSON_HEDLEY_UNLIKELY(m_data.m_value.array->capacity() != old_capacity))
  18528. {
  18529. // capacity has changed: update all parents
  18530. set_parents();
  18531. }
  18532. else
  18533. {
  18534. // set parent for values added above
  18535. set_parents(begin() + static_cast<typename iterator::difference_type>(old_size), static_cast<typename iterator::difference_type>(idx + 1 - old_size));
  18536. }
  18537. #endif
  18538. assert_invariant();
  18539. }
  18540. return m_data.m_value.array->operator[](idx);
  18541. }
  18542. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a numeric argument with ", type_name()), this));
  18543. }
  18544. /// @brief access specified array element
  18545. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  18546. const_reference operator[](size_type idx) const
  18547. {
  18548. // const operator[] only works for arrays
  18549. if (JSON_HEDLEY_LIKELY(is_array()))
  18550. {
  18551. return m_data.m_value.array->operator[](idx);
  18552. }
  18553. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a numeric argument with ", type_name()), this));
  18554. }
  18555. /// @brief access specified object element
  18556. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  18557. reference operator[](typename object_t::key_type key)
  18558. {
  18559. // implicitly convert null value to an empty object
  18560. if (is_null())
  18561. {
  18562. m_data.m_type = value_t::object;
  18563. m_data.m_value.object = create<object_t>();
  18564. assert_invariant();
  18565. }
  18566. // operator[] only works for objects
  18567. if (JSON_HEDLEY_LIKELY(is_object()))
  18568. {
  18569. auto result = m_data.m_value.object->emplace(std::move(key), nullptr);
  18570. return set_parent(result.first->second);
  18571. }
  18572. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  18573. }
  18574. /// @brief access specified object element
  18575. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  18576. const_reference operator[](const typename object_t::key_type& key) const
  18577. {
  18578. // const operator[] only works for objects
  18579. if (JSON_HEDLEY_LIKELY(is_object()))
  18580. {
  18581. auto it = m_data.m_value.object->find(key);
  18582. JSON_ASSERT(it != m_data.m_value.object->end());
  18583. return it->second;
  18584. }
  18585. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  18586. }
  18587. // these two functions resolve a (const) char * ambiguity affecting Clang and MSVC
  18588. // (they seemingly cannot be constrained to resolve the ambiguity)
  18589. template<typename T>
  18590. reference operator[](T* key)
  18591. {
  18592. return operator[](typename object_t::key_type(key));
  18593. }
  18594. template<typename T>
  18595. const_reference operator[](T* key) const
  18596. {
  18597. return operator[](typename object_t::key_type(key));
  18598. }
  18599. /// @brief access specified object element
  18600. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  18601. template<class KeyType, detail::enable_if_t<
  18602. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int > = 0 >
  18603. reference operator[](KeyType && key)
  18604. {
  18605. // implicitly convert null value to an empty object
  18606. if (is_null())
  18607. {
  18608. m_data.m_type = value_t::object;
  18609. m_data.m_value.object = create<object_t>();
  18610. assert_invariant();
  18611. }
  18612. // operator[] only works for objects
  18613. if (JSON_HEDLEY_LIKELY(is_object()))
  18614. {
  18615. auto result = m_data.m_value.object->emplace(std::forward<KeyType>(key), nullptr);
  18616. return set_parent(result.first->second);
  18617. }
  18618. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  18619. }
  18620. /// @brief access specified object element
  18621. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  18622. template<class KeyType, detail::enable_if_t<
  18623. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int > = 0 >
  18624. const_reference operator[](KeyType && key) const
  18625. {
  18626. // const operator[] only works for objects
  18627. if (JSON_HEDLEY_LIKELY(is_object()))
  18628. {
  18629. auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
  18630. JSON_ASSERT(it != m_data.m_value.object->end());
  18631. return it->second;
  18632. }
  18633. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  18634. }
  18635. private:
  18636. template<typename KeyType>
  18637. using is_comparable_with_object_key = detail::is_comparable <
  18638. object_comparator_t, const typename object_t::key_type&, KeyType >;
  18639. template<typename ValueType>
  18640. using value_return_type = std::conditional <
  18641. detail::is_c_string_uncvref<ValueType>::value,
  18642. string_t, typename std::decay<ValueType>::type >;
  18643. public:
  18644. /// @brief access specified object element with default value
  18645. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18646. template < class ValueType, detail::enable_if_t <
  18647. !detail::is_transparent<object_comparator_t>::value
  18648. && detail::is_getable<basic_json_t, ValueType>::value
  18649. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18650. ValueType value(const typename object_t::key_type& key, const ValueType& default_value) const
  18651. {
  18652. // value only works for objects
  18653. if (JSON_HEDLEY_LIKELY(is_object()))
  18654. {
  18655. // if key is found, return value and given default value otherwise
  18656. const auto it = find(key);
  18657. if (it != end())
  18658. {
  18659. return it->template get<ValueType>();
  18660. }
  18661. return default_value;
  18662. }
  18663. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  18664. }
  18665. /// @brief access specified object element with default value
  18666. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18667. template < class ValueType, class ReturnType = typename value_return_type<ValueType>::type,
  18668. detail::enable_if_t <
  18669. !detail::is_transparent<object_comparator_t>::value
  18670. && detail::is_getable<basic_json_t, ReturnType>::value
  18671. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18672. ReturnType value(const typename object_t::key_type& key, ValueType && default_value) const
  18673. {
  18674. // value only works for objects
  18675. if (JSON_HEDLEY_LIKELY(is_object()))
  18676. {
  18677. // if key is found, return value and given default value otherwise
  18678. const auto it = find(key);
  18679. if (it != end())
  18680. {
  18681. return it->template get<ReturnType>();
  18682. }
  18683. return std::forward<ValueType>(default_value);
  18684. }
  18685. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  18686. }
  18687. /// @brief access specified object element with default value
  18688. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18689. template < class ValueType, class KeyType, detail::enable_if_t <
  18690. detail::is_transparent<object_comparator_t>::value
  18691. && !detail::is_json_pointer<KeyType>::value
  18692. && is_comparable_with_object_key<KeyType>::value
  18693. && detail::is_getable<basic_json_t, ValueType>::value
  18694. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18695. ValueType value(KeyType && key, const ValueType& default_value) const
  18696. {
  18697. // value only works for objects
  18698. if (JSON_HEDLEY_LIKELY(is_object()))
  18699. {
  18700. // if key is found, return value and given default value otherwise
  18701. const auto it = find(std::forward<KeyType>(key));
  18702. if (it != end())
  18703. {
  18704. return it->template get<ValueType>();
  18705. }
  18706. return default_value;
  18707. }
  18708. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  18709. }
  18710. /// @brief access specified object element via JSON Pointer with default value
  18711. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18712. template < class ValueType, class KeyType, class ReturnType = typename value_return_type<ValueType>::type,
  18713. detail::enable_if_t <
  18714. detail::is_transparent<object_comparator_t>::value
  18715. && !detail::is_json_pointer<KeyType>::value
  18716. && is_comparable_with_object_key<KeyType>::value
  18717. && detail::is_getable<basic_json_t, ReturnType>::value
  18718. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18719. ReturnType value(KeyType && key, ValueType && default_value) const
  18720. {
  18721. // value only works for objects
  18722. if (JSON_HEDLEY_LIKELY(is_object()))
  18723. {
  18724. // if key is found, return value and given default value otherwise
  18725. const auto it = find(std::forward<KeyType>(key));
  18726. if (it != end())
  18727. {
  18728. return it->template get<ReturnType>();
  18729. }
  18730. return std::forward<ValueType>(default_value);
  18731. }
  18732. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  18733. }
  18734. /// @brief access specified object element via JSON Pointer with default value
  18735. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18736. template < class ValueType, detail::enable_if_t <
  18737. detail::is_getable<basic_json_t, ValueType>::value
  18738. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18739. ValueType value(const json_pointer& ptr, const ValueType& default_value) const
  18740. {
  18741. // value only works for objects
  18742. if (JSON_HEDLEY_LIKELY(is_object()))
  18743. {
  18744. // if pointer resolves a value, return it or use default value
  18745. JSON_TRY
  18746. {
  18747. return ptr.get_checked(this).template get<ValueType>();
  18748. }
  18749. JSON_INTERNAL_CATCH (out_of_range&)
  18750. {
  18751. return default_value;
  18752. }
  18753. }
  18754. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  18755. }
  18756. /// @brief access specified object element via JSON Pointer with default value
  18757. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18758. template < class ValueType, class ReturnType = typename value_return_type<ValueType>::type,
  18759. detail::enable_if_t <
  18760. detail::is_getable<basic_json_t, ReturnType>::value
  18761. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18762. ReturnType value(const json_pointer& ptr, ValueType && default_value) const
  18763. {
  18764. // value only works for objects
  18765. if (JSON_HEDLEY_LIKELY(is_object()))
  18766. {
  18767. // if pointer resolves a value, return it or use default value
  18768. JSON_TRY
  18769. {
  18770. return ptr.get_checked(this).template get<ReturnType>();
  18771. }
  18772. JSON_INTERNAL_CATCH (out_of_range&)
  18773. {
  18774. return std::forward<ValueType>(default_value);
  18775. }
  18776. }
  18777. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  18778. }
  18779. template < class ValueType, class BasicJsonType, detail::enable_if_t <
  18780. detail::is_basic_json<BasicJsonType>::value
  18781. && detail::is_getable<basic_json_t, ValueType>::value
  18782. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18783. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  18784. ValueType value(const ::nlohmann::json_pointer<BasicJsonType>& ptr, const ValueType& default_value) const
  18785. {
  18786. return value(ptr.convert(), default_value);
  18787. }
  18788. template < class ValueType, class BasicJsonType, class ReturnType = typename value_return_type<ValueType>::type,
  18789. detail::enable_if_t <
  18790. detail::is_basic_json<BasicJsonType>::value
  18791. && detail::is_getable<basic_json_t, ReturnType>::value
  18792. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18793. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  18794. ReturnType value(const ::nlohmann::json_pointer<BasicJsonType>& ptr, ValueType && default_value) const
  18795. {
  18796. return value(ptr.convert(), std::forward<ValueType>(default_value));
  18797. }
  18798. /// @brief access the first element
  18799. /// @sa https://json.nlohmann.me/api/basic_json/front/
  18800. reference front()
  18801. {
  18802. return *begin();
  18803. }
  18804. /// @brief access the first element
  18805. /// @sa https://json.nlohmann.me/api/basic_json/front/
  18806. const_reference front() const
  18807. {
  18808. return *cbegin();
  18809. }
  18810. /// @brief access the last element
  18811. /// @sa https://json.nlohmann.me/api/basic_json/back/
  18812. reference back()
  18813. {
  18814. auto tmp = end();
  18815. --tmp;
  18816. return *tmp;
  18817. }
  18818. /// @brief access the last element
  18819. /// @sa https://json.nlohmann.me/api/basic_json/back/
  18820. const_reference back() const
  18821. {
  18822. auto tmp = cend();
  18823. --tmp;
  18824. return *tmp;
  18825. }
  18826. /// @brief remove element given an iterator
  18827. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  18828. template < class IteratorType, detail::enable_if_t <
  18829. std::is_same<IteratorType, typename basic_json_t::iterator>::value ||
  18830. std::is_same<IteratorType, typename basic_json_t::const_iterator>::value, int > = 0 >
  18831. IteratorType erase(IteratorType pos)
  18832. {
  18833. // make sure iterator fits the current value
  18834. if (JSON_HEDLEY_UNLIKELY(this != pos.m_object))
  18835. {
  18836. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  18837. }
  18838. IteratorType result = end();
  18839. switch (m_data.m_type)
  18840. {
  18841. case value_t::boolean:
  18842. case value_t::number_float:
  18843. case value_t::number_integer:
  18844. case value_t::number_unsigned:
  18845. case value_t::string:
  18846. case value_t::binary:
  18847. {
  18848. if (JSON_HEDLEY_UNLIKELY(!pos.m_it.primitive_iterator.is_begin()))
  18849. {
  18850. JSON_THROW(invalid_iterator::create(205, "iterator out of range", this));
  18851. }
  18852. if (is_string())
  18853. {
  18854. AllocatorType<string_t> alloc;
  18855. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.string);
  18856. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.string, 1);
  18857. m_data.m_value.string = nullptr;
  18858. }
  18859. else if (is_binary())
  18860. {
  18861. AllocatorType<binary_t> alloc;
  18862. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.binary);
  18863. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.binary, 1);
  18864. m_data.m_value.binary = nullptr;
  18865. }
  18866. m_data.m_type = value_t::null;
  18867. assert_invariant();
  18868. break;
  18869. }
  18870. case value_t::object:
  18871. {
  18872. result.m_it.object_iterator = m_data.m_value.object->erase(pos.m_it.object_iterator);
  18873. break;
  18874. }
  18875. case value_t::array:
  18876. {
  18877. result.m_it.array_iterator = m_data.m_value.array->erase(pos.m_it.array_iterator);
  18878. break;
  18879. }
  18880. case value_t::null:
  18881. case value_t::discarded:
  18882. default:
  18883. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  18884. }
  18885. return result;
  18886. }
  18887. /// @brief remove elements given an iterator range
  18888. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  18889. template < class IteratorType, detail::enable_if_t <
  18890. std::is_same<IteratorType, typename basic_json_t::iterator>::value ||
  18891. std::is_same<IteratorType, typename basic_json_t::const_iterator>::value, int > = 0 >
  18892. IteratorType erase(IteratorType first, IteratorType last)
  18893. {
  18894. // make sure iterator fits the current value
  18895. if (JSON_HEDLEY_UNLIKELY(this != first.m_object || this != last.m_object))
  18896. {
  18897. JSON_THROW(invalid_iterator::create(203, "iterators do not fit current value", this));
  18898. }
  18899. IteratorType result = end();
  18900. switch (m_data.m_type)
  18901. {
  18902. case value_t::boolean:
  18903. case value_t::number_float:
  18904. case value_t::number_integer:
  18905. case value_t::number_unsigned:
  18906. case value_t::string:
  18907. case value_t::binary:
  18908. {
  18909. if (JSON_HEDLEY_LIKELY(!first.m_it.primitive_iterator.is_begin()
  18910. || !last.m_it.primitive_iterator.is_end()))
  18911. {
  18912. JSON_THROW(invalid_iterator::create(204, "iterators out of range", this));
  18913. }
  18914. if (is_string())
  18915. {
  18916. AllocatorType<string_t> alloc;
  18917. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.string);
  18918. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.string, 1);
  18919. m_data.m_value.string = nullptr;
  18920. }
  18921. else if (is_binary())
  18922. {
  18923. AllocatorType<binary_t> alloc;
  18924. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.binary);
  18925. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.binary, 1);
  18926. m_data.m_value.binary = nullptr;
  18927. }
  18928. m_data.m_type = value_t::null;
  18929. assert_invariant();
  18930. break;
  18931. }
  18932. case value_t::object:
  18933. {
  18934. result.m_it.object_iterator = m_data.m_value.object->erase(first.m_it.object_iterator,
  18935. last.m_it.object_iterator);
  18936. break;
  18937. }
  18938. case value_t::array:
  18939. {
  18940. result.m_it.array_iterator = m_data.m_value.array->erase(first.m_it.array_iterator,
  18941. last.m_it.array_iterator);
  18942. break;
  18943. }
  18944. case value_t::null:
  18945. case value_t::discarded:
  18946. default:
  18947. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  18948. }
  18949. return result;
  18950. }
  18951. private:
  18952. template < typename KeyType, detail::enable_if_t <
  18953. detail::has_erase_with_key_type<basic_json_t, KeyType>::value, int > = 0 >
  18954. size_type erase_internal(KeyType && key)
  18955. {
  18956. // this erase only works for objects
  18957. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  18958. {
  18959. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  18960. }
  18961. return m_data.m_value.object->erase(std::forward<KeyType>(key));
  18962. }
  18963. template < typename KeyType, detail::enable_if_t <
  18964. !detail::has_erase_with_key_type<basic_json_t, KeyType>::value, int > = 0 >
  18965. size_type erase_internal(KeyType && key)
  18966. {
  18967. // this erase only works for objects
  18968. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  18969. {
  18970. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  18971. }
  18972. const auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
  18973. if (it != m_data.m_value.object->end())
  18974. {
  18975. m_data.m_value.object->erase(it);
  18976. return 1;
  18977. }
  18978. return 0;
  18979. }
  18980. public:
  18981. /// @brief remove element from a JSON object given a key
  18982. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  18983. size_type erase(const typename object_t::key_type& key)
  18984. {
  18985. // the indirection via erase_internal() is added to avoid making this
  18986. // function a template and thus de-rank it during overload resolution
  18987. return erase_internal(key);
  18988. }
  18989. /// @brief remove element from a JSON object given a key
  18990. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  18991. template<class KeyType, detail::enable_if_t<
  18992. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  18993. size_type erase(KeyType && key)
  18994. {
  18995. return erase_internal(std::forward<KeyType>(key));
  18996. }
  18997. /// @brief remove element from a JSON array given an index
  18998. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  18999. void erase(const size_type idx)
  19000. {
  19001. // this erase only works for arrays
  19002. if (JSON_HEDLEY_LIKELY(is_array()))
  19003. {
  19004. if (JSON_HEDLEY_UNLIKELY(idx >= size()))
  19005. {
  19006. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
  19007. }
  19008. m_data.m_value.array->erase(m_data.m_value.array->begin() + static_cast<difference_type>(idx));
  19009. }
  19010. else
  19011. {
  19012. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  19013. }
  19014. }
  19015. /// @}
  19016. ////////////
  19017. // lookup //
  19018. ////////////
  19019. /// @name lookup
  19020. /// @{
  19021. /// @brief find an element in a JSON object
  19022. /// @sa https://json.nlohmann.me/api/basic_json/find/
  19023. iterator find(const typename object_t::key_type& key)
  19024. {
  19025. auto result = end();
  19026. if (is_object())
  19027. {
  19028. result.m_it.object_iterator = m_data.m_value.object->find(key);
  19029. }
  19030. return result;
  19031. }
  19032. /// @brief find an element in a JSON object
  19033. /// @sa https://json.nlohmann.me/api/basic_json/find/
  19034. const_iterator find(const typename object_t::key_type& key) const
  19035. {
  19036. auto result = cend();
  19037. if (is_object())
  19038. {
  19039. result.m_it.object_iterator = m_data.m_value.object->find(key);
  19040. }
  19041. return result;
  19042. }
  19043. /// @brief find an element in a JSON object
  19044. /// @sa https://json.nlohmann.me/api/basic_json/find/
  19045. template<class KeyType, detail::enable_if_t<
  19046. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  19047. iterator find(KeyType && key)
  19048. {
  19049. auto result = end();
  19050. if (is_object())
  19051. {
  19052. result.m_it.object_iterator = m_data.m_value.object->find(std::forward<KeyType>(key));
  19053. }
  19054. return result;
  19055. }
  19056. /// @brief find an element in a JSON object
  19057. /// @sa https://json.nlohmann.me/api/basic_json/find/
  19058. template<class KeyType, detail::enable_if_t<
  19059. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  19060. const_iterator find(KeyType && key) const
  19061. {
  19062. auto result = cend();
  19063. if (is_object())
  19064. {
  19065. result.m_it.object_iterator = m_data.m_value.object->find(std::forward<KeyType>(key));
  19066. }
  19067. return result;
  19068. }
  19069. /// @brief returns the number of occurrences of a key in a JSON object
  19070. /// @sa https://json.nlohmann.me/api/basic_json/count/
  19071. size_type count(const typename object_t::key_type& key) const
  19072. {
  19073. // return 0 for all nonobject types
  19074. return is_object() ? m_data.m_value.object->count(key) : 0;
  19075. }
  19076. /// @brief returns the number of occurrences of a key in a JSON object
  19077. /// @sa https://json.nlohmann.me/api/basic_json/count/
  19078. template<class KeyType, detail::enable_if_t<
  19079. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  19080. size_type count(KeyType && key) const
  19081. {
  19082. // return 0 for all nonobject types
  19083. return is_object() ? m_data.m_value.object->count(std::forward<KeyType>(key)) : 0;
  19084. }
  19085. /// @brief check the existence of an element in a JSON object
  19086. /// @sa https://json.nlohmann.me/api/basic_json/contains/
  19087. bool contains(const typename object_t::key_type& key) const
  19088. {
  19089. return is_object() && m_data.m_value.object->find(key) != m_data.m_value.object->end();
  19090. }
  19091. /// @brief check the existence of an element in a JSON object
  19092. /// @sa https://json.nlohmann.me/api/basic_json/contains/
  19093. template<class KeyType, detail::enable_if_t<
  19094. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  19095. bool contains(KeyType && key) const
  19096. {
  19097. return is_object() && m_data.m_value.object->find(std::forward<KeyType>(key)) != m_data.m_value.object->end();
  19098. }
  19099. /// @brief check the existence of an element in a JSON object given a JSON pointer
  19100. /// @sa https://json.nlohmann.me/api/basic_json/contains/
  19101. bool contains(const json_pointer& ptr) const
  19102. {
  19103. return ptr.contains(this);
  19104. }
  19105. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  19106. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  19107. bool contains(const typename ::nlohmann::json_pointer<BasicJsonType>& ptr) const
  19108. {
  19109. return ptr.contains(this);
  19110. }
  19111. /// @}
  19112. ///////////////
  19113. // iterators //
  19114. ///////////////
  19115. /// @name iterators
  19116. /// @{
  19117. /// @brief returns an iterator to the first element
  19118. /// @sa https://json.nlohmann.me/api/basic_json/begin/
  19119. iterator begin() noexcept
  19120. {
  19121. iterator result(this);
  19122. result.set_begin();
  19123. return result;
  19124. }
  19125. /// @brief returns an iterator to the first element
  19126. /// @sa https://json.nlohmann.me/api/basic_json/begin/
  19127. const_iterator begin() const noexcept
  19128. {
  19129. return cbegin();
  19130. }
  19131. /// @brief returns a const iterator to the first element
  19132. /// @sa https://json.nlohmann.me/api/basic_json/cbegin/
  19133. const_iterator cbegin() const noexcept
  19134. {
  19135. const_iterator result(this);
  19136. result.set_begin();
  19137. return result;
  19138. }
  19139. /// @brief returns an iterator to one past the last element
  19140. /// @sa https://json.nlohmann.me/api/basic_json/end/
  19141. iterator end() noexcept
  19142. {
  19143. iterator result(this);
  19144. result.set_end();
  19145. return result;
  19146. }
  19147. /// @brief returns an iterator to one past the last element
  19148. /// @sa https://json.nlohmann.me/api/basic_json/end/
  19149. const_iterator end() const noexcept
  19150. {
  19151. return cend();
  19152. }
  19153. /// @brief returns an iterator to one past the last element
  19154. /// @sa https://json.nlohmann.me/api/basic_json/cend/
  19155. const_iterator cend() const noexcept
  19156. {
  19157. const_iterator result(this);
  19158. result.set_end();
  19159. return result;
  19160. }
  19161. /// @brief returns an iterator to the reverse-beginning
  19162. /// @sa https://json.nlohmann.me/api/basic_json/rbegin/
  19163. reverse_iterator rbegin() noexcept
  19164. {
  19165. return reverse_iterator(end());
  19166. }
  19167. /// @brief returns an iterator to the reverse-beginning
  19168. /// @sa https://json.nlohmann.me/api/basic_json/rbegin/
  19169. const_reverse_iterator rbegin() const noexcept
  19170. {
  19171. return crbegin();
  19172. }
  19173. /// @brief returns an iterator to the reverse-end
  19174. /// @sa https://json.nlohmann.me/api/basic_json/rend/
  19175. reverse_iterator rend() noexcept
  19176. {
  19177. return reverse_iterator(begin());
  19178. }
  19179. /// @brief returns an iterator to the reverse-end
  19180. /// @sa https://json.nlohmann.me/api/basic_json/rend/
  19181. const_reverse_iterator rend() const noexcept
  19182. {
  19183. return crend();
  19184. }
  19185. /// @brief returns a const reverse iterator to the last element
  19186. /// @sa https://json.nlohmann.me/api/basic_json/crbegin/
  19187. const_reverse_iterator crbegin() const noexcept
  19188. {
  19189. return const_reverse_iterator(cend());
  19190. }
  19191. /// @brief returns a const reverse iterator to one before the first
  19192. /// @sa https://json.nlohmann.me/api/basic_json/crend/
  19193. const_reverse_iterator crend() const noexcept
  19194. {
  19195. return const_reverse_iterator(cbegin());
  19196. }
  19197. public:
  19198. /// @brief wrapper to access iterator member functions in range-based for
  19199. /// @sa https://json.nlohmann.me/api/basic_json/items/
  19200. /// @deprecated This function is deprecated since 3.1.0 and will be removed in
  19201. /// version 4.0.0 of the library. Please use @ref items() instead;
  19202. /// that is, replace `json::iterator_wrapper(j)` with `j.items()`.
  19203. JSON_HEDLEY_DEPRECATED_FOR(3.1.0, items())
  19204. static iteration_proxy<iterator> iterator_wrapper(reference ref) noexcept
  19205. {
  19206. return ref.items();
  19207. }
  19208. /// @brief wrapper to access iterator member functions in range-based for
  19209. /// @sa https://json.nlohmann.me/api/basic_json/items/
  19210. /// @deprecated This function is deprecated since 3.1.0 and will be removed in
  19211. /// version 4.0.0 of the library. Please use @ref items() instead;
  19212. /// that is, replace `json::iterator_wrapper(j)` with `j.items()`.
  19213. JSON_HEDLEY_DEPRECATED_FOR(3.1.0, items())
  19214. static iteration_proxy<const_iterator> iterator_wrapper(const_reference ref) noexcept
  19215. {
  19216. return ref.items();
  19217. }
  19218. /// @brief helper to access iterator member functions in range-based for
  19219. /// @sa https://json.nlohmann.me/api/basic_json/items/
  19220. iteration_proxy<iterator> items() noexcept
  19221. {
  19222. return iteration_proxy<iterator>(*this);
  19223. }
  19224. /// @brief helper to access iterator member functions in range-based for
  19225. /// @sa https://json.nlohmann.me/api/basic_json/items/
  19226. iteration_proxy<const_iterator> items() const noexcept
  19227. {
  19228. return iteration_proxy<const_iterator>(*this);
  19229. }
  19230. /// @}
  19231. //////////////
  19232. // capacity //
  19233. //////////////
  19234. /// @name capacity
  19235. /// @{
  19236. /// @brief checks whether the container is empty.
  19237. /// @sa https://json.nlohmann.me/api/basic_json/empty/
  19238. bool empty() const noexcept
  19239. {
  19240. switch (m_data.m_type)
  19241. {
  19242. case value_t::null:
  19243. {
  19244. // null values are empty
  19245. return true;
  19246. }
  19247. case value_t::array:
  19248. {
  19249. // delegate call to array_t::empty()
  19250. return m_data.m_value.array->empty();
  19251. }
  19252. case value_t::object:
  19253. {
  19254. // delegate call to object_t::empty()
  19255. return m_data.m_value.object->empty();
  19256. }
  19257. case value_t::string:
  19258. case value_t::boolean:
  19259. case value_t::number_integer:
  19260. case value_t::number_unsigned:
  19261. case value_t::number_float:
  19262. case value_t::binary:
  19263. case value_t::discarded:
  19264. default:
  19265. {
  19266. // all other types are nonempty
  19267. return false;
  19268. }
  19269. }
  19270. }
  19271. /// @brief returns the number of elements
  19272. /// @sa https://json.nlohmann.me/api/basic_json/size/
  19273. size_type size() const noexcept
  19274. {
  19275. switch (m_data.m_type)
  19276. {
  19277. case value_t::null:
  19278. {
  19279. // null values are empty
  19280. return 0;
  19281. }
  19282. case value_t::array:
  19283. {
  19284. // delegate call to array_t::size()
  19285. return m_data.m_value.array->size();
  19286. }
  19287. case value_t::object:
  19288. {
  19289. // delegate call to object_t::size()
  19290. return m_data.m_value.object->size();
  19291. }
  19292. case value_t::string:
  19293. case value_t::boolean:
  19294. case value_t::number_integer:
  19295. case value_t::number_unsigned:
  19296. case value_t::number_float:
  19297. case value_t::binary:
  19298. case value_t::discarded:
  19299. default:
  19300. {
  19301. // all other types have size 1
  19302. return 1;
  19303. }
  19304. }
  19305. }
  19306. /// @brief returns the maximum possible number of elements
  19307. /// @sa https://json.nlohmann.me/api/basic_json/max_size/
  19308. size_type max_size() const noexcept
  19309. {
  19310. switch (m_data.m_type)
  19311. {
  19312. case value_t::array:
  19313. {
  19314. // delegate call to array_t::max_size()
  19315. return m_data.m_value.array->max_size();
  19316. }
  19317. case value_t::object:
  19318. {
  19319. // delegate call to object_t::max_size()
  19320. return m_data.m_value.object->max_size();
  19321. }
  19322. case value_t::null:
  19323. case value_t::string:
  19324. case value_t::boolean:
  19325. case value_t::number_integer:
  19326. case value_t::number_unsigned:
  19327. case value_t::number_float:
  19328. case value_t::binary:
  19329. case value_t::discarded:
  19330. default:
  19331. {
  19332. // all other types have max_size() == size()
  19333. return size();
  19334. }
  19335. }
  19336. }
  19337. /// @}
  19338. ///////////////
  19339. // modifiers //
  19340. ///////////////
  19341. /// @name modifiers
  19342. /// @{
  19343. /// @brief clears the contents
  19344. /// @sa https://json.nlohmann.me/api/basic_json/clear/
  19345. void clear() noexcept
  19346. {
  19347. switch (m_data.m_type)
  19348. {
  19349. case value_t::number_integer:
  19350. {
  19351. m_data.m_value.number_integer = 0;
  19352. break;
  19353. }
  19354. case value_t::number_unsigned:
  19355. {
  19356. m_data.m_value.number_unsigned = 0;
  19357. break;
  19358. }
  19359. case value_t::number_float:
  19360. {
  19361. m_data.m_value.number_float = 0.0;
  19362. break;
  19363. }
  19364. case value_t::boolean:
  19365. {
  19366. m_data.m_value.boolean = false;
  19367. break;
  19368. }
  19369. case value_t::string:
  19370. {
  19371. m_data.m_value.string->clear();
  19372. break;
  19373. }
  19374. case value_t::binary:
  19375. {
  19376. m_data.m_value.binary->clear();
  19377. break;
  19378. }
  19379. case value_t::array:
  19380. {
  19381. m_data.m_value.array->clear();
  19382. break;
  19383. }
  19384. case value_t::object:
  19385. {
  19386. m_data.m_value.object->clear();
  19387. break;
  19388. }
  19389. case value_t::null:
  19390. case value_t::discarded:
  19391. default:
  19392. break;
  19393. }
  19394. }
  19395. /// @brief add an object to an array
  19396. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  19397. void push_back(basic_json&& val)
  19398. {
  19399. // push_back only works for null objects or arrays
  19400. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
  19401. {
  19402. JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
  19403. }
  19404. // transform null object into an array
  19405. if (is_null())
  19406. {
  19407. m_data.m_type = value_t::array;
  19408. m_data.m_value = value_t::array;
  19409. assert_invariant();
  19410. }
  19411. // add element to array (move semantics)
  19412. const auto old_capacity = m_data.m_value.array->capacity();
  19413. m_data.m_value.array->push_back(std::move(val));
  19414. set_parent(m_data.m_value.array->back(), old_capacity);
  19415. // if val is moved from, basic_json move constructor marks it null, so we do not call the destructor
  19416. }
  19417. /// @brief add an object to an array
  19418. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  19419. reference operator+=(basic_json&& val)
  19420. {
  19421. push_back(std::move(val));
  19422. return *this;
  19423. }
  19424. /// @brief add an object to an array
  19425. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  19426. void push_back(const basic_json& val)
  19427. {
  19428. // push_back only works for null objects or arrays
  19429. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
  19430. {
  19431. JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
  19432. }
  19433. // transform null object into an array
  19434. if (is_null())
  19435. {
  19436. m_data.m_type = value_t::array;
  19437. m_data.m_value = value_t::array;
  19438. assert_invariant();
  19439. }
  19440. // add element to array
  19441. const auto old_capacity = m_data.m_value.array->capacity();
  19442. m_data.m_value.array->push_back(val);
  19443. set_parent(m_data.m_value.array->back(), old_capacity);
  19444. }
  19445. /// @brief add an object to an array
  19446. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  19447. reference operator+=(const basic_json& val)
  19448. {
  19449. push_back(val);
  19450. return *this;
  19451. }
  19452. /// @brief add an object to an object
  19453. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  19454. void push_back(const typename object_t::value_type& val)
  19455. {
  19456. // push_back only works for null objects or objects
  19457. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object())))
  19458. {
  19459. JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
  19460. }
  19461. // transform null object into an object
  19462. if (is_null())
  19463. {
  19464. m_data.m_type = value_t::object;
  19465. m_data.m_value = value_t::object;
  19466. assert_invariant();
  19467. }
  19468. // add element to object
  19469. auto res = m_data.m_value.object->insert(val);
  19470. set_parent(res.first->second);
  19471. }
  19472. /// @brief add an object to an object
  19473. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  19474. reference operator+=(const typename object_t::value_type& val)
  19475. {
  19476. push_back(val);
  19477. return *this;
  19478. }
  19479. /// @brief add an object to an object
  19480. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  19481. void push_back(initializer_list_t init)
  19482. {
  19483. if (is_object() && init.size() == 2 && (*init.begin())->is_string())
  19484. {
  19485. basic_json&& key = init.begin()->moved_or_copied();
  19486. push_back(typename object_t::value_type(
  19487. std::move(key.get_ref<string_t&>()), (init.begin() + 1)->moved_or_copied()));
  19488. }
  19489. else
  19490. {
  19491. push_back(basic_json(init));
  19492. }
  19493. }
  19494. /// @brief add an object to an object
  19495. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  19496. reference operator+=(initializer_list_t init)
  19497. {
  19498. push_back(init);
  19499. return *this;
  19500. }
  19501. /// @brief add an object to an array
  19502. /// @sa https://json.nlohmann.me/api/basic_json/emplace_back/
  19503. template<class... Args>
  19504. reference emplace_back(Args&& ... args)
  19505. {
  19506. // emplace_back only works for null objects or arrays
  19507. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
  19508. {
  19509. JSON_THROW(type_error::create(311, detail::concat("cannot use emplace_back() with ", type_name()), this));
  19510. }
  19511. // transform null object into an array
  19512. if (is_null())
  19513. {
  19514. m_data.m_type = value_t::array;
  19515. m_data.m_value = value_t::array;
  19516. assert_invariant();
  19517. }
  19518. // add element to array (perfect forwarding)
  19519. const auto old_capacity = m_data.m_value.array->capacity();
  19520. m_data.m_value.array->emplace_back(std::forward<Args>(args)...);
  19521. return set_parent(m_data.m_value.array->back(), old_capacity);
  19522. }
  19523. /// @brief add an object to an object if key does not exist
  19524. /// @sa https://json.nlohmann.me/api/basic_json/emplace/
  19525. template<class... Args>
  19526. std::pair<iterator, bool> emplace(Args&& ... args)
  19527. {
  19528. // emplace only works for null objects or arrays
  19529. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object())))
  19530. {
  19531. JSON_THROW(type_error::create(311, detail::concat("cannot use emplace() with ", type_name()), this));
  19532. }
  19533. // transform null object into an object
  19534. if (is_null())
  19535. {
  19536. m_data.m_type = value_t::object;
  19537. m_data.m_value = value_t::object;
  19538. assert_invariant();
  19539. }
  19540. // add element to array (perfect forwarding)
  19541. auto res = m_data.m_value.object->emplace(std::forward<Args>(args)...);
  19542. set_parent(res.first->second);
  19543. // create result iterator and set iterator to the result of emplace
  19544. auto it = begin();
  19545. it.m_it.object_iterator = res.first;
  19546. // return pair of iterator and boolean
  19547. return {it, res.second};
  19548. }
  19549. /// Helper for insertion of an iterator
  19550. /// @note: This uses std::distance to support GCC 4.8,
  19551. /// see https://github.com/nlohmann/json/pull/1257
  19552. template<typename... Args>
  19553. iterator insert_iterator(const_iterator pos, Args&& ... args)
  19554. {
  19555. iterator result(this);
  19556. JSON_ASSERT(m_data.m_value.array != nullptr);
  19557. auto insert_pos = std::distance(m_data.m_value.array->begin(), pos.m_it.array_iterator);
  19558. m_data.m_value.array->insert(pos.m_it.array_iterator, std::forward<Args>(args)...);
  19559. result.m_it.array_iterator = m_data.m_value.array->begin() + insert_pos;
  19560. // This could have been written as:
  19561. // result.m_it.array_iterator = m_data.m_value.array->insert(pos.m_it.array_iterator, cnt, val);
  19562. // but the return value of insert is missing in GCC 4.8, so it is written this way instead.
  19563. set_parents();
  19564. return result;
  19565. }
  19566. /// @brief inserts element into array
  19567. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  19568. iterator insert(const_iterator pos, const basic_json& val)
  19569. {
  19570. // insert only works for arrays
  19571. if (JSON_HEDLEY_LIKELY(is_array()))
  19572. {
  19573. // check if iterator pos fits to this JSON value
  19574. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  19575. {
  19576. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  19577. }
  19578. // insert to array and return iterator
  19579. return insert_iterator(pos, val);
  19580. }
  19581. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  19582. }
  19583. /// @brief inserts element into array
  19584. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  19585. iterator insert(const_iterator pos, basic_json&& val)
  19586. {
  19587. return insert(pos, val);
  19588. }
  19589. /// @brief inserts copies of element into array
  19590. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  19591. iterator insert(const_iterator pos, size_type cnt, const basic_json& val)
  19592. {
  19593. // insert only works for arrays
  19594. if (JSON_HEDLEY_LIKELY(is_array()))
  19595. {
  19596. // check if iterator pos fits to this JSON value
  19597. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  19598. {
  19599. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  19600. }
  19601. // insert to array and return iterator
  19602. return insert_iterator(pos, cnt, val);
  19603. }
  19604. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  19605. }
  19606. /// @brief inserts range of elements into array
  19607. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  19608. iterator insert(const_iterator pos, const_iterator first, const_iterator last)
  19609. {
  19610. // insert only works for arrays
  19611. if (JSON_HEDLEY_UNLIKELY(!is_array()))
  19612. {
  19613. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  19614. }
  19615. // check if iterator pos fits to this JSON value
  19616. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  19617. {
  19618. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  19619. }
  19620. // check if range iterators belong to the same JSON object
  19621. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  19622. {
  19623. JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
  19624. }
  19625. if (JSON_HEDLEY_UNLIKELY(first.m_object == this))
  19626. {
  19627. JSON_THROW(invalid_iterator::create(211, "passed iterators may not belong to container", this));
  19628. }
  19629. // insert to array and return iterator
  19630. return insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator);
  19631. }
  19632. /// @brief inserts elements from initializer list into array
  19633. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  19634. iterator insert(const_iterator pos, initializer_list_t ilist)
  19635. {
  19636. // insert only works for arrays
  19637. if (JSON_HEDLEY_UNLIKELY(!is_array()))
  19638. {
  19639. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  19640. }
  19641. // check if iterator pos fits to this JSON value
  19642. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  19643. {
  19644. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  19645. }
  19646. // insert to array and return iterator
  19647. return insert_iterator(pos, ilist.begin(), ilist.end());
  19648. }
  19649. /// @brief inserts range of elements into object
  19650. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  19651. void insert(const_iterator first, const_iterator last)
  19652. {
  19653. // insert only works for objects
  19654. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  19655. {
  19656. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  19657. }
  19658. // check if range iterators belong to the same JSON object
  19659. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  19660. {
  19661. JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
  19662. }
  19663. // passed iterators must belong to objects
  19664. if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()))
  19665. {
  19666. JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", this));
  19667. }
  19668. m_data.m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator);
  19669. }
  19670. /// @brief updates a JSON object from another object, overwriting existing keys
  19671. /// @sa https://json.nlohmann.me/api/basic_json/update/
  19672. void update(const_reference j, bool merge_objects = false)
  19673. {
  19674. update(j.begin(), j.end(), merge_objects);
  19675. }
  19676. /// @brief updates a JSON object from another object, overwriting existing keys
  19677. /// @sa https://json.nlohmann.me/api/basic_json/update/
  19678. void update(const_iterator first, const_iterator last, bool merge_objects = false)
  19679. {
  19680. // implicitly convert null value to an empty object
  19681. if (is_null())
  19682. {
  19683. m_data.m_type = value_t::object;
  19684. m_data.m_value.object = create<object_t>();
  19685. assert_invariant();
  19686. }
  19687. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  19688. {
  19689. JSON_THROW(type_error::create(312, detail::concat("cannot use update() with ", type_name()), this));
  19690. }
  19691. // check if range iterators belong to the same JSON object
  19692. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  19693. {
  19694. JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
  19695. }
  19696. // passed iterators must belong to objects
  19697. if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()))
  19698. {
  19699. JSON_THROW(type_error::create(312, detail::concat("cannot use update() with ", first.m_object->type_name()), first.m_object));
  19700. }
  19701. for (auto it = first; it != last; ++it)
  19702. {
  19703. if (merge_objects && it.value().is_object())
  19704. {
  19705. auto it2 = m_data.m_value.object->find(it.key());
  19706. if (it2 != m_data.m_value.object->end())
  19707. {
  19708. it2->second.update(it.value(), true);
  19709. continue;
  19710. }
  19711. }
  19712. m_data.m_value.object->operator[](it.key()) = it.value();
  19713. #if JSON_DIAGNOSTICS
  19714. m_data.m_value.object->operator[](it.key()).m_parent = this;
  19715. #endif
  19716. }
  19717. }
  19718. /// @brief exchanges the values
  19719. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19720. void swap(reference other) noexcept (
  19721. std::is_nothrow_move_constructible<value_t>::value&&
  19722. std::is_nothrow_move_assignable<value_t>::value&&
  19723. std::is_nothrow_move_constructible<json_value>::value&& // NOLINT(cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  19724. std::is_nothrow_move_assignable<json_value>::value
  19725. )
  19726. {
  19727. std::swap(m_data.m_type, other.m_data.m_type);
  19728. std::swap(m_data.m_value, other.m_data.m_value);
  19729. set_parents();
  19730. other.set_parents();
  19731. assert_invariant();
  19732. }
  19733. /// @brief exchanges the values
  19734. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19735. friend void swap(reference left, reference right) noexcept (
  19736. std::is_nothrow_move_constructible<value_t>::value&&
  19737. std::is_nothrow_move_assignable<value_t>::value&&
  19738. std::is_nothrow_move_constructible<json_value>::value&& // NOLINT(cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  19739. std::is_nothrow_move_assignable<json_value>::value
  19740. )
  19741. {
  19742. left.swap(right);
  19743. }
  19744. /// @brief exchanges the values
  19745. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19746. void swap(array_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  19747. {
  19748. // swap only works for arrays
  19749. if (JSON_HEDLEY_LIKELY(is_array()))
  19750. {
  19751. using std::swap;
  19752. swap(*(m_data.m_value.array), other);
  19753. }
  19754. else
  19755. {
  19756. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(array_t&) with ", type_name()), this));
  19757. }
  19758. }
  19759. /// @brief exchanges the values
  19760. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19761. void swap(object_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  19762. {
  19763. // swap only works for objects
  19764. if (JSON_HEDLEY_LIKELY(is_object()))
  19765. {
  19766. using std::swap;
  19767. swap(*(m_data.m_value.object), other);
  19768. }
  19769. else
  19770. {
  19771. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(object_t&) with ", type_name()), this));
  19772. }
  19773. }
  19774. /// @brief exchanges the values
  19775. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19776. void swap(string_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  19777. {
  19778. // swap only works for strings
  19779. if (JSON_HEDLEY_LIKELY(is_string()))
  19780. {
  19781. using std::swap;
  19782. swap(*(m_data.m_value.string), other);
  19783. }
  19784. else
  19785. {
  19786. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(string_t&) with ", type_name()), this));
  19787. }
  19788. }
  19789. /// @brief exchanges the values
  19790. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19791. void swap(binary_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  19792. {
  19793. // swap only works for strings
  19794. if (JSON_HEDLEY_LIKELY(is_binary()))
  19795. {
  19796. using std::swap;
  19797. swap(*(m_data.m_value.binary), other);
  19798. }
  19799. else
  19800. {
  19801. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(binary_t&) with ", type_name()), this));
  19802. }
  19803. }
  19804. /// @brief exchanges the values
  19805. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19806. void swap(typename binary_t::container_type& other) // NOLINT(bugprone-exception-escape)
  19807. {
  19808. // swap only works for strings
  19809. if (JSON_HEDLEY_LIKELY(is_binary()))
  19810. {
  19811. using std::swap;
  19812. swap(*(m_data.m_value.binary), other);
  19813. }
  19814. else
  19815. {
  19816. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(binary_t::container_type&) with ", type_name()), this));
  19817. }
  19818. }
  19819. /// @}
  19820. //////////////////////////////////////////
  19821. // lexicographical comparison operators //
  19822. //////////////////////////////////////////
  19823. /// @name lexicographical comparison operators
  19824. /// @{
  19825. // note parentheses around operands are necessary; see
  19826. // https://github.com/nlohmann/json/issues/1530
  19827. #define JSON_IMPLEMENT_OPERATOR(op, null_result, unordered_result, default_result) \
  19828. const auto lhs_type = lhs.type(); \
  19829. const auto rhs_type = rhs.type(); \
  19830. \
  19831. if (lhs_type == rhs_type) /* NOLINT(readability/braces) */ \
  19832. { \
  19833. switch (lhs_type) \
  19834. { \
  19835. case value_t::array: \
  19836. return (*lhs.m_data.m_value.array) op (*rhs.m_data.m_value.array); \
  19837. \
  19838. case value_t::object: \
  19839. return (*lhs.m_data.m_value.object) op (*rhs.m_data.m_value.object); \
  19840. \
  19841. case value_t::null: \
  19842. return (null_result); \
  19843. \
  19844. case value_t::string: \
  19845. return (*lhs.m_data.m_value.string) op (*rhs.m_data.m_value.string); \
  19846. \
  19847. case value_t::boolean: \
  19848. return (lhs.m_data.m_value.boolean) op (rhs.m_data.m_value.boolean); \
  19849. \
  19850. case value_t::number_integer: \
  19851. return (lhs.m_data.m_value.number_integer) op (rhs.m_data.m_value.number_integer); \
  19852. \
  19853. case value_t::number_unsigned: \
  19854. return (lhs.m_data.m_value.number_unsigned) op (rhs.m_data.m_value.number_unsigned); \
  19855. \
  19856. case value_t::number_float: \
  19857. return (lhs.m_data.m_value.number_float) op (rhs.m_data.m_value.number_float); \
  19858. \
  19859. case value_t::binary: \
  19860. return (*lhs.m_data.m_value.binary) op (*rhs.m_data.m_value.binary); \
  19861. \
  19862. case value_t::discarded: \
  19863. default: \
  19864. return (unordered_result); \
  19865. } \
  19866. } \
  19867. else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_float) \
  19868. { \
  19869. return static_cast<number_float_t>(lhs.m_data.m_value.number_integer) op rhs.m_data.m_value.number_float; \
  19870. } \
  19871. else if (lhs_type == value_t::number_float && rhs_type == value_t::number_integer) \
  19872. { \
  19873. return lhs.m_data.m_value.number_float op static_cast<number_float_t>(rhs.m_data.m_value.number_integer); \
  19874. } \
  19875. else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_float) \
  19876. { \
  19877. return static_cast<number_float_t>(lhs.m_data.m_value.number_unsigned) op rhs.m_data.m_value.number_float; \
  19878. } \
  19879. else if (lhs_type == value_t::number_float && rhs_type == value_t::number_unsigned) \
  19880. { \
  19881. return lhs.m_data.m_value.number_float op static_cast<number_float_t>(rhs.m_data.m_value.number_unsigned); \
  19882. } \
  19883. else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_integer) \
  19884. { \
  19885. return static_cast<number_integer_t>(lhs.m_data.m_value.number_unsigned) op rhs.m_data.m_value.number_integer; \
  19886. } \
  19887. else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_unsigned) \
  19888. { \
  19889. return lhs.m_data.m_value.number_integer op static_cast<number_integer_t>(rhs.m_data.m_value.number_unsigned); \
  19890. } \
  19891. else if(compares_unordered(lhs, rhs))\
  19892. {\
  19893. return (unordered_result);\
  19894. }\
  19895. \
  19896. return (default_result);
  19897. JSON_PRIVATE_UNLESS_TESTED:
  19898. // returns true if:
  19899. // - any operand is NaN and the other operand is of number type
  19900. // - any operand is discarded
  19901. // in legacy mode, discarded values are considered ordered if
  19902. // an operation is computed as an odd number of inverses of others
  19903. static bool compares_unordered(const_reference lhs, const_reference rhs, bool inverse = false) noexcept
  19904. {
  19905. if ((lhs.is_number_float() && std::isnan(lhs.m_data.m_value.number_float) && rhs.is_number())
  19906. || (rhs.is_number_float() && std::isnan(rhs.m_data.m_value.number_float) && lhs.is_number()))
  19907. {
  19908. return true;
  19909. }
  19910. #if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  19911. return (lhs.is_discarded() || rhs.is_discarded()) && !inverse;
  19912. #else
  19913. static_cast<void>(inverse);
  19914. return lhs.is_discarded() || rhs.is_discarded();
  19915. #endif
  19916. }
  19917. private:
  19918. bool compares_unordered(const_reference rhs, bool inverse = false) const noexcept
  19919. {
  19920. return compares_unordered(*this, rhs, inverse);
  19921. }
  19922. public:
  19923. #if JSON_HAS_THREE_WAY_COMPARISON
  19924. /// @brief comparison: equal
  19925. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  19926. bool operator==(const_reference rhs) const noexcept
  19927. {
  19928. #ifdef __GNUC__
  19929. #pragma GCC diagnostic push
  19930. #pragma GCC diagnostic ignored "-Wfloat-equal"
  19931. #endif
  19932. const_reference lhs = *this;
  19933. JSON_IMPLEMENT_OPERATOR( ==, true, false, false)
  19934. #ifdef __GNUC__
  19935. #pragma GCC diagnostic pop
  19936. #endif
  19937. }
  19938. /// @brief comparison: equal
  19939. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  19940. template<typename ScalarType>
  19941. requires std::is_scalar_v<ScalarType>
  19942. bool operator==(ScalarType rhs) const noexcept
  19943. {
  19944. return *this == basic_json(rhs);
  19945. }
  19946. /// @brief comparison: not equal
  19947. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  19948. bool operator!=(const_reference rhs) const noexcept
  19949. {
  19950. if (compares_unordered(rhs, true))
  19951. {
  19952. return false;
  19953. }
  19954. return !operator==(rhs);
  19955. }
  19956. /// @brief comparison: 3-way
  19957. /// @sa https://json.nlohmann.me/api/basic_json/operator_spaceship/
  19958. std::partial_ordering operator<=>(const_reference rhs) const noexcept // *NOPAD*
  19959. {
  19960. const_reference lhs = *this;
  19961. // default_result is used if we cannot compare values. In that case,
  19962. // we compare types.
  19963. JSON_IMPLEMENT_OPERATOR(<=>, // *NOPAD*
  19964. std::partial_ordering::equivalent,
  19965. std::partial_ordering::unordered,
  19966. lhs_type <=> rhs_type) // *NOPAD*
  19967. }
  19968. /// @brief comparison: 3-way
  19969. /// @sa https://json.nlohmann.me/api/basic_json/operator_spaceship/
  19970. template<typename ScalarType>
  19971. requires std::is_scalar_v<ScalarType>
  19972. std::partial_ordering operator<=>(ScalarType rhs) const noexcept // *NOPAD*
  19973. {
  19974. return *this <=> basic_json(rhs); // *NOPAD*
  19975. }
  19976. #if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  19977. // all operators that are computed as an odd number of inverses of others
  19978. // need to be overloaded to emulate the legacy comparison behavior
  19979. /// @brief comparison: less than or equal
  19980. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  19981. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON)
  19982. bool operator<=(const_reference rhs) const noexcept
  19983. {
  19984. if (compares_unordered(rhs, true))
  19985. {
  19986. return false;
  19987. }
  19988. return !(rhs < *this);
  19989. }
  19990. /// @brief comparison: less than or equal
  19991. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  19992. template<typename ScalarType>
  19993. requires std::is_scalar_v<ScalarType>
  19994. bool operator<=(ScalarType rhs) const noexcept
  19995. {
  19996. return *this <= basic_json(rhs);
  19997. }
  19998. /// @brief comparison: greater than or equal
  19999. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  20000. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON)
  20001. bool operator>=(const_reference rhs) const noexcept
  20002. {
  20003. if (compares_unordered(rhs, true))
  20004. {
  20005. return false;
  20006. }
  20007. return !(*this < rhs);
  20008. }
  20009. /// @brief comparison: greater than or equal
  20010. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  20011. template<typename ScalarType>
  20012. requires std::is_scalar_v<ScalarType>
  20013. bool operator>=(ScalarType rhs) const noexcept
  20014. {
  20015. return *this >= basic_json(rhs);
  20016. }
  20017. #endif
  20018. #else
  20019. /// @brief comparison: equal
  20020. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  20021. friend bool operator==(const_reference lhs, const_reference rhs) noexcept
  20022. {
  20023. #ifdef __GNUC__
  20024. #pragma GCC diagnostic push
  20025. #pragma GCC diagnostic ignored "-Wfloat-equal"
  20026. #endif
  20027. JSON_IMPLEMENT_OPERATOR( ==, true, false, false)
  20028. #ifdef __GNUC__
  20029. #pragma GCC diagnostic pop
  20030. #endif
  20031. }
  20032. /// @brief comparison: equal
  20033. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  20034. template<typename ScalarType, typename std::enable_if<
  20035. std::is_scalar<ScalarType>::value, int>::type = 0>
  20036. friend bool operator==(const_reference lhs, ScalarType rhs) noexcept
  20037. {
  20038. return lhs == basic_json(rhs);
  20039. }
  20040. /// @brief comparison: equal
  20041. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  20042. template<typename ScalarType, typename std::enable_if<
  20043. std::is_scalar<ScalarType>::value, int>::type = 0>
  20044. friend bool operator==(ScalarType lhs, const_reference rhs) noexcept
  20045. {
  20046. return basic_json(lhs) == rhs;
  20047. }
  20048. /// @brief comparison: not equal
  20049. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  20050. friend bool operator!=(const_reference lhs, const_reference rhs) noexcept
  20051. {
  20052. if (compares_unordered(lhs, rhs, true))
  20053. {
  20054. return false;
  20055. }
  20056. return !(lhs == rhs);
  20057. }
  20058. /// @brief comparison: not equal
  20059. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  20060. template<typename ScalarType, typename std::enable_if<
  20061. std::is_scalar<ScalarType>::value, int>::type = 0>
  20062. friend bool operator!=(const_reference lhs, ScalarType rhs) noexcept
  20063. {
  20064. return lhs != basic_json(rhs);
  20065. }
  20066. /// @brief comparison: not equal
  20067. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  20068. template<typename ScalarType, typename std::enable_if<
  20069. std::is_scalar<ScalarType>::value, int>::type = 0>
  20070. friend bool operator!=(ScalarType lhs, const_reference rhs) noexcept
  20071. {
  20072. return basic_json(lhs) != rhs;
  20073. }
  20074. /// @brief comparison: less than
  20075. /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
  20076. friend bool operator<(const_reference lhs, const_reference rhs) noexcept
  20077. {
  20078. // default_result is used if we cannot compare values. In that case,
  20079. // we compare types. Note we have to call the operator explicitly,
  20080. // because MSVC has problems otherwise.
  20081. JSON_IMPLEMENT_OPERATOR( <, false, false, operator<(lhs_type, rhs_type))
  20082. }
  20083. /// @brief comparison: less than
  20084. /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
  20085. template<typename ScalarType, typename std::enable_if<
  20086. std::is_scalar<ScalarType>::value, int>::type = 0>
  20087. friend bool operator<(const_reference lhs, ScalarType rhs) noexcept
  20088. {
  20089. return lhs < basic_json(rhs);
  20090. }
  20091. /// @brief comparison: less than
  20092. /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
  20093. template<typename ScalarType, typename std::enable_if<
  20094. std::is_scalar<ScalarType>::value, int>::type = 0>
  20095. friend bool operator<(ScalarType lhs, const_reference rhs) noexcept
  20096. {
  20097. return basic_json(lhs) < rhs;
  20098. }
  20099. /// @brief comparison: less than or equal
  20100. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  20101. friend bool operator<=(const_reference lhs, const_reference rhs) noexcept
  20102. {
  20103. if (compares_unordered(lhs, rhs, true))
  20104. {
  20105. return false;
  20106. }
  20107. return !(rhs < lhs);
  20108. }
  20109. /// @brief comparison: less than or equal
  20110. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  20111. template<typename ScalarType, typename std::enable_if<
  20112. std::is_scalar<ScalarType>::value, int>::type = 0>
  20113. friend bool operator<=(const_reference lhs, ScalarType rhs) noexcept
  20114. {
  20115. return lhs <= basic_json(rhs);
  20116. }
  20117. /// @brief comparison: less than or equal
  20118. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  20119. template<typename ScalarType, typename std::enable_if<
  20120. std::is_scalar<ScalarType>::value, int>::type = 0>
  20121. friend bool operator<=(ScalarType lhs, const_reference rhs) noexcept
  20122. {
  20123. return basic_json(lhs) <= rhs;
  20124. }
  20125. /// @brief comparison: greater than
  20126. /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
  20127. friend bool operator>(const_reference lhs, const_reference rhs) noexcept
  20128. {
  20129. // double inverse
  20130. if (compares_unordered(lhs, rhs))
  20131. {
  20132. return false;
  20133. }
  20134. return !(lhs <= rhs);
  20135. }
  20136. /// @brief comparison: greater than
  20137. /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
  20138. template<typename ScalarType, typename std::enable_if<
  20139. std::is_scalar<ScalarType>::value, int>::type = 0>
  20140. friend bool operator>(const_reference lhs, ScalarType rhs) noexcept
  20141. {
  20142. return lhs > basic_json(rhs);
  20143. }
  20144. /// @brief comparison: greater than
  20145. /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
  20146. template<typename ScalarType, typename std::enable_if<
  20147. std::is_scalar<ScalarType>::value, int>::type = 0>
  20148. friend bool operator>(ScalarType lhs, const_reference rhs) noexcept
  20149. {
  20150. return basic_json(lhs) > rhs;
  20151. }
  20152. /// @brief comparison: greater than or equal
  20153. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  20154. friend bool operator>=(const_reference lhs, const_reference rhs) noexcept
  20155. {
  20156. if (compares_unordered(lhs, rhs, true))
  20157. {
  20158. return false;
  20159. }
  20160. return !(lhs < rhs);
  20161. }
  20162. /// @brief comparison: greater than or equal
  20163. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  20164. template<typename ScalarType, typename std::enable_if<
  20165. std::is_scalar<ScalarType>::value, int>::type = 0>
  20166. friend bool operator>=(const_reference lhs, ScalarType rhs) noexcept
  20167. {
  20168. return lhs >= basic_json(rhs);
  20169. }
  20170. /// @brief comparison: greater than or equal
  20171. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  20172. template<typename ScalarType, typename std::enable_if<
  20173. std::is_scalar<ScalarType>::value, int>::type = 0>
  20174. friend bool operator>=(ScalarType lhs, const_reference rhs) noexcept
  20175. {
  20176. return basic_json(lhs) >= rhs;
  20177. }
  20178. #endif
  20179. #undef JSON_IMPLEMENT_OPERATOR
  20180. /// @}
  20181. ///////////////////
  20182. // serialization //
  20183. ///////////////////
  20184. /// @name serialization
  20185. /// @{
  20186. #ifndef JSON_NO_IO
  20187. /// @brief serialize to stream
  20188. /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
  20189. friend std::ostream& operator<<(std::ostream& o, const basic_json& j)
  20190. {
  20191. // read width member and use it as indentation parameter if nonzero
  20192. const bool pretty_print = o.width() > 0;
  20193. const auto indentation = pretty_print ? o.width() : 0;
  20194. // reset width to 0 for subsequent calls to this stream
  20195. o.width(0);
  20196. // do the actual serialization
  20197. serializer s(detail::output_adapter<char>(o), o.fill());
  20198. s.dump(j, pretty_print, false, static_cast<unsigned int>(indentation));
  20199. return o;
  20200. }
  20201. /// @brief serialize to stream
  20202. /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
  20203. /// @deprecated This function is deprecated since 3.0.0 and will be removed in
  20204. /// version 4.0.0 of the library. Please use
  20205. /// operator<<(std::ostream&, const basic_json&) instead; that is,
  20206. /// replace calls like `j >> o;` with `o << j;`.
  20207. JSON_HEDLEY_DEPRECATED_FOR(3.0.0, operator<<(std::ostream&, const basic_json&))
  20208. friend std::ostream& operator>>(const basic_json& j, std::ostream& o)
  20209. {
  20210. return o << j;
  20211. }
  20212. #endif // JSON_NO_IO
  20213. /// @}
  20214. /////////////////////
  20215. // deserialization //
  20216. /////////////////////
  20217. /// @name deserialization
  20218. /// @{
  20219. /// @brief deserialize from a compatible input
  20220. /// @sa https://json.nlohmann.me/api/basic_json/parse/
  20221. template<typename InputType>
  20222. JSON_HEDLEY_WARN_UNUSED_RESULT
  20223. static basic_json parse(InputType&& i,
  20224. const parser_callback_t cb = nullptr,
  20225. const bool allow_exceptions = true,
  20226. const bool ignore_comments = false)
  20227. {
  20228. basic_json result;
  20229. parser(detail::input_adapter(std::forward<InputType>(i)), cb, allow_exceptions, ignore_comments).parse(true, result);
  20230. return result;
  20231. }
  20232. /// @brief deserialize from a pair of character iterators
  20233. /// @sa https://json.nlohmann.me/api/basic_json/parse/
  20234. template<typename IteratorType>
  20235. JSON_HEDLEY_WARN_UNUSED_RESULT
  20236. static basic_json parse(IteratorType first,
  20237. IteratorType last,
  20238. const parser_callback_t cb = nullptr,
  20239. const bool allow_exceptions = true,
  20240. const bool ignore_comments = false)
  20241. {
  20242. basic_json result;
  20243. parser(detail::input_adapter(std::move(first), std::move(last)), cb, allow_exceptions, ignore_comments).parse(true, result);
  20244. return result;
  20245. }
  20246. JSON_HEDLEY_WARN_UNUSED_RESULT
  20247. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, parse(ptr, ptr + len))
  20248. static basic_json parse(detail::span_input_adapter&& i,
  20249. const parser_callback_t cb = nullptr,
  20250. const bool allow_exceptions = true,
  20251. const bool ignore_comments = false)
  20252. {
  20253. basic_json result;
  20254. parser(i.get(), cb, allow_exceptions, ignore_comments).parse(true, result);
  20255. return result;
  20256. }
  20257. /// @brief check if the input is valid JSON
  20258. /// @sa https://json.nlohmann.me/api/basic_json/accept/
  20259. template<typename InputType>
  20260. static bool accept(InputType&& i,
  20261. const bool ignore_comments = false)
  20262. {
  20263. return parser(detail::input_adapter(std::forward<InputType>(i)), nullptr, false, ignore_comments).accept(true);
  20264. }
  20265. /// @brief check if the input is valid JSON
  20266. /// @sa https://json.nlohmann.me/api/basic_json/accept/
  20267. template<typename IteratorType>
  20268. static bool accept(IteratorType first, IteratorType last,
  20269. const bool ignore_comments = false)
  20270. {
  20271. return parser(detail::input_adapter(std::move(first), std::move(last)), nullptr, false, ignore_comments).accept(true);
  20272. }
  20273. JSON_HEDLEY_WARN_UNUSED_RESULT
  20274. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, accept(ptr, ptr + len))
  20275. static bool accept(detail::span_input_adapter&& i,
  20276. const bool ignore_comments = false)
  20277. {
  20278. return parser(i.get(), nullptr, false, ignore_comments).accept(true);
  20279. }
  20280. /// @brief generate SAX events
  20281. /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
  20282. template <typename InputType, typename SAX>
  20283. JSON_HEDLEY_NON_NULL(2)
  20284. static bool sax_parse(InputType&& i, SAX* sax,
  20285. input_format_t format = input_format_t::json,
  20286. const bool strict = true,
  20287. const bool ignore_comments = false)
  20288. {
  20289. auto ia = detail::input_adapter(std::forward<InputType>(i));
  20290. return format == input_format_t::json
  20291. ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
  20292. : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
  20293. }
  20294. /// @brief generate SAX events
  20295. /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
  20296. template<class IteratorType, class SAX>
  20297. JSON_HEDLEY_NON_NULL(3)
  20298. static bool sax_parse(IteratorType first, IteratorType last, SAX* sax,
  20299. input_format_t format = input_format_t::json,
  20300. const bool strict = true,
  20301. const bool ignore_comments = false)
  20302. {
  20303. auto ia = detail::input_adapter(std::move(first), std::move(last));
  20304. return format == input_format_t::json
  20305. ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
  20306. : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
  20307. }
  20308. /// @brief generate SAX events
  20309. /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
  20310. /// @deprecated This function is deprecated since 3.8.0 and will be removed in
  20311. /// version 4.0.0 of the library. Please use
  20312. /// sax_parse(ptr, ptr + len) instead.
  20313. template <typename SAX>
  20314. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, sax_parse(ptr, ptr + len, ...))
  20315. JSON_HEDLEY_NON_NULL(2)
  20316. static bool sax_parse(detail::span_input_adapter&& i, SAX* sax,
  20317. input_format_t format = input_format_t::json,
  20318. const bool strict = true,
  20319. const bool ignore_comments = false)
  20320. {
  20321. auto ia = i.get();
  20322. return format == input_format_t::json
  20323. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  20324. ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
  20325. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  20326. : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
  20327. }
  20328. #ifndef JSON_NO_IO
  20329. /// @brief deserialize from stream
  20330. /// @sa https://json.nlohmann.me/api/basic_json/operator_gtgt/
  20331. /// @deprecated This stream operator is deprecated since 3.0.0 and will be removed in
  20332. /// version 4.0.0 of the library. Please use
  20333. /// operator>>(std::istream&, basic_json&) instead; that is,
  20334. /// replace calls like `j << i;` with `i >> j;`.
  20335. JSON_HEDLEY_DEPRECATED_FOR(3.0.0, operator>>(std::istream&, basic_json&))
  20336. friend std::istream& operator<<(basic_json& j, std::istream& i)
  20337. {
  20338. return operator>>(i, j);
  20339. }
  20340. /// @brief deserialize from stream
  20341. /// @sa https://json.nlohmann.me/api/basic_json/operator_gtgt/
  20342. friend std::istream& operator>>(std::istream& i, basic_json& j)
  20343. {
  20344. parser(detail::input_adapter(i)).parse(false, j);
  20345. return i;
  20346. }
  20347. #endif // JSON_NO_IO
  20348. /// @}
  20349. ///////////////////////////
  20350. // convenience functions //
  20351. ///////////////////////////
  20352. /// @brief return the type as string
  20353. /// @sa https://json.nlohmann.me/api/basic_json/type_name/
  20354. JSON_HEDLEY_RETURNS_NON_NULL
  20355. const char* type_name() const noexcept
  20356. {
  20357. switch (m_data.m_type)
  20358. {
  20359. case value_t::null:
  20360. return "null";
  20361. case value_t::object:
  20362. return "object";
  20363. case value_t::array:
  20364. return "array";
  20365. case value_t::string:
  20366. return "string";
  20367. case value_t::boolean:
  20368. return "boolean";
  20369. case value_t::binary:
  20370. return "binary";
  20371. case value_t::discarded:
  20372. return "discarded";
  20373. case value_t::number_integer:
  20374. case value_t::number_unsigned:
  20375. case value_t::number_float:
  20376. default:
  20377. return "number";
  20378. }
  20379. }
  20380. JSON_PRIVATE_UNLESS_TESTED:
  20381. //////////////////////
  20382. // member variables //
  20383. //////////////////////
  20384. struct data
  20385. {
  20386. /// the type of the current element
  20387. value_t m_type = value_t::null;
  20388. /// the value of the current element
  20389. json_value m_value = {};
  20390. data(const value_t v)
  20391. : m_type(v), m_value(v)
  20392. {
  20393. }
  20394. data(size_type cnt, const basic_json& val)
  20395. : m_type(value_t::array)
  20396. {
  20397. m_value.array = create<array_t>(cnt, val);
  20398. }
  20399. data() noexcept = default;
  20400. data(data&&) noexcept = default;
  20401. data(const data&) noexcept = delete;
  20402. data& operator=(data&&) noexcept = delete;
  20403. data& operator=(const data&) noexcept = delete;
  20404. ~data() noexcept
  20405. {
  20406. m_value.destroy(m_type);
  20407. }
  20408. };
  20409. data m_data = {};
  20410. #if JSON_DIAGNOSTICS
  20411. /// a pointer to a parent value (for debugging purposes)
  20412. basic_json* m_parent = nullptr;
  20413. #endif
  20414. //////////////////////////////////////////
  20415. // binary serialization/deserialization //
  20416. //////////////////////////////////////////
  20417. /// @name binary serialization/deserialization support
  20418. /// @{
  20419. public:
  20420. /// @brief create a CBOR serialization of a given JSON value
  20421. /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
  20422. static std::vector<std::uint8_t> to_cbor(const basic_json& j)
  20423. {
  20424. std::vector<std::uint8_t> result;
  20425. to_cbor(j, result);
  20426. return result;
  20427. }
  20428. /// @brief create a CBOR serialization of a given JSON value
  20429. /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
  20430. static void to_cbor(const basic_json& j, detail::output_adapter<std::uint8_t> o)
  20431. {
  20432. binary_writer<std::uint8_t>(o).write_cbor(j);
  20433. }
  20434. /// @brief create a CBOR serialization of a given JSON value
  20435. /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
  20436. static void to_cbor(const basic_json& j, detail::output_adapter<char> o)
  20437. {
  20438. binary_writer<char>(o).write_cbor(j);
  20439. }
  20440. /// @brief create a MessagePack serialization of a given JSON value
  20441. /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
  20442. static std::vector<std::uint8_t> to_msgpack(const basic_json& j)
  20443. {
  20444. std::vector<std::uint8_t> result;
  20445. to_msgpack(j, result);
  20446. return result;
  20447. }
  20448. /// @brief create a MessagePack serialization of a given JSON value
  20449. /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
  20450. static void to_msgpack(const basic_json& j, detail::output_adapter<std::uint8_t> o)
  20451. {
  20452. binary_writer<std::uint8_t>(o).write_msgpack(j);
  20453. }
  20454. /// @brief create a MessagePack serialization of a given JSON value
  20455. /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
  20456. static void to_msgpack(const basic_json& j, detail::output_adapter<char> o)
  20457. {
  20458. binary_writer<char>(o).write_msgpack(j);
  20459. }
  20460. /// @brief create a UBJSON serialization of a given JSON value
  20461. /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
  20462. static std::vector<std::uint8_t> to_ubjson(const basic_json& j,
  20463. const bool use_size = false,
  20464. const bool use_type = false)
  20465. {
  20466. std::vector<std::uint8_t> result;
  20467. to_ubjson(j, result, use_size, use_type);
  20468. return result;
  20469. }
  20470. /// @brief create a UBJSON serialization of a given JSON value
  20471. /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
  20472. static void to_ubjson(const basic_json& j, detail::output_adapter<std::uint8_t> o,
  20473. const bool use_size = false, const bool use_type = false)
  20474. {
  20475. binary_writer<std::uint8_t>(o).write_ubjson(j, use_size, use_type);
  20476. }
  20477. /// @brief create a UBJSON serialization of a given JSON value
  20478. /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
  20479. static void to_ubjson(const basic_json& j, detail::output_adapter<char> o,
  20480. const bool use_size = false, const bool use_type = false)
  20481. {
  20482. binary_writer<char>(o).write_ubjson(j, use_size, use_type);
  20483. }
  20484. /// @brief create a BJData serialization of a given JSON value
  20485. /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
  20486. static std::vector<std::uint8_t> to_bjdata(const basic_json& j,
  20487. const bool use_size = false,
  20488. const bool use_type = false)
  20489. {
  20490. std::vector<std::uint8_t> result;
  20491. to_bjdata(j, result, use_size, use_type);
  20492. return result;
  20493. }
  20494. /// @brief create a BJData serialization of a given JSON value
  20495. /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
  20496. static void to_bjdata(const basic_json& j, detail::output_adapter<std::uint8_t> o,
  20497. const bool use_size = false, const bool use_type = false)
  20498. {
  20499. binary_writer<std::uint8_t>(o).write_ubjson(j, use_size, use_type, true, true);
  20500. }
  20501. /// @brief create a BJData serialization of a given JSON value
  20502. /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
  20503. static void to_bjdata(const basic_json& j, detail::output_adapter<char> o,
  20504. const bool use_size = false, const bool use_type = false)
  20505. {
  20506. binary_writer<char>(o).write_ubjson(j, use_size, use_type, true, true);
  20507. }
  20508. /// @brief create a BSON serialization of a given JSON value
  20509. /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
  20510. static std::vector<std::uint8_t> to_bson(const basic_json& j)
  20511. {
  20512. std::vector<std::uint8_t> result;
  20513. to_bson(j, result);
  20514. return result;
  20515. }
  20516. /// @brief create a BSON serialization of a given JSON value
  20517. /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
  20518. static void to_bson(const basic_json& j, detail::output_adapter<std::uint8_t> o)
  20519. {
  20520. binary_writer<std::uint8_t>(o).write_bson(j);
  20521. }
  20522. /// @brief create a BSON serialization of a given JSON value
  20523. /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
  20524. static void to_bson(const basic_json& j, detail::output_adapter<char> o)
  20525. {
  20526. binary_writer<char>(o).write_bson(j);
  20527. }
  20528. /// @brief create a JSON value from an input in CBOR format
  20529. /// @sa https://json.nlohmann.me/api/basic_json/from_cbor/
  20530. template<typename InputType>
  20531. JSON_HEDLEY_WARN_UNUSED_RESULT
  20532. static basic_json from_cbor(InputType&& i,
  20533. const bool strict = true,
  20534. const bool allow_exceptions = true,
  20535. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  20536. {
  20537. basic_json result;
  20538. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20539. auto ia = detail::input_adapter(std::forward<InputType>(i));
  20540. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler);
  20541. return res ? result : basic_json(value_t::discarded);
  20542. }
  20543. /// @brief create a JSON value from an input in CBOR format
  20544. /// @sa https://json.nlohmann.me/api/basic_json/from_cbor/
  20545. template<typename IteratorType>
  20546. JSON_HEDLEY_WARN_UNUSED_RESULT
  20547. static basic_json from_cbor(IteratorType first, IteratorType last,
  20548. const bool strict = true,
  20549. const bool allow_exceptions = true,
  20550. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  20551. {
  20552. basic_json result;
  20553. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20554. auto ia = detail::input_adapter(std::move(first), std::move(last));
  20555. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler);
  20556. return res ? result : basic_json(value_t::discarded);
  20557. }
  20558. template<typename T>
  20559. JSON_HEDLEY_WARN_UNUSED_RESULT
  20560. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_cbor(ptr, ptr + len))
  20561. static basic_json from_cbor(const T* ptr, std::size_t len,
  20562. const bool strict = true,
  20563. const bool allow_exceptions = true,
  20564. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  20565. {
  20566. return from_cbor(ptr, ptr + len, strict, allow_exceptions, tag_handler);
  20567. }
  20568. JSON_HEDLEY_WARN_UNUSED_RESULT
  20569. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_cbor(ptr, ptr + len))
  20570. static basic_json from_cbor(detail::span_input_adapter&& i,
  20571. const bool strict = true,
  20572. const bool allow_exceptions = true,
  20573. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  20574. {
  20575. basic_json result;
  20576. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20577. auto ia = i.get();
  20578. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  20579. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler);
  20580. return res ? result : basic_json(value_t::discarded);
  20581. }
  20582. /// @brief create a JSON value from an input in MessagePack format
  20583. /// @sa https://json.nlohmann.me/api/basic_json/from_msgpack/
  20584. template<typename InputType>
  20585. JSON_HEDLEY_WARN_UNUSED_RESULT
  20586. static basic_json from_msgpack(InputType&& i,
  20587. const bool strict = true,
  20588. const bool allow_exceptions = true)
  20589. {
  20590. basic_json result;
  20591. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20592. auto ia = detail::input_adapter(std::forward<InputType>(i));
  20593. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict);
  20594. return res ? result : basic_json(value_t::discarded);
  20595. }
  20596. /// @brief create a JSON value from an input in MessagePack format
  20597. /// @sa https://json.nlohmann.me/api/basic_json/from_msgpack/
  20598. template<typename IteratorType>
  20599. JSON_HEDLEY_WARN_UNUSED_RESULT
  20600. static basic_json from_msgpack(IteratorType first, IteratorType last,
  20601. const bool strict = true,
  20602. const bool allow_exceptions = true)
  20603. {
  20604. basic_json result;
  20605. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20606. auto ia = detail::input_adapter(std::move(first), std::move(last));
  20607. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict);
  20608. return res ? result : basic_json(value_t::discarded);
  20609. }
  20610. template<typename T>
  20611. JSON_HEDLEY_WARN_UNUSED_RESULT
  20612. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_msgpack(ptr, ptr + len))
  20613. static basic_json from_msgpack(const T* ptr, std::size_t len,
  20614. const bool strict = true,
  20615. const bool allow_exceptions = true)
  20616. {
  20617. return from_msgpack(ptr, ptr + len, strict, allow_exceptions);
  20618. }
  20619. JSON_HEDLEY_WARN_UNUSED_RESULT
  20620. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_msgpack(ptr, ptr + len))
  20621. static basic_json from_msgpack(detail::span_input_adapter&& i,
  20622. const bool strict = true,
  20623. const bool allow_exceptions = true)
  20624. {
  20625. basic_json result;
  20626. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20627. auto ia = i.get();
  20628. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  20629. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict);
  20630. return res ? result : basic_json(value_t::discarded);
  20631. }
  20632. /// @brief create a JSON value from an input in UBJSON format
  20633. /// @sa https://json.nlohmann.me/api/basic_json/from_ubjson/
  20634. template<typename InputType>
  20635. JSON_HEDLEY_WARN_UNUSED_RESULT
  20636. static basic_json from_ubjson(InputType&& i,
  20637. const bool strict = true,
  20638. const bool allow_exceptions = true)
  20639. {
  20640. basic_json result;
  20641. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20642. auto ia = detail::input_adapter(std::forward<InputType>(i));
  20643. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict);
  20644. return res ? result : basic_json(value_t::discarded);
  20645. }
  20646. /// @brief create a JSON value from an input in UBJSON format
  20647. /// @sa https://json.nlohmann.me/api/basic_json/from_ubjson/
  20648. template<typename IteratorType>
  20649. JSON_HEDLEY_WARN_UNUSED_RESULT
  20650. static basic_json from_ubjson(IteratorType first, IteratorType last,
  20651. const bool strict = true,
  20652. const bool allow_exceptions = true)
  20653. {
  20654. basic_json result;
  20655. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20656. auto ia = detail::input_adapter(std::move(first), std::move(last));
  20657. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict);
  20658. return res ? result : basic_json(value_t::discarded);
  20659. }
  20660. template<typename T>
  20661. JSON_HEDLEY_WARN_UNUSED_RESULT
  20662. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_ubjson(ptr, ptr + len))
  20663. static basic_json from_ubjson(const T* ptr, std::size_t len,
  20664. const bool strict = true,
  20665. const bool allow_exceptions = true)
  20666. {
  20667. return from_ubjson(ptr, ptr + len, strict, allow_exceptions);
  20668. }
  20669. JSON_HEDLEY_WARN_UNUSED_RESULT
  20670. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_ubjson(ptr, ptr + len))
  20671. static basic_json from_ubjson(detail::span_input_adapter&& i,
  20672. const bool strict = true,
  20673. const bool allow_exceptions = true)
  20674. {
  20675. basic_json result;
  20676. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20677. auto ia = i.get();
  20678. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  20679. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict);
  20680. return res ? result : basic_json(value_t::discarded);
  20681. }
  20682. /// @brief create a JSON value from an input in BJData format
  20683. /// @sa https://json.nlohmann.me/api/basic_json/from_bjdata/
  20684. template<typename InputType>
  20685. JSON_HEDLEY_WARN_UNUSED_RESULT
  20686. static basic_json from_bjdata(InputType&& i,
  20687. const bool strict = true,
  20688. const bool allow_exceptions = true)
  20689. {
  20690. basic_json result;
  20691. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20692. auto ia = detail::input_adapter(std::forward<InputType>(i));
  20693. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict);
  20694. return res ? result : basic_json(value_t::discarded);
  20695. }
  20696. /// @brief create a JSON value from an input in BJData format
  20697. /// @sa https://json.nlohmann.me/api/basic_json/from_bjdata/
  20698. template<typename IteratorType>
  20699. JSON_HEDLEY_WARN_UNUSED_RESULT
  20700. static basic_json from_bjdata(IteratorType first, IteratorType last,
  20701. const bool strict = true,
  20702. const bool allow_exceptions = true)
  20703. {
  20704. basic_json result;
  20705. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20706. auto ia = detail::input_adapter(std::move(first), std::move(last));
  20707. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict);
  20708. return res ? result : basic_json(value_t::discarded);
  20709. }
  20710. /// @brief create a JSON value from an input in BSON format
  20711. /// @sa https://json.nlohmann.me/api/basic_json/from_bson/
  20712. template<typename InputType>
  20713. JSON_HEDLEY_WARN_UNUSED_RESULT
  20714. static basic_json from_bson(InputType&& i,
  20715. const bool strict = true,
  20716. const bool allow_exceptions = true)
  20717. {
  20718. basic_json result;
  20719. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20720. auto ia = detail::input_adapter(std::forward<InputType>(i));
  20721. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict);
  20722. return res ? result : basic_json(value_t::discarded);
  20723. }
  20724. /// @brief create a JSON value from an input in BSON format
  20725. /// @sa https://json.nlohmann.me/api/basic_json/from_bson/
  20726. template<typename IteratorType>
  20727. JSON_HEDLEY_WARN_UNUSED_RESULT
  20728. static basic_json from_bson(IteratorType first, IteratorType last,
  20729. const bool strict = true,
  20730. const bool allow_exceptions = true)
  20731. {
  20732. basic_json result;
  20733. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20734. auto ia = detail::input_adapter(std::move(first), std::move(last));
  20735. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict);
  20736. return res ? result : basic_json(value_t::discarded);
  20737. }
  20738. template<typename T>
  20739. JSON_HEDLEY_WARN_UNUSED_RESULT
  20740. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_bson(ptr, ptr + len))
  20741. static basic_json from_bson(const T* ptr, std::size_t len,
  20742. const bool strict = true,
  20743. const bool allow_exceptions = true)
  20744. {
  20745. return from_bson(ptr, ptr + len, strict, allow_exceptions);
  20746. }
  20747. JSON_HEDLEY_WARN_UNUSED_RESULT
  20748. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_bson(ptr, ptr + len))
  20749. static basic_json from_bson(detail::span_input_adapter&& i,
  20750. const bool strict = true,
  20751. const bool allow_exceptions = true)
  20752. {
  20753. basic_json result;
  20754. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20755. auto ia = i.get();
  20756. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  20757. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict);
  20758. return res ? result : basic_json(value_t::discarded);
  20759. }
  20760. /// @}
  20761. //////////////////////////
  20762. // JSON Pointer support //
  20763. //////////////////////////
  20764. /// @name JSON Pointer functions
  20765. /// @{
  20766. /// @brief access specified element via JSON Pointer
  20767. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  20768. reference operator[](const json_pointer& ptr)
  20769. {
  20770. return ptr.get_unchecked(this);
  20771. }
  20772. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  20773. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  20774. reference operator[](const ::nlohmann::json_pointer<BasicJsonType>& ptr)
  20775. {
  20776. return ptr.get_unchecked(this);
  20777. }
  20778. /// @brief access specified element via JSON Pointer
  20779. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  20780. const_reference operator[](const json_pointer& ptr) const
  20781. {
  20782. return ptr.get_unchecked(this);
  20783. }
  20784. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  20785. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  20786. const_reference operator[](const ::nlohmann::json_pointer<BasicJsonType>& ptr) const
  20787. {
  20788. return ptr.get_unchecked(this);
  20789. }
  20790. /// @brief access specified element via JSON Pointer
  20791. /// @sa https://json.nlohmann.me/api/basic_json/at/
  20792. reference at(const json_pointer& ptr)
  20793. {
  20794. return ptr.get_checked(this);
  20795. }
  20796. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  20797. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  20798. reference at(const ::nlohmann::json_pointer<BasicJsonType>& ptr)
  20799. {
  20800. return ptr.get_checked(this);
  20801. }
  20802. /// @brief access specified element via JSON Pointer
  20803. /// @sa https://json.nlohmann.me/api/basic_json/at/
  20804. const_reference at(const json_pointer& ptr) const
  20805. {
  20806. return ptr.get_checked(this);
  20807. }
  20808. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  20809. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  20810. const_reference at(const ::nlohmann::json_pointer<BasicJsonType>& ptr) const
  20811. {
  20812. return ptr.get_checked(this);
  20813. }
  20814. /// @brief return flattened JSON value
  20815. /// @sa https://json.nlohmann.me/api/basic_json/flatten/
  20816. basic_json flatten() const
  20817. {
  20818. basic_json result(value_t::object);
  20819. json_pointer::flatten("", *this, result);
  20820. return result;
  20821. }
  20822. /// @brief unflatten a previously flattened JSON value
  20823. /// @sa https://json.nlohmann.me/api/basic_json/unflatten/
  20824. basic_json unflatten() const
  20825. {
  20826. return json_pointer::unflatten(*this);
  20827. }
  20828. /// @}
  20829. //////////////////////////
  20830. // JSON Patch functions //
  20831. //////////////////////////
  20832. /// @name JSON Patch functions
  20833. /// @{
  20834. /// @brief applies a JSON patch in-place without copying the object
  20835. /// @sa https://json.nlohmann.me/api/basic_json/patch/
  20836. void patch_inplace(const basic_json& json_patch)
  20837. {
  20838. basic_json& result = *this;
  20839. // the valid JSON Patch operations
  20840. enum class patch_operations {add, remove, replace, move, copy, test, invalid};
  20841. const auto get_op = [](const std::string & op)
  20842. {
  20843. if (op == "add")
  20844. {
  20845. return patch_operations::add;
  20846. }
  20847. if (op == "remove")
  20848. {
  20849. return patch_operations::remove;
  20850. }
  20851. if (op == "replace")
  20852. {
  20853. return patch_operations::replace;
  20854. }
  20855. if (op == "move")
  20856. {
  20857. return patch_operations::move;
  20858. }
  20859. if (op == "copy")
  20860. {
  20861. return patch_operations::copy;
  20862. }
  20863. if (op == "test")
  20864. {
  20865. return patch_operations::test;
  20866. }
  20867. return patch_operations::invalid;
  20868. };
  20869. // wrapper for "add" operation; add value at ptr
  20870. const auto operation_add = [&result](json_pointer & ptr, basic_json val)
  20871. {
  20872. // adding to the root of the target document means replacing it
  20873. if (ptr.empty())
  20874. {
  20875. result = val;
  20876. return;
  20877. }
  20878. // make sure the top element of the pointer exists
  20879. json_pointer const top_pointer = ptr.top();
  20880. if (top_pointer != ptr)
  20881. {
  20882. result.at(top_pointer);
  20883. }
  20884. // get reference to parent of JSON pointer ptr
  20885. const auto last_path = ptr.back();
  20886. ptr.pop_back();
  20887. // parent must exist when performing patch add per RFC6902 specs
  20888. basic_json& parent = result.at(ptr);
  20889. switch (parent.m_data.m_type)
  20890. {
  20891. case value_t::null:
  20892. case value_t::object:
  20893. {
  20894. // use operator[] to add value
  20895. parent[last_path] = val;
  20896. break;
  20897. }
  20898. case value_t::array:
  20899. {
  20900. if (last_path == "-")
  20901. {
  20902. // special case: append to back
  20903. parent.push_back(val);
  20904. }
  20905. else
  20906. {
  20907. const auto idx = json_pointer::template array_index<basic_json_t>(last_path);
  20908. if (JSON_HEDLEY_UNLIKELY(idx > parent.size()))
  20909. {
  20910. // avoid undefined behavior
  20911. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), &parent));
  20912. }
  20913. // default case: insert add offset
  20914. parent.insert(parent.begin() + static_cast<difference_type>(idx), val);
  20915. }
  20916. break;
  20917. }
  20918. // if there exists a parent it cannot be primitive
  20919. case value_t::string: // LCOV_EXCL_LINE
  20920. case value_t::boolean: // LCOV_EXCL_LINE
  20921. case value_t::number_integer: // LCOV_EXCL_LINE
  20922. case value_t::number_unsigned: // LCOV_EXCL_LINE
  20923. case value_t::number_float: // LCOV_EXCL_LINE
  20924. case value_t::binary: // LCOV_EXCL_LINE
  20925. case value_t::discarded: // LCOV_EXCL_LINE
  20926. default: // LCOV_EXCL_LINE
  20927. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  20928. }
  20929. };
  20930. // wrapper for "remove" operation; remove value at ptr
  20931. const auto operation_remove = [this, & result](json_pointer & ptr)
  20932. {
  20933. // get reference to parent of JSON pointer ptr
  20934. const auto last_path = ptr.back();
  20935. ptr.pop_back();
  20936. basic_json& parent = result.at(ptr);
  20937. // remove child
  20938. if (parent.is_object())
  20939. {
  20940. // perform range check
  20941. auto it = parent.find(last_path);
  20942. if (JSON_HEDLEY_LIKELY(it != parent.end()))
  20943. {
  20944. parent.erase(it);
  20945. }
  20946. else
  20947. {
  20948. JSON_THROW(out_of_range::create(403, detail::concat("key '", last_path, "' not found"), this));
  20949. }
  20950. }
  20951. else if (parent.is_array())
  20952. {
  20953. // note erase performs range check
  20954. parent.erase(json_pointer::template array_index<basic_json_t>(last_path));
  20955. }
  20956. };
  20957. // type check: top level value must be an array
  20958. if (JSON_HEDLEY_UNLIKELY(!json_patch.is_array()))
  20959. {
  20960. JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", &json_patch));
  20961. }
  20962. // iterate and apply the operations
  20963. for (const auto& val : json_patch)
  20964. {
  20965. // wrapper to get a value for an operation
  20966. const auto get_value = [&val](const std::string & op,
  20967. const std::string & member,
  20968. bool string_type) -> basic_json &
  20969. {
  20970. // find value
  20971. auto it = val.m_data.m_value.object->find(member);
  20972. // context-sensitive error message
  20973. const auto error_msg = (op == "op") ? "operation" : detail::concat("operation '", op, '\'');
  20974. // check if desired value is present
  20975. if (JSON_HEDLEY_UNLIKELY(it == val.m_data.m_value.object->end()))
  20976. {
  20977. // NOLINTNEXTLINE(performance-inefficient-string-concatenation)
  20978. JSON_THROW(parse_error::create(105, 0, detail::concat(error_msg, " must have member '", member, "'"), &val));
  20979. }
  20980. // check if result is of type string
  20981. if (JSON_HEDLEY_UNLIKELY(string_type && !it->second.is_string()))
  20982. {
  20983. // NOLINTNEXTLINE(performance-inefficient-string-concatenation)
  20984. JSON_THROW(parse_error::create(105, 0, detail::concat(error_msg, " must have string member '", member, "'"), &val));
  20985. }
  20986. // no error: return value
  20987. return it->second;
  20988. };
  20989. // type check: every element of the array must be an object
  20990. if (JSON_HEDLEY_UNLIKELY(!val.is_object()))
  20991. {
  20992. JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", &val));
  20993. }
  20994. // collect mandatory members
  20995. const auto op = get_value("op", "op", true).template get<std::string>();
  20996. const auto path = get_value(op, "path", true).template get<std::string>();
  20997. json_pointer ptr(path);
  20998. switch (get_op(op))
  20999. {
  21000. case patch_operations::add:
  21001. {
  21002. operation_add(ptr, get_value("add", "value", false));
  21003. break;
  21004. }
  21005. case patch_operations::remove:
  21006. {
  21007. operation_remove(ptr);
  21008. break;
  21009. }
  21010. case patch_operations::replace:
  21011. {
  21012. // the "path" location must exist - use at()
  21013. result.at(ptr) = get_value("replace", "value", false);
  21014. break;
  21015. }
  21016. case patch_operations::move:
  21017. {
  21018. const auto from_path = get_value("move", "from", true).template get<std::string>();
  21019. json_pointer from_ptr(from_path);
  21020. // the "from" location must exist - use at()
  21021. basic_json const v = result.at(from_ptr);
  21022. // The move operation is functionally identical to a
  21023. // "remove" operation on the "from" location, followed
  21024. // immediately by an "add" operation at the target
  21025. // location with the value that was just removed.
  21026. operation_remove(from_ptr);
  21027. operation_add(ptr, v);
  21028. break;
  21029. }
  21030. case patch_operations::copy:
  21031. {
  21032. const auto from_path = get_value("copy", "from", true).template get<std::string>();
  21033. const json_pointer from_ptr(from_path);
  21034. // the "from" location must exist - use at()
  21035. basic_json const v = result.at(from_ptr);
  21036. // The copy is functionally identical to an "add"
  21037. // operation at the target location using the value
  21038. // specified in the "from" member.
  21039. operation_add(ptr, v);
  21040. break;
  21041. }
  21042. case patch_operations::test:
  21043. {
  21044. bool success = false;
  21045. JSON_TRY
  21046. {
  21047. // check if "value" matches the one at "path"
  21048. // the "path" location must exist - use at()
  21049. success = (result.at(ptr) == get_value("test", "value", false));
  21050. }
  21051. JSON_INTERNAL_CATCH (out_of_range&)
  21052. {
  21053. // ignore out of range errors: success remains false
  21054. }
  21055. // throw an exception if test fails
  21056. if (JSON_HEDLEY_UNLIKELY(!success))
  21057. {
  21058. JSON_THROW(other_error::create(501, detail::concat("unsuccessful: ", val.dump()), &val));
  21059. }
  21060. break;
  21061. }
  21062. case patch_operations::invalid:
  21063. default:
  21064. {
  21065. // op must be "add", "remove", "replace", "move", "copy", or
  21066. // "test"
  21067. JSON_THROW(parse_error::create(105, 0, detail::concat("operation value '", op, "' is invalid"), &val));
  21068. }
  21069. }
  21070. }
  21071. }
  21072. /// @brief applies a JSON patch to a copy of the current object
  21073. /// @sa https://json.nlohmann.me/api/basic_json/patch/
  21074. basic_json patch(const basic_json& json_patch) const
  21075. {
  21076. basic_json result = *this;
  21077. result.patch_inplace(json_patch);
  21078. return result;
  21079. }
  21080. /// @brief creates a diff as a JSON patch
  21081. /// @sa https://json.nlohmann.me/api/basic_json/diff/
  21082. JSON_HEDLEY_WARN_UNUSED_RESULT
  21083. static basic_json diff(const basic_json& source, const basic_json& target,
  21084. const std::string& path = "")
  21085. {
  21086. // the patch
  21087. basic_json result(value_t::array);
  21088. // if the values are the same, return empty patch
  21089. if (source == target)
  21090. {
  21091. return result;
  21092. }
  21093. if (source.type() != target.type())
  21094. {
  21095. // different types: replace value
  21096. result.push_back(
  21097. {
  21098. {"op", "replace"}, {"path", path}, {"value", target}
  21099. });
  21100. return result;
  21101. }
  21102. switch (source.type())
  21103. {
  21104. case value_t::array:
  21105. {
  21106. // first pass: traverse common elements
  21107. std::size_t i = 0;
  21108. while (i < source.size() && i < target.size())
  21109. {
  21110. // recursive call to compare array values at index i
  21111. auto temp_diff = diff(source[i], target[i], detail::concat(path, '/', std::to_string(i)));
  21112. result.insert(result.end(), temp_diff.begin(), temp_diff.end());
  21113. ++i;
  21114. }
  21115. // We now reached the end of at least one array
  21116. // in a second pass, traverse the remaining elements
  21117. // remove my remaining elements
  21118. const auto end_index = static_cast<difference_type>(result.size());
  21119. while (i < source.size())
  21120. {
  21121. // add operations in reverse order to avoid invalid
  21122. // indices
  21123. result.insert(result.begin() + end_index, object(
  21124. {
  21125. {"op", "remove"},
  21126. {"path", detail::concat(path, '/', std::to_string(i))}
  21127. }));
  21128. ++i;
  21129. }
  21130. // add other remaining elements
  21131. while (i < target.size())
  21132. {
  21133. result.push_back(
  21134. {
  21135. {"op", "add"},
  21136. {"path", detail::concat(path, "/-")},
  21137. {"value", target[i]}
  21138. });
  21139. ++i;
  21140. }
  21141. break;
  21142. }
  21143. case value_t::object:
  21144. {
  21145. // first pass: traverse this object's elements
  21146. for (auto it = source.cbegin(); it != source.cend(); ++it)
  21147. {
  21148. // escape the key name to be used in a JSON patch
  21149. const auto path_key = detail::concat(path, '/', detail::escape(it.key()));
  21150. if (target.find(it.key()) != target.end())
  21151. {
  21152. // recursive call to compare object values at key it
  21153. auto temp_diff = diff(it.value(), target[it.key()], path_key);
  21154. result.insert(result.end(), temp_diff.begin(), temp_diff.end());
  21155. }
  21156. else
  21157. {
  21158. // found a key that is not in o -> remove it
  21159. result.push_back(object(
  21160. {
  21161. {"op", "remove"}, {"path", path_key}
  21162. }));
  21163. }
  21164. }
  21165. // second pass: traverse other object's elements
  21166. for (auto it = target.cbegin(); it != target.cend(); ++it)
  21167. {
  21168. if (source.find(it.key()) == source.end())
  21169. {
  21170. // found a key that is not in this -> add it
  21171. const auto path_key = detail::concat(path, '/', detail::escape(it.key()));
  21172. result.push_back(
  21173. {
  21174. {"op", "add"}, {"path", path_key},
  21175. {"value", it.value()}
  21176. });
  21177. }
  21178. }
  21179. break;
  21180. }
  21181. case value_t::null:
  21182. case value_t::string:
  21183. case value_t::boolean:
  21184. case value_t::number_integer:
  21185. case value_t::number_unsigned:
  21186. case value_t::number_float:
  21187. case value_t::binary:
  21188. case value_t::discarded:
  21189. default:
  21190. {
  21191. // both primitive type: replace value
  21192. result.push_back(
  21193. {
  21194. {"op", "replace"}, {"path", path}, {"value", target}
  21195. });
  21196. break;
  21197. }
  21198. }
  21199. return result;
  21200. }
  21201. /// @}
  21202. ////////////////////////////////
  21203. // JSON Merge Patch functions //
  21204. ////////////////////////////////
  21205. /// @name JSON Merge Patch functions
  21206. /// @{
  21207. /// @brief applies a JSON Merge Patch
  21208. /// @sa https://json.nlohmann.me/api/basic_json/merge_patch/
  21209. void merge_patch(const basic_json& apply_patch)
  21210. {
  21211. if (apply_patch.is_object())
  21212. {
  21213. if (!is_object())
  21214. {
  21215. *this = object();
  21216. }
  21217. for (auto it = apply_patch.begin(); it != apply_patch.end(); ++it)
  21218. {
  21219. if (it.value().is_null())
  21220. {
  21221. erase(it.key());
  21222. }
  21223. else
  21224. {
  21225. operator[](it.key()).merge_patch(it.value());
  21226. }
  21227. }
  21228. }
  21229. else
  21230. {
  21231. *this = apply_patch;
  21232. }
  21233. }
  21234. /// @}
  21235. };
  21236. /// @brief user-defined to_string function for JSON values
  21237. /// @sa https://json.nlohmann.me/api/basic_json/to_string/
  21238. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  21239. std::string to_string(const NLOHMANN_BASIC_JSON_TPL& j)
  21240. {
  21241. return j.dump();
  21242. }
  21243. inline namespace literals
  21244. {
  21245. inline namespace json_literals
  21246. {
  21247. /// @brief user-defined string literal for JSON values
  21248. /// @sa https://json.nlohmann.me/api/basic_json/operator_literal_json/
  21249. JSON_HEDLEY_NON_NULL(1)
  21250. #if !defined(JSON_HEDLEY_GCC_VERSION) || JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0)
  21251. inline nlohmann::json operator ""_json(const char* s, std::size_t n)
  21252. #else
  21253. inline nlohmann::json operator "" _json(const char* s, std::size_t n)
  21254. #endif
  21255. {
  21256. return nlohmann::json::parse(s, s + n);
  21257. }
  21258. /// @brief user-defined string literal for JSON pointer
  21259. /// @sa https://json.nlohmann.me/api/basic_json/operator_literal_json_pointer/
  21260. JSON_HEDLEY_NON_NULL(1)
  21261. #if !defined(JSON_HEDLEY_GCC_VERSION) || JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0)
  21262. inline nlohmann::json::json_pointer operator ""_json_pointer(const char* s, std::size_t n)
  21263. #else
  21264. inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std::size_t n)
  21265. #endif
  21266. {
  21267. return nlohmann::json::json_pointer(std::string(s, n));
  21268. }
  21269. } // namespace json_literals
  21270. } // namespace literals
  21271. NLOHMANN_JSON_NAMESPACE_END
  21272. ///////////////////////
  21273. // nonmember support //
  21274. ///////////////////////
  21275. namespace std // NOLINT(cert-dcl58-cpp)
  21276. {
  21277. /// @brief hash value for JSON objects
  21278. /// @sa https://json.nlohmann.me/api/basic_json/std_hash/
  21279. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  21280. struct hash<nlohmann::NLOHMANN_BASIC_JSON_TPL> // NOLINT(cert-dcl58-cpp)
  21281. {
  21282. std::size_t operator()(const nlohmann::NLOHMANN_BASIC_JSON_TPL& j) const
  21283. {
  21284. return nlohmann::detail::hash(j);
  21285. }
  21286. };
  21287. // specialization for std::less<value_t>
  21288. template<>
  21289. struct less< ::nlohmann::detail::value_t> // do not remove the space after '<', see https://github.com/nlohmann/json/pull/679
  21290. {
  21291. /*!
  21292. @brief compare two value_t enum values
  21293. @since version 3.0.0
  21294. */
  21295. bool operator()(::nlohmann::detail::value_t lhs,
  21296. ::nlohmann::detail::value_t rhs) const noexcept
  21297. {
  21298. #if JSON_HAS_THREE_WAY_COMPARISON
  21299. return std::is_lt(lhs <=> rhs); // *NOPAD*
  21300. #else
  21301. return ::nlohmann::detail::operator<(lhs, rhs);
  21302. #endif
  21303. }
  21304. };
  21305. // C++20 prohibit function specialization in the std namespace.
  21306. #ifndef JSON_HAS_CPP_20
  21307. /// @brief exchanges the values of two JSON objects
  21308. /// @sa https://json.nlohmann.me/api/basic_json/std_swap/
  21309. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  21310. 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)
  21311. is_nothrow_move_constructible<nlohmann::NLOHMANN_BASIC_JSON_TPL>::value&& // NOLINT(misc-redundant-expression,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  21312. is_nothrow_move_assignable<nlohmann::NLOHMANN_BASIC_JSON_TPL>::value)
  21313. {
  21314. j1.swap(j2);
  21315. }
  21316. #endif
  21317. } // namespace std
  21318. #if JSON_USE_GLOBAL_UDLS
  21319. #if !defined(JSON_HEDLEY_GCC_VERSION) || JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0)
  21320. using nlohmann::literals::json_literals::operator ""_json; // NOLINT(misc-unused-using-decls,google-global-names-in-headers)
  21321. using nlohmann::literals::json_literals::operator ""_json_pointer; //NOLINT(misc-unused-using-decls,google-global-names-in-headers)
  21322. #else
  21323. using nlohmann::literals::json_literals::operator "" _json; // NOLINT(misc-unused-using-decls,google-global-names-in-headers)
  21324. using nlohmann::literals::json_literals::operator "" _json_pointer; //NOLINT(misc-unused-using-decls,google-global-names-in-headers)
  21325. #endif
  21326. #endif
  21327. // #include <nlohmann/detail/macro_unscope.hpp>
  21328. // __ _____ _____ _____
  21329. // __| | __| | | | JSON for Modern C++
  21330. // | | |__ | | | | | | version 3.11.2
  21331. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  21332. //
  21333. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  21334. // SPDX-License-Identifier: MIT
  21335. // restore clang diagnostic settings
  21336. #if defined(__clang__)
  21337. #pragma clang diagnostic pop
  21338. #endif
  21339. // clean up
  21340. #undef JSON_ASSERT
  21341. #undef JSON_INTERNAL_CATCH
  21342. #undef JSON_THROW
  21343. #undef JSON_PRIVATE_UNLESS_TESTED
  21344. #undef NLOHMANN_BASIC_JSON_TPL_DECLARATION
  21345. #undef NLOHMANN_BASIC_JSON_TPL
  21346. #undef JSON_EXPLICIT
  21347. #undef NLOHMANN_CAN_CALL_STD_FUNC_IMPL
  21348. #undef JSON_INLINE_VARIABLE
  21349. #undef JSON_NO_UNIQUE_ADDRESS
  21350. #undef JSON_DISABLE_ENUM_SERIALIZATION
  21351. #undef JSON_USE_GLOBAL_UDLS
  21352. #ifndef JSON_TEST_KEEP_MACROS
  21353. #undef JSON_CATCH
  21354. #undef JSON_TRY
  21355. #undef JSON_HAS_CPP_11
  21356. #undef JSON_HAS_CPP_14
  21357. #undef JSON_HAS_CPP_17
  21358. #undef JSON_HAS_CPP_20
  21359. #undef JSON_HAS_FILESYSTEM
  21360. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  21361. #undef JSON_HAS_THREE_WAY_COMPARISON
  21362. #undef JSON_HAS_RANGES
  21363. #undef JSON_HAS_STATIC_RTTI
  21364. #undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  21365. #endif
  21366. // #include <nlohmann/thirdparty/hedley/hedley_undef.hpp>
  21367. // __ _____ _____ _____
  21368. // __| | __| | | | JSON for Modern C++
  21369. // | | |__ | | | | | | version 3.11.2
  21370. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  21371. //
  21372. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  21373. // SPDX-License-Identifier: MIT
  21374. #undef JSON_HEDLEY_ALWAYS_INLINE
  21375. #undef JSON_HEDLEY_ARM_VERSION
  21376. #undef JSON_HEDLEY_ARM_VERSION_CHECK
  21377. #undef JSON_HEDLEY_ARRAY_PARAM
  21378. #undef JSON_HEDLEY_ASSUME
  21379. #undef JSON_HEDLEY_BEGIN_C_DECLS
  21380. #undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE
  21381. #undef JSON_HEDLEY_CLANG_HAS_BUILTIN
  21382. #undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE
  21383. #undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE
  21384. #undef JSON_HEDLEY_CLANG_HAS_EXTENSION
  21385. #undef JSON_HEDLEY_CLANG_HAS_FEATURE
  21386. #undef JSON_HEDLEY_CLANG_HAS_WARNING
  21387. #undef JSON_HEDLEY_COMPCERT_VERSION
  21388. #undef JSON_HEDLEY_COMPCERT_VERSION_CHECK
  21389. #undef JSON_HEDLEY_CONCAT
  21390. #undef JSON_HEDLEY_CONCAT3
  21391. #undef JSON_HEDLEY_CONCAT3_EX
  21392. #undef JSON_HEDLEY_CONCAT_EX
  21393. #undef JSON_HEDLEY_CONST
  21394. #undef JSON_HEDLEY_CONSTEXPR
  21395. #undef JSON_HEDLEY_CONST_CAST
  21396. #undef JSON_HEDLEY_CPP_CAST
  21397. #undef JSON_HEDLEY_CRAY_VERSION
  21398. #undef JSON_HEDLEY_CRAY_VERSION_CHECK
  21399. #undef JSON_HEDLEY_C_DECL
  21400. #undef JSON_HEDLEY_DEPRECATED
  21401. #undef JSON_HEDLEY_DEPRECATED_FOR
  21402. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
  21403. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_
  21404. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
  21405. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
  21406. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
  21407. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
  21408. #undef JSON_HEDLEY_DIAGNOSTIC_POP
  21409. #undef JSON_HEDLEY_DIAGNOSTIC_PUSH
  21410. #undef JSON_HEDLEY_DMC_VERSION
  21411. #undef JSON_HEDLEY_DMC_VERSION_CHECK
  21412. #undef JSON_HEDLEY_EMPTY_BASES
  21413. #undef JSON_HEDLEY_EMSCRIPTEN_VERSION
  21414. #undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK
  21415. #undef JSON_HEDLEY_END_C_DECLS
  21416. #undef JSON_HEDLEY_FLAGS
  21417. #undef JSON_HEDLEY_FLAGS_CAST
  21418. #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE
  21419. #undef JSON_HEDLEY_GCC_HAS_BUILTIN
  21420. #undef JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE
  21421. #undef JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE
  21422. #undef JSON_HEDLEY_GCC_HAS_EXTENSION
  21423. #undef JSON_HEDLEY_GCC_HAS_FEATURE
  21424. #undef JSON_HEDLEY_GCC_HAS_WARNING
  21425. #undef JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK
  21426. #undef JSON_HEDLEY_GCC_VERSION
  21427. #undef JSON_HEDLEY_GCC_VERSION_CHECK
  21428. #undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE
  21429. #undef JSON_HEDLEY_GNUC_HAS_BUILTIN
  21430. #undef JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE
  21431. #undef JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE
  21432. #undef JSON_HEDLEY_GNUC_HAS_EXTENSION
  21433. #undef JSON_HEDLEY_GNUC_HAS_FEATURE
  21434. #undef JSON_HEDLEY_GNUC_HAS_WARNING
  21435. #undef JSON_HEDLEY_GNUC_VERSION
  21436. #undef JSON_HEDLEY_GNUC_VERSION_CHECK
  21437. #undef JSON_HEDLEY_HAS_ATTRIBUTE
  21438. #undef JSON_HEDLEY_HAS_BUILTIN
  21439. #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE
  21440. #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS
  21441. #undef JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE
  21442. #undef JSON_HEDLEY_HAS_EXTENSION
  21443. #undef JSON_HEDLEY_HAS_FEATURE
  21444. #undef JSON_HEDLEY_HAS_WARNING
  21445. #undef JSON_HEDLEY_IAR_VERSION
  21446. #undef JSON_HEDLEY_IAR_VERSION_CHECK
  21447. #undef JSON_HEDLEY_IBM_VERSION
  21448. #undef JSON_HEDLEY_IBM_VERSION_CHECK
  21449. #undef JSON_HEDLEY_IMPORT
  21450. #undef JSON_HEDLEY_INLINE
  21451. #undef JSON_HEDLEY_INTEL_CL_VERSION
  21452. #undef JSON_HEDLEY_INTEL_CL_VERSION_CHECK
  21453. #undef JSON_HEDLEY_INTEL_VERSION
  21454. #undef JSON_HEDLEY_INTEL_VERSION_CHECK
  21455. #undef JSON_HEDLEY_IS_CONSTANT
  21456. #undef JSON_HEDLEY_IS_CONSTEXPR_
  21457. #undef JSON_HEDLEY_LIKELY
  21458. #undef JSON_HEDLEY_MALLOC
  21459. #undef JSON_HEDLEY_MCST_LCC_VERSION
  21460. #undef JSON_HEDLEY_MCST_LCC_VERSION_CHECK
  21461. #undef JSON_HEDLEY_MESSAGE
  21462. #undef JSON_HEDLEY_MSVC_VERSION
  21463. #undef JSON_HEDLEY_MSVC_VERSION_CHECK
  21464. #undef JSON_HEDLEY_NEVER_INLINE
  21465. #undef JSON_HEDLEY_NON_NULL
  21466. #undef JSON_HEDLEY_NO_ESCAPE
  21467. #undef JSON_HEDLEY_NO_RETURN
  21468. #undef JSON_HEDLEY_NO_THROW
  21469. #undef JSON_HEDLEY_NULL
  21470. #undef JSON_HEDLEY_PELLES_VERSION
  21471. #undef JSON_HEDLEY_PELLES_VERSION_CHECK
  21472. #undef JSON_HEDLEY_PGI_VERSION
  21473. #undef JSON_HEDLEY_PGI_VERSION_CHECK
  21474. #undef JSON_HEDLEY_PREDICT
  21475. #undef JSON_HEDLEY_PRINTF_FORMAT
  21476. #undef JSON_HEDLEY_PRIVATE
  21477. #undef JSON_HEDLEY_PUBLIC
  21478. #undef JSON_HEDLEY_PURE
  21479. #undef JSON_HEDLEY_REINTERPRET_CAST
  21480. #undef JSON_HEDLEY_REQUIRE
  21481. #undef JSON_HEDLEY_REQUIRE_CONSTEXPR
  21482. #undef JSON_HEDLEY_REQUIRE_MSG
  21483. #undef JSON_HEDLEY_RESTRICT
  21484. #undef JSON_HEDLEY_RETURNS_NON_NULL
  21485. #undef JSON_HEDLEY_SENTINEL
  21486. #undef JSON_HEDLEY_STATIC_ASSERT
  21487. #undef JSON_HEDLEY_STATIC_CAST
  21488. #undef JSON_HEDLEY_STRINGIFY
  21489. #undef JSON_HEDLEY_STRINGIFY_EX
  21490. #undef JSON_HEDLEY_SUNPRO_VERSION
  21491. #undef JSON_HEDLEY_SUNPRO_VERSION_CHECK
  21492. #undef JSON_HEDLEY_TINYC_VERSION
  21493. #undef JSON_HEDLEY_TINYC_VERSION_CHECK
  21494. #undef JSON_HEDLEY_TI_ARMCL_VERSION
  21495. #undef JSON_HEDLEY_TI_ARMCL_VERSION_CHECK
  21496. #undef JSON_HEDLEY_TI_CL2000_VERSION
  21497. #undef JSON_HEDLEY_TI_CL2000_VERSION_CHECK
  21498. #undef JSON_HEDLEY_TI_CL430_VERSION
  21499. #undef JSON_HEDLEY_TI_CL430_VERSION_CHECK
  21500. #undef JSON_HEDLEY_TI_CL6X_VERSION
  21501. #undef JSON_HEDLEY_TI_CL6X_VERSION_CHECK
  21502. #undef JSON_HEDLEY_TI_CL7X_VERSION
  21503. #undef JSON_HEDLEY_TI_CL7X_VERSION_CHECK
  21504. #undef JSON_HEDLEY_TI_CLPRU_VERSION
  21505. #undef JSON_HEDLEY_TI_CLPRU_VERSION_CHECK
  21506. #undef JSON_HEDLEY_TI_VERSION
  21507. #undef JSON_HEDLEY_TI_VERSION_CHECK
  21508. #undef JSON_HEDLEY_UNAVAILABLE
  21509. #undef JSON_HEDLEY_UNLIKELY
  21510. #undef JSON_HEDLEY_UNPREDICTABLE
  21511. #undef JSON_HEDLEY_UNREACHABLE
  21512. #undef JSON_HEDLEY_UNREACHABLE_RETURN
  21513. #undef JSON_HEDLEY_VERSION
  21514. #undef JSON_HEDLEY_VERSION_DECODE_MAJOR
  21515. #undef JSON_HEDLEY_VERSION_DECODE_MINOR
  21516. #undef JSON_HEDLEY_VERSION_DECODE_REVISION
  21517. #undef JSON_HEDLEY_VERSION_ENCODE
  21518. #undef JSON_HEDLEY_WARNING
  21519. #undef JSON_HEDLEY_WARN_UNUSED_RESULT
  21520. #undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG
  21521. #undef JSON_HEDLEY_FALL_THROUGH
  21522. #endif // INCLUDE_NLOHMANN_JSON_HPP_