json.hpp 887 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596
  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 <numeric> // accumulate
  28. #include <string> // string, stoi, to_string
  29. #include <utility> // declval, forward, move, pair, swap
  30. #include <vector> // vector
  31. // #include <nlohmann/adl_serializer.hpp>
  32. // __ _____ _____ _____
  33. // __| | __| | | | JSON for Modern C++
  34. // | | |__ | | | | | | version 3.11.2
  35. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  36. //
  37. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  38. // SPDX-License-Identifier: MIT
  39. #include <utility>
  40. // #include <nlohmann/detail/abi_macros.hpp>
  41. // __ _____ _____ _____
  42. // __| | __| | | | JSON for Modern C++
  43. // | | |__ | | | | | | version 3.11.2
  44. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  45. //
  46. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  47. // SPDX-License-Identifier: MIT
  48. // This file contains all macro definitions affecting or depending on the ABI
  49. #ifndef JSON_SKIP_LIBRARY_VERSION_CHECK
  50. #if defined(NLOHMANN_JSON_VERSION_MAJOR) && defined(NLOHMANN_JSON_VERSION_MINOR) && defined(NLOHMANN_JSON_VERSION_PATCH)
  51. #if NLOHMANN_JSON_VERSION_MAJOR != 3 || NLOHMANN_JSON_VERSION_MINOR != 11 || NLOHMANN_JSON_VERSION_PATCH != 2
  52. #warning "Already included a different version of the library!"
  53. #endif
  54. #endif
  55. #endif
  56. #define NLOHMANN_JSON_VERSION_MAJOR 3 // NOLINT(modernize-macro-to-enum)
  57. #define NLOHMANN_JSON_VERSION_MINOR 11 // NOLINT(modernize-macro-to-enum)
  58. #define NLOHMANN_JSON_VERSION_PATCH 2 // NOLINT(modernize-macro-to-enum)
  59. #ifndef JSON_DIAGNOSTICS
  60. #define JSON_DIAGNOSTICS 0
  61. #endif
  62. #ifndef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  63. #define JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON 0
  64. #endif
  65. #if JSON_DIAGNOSTICS
  66. #define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS _diag
  67. #else
  68. #define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS
  69. #endif
  70. #if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  71. #define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON _ldvcmp
  72. #else
  73. #define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON
  74. #endif
  75. #ifndef NLOHMANN_JSON_NAMESPACE_NO_VERSION
  76. #define NLOHMANN_JSON_NAMESPACE_NO_VERSION 0
  77. #endif
  78. // Construct the namespace ABI tags component
  79. #define NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b) json_abi ## a ## b
  80. #define NLOHMANN_JSON_ABI_TAGS_CONCAT(a, b) \
  81. NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b)
  82. #define NLOHMANN_JSON_ABI_TAGS \
  83. NLOHMANN_JSON_ABI_TAGS_CONCAT( \
  84. NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS, \
  85. NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON)
  86. // Construct the namespace version component
  87. #define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch) \
  88. _v ## major ## _ ## minor ## _ ## patch
  89. #define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(major, minor, patch) \
  90. NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch)
  91. #if NLOHMANN_JSON_NAMESPACE_NO_VERSION
  92. #define NLOHMANN_JSON_NAMESPACE_VERSION
  93. #else
  94. #define NLOHMANN_JSON_NAMESPACE_VERSION \
  95. NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(NLOHMANN_JSON_VERSION_MAJOR, \
  96. NLOHMANN_JSON_VERSION_MINOR, \
  97. NLOHMANN_JSON_VERSION_PATCH)
  98. #endif
  99. // Combine namespace components
  100. #define NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b) a ## b
  101. #define NLOHMANN_JSON_NAMESPACE_CONCAT(a, b) \
  102. NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b)
  103. #ifndef NLOHMANN_JSON_NAMESPACE
  104. #define NLOHMANN_JSON_NAMESPACE \
  105. nlohmann::NLOHMANN_JSON_NAMESPACE_CONCAT( \
  106. NLOHMANN_JSON_ABI_TAGS, \
  107. NLOHMANN_JSON_NAMESPACE_VERSION)
  108. #endif
  109. #ifndef NLOHMANN_JSON_NAMESPACE_BEGIN
  110. #define NLOHMANN_JSON_NAMESPACE_BEGIN \
  111. namespace nlohmann \
  112. { \
  113. inline namespace NLOHMANN_JSON_NAMESPACE_CONCAT( \
  114. NLOHMANN_JSON_ABI_TAGS, \
  115. NLOHMANN_JSON_NAMESPACE_VERSION) \
  116. {
  117. #endif
  118. #ifndef NLOHMANN_JSON_NAMESPACE_END
  119. #define NLOHMANN_JSON_NAMESPACE_END \
  120. } /* namespace (inline namespace) NOLINT(readability/namespace) */ \
  121. } // namespace nlohmann
  122. #endif
  123. // #include <nlohmann/detail/conversions/from_json.hpp>
  124. // __ _____ _____ _____
  125. // __| | __| | | | JSON for Modern C++
  126. // | | |__ | | | | | | version 3.11.2
  127. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  128. //
  129. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  130. // SPDX-License-Identifier: MIT
  131. #include <algorithm> // transform
  132. #include <array> // array
  133. #include <forward_list> // forward_list
  134. #include <iterator> // inserter, front_inserter, end
  135. #include <map> // map
  136. #include <string> // string
  137. #include <tuple> // tuple, make_tuple
  138. #include <type_traits> // is_arithmetic, is_same, is_enum, underlying_type, is_convertible
  139. #include <unordered_map> // unordered_map
  140. #include <utility> // pair, declval
  141. #include <valarray> // valarray
  142. // #include <nlohmann/detail/exceptions.hpp>
  143. // __ _____ _____ _____
  144. // __| | __| | | | JSON for Modern C++
  145. // | | |__ | | | | | | version 3.11.2
  146. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  147. //
  148. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  149. // SPDX-License-Identifier: MIT
  150. #include <cstddef> // nullptr_t
  151. #include <exception> // exception
  152. #include <stdexcept> // runtime_error
  153. #include <string> // to_string
  154. #include <vector> // vector
  155. // #include <nlohmann/detail/value_t.hpp>
  156. // __ _____ _____ _____
  157. // __| | __| | | | JSON for Modern C++
  158. // | | |__ | | | | | | version 3.11.2
  159. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  160. //
  161. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  162. // SPDX-License-Identifier: MIT
  163. #include <array> // array
  164. #include <cstddef> // size_t
  165. #include <cstdint> // uint8_t
  166. #include <string> // string
  167. // #include <nlohmann/detail/macro_scope.hpp>
  168. // __ _____ _____ _____
  169. // __| | __| | | | JSON for Modern C++
  170. // | | |__ | | | | | | version 3.11.2
  171. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  172. //
  173. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  174. // SPDX-License-Identifier: MIT
  175. #include <utility> // declval, pair
  176. // #include <nlohmann/detail/meta/detected.hpp>
  177. // __ _____ _____ _____
  178. // __| | __| | | | JSON for Modern C++
  179. // | | |__ | | | | | | version 3.11.2
  180. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  181. //
  182. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  183. // SPDX-License-Identifier: MIT
  184. #include <type_traits>
  185. // #include <nlohmann/detail/meta/void_t.hpp>
  186. // __ _____ _____ _____
  187. // __| | __| | | | JSON for Modern C++
  188. // | | |__ | | | | | | version 3.11.2
  189. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  190. //
  191. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  192. // SPDX-License-Identifier: MIT
  193. // #include <nlohmann/detail/abi_macros.hpp>
  194. NLOHMANN_JSON_NAMESPACE_BEGIN
  195. namespace detail
  196. {
  197. template<typename ...Ts> struct make_void
  198. {
  199. using type = void;
  200. };
  201. template<typename ...Ts> using void_t = typename make_void<Ts...>::type;
  202. } // namespace detail
  203. NLOHMANN_JSON_NAMESPACE_END
  204. NLOHMANN_JSON_NAMESPACE_BEGIN
  205. namespace detail
  206. {
  207. // https://en.cppreference.com/w/cpp/experimental/is_detected
  208. struct nonesuch
  209. {
  210. nonesuch() = delete;
  211. ~nonesuch() = delete;
  212. nonesuch(nonesuch const&) = delete;
  213. nonesuch(nonesuch const&&) = delete;
  214. void operator=(nonesuch const&) = delete;
  215. void operator=(nonesuch&&) = delete;
  216. };
  217. template<class Default,
  218. class AlwaysVoid,
  219. template<class...> class Op,
  220. class... Args>
  221. struct detector
  222. {
  223. using value_t = std::false_type;
  224. using type = Default;
  225. };
  226. template<class Default, template<class...> class Op, class... Args>
  227. struct detector<Default, void_t<Op<Args...>>, Op, Args...>
  228. {
  229. using value_t = std::true_type;
  230. using type = Op<Args...>;
  231. };
  232. template<template<class...> class Op, class... Args>
  233. using is_detected = typename detector<nonesuch, void, Op, Args...>::value_t;
  234. template<template<class...> class Op, class... Args>
  235. struct is_detected_lazy : is_detected<Op, Args...> { };
  236. template<template<class...> class Op, class... Args>
  237. using detected_t = typename detector<nonesuch, void, Op, Args...>::type;
  238. template<class Default, template<class...> class Op, class... Args>
  239. using detected_or = detector<Default, void, Op, Args...>;
  240. template<class Default, template<class...> class Op, class... Args>
  241. using detected_or_t = typename detected_or<Default, Op, Args...>::type;
  242. template<class Expected, template<class...> class Op, class... Args>
  243. using is_detected_exact = std::is_same<Expected, detected_t<Op, Args...>>;
  244. template<class To, template<class...> class Op, class... Args>
  245. using is_detected_convertible =
  246. std::is_convertible<detected_t<Op, Args...>, To>;
  247. } // namespace detail
  248. NLOHMANN_JSON_NAMESPACE_END
  249. // #include <nlohmann/thirdparty/hedley/hedley.hpp>
  250. // __ _____ _____ _____
  251. // __| | __| | | | JSON for Modern C++
  252. // | | |__ | | | | | | version 3.11.2
  253. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  254. //
  255. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  256. // SPDX-FileCopyrightText: 2016-2021 Evan Nemerson <evan@nemerson.com>
  257. // SPDX-License-Identifier: MIT
  258. /* Hedley - https://nemequ.github.io/hedley
  259. * Created by Evan Nemerson <evan@nemerson.com>
  260. */
  261. #if !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < 15)
  262. #if defined(JSON_HEDLEY_VERSION)
  263. #undef JSON_HEDLEY_VERSION
  264. #endif
  265. #define JSON_HEDLEY_VERSION 15
  266. #if defined(JSON_HEDLEY_STRINGIFY_EX)
  267. #undef JSON_HEDLEY_STRINGIFY_EX
  268. #endif
  269. #define JSON_HEDLEY_STRINGIFY_EX(x) #x
  270. #if defined(JSON_HEDLEY_STRINGIFY)
  271. #undef JSON_HEDLEY_STRINGIFY
  272. #endif
  273. #define JSON_HEDLEY_STRINGIFY(x) JSON_HEDLEY_STRINGIFY_EX(x)
  274. #if defined(JSON_HEDLEY_CONCAT_EX)
  275. #undef JSON_HEDLEY_CONCAT_EX
  276. #endif
  277. #define JSON_HEDLEY_CONCAT_EX(a,b) a##b
  278. #if defined(JSON_HEDLEY_CONCAT)
  279. #undef JSON_HEDLEY_CONCAT
  280. #endif
  281. #define JSON_HEDLEY_CONCAT(a,b) JSON_HEDLEY_CONCAT_EX(a,b)
  282. #if defined(JSON_HEDLEY_CONCAT3_EX)
  283. #undef JSON_HEDLEY_CONCAT3_EX
  284. #endif
  285. #define JSON_HEDLEY_CONCAT3_EX(a,b,c) a##b##c
  286. #if defined(JSON_HEDLEY_CONCAT3)
  287. #undef JSON_HEDLEY_CONCAT3
  288. #endif
  289. #define JSON_HEDLEY_CONCAT3(a,b,c) JSON_HEDLEY_CONCAT3_EX(a,b,c)
  290. #if defined(JSON_HEDLEY_VERSION_ENCODE)
  291. #undef JSON_HEDLEY_VERSION_ENCODE
  292. #endif
  293. #define JSON_HEDLEY_VERSION_ENCODE(major,minor,revision) (((major) * 1000000) + ((minor) * 1000) + (revision))
  294. #if defined(JSON_HEDLEY_VERSION_DECODE_MAJOR)
  295. #undef JSON_HEDLEY_VERSION_DECODE_MAJOR
  296. #endif
  297. #define JSON_HEDLEY_VERSION_DECODE_MAJOR(version) ((version) / 1000000)
  298. #if defined(JSON_HEDLEY_VERSION_DECODE_MINOR)
  299. #undef JSON_HEDLEY_VERSION_DECODE_MINOR
  300. #endif
  301. #define JSON_HEDLEY_VERSION_DECODE_MINOR(version) (((version) % 1000000) / 1000)
  302. #if defined(JSON_HEDLEY_VERSION_DECODE_REVISION)
  303. #undef JSON_HEDLEY_VERSION_DECODE_REVISION
  304. #endif
  305. #define JSON_HEDLEY_VERSION_DECODE_REVISION(version) ((version) % 1000)
  306. #if defined(JSON_HEDLEY_GNUC_VERSION)
  307. #undef JSON_HEDLEY_GNUC_VERSION
  308. #endif
  309. #if defined(__GNUC__) && defined(__GNUC_PATCHLEVEL__)
  310. #define JSON_HEDLEY_GNUC_VERSION JSON_HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
  311. #elif defined(__GNUC__)
  312. #define JSON_HEDLEY_GNUC_VERSION JSON_HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, 0)
  313. #endif
  314. #if defined(JSON_HEDLEY_GNUC_VERSION_CHECK)
  315. #undef JSON_HEDLEY_GNUC_VERSION_CHECK
  316. #endif
  317. #if defined(JSON_HEDLEY_GNUC_VERSION)
  318. #define JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_GNUC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  319. #else
  320. #define JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) (0)
  321. #endif
  322. #if defined(JSON_HEDLEY_MSVC_VERSION)
  323. #undef JSON_HEDLEY_MSVC_VERSION
  324. #endif
  325. #if defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 140000000) && !defined(__ICL)
  326. #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 10000000, (_MSC_FULL_VER % 10000000) / 100000, (_MSC_FULL_VER % 100000) / 100)
  327. #elif defined(_MSC_FULL_VER) && !defined(__ICL)
  328. #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 1000000, (_MSC_FULL_VER % 1000000) / 10000, (_MSC_FULL_VER % 10000) / 10)
  329. #elif defined(_MSC_VER) && !defined(__ICL)
  330. #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_VER / 100, _MSC_VER % 100, 0)
  331. #endif
  332. #if defined(JSON_HEDLEY_MSVC_VERSION_CHECK)
  333. #undef JSON_HEDLEY_MSVC_VERSION_CHECK
  334. #endif
  335. #if !defined(JSON_HEDLEY_MSVC_VERSION)
  336. #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (0)
  337. #elif defined(_MSC_VER) && (_MSC_VER >= 1400)
  338. #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 10000000) + (minor * 100000) + (patch)))
  339. #elif defined(_MSC_VER) && (_MSC_VER >= 1200)
  340. #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 1000000) + (minor * 10000) + (patch)))
  341. #else
  342. #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_VER >= ((major * 100) + (minor)))
  343. #endif
  344. #if defined(JSON_HEDLEY_INTEL_VERSION)
  345. #undef JSON_HEDLEY_INTEL_VERSION
  346. #endif
  347. #if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) && !defined(__ICL)
  348. #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, __INTEL_COMPILER_UPDATE)
  349. #elif defined(__INTEL_COMPILER) && !defined(__ICL)
  350. #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, 0)
  351. #endif
  352. #if defined(JSON_HEDLEY_INTEL_VERSION_CHECK)
  353. #undef JSON_HEDLEY_INTEL_VERSION_CHECK
  354. #endif
  355. #if defined(JSON_HEDLEY_INTEL_VERSION)
  356. #define JSON_HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_INTEL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  357. #else
  358. #define JSON_HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (0)
  359. #endif
  360. #if defined(JSON_HEDLEY_INTEL_CL_VERSION)
  361. #undef JSON_HEDLEY_INTEL_CL_VERSION
  362. #endif
  363. #if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) && defined(__ICL)
  364. #define JSON_HEDLEY_INTEL_CL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER, __INTEL_COMPILER_UPDATE, 0)
  365. #endif
  366. #if defined(JSON_HEDLEY_INTEL_CL_VERSION_CHECK)
  367. #undef JSON_HEDLEY_INTEL_CL_VERSION_CHECK
  368. #endif
  369. #if defined(JSON_HEDLEY_INTEL_CL_VERSION)
  370. #define JSON_HEDLEY_INTEL_CL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_INTEL_CL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  371. #else
  372. #define JSON_HEDLEY_INTEL_CL_VERSION_CHECK(major,minor,patch) (0)
  373. #endif
  374. #if defined(JSON_HEDLEY_PGI_VERSION)
  375. #undef JSON_HEDLEY_PGI_VERSION
  376. #endif
  377. #if defined(__PGI) && defined(__PGIC__) && defined(__PGIC_MINOR__) && defined(__PGIC_PATCHLEVEL__)
  378. #define JSON_HEDLEY_PGI_VERSION JSON_HEDLEY_VERSION_ENCODE(__PGIC__, __PGIC_MINOR__, __PGIC_PATCHLEVEL__)
  379. #endif
  380. #if defined(JSON_HEDLEY_PGI_VERSION_CHECK)
  381. #undef JSON_HEDLEY_PGI_VERSION_CHECK
  382. #endif
  383. #if defined(JSON_HEDLEY_PGI_VERSION)
  384. #define JSON_HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_PGI_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  385. #else
  386. #define JSON_HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (0)
  387. #endif
  388. #if defined(JSON_HEDLEY_SUNPRO_VERSION)
  389. #undef JSON_HEDLEY_SUNPRO_VERSION
  390. #endif
  391. #if defined(__SUNPRO_C) && (__SUNPRO_C > 0x1000)
  392. #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)
  393. #elif defined(__SUNPRO_C)
  394. #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((__SUNPRO_C >> 8) & 0xf, (__SUNPRO_C >> 4) & 0xf, (__SUNPRO_C) & 0xf)
  395. #elif defined(__SUNPRO_CC) && (__SUNPRO_CC > 0x1000)
  396. #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)
  397. #elif defined(__SUNPRO_CC)
  398. #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((__SUNPRO_CC >> 8) & 0xf, (__SUNPRO_CC >> 4) & 0xf, (__SUNPRO_CC) & 0xf)
  399. #endif
  400. #if defined(JSON_HEDLEY_SUNPRO_VERSION_CHECK)
  401. #undef JSON_HEDLEY_SUNPRO_VERSION_CHECK
  402. #endif
  403. #if defined(JSON_HEDLEY_SUNPRO_VERSION)
  404. #define JSON_HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_SUNPRO_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  405. #else
  406. #define JSON_HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) (0)
  407. #endif
  408. #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION)
  409. #undef JSON_HEDLEY_EMSCRIPTEN_VERSION
  410. #endif
  411. #if defined(__EMSCRIPTEN__)
  412. #define JSON_HEDLEY_EMSCRIPTEN_VERSION JSON_HEDLEY_VERSION_ENCODE(__EMSCRIPTEN_major__, __EMSCRIPTEN_minor__, __EMSCRIPTEN_tiny__)
  413. #endif
  414. #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK)
  415. #undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK
  416. #endif
  417. #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION)
  418. #define JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_EMSCRIPTEN_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  419. #else
  420. #define JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) (0)
  421. #endif
  422. #if defined(JSON_HEDLEY_ARM_VERSION)
  423. #undef JSON_HEDLEY_ARM_VERSION
  424. #endif
  425. #if defined(__CC_ARM) && defined(__ARMCOMPILER_VERSION)
  426. #define JSON_HEDLEY_ARM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ARMCOMPILER_VERSION / 1000000, (__ARMCOMPILER_VERSION % 1000000) / 10000, (__ARMCOMPILER_VERSION % 10000) / 100)
  427. #elif defined(__CC_ARM) && defined(__ARMCC_VERSION)
  428. #define JSON_HEDLEY_ARM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ARMCC_VERSION / 1000000, (__ARMCC_VERSION % 1000000) / 10000, (__ARMCC_VERSION % 10000) / 100)
  429. #endif
  430. #if defined(JSON_HEDLEY_ARM_VERSION_CHECK)
  431. #undef JSON_HEDLEY_ARM_VERSION_CHECK
  432. #endif
  433. #if defined(JSON_HEDLEY_ARM_VERSION)
  434. #define JSON_HEDLEY_ARM_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_ARM_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  435. #else
  436. #define JSON_HEDLEY_ARM_VERSION_CHECK(major,minor,patch) (0)
  437. #endif
  438. #if defined(JSON_HEDLEY_IBM_VERSION)
  439. #undef JSON_HEDLEY_IBM_VERSION
  440. #endif
  441. #if defined(__ibmxl__)
  442. #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ibmxl_version__, __ibmxl_release__, __ibmxl_modification__)
  443. #elif defined(__xlC__) && defined(__xlC_ver__)
  444. #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__xlC__ >> 8, __xlC__ & 0xff, (__xlC_ver__ >> 8) & 0xff)
  445. #elif defined(__xlC__)
  446. #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__xlC__ >> 8, __xlC__ & 0xff, 0)
  447. #endif
  448. #if defined(JSON_HEDLEY_IBM_VERSION_CHECK)
  449. #undef JSON_HEDLEY_IBM_VERSION_CHECK
  450. #endif
  451. #if defined(JSON_HEDLEY_IBM_VERSION)
  452. #define JSON_HEDLEY_IBM_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_IBM_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  453. #else
  454. #define JSON_HEDLEY_IBM_VERSION_CHECK(major,minor,patch) (0)
  455. #endif
  456. #if defined(JSON_HEDLEY_TI_VERSION)
  457. #undef JSON_HEDLEY_TI_VERSION
  458. #endif
  459. #if \
  460. defined(__TI_COMPILER_VERSION__) && \
  461. ( \
  462. defined(__TMS470__) || defined(__TI_ARM__) || \
  463. defined(__MSP430__) || \
  464. defined(__TMS320C2000__) \
  465. )
  466. #if (__TI_COMPILER_VERSION__ >= 16000000)
  467. #define JSON_HEDLEY_TI_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  468. #endif
  469. #endif
  470. #if defined(JSON_HEDLEY_TI_VERSION_CHECK)
  471. #undef JSON_HEDLEY_TI_VERSION_CHECK
  472. #endif
  473. #if defined(JSON_HEDLEY_TI_VERSION)
  474. #define JSON_HEDLEY_TI_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  475. #else
  476. #define JSON_HEDLEY_TI_VERSION_CHECK(major,minor,patch) (0)
  477. #endif
  478. #if defined(JSON_HEDLEY_TI_CL2000_VERSION)
  479. #undef JSON_HEDLEY_TI_CL2000_VERSION
  480. #endif
  481. #if defined(__TI_COMPILER_VERSION__) && defined(__TMS320C2000__)
  482. #define JSON_HEDLEY_TI_CL2000_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  483. #endif
  484. #if defined(JSON_HEDLEY_TI_CL2000_VERSION_CHECK)
  485. #undef JSON_HEDLEY_TI_CL2000_VERSION_CHECK
  486. #endif
  487. #if defined(JSON_HEDLEY_TI_CL2000_VERSION)
  488. #define JSON_HEDLEY_TI_CL2000_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL2000_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  489. #else
  490. #define JSON_HEDLEY_TI_CL2000_VERSION_CHECK(major,minor,patch) (0)
  491. #endif
  492. #if defined(JSON_HEDLEY_TI_CL430_VERSION)
  493. #undef JSON_HEDLEY_TI_CL430_VERSION
  494. #endif
  495. #if defined(__TI_COMPILER_VERSION__) && defined(__MSP430__)
  496. #define JSON_HEDLEY_TI_CL430_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  497. #endif
  498. #if defined(JSON_HEDLEY_TI_CL430_VERSION_CHECK)
  499. #undef JSON_HEDLEY_TI_CL430_VERSION_CHECK
  500. #endif
  501. #if defined(JSON_HEDLEY_TI_CL430_VERSION)
  502. #define JSON_HEDLEY_TI_CL430_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL430_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  503. #else
  504. #define JSON_HEDLEY_TI_CL430_VERSION_CHECK(major,minor,patch) (0)
  505. #endif
  506. #if defined(JSON_HEDLEY_TI_ARMCL_VERSION)
  507. #undef JSON_HEDLEY_TI_ARMCL_VERSION
  508. #endif
  509. #if defined(__TI_COMPILER_VERSION__) && (defined(__TMS470__) || defined(__TI_ARM__))
  510. #define JSON_HEDLEY_TI_ARMCL_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  511. #endif
  512. #if defined(JSON_HEDLEY_TI_ARMCL_VERSION_CHECK)
  513. #undef JSON_HEDLEY_TI_ARMCL_VERSION_CHECK
  514. #endif
  515. #if defined(JSON_HEDLEY_TI_ARMCL_VERSION)
  516. #define JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_ARMCL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  517. #else
  518. #define JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(major,minor,patch) (0)
  519. #endif
  520. #if defined(JSON_HEDLEY_TI_CL6X_VERSION)
  521. #undef JSON_HEDLEY_TI_CL6X_VERSION
  522. #endif
  523. #if defined(__TI_COMPILER_VERSION__) && defined(__TMS320C6X__)
  524. #define JSON_HEDLEY_TI_CL6X_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  525. #endif
  526. #if defined(JSON_HEDLEY_TI_CL6X_VERSION_CHECK)
  527. #undef JSON_HEDLEY_TI_CL6X_VERSION_CHECK
  528. #endif
  529. #if defined(JSON_HEDLEY_TI_CL6X_VERSION)
  530. #define JSON_HEDLEY_TI_CL6X_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL6X_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  531. #else
  532. #define JSON_HEDLEY_TI_CL6X_VERSION_CHECK(major,minor,patch) (0)
  533. #endif
  534. #if defined(JSON_HEDLEY_TI_CL7X_VERSION)
  535. #undef JSON_HEDLEY_TI_CL7X_VERSION
  536. #endif
  537. #if defined(__TI_COMPILER_VERSION__) && defined(__C7000__)
  538. #define JSON_HEDLEY_TI_CL7X_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  539. #endif
  540. #if defined(JSON_HEDLEY_TI_CL7X_VERSION_CHECK)
  541. #undef JSON_HEDLEY_TI_CL7X_VERSION_CHECK
  542. #endif
  543. #if defined(JSON_HEDLEY_TI_CL7X_VERSION)
  544. #define JSON_HEDLEY_TI_CL7X_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL7X_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  545. #else
  546. #define JSON_HEDLEY_TI_CL7X_VERSION_CHECK(major,minor,patch) (0)
  547. #endif
  548. #if defined(JSON_HEDLEY_TI_CLPRU_VERSION)
  549. #undef JSON_HEDLEY_TI_CLPRU_VERSION
  550. #endif
  551. #if defined(__TI_COMPILER_VERSION__) && defined(__PRU__)
  552. #define JSON_HEDLEY_TI_CLPRU_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  553. #endif
  554. #if defined(JSON_HEDLEY_TI_CLPRU_VERSION_CHECK)
  555. #undef JSON_HEDLEY_TI_CLPRU_VERSION_CHECK
  556. #endif
  557. #if defined(JSON_HEDLEY_TI_CLPRU_VERSION)
  558. #define JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CLPRU_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  559. #else
  560. #define JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(major,minor,patch) (0)
  561. #endif
  562. #if defined(JSON_HEDLEY_CRAY_VERSION)
  563. #undef JSON_HEDLEY_CRAY_VERSION
  564. #endif
  565. #if defined(_CRAYC)
  566. #if defined(_RELEASE_PATCHLEVEL)
  567. #define JSON_HEDLEY_CRAY_VERSION JSON_HEDLEY_VERSION_ENCODE(_RELEASE_MAJOR, _RELEASE_MINOR, _RELEASE_PATCHLEVEL)
  568. #else
  569. #define JSON_HEDLEY_CRAY_VERSION JSON_HEDLEY_VERSION_ENCODE(_RELEASE_MAJOR, _RELEASE_MINOR, 0)
  570. #endif
  571. #endif
  572. #if defined(JSON_HEDLEY_CRAY_VERSION_CHECK)
  573. #undef JSON_HEDLEY_CRAY_VERSION_CHECK
  574. #endif
  575. #if defined(JSON_HEDLEY_CRAY_VERSION)
  576. #define JSON_HEDLEY_CRAY_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_CRAY_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  577. #else
  578. #define JSON_HEDLEY_CRAY_VERSION_CHECK(major,minor,patch) (0)
  579. #endif
  580. #if defined(JSON_HEDLEY_IAR_VERSION)
  581. #undef JSON_HEDLEY_IAR_VERSION
  582. #endif
  583. #if defined(__IAR_SYSTEMS_ICC__)
  584. #if __VER__ > 1000
  585. #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE((__VER__ / 1000000), ((__VER__ / 1000) % 1000), (__VER__ % 1000))
  586. #else
  587. #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE(__VER__ / 100, __VER__ % 100, 0)
  588. #endif
  589. #endif
  590. #if defined(JSON_HEDLEY_IAR_VERSION_CHECK)
  591. #undef JSON_HEDLEY_IAR_VERSION_CHECK
  592. #endif
  593. #if defined(JSON_HEDLEY_IAR_VERSION)
  594. #define JSON_HEDLEY_IAR_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_IAR_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  595. #else
  596. #define JSON_HEDLEY_IAR_VERSION_CHECK(major,minor,patch) (0)
  597. #endif
  598. #if defined(JSON_HEDLEY_TINYC_VERSION)
  599. #undef JSON_HEDLEY_TINYC_VERSION
  600. #endif
  601. #if defined(__TINYC__)
  602. #define JSON_HEDLEY_TINYC_VERSION JSON_HEDLEY_VERSION_ENCODE(__TINYC__ / 1000, (__TINYC__ / 100) % 10, __TINYC__ % 100)
  603. #endif
  604. #if defined(JSON_HEDLEY_TINYC_VERSION_CHECK)
  605. #undef JSON_HEDLEY_TINYC_VERSION_CHECK
  606. #endif
  607. #if defined(JSON_HEDLEY_TINYC_VERSION)
  608. #define JSON_HEDLEY_TINYC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TINYC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  609. #else
  610. #define JSON_HEDLEY_TINYC_VERSION_CHECK(major,minor,patch) (0)
  611. #endif
  612. #if defined(JSON_HEDLEY_DMC_VERSION)
  613. #undef JSON_HEDLEY_DMC_VERSION
  614. #endif
  615. #if defined(__DMC__)
  616. #define JSON_HEDLEY_DMC_VERSION JSON_HEDLEY_VERSION_ENCODE(__DMC__ >> 8, (__DMC__ >> 4) & 0xf, __DMC__ & 0xf)
  617. #endif
  618. #if defined(JSON_HEDLEY_DMC_VERSION_CHECK)
  619. #undef JSON_HEDLEY_DMC_VERSION_CHECK
  620. #endif
  621. #if defined(JSON_HEDLEY_DMC_VERSION)
  622. #define JSON_HEDLEY_DMC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_DMC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  623. #else
  624. #define JSON_HEDLEY_DMC_VERSION_CHECK(major,minor,patch) (0)
  625. #endif
  626. #if defined(JSON_HEDLEY_COMPCERT_VERSION)
  627. #undef JSON_HEDLEY_COMPCERT_VERSION
  628. #endif
  629. #if defined(__COMPCERT_VERSION__)
  630. #define JSON_HEDLEY_COMPCERT_VERSION JSON_HEDLEY_VERSION_ENCODE(__COMPCERT_VERSION__ / 10000, (__COMPCERT_VERSION__ / 100) % 100, __COMPCERT_VERSION__ % 100)
  631. #endif
  632. #if defined(JSON_HEDLEY_COMPCERT_VERSION_CHECK)
  633. #undef JSON_HEDLEY_COMPCERT_VERSION_CHECK
  634. #endif
  635. #if defined(JSON_HEDLEY_COMPCERT_VERSION)
  636. #define JSON_HEDLEY_COMPCERT_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_COMPCERT_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  637. #else
  638. #define JSON_HEDLEY_COMPCERT_VERSION_CHECK(major,minor,patch) (0)
  639. #endif
  640. #if defined(JSON_HEDLEY_PELLES_VERSION)
  641. #undef JSON_HEDLEY_PELLES_VERSION
  642. #endif
  643. #if defined(__POCC__)
  644. #define JSON_HEDLEY_PELLES_VERSION JSON_HEDLEY_VERSION_ENCODE(__POCC__ / 100, __POCC__ % 100, 0)
  645. #endif
  646. #if defined(JSON_HEDLEY_PELLES_VERSION_CHECK)
  647. #undef JSON_HEDLEY_PELLES_VERSION_CHECK
  648. #endif
  649. #if defined(JSON_HEDLEY_PELLES_VERSION)
  650. #define JSON_HEDLEY_PELLES_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_PELLES_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  651. #else
  652. #define JSON_HEDLEY_PELLES_VERSION_CHECK(major,minor,patch) (0)
  653. #endif
  654. #if defined(JSON_HEDLEY_MCST_LCC_VERSION)
  655. #undef JSON_HEDLEY_MCST_LCC_VERSION
  656. #endif
  657. #if defined(__LCC__) && defined(__LCC_MINOR__)
  658. #define JSON_HEDLEY_MCST_LCC_VERSION JSON_HEDLEY_VERSION_ENCODE(__LCC__ / 100, __LCC__ % 100, __LCC_MINOR__)
  659. #endif
  660. #if defined(JSON_HEDLEY_MCST_LCC_VERSION_CHECK)
  661. #undef JSON_HEDLEY_MCST_LCC_VERSION_CHECK
  662. #endif
  663. #if defined(JSON_HEDLEY_MCST_LCC_VERSION)
  664. #define JSON_HEDLEY_MCST_LCC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_MCST_LCC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  665. #else
  666. #define JSON_HEDLEY_MCST_LCC_VERSION_CHECK(major,minor,patch) (0)
  667. #endif
  668. #if defined(JSON_HEDLEY_GCC_VERSION)
  669. #undef JSON_HEDLEY_GCC_VERSION
  670. #endif
  671. #if \
  672. defined(JSON_HEDLEY_GNUC_VERSION) && \
  673. !defined(__clang__) && \
  674. !defined(JSON_HEDLEY_INTEL_VERSION) && \
  675. !defined(JSON_HEDLEY_PGI_VERSION) && \
  676. !defined(JSON_HEDLEY_ARM_VERSION) && \
  677. !defined(JSON_HEDLEY_CRAY_VERSION) && \
  678. !defined(JSON_HEDLEY_TI_VERSION) && \
  679. !defined(JSON_HEDLEY_TI_ARMCL_VERSION) && \
  680. !defined(JSON_HEDLEY_TI_CL430_VERSION) && \
  681. !defined(JSON_HEDLEY_TI_CL2000_VERSION) && \
  682. !defined(JSON_HEDLEY_TI_CL6X_VERSION) && \
  683. !defined(JSON_HEDLEY_TI_CL7X_VERSION) && \
  684. !defined(JSON_HEDLEY_TI_CLPRU_VERSION) && \
  685. !defined(__COMPCERT__) && \
  686. !defined(JSON_HEDLEY_MCST_LCC_VERSION)
  687. #define JSON_HEDLEY_GCC_VERSION JSON_HEDLEY_GNUC_VERSION
  688. #endif
  689. #if defined(JSON_HEDLEY_GCC_VERSION_CHECK)
  690. #undef JSON_HEDLEY_GCC_VERSION_CHECK
  691. #endif
  692. #if defined(JSON_HEDLEY_GCC_VERSION)
  693. #define JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_GCC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  694. #else
  695. #define JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (0)
  696. #endif
  697. #if defined(JSON_HEDLEY_HAS_ATTRIBUTE)
  698. #undef JSON_HEDLEY_HAS_ATTRIBUTE
  699. #endif
  700. #if \
  701. defined(__has_attribute) && \
  702. ( \
  703. (!defined(JSON_HEDLEY_IAR_VERSION) || JSON_HEDLEY_IAR_VERSION_CHECK(8,5,9)) \
  704. )
  705. # define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) __has_attribute(attribute)
  706. #else
  707. # define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) (0)
  708. #endif
  709. #if defined(JSON_HEDLEY_GNUC_HAS_ATTRIBUTE)
  710. #undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE
  711. #endif
  712. #if defined(__has_attribute)
  713. #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
  714. #else
  715. #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  716. #endif
  717. #if defined(JSON_HEDLEY_GCC_HAS_ATTRIBUTE)
  718. #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE
  719. #endif
  720. #if defined(__has_attribute)
  721. #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
  722. #else
  723. #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  724. #endif
  725. #if defined(JSON_HEDLEY_HAS_CPP_ATTRIBUTE)
  726. #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE
  727. #endif
  728. #if \
  729. defined(__has_cpp_attribute) && \
  730. defined(__cplusplus) && \
  731. (!defined(JSON_HEDLEY_SUNPRO_VERSION) || JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0))
  732. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) __has_cpp_attribute(attribute)
  733. #else
  734. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) (0)
  735. #endif
  736. #if defined(JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS)
  737. #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS
  738. #endif
  739. #if !defined(__cplusplus) || !defined(__has_cpp_attribute)
  740. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) (0)
  741. #elif \
  742. !defined(JSON_HEDLEY_PGI_VERSION) && \
  743. !defined(JSON_HEDLEY_IAR_VERSION) && \
  744. (!defined(JSON_HEDLEY_SUNPRO_VERSION) || JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0)) && \
  745. (!defined(JSON_HEDLEY_MSVC_VERSION) || JSON_HEDLEY_MSVC_VERSION_CHECK(19,20,0))
  746. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) JSON_HEDLEY_HAS_CPP_ATTRIBUTE(ns::attribute)
  747. #else
  748. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) (0)
  749. #endif
  750. #if defined(JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE)
  751. #undef JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE
  752. #endif
  753. #if defined(__has_cpp_attribute) && defined(__cplusplus)
  754. #define JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) __has_cpp_attribute(attribute)
  755. #else
  756. #define JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  757. #endif
  758. #if defined(JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE)
  759. #undef JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE
  760. #endif
  761. #if defined(__has_cpp_attribute) && defined(__cplusplus)
  762. #define JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) __has_cpp_attribute(attribute)
  763. #else
  764. #define JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  765. #endif
  766. #if defined(JSON_HEDLEY_HAS_BUILTIN)
  767. #undef JSON_HEDLEY_HAS_BUILTIN
  768. #endif
  769. #if defined(__has_builtin)
  770. #define JSON_HEDLEY_HAS_BUILTIN(builtin) __has_builtin(builtin)
  771. #else
  772. #define JSON_HEDLEY_HAS_BUILTIN(builtin) (0)
  773. #endif
  774. #if defined(JSON_HEDLEY_GNUC_HAS_BUILTIN)
  775. #undef JSON_HEDLEY_GNUC_HAS_BUILTIN
  776. #endif
  777. #if defined(__has_builtin)
  778. #define JSON_HEDLEY_GNUC_HAS_BUILTIN(builtin,major,minor,patch) __has_builtin(builtin)
  779. #else
  780. #define JSON_HEDLEY_GNUC_HAS_BUILTIN(builtin,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  781. #endif
  782. #if defined(JSON_HEDLEY_GCC_HAS_BUILTIN)
  783. #undef JSON_HEDLEY_GCC_HAS_BUILTIN
  784. #endif
  785. #if defined(__has_builtin)
  786. #define JSON_HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) __has_builtin(builtin)
  787. #else
  788. #define JSON_HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  789. #endif
  790. #if defined(JSON_HEDLEY_HAS_FEATURE)
  791. #undef JSON_HEDLEY_HAS_FEATURE
  792. #endif
  793. #if defined(__has_feature)
  794. #define JSON_HEDLEY_HAS_FEATURE(feature) __has_feature(feature)
  795. #else
  796. #define JSON_HEDLEY_HAS_FEATURE(feature) (0)
  797. #endif
  798. #if defined(JSON_HEDLEY_GNUC_HAS_FEATURE)
  799. #undef JSON_HEDLEY_GNUC_HAS_FEATURE
  800. #endif
  801. #if defined(__has_feature)
  802. #define JSON_HEDLEY_GNUC_HAS_FEATURE(feature,major,minor,patch) __has_feature(feature)
  803. #else
  804. #define JSON_HEDLEY_GNUC_HAS_FEATURE(feature,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  805. #endif
  806. #if defined(JSON_HEDLEY_GCC_HAS_FEATURE)
  807. #undef JSON_HEDLEY_GCC_HAS_FEATURE
  808. #endif
  809. #if defined(__has_feature)
  810. #define JSON_HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) __has_feature(feature)
  811. #else
  812. #define JSON_HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  813. #endif
  814. #if defined(JSON_HEDLEY_HAS_EXTENSION)
  815. #undef JSON_HEDLEY_HAS_EXTENSION
  816. #endif
  817. #if defined(__has_extension)
  818. #define JSON_HEDLEY_HAS_EXTENSION(extension) __has_extension(extension)
  819. #else
  820. #define JSON_HEDLEY_HAS_EXTENSION(extension) (0)
  821. #endif
  822. #if defined(JSON_HEDLEY_GNUC_HAS_EXTENSION)
  823. #undef JSON_HEDLEY_GNUC_HAS_EXTENSION
  824. #endif
  825. #if defined(__has_extension)
  826. #define JSON_HEDLEY_GNUC_HAS_EXTENSION(extension,major,minor,patch) __has_extension(extension)
  827. #else
  828. #define JSON_HEDLEY_GNUC_HAS_EXTENSION(extension,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  829. #endif
  830. #if defined(JSON_HEDLEY_GCC_HAS_EXTENSION)
  831. #undef JSON_HEDLEY_GCC_HAS_EXTENSION
  832. #endif
  833. #if defined(__has_extension)
  834. #define JSON_HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) __has_extension(extension)
  835. #else
  836. #define JSON_HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  837. #endif
  838. #if defined(JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE)
  839. #undef JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE
  840. #endif
  841. #if defined(__has_declspec_attribute)
  842. #define JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) __has_declspec_attribute(attribute)
  843. #else
  844. #define JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) (0)
  845. #endif
  846. #if defined(JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE)
  847. #undef JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE
  848. #endif
  849. #if defined(__has_declspec_attribute)
  850. #define JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) __has_declspec_attribute(attribute)
  851. #else
  852. #define JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  853. #endif
  854. #if defined(JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE)
  855. #undef JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE
  856. #endif
  857. #if defined(__has_declspec_attribute)
  858. #define JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) __has_declspec_attribute(attribute)
  859. #else
  860. #define JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  861. #endif
  862. #if defined(JSON_HEDLEY_HAS_WARNING)
  863. #undef JSON_HEDLEY_HAS_WARNING
  864. #endif
  865. #if defined(__has_warning)
  866. #define JSON_HEDLEY_HAS_WARNING(warning) __has_warning(warning)
  867. #else
  868. #define JSON_HEDLEY_HAS_WARNING(warning) (0)
  869. #endif
  870. #if defined(JSON_HEDLEY_GNUC_HAS_WARNING)
  871. #undef JSON_HEDLEY_GNUC_HAS_WARNING
  872. #endif
  873. #if defined(__has_warning)
  874. #define JSON_HEDLEY_GNUC_HAS_WARNING(warning,major,minor,patch) __has_warning(warning)
  875. #else
  876. #define JSON_HEDLEY_GNUC_HAS_WARNING(warning,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  877. #endif
  878. #if defined(JSON_HEDLEY_GCC_HAS_WARNING)
  879. #undef JSON_HEDLEY_GCC_HAS_WARNING
  880. #endif
  881. #if defined(__has_warning)
  882. #define JSON_HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) __has_warning(warning)
  883. #else
  884. #define JSON_HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  885. #endif
  886. #if \
  887. (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
  888. defined(__clang__) || \
  889. JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \
  890. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  891. JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \
  892. JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \
  893. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  894. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  895. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,7,0) || \
  896. JSON_HEDLEY_TI_CL430_VERSION_CHECK(2,0,1) || \
  897. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,1,0) || \
  898. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,0,0) || \
  899. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  900. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  901. JSON_HEDLEY_CRAY_VERSION_CHECK(5,0,0) || \
  902. JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,17) || \
  903. JSON_HEDLEY_SUNPRO_VERSION_CHECK(8,0,0) || \
  904. (JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) && defined(__C99_PRAGMA_OPERATOR))
  905. #define JSON_HEDLEY_PRAGMA(value) _Pragma(#value)
  906. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
  907. #define JSON_HEDLEY_PRAGMA(value) __pragma(value)
  908. #else
  909. #define JSON_HEDLEY_PRAGMA(value)
  910. #endif
  911. #if defined(JSON_HEDLEY_DIAGNOSTIC_PUSH)
  912. #undef JSON_HEDLEY_DIAGNOSTIC_PUSH
  913. #endif
  914. #if defined(JSON_HEDLEY_DIAGNOSTIC_POP)
  915. #undef JSON_HEDLEY_DIAGNOSTIC_POP
  916. #endif
  917. #if defined(__clang__)
  918. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("clang diagnostic push")
  919. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("clang diagnostic pop")
  920. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  921. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)")
  922. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)")
  923. #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0)
  924. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push")
  925. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("GCC diagnostic pop")
  926. #elif \
  927. JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) || \
  928. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  929. #define JSON_HEDLEY_DIAGNOSTIC_PUSH __pragma(warning(push))
  930. #define JSON_HEDLEY_DIAGNOSTIC_POP __pragma(warning(pop))
  931. #elif JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0)
  932. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("push")
  933. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("pop")
  934. #elif \
  935. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  936. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  937. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,4,0) || \
  938. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,1,0) || \
  939. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  940. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0)
  941. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("diag_push")
  942. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("diag_pop")
  943. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,90,0)
  944. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)")
  945. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)")
  946. #else
  947. #define JSON_HEDLEY_DIAGNOSTIC_PUSH
  948. #define JSON_HEDLEY_DIAGNOSTIC_POP
  949. #endif
  950. /* JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_ is for
  951. HEDLEY INTERNAL USE ONLY. API subject to change without notice. */
  952. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_)
  953. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_
  954. #endif
  955. #if defined(__cplusplus)
  956. # if JSON_HEDLEY_HAS_WARNING("-Wc++98-compat")
  957. # if JSON_HEDLEY_HAS_WARNING("-Wc++17-extensions")
  958. # if JSON_HEDLEY_HAS_WARNING("-Wc++1z-extensions")
  959. # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
  960. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  961. _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
  962. _Pragma("clang diagnostic ignored \"-Wc++17-extensions\"") \
  963. _Pragma("clang diagnostic ignored \"-Wc++1z-extensions\"") \
  964. xpr \
  965. JSON_HEDLEY_DIAGNOSTIC_POP
  966. # else
  967. # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
  968. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  969. _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
  970. _Pragma("clang diagnostic ignored \"-Wc++17-extensions\"") \
  971. xpr \
  972. JSON_HEDLEY_DIAGNOSTIC_POP
  973. # endif
  974. # else
  975. # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
  976. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  977. _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
  978. xpr \
  979. JSON_HEDLEY_DIAGNOSTIC_POP
  980. # endif
  981. # endif
  982. #endif
  983. #if !defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_)
  984. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(x) x
  985. #endif
  986. #if defined(JSON_HEDLEY_CONST_CAST)
  987. #undef JSON_HEDLEY_CONST_CAST
  988. #endif
  989. #if defined(__cplusplus)
  990. # define JSON_HEDLEY_CONST_CAST(T, expr) (const_cast<T>(expr))
  991. #elif \
  992. JSON_HEDLEY_HAS_WARNING("-Wcast-qual") || \
  993. JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0) || \
  994. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  995. # define JSON_HEDLEY_CONST_CAST(T, expr) (__extension__ ({ \
  996. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  997. JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL \
  998. ((T) (expr)); \
  999. JSON_HEDLEY_DIAGNOSTIC_POP \
  1000. }))
  1001. #else
  1002. # define JSON_HEDLEY_CONST_CAST(T, expr) ((T) (expr))
  1003. #endif
  1004. #if defined(JSON_HEDLEY_REINTERPRET_CAST)
  1005. #undef JSON_HEDLEY_REINTERPRET_CAST
  1006. #endif
  1007. #if defined(__cplusplus)
  1008. #define JSON_HEDLEY_REINTERPRET_CAST(T, expr) (reinterpret_cast<T>(expr))
  1009. #else
  1010. #define JSON_HEDLEY_REINTERPRET_CAST(T, expr) ((T) (expr))
  1011. #endif
  1012. #if defined(JSON_HEDLEY_STATIC_CAST)
  1013. #undef JSON_HEDLEY_STATIC_CAST
  1014. #endif
  1015. #if defined(__cplusplus)
  1016. #define JSON_HEDLEY_STATIC_CAST(T, expr) (static_cast<T>(expr))
  1017. #else
  1018. #define JSON_HEDLEY_STATIC_CAST(T, expr) ((T) (expr))
  1019. #endif
  1020. #if defined(JSON_HEDLEY_CPP_CAST)
  1021. #undef JSON_HEDLEY_CPP_CAST
  1022. #endif
  1023. #if defined(__cplusplus)
  1024. # if JSON_HEDLEY_HAS_WARNING("-Wold-style-cast")
  1025. # define JSON_HEDLEY_CPP_CAST(T, expr) \
  1026. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  1027. _Pragma("clang diagnostic ignored \"-Wold-style-cast\"") \
  1028. ((T) (expr)) \
  1029. JSON_HEDLEY_DIAGNOSTIC_POP
  1030. # elif JSON_HEDLEY_IAR_VERSION_CHECK(8,3,0)
  1031. # define JSON_HEDLEY_CPP_CAST(T, expr) \
  1032. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  1033. _Pragma("diag_suppress=Pe137") \
  1034. JSON_HEDLEY_DIAGNOSTIC_POP
  1035. # else
  1036. # define JSON_HEDLEY_CPP_CAST(T, expr) ((T) (expr))
  1037. # endif
  1038. #else
  1039. # define JSON_HEDLEY_CPP_CAST(T, expr) (expr)
  1040. #endif
  1041. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED)
  1042. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
  1043. #endif
  1044. #if JSON_HEDLEY_HAS_WARNING("-Wdeprecated-declarations")
  1045. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"")
  1046. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  1047. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warning(disable:1478 1786)")
  1048. #elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1049. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:1478 1786))
  1050. #elif JSON_HEDLEY_PGI_VERSION_CHECK(20,7,0)
  1051. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1216,1444,1445")
  1052. #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
  1053. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1444")
  1054. #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0)
  1055. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
  1056. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
  1057. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:4996))
  1058. #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1059. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1444")
  1060. #elif \
  1061. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1062. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1063. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1064. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1065. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1066. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1067. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1068. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1069. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1070. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1071. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0)
  1072. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1291,1718")
  1073. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) && !defined(__cplusplus)
  1074. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("error_messages(off,E_DEPRECATED_ATT,E_DEPRECATED_ATT_MESS)")
  1075. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) && defined(__cplusplus)
  1076. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("error_messages(off,symdeprecated,symdeprecated2)")
  1077. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1078. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress=Pe1444,Pe1215")
  1079. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,90,0)
  1080. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warn(disable:2241)")
  1081. #else
  1082. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
  1083. #endif
  1084. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS)
  1085. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
  1086. #endif
  1087. #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
  1088. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("clang diagnostic ignored \"-Wunknown-pragmas\"")
  1089. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  1090. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("warning(disable:161)")
  1091. #elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1092. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS __pragma(warning(disable:161))
  1093. #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
  1094. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 1675")
  1095. #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0)
  1096. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("GCC diagnostic ignored \"-Wunknown-pragmas\"")
  1097. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
  1098. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS __pragma(warning(disable:4068))
  1099. #elif \
  1100. JSON_HEDLEY_TI_VERSION_CHECK(16,9,0) || \
  1101. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) || \
  1102. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1103. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0)
  1104. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163")
  1105. #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0)
  1106. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163")
  1107. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1108. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress=Pe161")
  1109. #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1110. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 161")
  1111. #else
  1112. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
  1113. #endif
  1114. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES)
  1115. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
  1116. #endif
  1117. #if JSON_HEDLEY_HAS_WARNING("-Wunknown-attributes")
  1118. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("clang diagnostic ignored \"-Wunknown-attributes\"")
  1119. #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0)
  1120. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
  1121. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(17,0,0)
  1122. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("warning(disable:1292)")
  1123. #elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1124. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES __pragma(warning(disable:1292))
  1125. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,0)
  1126. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES __pragma(warning(disable:5030))
  1127. #elif JSON_HEDLEY_PGI_VERSION_CHECK(20,7,0)
  1128. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097,1098")
  1129. #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
  1130. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097")
  1131. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,14,0) && defined(__cplusplus)
  1132. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("error_messages(off,attrskipunsup)")
  1133. #elif \
  1134. JSON_HEDLEY_TI_VERSION_CHECK(18,1,0) || \
  1135. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,3,0) || \
  1136. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0)
  1137. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1173")
  1138. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1139. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress=Pe1097")
  1140. #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1141. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097")
  1142. #else
  1143. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
  1144. #endif
  1145. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL)
  1146. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
  1147. #endif
  1148. #if JSON_HEDLEY_HAS_WARNING("-Wcast-qual")
  1149. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("clang diagnostic ignored \"-Wcast-qual\"")
  1150. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  1151. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("warning(disable:2203 2331)")
  1152. #elif JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0)
  1153. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("GCC diagnostic ignored \"-Wcast-qual\"")
  1154. #else
  1155. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
  1156. #endif
  1157. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION)
  1158. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
  1159. #endif
  1160. #if JSON_HEDLEY_HAS_WARNING("-Wunused-function")
  1161. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("clang diagnostic ignored \"-Wunused-function\"")
  1162. #elif JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0)
  1163. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("GCC diagnostic ignored \"-Wunused-function\"")
  1164. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(1,0,0)
  1165. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION __pragma(warning(disable:4505))
  1166. #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1167. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("diag_suppress 3142")
  1168. #else
  1169. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
  1170. #endif
  1171. #if defined(JSON_HEDLEY_DEPRECATED)
  1172. #undef JSON_HEDLEY_DEPRECATED
  1173. #endif
  1174. #if defined(JSON_HEDLEY_DEPRECATED_FOR)
  1175. #undef JSON_HEDLEY_DEPRECATED_FOR
  1176. #endif
  1177. #if \
  1178. JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
  1179. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1180. #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated("Since " # since))
  1181. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated("Since " #since "; use " #replacement))
  1182. #elif \
  1183. (JSON_HEDLEY_HAS_EXTENSION(attribute_deprecated_with_message) && !defined(JSON_HEDLEY_IAR_VERSION)) || \
  1184. JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \
  1185. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1186. JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) || \
  1187. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) || \
  1188. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1189. JSON_HEDLEY_TI_VERSION_CHECK(18,1,0) || \
  1190. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(18,1,0) || \
  1191. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,3,0) || \
  1192. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1193. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0) || \
  1194. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1195. #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__("Since " #since)))
  1196. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__("Since " #since "; use " #replacement)))
  1197. #elif defined(__cplusplus) && (__cplusplus >= 201402L)
  1198. #define JSON_HEDLEY_DEPRECATED(since) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since)]])
  1199. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since "; use " #replacement)]])
  1200. #elif \
  1201. JSON_HEDLEY_HAS_ATTRIBUTE(deprecated) || \
  1202. JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
  1203. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1204. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1205. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1206. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1207. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1208. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1209. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1210. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1211. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1212. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1213. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1214. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1215. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \
  1216. JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
  1217. #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__))
  1218. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__))
  1219. #elif \
  1220. JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
  1221. JSON_HEDLEY_PELLES_VERSION_CHECK(6,50,0) || \
  1222. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1223. #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated)
  1224. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated)
  1225. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1226. #define JSON_HEDLEY_DEPRECATED(since) _Pragma("deprecated")
  1227. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) _Pragma("deprecated")
  1228. #else
  1229. #define JSON_HEDLEY_DEPRECATED(since)
  1230. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement)
  1231. #endif
  1232. #if defined(JSON_HEDLEY_UNAVAILABLE)
  1233. #undef JSON_HEDLEY_UNAVAILABLE
  1234. #endif
  1235. #if \
  1236. JSON_HEDLEY_HAS_ATTRIBUTE(warning) || \
  1237. JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0) || \
  1238. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1239. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1240. #define JSON_HEDLEY_UNAVAILABLE(available_since) __attribute__((__warning__("Not available until " #available_since)))
  1241. #else
  1242. #define JSON_HEDLEY_UNAVAILABLE(available_since)
  1243. #endif
  1244. #if defined(JSON_HEDLEY_WARN_UNUSED_RESULT)
  1245. #undef JSON_HEDLEY_WARN_UNUSED_RESULT
  1246. #endif
  1247. #if defined(JSON_HEDLEY_WARN_UNUSED_RESULT_MSG)
  1248. #undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG
  1249. #endif
  1250. #if \
  1251. JSON_HEDLEY_HAS_ATTRIBUTE(warn_unused_result) || \
  1252. JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
  1253. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1254. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1255. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1256. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1257. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1258. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1259. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1260. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1261. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1262. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1263. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1264. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1265. (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)) || \
  1266. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1267. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1268. #define JSON_HEDLEY_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
  1269. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) __attribute__((__warn_unused_result__))
  1270. #elif (JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard) >= 201907L)
  1271. #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
  1272. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard(msg)]])
  1273. #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard)
  1274. #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
  1275. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
  1276. #elif defined(_Check_return_) /* SAL */
  1277. #define JSON_HEDLEY_WARN_UNUSED_RESULT _Check_return_
  1278. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) _Check_return_
  1279. #else
  1280. #define JSON_HEDLEY_WARN_UNUSED_RESULT
  1281. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg)
  1282. #endif
  1283. #if defined(JSON_HEDLEY_SENTINEL)
  1284. #undef JSON_HEDLEY_SENTINEL
  1285. #endif
  1286. #if \
  1287. JSON_HEDLEY_HAS_ATTRIBUTE(sentinel) || \
  1288. JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
  1289. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1290. JSON_HEDLEY_ARM_VERSION_CHECK(5,4,0) || \
  1291. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1292. #define JSON_HEDLEY_SENTINEL(position) __attribute__((__sentinel__(position)))
  1293. #else
  1294. #define JSON_HEDLEY_SENTINEL(position)
  1295. #endif
  1296. #if defined(JSON_HEDLEY_NO_RETURN)
  1297. #undef JSON_HEDLEY_NO_RETURN
  1298. #endif
  1299. #if JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1300. #define JSON_HEDLEY_NO_RETURN __noreturn
  1301. #elif \
  1302. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1303. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1304. #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__))
  1305. #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
  1306. #define JSON_HEDLEY_NO_RETURN _Noreturn
  1307. #elif defined(__cplusplus) && (__cplusplus >= 201103L)
  1308. #define JSON_HEDLEY_NO_RETURN JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[noreturn]])
  1309. #elif \
  1310. JSON_HEDLEY_HAS_ATTRIBUTE(noreturn) || \
  1311. JSON_HEDLEY_GCC_VERSION_CHECK(3,2,0) || \
  1312. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1313. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1314. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1315. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1316. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1317. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1318. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1319. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1320. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1321. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1322. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1323. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1324. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1325. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1326. JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
  1327. #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__))
  1328. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
  1329. #define JSON_HEDLEY_NO_RETURN _Pragma("does_not_return")
  1330. #elif \
  1331. JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
  1332. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1333. #define JSON_HEDLEY_NO_RETURN __declspec(noreturn)
  1334. #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,0,0) && defined(__cplusplus)
  1335. #define JSON_HEDLEY_NO_RETURN _Pragma("FUNC_NEVER_RETURNS;")
  1336. #elif JSON_HEDLEY_COMPCERT_VERSION_CHECK(3,2,0)
  1337. #define JSON_HEDLEY_NO_RETURN __attribute((noreturn))
  1338. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(9,0,0)
  1339. #define JSON_HEDLEY_NO_RETURN __declspec(noreturn)
  1340. #else
  1341. #define JSON_HEDLEY_NO_RETURN
  1342. #endif
  1343. #if defined(JSON_HEDLEY_NO_ESCAPE)
  1344. #undef JSON_HEDLEY_NO_ESCAPE
  1345. #endif
  1346. #if JSON_HEDLEY_HAS_ATTRIBUTE(noescape)
  1347. #define JSON_HEDLEY_NO_ESCAPE __attribute__((__noescape__))
  1348. #else
  1349. #define JSON_HEDLEY_NO_ESCAPE
  1350. #endif
  1351. #if defined(JSON_HEDLEY_UNREACHABLE)
  1352. #undef JSON_HEDLEY_UNREACHABLE
  1353. #endif
  1354. #if defined(JSON_HEDLEY_UNREACHABLE_RETURN)
  1355. #undef JSON_HEDLEY_UNREACHABLE_RETURN
  1356. #endif
  1357. #if defined(JSON_HEDLEY_ASSUME)
  1358. #undef JSON_HEDLEY_ASSUME
  1359. #endif
  1360. #if \
  1361. JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
  1362. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1363. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1364. #define JSON_HEDLEY_ASSUME(expr) __assume(expr)
  1365. #elif JSON_HEDLEY_HAS_BUILTIN(__builtin_assume)
  1366. #define JSON_HEDLEY_ASSUME(expr) __builtin_assume(expr)
  1367. #elif \
  1368. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
  1369. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0)
  1370. #if defined(__cplusplus)
  1371. #define JSON_HEDLEY_ASSUME(expr) std::_nassert(expr)
  1372. #else
  1373. #define JSON_HEDLEY_ASSUME(expr) _nassert(expr)
  1374. #endif
  1375. #endif
  1376. #if \
  1377. (JSON_HEDLEY_HAS_BUILTIN(__builtin_unreachable) && (!defined(JSON_HEDLEY_ARM_VERSION))) || \
  1378. JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \
  1379. JSON_HEDLEY_PGI_VERSION_CHECK(18,10,0) || \
  1380. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1381. JSON_HEDLEY_IBM_VERSION_CHECK(13,1,5) || \
  1382. JSON_HEDLEY_CRAY_VERSION_CHECK(10,0,0) || \
  1383. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1384. #define JSON_HEDLEY_UNREACHABLE() __builtin_unreachable()
  1385. #elif defined(JSON_HEDLEY_ASSUME)
  1386. #define JSON_HEDLEY_UNREACHABLE() JSON_HEDLEY_ASSUME(0)
  1387. #endif
  1388. #if !defined(JSON_HEDLEY_ASSUME)
  1389. #if defined(JSON_HEDLEY_UNREACHABLE)
  1390. #define JSON_HEDLEY_ASSUME(expr) JSON_HEDLEY_STATIC_CAST(void, ((expr) ? 1 : (JSON_HEDLEY_UNREACHABLE(), 1)))
  1391. #else
  1392. #define JSON_HEDLEY_ASSUME(expr) JSON_HEDLEY_STATIC_CAST(void, expr)
  1393. #endif
  1394. #endif
  1395. #if defined(JSON_HEDLEY_UNREACHABLE)
  1396. #if \
  1397. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
  1398. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0)
  1399. #define JSON_HEDLEY_UNREACHABLE_RETURN(value) return (JSON_HEDLEY_STATIC_CAST(void, JSON_HEDLEY_ASSUME(0)), (value))
  1400. #else
  1401. #define JSON_HEDLEY_UNREACHABLE_RETURN(value) JSON_HEDLEY_UNREACHABLE()
  1402. #endif
  1403. #else
  1404. #define JSON_HEDLEY_UNREACHABLE_RETURN(value) return (value)
  1405. #endif
  1406. #if !defined(JSON_HEDLEY_UNREACHABLE)
  1407. #define JSON_HEDLEY_UNREACHABLE() JSON_HEDLEY_ASSUME(0)
  1408. #endif
  1409. JSON_HEDLEY_DIAGNOSTIC_PUSH
  1410. #if JSON_HEDLEY_HAS_WARNING("-Wpedantic")
  1411. #pragma clang diagnostic ignored "-Wpedantic"
  1412. #endif
  1413. #if JSON_HEDLEY_HAS_WARNING("-Wc++98-compat-pedantic") && defined(__cplusplus)
  1414. #pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
  1415. #endif
  1416. #if JSON_HEDLEY_GCC_HAS_WARNING("-Wvariadic-macros",4,0,0)
  1417. #if defined(__clang__)
  1418. #pragma clang diagnostic ignored "-Wvariadic-macros"
  1419. #elif defined(JSON_HEDLEY_GCC_VERSION)
  1420. #pragma GCC diagnostic ignored "-Wvariadic-macros"
  1421. #endif
  1422. #endif
  1423. #if defined(JSON_HEDLEY_NON_NULL)
  1424. #undef JSON_HEDLEY_NON_NULL
  1425. #endif
  1426. #if \
  1427. JSON_HEDLEY_HAS_ATTRIBUTE(nonnull) || \
  1428. JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
  1429. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1430. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0)
  1431. #define JSON_HEDLEY_NON_NULL(...) __attribute__((__nonnull__(__VA_ARGS__)))
  1432. #else
  1433. #define JSON_HEDLEY_NON_NULL(...)
  1434. #endif
  1435. JSON_HEDLEY_DIAGNOSTIC_POP
  1436. #if defined(JSON_HEDLEY_PRINTF_FORMAT)
  1437. #undef JSON_HEDLEY_PRINTF_FORMAT
  1438. #endif
  1439. #if defined(__MINGW32__) && JSON_HEDLEY_GCC_HAS_ATTRIBUTE(format,4,4,0) && !defined(__USE_MINGW_ANSI_STDIO)
  1440. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(ms_printf, string_idx, first_to_check)))
  1441. #elif 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__(gnu_printf, string_idx, first_to_check)))
  1443. #elif \
  1444. JSON_HEDLEY_HAS_ATTRIBUTE(format) || \
  1445. JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
  1446. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1447. JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) || \
  1448. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1449. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1450. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1451. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1452. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1453. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1454. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1455. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1456. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1457. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1458. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1459. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1460. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1461. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(__printf__, string_idx, first_to_check)))
  1462. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(6,0,0)
  1463. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __declspec(vaformat(printf,string_idx,first_to_check))
  1464. #else
  1465. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check)
  1466. #endif
  1467. #if defined(JSON_HEDLEY_CONSTEXPR)
  1468. #undef JSON_HEDLEY_CONSTEXPR
  1469. #endif
  1470. #if defined(__cplusplus)
  1471. #if __cplusplus >= 201103L
  1472. #define JSON_HEDLEY_CONSTEXPR JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(constexpr)
  1473. #endif
  1474. #endif
  1475. #if !defined(JSON_HEDLEY_CONSTEXPR)
  1476. #define JSON_HEDLEY_CONSTEXPR
  1477. #endif
  1478. #if defined(JSON_HEDLEY_PREDICT)
  1479. #undef JSON_HEDLEY_PREDICT
  1480. #endif
  1481. #if defined(JSON_HEDLEY_LIKELY)
  1482. #undef JSON_HEDLEY_LIKELY
  1483. #endif
  1484. #if defined(JSON_HEDLEY_UNLIKELY)
  1485. #undef JSON_HEDLEY_UNLIKELY
  1486. #endif
  1487. #if defined(JSON_HEDLEY_UNPREDICTABLE)
  1488. #undef JSON_HEDLEY_UNPREDICTABLE
  1489. #endif
  1490. #if JSON_HEDLEY_HAS_BUILTIN(__builtin_unpredictable)
  1491. #define JSON_HEDLEY_UNPREDICTABLE(expr) __builtin_unpredictable((expr))
  1492. #endif
  1493. #if \
  1494. (JSON_HEDLEY_HAS_BUILTIN(__builtin_expect_with_probability) && !defined(JSON_HEDLEY_PGI_VERSION)) || \
  1495. JSON_HEDLEY_GCC_VERSION_CHECK(9,0,0) || \
  1496. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1497. # define JSON_HEDLEY_PREDICT(expr, value, probability) __builtin_expect_with_probability( (expr), (value), (probability))
  1498. # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) __builtin_expect_with_probability(!!(expr), 1 , (probability))
  1499. # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) __builtin_expect_with_probability(!!(expr), 0 , (probability))
  1500. # define JSON_HEDLEY_LIKELY(expr) __builtin_expect (!!(expr), 1 )
  1501. # define JSON_HEDLEY_UNLIKELY(expr) __builtin_expect (!!(expr), 0 )
  1502. #elif \
  1503. (JSON_HEDLEY_HAS_BUILTIN(__builtin_expect) && !defined(JSON_HEDLEY_INTEL_CL_VERSION)) || \
  1504. JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \
  1505. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1506. (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)) || \
  1507. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1508. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1509. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1510. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,7,0) || \
  1511. JSON_HEDLEY_TI_CL430_VERSION_CHECK(3,1,0) || \
  1512. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,1,0) || \
  1513. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
  1514. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1515. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1516. JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,27) || \
  1517. JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \
  1518. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1519. # define JSON_HEDLEY_PREDICT(expr, expected, probability) \
  1520. (((probability) >= 0.9) ? __builtin_expect((expr), (expected)) : (JSON_HEDLEY_STATIC_CAST(void, expected), (expr)))
  1521. # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) \
  1522. (__extension__ ({ \
  1523. double hedley_probability_ = (probability); \
  1524. ((hedley_probability_ >= 0.9) ? __builtin_expect(!!(expr), 1) : ((hedley_probability_ <= 0.1) ? __builtin_expect(!!(expr), 0) : !!(expr))); \
  1525. }))
  1526. # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) \
  1527. (__extension__ ({ \
  1528. double hedley_probability_ = (probability); \
  1529. ((hedley_probability_ >= 0.9) ? __builtin_expect(!!(expr), 0) : ((hedley_probability_ <= 0.1) ? __builtin_expect(!!(expr), 1) : !!(expr))); \
  1530. }))
  1531. # define JSON_HEDLEY_LIKELY(expr) __builtin_expect(!!(expr), 1)
  1532. # define JSON_HEDLEY_UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  1533. #else
  1534. # define JSON_HEDLEY_PREDICT(expr, expected, probability) (JSON_HEDLEY_STATIC_CAST(void, expected), (expr))
  1535. # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) (!!(expr))
  1536. # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) (!!(expr))
  1537. # define JSON_HEDLEY_LIKELY(expr) (!!(expr))
  1538. # define JSON_HEDLEY_UNLIKELY(expr) (!!(expr))
  1539. #endif
  1540. #if !defined(JSON_HEDLEY_UNPREDICTABLE)
  1541. #define JSON_HEDLEY_UNPREDICTABLE(expr) JSON_HEDLEY_PREDICT(expr, 1, 0.5)
  1542. #endif
  1543. #if defined(JSON_HEDLEY_MALLOC)
  1544. #undef JSON_HEDLEY_MALLOC
  1545. #endif
  1546. #if \
  1547. JSON_HEDLEY_HAS_ATTRIBUTE(malloc) || \
  1548. JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
  1549. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1550. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1551. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1552. JSON_HEDLEY_IBM_VERSION_CHECK(12,1,0) || \
  1553. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1554. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1555. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1556. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1557. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1558. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1559. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1560. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1561. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1562. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1563. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1564. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1565. #define JSON_HEDLEY_MALLOC __attribute__((__malloc__))
  1566. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
  1567. #define JSON_HEDLEY_MALLOC _Pragma("returns_new_memory")
  1568. #elif \
  1569. JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
  1570. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1571. #define JSON_HEDLEY_MALLOC __declspec(restrict)
  1572. #else
  1573. #define JSON_HEDLEY_MALLOC
  1574. #endif
  1575. #if defined(JSON_HEDLEY_PURE)
  1576. #undef JSON_HEDLEY_PURE
  1577. #endif
  1578. #if \
  1579. JSON_HEDLEY_HAS_ATTRIBUTE(pure) || \
  1580. JSON_HEDLEY_GCC_VERSION_CHECK(2,96,0) || \
  1581. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1582. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1583. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1584. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1585. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1586. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1587. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1588. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1589. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1590. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1591. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1592. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1593. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1594. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1595. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1596. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1597. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1598. # define JSON_HEDLEY_PURE __attribute__((__pure__))
  1599. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
  1600. # define JSON_HEDLEY_PURE _Pragma("does_not_write_global_data")
  1601. #elif defined(__cplusplus) && \
  1602. ( \
  1603. JSON_HEDLEY_TI_CL430_VERSION_CHECK(2,0,1) || \
  1604. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0) || \
  1605. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) \
  1606. )
  1607. # define JSON_HEDLEY_PURE _Pragma("FUNC_IS_PURE;")
  1608. #else
  1609. # define JSON_HEDLEY_PURE
  1610. #endif
  1611. #if defined(JSON_HEDLEY_CONST)
  1612. #undef JSON_HEDLEY_CONST
  1613. #endif
  1614. #if \
  1615. JSON_HEDLEY_HAS_ATTRIBUTE(const) || \
  1616. JSON_HEDLEY_GCC_VERSION_CHECK(2,5,0) || \
  1617. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1618. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1619. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1620. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1621. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1622. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1623. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1624. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1625. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1626. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1627. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1628. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1629. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1630. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1631. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1632. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1633. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1634. #define JSON_HEDLEY_CONST __attribute__((__const__))
  1635. #elif \
  1636. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
  1637. #define JSON_HEDLEY_CONST _Pragma("no_side_effect")
  1638. #else
  1639. #define JSON_HEDLEY_CONST JSON_HEDLEY_PURE
  1640. #endif
  1641. #if defined(JSON_HEDLEY_RESTRICT)
  1642. #undef JSON_HEDLEY_RESTRICT
  1643. #endif
  1644. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && !defined(__cplusplus)
  1645. #define JSON_HEDLEY_RESTRICT restrict
  1646. #elif \
  1647. JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
  1648. JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
  1649. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1650. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \
  1651. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1652. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1653. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1654. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1655. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,4) || \
  1656. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,1,0) || \
  1657. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1658. (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,14,0) && defined(__cplusplus)) || \
  1659. JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \
  1660. defined(__clang__) || \
  1661. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1662. #define JSON_HEDLEY_RESTRICT __restrict
  1663. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,3,0) && !defined(__cplusplus)
  1664. #define JSON_HEDLEY_RESTRICT _Restrict
  1665. #else
  1666. #define JSON_HEDLEY_RESTRICT
  1667. #endif
  1668. #if defined(JSON_HEDLEY_INLINE)
  1669. #undef JSON_HEDLEY_INLINE
  1670. #endif
  1671. #if \
  1672. (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
  1673. (defined(__cplusplus) && (__cplusplus >= 199711L))
  1674. #define JSON_HEDLEY_INLINE inline
  1675. #elif \
  1676. defined(JSON_HEDLEY_GCC_VERSION) || \
  1677. JSON_HEDLEY_ARM_VERSION_CHECK(6,2,0)
  1678. #define JSON_HEDLEY_INLINE __inline__
  1679. #elif \
  1680. JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \
  1681. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \
  1682. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1683. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,1,0) || \
  1684. JSON_HEDLEY_TI_CL430_VERSION_CHECK(3,1,0) || \
  1685. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
  1686. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) || \
  1687. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1688. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1689. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1690. #define JSON_HEDLEY_INLINE __inline
  1691. #else
  1692. #define JSON_HEDLEY_INLINE
  1693. #endif
  1694. #if defined(JSON_HEDLEY_ALWAYS_INLINE)
  1695. #undef JSON_HEDLEY_ALWAYS_INLINE
  1696. #endif
  1697. #if \
  1698. JSON_HEDLEY_HAS_ATTRIBUTE(always_inline) || \
  1699. JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
  1700. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1701. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1702. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1703. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1704. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1705. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1706. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1707. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1708. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1709. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1710. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1711. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1712. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1713. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1714. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1715. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \
  1716. JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
  1717. # define JSON_HEDLEY_ALWAYS_INLINE __attribute__((__always_inline__)) JSON_HEDLEY_INLINE
  1718. #elif \
  1719. JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \
  1720. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1721. # define JSON_HEDLEY_ALWAYS_INLINE __forceinline
  1722. #elif defined(__cplusplus) && \
  1723. ( \
  1724. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1725. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1726. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1727. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
  1728. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1729. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) \
  1730. )
  1731. # define JSON_HEDLEY_ALWAYS_INLINE _Pragma("FUNC_ALWAYS_INLINE;")
  1732. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1733. # define JSON_HEDLEY_ALWAYS_INLINE _Pragma("inline=forced")
  1734. #else
  1735. # define JSON_HEDLEY_ALWAYS_INLINE JSON_HEDLEY_INLINE
  1736. #endif
  1737. #if defined(JSON_HEDLEY_NEVER_INLINE)
  1738. #undef JSON_HEDLEY_NEVER_INLINE
  1739. #endif
  1740. #if \
  1741. JSON_HEDLEY_HAS_ATTRIBUTE(noinline) || \
  1742. JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
  1743. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1744. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1745. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1746. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1747. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1748. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1749. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1750. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1751. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1752. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1753. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1754. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1755. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1756. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1757. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1758. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \
  1759. JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
  1760. #define JSON_HEDLEY_NEVER_INLINE __attribute__((__noinline__))
  1761. #elif \
  1762. JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
  1763. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1764. #define JSON_HEDLEY_NEVER_INLINE __declspec(noinline)
  1765. #elif JSON_HEDLEY_PGI_VERSION_CHECK(10,2,0)
  1766. #define JSON_HEDLEY_NEVER_INLINE _Pragma("noinline")
  1767. #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,0,0) && defined(__cplusplus)
  1768. #define JSON_HEDLEY_NEVER_INLINE _Pragma("FUNC_CANNOT_INLINE;")
  1769. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1770. #define JSON_HEDLEY_NEVER_INLINE _Pragma("inline=never")
  1771. #elif JSON_HEDLEY_COMPCERT_VERSION_CHECK(3,2,0)
  1772. #define JSON_HEDLEY_NEVER_INLINE __attribute((noinline))
  1773. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(9,0,0)
  1774. #define JSON_HEDLEY_NEVER_INLINE __declspec(noinline)
  1775. #else
  1776. #define JSON_HEDLEY_NEVER_INLINE
  1777. #endif
  1778. #if defined(JSON_HEDLEY_PRIVATE)
  1779. #undef JSON_HEDLEY_PRIVATE
  1780. #endif
  1781. #if defined(JSON_HEDLEY_PUBLIC)
  1782. #undef JSON_HEDLEY_PUBLIC
  1783. #endif
  1784. #if defined(JSON_HEDLEY_IMPORT)
  1785. #undef JSON_HEDLEY_IMPORT
  1786. #endif
  1787. #if defined(_WIN32) || defined(__CYGWIN__)
  1788. # define JSON_HEDLEY_PRIVATE
  1789. # define JSON_HEDLEY_PUBLIC __declspec(dllexport)
  1790. # define JSON_HEDLEY_IMPORT __declspec(dllimport)
  1791. #else
  1792. # if \
  1793. JSON_HEDLEY_HAS_ATTRIBUTE(visibility) || \
  1794. JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
  1795. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1796. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1797. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1798. JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
  1799. ( \
  1800. defined(__TI_EABI__) && \
  1801. ( \
  1802. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1803. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) \
  1804. ) \
  1805. ) || \
  1806. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1807. # define JSON_HEDLEY_PRIVATE __attribute__((__visibility__("hidden")))
  1808. # define JSON_HEDLEY_PUBLIC __attribute__((__visibility__("default")))
  1809. # else
  1810. # define JSON_HEDLEY_PRIVATE
  1811. # define JSON_HEDLEY_PUBLIC
  1812. # endif
  1813. # define JSON_HEDLEY_IMPORT extern
  1814. #endif
  1815. #if defined(JSON_HEDLEY_NO_THROW)
  1816. #undef JSON_HEDLEY_NO_THROW
  1817. #endif
  1818. #if \
  1819. JSON_HEDLEY_HAS_ATTRIBUTE(nothrow) || \
  1820. JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
  1821. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1822. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1823. #define JSON_HEDLEY_NO_THROW __attribute__((__nothrow__))
  1824. #elif \
  1825. JSON_HEDLEY_MSVC_VERSION_CHECK(13,1,0) || \
  1826. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \
  1827. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0)
  1828. #define JSON_HEDLEY_NO_THROW __declspec(nothrow)
  1829. #else
  1830. #define JSON_HEDLEY_NO_THROW
  1831. #endif
  1832. #if defined(JSON_HEDLEY_FALL_THROUGH)
  1833. #undef JSON_HEDLEY_FALL_THROUGH
  1834. #endif
  1835. #if \
  1836. JSON_HEDLEY_HAS_ATTRIBUTE(fallthrough) || \
  1837. JSON_HEDLEY_GCC_VERSION_CHECK(7,0,0) || \
  1838. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1839. #define JSON_HEDLEY_FALL_THROUGH __attribute__((__fallthrough__))
  1840. #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(clang,fallthrough)
  1841. #define JSON_HEDLEY_FALL_THROUGH JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[clang::fallthrough]])
  1842. #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE(fallthrough)
  1843. #define JSON_HEDLEY_FALL_THROUGH JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[fallthrough]])
  1844. #elif defined(__fallthrough) /* SAL */
  1845. #define JSON_HEDLEY_FALL_THROUGH __fallthrough
  1846. #else
  1847. #define JSON_HEDLEY_FALL_THROUGH
  1848. #endif
  1849. #if defined(JSON_HEDLEY_RETURNS_NON_NULL)
  1850. #undef JSON_HEDLEY_RETURNS_NON_NULL
  1851. #endif
  1852. #if \
  1853. JSON_HEDLEY_HAS_ATTRIBUTE(returns_nonnull) || \
  1854. JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) || \
  1855. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1856. #define JSON_HEDLEY_RETURNS_NON_NULL __attribute__((__returns_nonnull__))
  1857. #elif defined(_Ret_notnull_) /* SAL */
  1858. #define JSON_HEDLEY_RETURNS_NON_NULL _Ret_notnull_
  1859. #else
  1860. #define JSON_HEDLEY_RETURNS_NON_NULL
  1861. #endif
  1862. #if defined(JSON_HEDLEY_ARRAY_PARAM)
  1863. #undef JSON_HEDLEY_ARRAY_PARAM
  1864. #endif
  1865. #if \
  1866. defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
  1867. !defined(__STDC_NO_VLA__) && \
  1868. !defined(__cplusplus) && \
  1869. !defined(JSON_HEDLEY_PGI_VERSION) && \
  1870. !defined(JSON_HEDLEY_TINYC_VERSION)
  1871. #define JSON_HEDLEY_ARRAY_PARAM(name) (name)
  1872. #else
  1873. #define JSON_HEDLEY_ARRAY_PARAM(name)
  1874. #endif
  1875. #if defined(JSON_HEDLEY_IS_CONSTANT)
  1876. #undef JSON_HEDLEY_IS_CONSTANT
  1877. #endif
  1878. #if defined(JSON_HEDLEY_REQUIRE_CONSTEXPR)
  1879. #undef JSON_HEDLEY_REQUIRE_CONSTEXPR
  1880. #endif
  1881. /* JSON_HEDLEY_IS_CONSTEXPR_ is for
  1882. HEDLEY INTERNAL USE ONLY. API subject to change without notice. */
  1883. #if defined(JSON_HEDLEY_IS_CONSTEXPR_)
  1884. #undef JSON_HEDLEY_IS_CONSTEXPR_
  1885. #endif
  1886. #if \
  1887. JSON_HEDLEY_HAS_BUILTIN(__builtin_constant_p) || \
  1888. JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
  1889. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1890. JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,19) || \
  1891. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1892. JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
  1893. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
  1894. (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) && !defined(__cplusplus)) || \
  1895. JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \
  1896. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1897. #define JSON_HEDLEY_IS_CONSTANT(expr) __builtin_constant_p(expr)
  1898. #endif
  1899. #if !defined(__cplusplus)
  1900. # if \
  1901. JSON_HEDLEY_HAS_BUILTIN(__builtin_types_compatible_p) || \
  1902. JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
  1903. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1904. JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
  1905. JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \
  1906. JSON_HEDLEY_ARM_VERSION_CHECK(5,4,0) || \
  1907. JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,24)
  1908. #if defined(__INTPTR_TYPE__)
  1909. #define JSON_HEDLEY_IS_CONSTEXPR_(expr) __builtin_types_compatible_p(__typeof__((1 ? (void*) ((__INTPTR_TYPE__) ((expr) * 0)) : (int*) 0)), int*)
  1910. #else
  1911. #include <stdint.h>
  1912. #define JSON_HEDLEY_IS_CONSTEXPR_(expr) __builtin_types_compatible_p(__typeof__((1 ? (void*) ((intptr_t) ((expr) * 0)) : (int*) 0)), int*)
  1913. #endif
  1914. # elif \
  1915. ( \
  1916. defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) && \
  1917. !defined(JSON_HEDLEY_SUNPRO_VERSION) && \
  1918. !defined(JSON_HEDLEY_PGI_VERSION) && \
  1919. !defined(JSON_HEDLEY_IAR_VERSION)) || \
  1920. (JSON_HEDLEY_HAS_EXTENSION(c_generic_selections) && !defined(JSON_HEDLEY_IAR_VERSION)) || \
  1921. JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) || \
  1922. JSON_HEDLEY_INTEL_VERSION_CHECK(17,0,0) || \
  1923. JSON_HEDLEY_IBM_VERSION_CHECK(12,1,0) || \
  1924. JSON_HEDLEY_ARM_VERSION_CHECK(5,3,0)
  1925. #if defined(__INTPTR_TYPE__)
  1926. #define JSON_HEDLEY_IS_CONSTEXPR_(expr) _Generic((1 ? (void*) ((__INTPTR_TYPE__) ((expr) * 0)) : (int*) 0), int*: 1, void*: 0)
  1927. #else
  1928. #include <stdint.h>
  1929. #define JSON_HEDLEY_IS_CONSTEXPR_(expr) _Generic((1 ? (void*) ((intptr_t) * 0) : (int*) 0), int*: 1, void*: 0)
  1930. #endif
  1931. # elif \
  1932. defined(JSON_HEDLEY_GCC_VERSION) || \
  1933. defined(JSON_HEDLEY_INTEL_VERSION) || \
  1934. defined(JSON_HEDLEY_TINYC_VERSION) || \
  1935. defined(JSON_HEDLEY_TI_ARMCL_VERSION) || \
  1936. JSON_HEDLEY_TI_CL430_VERSION_CHECK(18,12,0) || \
  1937. defined(JSON_HEDLEY_TI_CL2000_VERSION) || \
  1938. defined(JSON_HEDLEY_TI_CL6X_VERSION) || \
  1939. defined(JSON_HEDLEY_TI_CL7X_VERSION) || \
  1940. defined(JSON_HEDLEY_TI_CLPRU_VERSION) || \
  1941. defined(__clang__)
  1942. # define JSON_HEDLEY_IS_CONSTEXPR_(expr) ( \
  1943. sizeof(void) != \
  1944. sizeof(*( \
  1945. 1 ? \
  1946. ((void*) ((expr) * 0L) ) : \
  1947. ((struct { char v[sizeof(void) * 2]; } *) 1) \
  1948. ) \
  1949. ) \
  1950. )
  1951. # endif
  1952. #endif
  1953. #if defined(JSON_HEDLEY_IS_CONSTEXPR_)
  1954. #if !defined(JSON_HEDLEY_IS_CONSTANT)
  1955. #define JSON_HEDLEY_IS_CONSTANT(expr) JSON_HEDLEY_IS_CONSTEXPR_(expr)
  1956. #endif
  1957. #define JSON_HEDLEY_REQUIRE_CONSTEXPR(expr) (JSON_HEDLEY_IS_CONSTEXPR_(expr) ? (expr) : (-1))
  1958. #else
  1959. #if !defined(JSON_HEDLEY_IS_CONSTANT)
  1960. #define JSON_HEDLEY_IS_CONSTANT(expr) (0)
  1961. #endif
  1962. #define JSON_HEDLEY_REQUIRE_CONSTEXPR(expr) (expr)
  1963. #endif
  1964. #if defined(JSON_HEDLEY_BEGIN_C_DECLS)
  1965. #undef JSON_HEDLEY_BEGIN_C_DECLS
  1966. #endif
  1967. #if defined(JSON_HEDLEY_END_C_DECLS)
  1968. #undef JSON_HEDLEY_END_C_DECLS
  1969. #endif
  1970. #if defined(JSON_HEDLEY_C_DECL)
  1971. #undef JSON_HEDLEY_C_DECL
  1972. #endif
  1973. #if defined(__cplusplus)
  1974. #define JSON_HEDLEY_BEGIN_C_DECLS extern "C" {
  1975. #define JSON_HEDLEY_END_C_DECLS }
  1976. #define JSON_HEDLEY_C_DECL extern "C"
  1977. #else
  1978. #define JSON_HEDLEY_BEGIN_C_DECLS
  1979. #define JSON_HEDLEY_END_C_DECLS
  1980. #define JSON_HEDLEY_C_DECL
  1981. #endif
  1982. #if defined(JSON_HEDLEY_STATIC_ASSERT)
  1983. #undef JSON_HEDLEY_STATIC_ASSERT
  1984. #endif
  1985. #if \
  1986. !defined(__cplusplus) && ( \
  1987. (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)) || \
  1988. (JSON_HEDLEY_HAS_FEATURE(c_static_assert) && !defined(JSON_HEDLEY_INTEL_CL_VERSION)) || \
  1989. JSON_HEDLEY_GCC_VERSION_CHECK(6,0,0) || \
  1990. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1991. defined(_Static_assert) \
  1992. )
  1993. # define JSON_HEDLEY_STATIC_ASSERT(expr, message) _Static_assert(expr, message)
  1994. #elif \
  1995. (defined(__cplusplus) && (__cplusplus >= 201103L)) || \
  1996. JSON_HEDLEY_MSVC_VERSION_CHECK(16,0,0) || \
  1997. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1998. # define JSON_HEDLEY_STATIC_ASSERT(expr, message) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(static_assert(expr, message))
  1999. #else
  2000. # define JSON_HEDLEY_STATIC_ASSERT(expr, message)
  2001. #endif
  2002. #if defined(JSON_HEDLEY_NULL)
  2003. #undef JSON_HEDLEY_NULL
  2004. #endif
  2005. #if defined(__cplusplus)
  2006. #if __cplusplus >= 201103L
  2007. #define JSON_HEDLEY_NULL JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(nullptr)
  2008. #elif defined(NULL)
  2009. #define JSON_HEDLEY_NULL NULL
  2010. #else
  2011. #define JSON_HEDLEY_NULL JSON_HEDLEY_STATIC_CAST(void*, 0)
  2012. #endif
  2013. #elif defined(NULL)
  2014. #define JSON_HEDLEY_NULL NULL
  2015. #else
  2016. #define JSON_HEDLEY_NULL ((void*) 0)
  2017. #endif
  2018. #if defined(JSON_HEDLEY_MESSAGE)
  2019. #undef JSON_HEDLEY_MESSAGE
  2020. #endif
  2021. #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
  2022. # define JSON_HEDLEY_MESSAGE(msg) \
  2023. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  2024. JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS \
  2025. JSON_HEDLEY_PRAGMA(message msg) \
  2026. JSON_HEDLEY_DIAGNOSTIC_POP
  2027. #elif \
  2028. JSON_HEDLEY_GCC_VERSION_CHECK(4,4,0) || \
  2029. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  2030. # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message msg)
  2031. #elif JSON_HEDLEY_CRAY_VERSION_CHECK(5,0,0)
  2032. # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(_CRI message msg)
  2033. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  2034. # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message(msg))
  2035. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,0,0)
  2036. # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message(msg))
  2037. #else
  2038. # define JSON_HEDLEY_MESSAGE(msg)
  2039. #endif
  2040. #if defined(JSON_HEDLEY_WARNING)
  2041. #undef JSON_HEDLEY_WARNING
  2042. #endif
  2043. #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
  2044. # define JSON_HEDLEY_WARNING(msg) \
  2045. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  2046. JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS \
  2047. JSON_HEDLEY_PRAGMA(clang warning msg) \
  2048. JSON_HEDLEY_DIAGNOSTIC_POP
  2049. #elif \
  2050. JSON_HEDLEY_GCC_VERSION_CHECK(4,8,0) || \
  2051. JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \
  2052. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  2053. # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(GCC warning msg)
  2054. #elif \
  2055. JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) || \
  2056. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  2057. # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(message(msg))
  2058. #else
  2059. # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_MESSAGE(msg)
  2060. #endif
  2061. #if defined(JSON_HEDLEY_REQUIRE)
  2062. #undef JSON_HEDLEY_REQUIRE
  2063. #endif
  2064. #if defined(JSON_HEDLEY_REQUIRE_MSG)
  2065. #undef JSON_HEDLEY_REQUIRE_MSG
  2066. #endif
  2067. #if JSON_HEDLEY_HAS_ATTRIBUTE(diagnose_if)
  2068. # if JSON_HEDLEY_HAS_WARNING("-Wgcc-compat")
  2069. # define JSON_HEDLEY_REQUIRE(expr) \
  2070. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  2071. _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \
  2072. __attribute__((diagnose_if(!(expr), #expr, "error"))) \
  2073. JSON_HEDLEY_DIAGNOSTIC_POP
  2074. # define JSON_HEDLEY_REQUIRE_MSG(expr,msg) \
  2075. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  2076. _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \
  2077. __attribute__((diagnose_if(!(expr), msg, "error"))) \
  2078. JSON_HEDLEY_DIAGNOSTIC_POP
  2079. # else
  2080. # define JSON_HEDLEY_REQUIRE(expr) __attribute__((diagnose_if(!(expr), #expr, "error")))
  2081. # define JSON_HEDLEY_REQUIRE_MSG(expr,msg) __attribute__((diagnose_if(!(expr), msg, "error")))
  2082. # endif
  2083. #else
  2084. # define JSON_HEDLEY_REQUIRE(expr)
  2085. # define JSON_HEDLEY_REQUIRE_MSG(expr,msg)
  2086. #endif
  2087. #if defined(JSON_HEDLEY_FLAGS)
  2088. #undef JSON_HEDLEY_FLAGS
  2089. #endif
  2090. #if JSON_HEDLEY_HAS_ATTRIBUTE(flag_enum) && (!defined(__cplusplus) || JSON_HEDLEY_HAS_WARNING("-Wbitfield-enum-conversion"))
  2091. #define JSON_HEDLEY_FLAGS __attribute__((__flag_enum__))
  2092. #else
  2093. #define JSON_HEDLEY_FLAGS
  2094. #endif
  2095. #if defined(JSON_HEDLEY_FLAGS_CAST)
  2096. #undef JSON_HEDLEY_FLAGS_CAST
  2097. #endif
  2098. #if JSON_HEDLEY_INTEL_VERSION_CHECK(19,0,0)
  2099. # define JSON_HEDLEY_FLAGS_CAST(T, expr) (__extension__ ({ \
  2100. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  2101. _Pragma("warning(disable:188)") \
  2102. ((T) (expr)); \
  2103. JSON_HEDLEY_DIAGNOSTIC_POP \
  2104. }))
  2105. #else
  2106. # define JSON_HEDLEY_FLAGS_CAST(T, expr) JSON_HEDLEY_STATIC_CAST(T, expr)
  2107. #endif
  2108. #if defined(JSON_HEDLEY_EMPTY_BASES)
  2109. #undef JSON_HEDLEY_EMPTY_BASES
  2110. #endif
  2111. #if \
  2112. (JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,23918) && !JSON_HEDLEY_MSVC_VERSION_CHECK(20,0,0)) || \
  2113. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  2114. #define JSON_HEDLEY_EMPTY_BASES __declspec(empty_bases)
  2115. #else
  2116. #define JSON_HEDLEY_EMPTY_BASES
  2117. #endif
  2118. /* Remaining macros are deprecated. */
  2119. #if defined(JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK)
  2120. #undef JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK
  2121. #endif
  2122. #if defined(__clang__)
  2123. #define JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) (0)
  2124. #else
  2125. #define JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  2126. #endif
  2127. #if defined(JSON_HEDLEY_CLANG_HAS_ATTRIBUTE)
  2128. #undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE
  2129. #endif
  2130. #define JSON_HEDLEY_CLANG_HAS_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
  2131. #if defined(JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE)
  2132. #undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE
  2133. #endif
  2134. #define JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute)
  2135. #if defined(JSON_HEDLEY_CLANG_HAS_BUILTIN)
  2136. #undef JSON_HEDLEY_CLANG_HAS_BUILTIN
  2137. #endif
  2138. #define JSON_HEDLEY_CLANG_HAS_BUILTIN(builtin) JSON_HEDLEY_HAS_BUILTIN(builtin)
  2139. #if defined(JSON_HEDLEY_CLANG_HAS_FEATURE)
  2140. #undef JSON_HEDLEY_CLANG_HAS_FEATURE
  2141. #endif
  2142. #define JSON_HEDLEY_CLANG_HAS_FEATURE(feature) JSON_HEDLEY_HAS_FEATURE(feature)
  2143. #if defined(JSON_HEDLEY_CLANG_HAS_EXTENSION)
  2144. #undef JSON_HEDLEY_CLANG_HAS_EXTENSION
  2145. #endif
  2146. #define JSON_HEDLEY_CLANG_HAS_EXTENSION(extension) JSON_HEDLEY_HAS_EXTENSION(extension)
  2147. #if defined(JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE)
  2148. #undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE
  2149. #endif
  2150. #define JSON_HEDLEY_CLANG_HAS_DECLSPEC_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute)
  2151. #if defined(JSON_HEDLEY_CLANG_HAS_WARNING)
  2152. #undef JSON_HEDLEY_CLANG_HAS_WARNING
  2153. #endif
  2154. #define JSON_HEDLEY_CLANG_HAS_WARNING(warning) JSON_HEDLEY_HAS_WARNING(warning)
  2155. #endif /* !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < X) */
  2156. // This file contains all internal macro definitions (except those affecting ABI)
  2157. // You MUST include macro_unscope.hpp at the end of json.hpp to undef all of them
  2158. // #include <nlohmann/detail/abi_macros.hpp>
  2159. // exclude unsupported compilers
  2160. #if !defined(JSON_SKIP_UNSUPPORTED_COMPILER_CHECK)
  2161. #if defined(__clang__)
  2162. #if (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) < 30400
  2163. #error "unsupported Clang version - see https://github.com/nlohmann/json#supported-compilers"
  2164. #endif
  2165. #elif defined(__GNUC__) && !(defined(__ICC) || defined(__INTEL_COMPILER))
  2166. #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40800
  2167. #error "unsupported GCC version - see https://github.com/nlohmann/json#supported-compilers"
  2168. #endif
  2169. #endif
  2170. #endif
  2171. // C++ language standard detection
  2172. // if the user manually specified the used c++ version this is skipped
  2173. #if !defined(JSON_HAS_CPP_20) && !defined(JSON_HAS_CPP_17) && !defined(JSON_HAS_CPP_14) && !defined(JSON_HAS_CPP_11)
  2174. #if (defined(__cplusplus) && __cplusplus >= 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L)
  2175. #define JSON_HAS_CPP_20
  2176. #define JSON_HAS_CPP_17
  2177. #define JSON_HAS_CPP_14
  2178. #elif (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464
  2179. #define JSON_HAS_CPP_17
  2180. #define JSON_HAS_CPP_14
  2181. #elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1)
  2182. #define JSON_HAS_CPP_14
  2183. #endif
  2184. // the cpp 11 flag is always specified because it is the minimal required version
  2185. #define JSON_HAS_CPP_11
  2186. #endif
  2187. #ifdef __has_include
  2188. #if __has_include(<version>)
  2189. #include <version>
  2190. #endif
  2191. #endif
  2192. #if !defined(JSON_HAS_FILESYSTEM) && !defined(JSON_HAS_EXPERIMENTAL_FILESYSTEM)
  2193. #ifdef JSON_HAS_CPP_17
  2194. #if defined(__cpp_lib_filesystem)
  2195. #define JSON_HAS_FILESYSTEM 1
  2196. #elif defined(__cpp_lib_experimental_filesystem)
  2197. #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
  2198. #elif !defined(__has_include)
  2199. #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
  2200. #elif __has_include(<filesystem>)
  2201. #define JSON_HAS_FILESYSTEM 1
  2202. #elif __has_include(<experimental/filesystem>)
  2203. #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
  2204. #endif
  2205. // std::filesystem does not work on MinGW GCC 8: https://sourceforge.net/p/mingw-w64/bugs/737/
  2206. #if defined(__MINGW32__) && defined(__GNUC__) && __GNUC__ == 8
  2207. #undef JSON_HAS_FILESYSTEM
  2208. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2209. #endif
  2210. // no filesystem support before GCC 8: https://en.cppreference.com/w/cpp/compiler_support
  2211. #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 8
  2212. #undef JSON_HAS_FILESYSTEM
  2213. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2214. #endif
  2215. // no filesystem support before Clang 7: https://en.cppreference.com/w/cpp/compiler_support
  2216. #if defined(__clang_major__) && __clang_major__ < 7
  2217. #undef JSON_HAS_FILESYSTEM
  2218. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2219. #endif
  2220. // no filesystem support before MSVC 19.14: https://en.cppreference.com/w/cpp/compiler_support
  2221. #if defined(_MSC_VER) && _MSC_VER < 1914
  2222. #undef JSON_HAS_FILESYSTEM
  2223. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2224. #endif
  2225. // no filesystem support before iOS 13
  2226. #if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED < 130000
  2227. #undef JSON_HAS_FILESYSTEM
  2228. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2229. #endif
  2230. // no filesystem support before macOS Catalina
  2231. #if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED < 101500
  2232. #undef JSON_HAS_FILESYSTEM
  2233. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2234. #endif
  2235. #endif
  2236. #endif
  2237. #ifndef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2238. #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 0
  2239. #endif
  2240. #ifndef JSON_HAS_FILESYSTEM
  2241. #define JSON_HAS_FILESYSTEM 0
  2242. #endif
  2243. #ifndef JSON_HAS_THREE_WAY_COMPARISON
  2244. #if defined(__cpp_impl_three_way_comparison) && __cpp_impl_three_way_comparison >= 201907L \
  2245. && defined(__cpp_lib_three_way_comparison) && __cpp_lib_three_way_comparison >= 201907L
  2246. #define JSON_HAS_THREE_WAY_COMPARISON 1
  2247. #else
  2248. #define JSON_HAS_THREE_WAY_COMPARISON 0
  2249. #endif
  2250. #endif
  2251. #ifndef JSON_HAS_RANGES
  2252. // ranges header shipping in GCC 11.1.0 (released 2021-04-27) has syntax error
  2253. #if defined(__GLIBCXX__) && __GLIBCXX__ == 20210427
  2254. #define JSON_HAS_RANGES 0
  2255. #elif defined(__cpp_lib_ranges)
  2256. #define JSON_HAS_RANGES 1
  2257. #else
  2258. #define JSON_HAS_RANGES 0
  2259. #endif
  2260. #endif
  2261. #ifdef JSON_HAS_CPP_17
  2262. #define JSON_INLINE_VARIABLE inline
  2263. #else
  2264. #define JSON_INLINE_VARIABLE
  2265. #endif
  2266. #if JSON_HEDLEY_HAS_ATTRIBUTE(no_unique_address)
  2267. #define JSON_NO_UNIQUE_ADDRESS [[no_unique_address]]
  2268. #else
  2269. #define JSON_NO_UNIQUE_ADDRESS
  2270. #endif
  2271. // disable documentation warnings on clang
  2272. #if defined(__clang__)
  2273. #pragma clang diagnostic push
  2274. #pragma clang diagnostic ignored "-Wdocumentation"
  2275. #pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
  2276. #endif
  2277. // allow disabling exceptions
  2278. #if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION)
  2279. #define JSON_THROW(exception) throw exception
  2280. #define JSON_TRY try
  2281. #define JSON_CATCH(exception) catch(exception)
  2282. #define JSON_INTERNAL_CATCH(exception) catch(exception)
  2283. #else
  2284. #include <cstdlib>
  2285. #define JSON_THROW(exception) std::abort()
  2286. #define JSON_TRY if(true)
  2287. #define JSON_CATCH(exception) if(false)
  2288. #define JSON_INTERNAL_CATCH(exception) if(false)
  2289. #endif
  2290. // override exception macros
  2291. #if defined(JSON_THROW_USER)
  2292. #undef JSON_THROW
  2293. #define JSON_THROW JSON_THROW_USER
  2294. #endif
  2295. #if defined(JSON_TRY_USER)
  2296. #undef JSON_TRY
  2297. #define JSON_TRY JSON_TRY_USER
  2298. #endif
  2299. #if defined(JSON_CATCH_USER)
  2300. #undef JSON_CATCH
  2301. #define JSON_CATCH JSON_CATCH_USER
  2302. #undef JSON_INTERNAL_CATCH
  2303. #define JSON_INTERNAL_CATCH JSON_CATCH_USER
  2304. #endif
  2305. #if defined(JSON_INTERNAL_CATCH_USER)
  2306. #undef JSON_INTERNAL_CATCH
  2307. #define JSON_INTERNAL_CATCH JSON_INTERNAL_CATCH_USER
  2308. #endif
  2309. // allow overriding assert
  2310. #if !defined(JSON_ASSERT)
  2311. #include <cassert> // assert
  2312. #define JSON_ASSERT(x) assert(x)
  2313. #endif
  2314. // allow to access some private functions (needed by the test suite)
  2315. #if defined(JSON_TESTS_PRIVATE)
  2316. #define JSON_PRIVATE_UNLESS_TESTED public
  2317. #else
  2318. #define JSON_PRIVATE_UNLESS_TESTED private
  2319. #endif
  2320. /*!
  2321. @brief macro to briefly define a mapping between an enum and JSON
  2322. @def NLOHMANN_JSON_SERIALIZE_ENUM
  2323. @since version 3.4.0
  2324. */
  2325. #define NLOHMANN_JSON_SERIALIZE_ENUM(ENUM_TYPE, ...) \
  2326. template<typename BasicJsonType> \
  2327. inline void to_json(BasicJsonType& j, const ENUM_TYPE& e) \
  2328. { \
  2329. static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
  2330. static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
  2331. auto it = std::find_if(std::begin(m), std::end(m), \
  2332. [e](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
  2333. { \
  2334. return ej_pair.first == e; \
  2335. }); \
  2336. j = ((it != std::end(m)) ? it : std::begin(m))->second; \
  2337. } \
  2338. template<typename BasicJsonType> \
  2339. inline void from_json(const BasicJsonType& j, ENUM_TYPE& e) \
  2340. { \
  2341. static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
  2342. static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
  2343. auto it = std::find_if(std::begin(m), std::end(m), \
  2344. [&j](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
  2345. { \
  2346. return ej_pair.second == j; \
  2347. }); \
  2348. e = ((it != std::end(m)) ? it : std::begin(m))->first; \
  2349. }
  2350. // Ugly macros to avoid uglier copy-paste when specializing basic_json. They
  2351. // may be removed in the future once the class is split.
  2352. #define NLOHMANN_BASIC_JSON_TPL_DECLARATION \
  2353. template<template<typename, typename, typename...> class ObjectType, \
  2354. template<typename, typename...> class ArrayType, \
  2355. class StringType, class BooleanType, class NumberIntegerType, \
  2356. class NumberUnsignedType, class NumberFloatType, \
  2357. template<typename> class AllocatorType, \
  2358. template<typename, typename = void> class JSONSerializer, \
  2359. class BinaryType>
  2360. #define NLOHMANN_BASIC_JSON_TPL \
  2361. basic_json<ObjectType, ArrayType, StringType, BooleanType, \
  2362. NumberIntegerType, NumberUnsignedType, NumberFloatType, \
  2363. AllocatorType, JSONSerializer, BinaryType>
  2364. // Macros to simplify conversion from/to types
  2365. #define NLOHMANN_JSON_EXPAND( x ) x
  2366. #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
  2367. #define NLOHMANN_JSON_PASTE(...) NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_GET_MACRO(__VA_ARGS__, \
  2368. NLOHMANN_JSON_PASTE64, \
  2369. NLOHMANN_JSON_PASTE63, \
  2370. NLOHMANN_JSON_PASTE62, \
  2371. NLOHMANN_JSON_PASTE61, \
  2372. NLOHMANN_JSON_PASTE60, \
  2373. NLOHMANN_JSON_PASTE59, \
  2374. NLOHMANN_JSON_PASTE58, \
  2375. NLOHMANN_JSON_PASTE57, \
  2376. NLOHMANN_JSON_PASTE56, \
  2377. NLOHMANN_JSON_PASTE55, \
  2378. NLOHMANN_JSON_PASTE54, \
  2379. NLOHMANN_JSON_PASTE53, \
  2380. NLOHMANN_JSON_PASTE52, \
  2381. NLOHMANN_JSON_PASTE51, \
  2382. NLOHMANN_JSON_PASTE50, \
  2383. NLOHMANN_JSON_PASTE49, \
  2384. NLOHMANN_JSON_PASTE48, \
  2385. NLOHMANN_JSON_PASTE47, \
  2386. NLOHMANN_JSON_PASTE46, \
  2387. NLOHMANN_JSON_PASTE45, \
  2388. NLOHMANN_JSON_PASTE44, \
  2389. NLOHMANN_JSON_PASTE43, \
  2390. NLOHMANN_JSON_PASTE42, \
  2391. NLOHMANN_JSON_PASTE41, \
  2392. NLOHMANN_JSON_PASTE40, \
  2393. NLOHMANN_JSON_PASTE39, \
  2394. NLOHMANN_JSON_PASTE38, \
  2395. NLOHMANN_JSON_PASTE37, \
  2396. NLOHMANN_JSON_PASTE36, \
  2397. NLOHMANN_JSON_PASTE35, \
  2398. NLOHMANN_JSON_PASTE34, \
  2399. NLOHMANN_JSON_PASTE33, \
  2400. NLOHMANN_JSON_PASTE32, \
  2401. NLOHMANN_JSON_PASTE31, \
  2402. NLOHMANN_JSON_PASTE30, \
  2403. NLOHMANN_JSON_PASTE29, \
  2404. NLOHMANN_JSON_PASTE28, \
  2405. NLOHMANN_JSON_PASTE27, \
  2406. NLOHMANN_JSON_PASTE26, \
  2407. NLOHMANN_JSON_PASTE25, \
  2408. NLOHMANN_JSON_PASTE24, \
  2409. NLOHMANN_JSON_PASTE23, \
  2410. NLOHMANN_JSON_PASTE22, \
  2411. NLOHMANN_JSON_PASTE21, \
  2412. NLOHMANN_JSON_PASTE20, \
  2413. NLOHMANN_JSON_PASTE19, \
  2414. NLOHMANN_JSON_PASTE18, \
  2415. NLOHMANN_JSON_PASTE17, \
  2416. NLOHMANN_JSON_PASTE16, \
  2417. NLOHMANN_JSON_PASTE15, \
  2418. NLOHMANN_JSON_PASTE14, \
  2419. NLOHMANN_JSON_PASTE13, \
  2420. NLOHMANN_JSON_PASTE12, \
  2421. NLOHMANN_JSON_PASTE11, \
  2422. NLOHMANN_JSON_PASTE10, \
  2423. NLOHMANN_JSON_PASTE9, \
  2424. NLOHMANN_JSON_PASTE8, \
  2425. NLOHMANN_JSON_PASTE7, \
  2426. NLOHMANN_JSON_PASTE6, \
  2427. NLOHMANN_JSON_PASTE5, \
  2428. NLOHMANN_JSON_PASTE4, \
  2429. NLOHMANN_JSON_PASTE3, \
  2430. NLOHMANN_JSON_PASTE2, \
  2431. NLOHMANN_JSON_PASTE1)(__VA_ARGS__))
  2432. #define NLOHMANN_JSON_PASTE2(func, v1) func(v1)
  2433. #define NLOHMANN_JSON_PASTE3(func, v1, v2) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE2(func, v2)
  2434. #define NLOHMANN_JSON_PASTE4(func, v1, v2, v3) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE3(func, v2, v3)
  2435. #define NLOHMANN_JSON_PASTE5(func, v1, v2, v3, v4) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE4(func, v2, v3, v4)
  2436. #define NLOHMANN_JSON_PASTE6(func, v1, v2, v3, v4, v5) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE5(func, v2, v3, v4, v5)
  2437. #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)
  2438. #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)
  2439. #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)
  2440. #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)
  2441. #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)
  2442. #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)
  2443. #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)
  2444. #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)
  2445. #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)
  2446. #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)
  2447. #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)
  2448. #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)
  2449. #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)
  2450. #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)
  2451. #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)
  2452. #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)
  2453. #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)
  2454. #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)
  2455. #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)
  2456. #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)
  2457. #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)
  2458. #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)
  2459. #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)
  2460. #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)
  2461. #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)
  2462. #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)
  2463. #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)
  2464. #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)
  2465. #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)
  2466. #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)
  2467. #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)
  2468. #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)
  2469. #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)
  2470. #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)
  2471. #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)
  2472. #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)
  2473. #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)
  2474. #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)
  2475. #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)
  2476. #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)
  2477. #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)
  2478. #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)
  2479. #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)
  2480. #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)
  2481. #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)
  2482. #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)
  2483. #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)
  2484. #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)
  2485. #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)
  2486. #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)
  2487. #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)
  2488. #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)
  2489. #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)
  2490. #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)
  2491. #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)
  2492. #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)
  2493. #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)
  2494. #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)
  2495. #define NLOHMANN_JSON_TO(v1) nlohmann_json_j[#v1] = nlohmann_json_t.v1;
  2496. #define NLOHMANN_JSON_FROM(v1) nlohmann_json_j.at(#v1).get_to(nlohmann_json_t.v1);
  2497. #define NLOHMANN_JSON_FROM_WITH_DEFAULT(v1) nlohmann_json_t.v1 = nlohmann_json_j.value(#v1, nlohmann_json_default_obj.v1);
  2498. /*!
  2499. @brief macro
  2500. @def NLOHMANN_DEFINE_TYPE_INTRUSIVE
  2501. @since version 3.9.0
  2502. */
  2503. #define NLOHMANN_DEFINE_TYPE_INTRUSIVE(Type, ...) \
  2504. 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__)) } \
  2505. 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__)) }
  2506. #define NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(Type, ...) \
  2507. friend void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
  2508. friend void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { Type nlohmann_json_default_obj; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) }
  2509. /*!
  2510. @brief macro
  2511. @def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE
  2512. @since version 3.9.0
  2513. */
  2514. #define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Type, ...) \
  2515. 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__)) } \
  2516. 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__)) }
  2517. #define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Type, ...) \
  2518. inline void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
  2519. inline void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { Type nlohmann_json_default_obj; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) }
  2520. // inspired from https://stackoverflow.com/a/26745591
  2521. // allows to call any std function as if (e.g. with begin):
  2522. // using std::begin; begin(x);
  2523. //
  2524. // it allows using the detected idiom to retrieve the return type
  2525. // of such an expression
  2526. #define NLOHMANN_CAN_CALL_STD_FUNC_IMPL(std_name) \
  2527. namespace detail { \
  2528. using std::std_name; \
  2529. \
  2530. template<typename... T> \
  2531. using result_of_##std_name = decltype(std_name(std::declval<T>()...)); \
  2532. } \
  2533. \
  2534. namespace detail2 { \
  2535. struct std_name##_tag \
  2536. { \
  2537. }; \
  2538. \
  2539. template<typename... T> \
  2540. std_name##_tag std_name(T&&...); \
  2541. \
  2542. template<typename... T> \
  2543. using result_of_##std_name = decltype(std_name(std::declval<T>()...)); \
  2544. \
  2545. template<typename... T> \
  2546. struct would_call_std_##std_name \
  2547. { \
  2548. static constexpr auto const value = ::nlohmann::detail:: \
  2549. is_detected_exact<std_name##_tag, result_of_##std_name, T...>::value; \
  2550. }; \
  2551. } /* namespace detail2 */ \
  2552. \
  2553. template<typename... T> \
  2554. struct would_call_std_##std_name : detail2::would_call_std_##std_name<T...> \
  2555. { \
  2556. }
  2557. #ifndef JSON_USE_IMPLICIT_CONVERSIONS
  2558. #define JSON_USE_IMPLICIT_CONVERSIONS 1
  2559. #endif
  2560. #if JSON_USE_IMPLICIT_CONVERSIONS
  2561. #define JSON_EXPLICIT
  2562. #else
  2563. #define JSON_EXPLICIT explicit
  2564. #endif
  2565. #ifndef JSON_DISABLE_ENUM_SERIALIZATION
  2566. #define JSON_DISABLE_ENUM_SERIALIZATION 0
  2567. #endif
  2568. #ifndef JSON_USE_GLOBAL_UDLS
  2569. #define JSON_USE_GLOBAL_UDLS 1
  2570. #endif
  2571. #if JSON_HAS_THREE_WAY_COMPARISON
  2572. #include <compare> // partial_ordering
  2573. #endif
  2574. NLOHMANN_JSON_NAMESPACE_BEGIN
  2575. namespace detail
  2576. {
  2577. ///////////////////////////
  2578. // JSON type enumeration //
  2579. ///////////////////////////
  2580. /*!
  2581. @brief the JSON type enumeration
  2582. This enumeration collects the different JSON types. It is internally used to
  2583. distinguish the stored values, and the functions @ref basic_json::is_null(),
  2584. @ref basic_json::is_object(), @ref basic_json::is_array(),
  2585. @ref basic_json::is_string(), @ref basic_json::is_boolean(),
  2586. @ref basic_json::is_number() (with @ref basic_json::is_number_integer(),
  2587. @ref basic_json::is_number_unsigned(), and @ref basic_json::is_number_float()),
  2588. @ref basic_json::is_discarded(), @ref basic_json::is_primitive(), and
  2589. @ref basic_json::is_structured() rely on it.
  2590. @note There are three enumeration entries (number_integer, number_unsigned, and
  2591. number_float), because the library distinguishes these three types for numbers:
  2592. @ref basic_json::number_unsigned_t is used for unsigned integers,
  2593. @ref basic_json::number_integer_t is used for signed integers, and
  2594. @ref basic_json::number_float_t is used for floating-point numbers or to
  2595. approximate integers which do not fit in the limits of their respective type.
  2596. @sa see @ref basic_json::basic_json(const value_t value_type) -- create a JSON
  2597. value with the default value for a given type
  2598. @since version 1.0.0
  2599. */
  2600. enum class value_t : std::uint8_t
  2601. {
  2602. null, ///< null value
  2603. object, ///< object (unordered set of name/value pairs)
  2604. array, ///< array (ordered collection of values)
  2605. string, ///< string value
  2606. boolean, ///< boolean value
  2607. number_integer, ///< number value (signed integer)
  2608. number_unsigned, ///< number value (unsigned integer)
  2609. number_float, ///< number value (floating-point)
  2610. binary, ///< binary array (ordered collection of bytes)
  2611. discarded ///< discarded by the parser callback function
  2612. };
  2613. /*!
  2614. @brief comparison operator for JSON types
  2615. Returns an ordering that is similar to Python:
  2616. - order: null < boolean < number < object < array < string < binary
  2617. - furthermore, each type is not smaller than itself
  2618. - discarded values are not comparable
  2619. - binary is represented as a b"" string in python and directly comparable to a
  2620. string; however, making a binary array directly comparable with a string would
  2621. be surprising behavior in a JSON file.
  2622. @since version 1.0.0
  2623. */
  2624. #if JSON_HAS_THREE_WAY_COMPARISON
  2625. inline std::partial_ordering operator<=>(const value_t lhs, const value_t rhs) noexcept // *NOPAD*
  2626. #else
  2627. inline bool operator<(const value_t lhs, const value_t rhs) noexcept
  2628. #endif
  2629. {
  2630. static constexpr std::array<std::uint8_t, 9> order = {{
  2631. 0 /* null */, 3 /* object */, 4 /* array */, 5 /* string */,
  2632. 1 /* boolean */, 2 /* integer */, 2 /* unsigned */, 2 /* float */,
  2633. 6 /* binary */
  2634. }
  2635. };
  2636. const auto l_index = static_cast<std::size_t>(lhs);
  2637. const auto r_index = static_cast<std::size_t>(rhs);
  2638. #if JSON_HAS_THREE_WAY_COMPARISON
  2639. if (l_index < order.size() && r_index < order.size())
  2640. {
  2641. return order[l_index] <=> order[r_index]; // *NOPAD*
  2642. }
  2643. return std::partial_ordering::unordered;
  2644. #else
  2645. return l_index < order.size() && r_index < order.size() && order[l_index] < order[r_index];
  2646. #endif
  2647. }
  2648. // GCC selects the built-in operator< over an operator rewritten from
  2649. // a user-defined spaceship operator
  2650. // Clang, MSVC, and ICC select the rewritten candidate
  2651. // (see GCC bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105200)
  2652. #if JSON_HAS_THREE_WAY_COMPARISON && defined(__GNUC__)
  2653. inline bool operator<(const value_t lhs, const value_t rhs) noexcept
  2654. {
  2655. return std::is_lt(lhs <=> rhs); // *NOPAD*
  2656. }
  2657. #endif
  2658. } // namespace detail
  2659. NLOHMANN_JSON_NAMESPACE_END
  2660. // #include <nlohmann/detail/string_escape.hpp>
  2661. // __ _____ _____ _____
  2662. // __| | __| | | | JSON for Modern C++
  2663. // | | |__ | | | | | | version 3.11.2
  2664. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2665. //
  2666. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  2667. // SPDX-License-Identifier: MIT
  2668. // #include <nlohmann/detail/abi_macros.hpp>
  2669. NLOHMANN_JSON_NAMESPACE_BEGIN
  2670. namespace detail
  2671. {
  2672. /*!
  2673. @brief replace all occurrences of a substring by another string
  2674. @param[in,out] s the string to manipulate; changed so that all
  2675. occurrences of @a f are replaced with @a t
  2676. @param[in] f the substring to replace with @a t
  2677. @param[in] t the string to replace @a f
  2678. @pre The search string @a f must not be empty. **This precondition is
  2679. enforced with an assertion.**
  2680. @since version 2.0.0
  2681. */
  2682. template<typename StringType>
  2683. inline void replace_substring(StringType& s, const StringType& f,
  2684. const StringType& t)
  2685. {
  2686. JSON_ASSERT(!f.empty());
  2687. for (auto pos = s.find(f); // find first occurrence of f
  2688. pos != StringType::npos; // make sure f was found
  2689. s.replace(pos, f.size(), t), // replace with t, and
  2690. pos = s.find(f, pos + t.size())) // find next occurrence of f
  2691. {}
  2692. }
  2693. /*!
  2694. * @brief string escaping as described in RFC 6901 (Sect. 4)
  2695. * @param[in] s string to escape
  2696. * @return escaped string
  2697. *
  2698. * Note the order of escaping "~" to "~0" and "/" to "~1" is important.
  2699. */
  2700. template<typename StringType>
  2701. inline StringType escape(StringType s)
  2702. {
  2703. replace_substring(s, StringType{"~"}, StringType{"~0"});
  2704. replace_substring(s, StringType{"/"}, StringType{"~1"});
  2705. return s;
  2706. }
  2707. /*!
  2708. * @brief string unescaping as described in RFC 6901 (Sect. 4)
  2709. * @param[in] s string to unescape
  2710. * @return unescaped string
  2711. *
  2712. * Note the order of escaping "~1" to "/" and "~0" to "~" is important.
  2713. */
  2714. template<typename StringType>
  2715. static void unescape(StringType& s)
  2716. {
  2717. replace_substring(s, StringType{"~1"}, StringType{"/"});
  2718. replace_substring(s, StringType{"~0"}, StringType{"~"});
  2719. }
  2720. } // namespace detail
  2721. NLOHMANN_JSON_NAMESPACE_END
  2722. // #include <nlohmann/detail/input/position_t.hpp>
  2723. // __ _____ _____ _____
  2724. // __| | __| | | | JSON for Modern C++
  2725. // | | |__ | | | | | | version 3.11.2
  2726. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2727. //
  2728. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  2729. // SPDX-License-Identifier: MIT
  2730. #include <cstddef> // size_t
  2731. // #include <nlohmann/detail/abi_macros.hpp>
  2732. NLOHMANN_JSON_NAMESPACE_BEGIN
  2733. namespace detail
  2734. {
  2735. /// struct to capture the start position of the current token
  2736. struct position_t
  2737. {
  2738. /// the total number of characters read
  2739. std::size_t chars_read_total = 0;
  2740. /// the number of characters read in the current line
  2741. std::size_t chars_read_current_line = 0;
  2742. /// the number of lines read
  2743. std::size_t lines_read = 0;
  2744. /// conversion to size_t to preserve SAX interface
  2745. constexpr operator size_t() const
  2746. {
  2747. return chars_read_total;
  2748. }
  2749. };
  2750. } // namespace detail
  2751. NLOHMANN_JSON_NAMESPACE_END
  2752. // #include <nlohmann/detail/macro_scope.hpp>
  2753. // #include <nlohmann/detail/meta/cpp_future.hpp>
  2754. // __ _____ _____ _____
  2755. // __| | __| | | | JSON for Modern C++
  2756. // | | |__ | | | | | | version 3.11.2
  2757. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2758. //
  2759. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  2760. // SPDX-FileCopyrightText: 2018 The Abseil Authors
  2761. // SPDX-License-Identifier: MIT
  2762. #include <array> // array
  2763. #include <cstddef> // size_t
  2764. #include <type_traits> // conditional, enable_if, false_type, integral_constant, is_constructible, is_integral, is_same, remove_cv, remove_reference, true_type
  2765. #include <utility> // index_sequence, make_index_sequence, index_sequence_for
  2766. // #include <nlohmann/detail/macro_scope.hpp>
  2767. NLOHMANN_JSON_NAMESPACE_BEGIN
  2768. namespace detail
  2769. {
  2770. template<typename T>
  2771. using uncvref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
  2772. #ifdef JSON_HAS_CPP_14
  2773. // the following utilities are natively available in C++14
  2774. using std::enable_if_t;
  2775. using std::index_sequence;
  2776. using std::make_index_sequence;
  2777. using std::index_sequence_for;
  2778. #else
  2779. // alias templates to reduce boilerplate
  2780. template<bool B, typename T = void>
  2781. using enable_if_t = typename std::enable_if<B, T>::type;
  2782. // The following code is taken from https://github.com/abseil/abseil-cpp/blob/10cb35e459f5ecca5b2ff107635da0bfa41011b4/absl/utility/utility.h
  2783. // which is part of Google Abseil (https://github.com/abseil/abseil-cpp), licensed under the Apache License 2.0.
  2784. //// START OF CODE FROM GOOGLE ABSEIL
  2785. // integer_sequence
  2786. //
  2787. // Class template representing a compile-time integer sequence. An instantiation
  2788. // of `integer_sequence<T, Ints...>` has a sequence of integers encoded in its
  2789. // type through its template arguments (which is a common need when
  2790. // working with C++11 variadic templates). `absl::integer_sequence` is designed
  2791. // to be a drop-in replacement for C++14's `std::integer_sequence`.
  2792. //
  2793. // Example:
  2794. //
  2795. // template< class T, T... Ints >
  2796. // void user_function(integer_sequence<T, Ints...>);
  2797. //
  2798. // int main()
  2799. // {
  2800. // // user_function's `T` will be deduced to `int` and `Ints...`
  2801. // // will be deduced to `0, 1, 2, 3, 4`.
  2802. // user_function(make_integer_sequence<int, 5>());
  2803. // }
  2804. template <typename T, T... Ints>
  2805. struct integer_sequence
  2806. {
  2807. using value_type = T;
  2808. static constexpr std::size_t size() noexcept
  2809. {
  2810. return sizeof...(Ints);
  2811. }
  2812. };
  2813. // index_sequence
  2814. //
  2815. // A helper template for an `integer_sequence` of `size_t`,
  2816. // `absl::index_sequence` is designed to be a drop-in replacement for C++14's
  2817. // `std::index_sequence`.
  2818. template <size_t... Ints>
  2819. using index_sequence = integer_sequence<size_t, Ints...>;
  2820. namespace utility_internal
  2821. {
  2822. template <typename Seq, size_t SeqSize, size_t Rem>
  2823. struct Extend;
  2824. // Note that SeqSize == sizeof...(Ints). It's passed explicitly for efficiency.
  2825. template <typename T, T... Ints, size_t SeqSize>
  2826. struct Extend<integer_sequence<T, Ints...>, SeqSize, 0>
  2827. {
  2828. using type = integer_sequence < T, Ints..., (Ints + SeqSize)... >;
  2829. };
  2830. template <typename T, T... Ints, size_t SeqSize>
  2831. struct Extend<integer_sequence<T, Ints...>, SeqSize, 1>
  2832. {
  2833. using type = integer_sequence < T, Ints..., (Ints + SeqSize)..., 2 * SeqSize >;
  2834. };
  2835. // Recursion helper for 'make_integer_sequence<T, N>'.
  2836. // 'Gen<T, N>::type' is an alias for 'integer_sequence<T, 0, 1, ... N-1>'.
  2837. template <typename T, size_t N>
  2838. struct Gen
  2839. {
  2840. using type =
  2841. typename Extend < typename Gen < T, N / 2 >::type, N / 2, N % 2 >::type;
  2842. };
  2843. template <typename T>
  2844. struct Gen<T, 0>
  2845. {
  2846. using type = integer_sequence<T>;
  2847. };
  2848. } // namespace utility_internal
  2849. // Compile-time sequences of integers
  2850. // make_integer_sequence
  2851. //
  2852. // This template alias is equivalent to
  2853. // `integer_sequence<int, 0, 1, ..., N-1>`, and is designed to be a drop-in
  2854. // replacement for C++14's `std::make_integer_sequence`.
  2855. template <typename T, T N>
  2856. using make_integer_sequence = typename utility_internal::Gen<T, N>::type;
  2857. // make_index_sequence
  2858. //
  2859. // This template alias is equivalent to `index_sequence<0, 1, ..., N-1>`,
  2860. // and is designed to be a drop-in replacement for C++14's
  2861. // `std::make_index_sequence`.
  2862. template <size_t N>
  2863. using make_index_sequence = make_integer_sequence<size_t, N>;
  2864. // index_sequence_for
  2865. //
  2866. // Converts a typename pack into an index sequence of the same length, and
  2867. // is designed to be a drop-in replacement for C++14's
  2868. // `std::index_sequence_for()`
  2869. template <typename... Ts>
  2870. using index_sequence_for = make_index_sequence<sizeof...(Ts)>;
  2871. //// END OF CODE FROM GOOGLE ABSEIL
  2872. #endif
  2873. // dispatch utility (taken from ranges-v3)
  2874. template<unsigned N> struct priority_tag : priority_tag < N - 1 > {};
  2875. template<> struct priority_tag<0> {};
  2876. // taken from ranges-v3
  2877. template<typename T>
  2878. struct static_const
  2879. {
  2880. static JSON_INLINE_VARIABLE constexpr T value{};
  2881. };
  2882. #ifndef JSON_HAS_CPP_17
  2883. template<typename T>
  2884. constexpr T static_const<T>::value;
  2885. #endif
  2886. template<typename T, typename... Args>
  2887. inline constexpr std::array<T, sizeof...(Args)> make_array(Args&& ... args)
  2888. {
  2889. return std::array<T, sizeof...(Args)> {{static_cast<T>(std::forward<Args>(args))...}};
  2890. }
  2891. } // namespace detail
  2892. NLOHMANN_JSON_NAMESPACE_END
  2893. // #include <nlohmann/detail/meta/type_traits.hpp>
  2894. // __ _____ _____ _____
  2895. // __| | __| | | | JSON for Modern C++
  2896. // | | |__ | | | | | | version 3.11.2
  2897. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2898. //
  2899. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  2900. // SPDX-License-Identifier: MIT
  2901. #include <limits> // numeric_limits
  2902. #include <type_traits> // false_type, is_constructible, is_integral, is_same, true_type
  2903. #include <utility> // declval
  2904. #include <tuple> // tuple
  2905. // #include <nlohmann/detail/iterators/iterator_traits.hpp>
  2906. // __ _____ _____ _____
  2907. // __| | __| | | | JSON for Modern C++
  2908. // | | |__ | | | | | | version 3.11.2
  2909. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2910. //
  2911. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  2912. // SPDX-License-Identifier: MIT
  2913. #include <iterator> // random_access_iterator_tag
  2914. // #include <nlohmann/detail/abi_macros.hpp>
  2915. // #include <nlohmann/detail/meta/void_t.hpp>
  2916. // #include <nlohmann/detail/meta/cpp_future.hpp>
  2917. NLOHMANN_JSON_NAMESPACE_BEGIN
  2918. namespace detail
  2919. {
  2920. template<typename It, typename = void>
  2921. struct iterator_types {};
  2922. template<typename It>
  2923. struct iterator_types <
  2924. It,
  2925. void_t<typename It::difference_type, typename It::value_type, typename It::pointer,
  2926. typename It::reference, typename It::iterator_category >>
  2927. {
  2928. using difference_type = typename It::difference_type;
  2929. using value_type = typename It::value_type;
  2930. using pointer = typename It::pointer;
  2931. using reference = typename It::reference;
  2932. using iterator_category = typename It::iterator_category;
  2933. };
  2934. // This is required as some compilers implement std::iterator_traits in a way that
  2935. // doesn't work with SFINAE. See https://github.com/nlohmann/json/issues/1341.
  2936. template<typename T, typename = void>
  2937. struct iterator_traits
  2938. {
  2939. };
  2940. template<typename T>
  2941. struct iterator_traits < T, enable_if_t < !std::is_pointer<T>::value >>
  2942. : iterator_types<T>
  2943. {
  2944. };
  2945. template<typename T>
  2946. struct iterator_traits<T*, enable_if_t<std::is_object<T>::value>>
  2947. {
  2948. using iterator_category = std::random_access_iterator_tag;
  2949. using value_type = T;
  2950. using difference_type = ptrdiff_t;
  2951. using pointer = T*;
  2952. using reference = T&;
  2953. };
  2954. } // namespace detail
  2955. NLOHMANN_JSON_NAMESPACE_END
  2956. // #include <nlohmann/detail/macro_scope.hpp>
  2957. // #include <nlohmann/detail/meta/call_std/begin.hpp>
  2958. // __ _____ _____ _____
  2959. // __| | __| | | | JSON for Modern C++
  2960. // | | |__ | | | | | | version 3.11.2
  2961. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2962. //
  2963. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  2964. // SPDX-License-Identifier: MIT
  2965. // #include <nlohmann/detail/macro_scope.hpp>
  2966. NLOHMANN_JSON_NAMESPACE_BEGIN
  2967. NLOHMANN_CAN_CALL_STD_FUNC_IMPL(begin);
  2968. NLOHMANN_JSON_NAMESPACE_END
  2969. // #include <nlohmann/detail/meta/call_std/end.hpp>
  2970. // __ _____ _____ _____
  2971. // __| | __| | | | JSON for Modern C++
  2972. // | | |__ | | | | | | version 3.11.2
  2973. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2974. //
  2975. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  2976. // SPDX-License-Identifier: MIT
  2977. // #include <nlohmann/detail/macro_scope.hpp>
  2978. NLOHMANN_JSON_NAMESPACE_BEGIN
  2979. NLOHMANN_CAN_CALL_STD_FUNC_IMPL(end);
  2980. NLOHMANN_JSON_NAMESPACE_END
  2981. // #include <nlohmann/detail/meta/cpp_future.hpp>
  2982. // #include <nlohmann/detail/meta/detected.hpp>
  2983. // #include <nlohmann/json_fwd.hpp>
  2984. // __ _____ _____ _____
  2985. // __| | __| | | | JSON for Modern C++
  2986. // | | |__ | | | | | | version 3.11.2
  2987. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  2988. //
  2989. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  2990. // SPDX-License-Identifier: MIT
  2991. #ifndef INCLUDE_NLOHMANN_JSON_FWD_HPP_
  2992. #define INCLUDE_NLOHMANN_JSON_FWD_HPP_
  2993. #include <cstdint> // int64_t, uint64_t
  2994. #include <map> // map
  2995. #include <memory> // allocator
  2996. #include <string> // string
  2997. #include <vector> // vector
  2998. // #include <nlohmann/detail/abi_macros.hpp>
  2999. /*!
  3000. @brief namespace for Niels Lohmann
  3001. @see https://github.com/nlohmann
  3002. @since version 1.0.0
  3003. */
  3004. NLOHMANN_JSON_NAMESPACE_BEGIN
  3005. /*!
  3006. @brief default JSONSerializer template argument
  3007. This serializer ignores the template arguments and uses ADL
  3008. ([argument-dependent lookup](https://en.cppreference.com/w/cpp/language/adl))
  3009. for serialization.
  3010. */
  3011. template<typename T = void, typename SFINAE = void>
  3012. struct adl_serializer;
  3013. /// a class to store JSON values
  3014. /// @sa https://json.nlohmann.me/api/basic_json/
  3015. template<template<typename U, typename V, typename... Args> class ObjectType =
  3016. std::map,
  3017. template<typename U, typename... Args> class ArrayType = std::vector,
  3018. class StringType = std::string, class BooleanType = bool,
  3019. class NumberIntegerType = std::int64_t,
  3020. class NumberUnsignedType = std::uint64_t,
  3021. class NumberFloatType = double,
  3022. template<typename U> class AllocatorType = std::allocator,
  3023. template<typename T, typename SFINAE = void> class JSONSerializer =
  3024. adl_serializer,
  3025. class BinaryType = std::vector<std::uint8_t>>
  3026. class basic_json;
  3027. /// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document
  3028. /// @sa https://json.nlohmann.me/api/json_pointer/
  3029. template<typename RefStringType>
  3030. class json_pointer;
  3031. /*!
  3032. @brief default specialization
  3033. @sa https://json.nlohmann.me/api/json/
  3034. */
  3035. using json = basic_json<>;
  3036. /// @brief a minimal map-like container that preserves insertion order
  3037. /// @sa https://json.nlohmann.me/api/ordered_map/
  3038. template<class Key, class T, class IgnoredLess, class Allocator>
  3039. struct ordered_map;
  3040. /// @brief specialization that maintains the insertion order of object keys
  3041. /// @sa https://json.nlohmann.me/api/ordered_json/
  3042. using ordered_json = basic_json<nlohmann::ordered_map>;
  3043. NLOHMANN_JSON_NAMESPACE_END
  3044. #endif // INCLUDE_NLOHMANN_JSON_FWD_HPP_
  3045. NLOHMANN_JSON_NAMESPACE_BEGIN
  3046. /*!
  3047. @brief detail namespace with internal helper functions
  3048. This namespace collects functions that should not be exposed,
  3049. implementations of some @ref basic_json methods, and meta-programming helpers.
  3050. @since version 2.1.0
  3051. */
  3052. namespace detail
  3053. {
  3054. /////////////
  3055. // helpers //
  3056. /////////////
  3057. // Note to maintainers:
  3058. //
  3059. // Every trait in this file expects a non CV-qualified type.
  3060. // The only exceptions are in the 'aliases for detected' section
  3061. // (i.e. those of the form: decltype(T::member_function(std::declval<T>())))
  3062. //
  3063. // In this case, T has to be properly CV-qualified to constraint the function arguments
  3064. // (e.g. to_json(BasicJsonType&, const T&))
  3065. template<typename> struct is_basic_json : std::false_type {};
  3066. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  3067. struct is_basic_json<NLOHMANN_BASIC_JSON_TPL> : std::true_type {};
  3068. // used by exceptions create() member functions
  3069. // true_type for pointer to possibly cv-qualified basic_json or std::nullptr_t
  3070. // false_type otherwise
  3071. template<typename BasicJsonContext>
  3072. struct is_basic_json_context :
  3073. std::integral_constant < bool,
  3074. is_basic_json<typename std::remove_cv<typename std::remove_pointer<BasicJsonContext>::type>::type>::value
  3075. || std::is_same<BasicJsonContext, std::nullptr_t>::value >
  3076. {};
  3077. //////////////////////
  3078. // json_ref helpers //
  3079. //////////////////////
  3080. template<typename>
  3081. class json_ref;
  3082. template<typename>
  3083. struct is_json_ref : std::false_type {};
  3084. template<typename T>
  3085. struct is_json_ref<json_ref<T>> : std::true_type {};
  3086. //////////////////////////
  3087. // aliases for detected //
  3088. //////////////////////////
  3089. template<typename T>
  3090. using mapped_type_t = typename T::mapped_type;
  3091. template<typename T>
  3092. using key_type_t = typename T::key_type;
  3093. template<typename T>
  3094. using value_type_t = typename T::value_type;
  3095. template<typename T>
  3096. using difference_type_t = typename T::difference_type;
  3097. template<typename T>
  3098. using pointer_t = typename T::pointer;
  3099. template<typename T>
  3100. using reference_t = typename T::reference;
  3101. template<typename T>
  3102. using iterator_category_t = typename T::iterator_category;
  3103. template<typename T, typename... Args>
  3104. using to_json_function = decltype(T::to_json(std::declval<Args>()...));
  3105. template<typename T, typename... Args>
  3106. using from_json_function = decltype(T::from_json(std::declval<Args>()...));
  3107. template<typename T, typename U>
  3108. using get_template_function = decltype(std::declval<T>().template get<U>());
  3109. // trait checking if JSONSerializer<T>::from_json(json const&, udt&) exists
  3110. template<typename BasicJsonType, typename T, typename = void>
  3111. struct has_from_json : std::false_type {};
  3112. // trait checking if j.get<T> is valid
  3113. // use this trait instead of std::is_constructible or std::is_convertible,
  3114. // both rely on, or make use of implicit conversions, and thus fail when T
  3115. // has several constructors/operator= (see https://github.com/nlohmann/json/issues/958)
  3116. template <typename BasicJsonType, typename T>
  3117. struct is_getable
  3118. {
  3119. static constexpr bool value = is_detected<get_template_function, const BasicJsonType&, T>::value;
  3120. };
  3121. template<typename BasicJsonType, typename T>
  3122. struct has_from_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
  3123. {
  3124. using serializer = typename BasicJsonType::template json_serializer<T, void>;
  3125. static constexpr bool value =
  3126. is_detected_exact<void, from_json_function, serializer,
  3127. const BasicJsonType&, T&>::value;
  3128. };
  3129. // This trait checks if JSONSerializer<T>::from_json(json const&) exists
  3130. // this overload is used for non-default-constructible user-defined-types
  3131. template<typename BasicJsonType, typename T, typename = void>
  3132. struct has_non_default_from_json : std::false_type {};
  3133. template<typename BasicJsonType, typename T>
  3134. struct has_non_default_from_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
  3135. {
  3136. using serializer = typename BasicJsonType::template json_serializer<T, void>;
  3137. static constexpr bool value =
  3138. is_detected_exact<T, from_json_function, serializer,
  3139. const BasicJsonType&>::value;
  3140. };
  3141. // This trait checks if BasicJsonType::json_serializer<T>::to_json exists
  3142. // Do not evaluate the trait when T is a basic_json type, to avoid template instantiation infinite recursion.
  3143. template<typename BasicJsonType, typename T, typename = void>
  3144. struct has_to_json : std::false_type {};
  3145. template<typename BasicJsonType, typename T>
  3146. struct has_to_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
  3147. {
  3148. using serializer = typename BasicJsonType::template json_serializer<T, void>;
  3149. static constexpr bool value =
  3150. is_detected_exact<void, to_json_function, serializer, BasicJsonType&,
  3151. T>::value;
  3152. };
  3153. template<typename T>
  3154. using detect_key_compare = typename T::key_compare;
  3155. template<typename T>
  3156. struct has_key_compare : std::integral_constant<bool, is_detected<detect_key_compare, T>::value> {};
  3157. // obtains the actual object key comparator
  3158. template<typename BasicJsonType>
  3159. struct actual_object_comparator
  3160. {
  3161. using object_t = typename BasicJsonType::object_t;
  3162. using object_comparator_t = typename BasicJsonType::default_object_comparator_t;
  3163. using type = typename std::conditional < has_key_compare<object_t>::value,
  3164. typename object_t::key_compare, object_comparator_t>::type;
  3165. };
  3166. template<typename BasicJsonType>
  3167. using actual_object_comparator_t = typename actual_object_comparator<BasicJsonType>::type;
  3168. ///////////////////
  3169. // is_ functions //
  3170. ///////////////////
  3171. // https://en.cppreference.com/w/cpp/types/conjunction
  3172. template<class...> struct conjunction : std::true_type { };
  3173. template<class B> struct conjunction<B> : B { };
  3174. template<class B, class... Bn>
  3175. struct conjunction<B, Bn...>
  3176. : std::conditional<static_cast<bool>(B::value), conjunction<Bn...>, B>::type {};
  3177. // https://en.cppreference.com/w/cpp/types/negation
  3178. template<class B> struct negation : std::integral_constant < bool, !B::value > { };
  3179. // Reimplementation of is_constructible and is_default_constructible, due to them being broken for
  3180. // std::pair and std::tuple until LWG 2367 fix (see https://cplusplus.github.io/LWG/lwg-defects.html#2367).
  3181. // This causes compile errors in e.g. clang 3.5 or gcc 4.9.
  3182. template <typename T>
  3183. struct is_default_constructible : std::is_default_constructible<T> {};
  3184. template <typename T1, typename T2>
  3185. struct is_default_constructible<std::pair<T1, T2>>
  3186. : conjunction<is_default_constructible<T1>, is_default_constructible<T2>> {};
  3187. template <typename T1, typename T2>
  3188. struct is_default_constructible<const std::pair<T1, T2>>
  3189. : conjunction<is_default_constructible<T1>, is_default_constructible<T2>> {};
  3190. template <typename... Ts>
  3191. struct is_default_constructible<std::tuple<Ts...>>
  3192. : conjunction<is_default_constructible<Ts>...> {};
  3193. template <typename... Ts>
  3194. struct is_default_constructible<const std::tuple<Ts...>>
  3195. : conjunction<is_default_constructible<Ts>...> {};
  3196. template <typename T, typename... Args>
  3197. struct is_constructible : std::is_constructible<T, Args...> {};
  3198. template <typename T1, typename T2>
  3199. struct is_constructible<std::pair<T1, T2>> : is_default_constructible<std::pair<T1, T2>> {};
  3200. template <typename T1, typename T2>
  3201. struct is_constructible<const std::pair<T1, T2>> : is_default_constructible<const std::pair<T1, T2>> {};
  3202. template <typename... Ts>
  3203. struct is_constructible<std::tuple<Ts...>> : is_default_constructible<std::tuple<Ts...>> {};
  3204. template <typename... Ts>
  3205. struct is_constructible<const std::tuple<Ts...>> : is_default_constructible<const std::tuple<Ts...>> {};
  3206. template<typename T, typename = void>
  3207. struct is_iterator_traits : std::false_type {};
  3208. template<typename T>
  3209. struct is_iterator_traits<iterator_traits<T>>
  3210. {
  3211. private:
  3212. using traits = iterator_traits<T>;
  3213. public:
  3214. static constexpr auto value =
  3215. is_detected<value_type_t, traits>::value &&
  3216. is_detected<difference_type_t, traits>::value &&
  3217. is_detected<pointer_t, traits>::value &&
  3218. is_detected<iterator_category_t, traits>::value &&
  3219. is_detected<reference_t, traits>::value;
  3220. };
  3221. template<typename T>
  3222. struct is_range
  3223. {
  3224. private:
  3225. using t_ref = typename std::add_lvalue_reference<T>::type;
  3226. using iterator = detected_t<result_of_begin, t_ref>;
  3227. using sentinel = detected_t<result_of_end, t_ref>;
  3228. // to be 100% correct, it should use https://en.cppreference.com/w/cpp/iterator/input_or_output_iterator
  3229. // and https://en.cppreference.com/w/cpp/iterator/sentinel_for
  3230. // but reimplementing these would be too much work, as a lot of other concepts are used underneath
  3231. static constexpr auto is_iterator_begin =
  3232. is_iterator_traits<iterator_traits<iterator>>::value;
  3233. public:
  3234. static constexpr bool value = !std::is_same<iterator, nonesuch>::value && !std::is_same<sentinel, nonesuch>::value && is_iterator_begin;
  3235. };
  3236. template<typename R>
  3237. using iterator_t = enable_if_t<is_range<R>::value, result_of_begin<decltype(std::declval<R&>())>>;
  3238. template<typename T>
  3239. using range_value_t = value_type_t<iterator_traits<iterator_t<T>>>;
  3240. // The following implementation of is_complete_type is taken from
  3241. // https://blogs.msdn.microsoft.com/vcblog/2015/12/02/partial-support-for-expression-sfinae-in-vs-2015-update-1/
  3242. // and is written by Xiang Fan who agreed to using it in this library.
  3243. template<typename T, typename = void>
  3244. struct is_complete_type : std::false_type {};
  3245. template<typename T>
  3246. struct is_complete_type<T, decltype(void(sizeof(T)))> : std::true_type {};
  3247. template<typename BasicJsonType, typename CompatibleObjectType,
  3248. typename = void>
  3249. struct is_compatible_object_type_impl : std::false_type {};
  3250. template<typename BasicJsonType, typename CompatibleObjectType>
  3251. struct is_compatible_object_type_impl <
  3252. BasicJsonType, CompatibleObjectType,
  3253. enable_if_t < is_detected<mapped_type_t, CompatibleObjectType>::value&&
  3254. is_detected<key_type_t, CompatibleObjectType>::value >>
  3255. {
  3256. using object_t = typename BasicJsonType::object_t;
  3257. // macOS's is_constructible does not play well with nonesuch...
  3258. static constexpr bool value =
  3259. is_constructible<typename object_t::key_type,
  3260. typename CompatibleObjectType::key_type>::value &&
  3261. is_constructible<typename object_t::mapped_type,
  3262. typename CompatibleObjectType::mapped_type>::value;
  3263. };
  3264. template<typename BasicJsonType, typename CompatibleObjectType>
  3265. struct is_compatible_object_type
  3266. : is_compatible_object_type_impl<BasicJsonType, CompatibleObjectType> {};
  3267. template<typename BasicJsonType, typename ConstructibleObjectType,
  3268. typename = void>
  3269. struct is_constructible_object_type_impl : std::false_type {};
  3270. template<typename BasicJsonType, typename ConstructibleObjectType>
  3271. struct is_constructible_object_type_impl <
  3272. BasicJsonType, ConstructibleObjectType,
  3273. enable_if_t < is_detected<mapped_type_t, ConstructibleObjectType>::value&&
  3274. is_detected<key_type_t, ConstructibleObjectType>::value >>
  3275. {
  3276. using object_t = typename BasicJsonType::object_t;
  3277. static constexpr bool value =
  3278. (is_default_constructible<ConstructibleObjectType>::value &&
  3279. (std::is_move_assignable<ConstructibleObjectType>::value ||
  3280. std::is_copy_assignable<ConstructibleObjectType>::value) &&
  3281. (is_constructible<typename ConstructibleObjectType::key_type,
  3282. typename object_t::key_type>::value &&
  3283. std::is_same <
  3284. typename object_t::mapped_type,
  3285. typename ConstructibleObjectType::mapped_type >::value)) ||
  3286. (has_from_json<BasicJsonType,
  3287. typename ConstructibleObjectType::mapped_type>::value ||
  3288. has_non_default_from_json <
  3289. BasicJsonType,
  3290. typename ConstructibleObjectType::mapped_type >::value);
  3291. };
  3292. template<typename BasicJsonType, typename ConstructibleObjectType>
  3293. struct is_constructible_object_type
  3294. : is_constructible_object_type_impl<BasicJsonType,
  3295. ConstructibleObjectType> {};
  3296. template<typename BasicJsonType, typename CompatibleStringType>
  3297. struct is_compatible_string_type
  3298. {
  3299. static constexpr auto value =
  3300. is_constructible<typename BasicJsonType::string_t, CompatibleStringType>::value;
  3301. };
  3302. template<typename BasicJsonType, typename ConstructibleStringType>
  3303. struct is_constructible_string_type
  3304. {
  3305. // launder type through decltype() to fix compilation failure on ICPC
  3306. #ifdef __INTEL_COMPILER
  3307. using laundered_type = decltype(std::declval<ConstructibleStringType>());
  3308. #else
  3309. using laundered_type = ConstructibleStringType;
  3310. #endif
  3311. static constexpr auto value =
  3312. conjunction <
  3313. is_constructible<laundered_type, typename BasicJsonType::string_t>,
  3314. is_detected_exact<typename BasicJsonType::string_t::value_type,
  3315. value_type_t, laundered_type >>::value;
  3316. };
  3317. template<typename BasicJsonType, typename CompatibleArrayType, typename = void>
  3318. struct is_compatible_array_type_impl : std::false_type {};
  3319. template<typename BasicJsonType, typename CompatibleArrayType>
  3320. struct is_compatible_array_type_impl <
  3321. BasicJsonType, CompatibleArrayType,
  3322. enable_if_t <
  3323. is_detected<iterator_t, CompatibleArrayType>::value&&
  3324. is_iterator_traits<iterator_traits<detected_t<iterator_t, CompatibleArrayType>>>::value&&
  3325. // special case for types like std::filesystem::path whose iterator's value_type are themselves
  3326. // c.f. https://github.com/nlohmann/json/pull/3073
  3327. !std::is_same<CompatibleArrayType, detected_t<range_value_t, CompatibleArrayType>>::value >>
  3328. {
  3329. static constexpr bool value =
  3330. is_constructible<BasicJsonType,
  3331. range_value_t<CompatibleArrayType>>::value;
  3332. };
  3333. template<typename BasicJsonType, typename CompatibleArrayType>
  3334. struct is_compatible_array_type
  3335. : is_compatible_array_type_impl<BasicJsonType, CompatibleArrayType> {};
  3336. template<typename BasicJsonType, typename ConstructibleArrayType, typename = void>
  3337. struct is_constructible_array_type_impl : std::false_type {};
  3338. template<typename BasicJsonType, typename ConstructibleArrayType>
  3339. struct is_constructible_array_type_impl <
  3340. BasicJsonType, ConstructibleArrayType,
  3341. enable_if_t<std::is_same<ConstructibleArrayType,
  3342. typename BasicJsonType::value_type>::value >>
  3343. : std::true_type {};
  3344. template<typename BasicJsonType, typename ConstructibleArrayType>
  3345. struct is_constructible_array_type_impl <
  3346. BasicJsonType, ConstructibleArrayType,
  3347. enable_if_t < !std::is_same<ConstructibleArrayType,
  3348. typename BasicJsonType::value_type>::value&&
  3349. !is_compatible_string_type<BasicJsonType, ConstructibleArrayType>::value&&
  3350. is_default_constructible<ConstructibleArrayType>::value&&
  3351. (std::is_move_assignable<ConstructibleArrayType>::value ||
  3352. std::is_copy_assignable<ConstructibleArrayType>::value)&&
  3353. is_detected<iterator_t, ConstructibleArrayType>::value&&
  3354. is_iterator_traits<iterator_traits<detected_t<iterator_t, ConstructibleArrayType>>>::value&&
  3355. is_detected<range_value_t, ConstructibleArrayType>::value&&
  3356. // special case for types like std::filesystem::path whose iterator's value_type are themselves
  3357. // c.f. https://github.com/nlohmann/json/pull/3073
  3358. !std::is_same<ConstructibleArrayType, detected_t<range_value_t, ConstructibleArrayType>>::value&&
  3359. is_complete_type <
  3360. detected_t<range_value_t, ConstructibleArrayType >>::value >>
  3361. {
  3362. using value_type = range_value_t<ConstructibleArrayType>;
  3363. static constexpr bool value =
  3364. std::is_same<value_type,
  3365. typename BasicJsonType::array_t::value_type>::value ||
  3366. has_from_json<BasicJsonType,
  3367. value_type>::value ||
  3368. has_non_default_from_json <
  3369. BasicJsonType,
  3370. value_type >::value;
  3371. };
  3372. template<typename BasicJsonType, typename ConstructibleArrayType>
  3373. struct is_constructible_array_type
  3374. : is_constructible_array_type_impl<BasicJsonType, ConstructibleArrayType> {};
  3375. template<typename RealIntegerType, typename CompatibleNumberIntegerType,
  3376. typename = void>
  3377. struct is_compatible_integer_type_impl : std::false_type {};
  3378. template<typename RealIntegerType, typename CompatibleNumberIntegerType>
  3379. struct is_compatible_integer_type_impl <
  3380. RealIntegerType, CompatibleNumberIntegerType,
  3381. enable_if_t < std::is_integral<RealIntegerType>::value&&
  3382. std::is_integral<CompatibleNumberIntegerType>::value&&
  3383. !std::is_same<bool, CompatibleNumberIntegerType>::value >>
  3384. {
  3385. // is there an assert somewhere on overflows?
  3386. using RealLimits = std::numeric_limits<RealIntegerType>;
  3387. using CompatibleLimits = std::numeric_limits<CompatibleNumberIntegerType>;
  3388. static constexpr auto value =
  3389. is_constructible<RealIntegerType,
  3390. CompatibleNumberIntegerType>::value &&
  3391. CompatibleLimits::is_integer &&
  3392. RealLimits::is_signed == CompatibleLimits::is_signed;
  3393. };
  3394. template<typename RealIntegerType, typename CompatibleNumberIntegerType>
  3395. struct is_compatible_integer_type
  3396. : is_compatible_integer_type_impl<RealIntegerType,
  3397. CompatibleNumberIntegerType> {};
  3398. template<typename BasicJsonType, typename CompatibleType, typename = void>
  3399. struct is_compatible_type_impl: std::false_type {};
  3400. template<typename BasicJsonType, typename CompatibleType>
  3401. struct is_compatible_type_impl <
  3402. BasicJsonType, CompatibleType,
  3403. enable_if_t<is_complete_type<CompatibleType>::value >>
  3404. {
  3405. static constexpr bool value =
  3406. has_to_json<BasicJsonType, CompatibleType>::value;
  3407. };
  3408. template<typename BasicJsonType, typename CompatibleType>
  3409. struct is_compatible_type
  3410. : is_compatible_type_impl<BasicJsonType, CompatibleType> {};
  3411. template<typename T1, typename T2>
  3412. struct is_constructible_tuple : std::false_type {};
  3413. template<typename T1, typename... Args>
  3414. struct is_constructible_tuple<T1, std::tuple<Args...>> : conjunction<is_constructible<T1, Args>...> {};
  3415. template<typename BasicJsonType, typename T>
  3416. struct is_json_iterator_of : std::false_type {};
  3417. template<typename BasicJsonType>
  3418. struct is_json_iterator_of<BasicJsonType, typename BasicJsonType::iterator> : std::true_type {};
  3419. template<typename BasicJsonType>
  3420. struct is_json_iterator_of<BasicJsonType, typename BasicJsonType::const_iterator> : std::true_type
  3421. {};
  3422. // checks if a given type T is a template specialization of Primary
  3423. template<template <typename...> class Primary, typename T>
  3424. struct is_specialization_of : std::false_type {};
  3425. template<template <typename...> class Primary, typename... Args>
  3426. struct is_specialization_of<Primary, Primary<Args...>> : std::true_type {};
  3427. template<typename T>
  3428. using is_json_pointer = is_specialization_of<::nlohmann::json_pointer, uncvref_t<T>>;
  3429. // checks if A and B are comparable using Compare functor
  3430. template<typename Compare, typename A, typename B, typename = void>
  3431. struct is_comparable : std::false_type {};
  3432. template<typename Compare, typename A, typename B>
  3433. struct is_comparable<Compare, A, B, void_t<
  3434. decltype(std::declval<Compare>()(std::declval<A>(), std::declval<B>())),
  3435. decltype(std::declval<Compare>()(std::declval<B>(), std::declval<A>()))
  3436. >> : std::true_type {};
  3437. template<typename T>
  3438. using detect_is_transparent = typename T::is_transparent;
  3439. // type trait to check if KeyType can be used as object key (without a BasicJsonType)
  3440. // see is_usable_as_basic_json_key_type below
  3441. template<typename Comparator, typename ObjectKeyType, typename KeyTypeCVRef, bool RequireTransparentComparator = true,
  3442. bool ExcludeObjectKeyType = RequireTransparentComparator, typename KeyType = uncvref_t<KeyTypeCVRef>>
  3443. using is_usable_as_key_type = typename std::conditional <
  3444. is_comparable<Comparator, ObjectKeyType, KeyTypeCVRef>::value
  3445. && !(ExcludeObjectKeyType && std::is_same<KeyType,
  3446. ObjectKeyType>::value)
  3447. && (!RequireTransparentComparator
  3448. || is_detected <detect_is_transparent, Comparator>::value)
  3449. && !is_json_pointer<KeyType>::value,
  3450. std::true_type,
  3451. std::false_type >::type;
  3452. // type trait to check if KeyType can be used as object key
  3453. // true if:
  3454. // - KeyType is comparable with BasicJsonType::object_t::key_type
  3455. // - if ExcludeObjectKeyType is true, KeyType is not BasicJsonType::object_t::key_type
  3456. // - the comparator is transparent or RequireTransparentComparator is false
  3457. // - KeyType is not a JSON iterator or json_pointer
  3458. template<typename BasicJsonType, typename KeyTypeCVRef, bool RequireTransparentComparator = true,
  3459. bool ExcludeObjectKeyType = RequireTransparentComparator, typename KeyType = uncvref_t<KeyTypeCVRef>>
  3460. using is_usable_as_basic_json_key_type = typename std::conditional <
  3461. is_usable_as_key_type<typename BasicJsonType::object_comparator_t,
  3462. typename BasicJsonType::object_t::key_type, KeyTypeCVRef,
  3463. RequireTransparentComparator, ExcludeObjectKeyType>::value
  3464. && !is_json_iterator_of<BasicJsonType, KeyType>::value,
  3465. std::true_type,
  3466. std::false_type >::type;
  3467. template<typename ObjectType, typename KeyType>
  3468. using detect_erase_with_key_type = decltype(std::declval<ObjectType&>().erase(std::declval<KeyType>()));
  3469. // type trait to check if object_t has an erase() member functions accepting KeyType
  3470. template<typename BasicJsonType, typename KeyType>
  3471. using has_erase_with_key_type = typename std::conditional <
  3472. is_detected <
  3473. detect_erase_with_key_type,
  3474. typename BasicJsonType::object_t, KeyType >::value,
  3475. std::true_type,
  3476. std::false_type >::type;
  3477. // a naive helper to check if a type is an ordered_map (exploits the fact that
  3478. // ordered_map inherits capacity() from std::vector)
  3479. template <typename T>
  3480. struct is_ordered_map
  3481. {
  3482. using one = char;
  3483. struct two
  3484. {
  3485. char x[2]; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  3486. };
  3487. template <typename C> static one test( decltype(&C::capacity) ) ;
  3488. template <typename C> static two test(...);
  3489. enum { value = sizeof(test<T>(nullptr)) == sizeof(char) }; // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  3490. };
  3491. // to avoid useless casts (see https://github.com/nlohmann/json/issues/2893#issuecomment-889152324)
  3492. template < typename T, typename U, enable_if_t < !std::is_same<T, U>::value, int > = 0 >
  3493. T conditional_static_cast(U value)
  3494. {
  3495. return static_cast<T>(value);
  3496. }
  3497. template<typename T, typename U, enable_if_t<std::is_same<T, U>::value, int> = 0>
  3498. T conditional_static_cast(U value)
  3499. {
  3500. return value;
  3501. }
  3502. template<typename... Types>
  3503. using all_integral = conjunction<std::is_integral<Types>...>;
  3504. template<typename... Types>
  3505. using all_signed = conjunction<std::is_signed<Types>...>;
  3506. template<typename... Types>
  3507. using all_unsigned = conjunction<std::is_unsigned<Types>...>;
  3508. // there's a disjunction trait in another PR; replace when merged
  3509. template<typename... Types>
  3510. using same_sign = std::integral_constant < bool,
  3511. all_signed<Types...>::value || all_unsigned<Types...>::value >;
  3512. template<typename OfType, typename T>
  3513. using never_out_of_range = std::integral_constant < bool,
  3514. (std::is_signed<OfType>::value && (sizeof(T) < sizeof(OfType)))
  3515. || (same_sign<OfType, T>::value && sizeof(OfType) == sizeof(T)) >;
  3516. template<typename OfType, typename T,
  3517. bool OfTypeSigned = std::is_signed<OfType>::value,
  3518. bool TSigned = std::is_signed<T>::value>
  3519. struct value_in_range_of_impl2;
  3520. template<typename OfType, typename T>
  3521. struct value_in_range_of_impl2<OfType, T, false, false>
  3522. {
  3523. static constexpr bool test(T val)
  3524. {
  3525. using CommonType = typename std::common_type<OfType, T>::type;
  3526. return static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
  3527. }
  3528. };
  3529. template<typename OfType, typename T>
  3530. struct value_in_range_of_impl2<OfType, T, true, false>
  3531. {
  3532. static constexpr bool test(T val)
  3533. {
  3534. using CommonType = typename std::common_type<OfType, T>::type;
  3535. return static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
  3536. }
  3537. };
  3538. template<typename OfType, typename T>
  3539. struct value_in_range_of_impl2<OfType, T, false, true>
  3540. {
  3541. static constexpr bool test(T val)
  3542. {
  3543. using CommonType = typename std::common_type<OfType, T>::type;
  3544. return val >= 0 && static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
  3545. }
  3546. };
  3547. template<typename OfType, typename T>
  3548. struct value_in_range_of_impl2<OfType, T, true, true>
  3549. {
  3550. static constexpr bool test(T val)
  3551. {
  3552. using CommonType = typename std::common_type<OfType, T>::type;
  3553. return static_cast<CommonType>(val) >= static_cast<CommonType>((std::numeric_limits<OfType>::min)())
  3554. && static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
  3555. }
  3556. };
  3557. template<typename OfType, typename T,
  3558. bool NeverOutOfRange = never_out_of_range<OfType, T>::value,
  3559. typename = detail::enable_if_t<all_integral<OfType, T>::value>>
  3560. struct value_in_range_of_impl1;
  3561. template<typename OfType, typename T>
  3562. struct value_in_range_of_impl1<OfType, T, false>
  3563. {
  3564. static constexpr bool test(T val)
  3565. {
  3566. return value_in_range_of_impl2<OfType, T>::test(val);
  3567. }
  3568. };
  3569. template<typename OfType, typename T>
  3570. struct value_in_range_of_impl1<OfType, T, true>
  3571. {
  3572. static constexpr bool test(T /*val*/)
  3573. {
  3574. return true;
  3575. }
  3576. };
  3577. template<typename OfType, typename T>
  3578. inline constexpr bool value_in_range_of(T val)
  3579. {
  3580. return value_in_range_of_impl1<OfType, T>::test(val);
  3581. }
  3582. template<bool Value>
  3583. using bool_constant = std::integral_constant<bool, Value>;
  3584. ///////////////////////////////////////////////////////////////////////////////
  3585. // is_c_string
  3586. ///////////////////////////////////////////////////////////////////////////////
  3587. namespace impl
  3588. {
  3589. template<typename T>
  3590. inline constexpr bool is_c_string()
  3591. {
  3592. using TUnExt = typename std::remove_extent<T>::type;
  3593. using TUnCVExt = typename std::remove_cv<TUnExt>::type;
  3594. using TUnPtr = typename std::remove_pointer<T>::type;
  3595. using TUnCVPtr = typename std::remove_cv<TUnPtr>::type;
  3596. return
  3597. (std::is_array<T>::value && std::is_same<TUnCVExt, char>::value)
  3598. || (std::is_pointer<T>::value && std::is_same<TUnCVPtr, char>::value);
  3599. }
  3600. } // namespace impl
  3601. // checks whether T is a [cv] char */[cv] char[] C string
  3602. template<typename T>
  3603. struct is_c_string : bool_constant<impl::is_c_string<T>()> {};
  3604. template<typename T>
  3605. using is_c_string_uncvref = is_c_string<uncvref_t<T>>;
  3606. ///////////////////////////////////////////////////////////////////////////////
  3607. // is_transparent
  3608. ///////////////////////////////////////////////////////////////////////////////
  3609. namespace impl
  3610. {
  3611. template<typename T>
  3612. inline constexpr bool is_transparent()
  3613. {
  3614. return is_detected<detect_is_transparent, T>::value;
  3615. }
  3616. } // namespace impl
  3617. // checks whether T has a member named is_transparent
  3618. template<typename T>
  3619. struct is_transparent : bool_constant<impl::is_transparent<T>()> {};
  3620. ///////////////////////////////////////////////////////////////////////////////
  3621. } // namespace detail
  3622. NLOHMANN_JSON_NAMESPACE_END
  3623. // #include <nlohmann/detail/string_concat.hpp>
  3624. // __ _____ _____ _____
  3625. // __| | __| | | | JSON for Modern C++
  3626. // | | |__ | | | | | | version 3.11.2
  3627. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  3628. //
  3629. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  3630. // SPDX-License-Identifier: MIT
  3631. #include <cstring> // strlen
  3632. #include <string> // string
  3633. #include <utility> // forward
  3634. // #include <nlohmann/detail/meta/cpp_future.hpp>
  3635. // #include <nlohmann/detail/meta/detected.hpp>
  3636. NLOHMANN_JSON_NAMESPACE_BEGIN
  3637. namespace detail
  3638. {
  3639. inline std::size_t concat_length()
  3640. {
  3641. return 0;
  3642. }
  3643. template<typename... Args>
  3644. inline std::size_t concat_length(const char* cstr, Args&& ... rest);
  3645. template<typename StringType, typename... Args>
  3646. inline std::size_t concat_length(const StringType& str, Args&& ... rest);
  3647. template<typename... Args>
  3648. inline std::size_t concat_length(const char /*c*/, Args&& ... rest)
  3649. {
  3650. return 1 + concat_length(std::forward<Args>(rest)...);
  3651. }
  3652. template<typename... Args>
  3653. inline std::size_t concat_length(const char* cstr, Args&& ... rest)
  3654. {
  3655. // cppcheck-suppress ignoredReturnValue
  3656. return ::strlen(cstr) + concat_length(std::forward<Args>(rest)...);
  3657. }
  3658. template<typename StringType, typename... Args>
  3659. inline std::size_t concat_length(const StringType& str, Args&& ... rest)
  3660. {
  3661. return str.size() + concat_length(std::forward<Args>(rest)...);
  3662. }
  3663. template<typename OutStringType>
  3664. inline void concat_into(OutStringType& /*out*/)
  3665. {}
  3666. template<typename StringType, typename Arg>
  3667. using string_can_append = decltype(std::declval<StringType&>().append(std::declval < Arg && > ()));
  3668. template<typename StringType, typename Arg>
  3669. using detect_string_can_append = is_detected<string_can_append, StringType, Arg>;
  3670. template<typename StringType, typename Arg>
  3671. using string_can_append_op = decltype(std::declval<StringType&>() += std::declval < Arg && > ());
  3672. template<typename StringType, typename Arg>
  3673. using detect_string_can_append_op = is_detected<string_can_append_op, StringType, Arg>;
  3674. template<typename StringType, typename Arg>
  3675. using string_can_append_iter = decltype(std::declval<StringType&>().append(std::declval<const Arg&>().begin(), std::declval<const Arg&>().end()));
  3676. template<typename StringType, typename Arg>
  3677. using detect_string_can_append_iter = is_detected<string_can_append_iter, StringType, Arg>;
  3678. template<typename StringType, typename Arg>
  3679. using string_can_append_data = decltype(std::declval<StringType&>().append(std::declval<const Arg&>().data(), std::declval<const Arg&>().size()));
  3680. template<typename StringType, typename Arg>
  3681. using detect_string_can_append_data = is_detected<string_can_append_data, StringType, Arg>;
  3682. template < typename OutStringType, typename Arg, typename... Args,
  3683. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3684. && detect_string_can_append_op<OutStringType, Arg>::value, int > = 0 >
  3685. inline void concat_into(OutStringType& out, Arg && arg, Args && ... rest);
  3686. template < typename OutStringType, typename Arg, typename... Args,
  3687. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3688. && !detect_string_can_append_op<OutStringType, Arg>::value
  3689. && detect_string_can_append_iter<OutStringType, Arg>::value, int > = 0 >
  3690. inline void concat_into(OutStringType& out, const Arg& arg, Args && ... rest);
  3691. template < typename OutStringType, typename Arg, typename... Args,
  3692. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3693. && !detect_string_can_append_op<OutStringType, Arg>::value
  3694. && !detect_string_can_append_iter<OutStringType, Arg>::value
  3695. && detect_string_can_append_data<OutStringType, Arg>::value, int > = 0 >
  3696. inline void concat_into(OutStringType& out, const Arg& arg, Args && ... rest);
  3697. template<typename OutStringType, typename Arg, typename... Args,
  3698. enable_if_t<detect_string_can_append<OutStringType, Arg>::value, int> = 0>
  3699. inline void concat_into(OutStringType& out, Arg && arg, Args && ... rest)
  3700. {
  3701. out.append(std::forward<Arg>(arg));
  3702. concat_into(out, std::forward<Args>(rest)...);
  3703. }
  3704. template < typename OutStringType, typename Arg, typename... Args,
  3705. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3706. && detect_string_can_append_op<OutStringType, Arg>::value, int > >
  3707. inline void concat_into(OutStringType& out, Arg&& arg, Args&& ... rest)
  3708. {
  3709. out += std::forward<Arg>(arg);
  3710. concat_into(out, std::forward<Args>(rest)...);
  3711. }
  3712. template < typename OutStringType, typename Arg, typename... Args,
  3713. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3714. && !detect_string_can_append_op<OutStringType, Arg>::value
  3715. && detect_string_can_append_iter<OutStringType, Arg>::value, int > >
  3716. inline void concat_into(OutStringType& out, const Arg& arg, Args&& ... rest)
  3717. {
  3718. out.append(arg.begin(), arg.end());
  3719. concat_into(out, std::forward<Args>(rest)...);
  3720. }
  3721. template < typename OutStringType, typename Arg, typename... Args,
  3722. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3723. && !detect_string_can_append_op<OutStringType, Arg>::value
  3724. && !detect_string_can_append_iter<OutStringType, Arg>::value
  3725. && detect_string_can_append_data<OutStringType, Arg>::value, int > >
  3726. inline void concat_into(OutStringType& out, const Arg& arg, Args&& ... rest)
  3727. {
  3728. out.append(arg.data(), arg.size());
  3729. concat_into(out, std::forward<Args>(rest)...);
  3730. }
  3731. template<typename OutStringType = std::string, typename... Args>
  3732. inline OutStringType concat(Args && ... args)
  3733. {
  3734. OutStringType str;
  3735. str.reserve(concat_length(std::forward<Args>(args)...));
  3736. concat_into(str, std::forward<Args>(args)...);
  3737. return str;
  3738. }
  3739. } // namespace detail
  3740. NLOHMANN_JSON_NAMESPACE_END
  3741. NLOHMANN_JSON_NAMESPACE_BEGIN
  3742. namespace detail
  3743. {
  3744. ////////////////
  3745. // exceptions //
  3746. ////////////////
  3747. /// @brief general exception of the @ref basic_json class
  3748. /// @sa https://json.nlohmann.me/api/basic_json/exception/
  3749. class exception : public std::exception
  3750. {
  3751. public:
  3752. /// returns the explanatory string
  3753. const char* what() const noexcept override
  3754. {
  3755. return m.what();
  3756. }
  3757. /// the id of the exception
  3758. const int id; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes)
  3759. protected:
  3760. JSON_HEDLEY_NON_NULL(3)
  3761. exception(int id_, const char* what_arg) : id(id_), m(what_arg) {} // NOLINT(bugprone-throw-keyword-missing)
  3762. static std::string name(const std::string& ename, int id_)
  3763. {
  3764. return concat("[json.exception.", ename, '.', std::to_string(id_), "] ");
  3765. }
  3766. static std::string diagnostics(std::nullptr_t /*leaf_element*/)
  3767. {
  3768. return "";
  3769. }
  3770. template<typename BasicJsonType>
  3771. static std::string diagnostics(const BasicJsonType* leaf_element)
  3772. {
  3773. #if JSON_DIAGNOSTICS
  3774. std::vector<std::string> tokens;
  3775. for (const auto* current = leaf_element; current != nullptr && current->m_parent != nullptr; current = current->m_parent)
  3776. {
  3777. switch (current->m_parent->type())
  3778. {
  3779. case value_t::array:
  3780. {
  3781. for (std::size_t i = 0; i < current->m_parent->m_value.array->size(); ++i)
  3782. {
  3783. if (&current->m_parent->m_value.array->operator[](i) == current)
  3784. {
  3785. tokens.emplace_back(std::to_string(i));
  3786. break;
  3787. }
  3788. }
  3789. break;
  3790. }
  3791. case value_t::object:
  3792. {
  3793. for (const auto& element : *current->m_parent->m_value.object)
  3794. {
  3795. if (&element.second == current)
  3796. {
  3797. tokens.emplace_back(element.first.c_str());
  3798. break;
  3799. }
  3800. }
  3801. break;
  3802. }
  3803. case value_t::null: // LCOV_EXCL_LINE
  3804. case value_t::string: // LCOV_EXCL_LINE
  3805. case value_t::boolean: // LCOV_EXCL_LINE
  3806. case value_t::number_integer: // LCOV_EXCL_LINE
  3807. case value_t::number_unsigned: // LCOV_EXCL_LINE
  3808. case value_t::number_float: // LCOV_EXCL_LINE
  3809. case value_t::binary: // LCOV_EXCL_LINE
  3810. case value_t::discarded: // LCOV_EXCL_LINE
  3811. default: // LCOV_EXCL_LINE
  3812. break; // LCOV_EXCL_LINE
  3813. }
  3814. }
  3815. if (tokens.empty())
  3816. {
  3817. return "";
  3818. }
  3819. auto str = std::accumulate(tokens.rbegin(), tokens.rend(), std::string{},
  3820. [](const std::string & a, const std::string & b)
  3821. {
  3822. return concat(a, '/', detail::escape(b));
  3823. });
  3824. return concat('(', str, ") ");
  3825. #else
  3826. static_cast<void>(leaf_element);
  3827. return "";
  3828. #endif
  3829. }
  3830. private:
  3831. /// an exception object as storage for error messages
  3832. std::runtime_error m;
  3833. };
  3834. /// @brief exception indicating a parse error
  3835. /// @sa https://json.nlohmann.me/api/basic_json/parse_error/
  3836. class parse_error : public exception
  3837. {
  3838. public:
  3839. /*!
  3840. @brief create a parse error exception
  3841. @param[in] id_ the id of the exception
  3842. @param[in] pos the position where the error occurred (or with
  3843. chars_read_total=0 if the position cannot be
  3844. determined)
  3845. @param[in] what_arg the explanatory string
  3846. @return parse_error object
  3847. */
  3848. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3849. static parse_error create(int id_, const position_t& pos, const std::string& what_arg, BasicJsonContext context)
  3850. {
  3851. std::string w = concat(exception::name("parse_error", id_), "parse error",
  3852. position_string(pos), ": ", exception::diagnostics(context), what_arg);
  3853. return {id_, pos.chars_read_total, w.c_str()};
  3854. }
  3855. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3856. static parse_error create(int id_, std::size_t byte_, const std::string& what_arg, BasicJsonContext context)
  3857. {
  3858. std::string w = concat(exception::name("parse_error", id_), "parse error",
  3859. (byte_ != 0 ? (concat(" at byte ", std::to_string(byte_))) : ""),
  3860. ": ", exception::diagnostics(context), what_arg);
  3861. return {id_, byte_, w.c_str()};
  3862. }
  3863. /*!
  3864. @brief byte index of the parse error
  3865. The byte index of the last read character in the input file.
  3866. @note For an input with n bytes, 1 is the index of the first character and
  3867. n+1 is the index of the terminating null byte or the end of file.
  3868. This also holds true when reading a byte vector (CBOR or MessagePack).
  3869. */
  3870. const std::size_t byte;
  3871. private:
  3872. parse_error(int id_, std::size_t byte_, const char* what_arg)
  3873. : exception(id_, what_arg), byte(byte_) {}
  3874. static std::string position_string(const position_t& pos)
  3875. {
  3876. return concat(" at line ", std::to_string(pos.lines_read + 1),
  3877. ", column ", std::to_string(pos.chars_read_current_line));
  3878. }
  3879. };
  3880. /// @brief exception indicating errors with iterators
  3881. /// @sa https://json.nlohmann.me/api/basic_json/invalid_iterator/
  3882. class invalid_iterator : public exception
  3883. {
  3884. public:
  3885. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3886. static invalid_iterator create(int id_, const std::string& what_arg, BasicJsonContext context)
  3887. {
  3888. std::string w = concat(exception::name("invalid_iterator", id_), exception::diagnostics(context), what_arg);
  3889. return {id_, w.c_str()};
  3890. }
  3891. private:
  3892. JSON_HEDLEY_NON_NULL(3)
  3893. invalid_iterator(int id_, const char* what_arg)
  3894. : exception(id_, what_arg) {}
  3895. };
  3896. /// @brief exception indicating executing a member function with a wrong type
  3897. /// @sa https://json.nlohmann.me/api/basic_json/type_error/
  3898. class type_error : public exception
  3899. {
  3900. public:
  3901. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3902. static type_error create(int id_, const std::string& what_arg, BasicJsonContext context)
  3903. {
  3904. std::string w = concat(exception::name("type_error", id_), exception::diagnostics(context), what_arg);
  3905. return {id_, w.c_str()};
  3906. }
  3907. private:
  3908. JSON_HEDLEY_NON_NULL(3)
  3909. type_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
  3910. };
  3911. /// @brief exception indicating access out of the defined range
  3912. /// @sa https://json.nlohmann.me/api/basic_json/out_of_range/
  3913. class out_of_range : public exception
  3914. {
  3915. public:
  3916. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3917. static out_of_range create(int id_, const std::string& what_arg, BasicJsonContext context)
  3918. {
  3919. std::string w = concat(exception::name("out_of_range", id_), exception::diagnostics(context), what_arg);
  3920. return {id_, w.c_str()};
  3921. }
  3922. private:
  3923. JSON_HEDLEY_NON_NULL(3)
  3924. out_of_range(int id_, const char* what_arg) : exception(id_, what_arg) {}
  3925. };
  3926. /// @brief exception indicating other library errors
  3927. /// @sa https://json.nlohmann.me/api/basic_json/other_error/
  3928. class other_error : public exception
  3929. {
  3930. public:
  3931. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3932. static other_error create(int id_, const std::string& what_arg, BasicJsonContext context)
  3933. {
  3934. std::string w = concat(exception::name("other_error", id_), exception::diagnostics(context), what_arg);
  3935. return {id_, w.c_str()};
  3936. }
  3937. private:
  3938. JSON_HEDLEY_NON_NULL(3)
  3939. other_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
  3940. };
  3941. } // namespace detail
  3942. NLOHMANN_JSON_NAMESPACE_END
  3943. // #include <nlohmann/detail/macro_scope.hpp>
  3944. // #include <nlohmann/detail/meta/cpp_future.hpp>
  3945. // #include <nlohmann/detail/meta/identity_tag.hpp>
  3946. // __ _____ _____ _____
  3947. // __| | __| | | | JSON for Modern C++
  3948. // | | |__ | | | | | | version 3.11.2
  3949. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  3950. //
  3951. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  3952. // SPDX-License-Identifier: MIT
  3953. // #include <nlohmann/detail/abi_macros.hpp>
  3954. NLOHMANN_JSON_NAMESPACE_BEGIN
  3955. namespace detail
  3956. {
  3957. // dispatching helper struct
  3958. template <class T> struct identity_tag {};
  3959. } // namespace detail
  3960. NLOHMANN_JSON_NAMESPACE_END
  3961. // #include <nlohmann/detail/meta/std_fs.hpp>
  3962. // __ _____ _____ _____
  3963. // __| | __| | | | JSON for Modern C++
  3964. // | | |__ | | | | | | version 3.11.2
  3965. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  3966. //
  3967. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  3968. // SPDX-License-Identifier: MIT
  3969. // #include <nlohmann/detail/macro_scope.hpp>
  3970. #if JSON_HAS_EXPERIMENTAL_FILESYSTEM
  3971. #include <experimental/filesystem>
  3972. NLOHMANN_JSON_NAMESPACE_BEGIN
  3973. namespace detail
  3974. {
  3975. namespace std_fs = std::experimental::filesystem;
  3976. } // namespace detail
  3977. NLOHMANN_JSON_NAMESPACE_END
  3978. #elif JSON_HAS_FILESYSTEM
  3979. #include <filesystem>
  3980. NLOHMANN_JSON_NAMESPACE_BEGIN
  3981. namespace detail
  3982. {
  3983. namespace std_fs = std::filesystem;
  3984. } // namespace detail
  3985. NLOHMANN_JSON_NAMESPACE_END
  3986. #endif
  3987. // #include <nlohmann/detail/meta/type_traits.hpp>
  3988. // #include <nlohmann/detail/string_concat.hpp>
  3989. // #include <nlohmann/detail/value_t.hpp>
  3990. NLOHMANN_JSON_NAMESPACE_BEGIN
  3991. namespace detail
  3992. {
  3993. template<typename BasicJsonType>
  3994. inline void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
  3995. {
  3996. if (JSON_HEDLEY_UNLIKELY(!j.is_null()))
  3997. {
  3998. JSON_THROW(type_error::create(302, concat("type must be null, but is ", j.type_name()), &j));
  3999. }
  4000. n = nullptr;
  4001. }
  4002. // overloads for basic_json template parameters
  4003. template < typename BasicJsonType, typename ArithmeticType,
  4004. enable_if_t < std::is_arithmetic<ArithmeticType>::value&&
  4005. !std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
  4006. int > = 0 >
  4007. void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val)
  4008. {
  4009. switch (static_cast<value_t>(j))
  4010. {
  4011. case value_t::number_unsigned:
  4012. {
  4013. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
  4014. break;
  4015. }
  4016. case value_t::number_integer:
  4017. {
  4018. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
  4019. break;
  4020. }
  4021. case value_t::number_float:
  4022. {
  4023. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
  4024. break;
  4025. }
  4026. case value_t::null:
  4027. case value_t::object:
  4028. case value_t::array:
  4029. case value_t::string:
  4030. case value_t::boolean:
  4031. case value_t::binary:
  4032. case value_t::discarded:
  4033. default:
  4034. JSON_THROW(type_error::create(302, concat("type must be number, but is ", j.type_name()), &j));
  4035. }
  4036. }
  4037. template<typename BasicJsonType>
  4038. inline void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
  4039. {
  4040. if (JSON_HEDLEY_UNLIKELY(!j.is_boolean()))
  4041. {
  4042. JSON_THROW(type_error::create(302, concat("type must be boolean, but is ", j.type_name()), &j));
  4043. }
  4044. b = *j.template get_ptr<const typename BasicJsonType::boolean_t*>();
  4045. }
  4046. template<typename BasicJsonType>
  4047. inline void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s)
  4048. {
  4049. if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
  4050. {
  4051. JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
  4052. }
  4053. s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
  4054. }
  4055. template <
  4056. typename BasicJsonType, typename StringType,
  4057. enable_if_t <
  4058. std::is_assignable<StringType&, const typename BasicJsonType::string_t>::value
  4059. && is_detected_exact<typename BasicJsonType::string_t::value_type, value_type_t, StringType>::value
  4060. && !std::is_same<typename BasicJsonType::string_t, StringType>::value
  4061. && !is_json_ref<StringType>::value, int > = 0 >
  4062. inline void from_json(const BasicJsonType& j, StringType& s)
  4063. {
  4064. if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
  4065. {
  4066. JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
  4067. }
  4068. s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
  4069. }
  4070. template<typename BasicJsonType>
  4071. inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val)
  4072. {
  4073. get_arithmetic_value(j, val);
  4074. }
  4075. template<typename BasicJsonType>
  4076. inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_unsigned_t& val)
  4077. {
  4078. get_arithmetic_value(j, val);
  4079. }
  4080. template<typename BasicJsonType>
  4081. inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& val)
  4082. {
  4083. get_arithmetic_value(j, val);
  4084. }
  4085. #if !JSON_DISABLE_ENUM_SERIALIZATION
  4086. template<typename BasicJsonType, typename EnumType,
  4087. enable_if_t<std::is_enum<EnumType>::value, int> = 0>
  4088. inline void from_json(const BasicJsonType& j, EnumType& e)
  4089. {
  4090. typename std::underlying_type<EnumType>::type val;
  4091. get_arithmetic_value(j, val);
  4092. e = static_cast<EnumType>(val);
  4093. }
  4094. #endif // JSON_DISABLE_ENUM_SERIALIZATION
  4095. // forward_list doesn't have an insert method
  4096. template<typename BasicJsonType, typename T, typename Allocator,
  4097. enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
  4098. inline void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
  4099. {
  4100. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4101. {
  4102. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4103. }
  4104. l.clear();
  4105. std::transform(j.rbegin(), j.rend(),
  4106. std::front_inserter(l), [](const BasicJsonType & i)
  4107. {
  4108. return i.template get<T>();
  4109. });
  4110. }
  4111. // valarray doesn't have an insert method
  4112. template<typename BasicJsonType, typename T,
  4113. enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
  4114. inline void from_json(const BasicJsonType& j, std::valarray<T>& l)
  4115. {
  4116. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4117. {
  4118. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4119. }
  4120. l.resize(j.size());
  4121. std::transform(j.begin(), j.end(), std::begin(l),
  4122. [](const BasicJsonType & elem)
  4123. {
  4124. return elem.template get<T>();
  4125. });
  4126. }
  4127. template<typename BasicJsonType, typename T, std::size_t N>
  4128. auto from_json(const BasicJsonType& j, T (&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  4129. -> decltype(j.template get<T>(), void())
  4130. {
  4131. for (std::size_t i = 0; i < N; ++i)
  4132. {
  4133. arr[i] = j.at(i).template get<T>();
  4134. }
  4135. }
  4136. template<typename BasicJsonType>
  4137. inline void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType::array_t& arr, priority_tag<3> /*unused*/)
  4138. {
  4139. arr = *j.template get_ptr<const typename BasicJsonType::array_t*>();
  4140. }
  4141. template<typename BasicJsonType, typename T, std::size_t N>
  4142. auto from_json_array_impl(const BasicJsonType& j, std::array<T, N>& arr,
  4143. priority_tag<2> /*unused*/)
  4144. -> decltype(j.template get<T>(), void())
  4145. {
  4146. for (std::size_t i = 0; i < N; ++i)
  4147. {
  4148. arr[i] = j.at(i).template get<T>();
  4149. }
  4150. }
  4151. template<typename BasicJsonType, typename ConstructibleArrayType,
  4152. enable_if_t<
  4153. std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
  4154. int> = 0>
  4155. auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, priority_tag<1> /*unused*/)
  4156. -> decltype(
  4157. arr.reserve(std::declval<typename ConstructibleArrayType::size_type>()),
  4158. j.template get<typename ConstructibleArrayType::value_type>(),
  4159. void())
  4160. {
  4161. using std::end;
  4162. ConstructibleArrayType ret;
  4163. ret.reserve(j.size());
  4164. std::transform(j.begin(), j.end(),
  4165. std::inserter(ret, end(ret)), [](const BasicJsonType & i)
  4166. {
  4167. // get<BasicJsonType>() returns *this, this won't call a from_json
  4168. // method when value_type is BasicJsonType
  4169. return i.template get<typename ConstructibleArrayType::value_type>();
  4170. });
  4171. arr = std::move(ret);
  4172. }
  4173. template<typename BasicJsonType, typename ConstructibleArrayType,
  4174. enable_if_t<
  4175. std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
  4176. int> = 0>
  4177. inline void from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr,
  4178. priority_tag<0> /*unused*/)
  4179. {
  4180. using std::end;
  4181. ConstructibleArrayType ret;
  4182. std::transform(
  4183. j.begin(), j.end(), std::inserter(ret, end(ret)),
  4184. [](const BasicJsonType & i)
  4185. {
  4186. // get<BasicJsonType>() returns *this, this won't call a from_json
  4187. // method when value_type is BasicJsonType
  4188. return i.template get<typename ConstructibleArrayType::value_type>();
  4189. });
  4190. arr = std::move(ret);
  4191. }
  4192. template < typename BasicJsonType, typename ConstructibleArrayType,
  4193. enable_if_t <
  4194. is_constructible_array_type<BasicJsonType, ConstructibleArrayType>::value&&
  4195. !is_constructible_object_type<BasicJsonType, ConstructibleArrayType>::value&&
  4196. !is_constructible_string_type<BasicJsonType, ConstructibleArrayType>::value&&
  4197. !std::is_same<ConstructibleArrayType, typename BasicJsonType::binary_t>::value&&
  4198. !is_basic_json<ConstructibleArrayType>::value,
  4199. int > = 0 >
  4200. auto from_json(const BasicJsonType& j, ConstructibleArrayType& arr)
  4201. -> decltype(from_json_array_impl(j, arr, priority_tag<3> {}),
  4202. j.template get<typename ConstructibleArrayType::value_type>(),
  4203. void())
  4204. {
  4205. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4206. {
  4207. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4208. }
  4209. from_json_array_impl(j, arr, priority_tag<3> {});
  4210. }
  4211. template < typename BasicJsonType, typename T, std::size_t... Idx >
  4212. std::array<T, sizeof...(Idx)> from_json_inplace_array_impl(BasicJsonType&& j,
  4213. identity_tag<std::array<T, sizeof...(Idx)>> /*unused*/, index_sequence<Idx...> /*unused*/)
  4214. {
  4215. return { { std::forward<BasicJsonType>(j).at(Idx).template get<T>()... } };
  4216. }
  4217. template < typename BasicJsonType, typename T, std::size_t N >
  4218. auto from_json(BasicJsonType&& j, identity_tag<std::array<T, N>> tag)
  4219. -> decltype(from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {}))
  4220. {
  4221. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4222. {
  4223. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4224. }
  4225. return from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {});
  4226. }
  4227. template<typename BasicJsonType>
  4228. inline void from_json(const BasicJsonType& j, typename BasicJsonType::binary_t& bin)
  4229. {
  4230. if (JSON_HEDLEY_UNLIKELY(!j.is_binary()))
  4231. {
  4232. JSON_THROW(type_error::create(302, concat("type must be binary, but is ", j.type_name()), &j));
  4233. }
  4234. bin = *j.template get_ptr<const typename BasicJsonType::binary_t*>();
  4235. }
  4236. template<typename BasicJsonType, typename ConstructibleObjectType,
  4237. enable_if_t<is_constructible_object_type<BasicJsonType, ConstructibleObjectType>::value, int> = 0>
  4238. inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
  4239. {
  4240. if (JSON_HEDLEY_UNLIKELY(!j.is_object()))
  4241. {
  4242. JSON_THROW(type_error::create(302, concat("type must be object, but is ", j.type_name()), &j));
  4243. }
  4244. ConstructibleObjectType ret;
  4245. const auto* inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
  4246. using value_type = typename ConstructibleObjectType::value_type;
  4247. std::transform(
  4248. inner_object->begin(), inner_object->end(),
  4249. std::inserter(ret, ret.begin()),
  4250. [](typename BasicJsonType::object_t::value_type const & p)
  4251. {
  4252. return value_type(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
  4253. });
  4254. obj = std::move(ret);
  4255. }
  4256. // overload for arithmetic types, not chosen for basic_json template arguments
  4257. // (BooleanType, etc..); note: Is it really necessary to provide explicit
  4258. // overloads for boolean_t etc. in case of a custom BooleanType which is not
  4259. // an arithmetic type?
  4260. template < typename BasicJsonType, typename ArithmeticType,
  4261. enable_if_t <
  4262. std::is_arithmetic<ArithmeticType>::value&&
  4263. !std::is_same<ArithmeticType, typename BasicJsonType::number_unsigned_t>::value&&
  4264. !std::is_same<ArithmeticType, typename BasicJsonType::number_integer_t>::value&&
  4265. !std::is_same<ArithmeticType, typename BasicJsonType::number_float_t>::value&&
  4266. !std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
  4267. int > = 0 >
  4268. inline void from_json(const BasicJsonType& j, ArithmeticType& val)
  4269. {
  4270. switch (static_cast<value_t>(j))
  4271. {
  4272. case value_t::number_unsigned:
  4273. {
  4274. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
  4275. break;
  4276. }
  4277. case value_t::number_integer:
  4278. {
  4279. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
  4280. break;
  4281. }
  4282. case value_t::number_float:
  4283. {
  4284. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
  4285. break;
  4286. }
  4287. case value_t::boolean:
  4288. {
  4289. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::boolean_t*>());
  4290. break;
  4291. }
  4292. case value_t::null:
  4293. case value_t::object:
  4294. case value_t::array:
  4295. case value_t::string:
  4296. case value_t::binary:
  4297. case value_t::discarded:
  4298. default:
  4299. JSON_THROW(type_error::create(302, concat("type must be number, but is ", j.type_name()), &j));
  4300. }
  4301. }
  4302. template<typename BasicJsonType, typename... Args, std::size_t... Idx>
  4303. std::tuple<Args...> from_json_tuple_impl_base(BasicJsonType&& j, index_sequence<Idx...> /*unused*/)
  4304. {
  4305. return std::make_tuple(std::forward<BasicJsonType>(j).at(Idx).template get<Args>()...);
  4306. }
  4307. template < typename BasicJsonType, class A1, class A2 >
  4308. std::pair<A1, A2> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::pair<A1, A2>> /*unused*/, priority_tag<0> /*unused*/)
  4309. {
  4310. return {std::forward<BasicJsonType>(j).at(0).template get<A1>(),
  4311. std::forward<BasicJsonType>(j).at(1).template get<A2>()};
  4312. }
  4313. template<typename BasicJsonType, typename A1, typename A2>
  4314. inline void from_json_tuple_impl(BasicJsonType&& j, std::pair<A1, A2>& p, priority_tag<1> /*unused*/)
  4315. {
  4316. p = from_json_tuple_impl(std::forward<BasicJsonType>(j), identity_tag<std::pair<A1, A2>> {}, priority_tag<0> {});
  4317. }
  4318. template<typename BasicJsonType, typename... Args>
  4319. std::tuple<Args...> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::tuple<Args...>> /*unused*/, priority_tag<2> /*unused*/)
  4320. {
  4321. return from_json_tuple_impl_base<BasicJsonType, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
  4322. }
  4323. template<typename BasicJsonType, typename... Args>
  4324. inline void from_json_tuple_impl(BasicJsonType&& j, std::tuple<Args...>& t, priority_tag<3> /*unused*/)
  4325. {
  4326. t = from_json_tuple_impl_base<BasicJsonType, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
  4327. }
  4328. template<typename BasicJsonType, typename TupleRelated>
  4329. auto from_json(BasicJsonType&& j, TupleRelated&& t)
  4330. -> decltype(from_json_tuple_impl(std::forward<BasicJsonType>(j), std::forward<TupleRelated>(t), priority_tag<3> {}))
  4331. {
  4332. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4333. {
  4334. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4335. }
  4336. return from_json_tuple_impl(std::forward<BasicJsonType>(j), std::forward<TupleRelated>(t), priority_tag<3> {});
  4337. }
  4338. template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator,
  4339. typename = enable_if_t < !std::is_constructible <
  4340. typename BasicJsonType::string_t, Key >::value >>
  4341. inline void from_json(const BasicJsonType& j, std::map<Key, Value, Compare, Allocator>& m)
  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. m.clear();
  4348. for (const auto& p : j)
  4349. {
  4350. if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
  4351. {
  4352. JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &j));
  4353. }
  4354. m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
  4355. }
  4356. }
  4357. template < typename BasicJsonType, typename Key, typename Value, typename Hash, typename KeyEqual, typename Allocator,
  4358. typename = enable_if_t < !std::is_constructible <
  4359. typename BasicJsonType::string_t, Key >::value >>
  4360. inline void from_json(const BasicJsonType& j, std::unordered_map<Key, Value, Hash, KeyEqual, Allocator>& m)
  4361. {
  4362. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4363. {
  4364. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4365. }
  4366. m.clear();
  4367. for (const auto& p : j)
  4368. {
  4369. if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
  4370. {
  4371. JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &j));
  4372. }
  4373. m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
  4374. }
  4375. }
  4376. #if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
  4377. template<typename BasicJsonType>
  4378. inline void from_json(const BasicJsonType& j, std_fs::path& p)
  4379. {
  4380. if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
  4381. {
  4382. JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
  4383. }
  4384. p = *j.template get_ptr<const typename BasicJsonType::string_t*>();
  4385. }
  4386. #endif
  4387. struct from_json_fn
  4388. {
  4389. template<typename BasicJsonType, typename T>
  4390. auto operator()(const BasicJsonType& j, T&& val) const
  4391. noexcept(noexcept(from_json(j, std::forward<T>(val))))
  4392. -> decltype(from_json(j, std::forward<T>(val)))
  4393. {
  4394. return from_json(j, std::forward<T>(val));
  4395. }
  4396. };
  4397. } // namespace detail
  4398. #ifndef JSON_HAS_CPP_17
  4399. /// namespace to hold default `from_json` function
  4400. /// to see why this is required:
  4401. /// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
  4402. namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces)
  4403. {
  4404. #endif
  4405. JSON_INLINE_VARIABLE constexpr const auto& from_json = // NOLINT(misc-definitions-in-headers)
  4406. detail::static_const<detail::from_json_fn>::value;
  4407. #ifndef JSON_HAS_CPP_17
  4408. } // namespace
  4409. #endif
  4410. NLOHMANN_JSON_NAMESPACE_END
  4411. // #include <nlohmann/detail/conversions/to_json.hpp>
  4412. // __ _____ _____ _____
  4413. // __| | __| | | | JSON for Modern C++
  4414. // | | |__ | | | | | | version 3.11.2
  4415. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  4416. //
  4417. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  4418. // SPDX-License-Identifier: MIT
  4419. #include <algorithm> // copy
  4420. #include <iterator> // begin, end
  4421. #include <string> // string
  4422. #include <tuple> // tuple, get
  4423. #include <type_traits> // is_same, is_constructible, is_floating_point, is_enum, underlying_type
  4424. #include <utility> // move, forward, declval, pair
  4425. #include <valarray> // valarray
  4426. #include <vector> // vector
  4427. // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
  4428. // __ _____ _____ _____
  4429. // __| | __| | | | JSON for Modern C++
  4430. // | | |__ | | | | | | version 3.11.2
  4431. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  4432. //
  4433. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  4434. // SPDX-License-Identifier: MIT
  4435. #include <cstddef> // size_t
  4436. #include <iterator> // input_iterator_tag
  4437. #include <string> // string, to_string
  4438. #include <tuple> // tuple_size, get, tuple_element
  4439. #include <utility> // move
  4440. #if JSON_HAS_RANGES
  4441. #include <ranges> // enable_borrowed_range
  4442. #endif
  4443. // #include <nlohmann/detail/abi_macros.hpp>
  4444. // #include <nlohmann/detail/meta/type_traits.hpp>
  4445. // #include <nlohmann/detail/value_t.hpp>
  4446. NLOHMANN_JSON_NAMESPACE_BEGIN
  4447. namespace detail
  4448. {
  4449. template<typename string_type>
  4450. void int_to_string( string_type& target, std::size_t value )
  4451. {
  4452. // For ADL
  4453. using std::to_string;
  4454. target = to_string(value);
  4455. }
  4456. template<typename IteratorType> class iteration_proxy_value
  4457. {
  4458. public:
  4459. using difference_type = std::ptrdiff_t;
  4460. using value_type = iteration_proxy_value;
  4461. using pointer = value_type *;
  4462. using reference = value_type &;
  4463. using iterator_category = std::input_iterator_tag;
  4464. using string_type = typename std::remove_cv< typename std::remove_reference<decltype( std::declval<IteratorType>().key() ) >::type >::type;
  4465. private:
  4466. /// the iterator
  4467. IteratorType anchor{};
  4468. /// an index for arrays (used to create key names)
  4469. std::size_t array_index = 0;
  4470. /// last stringified array index
  4471. mutable std::size_t array_index_last = 0;
  4472. /// a string representation of the array index
  4473. mutable string_type array_index_str = "0";
  4474. /// an empty string (to return a reference for primitive values)
  4475. string_type empty_str{};
  4476. public:
  4477. explicit iteration_proxy_value() = default;
  4478. explicit iteration_proxy_value(IteratorType it, std::size_t array_index_ = 0)
  4479. noexcept(std::is_nothrow_move_constructible<IteratorType>::value
  4480. && std::is_nothrow_default_constructible<string_type>::value)
  4481. : anchor(std::move(it))
  4482. , array_index(array_index_)
  4483. {}
  4484. iteration_proxy_value(iteration_proxy_value const&) = default;
  4485. iteration_proxy_value& operator=(iteration_proxy_value const&) = default;
  4486. // older GCCs are a bit fussy and require explicit noexcept specifiers on defaulted functions
  4487. iteration_proxy_value(iteration_proxy_value&&)
  4488. noexcept(std::is_nothrow_move_constructible<IteratorType>::value
  4489. && std::is_nothrow_move_constructible<string_type>::value) = default;
  4490. iteration_proxy_value& operator=(iteration_proxy_value&&)
  4491. noexcept(std::is_nothrow_move_assignable<IteratorType>::value
  4492. && std::is_nothrow_move_assignable<string_type>::value) = default;
  4493. ~iteration_proxy_value() = default;
  4494. /// dereference operator (needed for range-based for)
  4495. const iteration_proxy_value& operator*() const
  4496. {
  4497. return *this;
  4498. }
  4499. /// increment operator (needed for range-based for)
  4500. iteration_proxy_value& operator++()
  4501. {
  4502. ++anchor;
  4503. ++array_index;
  4504. return *this;
  4505. }
  4506. iteration_proxy_value operator++(int)& // NOLINT(cert-dcl21-cpp)
  4507. {
  4508. auto tmp = iteration_proxy_value(anchor, array_index);
  4509. ++anchor;
  4510. ++array_index;
  4511. return tmp;
  4512. }
  4513. /// equality operator (needed for InputIterator)
  4514. bool operator==(const iteration_proxy_value& o) const
  4515. {
  4516. return anchor == o.anchor;
  4517. }
  4518. /// inequality operator (needed for range-based for)
  4519. bool operator!=(const iteration_proxy_value& o) const
  4520. {
  4521. return anchor != o.anchor;
  4522. }
  4523. /// return key of the iterator
  4524. const string_type& key() const
  4525. {
  4526. JSON_ASSERT(anchor.m_object != nullptr);
  4527. switch (anchor.m_object->type())
  4528. {
  4529. // use integer array index as key
  4530. case value_t::array:
  4531. {
  4532. if (array_index != array_index_last)
  4533. {
  4534. int_to_string( array_index_str, array_index );
  4535. array_index_last = array_index;
  4536. }
  4537. return array_index_str;
  4538. }
  4539. // use key from the object
  4540. case value_t::object:
  4541. return anchor.key();
  4542. // use an empty key for all primitive types
  4543. case value_t::null:
  4544. case value_t::string:
  4545. case value_t::boolean:
  4546. case value_t::number_integer:
  4547. case value_t::number_unsigned:
  4548. case value_t::number_float:
  4549. case value_t::binary:
  4550. case value_t::discarded:
  4551. default:
  4552. return empty_str;
  4553. }
  4554. }
  4555. /// return value of the iterator
  4556. typename IteratorType::reference value() const
  4557. {
  4558. return anchor.value();
  4559. }
  4560. };
  4561. /// proxy class for the items() function
  4562. template<typename IteratorType> class iteration_proxy
  4563. {
  4564. private:
  4565. /// the container to iterate
  4566. typename IteratorType::pointer container = nullptr;
  4567. public:
  4568. explicit iteration_proxy() = default;
  4569. /// construct iteration proxy from a container
  4570. explicit iteration_proxy(typename IteratorType::reference cont) noexcept
  4571. : container(&cont) {}
  4572. iteration_proxy(iteration_proxy const&) = default;
  4573. iteration_proxy& operator=(iteration_proxy const&) = default;
  4574. iteration_proxy(iteration_proxy&&) noexcept = default;
  4575. iteration_proxy& operator=(iteration_proxy&&) noexcept = default;
  4576. ~iteration_proxy() = default;
  4577. /// return iterator begin (needed for range-based for)
  4578. iteration_proxy_value<IteratorType> begin() const noexcept
  4579. {
  4580. return iteration_proxy_value<IteratorType>(container->begin());
  4581. }
  4582. /// return iterator end (needed for range-based for)
  4583. iteration_proxy_value<IteratorType> end() const noexcept
  4584. {
  4585. return iteration_proxy_value<IteratorType>(container->end());
  4586. }
  4587. };
  4588. // Structured Bindings Support
  4589. // For further reference see https://blog.tartanllama.xyz/structured-bindings/
  4590. // And see https://github.com/nlohmann/json/pull/1391
  4591. template<std::size_t N, typename IteratorType, enable_if_t<N == 0, int> = 0>
  4592. auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.key())
  4593. {
  4594. return i.key();
  4595. }
  4596. // Structured Bindings Support
  4597. // For further reference see https://blog.tartanllama.xyz/structured-bindings/
  4598. // And see https://github.com/nlohmann/json/pull/1391
  4599. template<std::size_t N, typename IteratorType, enable_if_t<N == 1, int> = 0>
  4600. auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.value())
  4601. {
  4602. return i.value();
  4603. }
  4604. } // namespace detail
  4605. NLOHMANN_JSON_NAMESPACE_END
  4606. // The Addition to the STD Namespace is required to add
  4607. // Structured Bindings Support to the iteration_proxy_value class
  4608. // For further reference see https://blog.tartanllama.xyz/structured-bindings/
  4609. // And see https://github.com/nlohmann/json/pull/1391
  4610. namespace std
  4611. {
  4612. #if defined(__clang__)
  4613. // Fix: https://github.com/nlohmann/json/issues/1401
  4614. #pragma clang diagnostic push
  4615. #pragma clang diagnostic ignored "-Wmismatched-tags"
  4616. #endif
  4617. template<typename IteratorType>
  4618. class tuple_size<::nlohmann::detail::iteration_proxy_value<IteratorType>>
  4619. : public std::integral_constant<std::size_t, 2> {};
  4620. template<std::size_t N, typename IteratorType>
  4621. class tuple_element<N, ::nlohmann::detail::iteration_proxy_value<IteratorType >>
  4622. {
  4623. public:
  4624. using type = decltype(
  4625. get<N>(std::declval <
  4626. ::nlohmann::detail::iteration_proxy_value<IteratorType >> ()));
  4627. };
  4628. #if defined(__clang__)
  4629. #pragma clang diagnostic pop
  4630. #endif
  4631. } // namespace std
  4632. #if JSON_HAS_RANGES
  4633. template <typename IteratorType>
  4634. inline constexpr bool ::std::ranges::enable_borrowed_range<::nlohmann::detail::iteration_proxy<IteratorType>> = true;
  4635. #endif
  4636. // #include <nlohmann/detail/macro_scope.hpp>
  4637. // #include <nlohmann/detail/meta/cpp_future.hpp>
  4638. // #include <nlohmann/detail/meta/std_fs.hpp>
  4639. // #include <nlohmann/detail/meta/type_traits.hpp>
  4640. // #include <nlohmann/detail/value_t.hpp>
  4641. NLOHMANN_JSON_NAMESPACE_BEGIN
  4642. namespace detail
  4643. {
  4644. //////////////////
  4645. // constructors //
  4646. //////////////////
  4647. /*
  4648. * Note all external_constructor<>::construct functions need to call
  4649. * j.m_value.destroy(j.m_type) to avoid a memory leak in case j contains an
  4650. * allocated value (e.g., a string). See bug issue
  4651. * https://github.com/nlohmann/json/issues/2865 for more information.
  4652. */
  4653. template<value_t> struct external_constructor;
  4654. template<>
  4655. struct external_constructor<value_t::boolean>
  4656. {
  4657. template<typename BasicJsonType>
  4658. static void construct(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept
  4659. {
  4660. j.m_value.destroy(j.m_type);
  4661. j.m_type = value_t::boolean;
  4662. j.m_value = b;
  4663. j.assert_invariant();
  4664. }
  4665. };
  4666. template<>
  4667. struct external_constructor<value_t::string>
  4668. {
  4669. template<typename BasicJsonType>
  4670. static void construct(BasicJsonType& j, const typename BasicJsonType::string_t& s)
  4671. {
  4672. j.m_value.destroy(j.m_type);
  4673. j.m_type = value_t::string;
  4674. j.m_value = s;
  4675. j.assert_invariant();
  4676. }
  4677. template<typename BasicJsonType>
  4678. static void construct(BasicJsonType& j, typename BasicJsonType::string_t&& s)
  4679. {
  4680. j.m_value.destroy(j.m_type);
  4681. j.m_type = value_t::string;
  4682. j.m_value = std::move(s);
  4683. j.assert_invariant();
  4684. }
  4685. template < typename BasicJsonType, typename CompatibleStringType,
  4686. enable_if_t < !std::is_same<CompatibleStringType, typename BasicJsonType::string_t>::value,
  4687. int > = 0 >
  4688. static void construct(BasicJsonType& j, const CompatibleStringType& str)
  4689. {
  4690. j.m_value.destroy(j.m_type);
  4691. j.m_type = value_t::string;
  4692. j.m_value.string = j.template create<typename BasicJsonType::string_t>(str);
  4693. j.assert_invariant();
  4694. }
  4695. };
  4696. template<>
  4697. struct external_constructor<value_t::binary>
  4698. {
  4699. template<typename BasicJsonType>
  4700. static void construct(BasicJsonType& j, const typename BasicJsonType::binary_t& b)
  4701. {
  4702. j.m_value.destroy(j.m_type);
  4703. j.m_type = value_t::binary;
  4704. j.m_value = typename BasicJsonType::binary_t(b);
  4705. j.assert_invariant();
  4706. }
  4707. template<typename BasicJsonType>
  4708. static void construct(BasicJsonType& j, typename BasicJsonType::binary_t&& b)
  4709. {
  4710. j.m_value.destroy(j.m_type);
  4711. j.m_type = value_t::binary;
  4712. j.m_value = typename BasicJsonType::binary_t(std::move(b));
  4713. j.assert_invariant();
  4714. }
  4715. };
  4716. template<>
  4717. struct external_constructor<value_t::number_float>
  4718. {
  4719. template<typename BasicJsonType>
  4720. static void construct(BasicJsonType& j, typename BasicJsonType::number_float_t val) noexcept
  4721. {
  4722. j.m_value.destroy(j.m_type);
  4723. j.m_type = value_t::number_float;
  4724. j.m_value = val;
  4725. j.assert_invariant();
  4726. }
  4727. };
  4728. template<>
  4729. struct external_constructor<value_t::number_unsigned>
  4730. {
  4731. template<typename BasicJsonType>
  4732. static void construct(BasicJsonType& j, typename BasicJsonType::number_unsigned_t val) noexcept
  4733. {
  4734. j.m_value.destroy(j.m_type);
  4735. j.m_type = value_t::number_unsigned;
  4736. j.m_value = val;
  4737. j.assert_invariant();
  4738. }
  4739. };
  4740. template<>
  4741. struct external_constructor<value_t::number_integer>
  4742. {
  4743. template<typename BasicJsonType>
  4744. static void construct(BasicJsonType& j, typename BasicJsonType::number_integer_t val) noexcept
  4745. {
  4746. j.m_value.destroy(j.m_type);
  4747. j.m_type = value_t::number_integer;
  4748. j.m_value = val;
  4749. j.assert_invariant();
  4750. }
  4751. };
  4752. template<>
  4753. struct external_constructor<value_t::array>
  4754. {
  4755. template<typename BasicJsonType>
  4756. static void construct(BasicJsonType& j, const typename BasicJsonType::array_t& arr)
  4757. {
  4758. j.m_value.destroy(j.m_type);
  4759. j.m_type = value_t::array;
  4760. j.m_value = arr;
  4761. j.set_parents();
  4762. j.assert_invariant();
  4763. }
  4764. template<typename BasicJsonType>
  4765. static void construct(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
  4766. {
  4767. j.m_value.destroy(j.m_type);
  4768. j.m_type = value_t::array;
  4769. j.m_value = std::move(arr);
  4770. j.set_parents();
  4771. j.assert_invariant();
  4772. }
  4773. template < typename BasicJsonType, typename CompatibleArrayType,
  4774. enable_if_t < !std::is_same<CompatibleArrayType, typename BasicJsonType::array_t>::value,
  4775. int > = 0 >
  4776. static void construct(BasicJsonType& j, const CompatibleArrayType& arr)
  4777. {
  4778. using std::begin;
  4779. using std::end;
  4780. j.m_value.destroy(j.m_type);
  4781. j.m_type = value_t::array;
  4782. j.m_value.array = j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
  4783. j.set_parents();
  4784. j.assert_invariant();
  4785. }
  4786. template<typename BasicJsonType>
  4787. static void construct(BasicJsonType& j, const std::vector<bool>& arr)
  4788. {
  4789. j.m_value.destroy(j.m_type);
  4790. j.m_type = value_t::array;
  4791. j.m_value = value_t::array;
  4792. j.m_value.array->reserve(arr.size());
  4793. for (const bool x : arr)
  4794. {
  4795. j.m_value.array->push_back(x);
  4796. j.set_parent(j.m_value.array->back());
  4797. }
  4798. j.assert_invariant();
  4799. }
  4800. template<typename BasicJsonType, typename T,
  4801. enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
  4802. static void construct(BasicJsonType& j, const std::valarray<T>& arr)
  4803. {
  4804. j.m_value.destroy(j.m_type);
  4805. j.m_type = value_t::array;
  4806. j.m_value = value_t::array;
  4807. j.m_value.array->resize(arr.size());
  4808. if (arr.size() > 0)
  4809. {
  4810. std::copy(std::begin(arr), std::end(arr), j.m_value.array->begin());
  4811. }
  4812. j.set_parents();
  4813. j.assert_invariant();
  4814. }
  4815. };
  4816. template<>
  4817. struct external_constructor<value_t::object>
  4818. {
  4819. template<typename BasicJsonType>
  4820. static void construct(BasicJsonType& j, const typename BasicJsonType::object_t& obj)
  4821. {
  4822. j.m_value.destroy(j.m_type);
  4823. j.m_type = value_t::object;
  4824. j.m_value = obj;
  4825. j.set_parents();
  4826. j.assert_invariant();
  4827. }
  4828. template<typename BasicJsonType>
  4829. static void construct(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
  4830. {
  4831. j.m_value.destroy(j.m_type);
  4832. j.m_type = value_t::object;
  4833. j.m_value = std::move(obj);
  4834. j.set_parents();
  4835. j.assert_invariant();
  4836. }
  4837. template < typename BasicJsonType, typename CompatibleObjectType,
  4838. enable_if_t < !std::is_same<CompatibleObjectType, typename BasicJsonType::object_t>::value, int > = 0 >
  4839. static void construct(BasicJsonType& j, const CompatibleObjectType& obj)
  4840. {
  4841. using std::begin;
  4842. using std::end;
  4843. j.m_value.destroy(j.m_type);
  4844. j.m_type = value_t::object;
  4845. j.m_value.object = j.template create<typename BasicJsonType::object_t>(begin(obj), end(obj));
  4846. j.set_parents();
  4847. j.assert_invariant();
  4848. }
  4849. };
  4850. /////////////
  4851. // to_json //
  4852. /////////////
  4853. template<typename BasicJsonType, typename T,
  4854. enable_if_t<std::is_same<T, typename BasicJsonType::boolean_t>::value, int> = 0>
  4855. inline void to_json(BasicJsonType& j, T b) noexcept
  4856. {
  4857. external_constructor<value_t::boolean>::construct(j, b);
  4858. }
  4859. template < typename BasicJsonType, typename BoolRef,
  4860. enable_if_t <
  4861. ((std::is_same<std::vector<bool>::reference, BoolRef>::value
  4862. && !std::is_same <std::vector<bool>::reference, typename BasicJsonType::boolean_t&>::value)
  4863. || (std::is_same<std::vector<bool>::const_reference, BoolRef>::value
  4864. && !std::is_same <detail::uncvref_t<std::vector<bool>::const_reference>,
  4865. typename BasicJsonType::boolean_t >::value))
  4866. && std::is_convertible<const BoolRef&, typename BasicJsonType::boolean_t>::value, int > = 0 >
  4867. inline void to_json(BasicJsonType& j, const BoolRef& b) noexcept
  4868. {
  4869. external_constructor<value_t::boolean>::construct(j, static_cast<typename BasicJsonType::boolean_t>(b));
  4870. }
  4871. template<typename BasicJsonType, typename CompatibleString,
  4872. enable_if_t<std::is_constructible<typename BasicJsonType::string_t, CompatibleString>::value, int> = 0>
  4873. inline void to_json(BasicJsonType& j, const CompatibleString& s)
  4874. {
  4875. external_constructor<value_t::string>::construct(j, s);
  4876. }
  4877. template<typename BasicJsonType>
  4878. inline void to_json(BasicJsonType& j, typename BasicJsonType::string_t&& s)
  4879. {
  4880. external_constructor<value_t::string>::construct(j, std::move(s));
  4881. }
  4882. template<typename BasicJsonType, typename FloatType,
  4883. enable_if_t<std::is_floating_point<FloatType>::value, int> = 0>
  4884. inline void to_json(BasicJsonType& j, FloatType val) noexcept
  4885. {
  4886. external_constructor<value_t::number_float>::construct(j, static_cast<typename BasicJsonType::number_float_t>(val));
  4887. }
  4888. template<typename BasicJsonType, typename CompatibleNumberUnsignedType,
  4889. enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_unsigned_t, CompatibleNumberUnsignedType>::value, int> = 0>
  4890. inline void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val) noexcept
  4891. {
  4892. external_constructor<value_t::number_unsigned>::construct(j, static_cast<typename BasicJsonType::number_unsigned_t>(val));
  4893. }
  4894. template<typename BasicJsonType, typename CompatibleNumberIntegerType,
  4895. enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_integer_t, CompatibleNumberIntegerType>::value, int> = 0>
  4896. inline void to_json(BasicJsonType& j, CompatibleNumberIntegerType val) noexcept
  4897. {
  4898. external_constructor<value_t::number_integer>::construct(j, static_cast<typename BasicJsonType::number_integer_t>(val));
  4899. }
  4900. #if !JSON_DISABLE_ENUM_SERIALIZATION
  4901. template<typename BasicJsonType, typename EnumType,
  4902. enable_if_t<std::is_enum<EnumType>::value, int> = 0>
  4903. inline void to_json(BasicJsonType& j, EnumType e) noexcept
  4904. {
  4905. using underlying_type = typename std::underlying_type<EnumType>::type;
  4906. external_constructor<value_t::number_integer>::construct(j, static_cast<underlying_type>(e));
  4907. }
  4908. #endif // JSON_DISABLE_ENUM_SERIALIZATION
  4909. template<typename BasicJsonType>
  4910. inline void to_json(BasicJsonType& j, const std::vector<bool>& e)
  4911. {
  4912. external_constructor<value_t::array>::construct(j, e);
  4913. }
  4914. template < typename BasicJsonType, typename CompatibleArrayType,
  4915. enable_if_t < is_compatible_array_type<BasicJsonType,
  4916. CompatibleArrayType>::value&&
  4917. !is_compatible_object_type<BasicJsonType, CompatibleArrayType>::value&&
  4918. !is_compatible_string_type<BasicJsonType, CompatibleArrayType>::value&&
  4919. !std::is_same<typename BasicJsonType::binary_t, CompatibleArrayType>::value&&
  4920. !is_basic_json<CompatibleArrayType>::value,
  4921. int > = 0 >
  4922. inline void to_json(BasicJsonType& j, const CompatibleArrayType& arr)
  4923. {
  4924. external_constructor<value_t::array>::construct(j, arr);
  4925. }
  4926. template<typename BasicJsonType>
  4927. inline void to_json(BasicJsonType& j, const typename BasicJsonType::binary_t& bin)
  4928. {
  4929. external_constructor<value_t::binary>::construct(j, bin);
  4930. }
  4931. template<typename BasicJsonType, typename T,
  4932. enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
  4933. inline void to_json(BasicJsonType& j, const std::valarray<T>& arr)
  4934. {
  4935. external_constructor<value_t::array>::construct(j, std::move(arr));
  4936. }
  4937. template<typename BasicJsonType>
  4938. inline void to_json(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
  4939. {
  4940. external_constructor<value_t::array>::construct(j, std::move(arr));
  4941. }
  4942. template < typename BasicJsonType, typename CompatibleObjectType,
  4943. enable_if_t < is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value&& !is_basic_json<CompatibleObjectType>::value, int > = 0 >
  4944. inline void to_json(BasicJsonType& j, const CompatibleObjectType& obj)
  4945. {
  4946. external_constructor<value_t::object>::construct(j, obj);
  4947. }
  4948. template<typename BasicJsonType>
  4949. inline void to_json(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
  4950. {
  4951. external_constructor<value_t::object>::construct(j, std::move(obj));
  4952. }
  4953. template <
  4954. typename BasicJsonType, typename T, std::size_t N,
  4955. enable_if_t < !std::is_constructible<typename BasicJsonType::string_t,
  4956. const T(&)[N]>::value, // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  4957. int > = 0 >
  4958. inline void to_json(BasicJsonType& j, const T(&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  4959. {
  4960. external_constructor<value_t::array>::construct(j, arr);
  4961. }
  4962. template < typename BasicJsonType, typename T1, typename T2, enable_if_t < std::is_constructible<BasicJsonType, T1>::value&& std::is_constructible<BasicJsonType, T2>::value, int > = 0 >
  4963. inline void to_json(BasicJsonType& j, const std::pair<T1, T2>& p)
  4964. {
  4965. j = { p.first, p.second };
  4966. }
  4967. // for https://github.com/nlohmann/json/pull/1134
  4968. template<typename BasicJsonType, typename T,
  4969. enable_if_t<std::is_same<T, iteration_proxy_value<typename BasicJsonType::iterator>>::value, int> = 0>
  4970. inline void to_json(BasicJsonType& j, const T& b)
  4971. {
  4972. j = { {b.key(), b.value()} };
  4973. }
  4974. template<typename BasicJsonType, typename Tuple, std::size_t... Idx>
  4975. inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence<Idx...> /*unused*/)
  4976. {
  4977. j = { std::get<Idx>(t)... };
  4978. }
  4979. template<typename BasicJsonType, typename T, enable_if_t<is_constructible_tuple<BasicJsonType, T>::value, int > = 0>
  4980. inline void to_json(BasicJsonType& j, const T& t)
  4981. {
  4982. to_json_tuple_impl(j, t, make_index_sequence<std::tuple_size<T>::value> {});
  4983. }
  4984. #if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
  4985. template<typename BasicJsonType>
  4986. inline void to_json(BasicJsonType& j, const std_fs::path& p)
  4987. {
  4988. j = p.string();
  4989. }
  4990. #endif
  4991. struct to_json_fn
  4992. {
  4993. template<typename BasicJsonType, typename T>
  4994. auto operator()(BasicJsonType& j, T&& val) const noexcept(noexcept(to_json(j, std::forward<T>(val))))
  4995. -> decltype(to_json(j, std::forward<T>(val)), void())
  4996. {
  4997. return to_json(j, std::forward<T>(val));
  4998. }
  4999. };
  5000. } // namespace detail
  5001. #ifndef JSON_HAS_CPP_17
  5002. /// namespace to hold default `to_json` function
  5003. /// to see why this is required:
  5004. /// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
  5005. namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces)
  5006. {
  5007. #endif
  5008. JSON_INLINE_VARIABLE constexpr const auto& to_json = // NOLINT(misc-definitions-in-headers)
  5009. detail::static_const<detail::to_json_fn>::value;
  5010. #ifndef JSON_HAS_CPP_17
  5011. } // namespace
  5012. #endif
  5013. NLOHMANN_JSON_NAMESPACE_END
  5014. // #include <nlohmann/detail/meta/identity_tag.hpp>
  5015. NLOHMANN_JSON_NAMESPACE_BEGIN
  5016. /// @sa https://json.nlohmann.me/api/adl_serializer/
  5017. template<typename ValueType, typename>
  5018. struct adl_serializer
  5019. {
  5020. /// @brief convert a JSON value to any value type
  5021. /// @sa https://json.nlohmann.me/api/adl_serializer/from_json/
  5022. template<typename BasicJsonType, typename TargetType = ValueType>
  5023. static auto from_json(BasicJsonType && j, TargetType& val) noexcept(
  5024. noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), val)))
  5025. -> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), val), void())
  5026. {
  5027. ::nlohmann::from_json(std::forward<BasicJsonType>(j), val);
  5028. }
  5029. /// @brief convert a JSON value to any value type
  5030. /// @sa https://json.nlohmann.me/api/adl_serializer/from_json/
  5031. template<typename BasicJsonType, typename TargetType = ValueType>
  5032. static auto from_json(BasicJsonType && j) noexcept(
  5033. noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {})))
  5034. -> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {}))
  5035. {
  5036. return ::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {});
  5037. }
  5038. /// @brief convert any value type to a JSON value
  5039. /// @sa https://json.nlohmann.me/api/adl_serializer/to_json/
  5040. template<typename BasicJsonType, typename TargetType = ValueType>
  5041. static auto to_json(BasicJsonType& j, TargetType && val) noexcept(
  5042. noexcept(::nlohmann::to_json(j, std::forward<TargetType>(val))))
  5043. -> decltype(::nlohmann::to_json(j, std::forward<TargetType>(val)), void())
  5044. {
  5045. ::nlohmann::to_json(j, std::forward<TargetType>(val));
  5046. }
  5047. };
  5048. NLOHMANN_JSON_NAMESPACE_END
  5049. // #include <nlohmann/byte_container_with_subtype.hpp>
  5050. // __ _____ _____ _____
  5051. // __| | __| | | | JSON for Modern C++
  5052. // | | |__ | | | | | | version 3.11.2
  5053. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5054. //
  5055. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  5056. // SPDX-License-Identifier: MIT
  5057. #include <cstdint> // uint8_t, uint64_t
  5058. #include <tuple> // tie
  5059. #include <utility> // move
  5060. // #include <nlohmann/detail/abi_macros.hpp>
  5061. NLOHMANN_JSON_NAMESPACE_BEGIN
  5062. /// @brief an internal type for a backed binary type
  5063. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/
  5064. template<typename BinaryType>
  5065. class byte_container_with_subtype : public BinaryType
  5066. {
  5067. public:
  5068. using container_type = BinaryType;
  5069. using subtype_type = std::uint64_t;
  5070. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5071. byte_container_with_subtype() noexcept(noexcept(container_type()))
  5072. : container_type()
  5073. {}
  5074. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5075. byte_container_with_subtype(const container_type& b) noexcept(noexcept(container_type(b)))
  5076. : container_type(b)
  5077. {}
  5078. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5079. byte_container_with_subtype(container_type&& b) noexcept(noexcept(container_type(std::move(b))))
  5080. : container_type(std::move(b))
  5081. {}
  5082. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5083. byte_container_with_subtype(const container_type& b, subtype_type subtype_) noexcept(noexcept(container_type(b)))
  5084. : container_type(b)
  5085. , m_subtype(subtype_)
  5086. , m_has_subtype(true)
  5087. {}
  5088. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  5089. byte_container_with_subtype(container_type&& b, subtype_type subtype_) noexcept(noexcept(container_type(std::move(b))))
  5090. : container_type(std::move(b))
  5091. , m_subtype(subtype_)
  5092. , m_has_subtype(true)
  5093. {}
  5094. bool operator==(const byte_container_with_subtype& rhs) const
  5095. {
  5096. return std::tie(static_cast<const BinaryType&>(*this), m_subtype, m_has_subtype) ==
  5097. std::tie(static_cast<const BinaryType&>(rhs), rhs.m_subtype, rhs.m_has_subtype);
  5098. }
  5099. bool operator!=(const byte_container_with_subtype& rhs) const
  5100. {
  5101. return !(rhs == *this);
  5102. }
  5103. /// @brief sets the binary subtype
  5104. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/set_subtype/
  5105. void set_subtype(subtype_type subtype_) noexcept
  5106. {
  5107. m_subtype = subtype_;
  5108. m_has_subtype = true;
  5109. }
  5110. /// @brief return the binary subtype
  5111. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/subtype/
  5112. constexpr subtype_type subtype() const noexcept
  5113. {
  5114. return m_has_subtype ? m_subtype : static_cast<subtype_type>(-1);
  5115. }
  5116. /// @brief return whether the value has a subtype
  5117. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/has_subtype/
  5118. constexpr bool has_subtype() const noexcept
  5119. {
  5120. return m_has_subtype;
  5121. }
  5122. /// @brief clears the binary subtype
  5123. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/clear_subtype/
  5124. void clear_subtype() noexcept
  5125. {
  5126. m_subtype = 0;
  5127. m_has_subtype = false;
  5128. }
  5129. private:
  5130. subtype_type m_subtype = 0;
  5131. bool m_has_subtype = false;
  5132. };
  5133. NLOHMANN_JSON_NAMESPACE_END
  5134. // #include <nlohmann/detail/conversions/from_json.hpp>
  5135. // #include <nlohmann/detail/conversions/to_json.hpp>
  5136. // #include <nlohmann/detail/exceptions.hpp>
  5137. // #include <nlohmann/detail/hash.hpp>
  5138. // __ _____ _____ _____
  5139. // __| | __| | | | JSON for Modern C++
  5140. // | | |__ | | | | | | version 3.11.2
  5141. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5142. //
  5143. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  5144. // SPDX-License-Identifier: MIT
  5145. #include <cstdint> // uint8_t
  5146. #include <cstddef> // size_t
  5147. #include <functional> // hash
  5148. // #include <nlohmann/detail/abi_macros.hpp>
  5149. // #include <nlohmann/detail/value_t.hpp>
  5150. NLOHMANN_JSON_NAMESPACE_BEGIN
  5151. namespace detail
  5152. {
  5153. // boost::hash_combine
  5154. inline std::size_t combine(std::size_t seed, std::size_t h) noexcept
  5155. {
  5156. seed ^= h + 0x9e3779b9 + (seed << 6U) + (seed >> 2U);
  5157. return seed;
  5158. }
  5159. /*!
  5160. @brief hash a JSON value
  5161. The hash function tries to rely on std::hash where possible. Furthermore, the
  5162. type of the JSON value is taken into account to have different hash values for
  5163. null, 0, 0U, and false, etc.
  5164. @tparam BasicJsonType basic_json specialization
  5165. @param j JSON value to hash
  5166. @return hash value of j
  5167. */
  5168. template<typename BasicJsonType>
  5169. std::size_t hash(const BasicJsonType& j)
  5170. {
  5171. using string_t = typename BasicJsonType::string_t;
  5172. using number_integer_t = typename BasicJsonType::number_integer_t;
  5173. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  5174. using number_float_t = typename BasicJsonType::number_float_t;
  5175. const auto type = static_cast<std::size_t>(j.type());
  5176. switch (j.type())
  5177. {
  5178. case BasicJsonType::value_t::null:
  5179. case BasicJsonType::value_t::discarded:
  5180. {
  5181. return combine(type, 0);
  5182. }
  5183. case BasicJsonType::value_t::object:
  5184. {
  5185. auto seed = combine(type, j.size());
  5186. for (const auto& element : j.items())
  5187. {
  5188. const auto h = std::hash<string_t> {}(element.key());
  5189. seed = combine(seed, h);
  5190. seed = combine(seed, hash(element.value()));
  5191. }
  5192. return seed;
  5193. }
  5194. case BasicJsonType::value_t::array:
  5195. {
  5196. auto seed = combine(type, j.size());
  5197. for (const auto& element : j)
  5198. {
  5199. seed = combine(seed, hash(element));
  5200. }
  5201. return seed;
  5202. }
  5203. case BasicJsonType::value_t::string:
  5204. {
  5205. const auto h = std::hash<string_t> {}(j.template get_ref<const string_t&>());
  5206. return combine(type, h);
  5207. }
  5208. case BasicJsonType::value_t::boolean:
  5209. {
  5210. const auto h = std::hash<bool> {}(j.template get<bool>());
  5211. return combine(type, h);
  5212. }
  5213. case BasicJsonType::value_t::number_integer:
  5214. {
  5215. const auto h = std::hash<number_integer_t> {}(j.template get<number_integer_t>());
  5216. return combine(type, h);
  5217. }
  5218. case BasicJsonType::value_t::number_unsigned:
  5219. {
  5220. const auto h = std::hash<number_unsigned_t> {}(j.template get<number_unsigned_t>());
  5221. return combine(type, h);
  5222. }
  5223. case BasicJsonType::value_t::number_float:
  5224. {
  5225. const auto h = std::hash<number_float_t> {}(j.template get<number_float_t>());
  5226. return combine(type, h);
  5227. }
  5228. case BasicJsonType::value_t::binary:
  5229. {
  5230. auto seed = combine(type, j.get_binary().size());
  5231. const auto h = std::hash<bool> {}(j.get_binary().has_subtype());
  5232. seed = combine(seed, h);
  5233. seed = combine(seed, static_cast<std::size_t>(j.get_binary().subtype()));
  5234. for (const auto byte : j.get_binary())
  5235. {
  5236. seed = combine(seed, std::hash<std::uint8_t> {}(byte));
  5237. }
  5238. return seed;
  5239. }
  5240. default: // LCOV_EXCL_LINE
  5241. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  5242. return 0; // LCOV_EXCL_LINE
  5243. }
  5244. }
  5245. } // namespace detail
  5246. NLOHMANN_JSON_NAMESPACE_END
  5247. // #include <nlohmann/detail/input/binary_reader.hpp>
  5248. // __ _____ _____ _____
  5249. // __| | __| | | | JSON for Modern C++
  5250. // | | |__ | | | | | | version 3.11.2
  5251. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5252. //
  5253. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  5254. // SPDX-License-Identifier: MIT
  5255. #include <algorithm> // generate_n
  5256. #include <array> // array
  5257. #include <cmath> // ldexp
  5258. #include <cstddef> // size_t
  5259. #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
  5260. #include <cstdio> // snprintf
  5261. #include <cstring> // memcpy
  5262. #include <iterator> // back_inserter
  5263. #include <limits> // numeric_limits
  5264. #include <string> // char_traits, string
  5265. #include <utility> // make_pair, move
  5266. #include <vector> // vector
  5267. // #include <nlohmann/detail/exceptions.hpp>
  5268. // #include <nlohmann/detail/input/input_adapters.hpp>
  5269. // __ _____ _____ _____
  5270. // __| | __| | | | JSON for Modern C++
  5271. // | | |__ | | | | | | version 3.11.2
  5272. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5273. //
  5274. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  5275. // SPDX-License-Identifier: MIT
  5276. #include <array> // array
  5277. #include <cstddef> // size_t
  5278. #include <cstring> // strlen
  5279. #include <iterator> // begin, end, iterator_traits, random_access_iterator_tag, distance, next
  5280. #include <memory> // shared_ptr, make_shared, addressof
  5281. #include <numeric> // accumulate
  5282. #include <string> // string, char_traits
  5283. #include <type_traits> // enable_if, is_base_of, is_pointer, is_integral, remove_pointer
  5284. #include <utility> // pair, declval
  5285. #ifndef JSON_NO_IO
  5286. #include <cstdio> // FILE *
  5287. #include <istream> // istream
  5288. #endif // JSON_NO_IO
  5289. // #include <nlohmann/detail/iterators/iterator_traits.hpp>
  5290. // #include <nlohmann/detail/macro_scope.hpp>
  5291. NLOHMANN_JSON_NAMESPACE_BEGIN
  5292. namespace detail
  5293. {
  5294. /// the supported input formats
  5295. enum class input_format_t { json, cbor, msgpack, ubjson, bson, bjdata };
  5296. ////////////////////
  5297. // input adapters //
  5298. ////////////////////
  5299. #ifndef JSON_NO_IO
  5300. /*!
  5301. Input adapter for stdio file access. This adapter read only 1 byte and do not use any
  5302. buffer. This adapter is a very low level adapter.
  5303. */
  5304. class file_input_adapter
  5305. {
  5306. public:
  5307. using char_type = char;
  5308. JSON_HEDLEY_NON_NULL(2)
  5309. explicit file_input_adapter(std::FILE* f) noexcept
  5310. : m_file(f)
  5311. {
  5312. JSON_ASSERT(m_file != nullptr);
  5313. }
  5314. // make class move-only
  5315. file_input_adapter(const file_input_adapter&) = delete;
  5316. file_input_adapter(file_input_adapter&&) noexcept = default;
  5317. file_input_adapter& operator=(const file_input_adapter&) = delete;
  5318. file_input_adapter& operator=(file_input_adapter&&) = delete;
  5319. ~file_input_adapter() = default;
  5320. std::char_traits<char>::int_type get_character() noexcept
  5321. {
  5322. return std::fgetc(m_file);
  5323. }
  5324. private:
  5325. /// the file pointer to read from
  5326. std::FILE* m_file;
  5327. };
  5328. /*!
  5329. Input adapter for a (caching) istream. Ignores a UFT Byte Order Mark at
  5330. beginning of input. Does not support changing the underlying std::streambuf
  5331. in mid-input. Maintains underlying std::istream and std::streambuf to support
  5332. subsequent use of standard std::istream operations to process any input
  5333. characters following those used in parsing the JSON input. Clears the
  5334. std::istream flags; any input errors (e.g., EOF) will be detected by the first
  5335. subsequent call for input from the std::istream.
  5336. */
  5337. class input_stream_adapter
  5338. {
  5339. public:
  5340. using char_type = char;
  5341. ~input_stream_adapter()
  5342. {
  5343. // clear stream flags; we use underlying streambuf I/O, do not
  5344. // maintain ifstream flags, except eof
  5345. if (is != nullptr)
  5346. {
  5347. is->clear(is->rdstate() & std::ios::eofbit);
  5348. }
  5349. }
  5350. explicit input_stream_adapter(std::istream& i)
  5351. : is(&i), sb(i.rdbuf())
  5352. {}
  5353. // delete because of pointer members
  5354. input_stream_adapter(const input_stream_adapter&) = delete;
  5355. input_stream_adapter& operator=(input_stream_adapter&) = delete;
  5356. input_stream_adapter& operator=(input_stream_adapter&&) = delete;
  5357. input_stream_adapter(input_stream_adapter&& rhs) noexcept
  5358. : is(rhs.is), sb(rhs.sb)
  5359. {
  5360. rhs.is = nullptr;
  5361. rhs.sb = nullptr;
  5362. }
  5363. // std::istream/std::streambuf use std::char_traits<char>::to_int_type, to
  5364. // ensure that std::char_traits<char>::eof() and the character 0xFF do not
  5365. // end up as the same value, e.g. 0xFFFFFFFF.
  5366. std::char_traits<char>::int_type get_character()
  5367. {
  5368. auto res = sb->sbumpc();
  5369. // set eof manually, as we don't use the istream interface.
  5370. if (JSON_HEDLEY_UNLIKELY(res == std::char_traits<char>::eof()))
  5371. {
  5372. is->clear(is->rdstate() | std::ios::eofbit);
  5373. }
  5374. return res;
  5375. }
  5376. private:
  5377. /// the associated input stream
  5378. std::istream* is = nullptr;
  5379. std::streambuf* sb = nullptr;
  5380. };
  5381. #endif // JSON_NO_IO
  5382. // General-purpose iterator-based adapter. It might not be as fast as
  5383. // theoretically possible for some containers, but it is extremely versatile.
  5384. template<typename IteratorType>
  5385. class iterator_input_adapter
  5386. {
  5387. public:
  5388. using char_type = typename std::iterator_traits<IteratorType>::value_type;
  5389. iterator_input_adapter(IteratorType first, IteratorType last)
  5390. : current(std::move(first)), end(std::move(last))
  5391. {}
  5392. typename std::char_traits<char_type>::int_type get_character()
  5393. {
  5394. if (JSON_HEDLEY_LIKELY(current != end))
  5395. {
  5396. auto result = std::char_traits<char_type>::to_int_type(*current);
  5397. std::advance(current, 1);
  5398. return result;
  5399. }
  5400. return std::char_traits<char_type>::eof();
  5401. }
  5402. private:
  5403. IteratorType current;
  5404. IteratorType end;
  5405. template<typename BaseInputAdapter, size_t T>
  5406. friend struct wide_string_input_helper;
  5407. bool empty() const
  5408. {
  5409. return current == end;
  5410. }
  5411. };
  5412. template<typename BaseInputAdapter, size_t T>
  5413. struct wide_string_input_helper;
  5414. template<typename BaseInputAdapter>
  5415. struct wide_string_input_helper<BaseInputAdapter, 4>
  5416. {
  5417. // UTF-32
  5418. static void fill_buffer(BaseInputAdapter& input,
  5419. std::array<std::char_traits<char>::int_type, 4>& utf8_bytes,
  5420. size_t& utf8_bytes_index,
  5421. size_t& utf8_bytes_filled)
  5422. {
  5423. utf8_bytes_index = 0;
  5424. if (JSON_HEDLEY_UNLIKELY(input.empty()))
  5425. {
  5426. utf8_bytes[0] = std::char_traits<char>::eof();
  5427. utf8_bytes_filled = 1;
  5428. }
  5429. else
  5430. {
  5431. // get the current character
  5432. const auto wc = input.get_character();
  5433. // UTF-32 to UTF-8 encoding
  5434. if (wc < 0x80)
  5435. {
  5436. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  5437. utf8_bytes_filled = 1;
  5438. }
  5439. else if (wc <= 0x7FF)
  5440. {
  5441. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xC0u | ((static_cast<unsigned int>(wc) >> 6u) & 0x1Fu));
  5442. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5443. utf8_bytes_filled = 2;
  5444. }
  5445. else if (wc <= 0xFFFF)
  5446. {
  5447. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xE0u | ((static_cast<unsigned int>(wc) >> 12u) & 0x0Fu));
  5448. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
  5449. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5450. utf8_bytes_filled = 3;
  5451. }
  5452. else if (wc <= 0x10FFFF)
  5453. {
  5454. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xF0u | ((static_cast<unsigned int>(wc) >> 18u) & 0x07u));
  5455. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 12u) & 0x3Fu));
  5456. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
  5457. utf8_bytes[3] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5458. utf8_bytes_filled = 4;
  5459. }
  5460. else
  5461. {
  5462. // unknown character
  5463. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  5464. utf8_bytes_filled = 1;
  5465. }
  5466. }
  5467. }
  5468. };
  5469. template<typename BaseInputAdapter>
  5470. struct wide_string_input_helper<BaseInputAdapter, 2>
  5471. {
  5472. // UTF-16
  5473. static void fill_buffer(BaseInputAdapter& input,
  5474. std::array<std::char_traits<char>::int_type, 4>& utf8_bytes,
  5475. size_t& utf8_bytes_index,
  5476. size_t& utf8_bytes_filled)
  5477. {
  5478. utf8_bytes_index = 0;
  5479. if (JSON_HEDLEY_UNLIKELY(input.empty()))
  5480. {
  5481. utf8_bytes[0] = std::char_traits<char>::eof();
  5482. utf8_bytes_filled = 1;
  5483. }
  5484. else
  5485. {
  5486. // get the current character
  5487. const auto wc = input.get_character();
  5488. // UTF-16 to UTF-8 encoding
  5489. if (wc < 0x80)
  5490. {
  5491. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  5492. utf8_bytes_filled = 1;
  5493. }
  5494. else if (wc <= 0x7FF)
  5495. {
  5496. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xC0u | ((static_cast<unsigned int>(wc) >> 6u)));
  5497. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5498. utf8_bytes_filled = 2;
  5499. }
  5500. else if (0xD800 > wc || wc >= 0xE000)
  5501. {
  5502. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xE0u | ((static_cast<unsigned int>(wc) >> 12u)));
  5503. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
  5504. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5505. utf8_bytes_filled = 3;
  5506. }
  5507. else
  5508. {
  5509. if (JSON_HEDLEY_UNLIKELY(!input.empty()))
  5510. {
  5511. const auto wc2 = static_cast<unsigned int>(input.get_character());
  5512. const auto charcode = 0x10000u + (((static_cast<unsigned int>(wc) & 0x3FFu) << 10u) | (wc2 & 0x3FFu));
  5513. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xF0u | (charcode >> 18u));
  5514. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((charcode >> 12u) & 0x3Fu));
  5515. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | ((charcode >> 6u) & 0x3Fu));
  5516. utf8_bytes[3] = static_cast<std::char_traits<char>::int_type>(0x80u | (charcode & 0x3Fu));
  5517. utf8_bytes_filled = 4;
  5518. }
  5519. else
  5520. {
  5521. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  5522. utf8_bytes_filled = 1;
  5523. }
  5524. }
  5525. }
  5526. }
  5527. };
  5528. // Wraps another input apdater to convert wide character types into individual bytes.
  5529. template<typename BaseInputAdapter, typename WideCharType>
  5530. class wide_string_input_adapter
  5531. {
  5532. public:
  5533. using char_type = char;
  5534. wide_string_input_adapter(BaseInputAdapter base)
  5535. : base_adapter(base) {}
  5536. typename std::char_traits<char>::int_type get_character() noexcept
  5537. {
  5538. // check if buffer needs to be filled
  5539. if (utf8_bytes_index == utf8_bytes_filled)
  5540. {
  5541. fill_buffer<sizeof(WideCharType)>();
  5542. JSON_ASSERT(utf8_bytes_filled > 0);
  5543. JSON_ASSERT(utf8_bytes_index == 0);
  5544. }
  5545. // use buffer
  5546. JSON_ASSERT(utf8_bytes_filled > 0);
  5547. JSON_ASSERT(utf8_bytes_index < utf8_bytes_filled);
  5548. return utf8_bytes[utf8_bytes_index++];
  5549. }
  5550. private:
  5551. BaseInputAdapter base_adapter;
  5552. template<size_t T>
  5553. void fill_buffer()
  5554. {
  5555. wide_string_input_helper<BaseInputAdapter, T>::fill_buffer(base_adapter, utf8_bytes, utf8_bytes_index, utf8_bytes_filled);
  5556. }
  5557. /// a buffer for UTF-8 bytes
  5558. std::array<std::char_traits<char>::int_type, 4> utf8_bytes = {{0, 0, 0, 0}};
  5559. /// index to the utf8_codes array for the next valid byte
  5560. std::size_t utf8_bytes_index = 0;
  5561. /// number of valid bytes in the utf8_codes array
  5562. std::size_t utf8_bytes_filled = 0;
  5563. };
  5564. template<typename IteratorType, typename Enable = void>
  5565. struct iterator_input_adapter_factory
  5566. {
  5567. using iterator_type = IteratorType;
  5568. using char_type = typename std::iterator_traits<iterator_type>::value_type;
  5569. using adapter_type = iterator_input_adapter<iterator_type>;
  5570. static adapter_type create(IteratorType first, IteratorType last)
  5571. {
  5572. return adapter_type(std::move(first), std::move(last));
  5573. }
  5574. };
  5575. template<typename T>
  5576. struct is_iterator_of_multibyte
  5577. {
  5578. using value_type = typename std::iterator_traits<T>::value_type;
  5579. enum
  5580. {
  5581. value = sizeof(value_type) > 1
  5582. };
  5583. };
  5584. template<typename IteratorType>
  5585. struct iterator_input_adapter_factory<IteratorType, enable_if_t<is_iterator_of_multibyte<IteratorType>::value>>
  5586. {
  5587. using iterator_type = IteratorType;
  5588. using char_type = typename std::iterator_traits<iterator_type>::value_type;
  5589. using base_adapter_type = iterator_input_adapter<iterator_type>;
  5590. using adapter_type = wide_string_input_adapter<base_adapter_type, char_type>;
  5591. static adapter_type create(IteratorType first, IteratorType last)
  5592. {
  5593. return adapter_type(base_adapter_type(std::move(first), std::move(last)));
  5594. }
  5595. };
  5596. // General purpose iterator-based input
  5597. template<typename IteratorType>
  5598. typename iterator_input_adapter_factory<IteratorType>::adapter_type input_adapter(IteratorType first, IteratorType last)
  5599. {
  5600. using factory_type = iterator_input_adapter_factory<IteratorType>;
  5601. return factory_type::create(first, last);
  5602. }
  5603. // Convenience shorthand from container to iterator
  5604. // Enables ADL on begin(container) and end(container)
  5605. // Encloses the using declarations in namespace for not to leak them to outside scope
  5606. namespace container_input_adapter_factory_impl
  5607. {
  5608. using std::begin;
  5609. using std::end;
  5610. template<typename ContainerType, typename Enable = void>
  5611. struct container_input_adapter_factory {};
  5612. template<typename ContainerType>
  5613. struct container_input_adapter_factory< ContainerType,
  5614. void_t<decltype(begin(std::declval<ContainerType>()), end(std::declval<ContainerType>()))>>
  5615. {
  5616. using adapter_type = decltype(input_adapter(begin(std::declval<ContainerType>()), end(std::declval<ContainerType>())));
  5617. static adapter_type create(const ContainerType& container)
  5618. {
  5619. return input_adapter(begin(container), end(container));
  5620. }
  5621. };
  5622. } // namespace container_input_adapter_factory_impl
  5623. template<typename ContainerType>
  5624. typename container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::adapter_type input_adapter(const ContainerType& container)
  5625. {
  5626. return container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::create(container);
  5627. }
  5628. #ifndef JSON_NO_IO
  5629. // Special cases with fast paths
  5630. inline file_input_adapter input_adapter(std::FILE* file)
  5631. {
  5632. return file_input_adapter(file);
  5633. }
  5634. inline input_stream_adapter input_adapter(std::istream& stream)
  5635. {
  5636. return input_stream_adapter(stream);
  5637. }
  5638. inline input_stream_adapter input_adapter(std::istream&& stream)
  5639. {
  5640. return input_stream_adapter(stream);
  5641. }
  5642. #endif // JSON_NO_IO
  5643. using contiguous_bytes_input_adapter = decltype(input_adapter(std::declval<const char*>(), std::declval<const char*>()));
  5644. // Null-delimited strings, and the like.
  5645. template < typename CharT,
  5646. typename std::enable_if <
  5647. std::is_pointer<CharT>::value&&
  5648. !std::is_array<CharT>::value&&
  5649. std::is_integral<typename std::remove_pointer<CharT>::type>::value&&
  5650. sizeof(typename std::remove_pointer<CharT>::type) == 1,
  5651. int >::type = 0 >
  5652. contiguous_bytes_input_adapter input_adapter(CharT b)
  5653. {
  5654. auto length = std::strlen(reinterpret_cast<const char*>(b));
  5655. const auto* ptr = reinterpret_cast<const char*>(b);
  5656. return input_adapter(ptr, ptr + length);
  5657. }
  5658. template<typename T, std::size_t N>
  5659. 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)
  5660. {
  5661. return input_adapter(array, array + N);
  5662. }
  5663. // This class only handles inputs of input_buffer_adapter type.
  5664. // It's required so that expressions like {ptr, len} can be implicitly cast
  5665. // to the correct adapter.
  5666. class span_input_adapter
  5667. {
  5668. public:
  5669. template < typename CharT,
  5670. typename std::enable_if <
  5671. std::is_pointer<CharT>::value&&
  5672. std::is_integral<typename std::remove_pointer<CharT>::type>::value&&
  5673. sizeof(typename std::remove_pointer<CharT>::type) == 1,
  5674. int >::type = 0 >
  5675. span_input_adapter(CharT b, std::size_t l)
  5676. : ia(reinterpret_cast<const char*>(b), reinterpret_cast<const char*>(b) + l) {}
  5677. template<class IteratorType,
  5678. typename std::enable_if<
  5679. std::is_same<typename iterator_traits<IteratorType>::iterator_category, std::random_access_iterator_tag>::value,
  5680. int>::type = 0>
  5681. span_input_adapter(IteratorType first, IteratorType last)
  5682. : ia(input_adapter(first, last)) {}
  5683. contiguous_bytes_input_adapter&& get()
  5684. {
  5685. return std::move(ia); // NOLINT(hicpp-move-const-arg,performance-move-const-arg)
  5686. }
  5687. private:
  5688. contiguous_bytes_input_adapter ia;
  5689. };
  5690. } // namespace detail
  5691. NLOHMANN_JSON_NAMESPACE_END
  5692. // #include <nlohmann/detail/input/json_sax.hpp>
  5693. // __ _____ _____ _____
  5694. // __| | __| | | | JSON for Modern C++
  5695. // | | |__ | | | | | | version 3.11.2
  5696. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5697. //
  5698. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  5699. // SPDX-License-Identifier: MIT
  5700. #include <cstddef>
  5701. #include <string> // string
  5702. #include <utility> // move
  5703. #include <vector> // vector
  5704. // #include <nlohmann/detail/exceptions.hpp>
  5705. // #include <nlohmann/detail/macro_scope.hpp>
  5706. // #include <nlohmann/detail/string_concat.hpp>
  5707. NLOHMANN_JSON_NAMESPACE_BEGIN
  5708. /*!
  5709. @brief SAX interface
  5710. This class describes the SAX interface used by @ref nlohmann::json::sax_parse.
  5711. Each function is called in different situations while the input is parsed. The
  5712. boolean return value informs the parser whether to continue processing the
  5713. input.
  5714. */
  5715. template<typename BasicJsonType>
  5716. struct json_sax
  5717. {
  5718. using number_integer_t = typename BasicJsonType::number_integer_t;
  5719. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  5720. using number_float_t = typename BasicJsonType::number_float_t;
  5721. using string_t = typename BasicJsonType::string_t;
  5722. using binary_t = typename BasicJsonType::binary_t;
  5723. /*!
  5724. @brief a null value was read
  5725. @return whether parsing should proceed
  5726. */
  5727. virtual bool null() = 0;
  5728. /*!
  5729. @brief a boolean value was read
  5730. @param[in] val boolean value
  5731. @return whether parsing should proceed
  5732. */
  5733. virtual bool boolean(bool val) = 0;
  5734. /*!
  5735. @brief an integer number was read
  5736. @param[in] val integer value
  5737. @return whether parsing should proceed
  5738. */
  5739. virtual bool number_integer(number_integer_t val) = 0;
  5740. /*!
  5741. @brief an unsigned integer number was read
  5742. @param[in] val unsigned integer value
  5743. @return whether parsing should proceed
  5744. */
  5745. virtual bool number_unsigned(number_unsigned_t val) = 0;
  5746. /*!
  5747. @brief a floating-point number was read
  5748. @param[in] val floating-point value
  5749. @param[in] s raw token value
  5750. @return whether parsing should proceed
  5751. */
  5752. virtual bool number_float(number_float_t val, const string_t& s) = 0;
  5753. /*!
  5754. @brief a string value was read
  5755. @param[in] val string value
  5756. @return whether parsing should proceed
  5757. @note It is safe to move the passed string value.
  5758. */
  5759. virtual bool string(string_t& val) = 0;
  5760. /*!
  5761. @brief a binary value was read
  5762. @param[in] val binary value
  5763. @return whether parsing should proceed
  5764. @note It is safe to move the passed binary value.
  5765. */
  5766. virtual bool binary(binary_t& val) = 0;
  5767. /*!
  5768. @brief the beginning of an object was read
  5769. @param[in] elements number of object elements or -1 if unknown
  5770. @return whether parsing should proceed
  5771. @note binary formats may report the number of elements
  5772. */
  5773. virtual bool start_object(std::size_t elements) = 0;
  5774. /*!
  5775. @brief an object key was read
  5776. @param[in] val object key
  5777. @return whether parsing should proceed
  5778. @note It is safe to move the passed string.
  5779. */
  5780. virtual bool key(string_t& val) = 0;
  5781. /*!
  5782. @brief the end of an object was read
  5783. @return whether parsing should proceed
  5784. */
  5785. virtual bool end_object() = 0;
  5786. /*!
  5787. @brief the beginning of an array was read
  5788. @param[in] elements number of array elements or -1 if unknown
  5789. @return whether parsing should proceed
  5790. @note binary formats may report the number of elements
  5791. */
  5792. virtual bool start_array(std::size_t elements) = 0;
  5793. /*!
  5794. @brief the end of an array was read
  5795. @return whether parsing should proceed
  5796. */
  5797. virtual bool end_array() = 0;
  5798. /*!
  5799. @brief a parse error occurred
  5800. @param[in] position the position in the input where the error occurs
  5801. @param[in] last_token the last read token
  5802. @param[in] ex an exception object describing the error
  5803. @return whether parsing should proceed (must return false)
  5804. */
  5805. virtual bool parse_error(std::size_t position,
  5806. const std::string& last_token,
  5807. const detail::exception& ex) = 0;
  5808. json_sax() = default;
  5809. json_sax(const json_sax&) = default;
  5810. json_sax(json_sax&&) noexcept = default;
  5811. json_sax& operator=(const json_sax&) = default;
  5812. json_sax& operator=(json_sax&&) noexcept = default;
  5813. virtual ~json_sax() = default;
  5814. };
  5815. namespace detail
  5816. {
  5817. /*!
  5818. @brief SAX implementation to create a JSON value from SAX events
  5819. This class implements the @ref json_sax interface and processes the SAX events
  5820. to create a JSON value which makes it basically a DOM parser. The structure or
  5821. hierarchy of the JSON value is managed by the stack `ref_stack` which contains
  5822. a pointer to the respective array or object for each recursion depth.
  5823. After successful parsing, the value that is passed by reference to the
  5824. constructor contains the parsed value.
  5825. @tparam BasicJsonType the JSON type
  5826. */
  5827. template<typename BasicJsonType>
  5828. class json_sax_dom_parser
  5829. {
  5830. public:
  5831. using number_integer_t = typename BasicJsonType::number_integer_t;
  5832. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  5833. using number_float_t = typename BasicJsonType::number_float_t;
  5834. using string_t = typename BasicJsonType::string_t;
  5835. using binary_t = typename BasicJsonType::binary_t;
  5836. /*!
  5837. @param[in,out] r reference to a JSON value that is manipulated while
  5838. parsing
  5839. @param[in] allow_exceptions_ whether parse errors yield exceptions
  5840. */
  5841. explicit json_sax_dom_parser(BasicJsonType& r, const bool allow_exceptions_ = true)
  5842. : root(r), allow_exceptions(allow_exceptions_)
  5843. {}
  5844. // make class move-only
  5845. json_sax_dom_parser(const json_sax_dom_parser&) = delete;
  5846. json_sax_dom_parser(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  5847. json_sax_dom_parser& operator=(const json_sax_dom_parser&) = delete;
  5848. json_sax_dom_parser& operator=(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  5849. ~json_sax_dom_parser() = default;
  5850. bool null()
  5851. {
  5852. handle_value(nullptr);
  5853. return true;
  5854. }
  5855. bool boolean(bool val)
  5856. {
  5857. handle_value(val);
  5858. return true;
  5859. }
  5860. bool number_integer(number_integer_t val)
  5861. {
  5862. handle_value(val);
  5863. return true;
  5864. }
  5865. bool number_unsigned(number_unsigned_t val)
  5866. {
  5867. handle_value(val);
  5868. return true;
  5869. }
  5870. bool number_float(number_float_t val, const string_t& /*unused*/)
  5871. {
  5872. handle_value(val);
  5873. return true;
  5874. }
  5875. bool string(string_t& val)
  5876. {
  5877. handle_value(val);
  5878. return true;
  5879. }
  5880. bool binary(binary_t& val)
  5881. {
  5882. handle_value(std::move(val));
  5883. return true;
  5884. }
  5885. bool start_object(std::size_t len)
  5886. {
  5887. ref_stack.push_back(handle_value(BasicJsonType::value_t::object));
  5888. if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
  5889. {
  5890. JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
  5891. }
  5892. return true;
  5893. }
  5894. bool key(string_t& val)
  5895. {
  5896. JSON_ASSERT(!ref_stack.empty());
  5897. JSON_ASSERT(ref_stack.back()->is_object());
  5898. // add null at given key and store the reference for later
  5899. object_element = &(ref_stack.back()->m_value.object->operator[](val));
  5900. return true;
  5901. }
  5902. bool end_object()
  5903. {
  5904. JSON_ASSERT(!ref_stack.empty());
  5905. JSON_ASSERT(ref_stack.back()->is_object());
  5906. ref_stack.back()->set_parents();
  5907. ref_stack.pop_back();
  5908. return true;
  5909. }
  5910. bool start_array(std::size_t len)
  5911. {
  5912. ref_stack.push_back(handle_value(BasicJsonType::value_t::array));
  5913. if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
  5914. {
  5915. JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
  5916. }
  5917. return true;
  5918. }
  5919. bool end_array()
  5920. {
  5921. JSON_ASSERT(!ref_stack.empty());
  5922. JSON_ASSERT(ref_stack.back()->is_array());
  5923. ref_stack.back()->set_parents();
  5924. ref_stack.pop_back();
  5925. return true;
  5926. }
  5927. template<class Exception>
  5928. bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
  5929. const Exception& ex)
  5930. {
  5931. errored = true;
  5932. static_cast<void>(ex);
  5933. if (allow_exceptions)
  5934. {
  5935. JSON_THROW(ex);
  5936. }
  5937. return false;
  5938. }
  5939. constexpr bool is_errored() const
  5940. {
  5941. return errored;
  5942. }
  5943. private:
  5944. /*!
  5945. @invariant If the ref stack is empty, then the passed value will be the new
  5946. root.
  5947. @invariant If the ref stack contains a value, then it is an array or an
  5948. object to which we can add elements
  5949. */
  5950. template<typename Value>
  5951. JSON_HEDLEY_RETURNS_NON_NULL
  5952. BasicJsonType* handle_value(Value&& v)
  5953. {
  5954. if (ref_stack.empty())
  5955. {
  5956. root = BasicJsonType(std::forward<Value>(v));
  5957. return &root;
  5958. }
  5959. JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
  5960. if (ref_stack.back()->is_array())
  5961. {
  5962. ref_stack.back()->m_value.array->emplace_back(std::forward<Value>(v));
  5963. return &(ref_stack.back()->m_value.array->back());
  5964. }
  5965. JSON_ASSERT(ref_stack.back()->is_object());
  5966. JSON_ASSERT(object_element);
  5967. *object_element = BasicJsonType(std::forward<Value>(v));
  5968. return object_element;
  5969. }
  5970. /// the parsed JSON value
  5971. BasicJsonType& root;
  5972. /// stack to model hierarchy of values
  5973. std::vector<BasicJsonType*> ref_stack {};
  5974. /// helper to hold the reference for the next object element
  5975. BasicJsonType* object_element = nullptr;
  5976. /// whether a syntax error occurred
  5977. bool errored = false;
  5978. /// whether to throw exceptions in case of errors
  5979. const bool allow_exceptions = true;
  5980. };
  5981. template<typename BasicJsonType>
  5982. class json_sax_dom_callback_parser
  5983. {
  5984. public:
  5985. using number_integer_t = typename BasicJsonType::number_integer_t;
  5986. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  5987. using number_float_t = typename BasicJsonType::number_float_t;
  5988. using string_t = typename BasicJsonType::string_t;
  5989. using binary_t = typename BasicJsonType::binary_t;
  5990. using parser_callback_t = typename BasicJsonType::parser_callback_t;
  5991. using parse_event_t = typename BasicJsonType::parse_event_t;
  5992. json_sax_dom_callback_parser(BasicJsonType& r,
  5993. const parser_callback_t cb,
  5994. const bool allow_exceptions_ = true)
  5995. : root(r), callback(cb), allow_exceptions(allow_exceptions_)
  5996. {
  5997. keep_stack.push_back(true);
  5998. }
  5999. // make class move-only
  6000. json_sax_dom_callback_parser(const json_sax_dom_callback_parser&) = delete;
  6001. json_sax_dom_callback_parser(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  6002. json_sax_dom_callback_parser& operator=(const json_sax_dom_callback_parser&) = delete;
  6003. json_sax_dom_callback_parser& operator=(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  6004. ~json_sax_dom_callback_parser() = default;
  6005. bool null()
  6006. {
  6007. handle_value(nullptr);
  6008. return true;
  6009. }
  6010. bool boolean(bool val)
  6011. {
  6012. handle_value(val);
  6013. return true;
  6014. }
  6015. bool number_integer(number_integer_t val)
  6016. {
  6017. handle_value(val);
  6018. return true;
  6019. }
  6020. bool number_unsigned(number_unsigned_t val)
  6021. {
  6022. handle_value(val);
  6023. return true;
  6024. }
  6025. bool number_float(number_float_t val, const string_t& /*unused*/)
  6026. {
  6027. handle_value(val);
  6028. return true;
  6029. }
  6030. bool string(string_t& val)
  6031. {
  6032. handle_value(val);
  6033. return true;
  6034. }
  6035. bool binary(binary_t& val)
  6036. {
  6037. handle_value(std::move(val));
  6038. return true;
  6039. }
  6040. bool start_object(std::size_t len)
  6041. {
  6042. // check callback for object start
  6043. const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::object_start, discarded);
  6044. keep_stack.push_back(keep);
  6045. auto val = handle_value(BasicJsonType::value_t::object, true);
  6046. ref_stack.push_back(val.second);
  6047. // check object limit
  6048. if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
  6049. {
  6050. JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
  6051. }
  6052. return true;
  6053. }
  6054. bool key(string_t& val)
  6055. {
  6056. BasicJsonType k = BasicJsonType(val);
  6057. // check callback for key
  6058. const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::key, k);
  6059. key_keep_stack.push_back(keep);
  6060. // add discarded value at given key and store the reference for later
  6061. if (keep && ref_stack.back())
  6062. {
  6063. object_element = &(ref_stack.back()->m_value.object->operator[](val) = discarded);
  6064. }
  6065. return true;
  6066. }
  6067. bool end_object()
  6068. {
  6069. if (ref_stack.back())
  6070. {
  6071. if (!callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back()))
  6072. {
  6073. // discard object
  6074. *ref_stack.back() = discarded;
  6075. }
  6076. else
  6077. {
  6078. ref_stack.back()->set_parents();
  6079. }
  6080. }
  6081. JSON_ASSERT(!ref_stack.empty());
  6082. JSON_ASSERT(!keep_stack.empty());
  6083. ref_stack.pop_back();
  6084. keep_stack.pop_back();
  6085. if (!ref_stack.empty() && ref_stack.back() && ref_stack.back()->is_structured())
  6086. {
  6087. // remove discarded value
  6088. for (auto it = ref_stack.back()->begin(); it != ref_stack.back()->end(); ++it)
  6089. {
  6090. if (it->is_discarded())
  6091. {
  6092. ref_stack.back()->erase(it);
  6093. break;
  6094. }
  6095. }
  6096. }
  6097. return true;
  6098. }
  6099. bool start_array(std::size_t len)
  6100. {
  6101. const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::array_start, discarded);
  6102. keep_stack.push_back(keep);
  6103. auto val = handle_value(BasicJsonType::value_t::array, true);
  6104. ref_stack.push_back(val.second);
  6105. // check array limit
  6106. if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
  6107. {
  6108. JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
  6109. }
  6110. return true;
  6111. }
  6112. bool end_array()
  6113. {
  6114. bool keep = true;
  6115. if (ref_stack.back())
  6116. {
  6117. keep = callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::array_end, *ref_stack.back());
  6118. if (keep)
  6119. {
  6120. ref_stack.back()->set_parents();
  6121. }
  6122. else
  6123. {
  6124. // discard array
  6125. *ref_stack.back() = discarded;
  6126. }
  6127. }
  6128. JSON_ASSERT(!ref_stack.empty());
  6129. JSON_ASSERT(!keep_stack.empty());
  6130. ref_stack.pop_back();
  6131. keep_stack.pop_back();
  6132. // remove discarded value
  6133. if (!keep && !ref_stack.empty() && ref_stack.back()->is_array())
  6134. {
  6135. ref_stack.back()->m_value.array->pop_back();
  6136. }
  6137. return true;
  6138. }
  6139. template<class Exception>
  6140. bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
  6141. const Exception& ex)
  6142. {
  6143. errored = true;
  6144. static_cast<void>(ex);
  6145. if (allow_exceptions)
  6146. {
  6147. JSON_THROW(ex);
  6148. }
  6149. return false;
  6150. }
  6151. constexpr bool is_errored() const
  6152. {
  6153. return errored;
  6154. }
  6155. private:
  6156. /*!
  6157. @param[in] v value to add to the JSON value we build during parsing
  6158. @param[in] skip_callback whether we should skip calling the callback
  6159. function; this is required after start_array() and
  6160. start_object() SAX events, because otherwise we would call the
  6161. callback function with an empty array or object, respectively.
  6162. @invariant If the ref stack is empty, then the passed value will be the new
  6163. root.
  6164. @invariant If the ref stack contains a value, then it is an array or an
  6165. object to which we can add elements
  6166. @return pair of boolean (whether value should be kept) and pointer (to the
  6167. passed value in the ref_stack hierarchy; nullptr if not kept)
  6168. */
  6169. template<typename Value>
  6170. std::pair<bool, BasicJsonType*> handle_value(Value&& v, const bool skip_callback = false)
  6171. {
  6172. JSON_ASSERT(!keep_stack.empty());
  6173. // do not handle this value if we know it would be added to a discarded
  6174. // container
  6175. if (!keep_stack.back())
  6176. {
  6177. return {false, nullptr};
  6178. }
  6179. // create value
  6180. auto value = BasicJsonType(std::forward<Value>(v));
  6181. // check callback
  6182. const bool keep = skip_callback || callback(static_cast<int>(ref_stack.size()), parse_event_t::value, value);
  6183. // do not handle this value if we just learnt it shall be discarded
  6184. if (!keep)
  6185. {
  6186. return {false, nullptr};
  6187. }
  6188. if (ref_stack.empty())
  6189. {
  6190. root = std::move(value);
  6191. return {true, &root};
  6192. }
  6193. // skip this value if we already decided to skip the parent
  6194. // (https://github.com/nlohmann/json/issues/971#issuecomment-413678360)
  6195. if (!ref_stack.back())
  6196. {
  6197. return {false, nullptr};
  6198. }
  6199. // we now only expect arrays and objects
  6200. JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
  6201. // array
  6202. if (ref_stack.back()->is_array())
  6203. {
  6204. ref_stack.back()->m_value.array->emplace_back(std::move(value));
  6205. return {true, &(ref_stack.back()->m_value.array->back())};
  6206. }
  6207. // object
  6208. JSON_ASSERT(ref_stack.back()->is_object());
  6209. // check if we should store an element for the current key
  6210. JSON_ASSERT(!key_keep_stack.empty());
  6211. const bool store_element = key_keep_stack.back();
  6212. key_keep_stack.pop_back();
  6213. if (!store_element)
  6214. {
  6215. return {false, nullptr};
  6216. }
  6217. JSON_ASSERT(object_element);
  6218. *object_element = std::move(value);
  6219. return {true, object_element};
  6220. }
  6221. /// the parsed JSON value
  6222. BasicJsonType& root;
  6223. /// stack to model hierarchy of values
  6224. std::vector<BasicJsonType*> ref_stack {};
  6225. /// stack to manage which values to keep
  6226. std::vector<bool> keep_stack {};
  6227. /// stack to manage which object keys to keep
  6228. std::vector<bool> key_keep_stack {};
  6229. /// helper to hold the reference for the next object element
  6230. BasicJsonType* object_element = nullptr;
  6231. /// whether a syntax error occurred
  6232. bool errored = false;
  6233. /// callback function
  6234. const parser_callback_t callback = nullptr;
  6235. /// whether to throw exceptions in case of errors
  6236. const bool allow_exceptions = true;
  6237. /// a discarded value for the callback
  6238. BasicJsonType discarded = BasicJsonType::value_t::discarded;
  6239. };
  6240. template<typename BasicJsonType>
  6241. class json_sax_acceptor
  6242. {
  6243. public:
  6244. using number_integer_t = typename BasicJsonType::number_integer_t;
  6245. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  6246. using number_float_t = typename BasicJsonType::number_float_t;
  6247. using string_t = typename BasicJsonType::string_t;
  6248. using binary_t = typename BasicJsonType::binary_t;
  6249. bool null()
  6250. {
  6251. return true;
  6252. }
  6253. bool boolean(bool /*unused*/)
  6254. {
  6255. return true;
  6256. }
  6257. bool number_integer(number_integer_t /*unused*/)
  6258. {
  6259. return true;
  6260. }
  6261. bool number_unsigned(number_unsigned_t /*unused*/)
  6262. {
  6263. return true;
  6264. }
  6265. bool number_float(number_float_t /*unused*/, const string_t& /*unused*/)
  6266. {
  6267. return true;
  6268. }
  6269. bool string(string_t& /*unused*/)
  6270. {
  6271. return true;
  6272. }
  6273. bool binary(binary_t& /*unused*/)
  6274. {
  6275. return true;
  6276. }
  6277. bool start_object(std::size_t /*unused*/ = static_cast<std::size_t>(-1))
  6278. {
  6279. return true;
  6280. }
  6281. bool key(string_t& /*unused*/)
  6282. {
  6283. return true;
  6284. }
  6285. bool end_object()
  6286. {
  6287. return true;
  6288. }
  6289. bool start_array(std::size_t /*unused*/ = static_cast<std::size_t>(-1))
  6290. {
  6291. return true;
  6292. }
  6293. bool end_array()
  6294. {
  6295. return true;
  6296. }
  6297. bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const detail::exception& /*unused*/)
  6298. {
  6299. return false;
  6300. }
  6301. };
  6302. } // namespace detail
  6303. NLOHMANN_JSON_NAMESPACE_END
  6304. // #include <nlohmann/detail/input/lexer.hpp>
  6305. // __ _____ _____ _____
  6306. // __| | __| | | | JSON for Modern C++
  6307. // | | |__ | | | | | | version 3.11.2
  6308. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  6309. //
  6310. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  6311. // SPDX-License-Identifier: MIT
  6312. #include <array> // array
  6313. #include <clocale> // localeconv
  6314. #include <cstddef> // size_t
  6315. #include <cstdio> // snprintf
  6316. #include <cstdlib> // strtof, strtod, strtold, strtoll, strtoull
  6317. #include <initializer_list> // initializer_list
  6318. #include <string> // char_traits, string
  6319. #include <utility> // move
  6320. #include <vector> // vector
  6321. // #include <nlohmann/detail/input/input_adapters.hpp>
  6322. // #include <nlohmann/detail/input/position_t.hpp>
  6323. // #include <nlohmann/detail/macro_scope.hpp>
  6324. NLOHMANN_JSON_NAMESPACE_BEGIN
  6325. namespace detail
  6326. {
  6327. ///////////
  6328. // lexer //
  6329. ///////////
  6330. template<typename BasicJsonType>
  6331. class lexer_base
  6332. {
  6333. public:
  6334. /// token types for the parser
  6335. enum class token_type
  6336. {
  6337. uninitialized, ///< indicating the scanner is uninitialized
  6338. literal_true, ///< the `true` literal
  6339. literal_false, ///< the `false` literal
  6340. literal_null, ///< the `null` literal
  6341. value_string, ///< a string -- use get_string() for actual value
  6342. value_unsigned, ///< an unsigned integer -- use get_number_unsigned() for actual value
  6343. value_integer, ///< a signed integer -- use get_number_integer() for actual value
  6344. value_float, ///< an floating point number -- use get_number_float() for actual value
  6345. begin_array, ///< the character for array begin `[`
  6346. begin_object, ///< the character for object begin `{`
  6347. end_array, ///< the character for array end `]`
  6348. end_object, ///< the character for object end `}`
  6349. name_separator, ///< the name separator `:`
  6350. value_separator, ///< the value separator `,`
  6351. parse_error, ///< indicating a parse error
  6352. end_of_input, ///< indicating the end of the input buffer
  6353. literal_or_value ///< a literal or the begin of a value (only for diagnostics)
  6354. };
  6355. /// return name of values of type token_type (only used for errors)
  6356. JSON_HEDLEY_RETURNS_NON_NULL
  6357. JSON_HEDLEY_CONST
  6358. static const char* token_type_name(const token_type t) noexcept
  6359. {
  6360. switch (t)
  6361. {
  6362. case token_type::uninitialized:
  6363. return "<uninitialized>";
  6364. case token_type::literal_true:
  6365. return "true literal";
  6366. case token_type::literal_false:
  6367. return "false literal";
  6368. case token_type::literal_null:
  6369. return "null literal";
  6370. case token_type::value_string:
  6371. return "string literal";
  6372. case token_type::value_unsigned:
  6373. case token_type::value_integer:
  6374. case token_type::value_float:
  6375. return "number literal";
  6376. case token_type::begin_array:
  6377. return "'['";
  6378. case token_type::begin_object:
  6379. return "'{'";
  6380. case token_type::end_array:
  6381. return "']'";
  6382. case token_type::end_object:
  6383. return "'}'";
  6384. case token_type::name_separator:
  6385. return "':'";
  6386. case token_type::value_separator:
  6387. return "','";
  6388. case token_type::parse_error:
  6389. return "<parse error>";
  6390. case token_type::end_of_input:
  6391. return "end of input";
  6392. case token_type::literal_or_value:
  6393. return "'[', '{', or a literal";
  6394. // LCOV_EXCL_START
  6395. default: // catch non-enum values
  6396. return "unknown token";
  6397. // LCOV_EXCL_STOP
  6398. }
  6399. }
  6400. };
  6401. /*!
  6402. @brief lexical analysis
  6403. This class organizes the lexical analysis during JSON deserialization.
  6404. */
  6405. template<typename BasicJsonType, typename InputAdapterType>
  6406. class lexer : public lexer_base<BasicJsonType>
  6407. {
  6408. using number_integer_t = typename BasicJsonType::number_integer_t;
  6409. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  6410. using number_float_t = typename BasicJsonType::number_float_t;
  6411. using string_t = typename BasicJsonType::string_t;
  6412. using char_type = typename InputAdapterType::char_type;
  6413. using char_int_type = typename std::char_traits<char_type>::int_type;
  6414. public:
  6415. using token_type = typename lexer_base<BasicJsonType>::token_type;
  6416. explicit lexer(InputAdapterType&& adapter, bool ignore_comments_ = false) noexcept
  6417. : ia(std::move(adapter))
  6418. , ignore_comments(ignore_comments_)
  6419. , decimal_point_char(static_cast<char_int_type>(get_decimal_point()))
  6420. {}
  6421. // delete because of pointer members
  6422. lexer(const lexer&) = delete;
  6423. lexer(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  6424. lexer& operator=(lexer&) = delete;
  6425. lexer& operator=(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  6426. ~lexer() = default;
  6427. private:
  6428. /////////////////////
  6429. // locales
  6430. /////////////////////
  6431. /// return the locale-dependent decimal point
  6432. JSON_HEDLEY_PURE
  6433. static char get_decimal_point() noexcept
  6434. {
  6435. const auto* loc = localeconv();
  6436. JSON_ASSERT(loc != nullptr);
  6437. return (loc->decimal_point == nullptr) ? '.' : *(loc->decimal_point);
  6438. }
  6439. /////////////////////
  6440. // scan functions
  6441. /////////////////////
  6442. /*!
  6443. @brief get codepoint from 4 hex characters following `\u`
  6444. For input "\u c1 c2 c3 c4" the codepoint is:
  6445. (c1 * 0x1000) + (c2 * 0x0100) + (c3 * 0x0010) + c4
  6446. = (c1 << 12) + (c2 << 8) + (c3 << 4) + (c4 << 0)
  6447. Furthermore, the possible characters '0'..'9', 'A'..'F', and 'a'..'f'
  6448. must be converted to the integers 0x0..0x9, 0xA..0xF, 0xA..0xF, resp. The
  6449. conversion is done by subtracting the offset (0x30, 0x37, and 0x57)
  6450. between the ASCII value of the character and the desired integer value.
  6451. @return codepoint (0x0000..0xFFFF) or -1 in case of an error (e.g. EOF or
  6452. non-hex character)
  6453. */
  6454. int get_codepoint()
  6455. {
  6456. // this function only makes sense after reading `\u`
  6457. JSON_ASSERT(current == 'u');
  6458. int codepoint = 0;
  6459. const auto factors = { 12u, 8u, 4u, 0u };
  6460. for (const auto factor : factors)
  6461. {
  6462. get();
  6463. if (current >= '0' && current <= '9')
  6464. {
  6465. codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x30u) << factor);
  6466. }
  6467. else if (current >= 'A' && current <= 'F')
  6468. {
  6469. codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x37u) << factor);
  6470. }
  6471. else if (current >= 'a' && current <= 'f')
  6472. {
  6473. codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x57u) << factor);
  6474. }
  6475. else
  6476. {
  6477. return -1;
  6478. }
  6479. }
  6480. JSON_ASSERT(0x0000 <= codepoint && codepoint <= 0xFFFF);
  6481. return codepoint;
  6482. }
  6483. /*!
  6484. @brief check if the next byte(s) are inside a given range
  6485. Adds the current byte and, for each passed range, reads a new byte and
  6486. checks if it is inside the range. If a violation was detected, set up an
  6487. error message and return false. Otherwise, return true.
  6488. @param[in] ranges list of integers; interpreted as list of pairs of
  6489. inclusive lower and upper bound, respectively
  6490. @pre The passed list @a ranges must have 2, 4, or 6 elements; that is,
  6491. 1, 2, or 3 pairs. This precondition is enforced by an assertion.
  6492. @return true if and only if no range violation was detected
  6493. */
  6494. bool next_byte_in_range(std::initializer_list<char_int_type> ranges)
  6495. {
  6496. JSON_ASSERT(ranges.size() == 2 || ranges.size() == 4 || ranges.size() == 6);
  6497. add(current);
  6498. for (auto range = ranges.begin(); range != ranges.end(); ++range)
  6499. {
  6500. get();
  6501. if (JSON_HEDLEY_LIKELY(*range <= current && current <= *(++range)))
  6502. {
  6503. add(current);
  6504. }
  6505. else
  6506. {
  6507. error_message = "invalid string: ill-formed UTF-8 byte";
  6508. return false;
  6509. }
  6510. }
  6511. return true;
  6512. }
  6513. /*!
  6514. @brief scan a string literal
  6515. This function scans a string according to Sect. 7 of RFC 8259. While
  6516. scanning, bytes are escaped and copied into buffer token_buffer. Then the
  6517. function returns successfully, token_buffer is *not* null-terminated (as it
  6518. may contain \0 bytes), and token_buffer.size() is the number of bytes in the
  6519. string.
  6520. @return token_type::value_string if string could be successfully scanned,
  6521. token_type::parse_error otherwise
  6522. @note In case of errors, variable error_message contains a textual
  6523. description.
  6524. */
  6525. token_type scan_string()
  6526. {
  6527. // reset token_buffer (ignore opening quote)
  6528. reset();
  6529. // we entered the function by reading an open quote
  6530. JSON_ASSERT(current == '\"');
  6531. while (true)
  6532. {
  6533. // get next character
  6534. switch (get())
  6535. {
  6536. // end of file while parsing string
  6537. case std::char_traits<char_type>::eof():
  6538. {
  6539. error_message = "invalid string: missing closing quote";
  6540. return token_type::parse_error;
  6541. }
  6542. // closing quote
  6543. case '\"':
  6544. {
  6545. return token_type::value_string;
  6546. }
  6547. // escapes
  6548. case '\\':
  6549. {
  6550. switch (get())
  6551. {
  6552. // quotation mark
  6553. case '\"':
  6554. add('\"');
  6555. break;
  6556. // reverse solidus
  6557. case '\\':
  6558. add('\\');
  6559. break;
  6560. // solidus
  6561. case '/':
  6562. add('/');
  6563. break;
  6564. // backspace
  6565. case 'b':
  6566. add('\b');
  6567. break;
  6568. // form feed
  6569. case 'f':
  6570. add('\f');
  6571. break;
  6572. // line feed
  6573. case 'n':
  6574. add('\n');
  6575. break;
  6576. // carriage return
  6577. case 'r':
  6578. add('\r');
  6579. break;
  6580. // tab
  6581. case 't':
  6582. add('\t');
  6583. break;
  6584. // unicode escapes
  6585. case 'u':
  6586. {
  6587. const int codepoint1 = get_codepoint();
  6588. int codepoint = codepoint1; // start with codepoint1
  6589. if (JSON_HEDLEY_UNLIKELY(codepoint1 == -1))
  6590. {
  6591. error_message = "invalid string: '\\u' must be followed by 4 hex digits";
  6592. return token_type::parse_error;
  6593. }
  6594. // check if code point is a high surrogate
  6595. if (0xD800 <= codepoint1 && codepoint1 <= 0xDBFF)
  6596. {
  6597. // expect next \uxxxx entry
  6598. if (JSON_HEDLEY_LIKELY(get() == '\\' && get() == 'u'))
  6599. {
  6600. const int codepoint2 = get_codepoint();
  6601. if (JSON_HEDLEY_UNLIKELY(codepoint2 == -1))
  6602. {
  6603. error_message = "invalid string: '\\u' must be followed by 4 hex digits";
  6604. return token_type::parse_error;
  6605. }
  6606. // check if codepoint2 is a low surrogate
  6607. if (JSON_HEDLEY_LIKELY(0xDC00 <= codepoint2 && codepoint2 <= 0xDFFF))
  6608. {
  6609. // overwrite codepoint
  6610. codepoint = static_cast<int>(
  6611. // high surrogate occupies the most significant 22 bits
  6612. (static_cast<unsigned int>(codepoint1) << 10u)
  6613. // low surrogate occupies the least significant 15 bits
  6614. + static_cast<unsigned int>(codepoint2)
  6615. // there is still the 0xD800, 0xDC00 and 0x10000 noise
  6616. // in the result, so we have to subtract with:
  6617. // (0xD800 << 10) + DC00 - 0x10000 = 0x35FDC00
  6618. - 0x35FDC00u);
  6619. }
  6620. else
  6621. {
  6622. error_message = "invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF";
  6623. return token_type::parse_error;
  6624. }
  6625. }
  6626. else
  6627. {
  6628. error_message = "invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF";
  6629. return token_type::parse_error;
  6630. }
  6631. }
  6632. else
  6633. {
  6634. if (JSON_HEDLEY_UNLIKELY(0xDC00 <= codepoint1 && codepoint1 <= 0xDFFF))
  6635. {
  6636. error_message = "invalid string: surrogate U+DC00..U+DFFF must follow U+D800..U+DBFF";
  6637. return token_type::parse_error;
  6638. }
  6639. }
  6640. // result of the above calculation yields a proper codepoint
  6641. JSON_ASSERT(0x00 <= codepoint && codepoint <= 0x10FFFF);
  6642. // translate codepoint into bytes
  6643. if (codepoint < 0x80)
  6644. {
  6645. // 1-byte characters: 0xxxxxxx (ASCII)
  6646. add(static_cast<char_int_type>(codepoint));
  6647. }
  6648. else if (codepoint <= 0x7FF)
  6649. {
  6650. // 2-byte characters: 110xxxxx 10xxxxxx
  6651. add(static_cast<char_int_type>(0xC0u | (static_cast<unsigned int>(codepoint) >> 6u)));
  6652. add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
  6653. }
  6654. else if (codepoint <= 0xFFFF)
  6655. {
  6656. // 3-byte characters: 1110xxxx 10xxxxxx 10xxxxxx
  6657. add(static_cast<char_int_type>(0xE0u | (static_cast<unsigned int>(codepoint) >> 12u)));
  6658. add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 6u) & 0x3Fu)));
  6659. add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
  6660. }
  6661. else
  6662. {
  6663. // 4-byte characters: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  6664. add(static_cast<char_int_type>(0xF0u | (static_cast<unsigned int>(codepoint) >> 18u)));
  6665. add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 12u) & 0x3Fu)));
  6666. add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 6u) & 0x3Fu)));
  6667. add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
  6668. }
  6669. break;
  6670. }
  6671. // other characters after escape
  6672. default:
  6673. error_message = "invalid string: forbidden character after backslash";
  6674. return token_type::parse_error;
  6675. }
  6676. break;
  6677. }
  6678. // invalid control characters
  6679. case 0x00:
  6680. {
  6681. error_message = "invalid string: control character U+0000 (NUL) must be escaped to \\u0000";
  6682. return token_type::parse_error;
  6683. }
  6684. case 0x01:
  6685. {
  6686. error_message = "invalid string: control character U+0001 (SOH) must be escaped to \\u0001";
  6687. return token_type::parse_error;
  6688. }
  6689. case 0x02:
  6690. {
  6691. error_message = "invalid string: control character U+0002 (STX) must be escaped to \\u0002";
  6692. return token_type::parse_error;
  6693. }
  6694. case 0x03:
  6695. {
  6696. error_message = "invalid string: control character U+0003 (ETX) must be escaped to \\u0003";
  6697. return token_type::parse_error;
  6698. }
  6699. case 0x04:
  6700. {
  6701. error_message = "invalid string: control character U+0004 (EOT) must be escaped to \\u0004";
  6702. return token_type::parse_error;
  6703. }
  6704. case 0x05:
  6705. {
  6706. error_message = "invalid string: control character U+0005 (ENQ) must be escaped to \\u0005";
  6707. return token_type::parse_error;
  6708. }
  6709. case 0x06:
  6710. {
  6711. error_message = "invalid string: control character U+0006 (ACK) must be escaped to \\u0006";
  6712. return token_type::parse_error;
  6713. }
  6714. case 0x07:
  6715. {
  6716. error_message = "invalid string: control character U+0007 (BEL) must be escaped to \\u0007";
  6717. return token_type::parse_error;
  6718. }
  6719. case 0x08:
  6720. {
  6721. error_message = "invalid string: control character U+0008 (BS) must be escaped to \\u0008 or \\b";
  6722. return token_type::parse_error;
  6723. }
  6724. case 0x09:
  6725. {
  6726. error_message = "invalid string: control character U+0009 (HT) must be escaped to \\u0009 or \\t";
  6727. return token_type::parse_error;
  6728. }
  6729. case 0x0A:
  6730. {
  6731. error_message = "invalid string: control character U+000A (LF) must be escaped to \\u000A or \\n";
  6732. return token_type::parse_error;
  6733. }
  6734. case 0x0B:
  6735. {
  6736. error_message = "invalid string: control character U+000B (VT) must be escaped to \\u000B";
  6737. return token_type::parse_error;
  6738. }
  6739. case 0x0C:
  6740. {
  6741. error_message = "invalid string: control character U+000C (FF) must be escaped to \\u000C or \\f";
  6742. return token_type::parse_error;
  6743. }
  6744. case 0x0D:
  6745. {
  6746. error_message = "invalid string: control character U+000D (CR) must be escaped to \\u000D or \\r";
  6747. return token_type::parse_error;
  6748. }
  6749. case 0x0E:
  6750. {
  6751. error_message = "invalid string: control character U+000E (SO) must be escaped to \\u000E";
  6752. return token_type::parse_error;
  6753. }
  6754. case 0x0F:
  6755. {
  6756. error_message = "invalid string: control character U+000F (SI) must be escaped to \\u000F";
  6757. return token_type::parse_error;
  6758. }
  6759. case 0x10:
  6760. {
  6761. error_message = "invalid string: control character U+0010 (DLE) must be escaped to \\u0010";
  6762. return token_type::parse_error;
  6763. }
  6764. case 0x11:
  6765. {
  6766. error_message = "invalid string: control character U+0011 (DC1) must be escaped to \\u0011";
  6767. return token_type::parse_error;
  6768. }
  6769. case 0x12:
  6770. {
  6771. error_message = "invalid string: control character U+0012 (DC2) must be escaped to \\u0012";
  6772. return token_type::parse_error;
  6773. }
  6774. case 0x13:
  6775. {
  6776. error_message = "invalid string: control character U+0013 (DC3) must be escaped to \\u0013";
  6777. return token_type::parse_error;
  6778. }
  6779. case 0x14:
  6780. {
  6781. error_message = "invalid string: control character U+0014 (DC4) must be escaped to \\u0014";
  6782. return token_type::parse_error;
  6783. }
  6784. case 0x15:
  6785. {
  6786. error_message = "invalid string: control character U+0015 (NAK) must be escaped to \\u0015";
  6787. return token_type::parse_error;
  6788. }
  6789. case 0x16:
  6790. {
  6791. error_message = "invalid string: control character U+0016 (SYN) must be escaped to \\u0016";
  6792. return token_type::parse_error;
  6793. }
  6794. case 0x17:
  6795. {
  6796. error_message = "invalid string: control character U+0017 (ETB) must be escaped to \\u0017";
  6797. return token_type::parse_error;
  6798. }
  6799. case 0x18:
  6800. {
  6801. error_message = "invalid string: control character U+0018 (CAN) must be escaped to \\u0018";
  6802. return token_type::parse_error;
  6803. }
  6804. case 0x19:
  6805. {
  6806. error_message = "invalid string: control character U+0019 (EM) must be escaped to \\u0019";
  6807. return token_type::parse_error;
  6808. }
  6809. case 0x1A:
  6810. {
  6811. error_message = "invalid string: control character U+001A (SUB) must be escaped to \\u001A";
  6812. return token_type::parse_error;
  6813. }
  6814. case 0x1B:
  6815. {
  6816. error_message = "invalid string: control character U+001B (ESC) must be escaped to \\u001B";
  6817. return token_type::parse_error;
  6818. }
  6819. case 0x1C:
  6820. {
  6821. error_message = "invalid string: control character U+001C (FS) must be escaped to \\u001C";
  6822. return token_type::parse_error;
  6823. }
  6824. case 0x1D:
  6825. {
  6826. error_message = "invalid string: control character U+001D (GS) must be escaped to \\u001D";
  6827. return token_type::parse_error;
  6828. }
  6829. case 0x1E:
  6830. {
  6831. error_message = "invalid string: control character U+001E (RS) must be escaped to \\u001E";
  6832. return token_type::parse_error;
  6833. }
  6834. case 0x1F:
  6835. {
  6836. error_message = "invalid string: control character U+001F (US) must be escaped to \\u001F";
  6837. return token_type::parse_error;
  6838. }
  6839. // U+0020..U+007F (except U+0022 (quote) and U+005C (backspace))
  6840. case 0x20:
  6841. case 0x21:
  6842. case 0x23:
  6843. case 0x24:
  6844. case 0x25:
  6845. case 0x26:
  6846. case 0x27:
  6847. case 0x28:
  6848. case 0x29:
  6849. case 0x2A:
  6850. case 0x2B:
  6851. case 0x2C:
  6852. case 0x2D:
  6853. case 0x2E:
  6854. case 0x2F:
  6855. case 0x30:
  6856. case 0x31:
  6857. case 0x32:
  6858. case 0x33:
  6859. case 0x34:
  6860. case 0x35:
  6861. case 0x36:
  6862. case 0x37:
  6863. case 0x38:
  6864. case 0x39:
  6865. case 0x3A:
  6866. case 0x3B:
  6867. case 0x3C:
  6868. case 0x3D:
  6869. case 0x3E:
  6870. case 0x3F:
  6871. case 0x40:
  6872. case 0x41:
  6873. case 0x42:
  6874. case 0x43:
  6875. case 0x44:
  6876. case 0x45:
  6877. case 0x46:
  6878. case 0x47:
  6879. case 0x48:
  6880. case 0x49:
  6881. case 0x4A:
  6882. case 0x4B:
  6883. case 0x4C:
  6884. case 0x4D:
  6885. case 0x4E:
  6886. case 0x4F:
  6887. case 0x50:
  6888. case 0x51:
  6889. case 0x52:
  6890. case 0x53:
  6891. case 0x54:
  6892. case 0x55:
  6893. case 0x56:
  6894. case 0x57:
  6895. case 0x58:
  6896. case 0x59:
  6897. case 0x5A:
  6898. case 0x5B:
  6899. case 0x5D:
  6900. case 0x5E:
  6901. case 0x5F:
  6902. case 0x60:
  6903. case 0x61:
  6904. case 0x62:
  6905. case 0x63:
  6906. case 0x64:
  6907. case 0x65:
  6908. case 0x66:
  6909. case 0x67:
  6910. case 0x68:
  6911. case 0x69:
  6912. case 0x6A:
  6913. case 0x6B:
  6914. case 0x6C:
  6915. case 0x6D:
  6916. case 0x6E:
  6917. case 0x6F:
  6918. case 0x70:
  6919. case 0x71:
  6920. case 0x72:
  6921. case 0x73:
  6922. case 0x74:
  6923. case 0x75:
  6924. case 0x76:
  6925. case 0x77:
  6926. case 0x78:
  6927. case 0x79:
  6928. case 0x7A:
  6929. case 0x7B:
  6930. case 0x7C:
  6931. case 0x7D:
  6932. case 0x7E:
  6933. case 0x7F:
  6934. {
  6935. add(current);
  6936. break;
  6937. }
  6938. // U+0080..U+07FF: bytes C2..DF 80..BF
  6939. case 0xC2:
  6940. case 0xC3:
  6941. case 0xC4:
  6942. case 0xC5:
  6943. case 0xC6:
  6944. case 0xC7:
  6945. case 0xC8:
  6946. case 0xC9:
  6947. case 0xCA:
  6948. case 0xCB:
  6949. case 0xCC:
  6950. case 0xCD:
  6951. case 0xCE:
  6952. case 0xCF:
  6953. case 0xD0:
  6954. case 0xD1:
  6955. case 0xD2:
  6956. case 0xD3:
  6957. case 0xD4:
  6958. case 0xD5:
  6959. case 0xD6:
  6960. case 0xD7:
  6961. case 0xD8:
  6962. case 0xD9:
  6963. case 0xDA:
  6964. case 0xDB:
  6965. case 0xDC:
  6966. case 0xDD:
  6967. case 0xDE:
  6968. case 0xDF:
  6969. {
  6970. if (JSON_HEDLEY_UNLIKELY(!next_byte_in_range({0x80, 0xBF})))
  6971. {
  6972. return token_type::parse_error;
  6973. }
  6974. break;
  6975. }
  6976. // U+0800..U+0FFF: bytes E0 A0..BF 80..BF
  6977. case 0xE0:
  6978. {
  6979. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0xA0, 0xBF, 0x80, 0xBF}))))
  6980. {
  6981. return token_type::parse_error;
  6982. }
  6983. break;
  6984. }
  6985. // U+1000..U+CFFF: bytes E1..EC 80..BF 80..BF
  6986. // U+E000..U+FFFF: bytes EE..EF 80..BF 80..BF
  6987. case 0xE1:
  6988. case 0xE2:
  6989. case 0xE3:
  6990. case 0xE4:
  6991. case 0xE5:
  6992. case 0xE6:
  6993. case 0xE7:
  6994. case 0xE8:
  6995. case 0xE9:
  6996. case 0xEA:
  6997. case 0xEB:
  6998. case 0xEC:
  6999. case 0xEE:
  7000. case 0xEF:
  7001. {
  7002. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0xBF, 0x80, 0xBF}))))
  7003. {
  7004. return token_type::parse_error;
  7005. }
  7006. break;
  7007. }
  7008. // U+D000..U+D7FF: bytes ED 80..9F 80..BF
  7009. case 0xED:
  7010. {
  7011. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0x9F, 0x80, 0xBF}))))
  7012. {
  7013. return token_type::parse_error;
  7014. }
  7015. break;
  7016. }
  7017. // U+10000..U+3FFFF F0 90..BF 80..BF 80..BF
  7018. case 0xF0:
  7019. {
  7020. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x90, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
  7021. {
  7022. return token_type::parse_error;
  7023. }
  7024. break;
  7025. }
  7026. // U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF
  7027. case 0xF1:
  7028. case 0xF2:
  7029. case 0xF3:
  7030. {
  7031. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
  7032. {
  7033. return token_type::parse_error;
  7034. }
  7035. break;
  7036. }
  7037. // U+100000..U+10FFFF F4 80..8F 80..BF 80..BF
  7038. case 0xF4:
  7039. {
  7040. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0x8F, 0x80, 0xBF, 0x80, 0xBF}))))
  7041. {
  7042. return token_type::parse_error;
  7043. }
  7044. break;
  7045. }
  7046. // remaining bytes (80..C1 and F5..FF) are ill-formed
  7047. default:
  7048. {
  7049. error_message = "invalid string: ill-formed UTF-8 byte";
  7050. return token_type::parse_error;
  7051. }
  7052. }
  7053. }
  7054. }
  7055. /*!
  7056. * @brief scan a comment
  7057. * @return whether comment could be scanned successfully
  7058. */
  7059. bool scan_comment()
  7060. {
  7061. switch (get())
  7062. {
  7063. // single-line comments skip input until a newline or EOF is read
  7064. case '/':
  7065. {
  7066. while (true)
  7067. {
  7068. switch (get())
  7069. {
  7070. case '\n':
  7071. case '\r':
  7072. case std::char_traits<char_type>::eof():
  7073. case '\0':
  7074. return true;
  7075. default:
  7076. break;
  7077. }
  7078. }
  7079. }
  7080. // multi-line comments skip input until */ is read
  7081. case '*':
  7082. {
  7083. while (true)
  7084. {
  7085. switch (get())
  7086. {
  7087. case std::char_traits<char_type>::eof():
  7088. case '\0':
  7089. {
  7090. error_message = "invalid comment; missing closing '*/'";
  7091. return false;
  7092. }
  7093. case '*':
  7094. {
  7095. switch (get())
  7096. {
  7097. case '/':
  7098. return true;
  7099. default:
  7100. {
  7101. unget();
  7102. continue;
  7103. }
  7104. }
  7105. }
  7106. default:
  7107. continue;
  7108. }
  7109. }
  7110. }
  7111. // unexpected character after reading '/'
  7112. default:
  7113. {
  7114. error_message = "invalid comment; expecting '/' or '*' after '/'";
  7115. return false;
  7116. }
  7117. }
  7118. }
  7119. JSON_HEDLEY_NON_NULL(2)
  7120. static void strtof(float& f, const char* str, char** endptr) noexcept
  7121. {
  7122. f = std::strtof(str, endptr);
  7123. }
  7124. JSON_HEDLEY_NON_NULL(2)
  7125. static void strtof(double& f, const char* str, char** endptr) noexcept
  7126. {
  7127. f = std::strtod(str, endptr);
  7128. }
  7129. JSON_HEDLEY_NON_NULL(2)
  7130. static void strtof(long double& f, const char* str, char** endptr) noexcept
  7131. {
  7132. f = std::strtold(str, endptr);
  7133. }
  7134. /*!
  7135. @brief scan a number literal
  7136. This function scans a string according to Sect. 6 of RFC 8259.
  7137. The function is realized with a deterministic finite state machine derived
  7138. from the grammar described in RFC 8259. Starting in state "init", the
  7139. input is read and used to determined the next state. Only state "done"
  7140. accepts the number. State "error" is a trap state to model errors. In the
  7141. table below, "anything" means any character but the ones listed before.
  7142. state | 0 | 1-9 | e E | + | - | . | anything
  7143. ---------|----------|----------|----------|---------|---------|----------|-----------
  7144. init | zero | any1 | [error] | [error] | minus | [error] | [error]
  7145. minus | zero | any1 | [error] | [error] | [error] | [error] | [error]
  7146. zero | done | done | exponent | done | done | decimal1 | done
  7147. any1 | any1 | any1 | exponent | done | done | decimal1 | done
  7148. decimal1 | decimal2 | decimal2 | [error] | [error] | [error] | [error] | [error]
  7149. decimal2 | decimal2 | decimal2 | exponent | done | done | done | done
  7150. exponent | any2 | any2 | [error] | sign | sign | [error] | [error]
  7151. sign | any2 | any2 | [error] | [error] | [error] | [error] | [error]
  7152. any2 | any2 | any2 | done | done | done | done | done
  7153. The state machine is realized with one label per state (prefixed with
  7154. "scan_number_") and `goto` statements between them. The state machine
  7155. contains cycles, but any cycle can be left when EOF is read. Therefore,
  7156. the function is guaranteed to terminate.
  7157. During scanning, the read bytes are stored in token_buffer. This string is
  7158. then converted to a signed integer, an unsigned integer, or a
  7159. floating-point number.
  7160. @return token_type::value_unsigned, token_type::value_integer, or
  7161. token_type::value_float if number could be successfully scanned,
  7162. token_type::parse_error otherwise
  7163. @note The scanner is independent of the current locale. Internally, the
  7164. locale's decimal point is used instead of `.` to work with the
  7165. locale-dependent converters.
  7166. */
  7167. token_type scan_number() // lgtm [cpp/use-of-goto]
  7168. {
  7169. // reset token_buffer to store the number's bytes
  7170. reset();
  7171. // the type of the parsed number; initially set to unsigned; will be
  7172. // changed if minus sign, decimal point or exponent is read
  7173. token_type number_type = token_type::value_unsigned;
  7174. // state (init): we just found out we need to scan a number
  7175. switch (current)
  7176. {
  7177. case '-':
  7178. {
  7179. add(current);
  7180. goto scan_number_minus;
  7181. }
  7182. case '0':
  7183. {
  7184. add(current);
  7185. goto scan_number_zero;
  7186. }
  7187. case '1':
  7188. case '2':
  7189. case '3':
  7190. case '4':
  7191. case '5':
  7192. case '6':
  7193. case '7':
  7194. case '8':
  7195. case '9':
  7196. {
  7197. add(current);
  7198. goto scan_number_any1;
  7199. }
  7200. // all other characters are rejected outside scan_number()
  7201. default: // LCOV_EXCL_LINE
  7202. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  7203. }
  7204. scan_number_minus:
  7205. // state: we just parsed a leading minus sign
  7206. number_type = token_type::value_integer;
  7207. switch (get())
  7208. {
  7209. case '0':
  7210. {
  7211. add(current);
  7212. goto scan_number_zero;
  7213. }
  7214. case '1':
  7215. case '2':
  7216. case '3':
  7217. case '4':
  7218. case '5':
  7219. case '6':
  7220. case '7':
  7221. case '8':
  7222. case '9':
  7223. {
  7224. add(current);
  7225. goto scan_number_any1;
  7226. }
  7227. default:
  7228. {
  7229. error_message = "invalid number; expected digit after '-'";
  7230. return token_type::parse_error;
  7231. }
  7232. }
  7233. scan_number_zero:
  7234. // state: we just parse a zero (maybe with a leading minus sign)
  7235. switch (get())
  7236. {
  7237. case '.':
  7238. {
  7239. add(decimal_point_char);
  7240. goto scan_number_decimal1;
  7241. }
  7242. case 'e':
  7243. case 'E':
  7244. {
  7245. add(current);
  7246. goto scan_number_exponent;
  7247. }
  7248. default:
  7249. goto scan_number_done;
  7250. }
  7251. scan_number_any1:
  7252. // state: we just parsed a number 0-9 (maybe with a leading minus sign)
  7253. switch (get())
  7254. {
  7255. case '0':
  7256. case '1':
  7257. case '2':
  7258. case '3':
  7259. case '4':
  7260. case '5':
  7261. case '6':
  7262. case '7':
  7263. case '8':
  7264. case '9':
  7265. {
  7266. add(current);
  7267. goto scan_number_any1;
  7268. }
  7269. case '.':
  7270. {
  7271. add(decimal_point_char);
  7272. goto scan_number_decimal1;
  7273. }
  7274. case 'e':
  7275. case 'E':
  7276. {
  7277. add(current);
  7278. goto scan_number_exponent;
  7279. }
  7280. default:
  7281. goto scan_number_done;
  7282. }
  7283. scan_number_decimal1:
  7284. // state: we just parsed a decimal point
  7285. number_type = token_type::value_float;
  7286. switch (get())
  7287. {
  7288. case '0':
  7289. case '1':
  7290. case '2':
  7291. case '3':
  7292. case '4':
  7293. case '5':
  7294. case '6':
  7295. case '7':
  7296. case '8':
  7297. case '9':
  7298. {
  7299. add(current);
  7300. goto scan_number_decimal2;
  7301. }
  7302. default:
  7303. {
  7304. error_message = "invalid number; expected digit after '.'";
  7305. return token_type::parse_error;
  7306. }
  7307. }
  7308. scan_number_decimal2:
  7309. // we just parsed at least one number after a decimal point
  7310. switch (get())
  7311. {
  7312. case '0':
  7313. case '1':
  7314. case '2':
  7315. case '3':
  7316. case '4':
  7317. case '5':
  7318. case '6':
  7319. case '7':
  7320. case '8':
  7321. case '9':
  7322. {
  7323. add(current);
  7324. goto scan_number_decimal2;
  7325. }
  7326. case 'e':
  7327. case 'E':
  7328. {
  7329. add(current);
  7330. goto scan_number_exponent;
  7331. }
  7332. default:
  7333. goto scan_number_done;
  7334. }
  7335. scan_number_exponent:
  7336. // we just parsed an exponent
  7337. number_type = token_type::value_float;
  7338. switch (get())
  7339. {
  7340. case '+':
  7341. case '-':
  7342. {
  7343. add(current);
  7344. goto scan_number_sign;
  7345. }
  7346. case '0':
  7347. case '1':
  7348. case '2':
  7349. case '3':
  7350. case '4':
  7351. case '5':
  7352. case '6':
  7353. case '7':
  7354. case '8':
  7355. case '9':
  7356. {
  7357. add(current);
  7358. goto scan_number_any2;
  7359. }
  7360. default:
  7361. {
  7362. error_message =
  7363. "invalid number; expected '+', '-', or digit after exponent";
  7364. return token_type::parse_error;
  7365. }
  7366. }
  7367. scan_number_sign:
  7368. // we just parsed an exponent sign
  7369. switch (get())
  7370. {
  7371. case '0':
  7372. case '1':
  7373. case '2':
  7374. case '3':
  7375. case '4':
  7376. case '5':
  7377. case '6':
  7378. case '7':
  7379. case '8':
  7380. case '9':
  7381. {
  7382. add(current);
  7383. goto scan_number_any2;
  7384. }
  7385. default:
  7386. {
  7387. error_message = "invalid number; expected digit after exponent sign";
  7388. return token_type::parse_error;
  7389. }
  7390. }
  7391. scan_number_any2:
  7392. // we just parsed a number after the exponent or exponent sign
  7393. switch (get())
  7394. {
  7395. case '0':
  7396. case '1':
  7397. case '2':
  7398. case '3':
  7399. case '4':
  7400. case '5':
  7401. case '6':
  7402. case '7':
  7403. case '8':
  7404. case '9':
  7405. {
  7406. add(current);
  7407. goto scan_number_any2;
  7408. }
  7409. default:
  7410. goto scan_number_done;
  7411. }
  7412. scan_number_done:
  7413. // unget the character after the number (we only read it to know that
  7414. // we are done scanning a number)
  7415. unget();
  7416. char* endptr = nullptr; // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  7417. errno = 0;
  7418. // try to parse integers first and fall back to floats
  7419. if (number_type == token_type::value_unsigned)
  7420. {
  7421. const auto x = std::strtoull(token_buffer.data(), &endptr, 10);
  7422. // we checked the number format before
  7423. JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
  7424. if (errno == 0)
  7425. {
  7426. value_unsigned = static_cast<number_unsigned_t>(x);
  7427. if (value_unsigned == x)
  7428. {
  7429. return token_type::value_unsigned;
  7430. }
  7431. }
  7432. }
  7433. else if (number_type == token_type::value_integer)
  7434. {
  7435. const auto x = std::strtoll(token_buffer.data(), &endptr, 10);
  7436. // we checked the number format before
  7437. JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
  7438. if (errno == 0)
  7439. {
  7440. value_integer = static_cast<number_integer_t>(x);
  7441. if (value_integer == x)
  7442. {
  7443. return token_type::value_integer;
  7444. }
  7445. }
  7446. }
  7447. // this code is reached if we parse a floating-point number or if an
  7448. // integer conversion above failed
  7449. strtof(value_float, token_buffer.data(), &endptr);
  7450. // we checked the number format before
  7451. JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
  7452. return token_type::value_float;
  7453. }
  7454. /*!
  7455. @param[in] literal_text the literal text to expect
  7456. @param[in] length the length of the passed literal text
  7457. @param[in] return_type the token type to return on success
  7458. */
  7459. JSON_HEDLEY_NON_NULL(2)
  7460. token_type scan_literal(const char_type* literal_text, const std::size_t length,
  7461. token_type return_type)
  7462. {
  7463. JSON_ASSERT(std::char_traits<char_type>::to_char_type(current) == literal_text[0]);
  7464. for (std::size_t i = 1; i < length; ++i)
  7465. {
  7466. if (JSON_HEDLEY_UNLIKELY(std::char_traits<char_type>::to_char_type(get()) != literal_text[i]))
  7467. {
  7468. error_message = "invalid literal";
  7469. return token_type::parse_error;
  7470. }
  7471. }
  7472. return return_type;
  7473. }
  7474. /////////////////////
  7475. // input management
  7476. /////////////////////
  7477. /// reset token_buffer; current character is beginning of token
  7478. void reset() noexcept
  7479. {
  7480. token_buffer.clear();
  7481. token_string.clear();
  7482. token_string.push_back(std::char_traits<char_type>::to_char_type(current));
  7483. }
  7484. /*
  7485. @brief get next character from the input
  7486. This function provides the interface to the used input adapter. It does
  7487. not throw in case the input reached EOF, but returns a
  7488. `std::char_traits<char>::eof()` in that case. Stores the scanned characters
  7489. for use in error messages.
  7490. @return character read from the input
  7491. */
  7492. char_int_type get()
  7493. {
  7494. ++position.chars_read_total;
  7495. ++position.chars_read_current_line;
  7496. if (next_unget)
  7497. {
  7498. // just reset the next_unget variable and work with current
  7499. next_unget = false;
  7500. }
  7501. else
  7502. {
  7503. current = ia.get_character();
  7504. }
  7505. if (JSON_HEDLEY_LIKELY(current != std::char_traits<char_type>::eof()))
  7506. {
  7507. token_string.push_back(std::char_traits<char_type>::to_char_type(current));
  7508. }
  7509. if (current == '\n')
  7510. {
  7511. ++position.lines_read;
  7512. position.chars_read_current_line = 0;
  7513. }
  7514. return current;
  7515. }
  7516. /*!
  7517. @brief unget current character (read it again on next get)
  7518. We implement unget by setting variable next_unget to true. The input is not
  7519. changed - we just simulate ungetting by modifying chars_read_total,
  7520. chars_read_current_line, and token_string. The next call to get() will
  7521. behave as if the unget character is read again.
  7522. */
  7523. void unget()
  7524. {
  7525. next_unget = true;
  7526. --position.chars_read_total;
  7527. // in case we "unget" a newline, we have to also decrement the lines_read
  7528. if (position.chars_read_current_line == 0)
  7529. {
  7530. if (position.lines_read > 0)
  7531. {
  7532. --position.lines_read;
  7533. }
  7534. }
  7535. else
  7536. {
  7537. --position.chars_read_current_line;
  7538. }
  7539. if (JSON_HEDLEY_LIKELY(current != std::char_traits<char_type>::eof()))
  7540. {
  7541. JSON_ASSERT(!token_string.empty());
  7542. token_string.pop_back();
  7543. }
  7544. }
  7545. /// add a character to token_buffer
  7546. void add(char_int_type c)
  7547. {
  7548. token_buffer.push_back(static_cast<typename string_t::value_type>(c));
  7549. }
  7550. public:
  7551. /////////////////////
  7552. // value getters
  7553. /////////////////////
  7554. /// return integer value
  7555. constexpr number_integer_t get_number_integer() const noexcept
  7556. {
  7557. return value_integer;
  7558. }
  7559. /// return unsigned integer value
  7560. constexpr number_unsigned_t get_number_unsigned() const noexcept
  7561. {
  7562. return value_unsigned;
  7563. }
  7564. /// return floating-point value
  7565. constexpr number_float_t get_number_float() const noexcept
  7566. {
  7567. return value_float;
  7568. }
  7569. /// return current string value (implicitly resets the token; useful only once)
  7570. string_t& get_string()
  7571. {
  7572. return token_buffer;
  7573. }
  7574. /////////////////////
  7575. // diagnostics
  7576. /////////////////////
  7577. /// return position of last read token
  7578. constexpr position_t get_position() const noexcept
  7579. {
  7580. return position;
  7581. }
  7582. /// return the last read token (for errors only). Will never contain EOF
  7583. /// (an arbitrary value that is not a valid char value, often -1), because
  7584. /// 255 may legitimately occur. May contain NUL, which should be escaped.
  7585. std::string get_token_string() const
  7586. {
  7587. // escape control characters
  7588. std::string result;
  7589. for (const auto c : token_string)
  7590. {
  7591. if (static_cast<unsigned char>(c) <= '\x1F')
  7592. {
  7593. // escape control characters
  7594. std::array<char, 9> cs{{}};
  7595. static_cast<void>((std::snprintf)(cs.data(), cs.size(), "<U+%.4X>", static_cast<unsigned char>(c))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  7596. result += cs.data();
  7597. }
  7598. else
  7599. {
  7600. // add character as is
  7601. result.push_back(static_cast<std::string::value_type>(c));
  7602. }
  7603. }
  7604. return result;
  7605. }
  7606. /// return syntax error message
  7607. JSON_HEDLEY_RETURNS_NON_NULL
  7608. constexpr const char* get_error_message() const noexcept
  7609. {
  7610. return error_message;
  7611. }
  7612. /////////////////////
  7613. // actual scanner
  7614. /////////////////////
  7615. /*!
  7616. @brief skip the UTF-8 byte order mark
  7617. @return true iff there is no BOM or the correct BOM has been skipped
  7618. */
  7619. bool skip_bom()
  7620. {
  7621. if (get() == 0xEF)
  7622. {
  7623. // check if we completely parse the BOM
  7624. return get() == 0xBB && get() == 0xBF;
  7625. }
  7626. // the first character is not the beginning of the BOM; unget it to
  7627. // process is later
  7628. unget();
  7629. return true;
  7630. }
  7631. void skip_whitespace()
  7632. {
  7633. do
  7634. {
  7635. get();
  7636. }
  7637. while (current == ' ' || current == '\t' || current == '\n' || current == '\r');
  7638. }
  7639. token_type scan()
  7640. {
  7641. // initially, skip the BOM
  7642. if (position.chars_read_total == 0 && !skip_bom())
  7643. {
  7644. error_message = "invalid BOM; must be 0xEF 0xBB 0xBF if given";
  7645. return token_type::parse_error;
  7646. }
  7647. // read next character and ignore whitespace
  7648. skip_whitespace();
  7649. // ignore comments
  7650. while (ignore_comments && current == '/')
  7651. {
  7652. if (!scan_comment())
  7653. {
  7654. return token_type::parse_error;
  7655. }
  7656. // skip following whitespace
  7657. skip_whitespace();
  7658. }
  7659. switch (current)
  7660. {
  7661. // structural characters
  7662. case '[':
  7663. return token_type::begin_array;
  7664. case ']':
  7665. return token_type::end_array;
  7666. case '{':
  7667. return token_type::begin_object;
  7668. case '}':
  7669. return token_type::end_object;
  7670. case ':':
  7671. return token_type::name_separator;
  7672. case ',':
  7673. return token_type::value_separator;
  7674. // literals
  7675. case 't':
  7676. {
  7677. 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')}};
  7678. return scan_literal(true_literal.data(), true_literal.size(), token_type::literal_true);
  7679. }
  7680. case 'f':
  7681. {
  7682. 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')}};
  7683. return scan_literal(false_literal.data(), false_literal.size(), token_type::literal_false);
  7684. }
  7685. case 'n':
  7686. {
  7687. 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')}};
  7688. return scan_literal(null_literal.data(), null_literal.size(), token_type::literal_null);
  7689. }
  7690. // string
  7691. case '\"':
  7692. return scan_string();
  7693. // number
  7694. case '-':
  7695. case '0':
  7696. case '1':
  7697. case '2':
  7698. case '3':
  7699. case '4':
  7700. case '5':
  7701. case '6':
  7702. case '7':
  7703. case '8':
  7704. case '9':
  7705. return scan_number();
  7706. // end of input (the null byte is needed when parsing from
  7707. // string literals)
  7708. case '\0':
  7709. case std::char_traits<char_type>::eof():
  7710. return token_type::end_of_input;
  7711. // error
  7712. default:
  7713. error_message = "invalid literal";
  7714. return token_type::parse_error;
  7715. }
  7716. }
  7717. private:
  7718. /// input adapter
  7719. InputAdapterType ia;
  7720. /// whether comments should be ignored (true) or signaled as errors (false)
  7721. const bool ignore_comments = false;
  7722. /// the current character
  7723. char_int_type current = std::char_traits<char_type>::eof();
  7724. /// whether the next get() call should just return current
  7725. bool next_unget = false;
  7726. /// the start position of the current token
  7727. position_t position {};
  7728. /// raw input token string (for error messages)
  7729. std::vector<char_type> token_string {};
  7730. /// buffer for variable-length tokens (numbers, strings)
  7731. string_t token_buffer {};
  7732. /// a description of occurred lexer errors
  7733. const char* error_message = "";
  7734. // number values
  7735. number_integer_t value_integer = 0;
  7736. number_unsigned_t value_unsigned = 0;
  7737. number_float_t value_float = 0;
  7738. /// the decimal point
  7739. const char_int_type decimal_point_char = '.';
  7740. };
  7741. } // namespace detail
  7742. NLOHMANN_JSON_NAMESPACE_END
  7743. // #include <nlohmann/detail/macro_scope.hpp>
  7744. // #include <nlohmann/detail/meta/is_sax.hpp>
  7745. // __ _____ _____ _____
  7746. // __| | __| | | | JSON for Modern C++
  7747. // | | |__ | | | | | | version 3.11.2
  7748. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  7749. //
  7750. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  7751. // SPDX-License-Identifier: MIT
  7752. #include <cstdint> // size_t
  7753. #include <utility> // declval
  7754. #include <string> // string
  7755. // #include <nlohmann/detail/abi_macros.hpp>
  7756. // #include <nlohmann/detail/meta/detected.hpp>
  7757. // #include <nlohmann/detail/meta/type_traits.hpp>
  7758. NLOHMANN_JSON_NAMESPACE_BEGIN
  7759. namespace detail
  7760. {
  7761. template<typename T>
  7762. using null_function_t = decltype(std::declval<T&>().null());
  7763. template<typename T>
  7764. using boolean_function_t =
  7765. decltype(std::declval<T&>().boolean(std::declval<bool>()));
  7766. template<typename T, typename Integer>
  7767. using number_integer_function_t =
  7768. decltype(std::declval<T&>().number_integer(std::declval<Integer>()));
  7769. template<typename T, typename Unsigned>
  7770. using number_unsigned_function_t =
  7771. decltype(std::declval<T&>().number_unsigned(std::declval<Unsigned>()));
  7772. template<typename T, typename Float, typename String>
  7773. using number_float_function_t = decltype(std::declval<T&>().number_float(
  7774. std::declval<Float>(), std::declval<const String&>()));
  7775. template<typename T, typename String>
  7776. using string_function_t =
  7777. decltype(std::declval<T&>().string(std::declval<String&>()));
  7778. template<typename T, typename Binary>
  7779. using binary_function_t =
  7780. decltype(std::declval<T&>().binary(std::declval<Binary&>()));
  7781. template<typename T>
  7782. using start_object_function_t =
  7783. decltype(std::declval<T&>().start_object(std::declval<std::size_t>()));
  7784. template<typename T, typename String>
  7785. using key_function_t =
  7786. decltype(std::declval<T&>().key(std::declval<String&>()));
  7787. template<typename T>
  7788. using end_object_function_t = decltype(std::declval<T&>().end_object());
  7789. template<typename T>
  7790. using start_array_function_t =
  7791. decltype(std::declval<T&>().start_array(std::declval<std::size_t>()));
  7792. template<typename T>
  7793. using end_array_function_t = decltype(std::declval<T&>().end_array());
  7794. template<typename T, typename Exception>
  7795. using parse_error_function_t = decltype(std::declval<T&>().parse_error(
  7796. std::declval<std::size_t>(), std::declval<const std::string&>(),
  7797. std::declval<const Exception&>()));
  7798. template<typename SAX, typename BasicJsonType>
  7799. struct is_sax
  7800. {
  7801. private:
  7802. static_assert(is_basic_json<BasicJsonType>::value,
  7803. "BasicJsonType must be of type basic_json<...>");
  7804. using number_integer_t = typename BasicJsonType::number_integer_t;
  7805. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  7806. using number_float_t = typename BasicJsonType::number_float_t;
  7807. using string_t = typename BasicJsonType::string_t;
  7808. using binary_t = typename BasicJsonType::binary_t;
  7809. using exception_t = typename BasicJsonType::exception;
  7810. public:
  7811. static constexpr bool value =
  7812. is_detected_exact<bool, null_function_t, SAX>::value &&
  7813. is_detected_exact<bool, boolean_function_t, SAX>::value &&
  7814. is_detected_exact<bool, number_integer_function_t, SAX, number_integer_t>::value &&
  7815. is_detected_exact<bool, number_unsigned_function_t, SAX, number_unsigned_t>::value &&
  7816. is_detected_exact<bool, number_float_function_t, SAX, number_float_t, string_t>::value &&
  7817. is_detected_exact<bool, string_function_t, SAX, string_t>::value &&
  7818. is_detected_exact<bool, binary_function_t, SAX, binary_t>::value &&
  7819. is_detected_exact<bool, start_object_function_t, SAX>::value &&
  7820. is_detected_exact<bool, key_function_t, SAX, string_t>::value &&
  7821. is_detected_exact<bool, end_object_function_t, SAX>::value &&
  7822. is_detected_exact<bool, start_array_function_t, SAX>::value &&
  7823. is_detected_exact<bool, end_array_function_t, SAX>::value &&
  7824. is_detected_exact<bool, parse_error_function_t, SAX, exception_t>::value;
  7825. };
  7826. template<typename SAX, typename BasicJsonType>
  7827. struct is_sax_static_asserts
  7828. {
  7829. private:
  7830. static_assert(is_basic_json<BasicJsonType>::value,
  7831. "BasicJsonType must be of type basic_json<...>");
  7832. using number_integer_t = typename BasicJsonType::number_integer_t;
  7833. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  7834. using number_float_t = typename BasicJsonType::number_float_t;
  7835. using string_t = typename BasicJsonType::string_t;
  7836. using binary_t = typename BasicJsonType::binary_t;
  7837. using exception_t = typename BasicJsonType::exception;
  7838. public:
  7839. static_assert(is_detected_exact<bool, null_function_t, SAX>::value,
  7840. "Missing/invalid function: bool null()");
  7841. static_assert(is_detected_exact<bool, boolean_function_t, SAX>::value,
  7842. "Missing/invalid function: bool boolean(bool)");
  7843. static_assert(is_detected_exact<bool, boolean_function_t, SAX>::value,
  7844. "Missing/invalid function: bool boolean(bool)");
  7845. static_assert(
  7846. is_detected_exact<bool, number_integer_function_t, SAX,
  7847. number_integer_t>::value,
  7848. "Missing/invalid function: bool number_integer(number_integer_t)");
  7849. static_assert(
  7850. is_detected_exact<bool, number_unsigned_function_t, SAX,
  7851. number_unsigned_t>::value,
  7852. "Missing/invalid function: bool number_unsigned(number_unsigned_t)");
  7853. static_assert(is_detected_exact<bool, number_float_function_t, SAX,
  7854. number_float_t, string_t>::value,
  7855. "Missing/invalid function: bool number_float(number_float_t, const string_t&)");
  7856. static_assert(
  7857. is_detected_exact<bool, string_function_t, SAX, string_t>::value,
  7858. "Missing/invalid function: bool string(string_t&)");
  7859. static_assert(
  7860. is_detected_exact<bool, binary_function_t, SAX, binary_t>::value,
  7861. "Missing/invalid function: bool binary(binary_t&)");
  7862. static_assert(is_detected_exact<bool, start_object_function_t, SAX>::value,
  7863. "Missing/invalid function: bool start_object(std::size_t)");
  7864. static_assert(is_detected_exact<bool, key_function_t, SAX, string_t>::value,
  7865. "Missing/invalid function: bool key(string_t&)");
  7866. static_assert(is_detected_exact<bool, end_object_function_t, SAX>::value,
  7867. "Missing/invalid function: bool end_object()");
  7868. static_assert(is_detected_exact<bool, start_array_function_t, SAX>::value,
  7869. "Missing/invalid function: bool start_array(std::size_t)");
  7870. static_assert(is_detected_exact<bool, end_array_function_t, SAX>::value,
  7871. "Missing/invalid function: bool end_array()");
  7872. static_assert(
  7873. is_detected_exact<bool, parse_error_function_t, SAX, exception_t>::value,
  7874. "Missing/invalid function: bool parse_error(std::size_t, const "
  7875. "std::string&, const exception&)");
  7876. };
  7877. } // namespace detail
  7878. NLOHMANN_JSON_NAMESPACE_END
  7879. // #include <nlohmann/detail/meta/type_traits.hpp>
  7880. // #include <nlohmann/detail/string_concat.hpp>
  7881. // #include <nlohmann/detail/value_t.hpp>
  7882. NLOHMANN_JSON_NAMESPACE_BEGIN
  7883. namespace detail
  7884. {
  7885. /// how to treat CBOR tags
  7886. enum class cbor_tag_handler_t
  7887. {
  7888. error, ///< throw a parse_error exception in case of a tag
  7889. ignore, ///< ignore tags
  7890. store ///< store tags as binary type
  7891. };
  7892. /*!
  7893. @brief determine system byte order
  7894. @return true if and only if system's byte order is little endian
  7895. @note from https://stackoverflow.com/a/1001328/266378
  7896. */
  7897. static inline bool little_endianness(int num = 1) noexcept
  7898. {
  7899. return *reinterpret_cast<char*>(&num) == 1;
  7900. }
  7901. ///////////////////
  7902. // binary reader //
  7903. ///////////////////
  7904. /*!
  7905. @brief deserialization of CBOR, MessagePack, and UBJSON values
  7906. */
  7907. template<typename BasicJsonType, typename InputAdapterType, typename SAX = json_sax_dom_parser<BasicJsonType>>
  7908. class binary_reader
  7909. {
  7910. using number_integer_t = typename BasicJsonType::number_integer_t;
  7911. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  7912. using number_float_t = typename BasicJsonType::number_float_t;
  7913. using string_t = typename BasicJsonType::string_t;
  7914. using binary_t = typename BasicJsonType::binary_t;
  7915. using json_sax_t = SAX;
  7916. using char_type = typename InputAdapterType::char_type;
  7917. using char_int_type = typename std::char_traits<char_type>::int_type;
  7918. public:
  7919. /*!
  7920. @brief create a binary reader
  7921. @param[in] adapter input adapter to read from
  7922. */
  7923. explicit binary_reader(InputAdapterType&& adapter, const input_format_t format = input_format_t::json) noexcept : ia(std::move(adapter)), input_format(format)
  7924. {
  7925. (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
  7926. }
  7927. // make class move-only
  7928. binary_reader(const binary_reader&) = delete;
  7929. binary_reader(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  7930. binary_reader& operator=(const binary_reader&) = delete;
  7931. binary_reader& operator=(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  7932. ~binary_reader() = default;
  7933. /*!
  7934. @param[in] format the binary format to parse
  7935. @param[in] sax_ a SAX event processor
  7936. @param[in] strict whether to expect the input to be consumed completed
  7937. @param[in] tag_handler how to treat CBOR tags
  7938. @return whether parsing was successful
  7939. */
  7940. JSON_HEDLEY_NON_NULL(3)
  7941. bool sax_parse(const input_format_t format,
  7942. json_sax_t* sax_,
  7943. const bool strict = true,
  7944. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  7945. {
  7946. sax = sax_;
  7947. bool result = false;
  7948. switch (format)
  7949. {
  7950. case input_format_t::bson:
  7951. result = parse_bson_internal();
  7952. break;
  7953. case input_format_t::cbor:
  7954. result = parse_cbor_internal(true, tag_handler);
  7955. break;
  7956. case input_format_t::msgpack:
  7957. result = parse_msgpack_internal();
  7958. break;
  7959. case input_format_t::ubjson:
  7960. case input_format_t::bjdata:
  7961. result = parse_ubjson_internal();
  7962. break;
  7963. case input_format_t::json: // LCOV_EXCL_LINE
  7964. default: // LCOV_EXCL_LINE
  7965. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  7966. }
  7967. // strict mode: next byte must be EOF
  7968. if (result && strict)
  7969. {
  7970. if (input_format == input_format_t::ubjson || input_format == input_format_t::bjdata)
  7971. {
  7972. get_ignore_noop();
  7973. }
  7974. else
  7975. {
  7976. get();
  7977. }
  7978. if (JSON_HEDLEY_UNLIKELY(current != std::char_traits<char_type>::eof()))
  7979. {
  7980. return sax->parse_error(chars_read, get_token_string(), parse_error::create(110, chars_read,
  7981. exception_message(input_format, concat("expected end of input; last byte: 0x", get_token_string()), "value"), nullptr));
  7982. }
  7983. }
  7984. return result;
  7985. }
  7986. private:
  7987. //////////
  7988. // BSON //
  7989. //////////
  7990. /*!
  7991. @brief Reads in a BSON-object and passes it to the SAX-parser.
  7992. @return whether a valid BSON-value was passed to the SAX parser
  7993. */
  7994. bool parse_bson_internal()
  7995. {
  7996. std::int32_t document_size{};
  7997. get_number<std::int32_t, true>(input_format_t::bson, document_size);
  7998. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast<std::size_t>(-1))))
  7999. {
  8000. return false;
  8001. }
  8002. if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/false)))
  8003. {
  8004. return false;
  8005. }
  8006. return sax->end_object();
  8007. }
  8008. /*!
  8009. @brief Parses a C-style string from the BSON input.
  8010. @param[in,out] result A reference to the string variable where the read
  8011. string is to be stored.
  8012. @return `true` if the \x00-byte indicating the end of the string was
  8013. encountered before the EOF; false` indicates an unexpected EOF.
  8014. */
  8015. bool get_bson_cstr(string_t& result)
  8016. {
  8017. auto out = std::back_inserter(result);
  8018. while (true)
  8019. {
  8020. get();
  8021. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "cstring")))
  8022. {
  8023. return false;
  8024. }
  8025. if (current == 0x00)
  8026. {
  8027. return true;
  8028. }
  8029. *out++ = static_cast<typename string_t::value_type>(current);
  8030. }
  8031. }
  8032. /*!
  8033. @brief Parses a zero-terminated string of length @a len from the BSON
  8034. input.
  8035. @param[in] len The length (including the zero-byte at the end) of the
  8036. string to be read.
  8037. @param[in,out] result A reference to the string variable where the read
  8038. string is to be stored.
  8039. @tparam NumberType The type of the length @a len
  8040. @pre len >= 1
  8041. @return `true` if the string was successfully parsed
  8042. */
  8043. template<typename NumberType>
  8044. bool get_bson_string(const NumberType len, string_t& result)
  8045. {
  8046. if (JSON_HEDLEY_UNLIKELY(len < 1))
  8047. {
  8048. auto last_token = get_token_string();
  8049. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  8050. exception_message(input_format_t::bson, concat("string length must be at least 1, is ", std::to_string(len)), "string"), nullptr));
  8051. }
  8052. return get_string(input_format_t::bson, len - static_cast<NumberType>(1), result) && get() != std::char_traits<char_type>::eof();
  8053. }
  8054. /*!
  8055. @brief Parses a byte array input of length @a len from the BSON input.
  8056. @param[in] len The length of the byte array to be read.
  8057. @param[in,out] result A reference to the binary variable where the read
  8058. array is to be stored.
  8059. @tparam NumberType The type of the length @a len
  8060. @pre len >= 0
  8061. @return `true` if the byte array was successfully parsed
  8062. */
  8063. template<typename NumberType>
  8064. bool get_bson_binary(const NumberType len, binary_t& result)
  8065. {
  8066. if (JSON_HEDLEY_UNLIKELY(len < 0))
  8067. {
  8068. auto last_token = get_token_string();
  8069. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  8070. exception_message(input_format_t::bson, concat("byte array length cannot be negative, is ", std::to_string(len)), "binary"), nullptr));
  8071. }
  8072. // All BSON binary values have a subtype
  8073. std::uint8_t subtype{};
  8074. get_number<std::uint8_t>(input_format_t::bson, subtype);
  8075. result.set_subtype(subtype);
  8076. return get_binary(input_format_t::bson, len, result);
  8077. }
  8078. /*!
  8079. @brief Read a BSON document element of the given @a element_type.
  8080. @param[in] element_type The BSON element type, c.f. http://bsonspec.org/spec.html
  8081. @param[in] element_type_parse_position The position in the input stream,
  8082. where the `element_type` was read.
  8083. @warning Not all BSON element types are supported yet. An unsupported
  8084. @a element_type will give rise to a parse_error.114:
  8085. Unsupported BSON record type 0x...
  8086. @return whether a valid BSON-object/array was passed to the SAX parser
  8087. */
  8088. bool parse_bson_element_internal(const char_int_type element_type,
  8089. const std::size_t element_type_parse_position)
  8090. {
  8091. switch (element_type)
  8092. {
  8093. case 0x01: // double
  8094. {
  8095. double number{};
  8096. return get_number<double, true>(input_format_t::bson, number) && sax->number_float(static_cast<number_float_t>(number), "");
  8097. }
  8098. case 0x02: // string
  8099. {
  8100. std::int32_t len{};
  8101. string_t value;
  8102. return get_number<std::int32_t, true>(input_format_t::bson, len) && get_bson_string(len, value) && sax->string(value);
  8103. }
  8104. case 0x03: // object
  8105. {
  8106. return parse_bson_internal();
  8107. }
  8108. case 0x04: // array
  8109. {
  8110. return parse_bson_array();
  8111. }
  8112. case 0x05: // binary
  8113. {
  8114. std::int32_t len{};
  8115. binary_t value;
  8116. return get_number<std::int32_t, true>(input_format_t::bson, len) && get_bson_binary(len, value) && sax->binary(value);
  8117. }
  8118. case 0x08: // boolean
  8119. {
  8120. return sax->boolean(get() != 0);
  8121. }
  8122. case 0x0A: // null
  8123. {
  8124. return sax->null();
  8125. }
  8126. case 0x10: // int32
  8127. {
  8128. std::int32_t value{};
  8129. return get_number<std::int32_t, true>(input_format_t::bson, value) && sax->number_integer(value);
  8130. }
  8131. case 0x12: // int64
  8132. {
  8133. std::int64_t value{};
  8134. return get_number<std::int64_t, true>(input_format_t::bson, value) && sax->number_integer(value);
  8135. }
  8136. default: // anything else not supported (yet)
  8137. {
  8138. std::array<char, 3> cr{{}};
  8139. static_cast<void>((std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(element_type))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  8140. std::string cr_str{cr.data()};
  8141. return sax->parse_error(element_type_parse_position, cr_str,
  8142. parse_error::create(114, element_type_parse_position, concat("Unsupported BSON record type 0x", cr_str), nullptr));
  8143. }
  8144. }
  8145. }
  8146. /*!
  8147. @brief Read a BSON element list (as specified in the BSON-spec)
  8148. The same binary layout is used for objects and arrays, hence it must be
  8149. indicated with the argument @a is_array which one is expected
  8150. (true --> array, false --> object).
  8151. @param[in] is_array Determines if the element list being read is to be
  8152. treated as an object (@a is_array == false), or as an
  8153. array (@a is_array == true).
  8154. @return whether a valid BSON-object/array was passed to the SAX parser
  8155. */
  8156. bool parse_bson_element_list(const bool is_array)
  8157. {
  8158. string_t key;
  8159. while (auto element_type = get())
  8160. {
  8161. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "element list")))
  8162. {
  8163. return false;
  8164. }
  8165. const std::size_t element_type_parse_position = chars_read;
  8166. if (JSON_HEDLEY_UNLIKELY(!get_bson_cstr(key)))
  8167. {
  8168. return false;
  8169. }
  8170. if (!is_array && !sax->key(key))
  8171. {
  8172. return false;
  8173. }
  8174. if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_internal(element_type, element_type_parse_position)))
  8175. {
  8176. return false;
  8177. }
  8178. // get_bson_cstr only appends
  8179. key.clear();
  8180. }
  8181. return true;
  8182. }
  8183. /*!
  8184. @brief Reads an array from the BSON input and passes it to the SAX-parser.
  8185. @return whether a valid BSON-array was passed to the SAX parser
  8186. */
  8187. bool parse_bson_array()
  8188. {
  8189. std::int32_t document_size{};
  8190. get_number<std::int32_t, true>(input_format_t::bson, document_size);
  8191. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
  8192. {
  8193. return false;
  8194. }
  8195. if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/true)))
  8196. {
  8197. return false;
  8198. }
  8199. return sax->end_array();
  8200. }
  8201. //////////
  8202. // CBOR //
  8203. //////////
  8204. /*!
  8205. @param[in] get_char whether a new character should be retrieved from the
  8206. input (true) or whether the last read character should
  8207. be considered instead (false)
  8208. @param[in] tag_handler how CBOR tags should be treated
  8209. @return whether a valid CBOR value was passed to the SAX parser
  8210. */
  8211. bool parse_cbor_internal(const bool get_char,
  8212. const cbor_tag_handler_t tag_handler)
  8213. {
  8214. switch (get_char ? get() : current)
  8215. {
  8216. // EOF
  8217. case std::char_traits<char_type>::eof():
  8218. return unexpect_eof(input_format_t::cbor, "value");
  8219. // Integer 0x00..0x17 (0..23)
  8220. case 0x00:
  8221. case 0x01:
  8222. case 0x02:
  8223. case 0x03:
  8224. case 0x04:
  8225. case 0x05:
  8226. case 0x06:
  8227. case 0x07:
  8228. case 0x08:
  8229. case 0x09:
  8230. case 0x0A:
  8231. case 0x0B:
  8232. case 0x0C:
  8233. case 0x0D:
  8234. case 0x0E:
  8235. case 0x0F:
  8236. case 0x10:
  8237. case 0x11:
  8238. case 0x12:
  8239. case 0x13:
  8240. case 0x14:
  8241. case 0x15:
  8242. case 0x16:
  8243. case 0x17:
  8244. return sax->number_unsigned(static_cast<number_unsigned_t>(current));
  8245. case 0x18: // Unsigned integer (one-byte uint8_t follows)
  8246. {
  8247. std::uint8_t number{};
  8248. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  8249. }
  8250. case 0x19: // Unsigned integer (two-byte uint16_t follows)
  8251. {
  8252. std::uint16_t number{};
  8253. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  8254. }
  8255. case 0x1A: // Unsigned integer (four-byte uint32_t follows)
  8256. {
  8257. std::uint32_t number{};
  8258. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  8259. }
  8260. case 0x1B: // Unsigned integer (eight-byte uint64_t follows)
  8261. {
  8262. std::uint64_t number{};
  8263. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  8264. }
  8265. // Negative integer -1-0x00..-1-0x17 (-1..-24)
  8266. case 0x20:
  8267. case 0x21:
  8268. case 0x22:
  8269. case 0x23:
  8270. case 0x24:
  8271. case 0x25:
  8272. case 0x26:
  8273. case 0x27:
  8274. case 0x28:
  8275. case 0x29:
  8276. case 0x2A:
  8277. case 0x2B:
  8278. case 0x2C:
  8279. case 0x2D:
  8280. case 0x2E:
  8281. case 0x2F:
  8282. case 0x30:
  8283. case 0x31:
  8284. case 0x32:
  8285. case 0x33:
  8286. case 0x34:
  8287. case 0x35:
  8288. case 0x36:
  8289. case 0x37:
  8290. return sax->number_integer(static_cast<std::int8_t>(0x20 - 1 - current));
  8291. case 0x38: // Negative integer (one-byte uint8_t follows)
  8292. {
  8293. std::uint8_t number{};
  8294. return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
  8295. }
  8296. case 0x39: // Negative integer -1-n (two-byte uint16_t follows)
  8297. {
  8298. std::uint16_t number{};
  8299. return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
  8300. }
  8301. case 0x3A: // Negative integer -1-n (four-byte uint32_t follows)
  8302. {
  8303. std::uint32_t number{};
  8304. return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
  8305. }
  8306. case 0x3B: // Negative integer -1-n (eight-byte uint64_t follows)
  8307. {
  8308. std::uint64_t number{};
  8309. return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1)
  8310. - static_cast<number_integer_t>(number));
  8311. }
  8312. // Binary data (0x00..0x17 bytes follow)
  8313. case 0x40:
  8314. case 0x41:
  8315. case 0x42:
  8316. case 0x43:
  8317. case 0x44:
  8318. case 0x45:
  8319. case 0x46:
  8320. case 0x47:
  8321. case 0x48:
  8322. case 0x49:
  8323. case 0x4A:
  8324. case 0x4B:
  8325. case 0x4C:
  8326. case 0x4D:
  8327. case 0x4E:
  8328. case 0x4F:
  8329. case 0x50:
  8330. case 0x51:
  8331. case 0x52:
  8332. case 0x53:
  8333. case 0x54:
  8334. case 0x55:
  8335. case 0x56:
  8336. case 0x57:
  8337. case 0x58: // Binary data (one-byte uint8_t for n follows)
  8338. case 0x59: // Binary data (two-byte uint16_t for n follow)
  8339. case 0x5A: // Binary data (four-byte uint32_t for n follow)
  8340. case 0x5B: // Binary data (eight-byte uint64_t for n follow)
  8341. case 0x5F: // Binary data (indefinite length)
  8342. {
  8343. binary_t b;
  8344. return get_cbor_binary(b) && sax->binary(b);
  8345. }
  8346. // UTF-8 string (0x00..0x17 bytes follow)
  8347. case 0x60:
  8348. case 0x61:
  8349. case 0x62:
  8350. case 0x63:
  8351. case 0x64:
  8352. case 0x65:
  8353. case 0x66:
  8354. case 0x67:
  8355. case 0x68:
  8356. case 0x69:
  8357. case 0x6A:
  8358. case 0x6B:
  8359. case 0x6C:
  8360. case 0x6D:
  8361. case 0x6E:
  8362. case 0x6F:
  8363. case 0x70:
  8364. case 0x71:
  8365. case 0x72:
  8366. case 0x73:
  8367. case 0x74:
  8368. case 0x75:
  8369. case 0x76:
  8370. case 0x77:
  8371. case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
  8372. case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
  8373. case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
  8374. case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
  8375. case 0x7F: // UTF-8 string (indefinite length)
  8376. {
  8377. string_t s;
  8378. return get_cbor_string(s) && sax->string(s);
  8379. }
  8380. // array (0x00..0x17 data items follow)
  8381. case 0x80:
  8382. case 0x81:
  8383. case 0x82:
  8384. case 0x83:
  8385. case 0x84:
  8386. case 0x85:
  8387. case 0x86:
  8388. case 0x87:
  8389. case 0x88:
  8390. case 0x89:
  8391. case 0x8A:
  8392. case 0x8B:
  8393. case 0x8C:
  8394. case 0x8D:
  8395. case 0x8E:
  8396. case 0x8F:
  8397. case 0x90:
  8398. case 0x91:
  8399. case 0x92:
  8400. case 0x93:
  8401. case 0x94:
  8402. case 0x95:
  8403. case 0x96:
  8404. case 0x97:
  8405. return get_cbor_array(
  8406. conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
  8407. case 0x98: // array (one-byte uint8_t for n follows)
  8408. {
  8409. std::uint8_t len{};
  8410. return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
  8411. }
  8412. case 0x99: // array (two-byte uint16_t for n follow)
  8413. {
  8414. std::uint16_t len{};
  8415. return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
  8416. }
  8417. case 0x9A: // array (four-byte uint32_t for n follow)
  8418. {
  8419. std::uint32_t len{};
  8420. return get_number(input_format_t::cbor, len) && get_cbor_array(conditional_static_cast<std::size_t>(len), tag_handler);
  8421. }
  8422. case 0x9B: // array (eight-byte uint64_t for n follow)
  8423. {
  8424. std::uint64_t len{};
  8425. return get_number(input_format_t::cbor, len) && get_cbor_array(conditional_static_cast<std::size_t>(len), tag_handler);
  8426. }
  8427. case 0x9F: // array (indefinite length)
  8428. return get_cbor_array(static_cast<std::size_t>(-1), tag_handler);
  8429. // map (0x00..0x17 pairs of data items follow)
  8430. case 0xA0:
  8431. case 0xA1:
  8432. case 0xA2:
  8433. case 0xA3:
  8434. case 0xA4:
  8435. case 0xA5:
  8436. case 0xA6:
  8437. case 0xA7:
  8438. case 0xA8:
  8439. case 0xA9:
  8440. case 0xAA:
  8441. case 0xAB:
  8442. case 0xAC:
  8443. case 0xAD:
  8444. case 0xAE:
  8445. case 0xAF:
  8446. case 0xB0:
  8447. case 0xB1:
  8448. case 0xB2:
  8449. case 0xB3:
  8450. case 0xB4:
  8451. case 0xB5:
  8452. case 0xB6:
  8453. case 0xB7:
  8454. return get_cbor_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
  8455. case 0xB8: // map (one-byte uint8_t for n follows)
  8456. {
  8457. std::uint8_t len{};
  8458. return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
  8459. }
  8460. case 0xB9: // map (two-byte uint16_t for n follow)
  8461. {
  8462. std::uint16_t len{};
  8463. return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
  8464. }
  8465. case 0xBA: // map (four-byte uint32_t for n follow)
  8466. {
  8467. std::uint32_t len{};
  8468. return get_number(input_format_t::cbor, len) && get_cbor_object(conditional_static_cast<std::size_t>(len), tag_handler);
  8469. }
  8470. case 0xBB: // map (eight-byte uint64_t for n follow)
  8471. {
  8472. std::uint64_t len{};
  8473. return get_number(input_format_t::cbor, len) && get_cbor_object(conditional_static_cast<std::size_t>(len), tag_handler);
  8474. }
  8475. case 0xBF: // map (indefinite length)
  8476. return get_cbor_object(static_cast<std::size_t>(-1), tag_handler);
  8477. case 0xC6: // tagged item
  8478. case 0xC7:
  8479. case 0xC8:
  8480. case 0xC9:
  8481. case 0xCA:
  8482. case 0xCB:
  8483. case 0xCC:
  8484. case 0xCD:
  8485. case 0xCE:
  8486. case 0xCF:
  8487. case 0xD0:
  8488. case 0xD1:
  8489. case 0xD2:
  8490. case 0xD3:
  8491. case 0xD4:
  8492. case 0xD8: // tagged item (1 bytes follow)
  8493. case 0xD9: // tagged item (2 bytes follow)
  8494. case 0xDA: // tagged item (4 bytes follow)
  8495. case 0xDB: // tagged item (8 bytes follow)
  8496. {
  8497. switch (tag_handler)
  8498. {
  8499. case cbor_tag_handler_t::error:
  8500. {
  8501. auto last_token = get_token_string();
  8502. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  8503. exception_message(input_format_t::cbor, concat("invalid byte: 0x", last_token), "value"), nullptr));
  8504. }
  8505. case cbor_tag_handler_t::ignore:
  8506. {
  8507. // ignore binary subtype
  8508. switch (current)
  8509. {
  8510. case 0xD8:
  8511. {
  8512. std::uint8_t subtype_to_ignore{};
  8513. get_number(input_format_t::cbor, subtype_to_ignore);
  8514. break;
  8515. }
  8516. case 0xD9:
  8517. {
  8518. std::uint16_t subtype_to_ignore{};
  8519. get_number(input_format_t::cbor, subtype_to_ignore);
  8520. break;
  8521. }
  8522. case 0xDA:
  8523. {
  8524. std::uint32_t subtype_to_ignore{};
  8525. get_number(input_format_t::cbor, subtype_to_ignore);
  8526. break;
  8527. }
  8528. case 0xDB:
  8529. {
  8530. std::uint64_t subtype_to_ignore{};
  8531. get_number(input_format_t::cbor, subtype_to_ignore);
  8532. break;
  8533. }
  8534. default:
  8535. break;
  8536. }
  8537. return parse_cbor_internal(true, tag_handler);
  8538. }
  8539. case cbor_tag_handler_t::store:
  8540. {
  8541. binary_t b;
  8542. // use binary subtype and store in binary container
  8543. switch (current)
  8544. {
  8545. case 0xD8:
  8546. {
  8547. std::uint8_t subtype{};
  8548. get_number(input_format_t::cbor, subtype);
  8549. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  8550. break;
  8551. }
  8552. case 0xD9:
  8553. {
  8554. std::uint16_t subtype{};
  8555. get_number(input_format_t::cbor, subtype);
  8556. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  8557. break;
  8558. }
  8559. case 0xDA:
  8560. {
  8561. std::uint32_t subtype{};
  8562. get_number(input_format_t::cbor, subtype);
  8563. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  8564. break;
  8565. }
  8566. case 0xDB:
  8567. {
  8568. std::uint64_t subtype{};
  8569. get_number(input_format_t::cbor, subtype);
  8570. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  8571. break;
  8572. }
  8573. default:
  8574. return parse_cbor_internal(true, tag_handler);
  8575. }
  8576. get();
  8577. return get_cbor_binary(b) && sax->binary(b);
  8578. }
  8579. default: // LCOV_EXCL_LINE
  8580. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  8581. return false; // LCOV_EXCL_LINE
  8582. }
  8583. }
  8584. case 0xF4: // false
  8585. return sax->boolean(false);
  8586. case 0xF5: // true
  8587. return sax->boolean(true);
  8588. case 0xF6: // null
  8589. return sax->null();
  8590. case 0xF9: // Half-Precision Float (two-byte IEEE 754)
  8591. {
  8592. const auto byte1_raw = get();
  8593. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number")))
  8594. {
  8595. return false;
  8596. }
  8597. const auto byte2_raw = get();
  8598. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number")))
  8599. {
  8600. return false;
  8601. }
  8602. const auto byte1 = static_cast<unsigned char>(byte1_raw);
  8603. const auto byte2 = static_cast<unsigned char>(byte2_raw);
  8604. // code from RFC 7049, Appendix D, Figure 3:
  8605. // As half-precision floating-point numbers were only added
  8606. // to IEEE 754 in 2008, today's programming platforms often
  8607. // still only have limited support for them. It is very
  8608. // easy to include at least decoding support for them even
  8609. // without such support. An example of a small decoder for
  8610. // half-precision floating-point numbers in the C language
  8611. // is shown in Fig. 3.
  8612. const auto half = static_cast<unsigned int>((byte1 << 8u) + byte2);
  8613. const double val = [&half]
  8614. {
  8615. const int exp = (half >> 10u) & 0x1Fu;
  8616. const unsigned int mant = half & 0x3FFu;
  8617. JSON_ASSERT(0 <= exp&& exp <= 32);
  8618. JSON_ASSERT(mant <= 1024);
  8619. switch (exp)
  8620. {
  8621. case 0:
  8622. return std::ldexp(mant, -24);
  8623. case 31:
  8624. return (mant == 0)
  8625. ? std::numeric_limits<double>::infinity()
  8626. : std::numeric_limits<double>::quiet_NaN();
  8627. default:
  8628. return std::ldexp(mant + 1024, exp - 25);
  8629. }
  8630. }();
  8631. return sax->number_float((half & 0x8000u) != 0
  8632. ? static_cast<number_float_t>(-val)
  8633. : static_cast<number_float_t>(val), "");
  8634. }
  8635. case 0xFA: // Single-Precision Float (four-byte IEEE 754)
  8636. {
  8637. float number{};
  8638. return get_number(input_format_t::cbor, number) && sax->number_float(static_cast<number_float_t>(number), "");
  8639. }
  8640. case 0xFB: // Double-Precision Float (eight-byte IEEE 754)
  8641. {
  8642. double number{};
  8643. return get_number(input_format_t::cbor, number) && sax->number_float(static_cast<number_float_t>(number), "");
  8644. }
  8645. default: // anything else (0xFF is handled inside the other types)
  8646. {
  8647. auto last_token = get_token_string();
  8648. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  8649. exception_message(input_format_t::cbor, concat("invalid byte: 0x", last_token), "value"), nullptr));
  8650. }
  8651. }
  8652. }
  8653. /*!
  8654. @brief reads a CBOR string
  8655. This function first reads starting bytes to determine the expected
  8656. string length and then copies this number of bytes into a string.
  8657. Additionally, CBOR's strings with indefinite lengths are supported.
  8658. @param[out] result created string
  8659. @return whether string creation completed
  8660. */
  8661. bool get_cbor_string(string_t& result)
  8662. {
  8663. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "string")))
  8664. {
  8665. return false;
  8666. }
  8667. switch (current)
  8668. {
  8669. // UTF-8 string (0x00..0x17 bytes follow)
  8670. case 0x60:
  8671. case 0x61:
  8672. case 0x62:
  8673. case 0x63:
  8674. case 0x64:
  8675. case 0x65:
  8676. case 0x66:
  8677. case 0x67:
  8678. case 0x68:
  8679. case 0x69:
  8680. case 0x6A:
  8681. case 0x6B:
  8682. case 0x6C:
  8683. case 0x6D:
  8684. case 0x6E:
  8685. case 0x6F:
  8686. case 0x70:
  8687. case 0x71:
  8688. case 0x72:
  8689. case 0x73:
  8690. case 0x74:
  8691. case 0x75:
  8692. case 0x76:
  8693. case 0x77:
  8694. {
  8695. return get_string(input_format_t::cbor, static_cast<unsigned int>(current) & 0x1Fu, result);
  8696. }
  8697. case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
  8698. {
  8699. std::uint8_t len{};
  8700. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  8701. }
  8702. case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
  8703. {
  8704. std::uint16_t len{};
  8705. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  8706. }
  8707. case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
  8708. {
  8709. std::uint32_t len{};
  8710. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  8711. }
  8712. case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
  8713. {
  8714. std::uint64_t len{};
  8715. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  8716. }
  8717. case 0x7F: // UTF-8 string (indefinite length)
  8718. {
  8719. while (get() != 0xFF)
  8720. {
  8721. string_t chunk;
  8722. if (!get_cbor_string(chunk))
  8723. {
  8724. return false;
  8725. }
  8726. result.append(chunk);
  8727. }
  8728. return true;
  8729. }
  8730. default:
  8731. {
  8732. auto last_token = get_token_string();
  8733. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
  8734. exception_message(input_format_t::cbor, concat("expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x", last_token), "string"), nullptr));
  8735. }
  8736. }
  8737. }
  8738. /*!
  8739. @brief reads a CBOR byte array
  8740. This function first reads starting bytes to determine the expected
  8741. byte array length and then copies this number of bytes into the byte array.
  8742. Additionally, CBOR's byte arrays with indefinite lengths are supported.
  8743. @param[out] result created byte array
  8744. @return whether byte array creation completed
  8745. */
  8746. bool get_cbor_binary(binary_t& result)
  8747. {
  8748. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "binary")))
  8749. {
  8750. return false;
  8751. }
  8752. switch (current)
  8753. {
  8754. // Binary data (0x00..0x17 bytes follow)
  8755. case 0x40:
  8756. case 0x41:
  8757. case 0x42:
  8758. case 0x43:
  8759. case 0x44:
  8760. case 0x45:
  8761. case 0x46:
  8762. case 0x47:
  8763. case 0x48:
  8764. case 0x49:
  8765. case 0x4A:
  8766. case 0x4B:
  8767. case 0x4C:
  8768. case 0x4D:
  8769. case 0x4E:
  8770. case 0x4F:
  8771. case 0x50:
  8772. case 0x51:
  8773. case 0x52:
  8774. case 0x53:
  8775. case 0x54:
  8776. case 0x55:
  8777. case 0x56:
  8778. case 0x57:
  8779. {
  8780. return get_binary(input_format_t::cbor, static_cast<unsigned int>(current) & 0x1Fu, result);
  8781. }
  8782. case 0x58: // Binary data (one-byte uint8_t for n follows)
  8783. {
  8784. std::uint8_t len{};
  8785. return get_number(input_format_t::cbor, len) &&
  8786. get_binary(input_format_t::cbor, len, result);
  8787. }
  8788. case 0x59: // Binary data (two-byte uint16_t for n follow)
  8789. {
  8790. std::uint16_t len{};
  8791. return get_number(input_format_t::cbor, len) &&
  8792. get_binary(input_format_t::cbor, len, result);
  8793. }
  8794. case 0x5A: // Binary data (four-byte uint32_t for n follow)
  8795. {
  8796. std::uint32_t len{};
  8797. return get_number(input_format_t::cbor, len) &&
  8798. get_binary(input_format_t::cbor, len, result);
  8799. }
  8800. case 0x5B: // Binary data (eight-byte uint64_t for n follow)
  8801. {
  8802. std::uint64_t len{};
  8803. return get_number(input_format_t::cbor, len) &&
  8804. get_binary(input_format_t::cbor, len, result);
  8805. }
  8806. case 0x5F: // Binary data (indefinite length)
  8807. {
  8808. while (get() != 0xFF)
  8809. {
  8810. binary_t chunk;
  8811. if (!get_cbor_binary(chunk))
  8812. {
  8813. return false;
  8814. }
  8815. result.insert(result.end(), chunk.begin(), chunk.end());
  8816. }
  8817. return true;
  8818. }
  8819. default:
  8820. {
  8821. auto last_token = get_token_string();
  8822. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
  8823. 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));
  8824. }
  8825. }
  8826. }
  8827. /*!
  8828. @param[in] len the length of the array or static_cast<std::size_t>(-1) for an
  8829. array of indefinite size
  8830. @param[in] tag_handler how CBOR tags should be treated
  8831. @return whether array creation completed
  8832. */
  8833. bool get_cbor_array(const std::size_t len,
  8834. const cbor_tag_handler_t tag_handler)
  8835. {
  8836. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
  8837. {
  8838. return false;
  8839. }
  8840. if (len != static_cast<std::size_t>(-1))
  8841. {
  8842. for (std::size_t i = 0; i < len; ++i)
  8843. {
  8844. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
  8845. {
  8846. return false;
  8847. }
  8848. }
  8849. }
  8850. else
  8851. {
  8852. while (get() != 0xFF)
  8853. {
  8854. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(false, tag_handler)))
  8855. {
  8856. return false;
  8857. }
  8858. }
  8859. }
  8860. return sax->end_array();
  8861. }
  8862. /*!
  8863. @param[in] len the length of the object or static_cast<std::size_t>(-1) for an
  8864. object of indefinite size
  8865. @param[in] tag_handler how CBOR tags should be treated
  8866. @return whether object creation completed
  8867. */
  8868. bool get_cbor_object(const std::size_t len,
  8869. const cbor_tag_handler_t tag_handler)
  8870. {
  8871. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
  8872. {
  8873. return false;
  8874. }
  8875. if (len != 0)
  8876. {
  8877. string_t key;
  8878. if (len != static_cast<std::size_t>(-1))
  8879. {
  8880. for (std::size_t i = 0; i < len; ++i)
  8881. {
  8882. get();
  8883. if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
  8884. {
  8885. return false;
  8886. }
  8887. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
  8888. {
  8889. return false;
  8890. }
  8891. key.clear();
  8892. }
  8893. }
  8894. else
  8895. {
  8896. while (get() != 0xFF)
  8897. {
  8898. if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
  8899. {
  8900. return false;
  8901. }
  8902. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
  8903. {
  8904. return false;
  8905. }
  8906. key.clear();
  8907. }
  8908. }
  8909. }
  8910. return sax->end_object();
  8911. }
  8912. /////////////
  8913. // MsgPack //
  8914. /////////////
  8915. /*!
  8916. @return whether a valid MessagePack value was passed to the SAX parser
  8917. */
  8918. bool parse_msgpack_internal()
  8919. {
  8920. switch (get())
  8921. {
  8922. // EOF
  8923. case std::char_traits<char_type>::eof():
  8924. return unexpect_eof(input_format_t::msgpack, "value");
  8925. // positive fixint
  8926. case 0x00:
  8927. case 0x01:
  8928. case 0x02:
  8929. case 0x03:
  8930. case 0x04:
  8931. case 0x05:
  8932. case 0x06:
  8933. case 0x07:
  8934. case 0x08:
  8935. case 0x09:
  8936. case 0x0A:
  8937. case 0x0B:
  8938. case 0x0C:
  8939. case 0x0D:
  8940. case 0x0E:
  8941. case 0x0F:
  8942. case 0x10:
  8943. case 0x11:
  8944. case 0x12:
  8945. case 0x13:
  8946. case 0x14:
  8947. case 0x15:
  8948. case 0x16:
  8949. case 0x17:
  8950. case 0x18:
  8951. case 0x19:
  8952. case 0x1A:
  8953. case 0x1B:
  8954. case 0x1C:
  8955. case 0x1D:
  8956. case 0x1E:
  8957. case 0x1F:
  8958. case 0x20:
  8959. case 0x21:
  8960. case 0x22:
  8961. case 0x23:
  8962. case 0x24:
  8963. case 0x25:
  8964. case 0x26:
  8965. case 0x27:
  8966. case 0x28:
  8967. case 0x29:
  8968. case 0x2A:
  8969. case 0x2B:
  8970. case 0x2C:
  8971. case 0x2D:
  8972. case 0x2E:
  8973. case 0x2F:
  8974. case 0x30:
  8975. case 0x31:
  8976. case 0x32:
  8977. case 0x33:
  8978. case 0x34:
  8979. case 0x35:
  8980. case 0x36:
  8981. case 0x37:
  8982. case 0x38:
  8983. case 0x39:
  8984. case 0x3A:
  8985. case 0x3B:
  8986. case 0x3C:
  8987. case 0x3D:
  8988. case 0x3E:
  8989. case 0x3F:
  8990. case 0x40:
  8991. case 0x41:
  8992. case 0x42:
  8993. case 0x43:
  8994. case 0x44:
  8995. case 0x45:
  8996. case 0x46:
  8997. case 0x47:
  8998. case 0x48:
  8999. case 0x49:
  9000. case 0x4A:
  9001. case 0x4B:
  9002. case 0x4C:
  9003. case 0x4D:
  9004. case 0x4E:
  9005. case 0x4F:
  9006. case 0x50:
  9007. case 0x51:
  9008. case 0x52:
  9009. case 0x53:
  9010. case 0x54:
  9011. case 0x55:
  9012. case 0x56:
  9013. case 0x57:
  9014. case 0x58:
  9015. case 0x59:
  9016. case 0x5A:
  9017. case 0x5B:
  9018. case 0x5C:
  9019. case 0x5D:
  9020. case 0x5E:
  9021. case 0x5F:
  9022. case 0x60:
  9023. case 0x61:
  9024. case 0x62:
  9025. case 0x63:
  9026. case 0x64:
  9027. case 0x65:
  9028. case 0x66:
  9029. case 0x67:
  9030. case 0x68:
  9031. case 0x69:
  9032. case 0x6A:
  9033. case 0x6B:
  9034. case 0x6C:
  9035. case 0x6D:
  9036. case 0x6E:
  9037. case 0x6F:
  9038. case 0x70:
  9039. case 0x71:
  9040. case 0x72:
  9041. case 0x73:
  9042. case 0x74:
  9043. case 0x75:
  9044. case 0x76:
  9045. case 0x77:
  9046. case 0x78:
  9047. case 0x79:
  9048. case 0x7A:
  9049. case 0x7B:
  9050. case 0x7C:
  9051. case 0x7D:
  9052. case 0x7E:
  9053. case 0x7F:
  9054. return sax->number_unsigned(static_cast<number_unsigned_t>(current));
  9055. // fixmap
  9056. case 0x80:
  9057. case 0x81:
  9058. case 0x82:
  9059. case 0x83:
  9060. case 0x84:
  9061. case 0x85:
  9062. case 0x86:
  9063. case 0x87:
  9064. case 0x88:
  9065. case 0x89:
  9066. case 0x8A:
  9067. case 0x8B:
  9068. case 0x8C:
  9069. case 0x8D:
  9070. case 0x8E:
  9071. case 0x8F:
  9072. return get_msgpack_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
  9073. // fixarray
  9074. case 0x90:
  9075. case 0x91:
  9076. case 0x92:
  9077. case 0x93:
  9078. case 0x94:
  9079. case 0x95:
  9080. case 0x96:
  9081. case 0x97:
  9082. case 0x98:
  9083. case 0x99:
  9084. case 0x9A:
  9085. case 0x9B:
  9086. case 0x9C:
  9087. case 0x9D:
  9088. case 0x9E:
  9089. case 0x9F:
  9090. return get_msgpack_array(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
  9091. // fixstr
  9092. case 0xA0:
  9093. case 0xA1:
  9094. case 0xA2:
  9095. case 0xA3:
  9096. case 0xA4:
  9097. case 0xA5:
  9098. case 0xA6:
  9099. case 0xA7:
  9100. case 0xA8:
  9101. case 0xA9:
  9102. case 0xAA:
  9103. case 0xAB:
  9104. case 0xAC:
  9105. case 0xAD:
  9106. case 0xAE:
  9107. case 0xAF:
  9108. case 0xB0:
  9109. case 0xB1:
  9110. case 0xB2:
  9111. case 0xB3:
  9112. case 0xB4:
  9113. case 0xB5:
  9114. case 0xB6:
  9115. case 0xB7:
  9116. case 0xB8:
  9117. case 0xB9:
  9118. case 0xBA:
  9119. case 0xBB:
  9120. case 0xBC:
  9121. case 0xBD:
  9122. case 0xBE:
  9123. case 0xBF:
  9124. case 0xD9: // str 8
  9125. case 0xDA: // str 16
  9126. case 0xDB: // str 32
  9127. {
  9128. string_t s;
  9129. return get_msgpack_string(s) && sax->string(s);
  9130. }
  9131. case 0xC0: // nil
  9132. return sax->null();
  9133. case 0xC2: // false
  9134. return sax->boolean(false);
  9135. case 0xC3: // true
  9136. return sax->boolean(true);
  9137. case 0xC4: // bin 8
  9138. case 0xC5: // bin 16
  9139. case 0xC6: // bin 32
  9140. case 0xC7: // ext 8
  9141. case 0xC8: // ext 16
  9142. case 0xC9: // ext 32
  9143. case 0xD4: // fixext 1
  9144. case 0xD5: // fixext 2
  9145. case 0xD6: // fixext 4
  9146. case 0xD7: // fixext 8
  9147. case 0xD8: // fixext 16
  9148. {
  9149. binary_t b;
  9150. return get_msgpack_binary(b) && sax->binary(b);
  9151. }
  9152. case 0xCA: // float 32
  9153. {
  9154. float number{};
  9155. return get_number(input_format_t::msgpack, number) && sax->number_float(static_cast<number_float_t>(number), "");
  9156. }
  9157. case 0xCB: // float 64
  9158. {
  9159. double number{};
  9160. return get_number(input_format_t::msgpack, number) && sax->number_float(static_cast<number_float_t>(number), "");
  9161. }
  9162. case 0xCC: // uint 8
  9163. {
  9164. std::uint8_t number{};
  9165. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  9166. }
  9167. case 0xCD: // uint 16
  9168. {
  9169. std::uint16_t number{};
  9170. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  9171. }
  9172. case 0xCE: // uint 32
  9173. {
  9174. std::uint32_t number{};
  9175. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  9176. }
  9177. case 0xCF: // uint 64
  9178. {
  9179. std::uint64_t number{};
  9180. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  9181. }
  9182. case 0xD0: // int 8
  9183. {
  9184. std::int8_t number{};
  9185. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  9186. }
  9187. case 0xD1: // int 16
  9188. {
  9189. std::int16_t number{};
  9190. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  9191. }
  9192. case 0xD2: // int 32
  9193. {
  9194. std::int32_t number{};
  9195. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  9196. }
  9197. case 0xD3: // int 64
  9198. {
  9199. std::int64_t number{};
  9200. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  9201. }
  9202. case 0xDC: // array 16
  9203. {
  9204. std::uint16_t len{};
  9205. return get_number(input_format_t::msgpack, len) && get_msgpack_array(static_cast<std::size_t>(len));
  9206. }
  9207. case 0xDD: // array 32
  9208. {
  9209. std::uint32_t len{};
  9210. return get_number(input_format_t::msgpack, len) && get_msgpack_array(conditional_static_cast<std::size_t>(len));
  9211. }
  9212. case 0xDE: // map 16
  9213. {
  9214. std::uint16_t len{};
  9215. return get_number(input_format_t::msgpack, len) && get_msgpack_object(static_cast<std::size_t>(len));
  9216. }
  9217. case 0xDF: // map 32
  9218. {
  9219. std::uint32_t len{};
  9220. return get_number(input_format_t::msgpack, len) && get_msgpack_object(conditional_static_cast<std::size_t>(len));
  9221. }
  9222. // negative fixint
  9223. case 0xE0:
  9224. case 0xE1:
  9225. case 0xE2:
  9226. case 0xE3:
  9227. case 0xE4:
  9228. case 0xE5:
  9229. case 0xE6:
  9230. case 0xE7:
  9231. case 0xE8:
  9232. case 0xE9:
  9233. case 0xEA:
  9234. case 0xEB:
  9235. case 0xEC:
  9236. case 0xED:
  9237. case 0xEE:
  9238. case 0xEF:
  9239. case 0xF0:
  9240. case 0xF1:
  9241. case 0xF2:
  9242. case 0xF3:
  9243. case 0xF4:
  9244. case 0xF5:
  9245. case 0xF6:
  9246. case 0xF7:
  9247. case 0xF8:
  9248. case 0xF9:
  9249. case 0xFA:
  9250. case 0xFB:
  9251. case 0xFC:
  9252. case 0xFD:
  9253. case 0xFE:
  9254. case 0xFF:
  9255. return sax->number_integer(static_cast<std::int8_t>(current));
  9256. default: // anything else
  9257. {
  9258. auto last_token = get_token_string();
  9259. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  9260. exception_message(input_format_t::msgpack, concat("invalid byte: 0x", last_token), "value"), nullptr));
  9261. }
  9262. }
  9263. }
  9264. /*!
  9265. @brief reads a MessagePack string
  9266. This function first reads starting bytes to determine the expected
  9267. string length and then copies this number of bytes into a string.
  9268. @param[out] result created string
  9269. @return whether string creation completed
  9270. */
  9271. bool get_msgpack_string(string_t& result)
  9272. {
  9273. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::msgpack, "string")))
  9274. {
  9275. return false;
  9276. }
  9277. switch (current)
  9278. {
  9279. // fixstr
  9280. case 0xA0:
  9281. case 0xA1:
  9282. case 0xA2:
  9283. case 0xA3:
  9284. case 0xA4:
  9285. case 0xA5:
  9286. case 0xA6:
  9287. case 0xA7:
  9288. case 0xA8:
  9289. case 0xA9:
  9290. case 0xAA:
  9291. case 0xAB:
  9292. case 0xAC:
  9293. case 0xAD:
  9294. case 0xAE:
  9295. case 0xAF:
  9296. case 0xB0:
  9297. case 0xB1:
  9298. case 0xB2:
  9299. case 0xB3:
  9300. case 0xB4:
  9301. case 0xB5:
  9302. case 0xB6:
  9303. case 0xB7:
  9304. case 0xB8:
  9305. case 0xB9:
  9306. case 0xBA:
  9307. case 0xBB:
  9308. case 0xBC:
  9309. case 0xBD:
  9310. case 0xBE:
  9311. case 0xBF:
  9312. {
  9313. return get_string(input_format_t::msgpack, static_cast<unsigned int>(current) & 0x1Fu, result);
  9314. }
  9315. case 0xD9: // str 8
  9316. {
  9317. std::uint8_t len{};
  9318. return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
  9319. }
  9320. case 0xDA: // str 16
  9321. {
  9322. std::uint16_t len{};
  9323. return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
  9324. }
  9325. case 0xDB: // str 32
  9326. {
  9327. std::uint32_t len{};
  9328. return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
  9329. }
  9330. default:
  9331. {
  9332. auto last_token = get_token_string();
  9333. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
  9334. exception_message(input_format_t::msgpack, concat("expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x", last_token), "string"), nullptr));
  9335. }
  9336. }
  9337. }
  9338. /*!
  9339. @brief reads a MessagePack byte array
  9340. This function first reads starting bytes to determine the expected
  9341. byte array length and then copies this number of bytes into a byte array.
  9342. @param[out] result created byte array
  9343. @return whether byte array creation completed
  9344. */
  9345. bool get_msgpack_binary(binary_t& result)
  9346. {
  9347. // helper function to set the subtype
  9348. auto assign_and_return_true = [&result](std::int8_t subtype)
  9349. {
  9350. result.set_subtype(static_cast<std::uint8_t>(subtype));
  9351. return true;
  9352. };
  9353. switch (current)
  9354. {
  9355. case 0xC4: // bin 8
  9356. {
  9357. std::uint8_t len{};
  9358. return get_number(input_format_t::msgpack, len) &&
  9359. get_binary(input_format_t::msgpack, len, result);
  9360. }
  9361. case 0xC5: // bin 16
  9362. {
  9363. std::uint16_t len{};
  9364. return get_number(input_format_t::msgpack, len) &&
  9365. get_binary(input_format_t::msgpack, len, result);
  9366. }
  9367. case 0xC6: // bin 32
  9368. {
  9369. std::uint32_t len{};
  9370. return get_number(input_format_t::msgpack, len) &&
  9371. get_binary(input_format_t::msgpack, len, result);
  9372. }
  9373. case 0xC7: // ext 8
  9374. {
  9375. std::uint8_t len{};
  9376. std::int8_t subtype{};
  9377. return get_number(input_format_t::msgpack, len) &&
  9378. get_number(input_format_t::msgpack, subtype) &&
  9379. get_binary(input_format_t::msgpack, len, result) &&
  9380. assign_and_return_true(subtype);
  9381. }
  9382. case 0xC8: // ext 16
  9383. {
  9384. std::uint16_t len{};
  9385. std::int8_t subtype{};
  9386. return get_number(input_format_t::msgpack, len) &&
  9387. get_number(input_format_t::msgpack, subtype) &&
  9388. get_binary(input_format_t::msgpack, len, result) &&
  9389. assign_and_return_true(subtype);
  9390. }
  9391. case 0xC9: // ext 32
  9392. {
  9393. std::uint32_t len{};
  9394. std::int8_t subtype{};
  9395. return get_number(input_format_t::msgpack, len) &&
  9396. get_number(input_format_t::msgpack, subtype) &&
  9397. get_binary(input_format_t::msgpack, len, result) &&
  9398. assign_and_return_true(subtype);
  9399. }
  9400. case 0xD4: // fixext 1
  9401. {
  9402. std::int8_t subtype{};
  9403. return get_number(input_format_t::msgpack, subtype) &&
  9404. get_binary(input_format_t::msgpack, 1, result) &&
  9405. assign_and_return_true(subtype);
  9406. }
  9407. case 0xD5: // fixext 2
  9408. {
  9409. std::int8_t subtype{};
  9410. return get_number(input_format_t::msgpack, subtype) &&
  9411. get_binary(input_format_t::msgpack, 2, result) &&
  9412. assign_and_return_true(subtype);
  9413. }
  9414. case 0xD6: // fixext 4
  9415. {
  9416. std::int8_t subtype{};
  9417. return get_number(input_format_t::msgpack, subtype) &&
  9418. get_binary(input_format_t::msgpack, 4, result) &&
  9419. assign_and_return_true(subtype);
  9420. }
  9421. case 0xD7: // fixext 8
  9422. {
  9423. std::int8_t subtype{};
  9424. return get_number(input_format_t::msgpack, subtype) &&
  9425. get_binary(input_format_t::msgpack, 8, result) &&
  9426. assign_and_return_true(subtype);
  9427. }
  9428. case 0xD8: // fixext 16
  9429. {
  9430. std::int8_t subtype{};
  9431. return get_number(input_format_t::msgpack, subtype) &&
  9432. get_binary(input_format_t::msgpack, 16, result) &&
  9433. assign_and_return_true(subtype);
  9434. }
  9435. default: // LCOV_EXCL_LINE
  9436. return false; // LCOV_EXCL_LINE
  9437. }
  9438. }
  9439. /*!
  9440. @param[in] len the length of the array
  9441. @return whether array creation completed
  9442. */
  9443. bool get_msgpack_array(const std::size_t len)
  9444. {
  9445. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
  9446. {
  9447. return false;
  9448. }
  9449. for (std::size_t i = 0; i < len; ++i)
  9450. {
  9451. if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
  9452. {
  9453. return false;
  9454. }
  9455. }
  9456. return sax->end_array();
  9457. }
  9458. /*!
  9459. @param[in] len the length of the object
  9460. @return whether object creation completed
  9461. */
  9462. bool get_msgpack_object(const std::size_t len)
  9463. {
  9464. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
  9465. {
  9466. return false;
  9467. }
  9468. string_t key;
  9469. for (std::size_t i = 0; i < len; ++i)
  9470. {
  9471. get();
  9472. if (JSON_HEDLEY_UNLIKELY(!get_msgpack_string(key) || !sax->key(key)))
  9473. {
  9474. return false;
  9475. }
  9476. if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
  9477. {
  9478. return false;
  9479. }
  9480. key.clear();
  9481. }
  9482. return sax->end_object();
  9483. }
  9484. ////////////
  9485. // UBJSON //
  9486. ////////////
  9487. /*!
  9488. @param[in] get_char whether a new character should be retrieved from the
  9489. input (true, default) or whether the last read
  9490. character should be considered instead
  9491. @return whether a valid UBJSON value was passed to the SAX parser
  9492. */
  9493. bool parse_ubjson_internal(const bool get_char = true)
  9494. {
  9495. return get_ubjson_value(get_char ? get_ignore_noop() : current);
  9496. }
  9497. /*!
  9498. @brief reads a UBJSON string
  9499. This function is either called after reading the 'S' byte explicitly
  9500. indicating a string, or in case of an object key where the 'S' byte can be
  9501. left out.
  9502. @param[out] result created string
  9503. @param[in] get_char whether a new character should be retrieved from the
  9504. input (true, default) or whether the last read
  9505. character should be considered instead
  9506. @return whether string creation completed
  9507. */
  9508. bool get_ubjson_string(string_t& result, const bool get_char = true)
  9509. {
  9510. if (get_char)
  9511. {
  9512. get(); // TODO(niels): may we ignore N here?
  9513. }
  9514. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "value")))
  9515. {
  9516. return false;
  9517. }
  9518. switch (current)
  9519. {
  9520. case 'U':
  9521. {
  9522. std::uint8_t len{};
  9523. return get_number(input_format, len) && get_string(input_format, len, result);
  9524. }
  9525. case 'i':
  9526. {
  9527. std::int8_t len{};
  9528. return get_number(input_format, len) && get_string(input_format, len, result);
  9529. }
  9530. case 'I':
  9531. {
  9532. std::int16_t len{};
  9533. return get_number(input_format, len) && get_string(input_format, len, result);
  9534. }
  9535. case 'l':
  9536. {
  9537. std::int32_t len{};
  9538. return get_number(input_format, len) && get_string(input_format, len, result);
  9539. }
  9540. case 'L':
  9541. {
  9542. std::int64_t len{};
  9543. return get_number(input_format, len) && get_string(input_format, len, result);
  9544. }
  9545. case 'u':
  9546. {
  9547. if (input_format != input_format_t::bjdata)
  9548. {
  9549. break;
  9550. }
  9551. std::uint16_t len{};
  9552. return get_number(input_format, len) && get_string(input_format, len, result);
  9553. }
  9554. case 'm':
  9555. {
  9556. if (input_format != input_format_t::bjdata)
  9557. {
  9558. break;
  9559. }
  9560. std::uint32_t len{};
  9561. return get_number(input_format, len) && get_string(input_format, len, result);
  9562. }
  9563. case 'M':
  9564. {
  9565. if (input_format != input_format_t::bjdata)
  9566. {
  9567. break;
  9568. }
  9569. std::uint64_t len{};
  9570. return get_number(input_format, len) && get_string(input_format, len, result);
  9571. }
  9572. default:
  9573. break;
  9574. }
  9575. auto last_token = get_token_string();
  9576. std::string message;
  9577. if (input_format != input_format_t::bjdata)
  9578. {
  9579. message = "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token;
  9580. }
  9581. else
  9582. {
  9583. message = "expected length type specification (U, i, u, I, m, l, M, L); last byte: 0x" + last_token;
  9584. }
  9585. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "string"), nullptr));
  9586. }
  9587. /*!
  9588. @param[out] dim an integer vector storing the ND array dimensions
  9589. @return whether reading ND array size vector is successful
  9590. */
  9591. bool get_ubjson_ndarray_size(std::vector<size_t>& dim)
  9592. {
  9593. std::pair<std::size_t, char_int_type> size_and_type;
  9594. size_t dimlen = 0;
  9595. bool no_ndarray = true;
  9596. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type, no_ndarray)))
  9597. {
  9598. return false;
  9599. }
  9600. if (size_and_type.first != npos)
  9601. {
  9602. if (size_and_type.second != 0)
  9603. {
  9604. if (size_and_type.second != 'N')
  9605. {
  9606. for (std::size_t i = 0; i < size_and_type.first; ++i)
  9607. {
  9608. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray, size_and_type.second)))
  9609. {
  9610. return false;
  9611. }
  9612. dim.push_back(dimlen);
  9613. }
  9614. }
  9615. }
  9616. else
  9617. {
  9618. for (std::size_t i = 0; i < size_and_type.first; ++i)
  9619. {
  9620. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray)))
  9621. {
  9622. return false;
  9623. }
  9624. dim.push_back(dimlen);
  9625. }
  9626. }
  9627. }
  9628. else
  9629. {
  9630. while (current != ']')
  9631. {
  9632. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray, current)))
  9633. {
  9634. return false;
  9635. }
  9636. dim.push_back(dimlen);
  9637. get_ignore_noop();
  9638. }
  9639. }
  9640. return true;
  9641. }
  9642. /*!
  9643. @param[out] result determined size
  9644. @param[in,out] is_ndarray for input, `true` means already inside an ndarray vector
  9645. or ndarray dimension is not allowed; `false` means ndarray
  9646. is allowed; for output, `true` means an ndarray is found;
  9647. is_ndarray can only return `true` when its initial value
  9648. is `false`
  9649. @param[in] prefix type marker if already read, otherwise set to 0
  9650. @return whether size determination completed
  9651. */
  9652. bool get_ubjson_size_value(std::size_t& result, bool& is_ndarray, char_int_type prefix = 0)
  9653. {
  9654. if (prefix == 0)
  9655. {
  9656. prefix = get_ignore_noop();
  9657. }
  9658. switch (prefix)
  9659. {
  9660. case 'U':
  9661. {
  9662. std::uint8_t number{};
  9663. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9664. {
  9665. return false;
  9666. }
  9667. result = static_cast<std::size_t>(number);
  9668. return true;
  9669. }
  9670. case 'i':
  9671. {
  9672. std::int8_t number{};
  9673. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9674. {
  9675. return false;
  9676. }
  9677. if (number < 0)
  9678. {
  9679. return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
  9680. exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
  9681. }
  9682. result = static_cast<std::size_t>(number); // NOLINT(bugprone-signed-char-misuse,cert-str34-c): number is not a char
  9683. return true;
  9684. }
  9685. case 'I':
  9686. {
  9687. std::int16_t number{};
  9688. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9689. {
  9690. return false;
  9691. }
  9692. if (number < 0)
  9693. {
  9694. return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
  9695. exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
  9696. }
  9697. result = static_cast<std::size_t>(number);
  9698. return true;
  9699. }
  9700. case 'l':
  9701. {
  9702. std::int32_t number{};
  9703. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9704. {
  9705. return false;
  9706. }
  9707. if (number < 0)
  9708. {
  9709. return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
  9710. exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
  9711. }
  9712. result = static_cast<std::size_t>(number);
  9713. return true;
  9714. }
  9715. case 'L':
  9716. {
  9717. std::int64_t number{};
  9718. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9719. {
  9720. return false;
  9721. }
  9722. if (number < 0)
  9723. {
  9724. return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
  9725. exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
  9726. }
  9727. if (!value_in_range_of<std::size_t>(number))
  9728. {
  9729. return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408,
  9730. exception_message(input_format, "integer value overflow", "size"), nullptr));
  9731. }
  9732. result = static_cast<std::size_t>(number);
  9733. return true;
  9734. }
  9735. case 'u':
  9736. {
  9737. if (input_format != input_format_t::bjdata)
  9738. {
  9739. break;
  9740. }
  9741. std::uint16_t number{};
  9742. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9743. {
  9744. return false;
  9745. }
  9746. result = static_cast<std::size_t>(number);
  9747. return true;
  9748. }
  9749. case 'm':
  9750. {
  9751. if (input_format != input_format_t::bjdata)
  9752. {
  9753. break;
  9754. }
  9755. std::uint32_t number{};
  9756. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9757. {
  9758. return false;
  9759. }
  9760. result = conditional_static_cast<std::size_t>(number);
  9761. return true;
  9762. }
  9763. case 'M':
  9764. {
  9765. if (input_format != input_format_t::bjdata)
  9766. {
  9767. break;
  9768. }
  9769. std::uint64_t number{};
  9770. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9771. {
  9772. return false;
  9773. }
  9774. if (!value_in_range_of<std::size_t>(number))
  9775. {
  9776. return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408,
  9777. exception_message(input_format, "integer value overflow", "size"), nullptr));
  9778. }
  9779. result = detail::conditional_static_cast<std::size_t>(number);
  9780. return true;
  9781. }
  9782. case '[':
  9783. {
  9784. if (input_format != input_format_t::bjdata)
  9785. {
  9786. break;
  9787. }
  9788. if (is_ndarray) // ndarray dimensional vector can only contain integers, and can not embed another array
  9789. {
  9790. return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read, exception_message(input_format, "ndarray dimentional vector is not allowed", "size"), nullptr));
  9791. }
  9792. std::vector<size_t> dim;
  9793. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_ndarray_size(dim)))
  9794. {
  9795. return false;
  9796. }
  9797. if (dim.size() == 1 || (dim.size() == 2 && dim.at(0) == 1)) // return normal array size if 1D row vector
  9798. {
  9799. result = dim.at(dim.size() - 1);
  9800. return true;
  9801. }
  9802. if (!dim.empty()) // if ndarray, convert to an object in JData annotated array format
  9803. {
  9804. for (auto i : dim) // test if any dimension in an ndarray is 0, if so, return a 1D empty container
  9805. {
  9806. if ( i == 0 )
  9807. {
  9808. result = 0;
  9809. return true;
  9810. }
  9811. }
  9812. string_t key = "_ArraySize_";
  9813. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(3) || !sax->key(key) || !sax->start_array(dim.size())))
  9814. {
  9815. return false;
  9816. }
  9817. result = 1;
  9818. for (auto i : dim)
  9819. {
  9820. result *= i;
  9821. 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()
  9822. {
  9823. 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));
  9824. }
  9825. if (JSON_HEDLEY_UNLIKELY(!sax->number_unsigned(static_cast<number_unsigned_t>(i))))
  9826. {
  9827. return false;
  9828. }
  9829. }
  9830. is_ndarray = true;
  9831. return sax->end_array();
  9832. }
  9833. result = 0;
  9834. return true;
  9835. }
  9836. default:
  9837. break;
  9838. }
  9839. auto last_token = get_token_string();
  9840. std::string message;
  9841. if (input_format != input_format_t::bjdata)
  9842. {
  9843. message = "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token;
  9844. }
  9845. else
  9846. {
  9847. message = "expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x" + last_token;
  9848. }
  9849. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "size"), nullptr));
  9850. }
  9851. /*!
  9852. @brief determine the type and size for a container
  9853. In the optimized UBJSON format, a type and a size can be provided to allow
  9854. for a more compact representation.
  9855. @param[out] result pair of the size and the type
  9856. @param[in] inside_ndarray whether the parser is parsing an ND array dimensional vector
  9857. @return whether pair creation completed
  9858. */
  9859. bool get_ubjson_size_type(std::pair<std::size_t, char_int_type>& result, bool inside_ndarray = false)
  9860. {
  9861. result.first = npos; // size
  9862. result.second = 0; // type
  9863. bool is_ndarray = false;
  9864. get_ignore_noop();
  9865. if (current == '$')
  9866. {
  9867. result.second = get(); // must not ignore 'N', because 'N' maybe the type
  9868. if (input_format == input_format_t::bjdata
  9869. && JSON_HEDLEY_UNLIKELY(std::binary_search(bjd_optimized_type_markers.begin(), bjd_optimized_type_markers.end(), result.second)))
  9870. {
  9871. auto last_token = get_token_string();
  9872. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  9873. exception_message(input_format, concat("marker 0x", last_token, " is not a permitted optimized array type"), "type"), nullptr));
  9874. }
  9875. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "type")))
  9876. {
  9877. return false;
  9878. }
  9879. get_ignore_noop();
  9880. if (JSON_HEDLEY_UNLIKELY(current != '#'))
  9881. {
  9882. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "value")))
  9883. {
  9884. return false;
  9885. }
  9886. auto last_token = get_token_string();
  9887. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  9888. exception_message(input_format, concat("expected '#' after type information; last byte: 0x", last_token), "size"), nullptr));
  9889. }
  9890. bool is_error = get_ubjson_size_value(result.first, is_ndarray);
  9891. if (input_format == input_format_t::bjdata && is_ndarray)
  9892. {
  9893. if (inside_ndarray)
  9894. {
  9895. return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
  9896. exception_message(input_format, "ndarray can not be recursive", "size"), nullptr));
  9897. }
  9898. result.second |= (1 << 8); // use bit 8 to indicate ndarray, all UBJSON and BJData markers should be ASCII letters
  9899. }
  9900. return is_error;
  9901. }
  9902. if (current == '#')
  9903. {
  9904. bool is_error = get_ubjson_size_value(result.first, is_ndarray);
  9905. if (input_format == input_format_t::bjdata && is_ndarray)
  9906. {
  9907. return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
  9908. exception_message(input_format, "ndarray requires both type and size", "size"), nullptr));
  9909. }
  9910. return is_error;
  9911. }
  9912. return true;
  9913. }
  9914. /*!
  9915. @param prefix the previously read or set type prefix
  9916. @return whether value creation completed
  9917. */
  9918. bool get_ubjson_value(const char_int_type prefix)
  9919. {
  9920. switch (prefix)
  9921. {
  9922. case std::char_traits<char_type>::eof(): // EOF
  9923. return unexpect_eof(input_format, "value");
  9924. case 'T': // true
  9925. return sax->boolean(true);
  9926. case 'F': // false
  9927. return sax->boolean(false);
  9928. case 'Z': // null
  9929. return sax->null();
  9930. case 'U':
  9931. {
  9932. std::uint8_t number{};
  9933. return get_number(input_format, number) && sax->number_unsigned(number);
  9934. }
  9935. case 'i':
  9936. {
  9937. std::int8_t number{};
  9938. return get_number(input_format, number) && sax->number_integer(number);
  9939. }
  9940. case 'I':
  9941. {
  9942. std::int16_t number{};
  9943. return get_number(input_format, number) && sax->number_integer(number);
  9944. }
  9945. case 'l':
  9946. {
  9947. std::int32_t number{};
  9948. return get_number(input_format, number) && sax->number_integer(number);
  9949. }
  9950. case 'L':
  9951. {
  9952. std::int64_t number{};
  9953. return get_number(input_format, number) && sax->number_integer(number);
  9954. }
  9955. case 'u':
  9956. {
  9957. if (input_format != input_format_t::bjdata)
  9958. {
  9959. break;
  9960. }
  9961. std::uint16_t number{};
  9962. return get_number(input_format, number) && sax->number_unsigned(number);
  9963. }
  9964. case 'm':
  9965. {
  9966. if (input_format != input_format_t::bjdata)
  9967. {
  9968. break;
  9969. }
  9970. std::uint32_t number{};
  9971. return get_number(input_format, number) && sax->number_unsigned(number);
  9972. }
  9973. case 'M':
  9974. {
  9975. if (input_format != input_format_t::bjdata)
  9976. {
  9977. break;
  9978. }
  9979. std::uint64_t number{};
  9980. return get_number(input_format, number) && sax->number_unsigned(number);
  9981. }
  9982. case 'h':
  9983. {
  9984. if (input_format != input_format_t::bjdata)
  9985. {
  9986. break;
  9987. }
  9988. const auto byte1_raw = get();
  9989. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
  9990. {
  9991. return false;
  9992. }
  9993. const auto byte2_raw = get();
  9994. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
  9995. {
  9996. return false;
  9997. }
  9998. const auto byte1 = static_cast<unsigned char>(byte1_raw);
  9999. const auto byte2 = static_cast<unsigned char>(byte2_raw);
  10000. // code from RFC 7049, Appendix D, Figure 3:
  10001. // As half-precision floating-point numbers were only added
  10002. // to IEEE 754 in 2008, today's programming platforms often
  10003. // still only have limited support for them. It is very
  10004. // easy to include at least decoding support for them even
  10005. // without such support. An example of a small decoder for
  10006. // half-precision floating-point numbers in the C language
  10007. // is shown in Fig. 3.
  10008. const auto half = static_cast<unsigned int>((byte2 << 8u) + byte1);
  10009. const double val = [&half]
  10010. {
  10011. const int exp = (half >> 10u) & 0x1Fu;
  10012. const unsigned int mant = half & 0x3FFu;
  10013. JSON_ASSERT(0 <= exp&& exp <= 32);
  10014. JSON_ASSERT(mant <= 1024);
  10015. switch (exp)
  10016. {
  10017. case 0:
  10018. return std::ldexp(mant, -24);
  10019. case 31:
  10020. return (mant == 0)
  10021. ? std::numeric_limits<double>::infinity()
  10022. : std::numeric_limits<double>::quiet_NaN();
  10023. default:
  10024. return std::ldexp(mant + 1024, exp - 25);
  10025. }
  10026. }();
  10027. return sax->number_float((half & 0x8000u) != 0
  10028. ? static_cast<number_float_t>(-val)
  10029. : static_cast<number_float_t>(val), "");
  10030. }
  10031. case 'd':
  10032. {
  10033. float number{};
  10034. return get_number(input_format, number) && sax->number_float(static_cast<number_float_t>(number), "");
  10035. }
  10036. case 'D':
  10037. {
  10038. double number{};
  10039. return get_number(input_format, number) && sax->number_float(static_cast<number_float_t>(number), "");
  10040. }
  10041. case 'H':
  10042. {
  10043. return get_ubjson_high_precision_number();
  10044. }
  10045. case 'C': // char
  10046. {
  10047. get();
  10048. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "char")))
  10049. {
  10050. return false;
  10051. }
  10052. if (JSON_HEDLEY_UNLIKELY(current > 127))
  10053. {
  10054. auto last_token = get_token_string();
  10055. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
  10056. exception_message(input_format, concat("byte after 'C' must be in range 0x00..0x7F; last byte: 0x", last_token), "char"), nullptr));
  10057. }
  10058. string_t s(1, static_cast<typename string_t::value_type>(current));
  10059. return sax->string(s);
  10060. }
  10061. case 'S': // string
  10062. {
  10063. string_t s;
  10064. return get_ubjson_string(s) && sax->string(s);
  10065. }
  10066. case '[': // array
  10067. return get_ubjson_array();
  10068. case '{': // object
  10069. return get_ubjson_object();
  10070. default: // anything else
  10071. break;
  10072. }
  10073. auto last_token = get_token_string();
  10074. 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));
  10075. }
  10076. /*!
  10077. @return whether array creation completed
  10078. */
  10079. bool get_ubjson_array()
  10080. {
  10081. std::pair<std::size_t, char_int_type> size_and_type;
  10082. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type)))
  10083. {
  10084. return false;
  10085. }
  10086. // 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):
  10087. // {"_ArrayType_" : "typeid", "_ArraySize_" : [n1, n2, ...], "_ArrayData_" : [v1, v2, ...]}
  10088. if (input_format == input_format_t::bjdata && size_and_type.first != npos && (size_and_type.second & (1 << 8)) != 0)
  10089. {
  10090. 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
  10091. 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)
  10092. {
  10093. return p.first < t;
  10094. });
  10095. string_t key = "_ArrayType_";
  10096. if (JSON_HEDLEY_UNLIKELY(it == bjd_types_map.end() || it->first != size_and_type.second))
  10097. {
  10098. auto last_token = get_token_string();
  10099. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  10100. exception_message(input_format, "invalid byte: 0x" + last_token, "type"), nullptr));
  10101. }
  10102. string_t type = it->second; // sax->string() takes a reference
  10103. if (JSON_HEDLEY_UNLIKELY(!sax->key(key) || !sax->string(type)))
  10104. {
  10105. return false;
  10106. }
  10107. if (size_and_type.second == 'C')
  10108. {
  10109. size_and_type.second = 'U';
  10110. }
  10111. key = "_ArrayData_";
  10112. if (JSON_HEDLEY_UNLIKELY(!sax->key(key) || !sax->start_array(size_and_type.first) ))
  10113. {
  10114. return false;
  10115. }
  10116. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10117. {
  10118. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
  10119. {
  10120. return false;
  10121. }
  10122. }
  10123. return (sax->end_array() && sax->end_object());
  10124. }
  10125. if (size_and_type.first != npos)
  10126. {
  10127. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(size_and_type.first)))
  10128. {
  10129. return false;
  10130. }
  10131. if (size_and_type.second != 0)
  10132. {
  10133. if (size_and_type.second != 'N')
  10134. {
  10135. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10136. {
  10137. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
  10138. {
  10139. return false;
  10140. }
  10141. }
  10142. }
  10143. }
  10144. else
  10145. {
  10146. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10147. {
  10148. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
  10149. {
  10150. return false;
  10151. }
  10152. }
  10153. }
  10154. }
  10155. else
  10156. {
  10157. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
  10158. {
  10159. return false;
  10160. }
  10161. while (current != ']')
  10162. {
  10163. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal(false)))
  10164. {
  10165. return false;
  10166. }
  10167. get_ignore_noop();
  10168. }
  10169. }
  10170. return sax->end_array();
  10171. }
  10172. /*!
  10173. @return whether object creation completed
  10174. */
  10175. bool get_ubjson_object()
  10176. {
  10177. std::pair<std::size_t, char_int_type> size_and_type;
  10178. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type)))
  10179. {
  10180. return false;
  10181. }
  10182. // do not accept ND-array size in objects in BJData
  10183. if (input_format == input_format_t::bjdata && size_and_type.first != npos && (size_and_type.second & (1 << 8)) != 0)
  10184. {
  10185. auto last_token = get_token_string();
  10186. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  10187. exception_message(input_format, "BJData object does not support ND-array size in optimized format", "object"), nullptr));
  10188. }
  10189. string_t key;
  10190. if (size_and_type.first != npos)
  10191. {
  10192. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(size_and_type.first)))
  10193. {
  10194. return false;
  10195. }
  10196. if (size_and_type.second != 0)
  10197. {
  10198. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10199. {
  10200. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
  10201. {
  10202. return false;
  10203. }
  10204. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
  10205. {
  10206. return false;
  10207. }
  10208. key.clear();
  10209. }
  10210. }
  10211. else
  10212. {
  10213. for (std::size_t i = 0; i < size_and_type.first; ++i)
  10214. {
  10215. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
  10216. {
  10217. return false;
  10218. }
  10219. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
  10220. {
  10221. return false;
  10222. }
  10223. key.clear();
  10224. }
  10225. }
  10226. }
  10227. else
  10228. {
  10229. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast<std::size_t>(-1))))
  10230. {
  10231. return false;
  10232. }
  10233. while (current != '}')
  10234. {
  10235. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key, false) || !sax->key(key)))
  10236. {
  10237. return false;
  10238. }
  10239. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
  10240. {
  10241. return false;
  10242. }
  10243. get_ignore_noop();
  10244. key.clear();
  10245. }
  10246. }
  10247. return sax->end_object();
  10248. }
  10249. // Note, no reader for UBJSON binary types is implemented because they do
  10250. // not exist
  10251. bool get_ubjson_high_precision_number()
  10252. {
  10253. // get size of following number string
  10254. std::size_t size{};
  10255. bool no_ndarray = true;
  10256. auto res = get_ubjson_size_value(size, no_ndarray);
  10257. if (JSON_HEDLEY_UNLIKELY(!res))
  10258. {
  10259. return res;
  10260. }
  10261. // get number string
  10262. std::vector<char> number_vector;
  10263. for (std::size_t i = 0; i < size; ++i)
  10264. {
  10265. get();
  10266. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
  10267. {
  10268. return false;
  10269. }
  10270. number_vector.push_back(static_cast<char>(current));
  10271. }
  10272. // parse number string
  10273. using ia_type = decltype(detail::input_adapter(number_vector));
  10274. auto number_lexer = detail::lexer<BasicJsonType, ia_type>(detail::input_adapter(number_vector), false);
  10275. const auto result_number = number_lexer.scan();
  10276. const auto number_string = number_lexer.get_token_string();
  10277. const auto result_remainder = number_lexer.scan();
  10278. using token_type = typename detail::lexer_base<BasicJsonType>::token_type;
  10279. if (JSON_HEDLEY_UNLIKELY(result_remainder != token_type::end_of_input))
  10280. {
  10281. return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read,
  10282. exception_message(input_format, concat("invalid number text: ", number_lexer.get_token_string()), "high-precision number"), nullptr));
  10283. }
  10284. switch (result_number)
  10285. {
  10286. case token_type::value_integer:
  10287. return sax->number_integer(number_lexer.get_number_integer());
  10288. case token_type::value_unsigned:
  10289. return sax->number_unsigned(number_lexer.get_number_unsigned());
  10290. case token_type::value_float:
  10291. return sax->number_float(number_lexer.get_number_float(), std::move(number_string));
  10292. case token_type::uninitialized:
  10293. case token_type::literal_true:
  10294. case token_type::literal_false:
  10295. case token_type::literal_null:
  10296. case token_type::value_string:
  10297. case token_type::begin_array:
  10298. case token_type::begin_object:
  10299. case token_type::end_array:
  10300. case token_type::end_object:
  10301. case token_type::name_separator:
  10302. case token_type::value_separator:
  10303. case token_type::parse_error:
  10304. case token_type::end_of_input:
  10305. case token_type::literal_or_value:
  10306. default:
  10307. return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read,
  10308. exception_message(input_format, concat("invalid number text: ", number_lexer.get_token_string()), "high-precision number"), nullptr));
  10309. }
  10310. }
  10311. ///////////////////////
  10312. // Utility functions //
  10313. ///////////////////////
  10314. /*!
  10315. @brief get next character from the input
  10316. This function provides the interface to the used input adapter. It does
  10317. not throw in case the input reached EOF, but returns a -'ve valued
  10318. `std::char_traits<char_type>::eof()` in that case.
  10319. @return character read from the input
  10320. */
  10321. char_int_type get()
  10322. {
  10323. ++chars_read;
  10324. return current = ia.get_character();
  10325. }
  10326. /*!
  10327. @return character read from the input after ignoring all 'N' entries
  10328. */
  10329. char_int_type get_ignore_noop()
  10330. {
  10331. do
  10332. {
  10333. get();
  10334. }
  10335. while (current == 'N');
  10336. return current;
  10337. }
  10338. /*
  10339. @brief read a number from the input
  10340. @tparam NumberType the type of the number
  10341. @param[in] format the current format (for diagnostics)
  10342. @param[out] result number of type @a NumberType
  10343. @return whether conversion completed
  10344. @note This function needs to respect the system's endianness, because
  10345. bytes in CBOR, MessagePack, and UBJSON are stored in network order
  10346. (big endian) and therefore need reordering on little endian systems.
  10347. On the other hand, BSON and BJData use little endian and should reorder
  10348. on big endian systems.
  10349. */
  10350. template<typename NumberType, bool InputIsLittleEndian = false>
  10351. bool get_number(const input_format_t format, NumberType& result)
  10352. {
  10353. // step 1: read input into array with system's byte order
  10354. std::array<std::uint8_t, sizeof(NumberType)> vec{};
  10355. for (std::size_t i = 0; i < sizeof(NumberType); ++i)
  10356. {
  10357. get();
  10358. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "number")))
  10359. {
  10360. return false;
  10361. }
  10362. // reverse byte order prior to conversion if necessary
  10363. if (is_little_endian != (InputIsLittleEndian || format == input_format_t::bjdata))
  10364. {
  10365. vec[sizeof(NumberType) - i - 1] = static_cast<std::uint8_t>(current);
  10366. }
  10367. else
  10368. {
  10369. vec[i] = static_cast<std::uint8_t>(current); // LCOV_EXCL_LINE
  10370. }
  10371. }
  10372. // step 2: convert array into number of type T and return
  10373. std::memcpy(&result, vec.data(), sizeof(NumberType));
  10374. return true;
  10375. }
  10376. /*!
  10377. @brief create a string by reading characters from the input
  10378. @tparam NumberType the type of the number
  10379. @param[in] format the current format (for diagnostics)
  10380. @param[in] len number of characters to read
  10381. @param[out] result string created by reading @a len bytes
  10382. @return whether string creation completed
  10383. @note We can not reserve @a len bytes for the result, because @a len
  10384. may be too large. Usually, @ref unexpect_eof() detects the end of
  10385. the input before we run out of string memory.
  10386. */
  10387. template<typename NumberType>
  10388. bool get_string(const input_format_t format,
  10389. const NumberType len,
  10390. string_t& result)
  10391. {
  10392. bool success = true;
  10393. for (NumberType i = 0; i < len; i++)
  10394. {
  10395. get();
  10396. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "string")))
  10397. {
  10398. success = false;
  10399. break;
  10400. }
  10401. result.push_back(static_cast<typename string_t::value_type>(current));
  10402. }
  10403. return success;
  10404. }
  10405. /*!
  10406. @brief create a byte array by reading bytes from the input
  10407. @tparam NumberType the type of the number
  10408. @param[in] format the current format (for diagnostics)
  10409. @param[in] len number of bytes to read
  10410. @param[out] result byte array created by reading @a len bytes
  10411. @return whether byte array creation completed
  10412. @note We can not reserve @a len bytes for the result, because @a len
  10413. may be too large. Usually, @ref unexpect_eof() detects the end of
  10414. the input before we run out of memory.
  10415. */
  10416. template<typename NumberType>
  10417. bool get_binary(const input_format_t format,
  10418. const NumberType len,
  10419. binary_t& result)
  10420. {
  10421. bool success = true;
  10422. for (NumberType i = 0; i < len; i++)
  10423. {
  10424. get();
  10425. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "binary")))
  10426. {
  10427. success = false;
  10428. break;
  10429. }
  10430. result.push_back(static_cast<std::uint8_t>(current));
  10431. }
  10432. return success;
  10433. }
  10434. /*!
  10435. @param[in] format the current format (for diagnostics)
  10436. @param[in] context further context information (for diagnostics)
  10437. @return whether the last read character is not EOF
  10438. */
  10439. JSON_HEDLEY_NON_NULL(3)
  10440. bool unexpect_eof(const input_format_t format, const char* context) const
  10441. {
  10442. if (JSON_HEDLEY_UNLIKELY(current == std::char_traits<char_type>::eof()))
  10443. {
  10444. return sax->parse_error(chars_read, "<end of file>",
  10445. parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), nullptr));
  10446. }
  10447. return true;
  10448. }
  10449. /*!
  10450. @return a string representation of the last read byte
  10451. */
  10452. std::string get_token_string() const
  10453. {
  10454. std::array<char, 3> cr{{}};
  10455. static_cast<void>((std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(current))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  10456. return std::string{cr.data()};
  10457. }
  10458. /*!
  10459. @param[in] format the current format
  10460. @param[in] detail a detailed error message
  10461. @param[in] context further context information
  10462. @return a message string to use in the parse_error exceptions
  10463. */
  10464. std::string exception_message(const input_format_t format,
  10465. const std::string& detail,
  10466. const std::string& context) const
  10467. {
  10468. std::string error_msg = "syntax error while parsing ";
  10469. switch (format)
  10470. {
  10471. case input_format_t::cbor:
  10472. error_msg += "CBOR";
  10473. break;
  10474. case input_format_t::msgpack:
  10475. error_msg += "MessagePack";
  10476. break;
  10477. case input_format_t::ubjson:
  10478. error_msg += "UBJSON";
  10479. break;
  10480. case input_format_t::bson:
  10481. error_msg += "BSON";
  10482. break;
  10483. case input_format_t::bjdata:
  10484. error_msg += "BJData";
  10485. break;
  10486. case input_format_t::json: // LCOV_EXCL_LINE
  10487. default: // LCOV_EXCL_LINE
  10488. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  10489. }
  10490. return concat(error_msg, ' ', context, ": ", detail);
  10491. }
  10492. private:
  10493. static JSON_INLINE_VARIABLE constexpr std::size_t npos = static_cast<std::size_t>(-1);
  10494. /// input adapter
  10495. InputAdapterType ia;
  10496. /// the current character
  10497. char_int_type current = std::char_traits<char_type>::eof();
  10498. /// the number of characters read
  10499. std::size_t chars_read = 0;
  10500. /// whether we can assume little endianness
  10501. const bool is_little_endian = little_endianness();
  10502. /// input format
  10503. const input_format_t input_format = input_format_t::json;
  10504. /// the SAX parser
  10505. json_sax_t* sax = nullptr;
  10506. // excluded markers in bjdata optimized type
  10507. #define JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_ \
  10508. make_array<char_int_type>('F', 'H', 'N', 'S', 'T', 'Z', '[', '{')
  10509. #define JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_ \
  10510. make_array<bjd_type>( \
  10511. bjd_type{'C', "char"}, \
  10512. bjd_type{'D', "double"}, \
  10513. bjd_type{'I', "int16"}, \
  10514. bjd_type{'L', "int64"}, \
  10515. bjd_type{'M', "uint64"}, \
  10516. bjd_type{'U', "uint8"}, \
  10517. bjd_type{'d', "single"}, \
  10518. bjd_type{'i', "int8"}, \
  10519. bjd_type{'l', "int32"}, \
  10520. bjd_type{'m', "uint32"}, \
  10521. bjd_type{'u', "uint16"})
  10522. JSON_PRIVATE_UNLESS_TESTED:
  10523. // lookup tables
  10524. // NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
  10525. const decltype(JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_) bjd_optimized_type_markers =
  10526. JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_;
  10527. using bjd_type = std::pair<char_int_type, string_t>;
  10528. // NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
  10529. const decltype(JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_) bjd_types_map =
  10530. JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_;
  10531. #undef JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_
  10532. #undef JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_
  10533. };
  10534. #ifndef JSON_HAS_CPP_17
  10535. template<typename BasicJsonType, typename InputAdapterType, typename SAX>
  10536. constexpr std::size_t binary_reader<BasicJsonType, InputAdapterType, SAX>::npos;
  10537. #endif
  10538. } // namespace detail
  10539. NLOHMANN_JSON_NAMESPACE_END
  10540. // #include <nlohmann/detail/input/input_adapters.hpp>
  10541. // #include <nlohmann/detail/input/lexer.hpp>
  10542. // #include <nlohmann/detail/input/parser.hpp>
  10543. // __ _____ _____ _____
  10544. // __| | __| | | | JSON for Modern C++
  10545. // | | |__ | | | | | | version 3.11.2
  10546. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  10547. //
  10548. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  10549. // SPDX-License-Identifier: MIT
  10550. #include <cmath> // isfinite
  10551. #include <cstdint> // uint8_t
  10552. #include <functional> // function
  10553. #include <string> // string
  10554. #include <utility> // move
  10555. #include <vector> // vector
  10556. // #include <nlohmann/detail/exceptions.hpp>
  10557. // #include <nlohmann/detail/input/input_adapters.hpp>
  10558. // #include <nlohmann/detail/input/json_sax.hpp>
  10559. // #include <nlohmann/detail/input/lexer.hpp>
  10560. // #include <nlohmann/detail/macro_scope.hpp>
  10561. // #include <nlohmann/detail/meta/is_sax.hpp>
  10562. // #include <nlohmann/detail/string_concat.hpp>
  10563. // #include <nlohmann/detail/value_t.hpp>
  10564. NLOHMANN_JSON_NAMESPACE_BEGIN
  10565. namespace detail
  10566. {
  10567. ////////////
  10568. // parser //
  10569. ////////////
  10570. enum class parse_event_t : std::uint8_t
  10571. {
  10572. /// the parser read `{` and started to process a JSON object
  10573. object_start,
  10574. /// the parser read `}` and finished processing a JSON object
  10575. object_end,
  10576. /// the parser read `[` and started to process a JSON array
  10577. array_start,
  10578. /// the parser read `]` and finished processing a JSON array
  10579. array_end,
  10580. /// the parser read a key of a value in an object
  10581. key,
  10582. /// the parser finished reading a JSON value
  10583. value
  10584. };
  10585. template<typename BasicJsonType>
  10586. using parser_callback_t =
  10587. std::function<bool(int /*depth*/, parse_event_t /*event*/, BasicJsonType& /*parsed*/)>;
  10588. /*!
  10589. @brief syntax analysis
  10590. This class implements a recursive descent parser.
  10591. */
  10592. template<typename BasicJsonType, typename InputAdapterType>
  10593. class parser
  10594. {
  10595. using number_integer_t = typename BasicJsonType::number_integer_t;
  10596. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  10597. using number_float_t = typename BasicJsonType::number_float_t;
  10598. using string_t = typename BasicJsonType::string_t;
  10599. using lexer_t = lexer<BasicJsonType, InputAdapterType>;
  10600. using token_type = typename lexer_t::token_type;
  10601. public:
  10602. /// a parser reading from an input adapter
  10603. explicit parser(InputAdapterType&& adapter,
  10604. const parser_callback_t<BasicJsonType> cb = nullptr,
  10605. const bool allow_exceptions_ = true,
  10606. const bool skip_comments = false)
  10607. : callback(cb)
  10608. , m_lexer(std::move(adapter), skip_comments)
  10609. , allow_exceptions(allow_exceptions_)
  10610. {
  10611. // read first token
  10612. get_token();
  10613. }
  10614. /*!
  10615. @brief public parser interface
  10616. @param[in] strict whether to expect the last token to be EOF
  10617. @param[in,out] result parsed JSON value
  10618. @throw parse_error.101 in case of an unexpected token
  10619. @throw parse_error.102 if to_unicode fails or surrogate error
  10620. @throw parse_error.103 if to_unicode fails
  10621. */
  10622. void parse(const bool strict, BasicJsonType& result)
  10623. {
  10624. if (callback)
  10625. {
  10626. json_sax_dom_callback_parser<BasicJsonType> sdp(result, callback, allow_exceptions);
  10627. sax_parse_internal(&sdp);
  10628. // in strict mode, input must be completely read
  10629. if (strict && (get_token() != token_type::end_of_input))
  10630. {
  10631. sdp.parse_error(m_lexer.get_position(),
  10632. m_lexer.get_token_string(),
  10633. parse_error::create(101, m_lexer.get_position(),
  10634. exception_message(token_type::end_of_input, "value"), nullptr));
  10635. }
  10636. // in case of an error, return discarded value
  10637. if (sdp.is_errored())
  10638. {
  10639. result = value_t::discarded;
  10640. return;
  10641. }
  10642. // set top-level value to null if it was discarded by the callback
  10643. // function
  10644. if (result.is_discarded())
  10645. {
  10646. result = nullptr;
  10647. }
  10648. }
  10649. else
  10650. {
  10651. json_sax_dom_parser<BasicJsonType> sdp(result, allow_exceptions);
  10652. sax_parse_internal(&sdp);
  10653. // in strict mode, input must be completely read
  10654. if (strict && (get_token() != token_type::end_of_input))
  10655. {
  10656. sdp.parse_error(m_lexer.get_position(),
  10657. m_lexer.get_token_string(),
  10658. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), nullptr));
  10659. }
  10660. // in case of an error, return discarded value
  10661. if (sdp.is_errored())
  10662. {
  10663. result = value_t::discarded;
  10664. return;
  10665. }
  10666. }
  10667. result.assert_invariant();
  10668. }
  10669. /*!
  10670. @brief public accept interface
  10671. @param[in] strict whether to expect the last token to be EOF
  10672. @return whether the input is a proper JSON text
  10673. */
  10674. bool accept(const bool strict = true)
  10675. {
  10676. json_sax_acceptor<BasicJsonType> sax_acceptor;
  10677. return sax_parse(&sax_acceptor, strict);
  10678. }
  10679. template<typename SAX>
  10680. JSON_HEDLEY_NON_NULL(2)
  10681. bool sax_parse(SAX* sax, const bool strict = true)
  10682. {
  10683. (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
  10684. const bool result = sax_parse_internal(sax);
  10685. // strict mode: next byte must be EOF
  10686. if (result && strict && (get_token() != token_type::end_of_input))
  10687. {
  10688. return sax->parse_error(m_lexer.get_position(),
  10689. m_lexer.get_token_string(),
  10690. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), nullptr));
  10691. }
  10692. return result;
  10693. }
  10694. private:
  10695. template<typename SAX>
  10696. JSON_HEDLEY_NON_NULL(2)
  10697. bool sax_parse_internal(SAX* sax)
  10698. {
  10699. // stack to remember the hierarchy of structured values we are parsing
  10700. // true = array; false = object
  10701. std::vector<bool> states;
  10702. // value to avoid a goto (see comment where set to true)
  10703. bool skip_to_state_evaluation = false;
  10704. while (true)
  10705. {
  10706. if (!skip_to_state_evaluation)
  10707. {
  10708. // invariant: get_token() was called before each iteration
  10709. switch (last_token)
  10710. {
  10711. case token_type::begin_object:
  10712. {
  10713. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast<std::size_t>(-1))))
  10714. {
  10715. return false;
  10716. }
  10717. // closing } -> we are done
  10718. if (get_token() == token_type::end_object)
  10719. {
  10720. if (JSON_HEDLEY_UNLIKELY(!sax->end_object()))
  10721. {
  10722. return false;
  10723. }
  10724. break;
  10725. }
  10726. // parse key
  10727. if (JSON_HEDLEY_UNLIKELY(last_token != token_type::value_string))
  10728. {
  10729. return sax->parse_error(m_lexer.get_position(),
  10730. m_lexer.get_token_string(),
  10731. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), nullptr));
  10732. }
  10733. if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string())))
  10734. {
  10735. return false;
  10736. }
  10737. // parse separator (:)
  10738. if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
  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::name_separator, "object separator"), nullptr));
  10743. }
  10744. // remember we are now inside an object
  10745. states.push_back(false);
  10746. // parse values
  10747. get_token();
  10748. continue;
  10749. }
  10750. case token_type::begin_array:
  10751. {
  10752. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
  10753. {
  10754. return false;
  10755. }
  10756. // closing ] -> we are done
  10757. if (get_token() == token_type::end_array)
  10758. {
  10759. if (JSON_HEDLEY_UNLIKELY(!sax->end_array()))
  10760. {
  10761. return false;
  10762. }
  10763. break;
  10764. }
  10765. // remember we are now inside an array
  10766. states.push_back(true);
  10767. // parse values (no need to call get_token)
  10768. continue;
  10769. }
  10770. case token_type::value_float:
  10771. {
  10772. const auto res = m_lexer.get_number_float();
  10773. if (JSON_HEDLEY_UNLIKELY(!std::isfinite(res)))
  10774. {
  10775. return sax->parse_error(m_lexer.get_position(),
  10776. m_lexer.get_token_string(),
  10777. out_of_range::create(406, concat("number overflow parsing '", m_lexer.get_token_string(), '\''), nullptr));
  10778. }
  10779. if (JSON_HEDLEY_UNLIKELY(!sax->number_float(res, m_lexer.get_string())))
  10780. {
  10781. return false;
  10782. }
  10783. break;
  10784. }
  10785. case token_type::literal_false:
  10786. {
  10787. if (JSON_HEDLEY_UNLIKELY(!sax->boolean(false)))
  10788. {
  10789. return false;
  10790. }
  10791. break;
  10792. }
  10793. case token_type::literal_null:
  10794. {
  10795. if (JSON_HEDLEY_UNLIKELY(!sax->null()))
  10796. {
  10797. return false;
  10798. }
  10799. break;
  10800. }
  10801. case token_type::literal_true:
  10802. {
  10803. if (JSON_HEDLEY_UNLIKELY(!sax->boolean(true)))
  10804. {
  10805. return false;
  10806. }
  10807. break;
  10808. }
  10809. case token_type::value_integer:
  10810. {
  10811. if (JSON_HEDLEY_UNLIKELY(!sax->number_integer(m_lexer.get_number_integer())))
  10812. {
  10813. return false;
  10814. }
  10815. break;
  10816. }
  10817. case token_type::value_string:
  10818. {
  10819. if (JSON_HEDLEY_UNLIKELY(!sax->string(m_lexer.get_string())))
  10820. {
  10821. return false;
  10822. }
  10823. break;
  10824. }
  10825. case token_type::value_unsigned:
  10826. {
  10827. if (JSON_HEDLEY_UNLIKELY(!sax->number_unsigned(m_lexer.get_number_unsigned())))
  10828. {
  10829. return false;
  10830. }
  10831. break;
  10832. }
  10833. case token_type::parse_error:
  10834. {
  10835. // using "uninitialized" to avoid "expected" message
  10836. return sax->parse_error(m_lexer.get_position(),
  10837. m_lexer.get_token_string(),
  10838. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::uninitialized, "value"), nullptr));
  10839. }
  10840. case token_type::uninitialized:
  10841. case token_type::end_array:
  10842. case token_type::end_object:
  10843. case token_type::name_separator:
  10844. case token_type::value_separator:
  10845. case token_type::end_of_input:
  10846. case token_type::literal_or_value:
  10847. default: // the last token was unexpected
  10848. {
  10849. return sax->parse_error(m_lexer.get_position(),
  10850. m_lexer.get_token_string(),
  10851. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::literal_or_value, "value"), nullptr));
  10852. }
  10853. }
  10854. }
  10855. else
  10856. {
  10857. skip_to_state_evaluation = false;
  10858. }
  10859. // we reached this line after we successfully parsed a value
  10860. if (states.empty())
  10861. {
  10862. // empty stack: we reached the end of the hierarchy: done
  10863. return true;
  10864. }
  10865. if (states.back()) // array
  10866. {
  10867. // comma -> next value
  10868. if (get_token() == token_type::value_separator)
  10869. {
  10870. // parse a new value
  10871. get_token();
  10872. continue;
  10873. }
  10874. // closing ]
  10875. if (JSON_HEDLEY_LIKELY(last_token == token_type::end_array))
  10876. {
  10877. if (JSON_HEDLEY_UNLIKELY(!sax->end_array()))
  10878. {
  10879. return false;
  10880. }
  10881. // We are done with this array. Before we can parse a
  10882. // new value, we need to evaluate the new state first.
  10883. // By setting skip_to_state_evaluation to false, we
  10884. // are effectively jumping to the beginning of this if.
  10885. JSON_ASSERT(!states.empty());
  10886. states.pop_back();
  10887. skip_to_state_evaluation = true;
  10888. continue;
  10889. }
  10890. return sax->parse_error(m_lexer.get_position(),
  10891. m_lexer.get_token_string(),
  10892. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_array, "array"), nullptr));
  10893. }
  10894. // states.back() is false -> object
  10895. // comma -> next value
  10896. if (get_token() == token_type::value_separator)
  10897. {
  10898. // parse key
  10899. if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::value_string))
  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::value_string, "object key"), nullptr));
  10904. }
  10905. if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string())))
  10906. {
  10907. return false;
  10908. }
  10909. // parse separator (:)
  10910. if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
  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::name_separator, "object separator"), nullptr));
  10915. }
  10916. // parse values
  10917. get_token();
  10918. continue;
  10919. }
  10920. // closing }
  10921. if (JSON_HEDLEY_LIKELY(last_token == token_type::end_object))
  10922. {
  10923. if (JSON_HEDLEY_UNLIKELY(!sax->end_object()))
  10924. {
  10925. return false;
  10926. }
  10927. // We are done with this object. Before we can parse a
  10928. // new value, we need to evaluate the new state first.
  10929. // By setting skip_to_state_evaluation to false, we
  10930. // are effectively jumping to the beginning of this if.
  10931. JSON_ASSERT(!states.empty());
  10932. states.pop_back();
  10933. skip_to_state_evaluation = true;
  10934. continue;
  10935. }
  10936. return sax->parse_error(m_lexer.get_position(),
  10937. m_lexer.get_token_string(),
  10938. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_object, "object"), nullptr));
  10939. }
  10940. }
  10941. /// get next token from lexer
  10942. token_type get_token()
  10943. {
  10944. return last_token = m_lexer.scan();
  10945. }
  10946. std::string exception_message(const token_type expected, const std::string& context)
  10947. {
  10948. std::string error_msg = "syntax error ";
  10949. if (!context.empty())
  10950. {
  10951. error_msg += concat("while parsing ", context, ' ');
  10952. }
  10953. error_msg += "- ";
  10954. if (last_token == token_type::parse_error)
  10955. {
  10956. error_msg += concat(m_lexer.get_error_message(), "; last read: '",
  10957. m_lexer.get_token_string(), '\'');
  10958. }
  10959. else
  10960. {
  10961. error_msg += concat("unexpected ", lexer_t::token_type_name(last_token));
  10962. }
  10963. if (expected != token_type::uninitialized)
  10964. {
  10965. error_msg += concat("; expected ", lexer_t::token_type_name(expected));
  10966. }
  10967. return error_msg;
  10968. }
  10969. private:
  10970. /// callback function
  10971. const parser_callback_t<BasicJsonType> callback = nullptr;
  10972. /// the type of the last read token
  10973. token_type last_token = token_type::uninitialized;
  10974. /// the lexer
  10975. lexer_t m_lexer;
  10976. /// whether to throw exceptions in case of errors
  10977. const bool allow_exceptions = true;
  10978. };
  10979. } // namespace detail
  10980. NLOHMANN_JSON_NAMESPACE_END
  10981. // #include <nlohmann/detail/iterators/internal_iterator.hpp>
  10982. // __ _____ _____ _____
  10983. // __| | __| | | | JSON for Modern C++
  10984. // | | |__ | | | | | | version 3.11.2
  10985. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  10986. //
  10987. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  10988. // SPDX-License-Identifier: MIT
  10989. // #include <nlohmann/detail/abi_macros.hpp>
  10990. // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
  10991. // __ _____ _____ _____
  10992. // __| | __| | | | JSON for Modern C++
  10993. // | | |__ | | | | | | version 3.11.2
  10994. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  10995. //
  10996. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  10997. // SPDX-License-Identifier: MIT
  10998. #include <cstddef> // ptrdiff_t
  10999. #include <limits> // numeric_limits
  11000. // #include <nlohmann/detail/macro_scope.hpp>
  11001. NLOHMANN_JSON_NAMESPACE_BEGIN
  11002. namespace detail
  11003. {
  11004. /*
  11005. @brief an iterator for primitive JSON types
  11006. This class models an iterator for primitive JSON types (boolean, number,
  11007. string). It's only purpose is to allow the iterator/const_iterator classes
  11008. to "iterate" over primitive values. Internally, the iterator is modeled by
  11009. a `difference_type` variable. Value begin_value (`0`) models the begin,
  11010. end_value (`1`) models past the end.
  11011. */
  11012. class primitive_iterator_t
  11013. {
  11014. private:
  11015. using difference_type = std::ptrdiff_t;
  11016. static constexpr difference_type begin_value = 0;
  11017. static constexpr difference_type end_value = begin_value + 1;
  11018. JSON_PRIVATE_UNLESS_TESTED:
  11019. /// iterator as signed integer type
  11020. difference_type m_it = (std::numeric_limits<std::ptrdiff_t>::min)();
  11021. public:
  11022. constexpr difference_type get_value() const noexcept
  11023. {
  11024. return m_it;
  11025. }
  11026. /// set iterator to a defined beginning
  11027. void set_begin() noexcept
  11028. {
  11029. m_it = begin_value;
  11030. }
  11031. /// set iterator to a defined past the end
  11032. void set_end() noexcept
  11033. {
  11034. m_it = end_value;
  11035. }
  11036. /// return whether the iterator can be dereferenced
  11037. constexpr bool is_begin() const noexcept
  11038. {
  11039. return m_it == begin_value;
  11040. }
  11041. /// return whether the iterator is at end
  11042. constexpr bool is_end() const noexcept
  11043. {
  11044. return m_it == end_value;
  11045. }
  11046. friend constexpr bool operator==(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
  11047. {
  11048. return lhs.m_it == rhs.m_it;
  11049. }
  11050. friend constexpr bool operator<(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
  11051. {
  11052. return lhs.m_it < rhs.m_it;
  11053. }
  11054. primitive_iterator_t operator+(difference_type n) noexcept
  11055. {
  11056. auto result = *this;
  11057. result += n;
  11058. return result;
  11059. }
  11060. friend constexpr difference_type operator-(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
  11061. {
  11062. return lhs.m_it - rhs.m_it;
  11063. }
  11064. primitive_iterator_t& operator++() noexcept
  11065. {
  11066. ++m_it;
  11067. return *this;
  11068. }
  11069. primitive_iterator_t operator++(int)& noexcept // NOLINT(cert-dcl21-cpp)
  11070. {
  11071. auto result = *this;
  11072. ++m_it;
  11073. return result;
  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+=(difference_type n) noexcept
  11087. {
  11088. m_it += n;
  11089. return *this;
  11090. }
  11091. primitive_iterator_t& operator-=(difference_type n) noexcept
  11092. {
  11093. m_it -= n;
  11094. return *this;
  11095. }
  11096. };
  11097. } // namespace detail
  11098. NLOHMANN_JSON_NAMESPACE_END
  11099. NLOHMANN_JSON_NAMESPACE_BEGIN
  11100. namespace detail
  11101. {
  11102. /*!
  11103. @brief an iterator value
  11104. @note This structure could easily be a union, but MSVC currently does not allow
  11105. unions members with complex constructors, see https://github.com/nlohmann/json/pull/105.
  11106. */
  11107. template<typename BasicJsonType> struct internal_iterator
  11108. {
  11109. /// iterator for JSON objects
  11110. typename BasicJsonType::object_t::iterator object_iterator {};
  11111. /// iterator for JSON arrays
  11112. typename BasicJsonType::array_t::iterator array_iterator {};
  11113. /// generic iterator for all other types
  11114. primitive_iterator_t primitive_iterator {};
  11115. };
  11116. } // namespace detail
  11117. NLOHMANN_JSON_NAMESPACE_END
  11118. // #include <nlohmann/detail/iterators/iter_impl.hpp>
  11119. // __ _____ _____ _____
  11120. // __| | __| | | | JSON for Modern C++
  11121. // | | |__ | | | | | | version 3.11.2
  11122. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  11123. //
  11124. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  11125. // SPDX-License-Identifier: MIT
  11126. #include <iterator> // iterator, random_access_iterator_tag, bidirectional_iterator_tag, advance, next
  11127. #include <type_traits> // conditional, is_const, remove_const
  11128. // #include <nlohmann/detail/exceptions.hpp>
  11129. // #include <nlohmann/detail/iterators/internal_iterator.hpp>
  11130. // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
  11131. // #include <nlohmann/detail/macro_scope.hpp>
  11132. // #include <nlohmann/detail/meta/cpp_future.hpp>
  11133. // #include <nlohmann/detail/meta/type_traits.hpp>
  11134. // #include <nlohmann/detail/value_t.hpp>
  11135. NLOHMANN_JSON_NAMESPACE_BEGIN
  11136. namespace detail
  11137. {
  11138. // forward declare, to be able to friend it later on
  11139. template<typename IteratorType> class iteration_proxy;
  11140. template<typename IteratorType> class iteration_proxy_value;
  11141. /*!
  11142. @brief a template for a bidirectional iterator for the @ref basic_json class
  11143. This class implements a both iterators (iterator and const_iterator) for the
  11144. @ref basic_json class.
  11145. @note An iterator is called *initialized* when a pointer to a JSON value has
  11146. been set (e.g., by a constructor or a copy assignment). If the iterator is
  11147. default-constructed, it is *uninitialized* and most methods are undefined.
  11148. **The library uses assertions to detect calls on uninitialized iterators.**
  11149. @requirement The class satisfies the following concept requirements:
  11150. -
  11151. [BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
  11152. The iterator that can be moved can be moved in both directions (i.e.
  11153. incremented and decremented).
  11154. @since version 1.0.0, simplified in version 2.0.9, change to bidirectional
  11155. iterators in version 3.0.0 (see https://github.com/nlohmann/json/issues/593)
  11156. */
  11157. template<typename BasicJsonType>
  11158. class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions)
  11159. {
  11160. /// the iterator with BasicJsonType of different const-ness
  11161. using other_iter_impl = iter_impl<typename std::conditional<std::is_const<BasicJsonType>::value, typename std::remove_const<BasicJsonType>::type, const BasicJsonType>::type>;
  11162. /// allow basic_json to access private members
  11163. friend other_iter_impl;
  11164. friend BasicJsonType;
  11165. friend iteration_proxy<iter_impl>;
  11166. friend iteration_proxy_value<iter_impl>;
  11167. using object_t = typename BasicJsonType::object_t;
  11168. using array_t = typename BasicJsonType::array_t;
  11169. // make sure BasicJsonType is basic_json or const basic_json
  11170. static_assert(is_basic_json<typename std::remove_const<BasicJsonType>::type>::value,
  11171. "iter_impl only accepts (const) basic_json");
  11172. // superficial check for the LegacyBidirectionalIterator named requirement
  11173. static_assert(std::is_base_of<std::bidirectional_iterator_tag, std::bidirectional_iterator_tag>::value
  11174. && std::is_base_of<std::bidirectional_iterator_tag, typename std::iterator_traits<typename array_t::iterator>::iterator_category>::value,
  11175. "basic_json iterator assumes array and object type iterators satisfy the LegacyBidirectionalIterator named requirement.");
  11176. public:
  11177. /// The std::iterator class template (used as a base class to provide typedefs) is deprecated in C++17.
  11178. /// The C++ Standard has never required user-defined iterators to derive from std::iterator.
  11179. /// A user-defined iterator should provide publicly accessible typedefs named
  11180. /// iterator_category, value_type, difference_type, pointer, and reference.
  11181. /// Note that value_type is required to be non-const, even for constant iterators.
  11182. using iterator_category = std::bidirectional_iterator_tag;
  11183. /// the type of the values when the iterator is dereferenced
  11184. using value_type = typename BasicJsonType::value_type;
  11185. /// a type to represent differences between iterators
  11186. using difference_type = typename BasicJsonType::difference_type;
  11187. /// defines a pointer to the type iterated over (value_type)
  11188. using pointer = typename std::conditional<std::is_const<BasicJsonType>::value,
  11189. typename BasicJsonType::const_pointer,
  11190. typename BasicJsonType::pointer>::type;
  11191. /// defines a reference to the type iterated over (value_type)
  11192. using reference =
  11193. typename std::conditional<std::is_const<BasicJsonType>::value,
  11194. typename BasicJsonType::const_reference,
  11195. typename BasicJsonType::reference>::type;
  11196. iter_impl() = default;
  11197. ~iter_impl() = default;
  11198. iter_impl(iter_impl&&) noexcept = default;
  11199. iter_impl& operator=(iter_impl&&) noexcept = default;
  11200. /*!
  11201. @brief constructor for a given JSON instance
  11202. @param[in] object pointer to a JSON object for this iterator
  11203. @pre object != nullptr
  11204. @post The iterator is initialized; i.e. `m_object != nullptr`.
  11205. */
  11206. explicit iter_impl(pointer object) noexcept : m_object(object)
  11207. {
  11208. JSON_ASSERT(m_object != nullptr);
  11209. switch (m_object->m_type)
  11210. {
  11211. case value_t::object:
  11212. {
  11213. m_it.object_iterator = typename object_t::iterator();
  11214. break;
  11215. }
  11216. case value_t::array:
  11217. {
  11218. m_it.array_iterator = typename array_t::iterator();
  11219. break;
  11220. }
  11221. case value_t::null:
  11222. case value_t::string:
  11223. case value_t::boolean:
  11224. case value_t::number_integer:
  11225. case value_t::number_unsigned:
  11226. case value_t::number_float:
  11227. case value_t::binary:
  11228. case value_t::discarded:
  11229. default:
  11230. {
  11231. m_it.primitive_iterator = primitive_iterator_t();
  11232. break;
  11233. }
  11234. }
  11235. }
  11236. /*!
  11237. @note The conventional copy constructor and copy assignment are implicitly
  11238. defined. Combined with the following converting constructor and
  11239. assignment, they support: (1) copy from iterator to iterator, (2)
  11240. copy from const iterator to const iterator, and (3) conversion from
  11241. iterator to const iterator. However conversion from const iterator
  11242. to iterator is not defined.
  11243. */
  11244. /*!
  11245. @brief const copy constructor
  11246. @param[in] other const iterator to copy from
  11247. @note This copy constructor had to be defined explicitly to circumvent a bug
  11248. occurring on msvc v19.0 compiler (VS 2015) debug build. For more
  11249. information refer to: https://github.com/nlohmann/json/issues/1608
  11250. */
  11251. iter_impl(const iter_impl<const BasicJsonType>& other) noexcept
  11252. : m_object(other.m_object), m_it(other.m_it)
  11253. {}
  11254. /*!
  11255. @brief converting assignment
  11256. @param[in] other const iterator to copy from
  11257. @return const/non-const iterator
  11258. @note It is not checked whether @a other is initialized.
  11259. */
  11260. iter_impl& operator=(const iter_impl<const BasicJsonType>& other) noexcept
  11261. {
  11262. if (&other != this)
  11263. {
  11264. m_object = other.m_object;
  11265. m_it = other.m_it;
  11266. }
  11267. return *this;
  11268. }
  11269. /*!
  11270. @brief converting constructor
  11271. @param[in] other non-const iterator to copy from
  11272. @note It is not checked whether @a other is initialized.
  11273. */
  11274. iter_impl(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept
  11275. : m_object(other.m_object), m_it(other.m_it)
  11276. {}
  11277. /*!
  11278. @brief converting assignment
  11279. @param[in] other non-const iterator to copy from
  11280. @return const/non-const iterator
  11281. @note It is not checked whether @a other is initialized.
  11282. */
  11283. iter_impl& operator=(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept // NOLINT(cert-oop54-cpp)
  11284. {
  11285. m_object = other.m_object;
  11286. m_it = other.m_it;
  11287. return *this;
  11288. }
  11289. JSON_PRIVATE_UNLESS_TESTED:
  11290. /*!
  11291. @brief set the iterator to the first value
  11292. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11293. */
  11294. void set_begin() noexcept
  11295. {
  11296. JSON_ASSERT(m_object != nullptr);
  11297. switch (m_object->m_type)
  11298. {
  11299. case value_t::object:
  11300. {
  11301. m_it.object_iterator = m_object->m_value.object->begin();
  11302. break;
  11303. }
  11304. case value_t::array:
  11305. {
  11306. m_it.array_iterator = m_object->m_value.array->begin();
  11307. break;
  11308. }
  11309. case value_t::null:
  11310. {
  11311. // set to end so begin()==end() is true: null is empty
  11312. m_it.primitive_iterator.set_end();
  11313. break;
  11314. }
  11315. case value_t::string:
  11316. case value_t::boolean:
  11317. case value_t::number_integer:
  11318. case value_t::number_unsigned:
  11319. case value_t::number_float:
  11320. case value_t::binary:
  11321. case value_t::discarded:
  11322. default:
  11323. {
  11324. m_it.primitive_iterator.set_begin();
  11325. break;
  11326. }
  11327. }
  11328. }
  11329. /*!
  11330. @brief set the iterator past the last value
  11331. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11332. */
  11333. void set_end() noexcept
  11334. {
  11335. JSON_ASSERT(m_object != nullptr);
  11336. switch (m_object->m_type)
  11337. {
  11338. case value_t::object:
  11339. {
  11340. m_it.object_iterator = m_object->m_value.object->end();
  11341. break;
  11342. }
  11343. case value_t::array:
  11344. {
  11345. m_it.array_iterator = m_object->m_value.array->end();
  11346. break;
  11347. }
  11348. case value_t::null:
  11349. case value_t::string:
  11350. case value_t::boolean:
  11351. case value_t::number_integer:
  11352. case value_t::number_unsigned:
  11353. case value_t::number_float:
  11354. case value_t::binary:
  11355. case value_t::discarded:
  11356. default:
  11357. {
  11358. m_it.primitive_iterator.set_end();
  11359. break;
  11360. }
  11361. }
  11362. }
  11363. public:
  11364. /*!
  11365. @brief return a reference to the value pointed to by the iterator
  11366. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11367. */
  11368. reference operator*() const
  11369. {
  11370. JSON_ASSERT(m_object != nullptr);
  11371. switch (m_object->m_type)
  11372. {
  11373. case value_t::object:
  11374. {
  11375. JSON_ASSERT(m_it.object_iterator != m_object->m_value.object->end());
  11376. return m_it.object_iterator->second;
  11377. }
  11378. case value_t::array:
  11379. {
  11380. JSON_ASSERT(m_it.array_iterator != m_object->m_value.array->end());
  11381. return *m_it.array_iterator;
  11382. }
  11383. case value_t::null:
  11384. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  11385. case value_t::string:
  11386. case value_t::boolean:
  11387. case value_t::number_integer:
  11388. case value_t::number_unsigned:
  11389. case value_t::number_float:
  11390. case value_t::binary:
  11391. case value_t::discarded:
  11392. default:
  11393. {
  11394. if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin()))
  11395. {
  11396. return *m_object;
  11397. }
  11398. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  11399. }
  11400. }
  11401. }
  11402. /*!
  11403. @brief dereference the iterator
  11404. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11405. */
  11406. pointer operator->() const
  11407. {
  11408. JSON_ASSERT(m_object != nullptr);
  11409. switch (m_object->m_type)
  11410. {
  11411. case value_t::object:
  11412. {
  11413. JSON_ASSERT(m_it.object_iterator != m_object->m_value.object->end());
  11414. return &(m_it.object_iterator->second);
  11415. }
  11416. case value_t::array:
  11417. {
  11418. JSON_ASSERT(m_it.array_iterator != m_object->m_value.array->end());
  11419. return &*m_it.array_iterator;
  11420. }
  11421. case value_t::null:
  11422. case value_t::string:
  11423. case value_t::boolean:
  11424. case value_t::number_integer:
  11425. case value_t::number_unsigned:
  11426. case value_t::number_float:
  11427. case value_t::binary:
  11428. case value_t::discarded:
  11429. default:
  11430. {
  11431. if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin()))
  11432. {
  11433. return m_object;
  11434. }
  11435. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  11436. }
  11437. }
  11438. }
  11439. /*!
  11440. @brief post-increment (it++)
  11441. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11442. */
  11443. iter_impl operator++(int)& // NOLINT(cert-dcl21-cpp)
  11444. {
  11445. auto result = *this;
  11446. ++(*this);
  11447. return result;
  11448. }
  11449. /*!
  11450. @brief pre-increment (++it)
  11451. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11452. */
  11453. iter_impl& operator++()
  11454. {
  11455. JSON_ASSERT(m_object != nullptr);
  11456. switch (m_object->m_type)
  11457. {
  11458. case value_t::object:
  11459. {
  11460. std::advance(m_it.object_iterator, 1);
  11461. break;
  11462. }
  11463. case value_t::array:
  11464. {
  11465. std::advance(m_it.array_iterator, 1);
  11466. break;
  11467. }
  11468. case value_t::null:
  11469. case value_t::string:
  11470. case value_t::boolean:
  11471. case value_t::number_integer:
  11472. case value_t::number_unsigned:
  11473. case value_t::number_float:
  11474. case value_t::binary:
  11475. case value_t::discarded:
  11476. default:
  11477. {
  11478. ++m_it.primitive_iterator;
  11479. break;
  11480. }
  11481. }
  11482. return *this;
  11483. }
  11484. /*!
  11485. @brief post-decrement (it--)
  11486. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11487. */
  11488. iter_impl operator--(int)& // NOLINT(cert-dcl21-cpp)
  11489. {
  11490. auto result = *this;
  11491. --(*this);
  11492. return result;
  11493. }
  11494. /*!
  11495. @brief pre-decrement (--it)
  11496. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11497. */
  11498. iter_impl& operator--()
  11499. {
  11500. JSON_ASSERT(m_object != nullptr);
  11501. switch (m_object->m_type)
  11502. {
  11503. case value_t::object:
  11504. {
  11505. std::advance(m_it.object_iterator, -1);
  11506. break;
  11507. }
  11508. case value_t::array:
  11509. {
  11510. std::advance(m_it.array_iterator, -1);
  11511. break;
  11512. }
  11513. case value_t::null:
  11514. case value_t::string:
  11515. case value_t::boolean:
  11516. case value_t::number_integer:
  11517. case value_t::number_unsigned:
  11518. case value_t::number_float:
  11519. case value_t::binary:
  11520. case value_t::discarded:
  11521. default:
  11522. {
  11523. --m_it.primitive_iterator;
  11524. break;
  11525. }
  11526. }
  11527. return *this;
  11528. }
  11529. /*!
  11530. @brief comparison: equal
  11531. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11532. */
  11533. 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 >
  11534. bool operator==(const IterImpl& other) const
  11535. {
  11536. // if objects are not the same, the comparison is undefined
  11537. if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
  11538. {
  11539. JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", m_object));
  11540. }
  11541. JSON_ASSERT(m_object != nullptr);
  11542. switch (m_object->m_type)
  11543. {
  11544. case value_t::object:
  11545. return (m_it.object_iterator == other.m_it.object_iterator);
  11546. case value_t::array:
  11547. return (m_it.array_iterator == other.m_it.array_iterator);
  11548. case value_t::null:
  11549. case value_t::string:
  11550. case value_t::boolean:
  11551. case value_t::number_integer:
  11552. case value_t::number_unsigned:
  11553. case value_t::number_float:
  11554. case value_t::binary:
  11555. case value_t::discarded:
  11556. default:
  11557. return (m_it.primitive_iterator == other.m_it.primitive_iterator);
  11558. }
  11559. }
  11560. /*!
  11561. @brief comparison: not equal
  11562. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11563. */
  11564. 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 >
  11565. bool operator!=(const IterImpl& other) const
  11566. {
  11567. return !operator==(other);
  11568. }
  11569. /*!
  11570. @brief comparison: smaller
  11571. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11572. */
  11573. bool operator<(const iter_impl& other) const
  11574. {
  11575. // if objects are not the same, the comparison is undefined
  11576. if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
  11577. {
  11578. JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", m_object));
  11579. }
  11580. JSON_ASSERT(m_object != nullptr);
  11581. switch (m_object->m_type)
  11582. {
  11583. case value_t::object:
  11584. JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators", m_object));
  11585. case value_t::array:
  11586. return (m_it.array_iterator < other.m_it.array_iterator);
  11587. case value_t::null:
  11588. case value_t::string:
  11589. case value_t::boolean:
  11590. case value_t::number_integer:
  11591. case value_t::number_unsigned:
  11592. case value_t::number_float:
  11593. case value_t::binary:
  11594. case value_t::discarded:
  11595. default:
  11596. return (m_it.primitive_iterator < other.m_it.primitive_iterator);
  11597. }
  11598. }
  11599. /*!
  11600. @brief comparison: less than or equal
  11601. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11602. */
  11603. bool operator<=(const iter_impl& other) const
  11604. {
  11605. return !other.operator < (*this);
  11606. }
  11607. /*!
  11608. @brief comparison: greater than
  11609. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11610. */
  11611. bool operator>(const iter_impl& other) const
  11612. {
  11613. return !operator<=(other);
  11614. }
  11615. /*!
  11616. @brief comparison: greater than or equal
  11617. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11618. */
  11619. bool operator>=(const iter_impl& other) const
  11620. {
  11621. return !operator<(other);
  11622. }
  11623. /*!
  11624. @brief add to iterator
  11625. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11626. */
  11627. iter_impl& operator+=(difference_type i)
  11628. {
  11629. JSON_ASSERT(m_object != nullptr);
  11630. switch (m_object->m_type)
  11631. {
  11632. case value_t::object:
  11633. JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", m_object));
  11634. case value_t::array:
  11635. {
  11636. std::advance(m_it.array_iterator, i);
  11637. break;
  11638. }
  11639. case value_t::null:
  11640. case value_t::string:
  11641. case value_t::boolean:
  11642. case value_t::number_integer:
  11643. case value_t::number_unsigned:
  11644. case value_t::number_float:
  11645. case value_t::binary:
  11646. case value_t::discarded:
  11647. default:
  11648. {
  11649. m_it.primitive_iterator += i;
  11650. break;
  11651. }
  11652. }
  11653. return *this;
  11654. }
  11655. /*!
  11656. @brief subtract from iterator
  11657. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11658. */
  11659. iter_impl& operator-=(difference_type i)
  11660. {
  11661. return operator+=(-i);
  11662. }
  11663. /*!
  11664. @brief add to iterator
  11665. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11666. */
  11667. iter_impl operator+(difference_type i) const
  11668. {
  11669. auto result = *this;
  11670. result += i;
  11671. return result;
  11672. }
  11673. /*!
  11674. @brief addition of distance and iterator
  11675. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11676. */
  11677. friend iter_impl operator+(difference_type i, const iter_impl& it)
  11678. {
  11679. auto result = it;
  11680. result += i;
  11681. return result;
  11682. }
  11683. /*!
  11684. @brief subtract from iterator
  11685. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11686. */
  11687. iter_impl operator-(difference_type i) const
  11688. {
  11689. auto result = *this;
  11690. result -= i;
  11691. return result;
  11692. }
  11693. /*!
  11694. @brief return difference
  11695. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11696. */
  11697. difference_type operator-(const iter_impl& other) const
  11698. {
  11699. JSON_ASSERT(m_object != nullptr);
  11700. switch (m_object->m_type)
  11701. {
  11702. case value_t::object:
  11703. JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", m_object));
  11704. case value_t::array:
  11705. return m_it.array_iterator - other.m_it.array_iterator;
  11706. case value_t::null:
  11707. case value_t::string:
  11708. case value_t::boolean:
  11709. case value_t::number_integer:
  11710. case value_t::number_unsigned:
  11711. case value_t::number_float:
  11712. case value_t::binary:
  11713. case value_t::discarded:
  11714. default:
  11715. return m_it.primitive_iterator - other.m_it.primitive_iterator;
  11716. }
  11717. }
  11718. /*!
  11719. @brief access to successor
  11720. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11721. */
  11722. reference operator[](difference_type n) const
  11723. {
  11724. JSON_ASSERT(m_object != nullptr);
  11725. switch (m_object->m_type)
  11726. {
  11727. case value_t::object:
  11728. JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators", m_object));
  11729. case value_t::array:
  11730. return *std::next(m_it.array_iterator, n);
  11731. case value_t::null:
  11732. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  11733. case value_t::string:
  11734. case value_t::boolean:
  11735. case value_t::number_integer:
  11736. case value_t::number_unsigned:
  11737. case value_t::number_float:
  11738. case value_t::binary:
  11739. case value_t::discarded:
  11740. default:
  11741. {
  11742. if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.get_value() == -n))
  11743. {
  11744. return *m_object;
  11745. }
  11746. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  11747. }
  11748. }
  11749. }
  11750. /*!
  11751. @brief return the key of an object iterator
  11752. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11753. */
  11754. const typename object_t::key_type& key() const
  11755. {
  11756. JSON_ASSERT(m_object != nullptr);
  11757. if (JSON_HEDLEY_LIKELY(m_object->is_object()))
  11758. {
  11759. return m_it.object_iterator->first;
  11760. }
  11761. JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators", m_object));
  11762. }
  11763. /*!
  11764. @brief return the value of an iterator
  11765. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11766. */
  11767. reference value() const
  11768. {
  11769. return operator*();
  11770. }
  11771. JSON_PRIVATE_UNLESS_TESTED:
  11772. /// associated JSON instance
  11773. pointer m_object = nullptr;
  11774. /// the actual iterator of the associated instance
  11775. internal_iterator<typename std::remove_const<BasicJsonType>::type> m_it {};
  11776. };
  11777. } // namespace detail
  11778. NLOHMANN_JSON_NAMESPACE_END
  11779. // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
  11780. // #include <nlohmann/detail/iterators/json_reverse_iterator.hpp>
  11781. // __ _____ _____ _____
  11782. // __| | __| | | | JSON for Modern C++
  11783. // | | |__ | | | | | | version 3.11.2
  11784. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  11785. //
  11786. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  11787. // SPDX-License-Identifier: MIT
  11788. #include <cstddef> // ptrdiff_t
  11789. #include <iterator> // reverse_iterator
  11790. #include <utility> // declval
  11791. // #include <nlohmann/detail/abi_macros.hpp>
  11792. NLOHMANN_JSON_NAMESPACE_BEGIN
  11793. namespace detail
  11794. {
  11795. //////////////////////
  11796. // reverse_iterator //
  11797. //////////////////////
  11798. /*!
  11799. @brief a template for a reverse iterator class
  11800. @tparam Base the base iterator type to reverse. Valid types are @ref
  11801. iterator (to create @ref reverse_iterator) and @ref const_iterator (to
  11802. create @ref const_reverse_iterator).
  11803. @requirement The class satisfies the following concept requirements:
  11804. -
  11805. [BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
  11806. The iterator that can be moved can be moved in both directions (i.e.
  11807. incremented and decremented).
  11808. - [OutputIterator](https://en.cppreference.com/w/cpp/named_req/OutputIterator):
  11809. It is possible to write to the pointed-to element (only if @a Base is
  11810. @ref iterator).
  11811. @since version 1.0.0
  11812. */
  11813. template<typename Base>
  11814. class json_reverse_iterator : public std::reverse_iterator<Base>
  11815. {
  11816. public:
  11817. using difference_type = std::ptrdiff_t;
  11818. /// shortcut to the reverse iterator adapter
  11819. using base_iterator = std::reverse_iterator<Base>;
  11820. /// the reference type for the pointed-to element
  11821. using reference = typename Base::reference;
  11822. /// create reverse iterator from iterator
  11823. explicit json_reverse_iterator(const typename base_iterator::iterator_type& it) noexcept
  11824. : base_iterator(it) {}
  11825. /// create reverse iterator from base class
  11826. explicit json_reverse_iterator(const base_iterator& it) noexcept : base_iterator(it) {}
  11827. /// post-increment (it++)
  11828. json_reverse_iterator operator++(int)& // NOLINT(cert-dcl21-cpp)
  11829. {
  11830. return static_cast<json_reverse_iterator>(base_iterator::operator++(1));
  11831. }
  11832. /// pre-increment (++it)
  11833. json_reverse_iterator& operator++()
  11834. {
  11835. return static_cast<json_reverse_iterator&>(base_iterator::operator++());
  11836. }
  11837. /// post-decrement (it--)
  11838. json_reverse_iterator operator--(int)& // NOLINT(cert-dcl21-cpp)
  11839. {
  11840. return static_cast<json_reverse_iterator>(base_iterator::operator--(1));
  11841. }
  11842. /// pre-decrement (--it)
  11843. json_reverse_iterator& operator--()
  11844. {
  11845. return static_cast<json_reverse_iterator&>(base_iterator::operator--());
  11846. }
  11847. /// add to iterator
  11848. json_reverse_iterator& operator+=(difference_type i)
  11849. {
  11850. return static_cast<json_reverse_iterator&>(base_iterator::operator+=(i));
  11851. }
  11852. /// add to iterator
  11853. json_reverse_iterator operator+(difference_type i) const
  11854. {
  11855. return static_cast<json_reverse_iterator>(base_iterator::operator+(i));
  11856. }
  11857. /// subtract from iterator
  11858. json_reverse_iterator operator-(difference_type i) const
  11859. {
  11860. return static_cast<json_reverse_iterator>(base_iterator::operator-(i));
  11861. }
  11862. /// return difference
  11863. difference_type operator-(const json_reverse_iterator& other) const
  11864. {
  11865. return base_iterator(*this) - base_iterator(other);
  11866. }
  11867. /// access to successor
  11868. reference operator[](difference_type n) const
  11869. {
  11870. return *(this->operator+(n));
  11871. }
  11872. /// return the key of an object iterator
  11873. auto key() const -> decltype(std::declval<Base>().key())
  11874. {
  11875. auto it = --this->base();
  11876. return it.key();
  11877. }
  11878. /// return the value of an iterator
  11879. reference value() const
  11880. {
  11881. auto it = --this->base();
  11882. return it.operator * ();
  11883. }
  11884. };
  11885. } // namespace detail
  11886. NLOHMANN_JSON_NAMESPACE_END
  11887. // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
  11888. // #include <nlohmann/detail/json_pointer.hpp>
  11889. // __ _____ _____ _____
  11890. // __| | __| | | | JSON for Modern C++
  11891. // | | |__ | | | | | | version 3.11.2
  11892. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  11893. //
  11894. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  11895. // SPDX-License-Identifier: MIT
  11896. #include <algorithm> // all_of
  11897. #include <cctype> // isdigit
  11898. #include <cerrno> // errno, ERANGE
  11899. #include <cstdlib> // strtoull
  11900. #ifndef JSON_NO_IO
  11901. #include <iosfwd> // ostream
  11902. #endif // JSON_NO_IO
  11903. #include <limits> // max
  11904. #include <numeric> // accumulate
  11905. #include <string> // string
  11906. #include <utility> // move
  11907. #include <vector> // vector
  11908. // #include <nlohmann/detail/exceptions.hpp>
  11909. // #include <nlohmann/detail/macro_scope.hpp>
  11910. // #include <nlohmann/detail/string_concat.hpp>
  11911. // #include <nlohmann/detail/string_escape.hpp>
  11912. // #include <nlohmann/detail/value_t.hpp>
  11913. NLOHMANN_JSON_NAMESPACE_BEGIN
  11914. /// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document
  11915. /// @sa https://json.nlohmann.me/api/json_pointer/
  11916. template<typename RefStringType>
  11917. class json_pointer
  11918. {
  11919. // allow basic_json to access private members
  11920. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  11921. friend class basic_json;
  11922. template<typename>
  11923. friend class json_pointer;
  11924. template<typename T>
  11925. struct string_t_helper
  11926. {
  11927. using type = T;
  11928. };
  11929. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  11930. struct string_t_helper<NLOHMANN_BASIC_JSON_TPL>
  11931. {
  11932. using type = StringType;
  11933. };
  11934. public:
  11935. // for backwards compatibility accept BasicJsonType
  11936. using string_t = typename string_t_helper<RefStringType>::type;
  11937. /// @brief create JSON pointer
  11938. /// @sa https://json.nlohmann.me/api/json_pointer/json_pointer/
  11939. explicit json_pointer(const string_t& s = "")
  11940. : reference_tokens(split(s))
  11941. {}
  11942. /// @brief return a string representation of the JSON pointer
  11943. /// @sa https://json.nlohmann.me/api/json_pointer/to_string/
  11944. string_t to_string() const
  11945. {
  11946. return std::accumulate(reference_tokens.begin(), reference_tokens.end(),
  11947. string_t{},
  11948. [](const string_t& a, const string_t& b)
  11949. {
  11950. return detail::concat(a, '/', detail::escape(b));
  11951. });
  11952. }
  11953. /// @brief return a string representation of the JSON pointer
  11954. /// @sa https://json.nlohmann.me/api/json_pointer/operator_string/
  11955. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, to_string())
  11956. operator string_t() const
  11957. {
  11958. return to_string();
  11959. }
  11960. #ifndef JSON_NO_IO
  11961. /// @brief write string representation of the JSON pointer to stream
  11962. /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
  11963. friend std::ostream& operator<<(std::ostream& o, const json_pointer& ptr)
  11964. {
  11965. o << ptr.to_string();
  11966. return o;
  11967. }
  11968. #endif
  11969. /// @brief append another JSON pointer at the end of this JSON pointer
  11970. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
  11971. json_pointer& operator/=(const json_pointer& ptr)
  11972. {
  11973. reference_tokens.insert(reference_tokens.end(),
  11974. ptr.reference_tokens.begin(),
  11975. ptr.reference_tokens.end());
  11976. return *this;
  11977. }
  11978. /// @brief append an unescaped reference token at the end of this JSON pointer
  11979. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
  11980. json_pointer& operator/=(string_t token)
  11981. {
  11982. push_back(std::move(token));
  11983. return *this;
  11984. }
  11985. /// @brief append an array index at the end of this JSON pointer
  11986. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
  11987. json_pointer& operator/=(std::size_t array_idx)
  11988. {
  11989. return *this /= std::to_string(array_idx);
  11990. }
  11991. /// @brief create a new JSON pointer by appending the right JSON pointer at the end of the left JSON pointer
  11992. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
  11993. friend json_pointer operator/(const json_pointer& lhs,
  11994. const json_pointer& rhs)
  11995. {
  11996. return json_pointer(lhs) /= rhs;
  11997. }
  11998. /// @brief create a new JSON pointer by appending the unescaped token at the end of the JSON pointer
  11999. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
  12000. friend json_pointer operator/(const json_pointer& lhs, string_t token) // NOLINT(performance-unnecessary-value-param)
  12001. {
  12002. return json_pointer(lhs) /= std::move(token);
  12003. }
  12004. /// @brief create a new JSON pointer by appending the array-index-token at the end of the JSON pointer
  12005. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
  12006. friend json_pointer operator/(const json_pointer& lhs, std::size_t array_idx)
  12007. {
  12008. return json_pointer(lhs) /= array_idx;
  12009. }
  12010. /// @brief returns the parent of this JSON pointer
  12011. /// @sa https://json.nlohmann.me/api/json_pointer/parent_pointer/
  12012. json_pointer parent_pointer() const
  12013. {
  12014. if (empty())
  12015. {
  12016. return *this;
  12017. }
  12018. json_pointer res = *this;
  12019. res.pop_back();
  12020. return res;
  12021. }
  12022. /// @brief remove last reference token
  12023. /// @sa https://json.nlohmann.me/api/json_pointer/pop_back/
  12024. void pop_back()
  12025. {
  12026. if (JSON_HEDLEY_UNLIKELY(empty()))
  12027. {
  12028. JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
  12029. }
  12030. reference_tokens.pop_back();
  12031. }
  12032. /// @brief return last reference token
  12033. /// @sa https://json.nlohmann.me/api/json_pointer/back/
  12034. const string_t& back() const
  12035. {
  12036. if (JSON_HEDLEY_UNLIKELY(empty()))
  12037. {
  12038. JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
  12039. }
  12040. return reference_tokens.back();
  12041. }
  12042. /// @brief append an unescaped token at the end of the reference pointer
  12043. /// @sa https://json.nlohmann.me/api/json_pointer/push_back/
  12044. void push_back(const string_t& token)
  12045. {
  12046. reference_tokens.push_back(token);
  12047. }
  12048. /// @brief append an unescaped token at the end of the reference pointer
  12049. /// @sa https://json.nlohmann.me/api/json_pointer/push_back/
  12050. void push_back(string_t&& token)
  12051. {
  12052. reference_tokens.push_back(std::move(token));
  12053. }
  12054. /// @brief return whether pointer points to the root document
  12055. /// @sa https://json.nlohmann.me/api/json_pointer/empty/
  12056. bool empty() const noexcept
  12057. {
  12058. return reference_tokens.empty();
  12059. }
  12060. private:
  12061. /*!
  12062. @param[in] s reference token to be converted into an array index
  12063. @return integer representation of @a s
  12064. @throw parse_error.106 if an array index begins with '0'
  12065. @throw parse_error.109 if an array index begins not with a digit
  12066. @throw out_of_range.404 if string @a s could not be converted to an integer
  12067. @throw out_of_range.410 if an array index exceeds size_type
  12068. */
  12069. template<typename BasicJsonType>
  12070. static typename BasicJsonType::size_type array_index(const string_t& s)
  12071. {
  12072. using size_type = typename BasicJsonType::size_type;
  12073. // error condition (cf. RFC 6901, Sect. 4)
  12074. if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && s[0] == '0'))
  12075. {
  12076. JSON_THROW(detail::parse_error::create(106, 0, detail::concat("array index '", s, "' must not begin with '0'"), nullptr));
  12077. }
  12078. // error condition (cf. RFC 6901, Sect. 4)
  12079. if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && !(s[0] >= '1' && s[0] <= '9')))
  12080. {
  12081. JSON_THROW(detail::parse_error::create(109, 0, detail::concat("array index '", s, "' is not a number"), nullptr));
  12082. }
  12083. const char* p = s.c_str();
  12084. char* p_end = nullptr;
  12085. errno = 0; // strtoull doesn't reset errno
  12086. unsigned long long res = std::strtoull(p, &p_end, 10); // NOLINT(runtime/int)
  12087. if (p == p_end // invalid input or empty string
  12088. || errno == ERANGE // out of range
  12089. || JSON_HEDLEY_UNLIKELY(static_cast<std::size_t>(p_end - p) != s.size())) // incomplete read
  12090. {
  12091. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", s, "'"), nullptr));
  12092. }
  12093. // only triggered on special platforms (like 32bit), see also
  12094. // https://github.com/nlohmann/json/pull/2203
  12095. if (res >= static_cast<unsigned long long>((std::numeric_limits<size_type>::max)())) // NOLINT(runtime/int)
  12096. {
  12097. JSON_THROW(detail::out_of_range::create(410, detail::concat("array index ", s, " exceeds size_type"), nullptr)); // LCOV_EXCL_LINE
  12098. }
  12099. return static_cast<size_type>(res);
  12100. }
  12101. JSON_PRIVATE_UNLESS_TESTED:
  12102. json_pointer top() const
  12103. {
  12104. if (JSON_HEDLEY_UNLIKELY(empty()))
  12105. {
  12106. JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
  12107. }
  12108. json_pointer result = *this;
  12109. result.reference_tokens = {reference_tokens[0]};
  12110. return result;
  12111. }
  12112. private:
  12113. /*!
  12114. @brief create and return a reference to the pointed to value
  12115. @complexity Linear in the number of reference tokens.
  12116. @throw parse_error.109 if array index is not a number
  12117. @throw type_error.313 if value cannot be unflattened
  12118. */
  12119. template<typename BasicJsonType>
  12120. BasicJsonType& get_and_create(BasicJsonType& j) const
  12121. {
  12122. auto* result = &j;
  12123. // in case no reference tokens exist, return a reference to the JSON value
  12124. // j which will be overwritten by a primitive value
  12125. for (const auto& reference_token : reference_tokens)
  12126. {
  12127. switch (result->type())
  12128. {
  12129. case detail::value_t::null:
  12130. {
  12131. if (reference_token == "0")
  12132. {
  12133. // start a new array if reference token is 0
  12134. result = &result->operator[](0);
  12135. }
  12136. else
  12137. {
  12138. // start a new object otherwise
  12139. result = &result->operator[](reference_token);
  12140. }
  12141. break;
  12142. }
  12143. case detail::value_t::object:
  12144. {
  12145. // create an entry in the object
  12146. result = &result->operator[](reference_token);
  12147. break;
  12148. }
  12149. case detail::value_t::array:
  12150. {
  12151. // create an entry in the array
  12152. result = &result->operator[](array_index<BasicJsonType>(reference_token));
  12153. break;
  12154. }
  12155. /*
  12156. The following code is only reached if there exists a reference
  12157. token _and_ the current value is primitive. In this case, we have
  12158. an error situation, because primitive values may only occur as
  12159. single value; that is, with an empty list of reference tokens.
  12160. */
  12161. case detail::value_t::string:
  12162. case detail::value_t::boolean:
  12163. case detail::value_t::number_integer:
  12164. case detail::value_t::number_unsigned:
  12165. case detail::value_t::number_float:
  12166. case detail::value_t::binary:
  12167. case detail::value_t::discarded:
  12168. default:
  12169. JSON_THROW(detail::type_error::create(313, "invalid value to unflatten", &j));
  12170. }
  12171. }
  12172. return *result;
  12173. }
  12174. /*!
  12175. @brief return a reference to the pointed to value
  12176. @note This version does not throw if a value is not present, but tries to
  12177. create nested values instead. For instance, calling this function
  12178. with pointer `"/this/that"` on a null value is equivalent to calling
  12179. `operator[]("this").operator[]("that")` on that value, effectively
  12180. changing the null value to an object.
  12181. @param[in] ptr a JSON value
  12182. @return reference to the JSON value pointed to by the JSON pointer
  12183. @complexity Linear in the length of the JSON pointer.
  12184. @throw parse_error.106 if an array index begins with '0'
  12185. @throw parse_error.109 if an array index was not a number
  12186. @throw out_of_range.404 if the JSON pointer can not be resolved
  12187. */
  12188. template<typename BasicJsonType>
  12189. BasicJsonType& get_unchecked(BasicJsonType* ptr) const
  12190. {
  12191. for (const auto& reference_token : reference_tokens)
  12192. {
  12193. // convert null values to arrays or objects before continuing
  12194. if (ptr->is_null())
  12195. {
  12196. // check if reference token is a number
  12197. const bool nums =
  12198. std::all_of(reference_token.begin(), reference_token.end(),
  12199. [](const unsigned char x)
  12200. {
  12201. return std::isdigit(x);
  12202. });
  12203. // change value to array for numbers or "-" or to object otherwise
  12204. *ptr = (nums || reference_token == "-")
  12205. ? detail::value_t::array
  12206. : detail::value_t::object;
  12207. }
  12208. switch (ptr->type())
  12209. {
  12210. case detail::value_t::object:
  12211. {
  12212. // use unchecked object access
  12213. ptr = &ptr->operator[](reference_token);
  12214. break;
  12215. }
  12216. case detail::value_t::array:
  12217. {
  12218. if (reference_token == "-")
  12219. {
  12220. // explicitly treat "-" as index beyond the end
  12221. ptr = &ptr->operator[](ptr->m_value.array->size());
  12222. }
  12223. else
  12224. {
  12225. // convert array index to number; unchecked access
  12226. ptr = &ptr->operator[](array_index<BasicJsonType>(reference_token));
  12227. }
  12228. break;
  12229. }
  12230. case detail::value_t::null:
  12231. case detail::value_t::string:
  12232. case detail::value_t::boolean:
  12233. case detail::value_t::number_integer:
  12234. case detail::value_t::number_unsigned:
  12235. case detail::value_t::number_float:
  12236. case detail::value_t::binary:
  12237. case detail::value_t::discarded:
  12238. default:
  12239. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
  12240. }
  12241. }
  12242. return *ptr;
  12243. }
  12244. /*!
  12245. @throw parse_error.106 if an array index begins with '0'
  12246. @throw parse_error.109 if an array index was not a number
  12247. @throw out_of_range.402 if the array index '-' is used
  12248. @throw out_of_range.404 if the JSON pointer can not be resolved
  12249. */
  12250. template<typename BasicJsonType>
  12251. BasicJsonType& get_checked(BasicJsonType* ptr) const
  12252. {
  12253. for (const auto& reference_token : reference_tokens)
  12254. {
  12255. switch (ptr->type())
  12256. {
  12257. case detail::value_t::object:
  12258. {
  12259. // note: at performs range check
  12260. ptr = &ptr->at(reference_token);
  12261. break;
  12262. }
  12263. case detail::value_t::array:
  12264. {
  12265. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  12266. {
  12267. // "-" always fails the range check
  12268. JSON_THROW(detail::out_of_range::create(402, detail::concat(
  12269. "array index '-' (", std::to_string(ptr->m_value.array->size()),
  12270. ") is out of range"), ptr));
  12271. }
  12272. // note: at performs range check
  12273. ptr = &ptr->at(array_index<BasicJsonType>(reference_token));
  12274. break;
  12275. }
  12276. case detail::value_t::null:
  12277. case detail::value_t::string:
  12278. case detail::value_t::boolean:
  12279. case detail::value_t::number_integer:
  12280. case detail::value_t::number_unsigned:
  12281. case detail::value_t::number_float:
  12282. case detail::value_t::binary:
  12283. case detail::value_t::discarded:
  12284. default:
  12285. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
  12286. }
  12287. }
  12288. return *ptr;
  12289. }
  12290. /*!
  12291. @brief return a const reference to the pointed to value
  12292. @param[in] ptr a JSON value
  12293. @return const reference to the JSON value pointed to by the JSON
  12294. pointer
  12295. @throw parse_error.106 if an array index begins with '0'
  12296. @throw parse_error.109 if an array index was not a number
  12297. @throw out_of_range.402 if the array index '-' is used
  12298. @throw out_of_range.404 if the JSON pointer can not be resolved
  12299. */
  12300. template<typename BasicJsonType>
  12301. const BasicJsonType& get_unchecked(const BasicJsonType* ptr) const
  12302. {
  12303. for (const auto& reference_token : reference_tokens)
  12304. {
  12305. switch (ptr->type())
  12306. {
  12307. case detail::value_t::object:
  12308. {
  12309. // use unchecked object access
  12310. ptr = &ptr->operator[](reference_token);
  12311. break;
  12312. }
  12313. case detail::value_t::array:
  12314. {
  12315. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  12316. {
  12317. // "-" cannot be used for const access
  12318. JSON_THROW(detail::out_of_range::create(402, detail::concat("array index '-' (", std::to_string(ptr->m_value.array->size()), ") is out of range"), ptr));
  12319. }
  12320. // use unchecked array access
  12321. ptr = &ptr->operator[](array_index<BasicJsonType>(reference_token));
  12322. break;
  12323. }
  12324. case detail::value_t::null:
  12325. case detail::value_t::string:
  12326. case detail::value_t::boolean:
  12327. case detail::value_t::number_integer:
  12328. case detail::value_t::number_unsigned:
  12329. case detail::value_t::number_float:
  12330. case detail::value_t::binary:
  12331. case detail::value_t::discarded:
  12332. default:
  12333. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
  12334. }
  12335. }
  12336. return *ptr;
  12337. }
  12338. /*!
  12339. @throw parse_error.106 if an array index begins with '0'
  12340. @throw parse_error.109 if an array index was not a number
  12341. @throw out_of_range.402 if the array index '-' is used
  12342. @throw out_of_range.404 if the JSON pointer can not be resolved
  12343. */
  12344. template<typename BasicJsonType>
  12345. const BasicJsonType& get_checked(const BasicJsonType* ptr) const
  12346. {
  12347. for (const auto& reference_token : reference_tokens)
  12348. {
  12349. switch (ptr->type())
  12350. {
  12351. case detail::value_t::object:
  12352. {
  12353. // note: at performs range check
  12354. ptr = &ptr->at(reference_token);
  12355. break;
  12356. }
  12357. case detail::value_t::array:
  12358. {
  12359. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  12360. {
  12361. // "-" always fails the range check
  12362. JSON_THROW(detail::out_of_range::create(402, detail::concat(
  12363. "array index '-' (", std::to_string(ptr->m_value.array->size()),
  12364. ") is out of range"), ptr));
  12365. }
  12366. // note: at performs range check
  12367. ptr = &ptr->at(array_index<BasicJsonType>(reference_token));
  12368. break;
  12369. }
  12370. case detail::value_t::null:
  12371. case detail::value_t::string:
  12372. case detail::value_t::boolean:
  12373. case detail::value_t::number_integer:
  12374. case detail::value_t::number_unsigned:
  12375. case detail::value_t::number_float:
  12376. case detail::value_t::binary:
  12377. case detail::value_t::discarded:
  12378. default:
  12379. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
  12380. }
  12381. }
  12382. return *ptr;
  12383. }
  12384. /*!
  12385. @throw parse_error.106 if an array index begins with '0'
  12386. @throw parse_error.109 if an array index was not a number
  12387. */
  12388. template<typename BasicJsonType>
  12389. bool contains(const BasicJsonType* ptr) const
  12390. {
  12391. for (const auto& reference_token : reference_tokens)
  12392. {
  12393. switch (ptr->type())
  12394. {
  12395. case detail::value_t::object:
  12396. {
  12397. if (!ptr->contains(reference_token))
  12398. {
  12399. // we did not find the key in the object
  12400. return false;
  12401. }
  12402. ptr = &ptr->operator[](reference_token);
  12403. break;
  12404. }
  12405. case detail::value_t::array:
  12406. {
  12407. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  12408. {
  12409. // "-" always fails the range check
  12410. return false;
  12411. }
  12412. if (JSON_HEDLEY_UNLIKELY(reference_token.size() == 1 && !("0" <= reference_token && reference_token <= "9")))
  12413. {
  12414. // invalid char
  12415. return false;
  12416. }
  12417. if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1))
  12418. {
  12419. if (JSON_HEDLEY_UNLIKELY(!('1' <= reference_token[0] && reference_token[0] <= '9')))
  12420. {
  12421. // first char should be between '1' and '9'
  12422. return false;
  12423. }
  12424. for (std::size_t i = 1; i < reference_token.size(); i++)
  12425. {
  12426. if (JSON_HEDLEY_UNLIKELY(!('0' <= reference_token[i] && reference_token[i] <= '9')))
  12427. {
  12428. // other char should be between '0' and '9'
  12429. return false;
  12430. }
  12431. }
  12432. }
  12433. const auto idx = array_index<BasicJsonType>(reference_token);
  12434. if (idx >= ptr->size())
  12435. {
  12436. // index out of range
  12437. return false;
  12438. }
  12439. ptr = &ptr->operator[](idx);
  12440. break;
  12441. }
  12442. case detail::value_t::null:
  12443. case detail::value_t::string:
  12444. case detail::value_t::boolean:
  12445. case detail::value_t::number_integer:
  12446. case detail::value_t::number_unsigned:
  12447. case detail::value_t::number_float:
  12448. case detail::value_t::binary:
  12449. case detail::value_t::discarded:
  12450. default:
  12451. {
  12452. // we do not expect primitive values if there is still a
  12453. // reference token to process
  12454. return false;
  12455. }
  12456. }
  12457. }
  12458. // no reference token left means we found a primitive value
  12459. return true;
  12460. }
  12461. /*!
  12462. @brief split the string input to reference tokens
  12463. @note This function is only called by the json_pointer constructor.
  12464. All exceptions below are documented there.
  12465. @throw parse_error.107 if the pointer is not empty or begins with '/'
  12466. @throw parse_error.108 if character '~' is not followed by '0' or '1'
  12467. */
  12468. static std::vector<string_t> split(const string_t& reference_string)
  12469. {
  12470. std::vector<string_t> result;
  12471. // special case: empty reference string -> no reference tokens
  12472. if (reference_string.empty())
  12473. {
  12474. return result;
  12475. }
  12476. // check if nonempty reference string begins with slash
  12477. if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/'))
  12478. {
  12479. JSON_THROW(detail::parse_error::create(107, 1, detail::concat("JSON pointer must be empty or begin with '/' - was: '", reference_string, "'"), nullptr));
  12480. }
  12481. // extract the reference tokens:
  12482. // - slash: position of the last read slash (or end of string)
  12483. // - start: position after the previous slash
  12484. for (
  12485. // search for the first slash after the first character
  12486. std::size_t slash = reference_string.find_first_of('/', 1),
  12487. // set the beginning of the first reference token
  12488. start = 1;
  12489. // we can stop if start == 0 (if slash == string_t::npos)
  12490. start != 0;
  12491. // set the beginning of the next reference token
  12492. // (will eventually be 0 if slash == string_t::npos)
  12493. start = (slash == string_t::npos) ? 0 : slash + 1,
  12494. // find next slash
  12495. slash = reference_string.find_first_of('/', start))
  12496. {
  12497. // use the text between the beginning of the reference token
  12498. // (start) and the last slash (slash).
  12499. auto reference_token = reference_string.substr(start, slash - start);
  12500. // check reference tokens are properly escaped
  12501. for (std::size_t pos = reference_token.find_first_of('~');
  12502. pos != string_t::npos;
  12503. pos = reference_token.find_first_of('~', pos + 1))
  12504. {
  12505. JSON_ASSERT(reference_token[pos] == '~');
  12506. // ~ must be followed by 0 or 1
  12507. if (JSON_HEDLEY_UNLIKELY(pos == reference_token.size() - 1 ||
  12508. (reference_token[pos + 1] != '0' &&
  12509. reference_token[pos + 1] != '1')))
  12510. {
  12511. JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'", nullptr));
  12512. }
  12513. }
  12514. // finally, store the reference token
  12515. detail::unescape(reference_token);
  12516. result.push_back(reference_token);
  12517. }
  12518. return result;
  12519. }
  12520. private:
  12521. /*!
  12522. @param[in] reference_string the reference string to the current value
  12523. @param[in] value the value to consider
  12524. @param[in,out] result the result object to insert values to
  12525. @note Empty objects or arrays are flattened to `null`.
  12526. */
  12527. template<typename BasicJsonType>
  12528. static void flatten(const string_t& reference_string,
  12529. const BasicJsonType& value,
  12530. BasicJsonType& result)
  12531. {
  12532. switch (value.type())
  12533. {
  12534. case detail::value_t::array:
  12535. {
  12536. if (value.m_value.array->empty())
  12537. {
  12538. // flatten empty array as null
  12539. result[reference_string] = nullptr;
  12540. }
  12541. else
  12542. {
  12543. // iterate array and use index as reference string
  12544. for (std::size_t i = 0; i < value.m_value.array->size(); ++i)
  12545. {
  12546. flatten(detail::concat(reference_string, '/', std::to_string(i)),
  12547. value.m_value.array->operator[](i), result);
  12548. }
  12549. }
  12550. break;
  12551. }
  12552. case detail::value_t::object:
  12553. {
  12554. if (value.m_value.object->empty())
  12555. {
  12556. // flatten empty object as null
  12557. result[reference_string] = nullptr;
  12558. }
  12559. else
  12560. {
  12561. // iterate object and use keys as reference string
  12562. for (const auto& element : *value.m_value.object)
  12563. {
  12564. flatten(detail::concat(reference_string, '/', detail::escape(element.first)), element.second, result);
  12565. }
  12566. }
  12567. break;
  12568. }
  12569. case detail::value_t::null:
  12570. case detail::value_t::string:
  12571. case detail::value_t::boolean:
  12572. case detail::value_t::number_integer:
  12573. case detail::value_t::number_unsigned:
  12574. case detail::value_t::number_float:
  12575. case detail::value_t::binary:
  12576. case detail::value_t::discarded:
  12577. default:
  12578. {
  12579. // add primitive value with its reference string
  12580. result[reference_string] = value;
  12581. break;
  12582. }
  12583. }
  12584. }
  12585. /*!
  12586. @param[in] value flattened JSON
  12587. @return unflattened JSON
  12588. @throw parse_error.109 if array index is not a number
  12589. @throw type_error.314 if value is not an object
  12590. @throw type_error.315 if object values are not primitive
  12591. @throw type_error.313 if value cannot be unflattened
  12592. */
  12593. template<typename BasicJsonType>
  12594. static BasicJsonType
  12595. unflatten(const BasicJsonType& value)
  12596. {
  12597. if (JSON_HEDLEY_UNLIKELY(!value.is_object()))
  12598. {
  12599. JSON_THROW(detail::type_error::create(314, "only objects can be unflattened", &value));
  12600. }
  12601. BasicJsonType result;
  12602. // iterate the JSON object values
  12603. for (const auto& element : *value.m_value.object)
  12604. {
  12605. if (JSON_HEDLEY_UNLIKELY(!element.second.is_primitive()))
  12606. {
  12607. JSON_THROW(detail::type_error::create(315, "values in object must be primitive", &element.second));
  12608. }
  12609. // assign value to reference pointed to by JSON pointer; Note that if
  12610. // the JSON pointer is "" (i.e., points to the whole value), function
  12611. // get_and_create returns a reference to result itself. An assignment
  12612. // will then create a primitive value.
  12613. json_pointer(element.first).get_and_create(result) = element.second;
  12614. }
  12615. return result;
  12616. }
  12617. // can't use conversion operator because of ambiguity
  12618. json_pointer<string_t> convert() const&
  12619. {
  12620. json_pointer<string_t> result;
  12621. result.reference_tokens = reference_tokens;
  12622. return result;
  12623. }
  12624. json_pointer<string_t> convert()&&
  12625. {
  12626. json_pointer<string_t> result;
  12627. result.reference_tokens = std::move(reference_tokens);
  12628. return result;
  12629. }
  12630. public:
  12631. #if JSON_HAS_THREE_WAY_COMPARISON
  12632. /// @brief compares two JSON pointers for equality
  12633. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  12634. template<typename RefStringTypeRhs>
  12635. bool operator==(const json_pointer<RefStringTypeRhs>& rhs) const noexcept
  12636. {
  12637. return reference_tokens == rhs.reference_tokens;
  12638. }
  12639. /// @brief compares JSON pointer and string for equality
  12640. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  12641. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer))
  12642. bool operator==(const string_t& rhs) const
  12643. {
  12644. return *this == json_pointer(rhs);
  12645. }
  12646. /// @brief 3-way compares two JSON pointers
  12647. template<typename RefStringTypeRhs>
  12648. std::strong_ordering operator<=>(const json_pointer<RefStringTypeRhs>& rhs) const noexcept // *NOPAD*
  12649. {
  12650. return reference_tokens <=> rhs.reference_tokens; // *NOPAD*
  12651. }
  12652. #else
  12653. /// @brief compares two JSON pointers for equality
  12654. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  12655. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  12656. // NOLINTNEXTLINE(readability-redundant-declaration)
  12657. friend bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
  12658. const json_pointer<RefStringTypeRhs>& rhs) noexcept;
  12659. /// @brief compares JSON pointer and string for equality
  12660. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  12661. template<typename RefStringTypeLhs, typename StringType>
  12662. // NOLINTNEXTLINE(readability-redundant-declaration)
  12663. friend bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
  12664. const StringType& rhs);
  12665. /// @brief compares string and JSON pointer for equality
  12666. /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
  12667. template<typename RefStringTypeRhs, typename StringType>
  12668. // NOLINTNEXTLINE(readability-redundant-declaration)
  12669. friend bool operator==(const StringType& lhs,
  12670. const json_pointer<RefStringTypeRhs>& rhs);
  12671. /// @brief compares two JSON pointers for inequality
  12672. /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/
  12673. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  12674. // NOLINTNEXTLINE(readability-redundant-declaration)
  12675. friend bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
  12676. const json_pointer<RefStringTypeRhs>& rhs) noexcept;
  12677. /// @brief compares JSON pointer and string for inequality
  12678. /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/
  12679. template<typename RefStringTypeLhs, typename StringType>
  12680. // NOLINTNEXTLINE(readability-redundant-declaration)
  12681. friend bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
  12682. const StringType& rhs);
  12683. /// @brief compares string and JSON pointer for inequality
  12684. /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/
  12685. template<typename RefStringTypeRhs, typename StringType>
  12686. // NOLINTNEXTLINE(readability-redundant-declaration)
  12687. friend bool operator!=(const StringType& lhs,
  12688. const json_pointer<RefStringTypeRhs>& rhs);
  12689. /// @brief compares two JSON pointer for less-than
  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. #endif
  12695. private:
  12696. /// the reference tokens
  12697. std::vector<string_t> reference_tokens;
  12698. };
  12699. #if !JSON_HAS_THREE_WAY_COMPARISON
  12700. // functions cannot be defined inside class due to ODR violations
  12701. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  12702. inline bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
  12703. const json_pointer<RefStringTypeRhs>& rhs) noexcept
  12704. {
  12705. return lhs.reference_tokens == rhs.reference_tokens;
  12706. }
  12707. template<typename RefStringTypeLhs,
  12708. typename StringType = typename json_pointer<RefStringTypeLhs>::string_t>
  12709. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer, json_pointer))
  12710. inline bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
  12711. const StringType& rhs)
  12712. {
  12713. return lhs == json_pointer<RefStringTypeLhs>(rhs);
  12714. }
  12715. template<typename RefStringTypeRhs,
  12716. typename StringType = typename json_pointer<RefStringTypeRhs>::string_t>
  12717. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer, json_pointer))
  12718. inline bool operator==(const StringType& lhs,
  12719. const json_pointer<RefStringTypeRhs>& rhs)
  12720. {
  12721. return json_pointer<RefStringTypeRhs>(lhs) == rhs;
  12722. }
  12723. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  12724. inline bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
  12725. const json_pointer<RefStringTypeRhs>& rhs) noexcept
  12726. {
  12727. return !(lhs == rhs);
  12728. }
  12729. template<typename RefStringTypeLhs,
  12730. typename StringType = typename json_pointer<RefStringTypeLhs>::string_t>
  12731. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator!=(json_pointer, json_pointer))
  12732. inline bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
  12733. const StringType& rhs)
  12734. {
  12735. return !(lhs == rhs);
  12736. }
  12737. template<typename RefStringTypeRhs,
  12738. typename StringType = typename json_pointer<RefStringTypeRhs>::string_t>
  12739. JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator!=(json_pointer, json_pointer))
  12740. inline bool operator!=(const StringType& lhs,
  12741. const json_pointer<RefStringTypeRhs>& rhs)
  12742. {
  12743. return !(lhs == rhs);
  12744. }
  12745. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  12746. inline bool operator<(const json_pointer<RefStringTypeLhs>& lhs,
  12747. const json_pointer<RefStringTypeRhs>& rhs) noexcept
  12748. {
  12749. return lhs.reference_tokens < rhs.reference_tokens;
  12750. }
  12751. #endif
  12752. NLOHMANN_JSON_NAMESPACE_END
  12753. // #include <nlohmann/detail/json_ref.hpp>
  12754. // __ _____ _____ _____
  12755. // __| | __| | | | JSON for Modern C++
  12756. // | | |__ | | | | | | version 3.11.2
  12757. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  12758. //
  12759. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  12760. // SPDX-License-Identifier: MIT
  12761. #include <initializer_list>
  12762. #include <utility>
  12763. // #include <nlohmann/detail/abi_macros.hpp>
  12764. // #include <nlohmann/detail/meta/type_traits.hpp>
  12765. NLOHMANN_JSON_NAMESPACE_BEGIN
  12766. namespace detail
  12767. {
  12768. template<typename BasicJsonType>
  12769. class json_ref
  12770. {
  12771. public:
  12772. using value_type = BasicJsonType;
  12773. json_ref(value_type&& value)
  12774. : owned_value(std::move(value))
  12775. {}
  12776. json_ref(const value_type& value)
  12777. : value_ref(&value)
  12778. {}
  12779. json_ref(std::initializer_list<json_ref> init)
  12780. : owned_value(init)
  12781. {}
  12782. template <
  12783. class... Args,
  12784. enable_if_t<std::is_constructible<value_type, Args...>::value, int> = 0 >
  12785. json_ref(Args && ... args)
  12786. : owned_value(std::forward<Args>(args)...)
  12787. {}
  12788. // class should be movable only
  12789. json_ref(json_ref&&) noexcept = default;
  12790. json_ref(const json_ref&) = delete;
  12791. json_ref& operator=(const json_ref&) = delete;
  12792. json_ref& operator=(json_ref&&) = delete;
  12793. ~json_ref() = default;
  12794. value_type moved_or_copied() const
  12795. {
  12796. if (value_ref == nullptr)
  12797. {
  12798. return std::move(owned_value);
  12799. }
  12800. return *value_ref;
  12801. }
  12802. value_type const& operator*() const
  12803. {
  12804. return value_ref ? *value_ref : owned_value;
  12805. }
  12806. value_type const* operator->() const
  12807. {
  12808. return &** this;
  12809. }
  12810. private:
  12811. mutable value_type owned_value = nullptr;
  12812. value_type const* value_ref = nullptr;
  12813. };
  12814. } // namespace detail
  12815. NLOHMANN_JSON_NAMESPACE_END
  12816. // #include <nlohmann/detail/macro_scope.hpp>
  12817. // #include <nlohmann/detail/string_concat.hpp>
  12818. // #include <nlohmann/detail/string_escape.hpp>
  12819. // #include <nlohmann/detail/meta/cpp_future.hpp>
  12820. // #include <nlohmann/detail/meta/type_traits.hpp>
  12821. // #include <nlohmann/detail/output/binary_writer.hpp>
  12822. // __ _____ _____ _____
  12823. // __| | __| | | | JSON for Modern C++
  12824. // | | |__ | | | | | | version 3.11.2
  12825. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  12826. //
  12827. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  12828. // SPDX-License-Identifier: MIT
  12829. #include <algorithm> // reverse
  12830. #include <array> // array
  12831. #include <map> // map
  12832. #include <cmath> // isnan, isinf
  12833. #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
  12834. #include <cstring> // memcpy
  12835. #include <limits> // numeric_limits
  12836. #include <string> // string
  12837. #include <utility> // move
  12838. #include <vector> // vector
  12839. // #include <nlohmann/detail/input/binary_reader.hpp>
  12840. // #include <nlohmann/detail/macro_scope.hpp>
  12841. // #include <nlohmann/detail/output/output_adapters.hpp>
  12842. // __ _____ _____ _____
  12843. // __| | __| | | | JSON for Modern C++
  12844. // | | |__ | | | | | | version 3.11.2
  12845. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  12846. //
  12847. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  12848. // SPDX-License-Identifier: MIT
  12849. #include <algorithm> // copy
  12850. #include <cstddef> // size_t
  12851. #include <iterator> // back_inserter
  12852. #include <memory> // shared_ptr, make_shared
  12853. #include <string> // basic_string
  12854. #include <vector> // vector
  12855. #ifndef JSON_NO_IO
  12856. #include <ios> // streamsize
  12857. #include <ostream> // basic_ostream
  12858. #endif // JSON_NO_IO
  12859. // #include <nlohmann/detail/macro_scope.hpp>
  12860. NLOHMANN_JSON_NAMESPACE_BEGIN
  12861. namespace detail
  12862. {
  12863. /// abstract output adapter interface
  12864. template<typename CharType> struct output_adapter_protocol
  12865. {
  12866. virtual void write_character(CharType c) = 0;
  12867. virtual void write_characters(const CharType* s, std::size_t length) = 0;
  12868. virtual ~output_adapter_protocol() = default;
  12869. output_adapter_protocol() = default;
  12870. output_adapter_protocol(const output_adapter_protocol&) = default;
  12871. output_adapter_protocol(output_adapter_protocol&&) noexcept = default;
  12872. output_adapter_protocol& operator=(const output_adapter_protocol&) = default;
  12873. output_adapter_protocol& operator=(output_adapter_protocol&&) noexcept = default;
  12874. };
  12875. /// a type to simplify interfaces
  12876. template<typename CharType>
  12877. using output_adapter_t = std::shared_ptr<output_adapter_protocol<CharType>>;
  12878. /// output adapter for byte vectors
  12879. template<typename CharType, typename AllocatorType = std::allocator<CharType>>
  12880. class output_vector_adapter : public output_adapter_protocol<CharType>
  12881. {
  12882. public:
  12883. explicit output_vector_adapter(std::vector<CharType, AllocatorType>& vec) noexcept
  12884. : v(vec)
  12885. {}
  12886. void write_character(CharType c) override
  12887. {
  12888. v.push_back(c);
  12889. }
  12890. JSON_HEDLEY_NON_NULL(2)
  12891. void write_characters(const CharType* s, std::size_t length) override
  12892. {
  12893. v.insert(v.end(), s, s + length);
  12894. }
  12895. private:
  12896. std::vector<CharType, AllocatorType>& v;
  12897. };
  12898. #ifndef JSON_NO_IO
  12899. /// output adapter for output streams
  12900. template<typename CharType>
  12901. class output_stream_adapter : public output_adapter_protocol<CharType>
  12902. {
  12903. public:
  12904. explicit output_stream_adapter(std::basic_ostream<CharType>& s) noexcept
  12905. : stream(s)
  12906. {}
  12907. void write_character(CharType c) override
  12908. {
  12909. stream.put(c);
  12910. }
  12911. JSON_HEDLEY_NON_NULL(2)
  12912. void write_characters(const CharType* s, std::size_t length) override
  12913. {
  12914. stream.write(s, static_cast<std::streamsize>(length));
  12915. }
  12916. private:
  12917. std::basic_ostream<CharType>& stream;
  12918. };
  12919. #endif // JSON_NO_IO
  12920. /// output adapter for basic_string
  12921. template<typename CharType, typename StringType = std::basic_string<CharType>>
  12922. class output_string_adapter : public output_adapter_protocol<CharType>
  12923. {
  12924. public:
  12925. explicit output_string_adapter(StringType& s) noexcept
  12926. : str(s)
  12927. {}
  12928. void write_character(CharType c) override
  12929. {
  12930. str.push_back(c);
  12931. }
  12932. JSON_HEDLEY_NON_NULL(2)
  12933. void write_characters(const CharType* s, std::size_t length) override
  12934. {
  12935. str.append(s, length);
  12936. }
  12937. private:
  12938. StringType& str;
  12939. };
  12940. template<typename CharType, typename StringType = std::basic_string<CharType>>
  12941. class output_adapter
  12942. {
  12943. public:
  12944. template<typename AllocatorType = std::allocator<CharType>>
  12945. output_adapter(std::vector<CharType, AllocatorType>& vec)
  12946. : oa(std::make_shared<output_vector_adapter<CharType, AllocatorType>>(vec)) {}
  12947. #ifndef JSON_NO_IO
  12948. output_adapter(std::basic_ostream<CharType>& s)
  12949. : oa(std::make_shared<output_stream_adapter<CharType>>(s)) {}
  12950. #endif // JSON_NO_IO
  12951. output_adapter(StringType& s)
  12952. : oa(std::make_shared<output_string_adapter<CharType, StringType>>(s)) {}
  12953. operator output_adapter_t<CharType>()
  12954. {
  12955. return oa;
  12956. }
  12957. private:
  12958. output_adapter_t<CharType> oa = nullptr;
  12959. };
  12960. } // namespace detail
  12961. NLOHMANN_JSON_NAMESPACE_END
  12962. // #include <nlohmann/detail/string_concat.hpp>
  12963. NLOHMANN_JSON_NAMESPACE_BEGIN
  12964. namespace detail
  12965. {
  12966. ///////////////////
  12967. // binary writer //
  12968. ///////////////////
  12969. /*!
  12970. @brief serialization to CBOR and MessagePack values
  12971. */
  12972. template<typename BasicJsonType, typename CharType>
  12973. class binary_writer
  12974. {
  12975. using string_t = typename BasicJsonType::string_t;
  12976. using binary_t = typename BasicJsonType::binary_t;
  12977. using number_float_t = typename BasicJsonType::number_float_t;
  12978. public:
  12979. /*!
  12980. @brief create a binary writer
  12981. @param[in] adapter output adapter to write to
  12982. */
  12983. explicit binary_writer(output_adapter_t<CharType> adapter) : oa(std::move(adapter))
  12984. {
  12985. JSON_ASSERT(oa);
  12986. }
  12987. /*!
  12988. @param[in] j JSON value to serialize
  12989. @pre j.type() == value_t::object
  12990. */
  12991. void write_bson(const BasicJsonType& j)
  12992. {
  12993. switch (j.type())
  12994. {
  12995. case value_t::object:
  12996. {
  12997. write_bson_object(*j.m_value.object);
  12998. break;
  12999. }
  13000. case value_t::null:
  13001. case value_t::array:
  13002. case value_t::string:
  13003. case value_t::boolean:
  13004. case value_t::number_integer:
  13005. case value_t::number_unsigned:
  13006. case value_t::number_float:
  13007. case value_t::binary:
  13008. case value_t::discarded:
  13009. default:
  13010. {
  13011. JSON_THROW(type_error::create(317, concat("to serialize to BSON, top-level type must be object, but is ", j.type_name()), &j));
  13012. }
  13013. }
  13014. }
  13015. /*!
  13016. @param[in] j JSON value to serialize
  13017. */
  13018. void write_cbor(const BasicJsonType& j)
  13019. {
  13020. switch (j.type())
  13021. {
  13022. case value_t::null:
  13023. {
  13024. oa->write_character(to_char_type(0xF6));
  13025. break;
  13026. }
  13027. case value_t::boolean:
  13028. {
  13029. oa->write_character(j.m_value.boolean
  13030. ? to_char_type(0xF5)
  13031. : to_char_type(0xF4));
  13032. break;
  13033. }
  13034. case value_t::number_integer:
  13035. {
  13036. if (j.m_value.number_integer >= 0)
  13037. {
  13038. // CBOR does not differentiate between positive signed
  13039. // integers and unsigned integers. Therefore, we used the
  13040. // code from the value_t::number_unsigned case here.
  13041. if (j.m_value.number_integer <= 0x17)
  13042. {
  13043. write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
  13044. }
  13045. else if (j.m_value.number_integer <= (std::numeric_limits<std::uint8_t>::max)())
  13046. {
  13047. oa->write_character(to_char_type(0x18));
  13048. write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
  13049. }
  13050. else if (j.m_value.number_integer <= (std::numeric_limits<std::uint16_t>::max)())
  13051. {
  13052. oa->write_character(to_char_type(0x19));
  13053. write_number(static_cast<std::uint16_t>(j.m_value.number_integer));
  13054. }
  13055. else if (j.m_value.number_integer <= (std::numeric_limits<std::uint32_t>::max)())
  13056. {
  13057. oa->write_character(to_char_type(0x1A));
  13058. write_number(static_cast<std::uint32_t>(j.m_value.number_integer));
  13059. }
  13060. else
  13061. {
  13062. oa->write_character(to_char_type(0x1B));
  13063. write_number(static_cast<std::uint64_t>(j.m_value.number_integer));
  13064. }
  13065. }
  13066. else
  13067. {
  13068. // The conversions below encode the sign in the first
  13069. // byte, and the value is converted to a positive number.
  13070. const auto positive_number = -1 - j.m_value.number_integer;
  13071. if (j.m_value.number_integer >= -24)
  13072. {
  13073. write_number(static_cast<std::uint8_t>(0x20 + positive_number));
  13074. }
  13075. else if (positive_number <= (std::numeric_limits<std::uint8_t>::max)())
  13076. {
  13077. oa->write_character(to_char_type(0x38));
  13078. write_number(static_cast<std::uint8_t>(positive_number));
  13079. }
  13080. else if (positive_number <= (std::numeric_limits<std::uint16_t>::max)())
  13081. {
  13082. oa->write_character(to_char_type(0x39));
  13083. write_number(static_cast<std::uint16_t>(positive_number));
  13084. }
  13085. else if (positive_number <= (std::numeric_limits<std::uint32_t>::max)())
  13086. {
  13087. oa->write_character(to_char_type(0x3A));
  13088. write_number(static_cast<std::uint32_t>(positive_number));
  13089. }
  13090. else
  13091. {
  13092. oa->write_character(to_char_type(0x3B));
  13093. write_number(static_cast<std::uint64_t>(positive_number));
  13094. }
  13095. }
  13096. break;
  13097. }
  13098. case value_t::number_unsigned:
  13099. {
  13100. if (j.m_value.number_unsigned <= 0x17)
  13101. {
  13102. write_number(static_cast<std::uint8_t>(j.m_value.number_unsigned));
  13103. }
  13104. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
  13105. {
  13106. oa->write_character(to_char_type(0x18));
  13107. write_number(static_cast<std::uint8_t>(j.m_value.number_unsigned));
  13108. }
  13109. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
  13110. {
  13111. oa->write_character(to_char_type(0x19));
  13112. write_number(static_cast<std::uint16_t>(j.m_value.number_unsigned));
  13113. }
  13114. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
  13115. {
  13116. oa->write_character(to_char_type(0x1A));
  13117. write_number(static_cast<std::uint32_t>(j.m_value.number_unsigned));
  13118. }
  13119. else
  13120. {
  13121. oa->write_character(to_char_type(0x1B));
  13122. write_number(static_cast<std::uint64_t>(j.m_value.number_unsigned));
  13123. }
  13124. break;
  13125. }
  13126. case value_t::number_float:
  13127. {
  13128. if (std::isnan(j.m_value.number_float))
  13129. {
  13130. // NaN is 0xf97e00 in CBOR
  13131. oa->write_character(to_char_type(0xF9));
  13132. oa->write_character(to_char_type(0x7E));
  13133. oa->write_character(to_char_type(0x00));
  13134. }
  13135. else if (std::isinf(j.m_value.number_float))
  13136. {
  13137. // Infinity is 0xf97c00, -Infinity is 0xf9fc00
  13138. oa->write_character(to_char_type(0xf9));
  13139. oa->write_character(j.m_value.number_float > 0 ? to_char_type(0x7C) : to_char_type(0xFC));
  13140. oa->write_character(to_char_type(0x00));
  13141. }
  13142. else
  13143. {
  13144. write_compact_float(j.m_value.number_float, detail::input_format_t::cbor);
  13145. }
  13146. break;
  13147. }
  13148. case value_t::string:
  13149. {
  13150. // step 1: write control byte and the string length
  13151. const auto N = j.m_value.string->size();
  13152. if (N <= 0x17)
  13153. {
  13154. write_number(static_cast<std::uint8_t>(0x60 + N));
  13155. }
  13156. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13157. {
  13158. oa->write_character(to_char_type(0x78));
  13159. write_number(static_cast<std::uint8_t>(N));
  13160. }
  13161. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13162. {
  13163. oa->write_character(to_char_type(0x79));
  13164. write_number(static_cast<std::uint16_t>(N));
  13165. }
  13166. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13167. {
  13168. oa->write_character(to_char_type(0x7A));
  13169. write_number(static_cast<std::uint32_t>(N));
  13170. }
  13171. // LCOV_EXCL_START
  13172. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  13173. {
  13174. oa->write_character(to_char_type(0x7B));
  13175. write_number(static_cast<std::uint64_t>(N));
  13176. }
  13177. // LCOV_EXCL_STOP
  13178. // step 2: write the string
  13179. oa->write_characters(
  13180. reinterpret_cast<const CharType*>(j.m_value.string->c_str()),
  13181. j.m_value.string->size());
  13182. break;
  13183. }
  13184. case value_t::array:
  13185. {
  13186. // step 1: write control byte and the array size
  13187. const auto N = j.m_value.array->size();
  13188. if (N <= 0x17)
  13189. {
  13190. write_number(static_cast<std::uint8_t>(0x80 + N));
  13191. }
  13192. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13193. {
  13194. oa->write_character(to_char_type(0x98));
  13195. write_number(static_cast<std::uint8_t>(N));
  13196. }
  13197. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13198. {
  13199. oa->write_character(to_char_type(0x99));
  13200. write_number(static_cast<std::uint16_t>(N));
  13201. }
  13202. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13203. {
  13204. oa->write_character(to_char_type(0x9A));
  13205. write_number(static_cast<std::uint32_t>(N));
  13206. }
  13207. // LCOV_EXCL_START
  13208. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  13209. {
  13210. oa->write_character(to_char_type(0x9B));
  13211. write_number(static_cast<std::uint64_t>(N));
  13212. }
  13213. // LCOV_EXCL_STOP
  13214. // step 2: write each element
  13215. for (const auto& el : *j.m_value.array)
  13216. {
  13217. write_cbor(el);
  13218. }
  13219. break;
  13220. }
  13221. case value_t::binary:
  13222. {
  13223. if (j.m_value.binary->has_subtype())
  13224. {
  13225. if (j.m_value.binary->subtype() <= (std::numeric_limits<std::uint8_t>::max)())
  13226. {
  13227. write_number(static_cast<std::uint8_t>(0xd8));
  13228. write_number(static_cast<std::uint8_t>(j.m_value.binary->subtype()));
  13229. }
  13230. else if (j.m_value.binary->subtype() <= (std::numeric_limits<std::uint16_t>::max)())
  13231. {
  13232. write_number(static_cast<std::uint8_t>(0xd9));
  13233. write_number(static_cast<std::uint16_t>(j.m_value.binary->subtype()));
  13234. }
  13235. else if (j.m_value.binary->subtype() <= (std::numeric_limits<std::uint32_t>::max)())
  13236. {
  13237. write_number(static_cast<std::uint8_t>(0xda));
  13238. write_number(static_cast<std::uint32_t>(j.m_value.binary->subtype()));
  13239. }
  13240. else if (j.m_value.binary->subtype() <= (std::numeric_limits<std::uint64_t>::max)())
  13241. {
  13242. write_number(static_cast<std::uint8_t>(0xdb));
  13243. write_number(static_cast<std::uint64_t>(j.m_value.binary->subtype()));
  13244. }
  13245. }
  13246. // step 1: write control byte and the binary array size
  13247. const auto N = j.m_value.binary->size();
  13248. if (N <= 0x17)
  13249. {
  13250. write_number(static_cast<std::uint8_t>(0x40 + N));
  13251. }
  13252. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13253. {
  13254. oa->write_character(to_char_type(0x58));
  13255. write_number(static_cast<std::uint8_t>(N));
  13256. }
  13257. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13258. {
  13259. oa->write_character(to_char_type(0x59));
  13260. write_number(static_cast<std::uint16_t>(N));
  13261. }
  13262. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13263. {
  13264. oa->write_character(to_char_type(0x5A));
  13265. write_number(static_cast<std::uint32_t>(N));
  13266. }
  13267. // LCOV_EXCL_START
  13268. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  13269. {
  13270. oa->write_character(to_char_type(0x5B));
  13271. write_number(static_cast<std::uint64_t>(N));
  13272. }
  13273. // LCOV_EXCL_STOP
  13274. // step 2: write each element
  13275. oa->write_characters(
  13276. reinterpret_cast<const CharType*>(j.m_value.binary->data()),
  13277. N);
  13278. break;
  13279. }
  13280. case value_t::object:
  13281. {
  13282. // step 1: write control byte and the object size
  13283. const auto N = j.m_value.object->size();
  13284. if (N <= 0x17)
  13285. {
  13286. write_number(static_cast<std::uint8_t>(0xA0 + N));
  13287. }
  13288. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13289. {
  13290. oa->write_character(to_char_type(0xB8));
  13291. write_number(static_cast<std::uint8_t>(N));
  13292. }
  13293. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13294. {
  13295. oa->write_character(to_char_type(0xB9));
  13296. write_number(static_cast<std::uint16_t>(N));
  13297. }
  13298. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13299. {
  13300. oa->write_character(to_char_type(0xBA));
  13301. write_number(static_cast<std::uint32_t>(N));
  13302. }
  13303. // LCOV_EXCL_START
  13304. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  13305. {
  13306. oa->write_character(to_char_type(0xBB));
  13307. write_number(static_cast<std::uint64_t>(N));
  13308. }
  13309. // LCOV_EXCL_STOP
  13310. // step 2: write each element
  13311. for (const auto& el : *j.m_value.object)
  13312. {
  13313. write_cbor(el.first);
  13314. write_cbor(el.second);
  13315. }
  13316. break;
  13317. }
  13318. case value_t::discarded:
  13319. default:
  13320. break;
  13321. }
  13322. }
  13323. /*!
  13324. @param[in] j JSON value to serialize
  13325. */
  13326. void write_msgpack(const BasicJsonType& j)
  13327. {
  13328. switch (j.type())
  13329. {
  13330. case value_t::null: // nil
  13331. {
  13332. oa->write_character(to_char_type(0xC0));
  13333. break;
  13334. }
  13335. case value_t::boolean: // true and false
  13336. {
  13337. oa->write_character(j.m_value.boolean
  13338. ? to_char_type(0xC3)
  13339. : to_char_type(0xC2));
  13340. break;
  13341. }
  13342. case value_t::number_integer:
  13343. {
  13344. if (j.m_value.number_integer >= 0)
  13345. {
  13346. // MessagePack does not differentiate between positive
  13347. // signed integers and unsigned integers. Therefore, we used
  13348. // the code from the value_t::number_unsigned case here.
  13349. if (j.m_value.number_unsigned < 128)
  13350. {
  13351. // positive fixnum
  13352. write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
  13353. }
  13354. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
  13355. {
  13356. // uint 8
  13357. oa->write_character(to_char_type(0xCC));
  13358. write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
  13359. }
  13360. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
  13361. {
  13362. // uint 16
  13363. oa->write_character(to_char_type(0xCD));
  13364. write_number(static_cast<std::uint16_t>(j.m_value.number_integer));
  13365. }
  13366. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
  13367. {
  13368. // uint 32
  13369. oa->write_character(to_char_type(0xCE));
  13370. write_number(static_cast<std::uint32_t>(j.m_value.number_integer));
  13371. }
  13372. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
  13373. {
  13374. // uint 64
  13375. oa->write_character(to_char_type(0xCF));
  13376. write_number(static_cast<std::uint64_t>(j.m_value.number_integer));
  13377. }
  13378. }
  13379. else
  13380. {
  13381. if (j.m_value.number_integer >= -32)
  13382. {
  13383. // negative fixnum
  13384. write_number(static_cast<std::int8_t>(j.m_value.number_integer));
  13385. }
  13386. else if (j.m_value.number_integer >= (std::numeric_limits<std::int8_t>::min)() &&
  13387. j.m_value.number_integer <= (std::numeric_limits<std::int8_t>::max)())
  13388. {
  13389. // int 8
  13390. oa->write_character(to_char_type(0xD0));
  13391. write_number(static_cast<std::int8_t>(j.m_value.number_integer));
  13392. }
  13393. else if (j.m_value.number_integer >= (std::numeric_limits<std::int16_t>::min)() &&
  13394. j.m_value.number_integer <= (std::numeric_limits<std::int16_t>::max)())
  13395. {
  13396. // int 16
  13397. oa->write_character(to_char_type(0xD1));
  13398. write_number(static_cast<std::int16_t>(j.m_value.number_integer));
  13399. }
  13400. else if (j.m_value.number_integer >= (std::numeric_limits<std::int32_t>::min)() &&
  13401. j.m_value.number_integer <= (std::numeric_limits<std::int32_t>::max)())
  13402. {
  13403. // int 32
  13404. oa->write_character(to_char_type(0xD2));
  13405. write_number(static_cast<std::int32_t>(j.m_value.number_integer));
  13406. }
  13407. else if (j.m_value.number_integer >= (std::numeric_limits<std::int64_t>::min)() &&
  13408. j.m_value.number_integer <= (std::numeric_limits<std::int64_t>::max)())
  13409. {
  13410. // int 64
  13411. oa->write_character(to_char_type(0xD3));
  13412. write_number(static_cast<std::int64_t>(j.m_value.number_integer));
  13413. }
  13414. }
  13415. break;
  13416. }
  13417. case value_t::number_unsigned:
  13418. {
  13419. if (j.m_value.number_unsigned < 128)
  13420. {
  13421. // positive fixnum
  13422. write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
  13423. }
  13424. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
  13425. {
  13426. // uint 8
  13427. oa->write_character(to_char_type(0xCC));
  13428. write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
  13429. }
  13430. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
  13431. {
  13432. // uint 16
  13433. oa->write_character(to_char_type(0xCD));
  13434. write_number(static_cast<std::uint16_t>(j.m_value.number_integer));
  13435. }
  13436. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
  13437. {
  13438. // uint 32
  13439. oa->write_character(to_char_type(0xCE));
  13440. write_number(static_cast<std::uint32_t>(j.m_value.number_integer));
  13441. }
  13442. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
  13443. {
  13444. // uint 64
  13445. oa->write_character(to_char_type(0xCF));
  13446. write_number(static_cast<std::uint64_t>(j.m_value.number_integer));
  13447. }
  13448. break;
  13449. }
  13450. case value_t::number_float:
  13451. {
  13452. write_compact_float(j.m_value.number_float, detail::input_format_t::msgpack);
  13453. break;
  13454. }
  13455. case value_t::string:
  13456. {
  13457. // step 1: write control byte and the string length
  13458. const auto N = j.m_value.string->size();
  13459. if (N <= 31)
  13460. {
  13461. // fixstr
  13462. write_number(static_cast<std::uint8_t>(0xA0 | N));
  13463. }
  13464. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13465. {
  13466. // str 8
  13467. oa->write_character(to_char_type(0xD9));
  13468. write_number(static_cast<std::uint8_t>(N));
  13469. }
  13470. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13471. {
  13472. // str 16
  13473. oa->write_character(to_char_type(0xDA));
  13474. write_number(static_cast<std::uint16_t>(N));
  13475. }
  13476. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13477. {
  13478. // str 32
  13479. oa->write_character(to_char_type(0xDB));
  13480. write_number(static_cast<std::uint32_t>(N));
  13481. }
  13482. // step 2: write the string
  13483. oa->write_characters(
  13484. reinterpret_cast<const CharType*>(j.m_value.string->c_str()),
  13485. j.m_value.string->size());
  13486. break;
  13487. }
  13488. case value_t::array:
  13489. {
  13490. // step 1: write control byte and the array size
  13491. const auto N = j.m_value.array->size();
  13492. if (N <= 15)
  13493. {
  13494. // fixarray
  13495. write_number(static_cast<std::uint8_t>(0x90 | N));
  13496. }
  13497. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13498. {
  13499. // array 16
  13500. oa->write_character(to_char_type(0xDC));
  13501. write_number(static_cast<std::uint16_t>(N));
  13502. }
  13503. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13504. {
  13505. // array 32
  13506. oa->write_character(to_char_type(0xDD));
  13507. write_number(static_cast<std::uint32_t>(N));
  13508. }
  13509. // step 2: write each element
  13510. for (const auto& el : *j.m_value.array)
  13511. {
  13512. write_msgpack(el);
  13513. }
  13514. break;
  13515. }
  13516. case value_t::binary:
  13517. {
  13518. // step 0: determine if the binary type has a set subtype to
  13519. // determine whether or not to use the ext or fixext types
  13520. const bool use_ext = j.m_value.binary->has_subtype();
  13521. // step 1: write control byte and the byte string length
  13522. const auto N = j.m_value.binary->size();
  13523. if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13524. {
  13525. std::uint8_t output_type{};
  13526. bool fixed = true;
  13527. if (use_ext)
  13528. {
  13529. switch (N)
  13530. {
  13531. case 1:
  13532. output_type = 0xD4; // fixext 1
  13533. break;
  13534. case 2:
  13535. output_type = 0xD5; // fixext 2
  13536. break;
  13537. case 4:
  13538. output_type = 0xD6; // fixext 4
  13539. break;
  13540. case 8:
  13541. output_type = 0xD7; // fixext 8
  13542. break;
  13543. case 16:
  13544. output_type = 0xD8; // fixext 16
  13545. break;
  13546. default:
  13547. output_type = 0xC7; // ext 8
  13548. fixed = false;
  13549. break;
  13550. }
  13551. }
  13552. else
  13553. {
  13554. output_type = 0xC4; // bin 8
  13555. fixed = false;
  13556. }
  13557. oa->write_character(to_char_type(output_type));
  13558. if (!fixed)
  13559. {
  13560. write_number(static_cast<std::uint8_t>(N));
  13561. }
  13562. }
  13563. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13564. {
  13565. std::uint8_t output_type = use_ext
  13566. ? 0xC8 // ext 16
  13567. : 0xC5; // bin 16
  13568. oa->write_character(to_char_type(output_type));
  13569. write_number(static_cast<std::uint16_t>(N));
  13570. }
  13571. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13572. {
  13573. std::uint8_t output_type = use_ext
  13574. ? 0xC9 // ext 32
  13575. : 0xC6; // bin 32
  13576. oa->write_character(to_char_type(output_type));
  13577. write_number(static_cast<std::uint32_t>(N));
  13578. }
  13579. // step 1.5: if this is an ext type, write the subtype
  13580. if (use_ext)
  13581. {
  13582. write_number(static_cast<std::int8_t>(j.m_value.binary->subtype()));
  13583. }
  13584. // step 2: write the byte string
  13585. oa->write_characters(
  13586. reinterpret_cast<const CharType*>(j.m_value.binary->data()),
  13587. N);
  13588. break;
  13589. }
  13590. case value_t::object:
  13591. {
  13592. // step 1: write control byte and the object size
  13593. const auto N = j.m_value.object->size();
  13594. if (N <= 15)
  13595. {
  13596. // fixmap
  13597. write_number(static_cast<std::uint8_t>(0x80 | (N & 0xF)));
  13598. }
  13599. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13600. {
  13601. // map 16
  13602. oa->write_character(to_char_type(0xDE));
  13603. write_number(static_cast<std::uint16_t>(N));
  13604. }
  13605. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13606. {
  13607. // map 32
  13608. oa->write_character(to_char_type(0xDF));
  13609. write_number(static_cast<std::uint32_t>(N));
  13610. }
  13611. // step 2: write each element
  13612. for (const auto& el : *j.m_value.object)
  13613. {
  13614. write_msgpack(el.first);
  13615. write_msgpack(el.second);
  13616. }
  13617. break;
  13618. }
  13619. case value_t::discarded:
  13620. default:
  13621. break;
  13622. }
  13623. }
  13624. /*!
  13625. @param[in] j JSON value to serialize
  13626. @param[in] use_count whether to use '#' prefixes (optimized format)
  13627. @param[in] use_type whether to use '$' prefixes (optimized format)
  13628. @param[in] add_prefix whether prefixes need to be used for this value
  13629. @param[in] use_bjdata whether write in BJData format, default is false
  13630. */
  13631. void write_ubjson(const BasicJsonType& j, const bool use_count,
  13632. const bool use_type, const bool add_prefix = true,
  13633. const bool use_bjdata = false)
  13634. {
  13635. switch (j.type())
  13636. {
  13637. case value_t::null:
  13638. {
  13639. if (add_prefix)
  13640. {
  13641. oa->write_character(to_char_type('Z'));
  13642. }
  13643. break;
  13644. }
  13645. case value_t::boolean:
  13646. {
  13647. if (add_prefix)
  13648. {
  13649. oa->write_character(j.m_value.boolean
  13650. ? to_char_type('T')
  13651. : to_char_type('F'));
  13652. }
  13653. break;
  13654. }
  13655. case value_t::number_integer:
  13656. {
  13657. write_number_with_ubjson_prefix(j.m_value.number_integer, add_prefix, use_bjdata);
  13658. break;
  13659. }
  13660. case value_t::number_unsigned:
  13661. {
  13662. write_number_with_ubjson_prefix(j.m_value.number_unsigned, add_prefix, use_bjdata);
  13663. break;
  13664. }
  13665. case value_t::number_float:
  13666. {
  13667. write_number_with_ubjson_prefix(j.m_value.number_float, add_prefix, use_bjdata);
  13668. break;
  13669. }
  13670. case value_t::string:
  13671. {
  13672. if (add_prefix)
  13673. {
  13674. oa->write_character(to_char_type('S'));
  13675. }
  13676. write_number_with_ubjson_prefix(j.m_value.string->size(), true, use_bjdata);
  13677. oa->write_characters(
  13678. reinterpret_cast<const CharType*>(j.m_value.string->c_str()),
  13679. j.m_value.string->size());
  13680. break;
  13681. }
  13682. case value_t::array:
  13683. {
  13684. if (add_prefix)
  13685. {
  13686. oa->write_character(to_char_type('['));
  13687. }
  13688. bool prefix_required = true;
  13689. if (use_type && !j.m_value.array->empty())
  13690. {
  13691. JSON_ASSERT(use_count);
  13692. const CharType first_prefix = ubjson_prefix(j.front(), use_bjdata);
  13693. const bool same_prefix = std::all_of(j.begin() + 1, j.end(),
  13694. [this, first_prefix, use_bjdata](const BasicJsonType & v)
  13695. {
  13696. return ubjson_prefix(v, use_bjdata) == first_prefix;
  13697. });
  13698. std::vector<CharType> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type
  13699. if (same_prefix && !(use_bjdata && std::find(bjdx.begin(), bjdx.end(), first_prefix) != bjdx.end()))
  13700. {
  13701. prefix_required = false;
  13702. oa->write_character(to_char_type('$'));
  13703. oa->write_character(first_prefix);
  13704. }
  13705. }
  13706. if (use_count)
  13707. {
  13708. oa->write_character(to_char_type('#'));
  13709. write_number_with_ubjson_prefix(j.m_value.array->size(), true, use_bjdata);
  13710. }
  13711. for (const auto& el : *j.m_value.array)
  13712. {
  13713. write_ubjson(el, use_count, use_type, prefix_required, use_bjdata);
  13714. }
  13715. if (!use_count)
  13716. {
  13717. oa->write_character(to_char_type(']'));
  13718. }
  13719. break;
  13720. }
  13721. case value_t::binary:
  13722. {
  13723. if (add_prefix)
  13724. {
  13725. oa->write_character(to_char_type('['));
  13726. }
  13727. if (use_type && !j.m_value.binary->empty())
  13728. {
  13729. JSON_ASSERT(use_count);
  13730. oa->write_character(to_char_type('$'));
  13731. oa->write_character('U');
  13732. }
  13733. if (use_count)
  13734. {
  13735. oa->write_character(to_char_type('#'));
  13736. write_number_with_ubjson_prefix(j.m_value.binary->size(), true, use_bjdata);
  13737. }
  13738. if (use_type)
  13739. {
  13740. oa->write_characters(
  13741. reinterpret_cast<const CharType*>(j.m_value.binary->data()),
  13742. j.m_value.binary->size());
  13743. }
  13744. else
  13745. {
  13746. for (size_t i = 0; i < j.m_value.binary->size(); ++i)
  13747. {
  13748. oa->write_character(to_char_type('U'));
  13749. oa->write_character(j.m_value.binary->data()[i]);
  13750. }
  13751. }
  13752. if (!use_count)
  13753. {
  13754. oa->write_character(to_char_type(']'));
  13755. }
  13756. break;
  13757. }
  13758. case value_t::object:
  13759. {
  13760. if (use_bjdata && j.m_value.object->size() == 3 && j.m_value.object->find("_ArrayType_") != j.m_value.object->end() && j.m_value.object->find("_ArraySize_") != j.m_value.object->end() && j.m_value.object->find("_ArrayData_") != j.m_value.object->end())
  13761. {
  13762. if (!write_bjdata_ndarray(*j.m_value.object, use_count, use_type)) // decode bjdata ndarray in the JData format (https://github.com/NeuroJSON/jdata)
  13763. {
  13764. break;
  13765. }
  13766. }
  13767. if (add_prefix)
  13768. {
  13769. oa->write_character(to_char_type('{'));
  13770. }
  13771. bool prefix_required = true;
  13772. if (use_type && !j.m_value.object->empty())
  13773. {
  13774. JSON_ASSERT(use_count);
  13775. const CharType first_prefix = ubjson_prefix(j.front(), use_bjdata);
  13776. const bool same_prefix = std::all_of(j.begin(), j.end(),
  13777. [this, first_prefix, use_bjdata](const BasicJsonType & v)
  13778. {
  13779. return ubjson_prefix(v, use_bjdata) == first_prefix;
  13780. });
  13781. std::vector<CharType> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type
  13782. if (same_prefix && !(use_bjdata && std::find(bjdx.begin(), bjdx.end(), first_prefix) != bjdx.end()))
  13783. {
  13784. prefix_required = false;
  13785. oa->write_character(to_char_type('$'));
  13786. oa->write_character(first_prefix);
  13787. }
  13788. }
  13789. if (use_count)
  13790. {
  13791. oa->write_character(to_char_type('#'));
  13792. write_number_with_ubjson_prefix(j.m_value.object->size(), true, use_bjdata);
  13793. }
  13794. for (const auto& el : *j.m_value.object)
  13795. {
  13796. write_number_with_ubjson_prefix(el.first.size(), true, use_bjdata);
  13797. oa->write_characters(
  13798. reinterpret_cast<const CharType*>(el.first.c_str()),
  13799. el.first.size());
  13800. write_ubjson(el.second, use_count, use_type, prefix_required, use_bjdata);
  13801. }
  13802. if (!use_count)
  13803. {
  13804. oa->write_character(to_char_type('}'));
  13805. }
  13806. break;
  13807. }
  13808. case value_t::discarded:
  13809. default:
  13810. break;
  13811. }
  13812. }
  13813. private:
  13814. //////////
  13815. // BSON //
  13816. //////////
  13817. /*!
  13818. @return The size of a BSON document entry header, including the id marker
  13819. and the entry name size (and its null-terminator).
  13820. */
  13821. static std::size_t calc_bson_entry_header_size(const string_t& name, const BasicJsonType& j)
  13822. {
  13823. const auto it = name.find(static_cast<typename string_t::value_type>(0));
  13824. if (JSON_HEDLEY_UNLIKELY(it != BasicJsonType::string_t::npos))
  13825. {
  13826. JSON_THROW(out_of_range::create(409, concat("BSON key cannot contain code point U+0000 (at byte ", std::to_string(it), ")"), &j));
  13827. static_cast<void>(j);
  13828. }
  13829. return /*id*/ 1ul + name.size() + /*zero-terminator*/1u;
  13830. }
  13831. /*!
  13832. @brief Writes the given @a element_type and @a name to the output adapter
  13833. */
  13834. void write_bson_entry_header(const string_t& name,
  13835. const std::uint8_t element_type)
  13836. {
  13837. oa->write_character(to_char_type(element_type)); // boolean
  13838. oa->write_characters(
  13839. reinterpret_cast<const CharType*>(name.c_str()),
  13840. name.size() + 1u);
  13841. }
  13842. /*!
  13843. @brief Writes a BSON element with key @a name and boolean value @a value
  13844. */
  13845. void write_bson_boolean(const string_t& name,
  13846. const bool value)
  13847. {
  13848. write_bson_entry_header(name, 0x08);
  13849. oa->write_character(value ? to_char_type(0x01) : to_char_type(0x00));
  13850. }
  13851. /*!
  13852. @brief Writes a BSON element with key @a name and double value @a value
  13853. */
  13854. void write_bson_double(const string_t& name,
  13855. const double value)
  13856. {
  13857. write_bson_entry_header(name, 0x01);
  13858. write_number<double>(value, true);
  13859. }
  13860. /*!
  13861. @return The size of the BSON-encoded string in @a value
  13862. */
  13863. static std::size_t calc_bson_string_size(const string_t& value)
  13864. {
  13865. return sizeof(std::int32_t) + value.size() + 1ul;
  13866. }
  13867. /*!
  13868. @brief Writes a BSON element with key @a name and string value @a value
  13869. */
  13870. void write_bson_string(const string_t& name,
  13871. const string_t& value)
  13872. {
  13873. write_bson_entry_header(name, 0x02);
  13874. write_number<std::int32_t>(static_cast<std::int32_t>(value.size() + 1ul), true);
  13875. oa->write_characters(
  13876. reinterpret_cast<const CharType*>(value.c_str()),
  13877. value.size() + 1);
  13878. }
  13879. /*!
  13880. @brief Writes a BSON element with key @a name and null value
  13881. */
  13882. void write_bson_null(const string_t& name)
  13883. {
  13884. write_bson_entry_header(name, 0x0A);
  13885. }
  13886. /*!
  13887. @return The size of the BSON-encoded integer @a value
  13888. */
  13889. static std::size_t calc_bson_integer_size(const std::int64_t value)
  13890. {
  13891. return (std::numeric_limits<std::int32_t>::min)() <= value && value <= (std::numeric_limits<std::int32_t>::max)()
  13892. ? sizeof(std::int32_t)
  13893. : sizeof(std::int64_t);
  13894. }
  13895. /*!
  13896. @brief Writes a BSON element with key @a name and integer @a value
  13897. */
  13898. void write_bson_integer(const string_t& name,
  13899. const std::int64_t value)
  13900. {
  13901. if ((std::numeric_limits<std::int32_t>::min)() <= value && value <= (std::numeric_limits<std::int32_t>::max)())
  13902. {
  13903. write_bson_entry_header(name, 0x10); // int32
  13904. write_number<std::int32_t>(static_cast<std::int32_t>(value), true);
  13905. }
  13906. else
  13907. {
  13908. write_bson_entry_header(name, 0x12); // int64
  13909. write_number<std::int64_t>(static_cast<std::int64_t>(value), true);
  13910. }
  13911. }
  13912. /*!
  13913. @return The size of the BSON-encoded unsigned integer in @a j
  13914. */
  13915. static constexpr std::size_t calc_bson_unsigned_size(const std::uint64_t value) noexcept
  13916. {
  13917. return (value <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  13918. ? sizeof(std::int32_t)
  13919. : sizeof(std::int64_t);
  13920. }
  13921. /*!
  13922. @brief Writes a BSON element with key @a name and unsigned @a value
  13923. */
  13924. void write_bson_unsigned(const string_t& name,
  13925. const BasicJsonType& j)
  13926. {
  13927. if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  13928. {
  13929. write_bson_entry_header(name, 0x10 /* int32 */);
  13930. write_number<std::int32_t>(static_cast<std::int32_t>(j.m_value.number_unsigned), true);
  13931. }
  13932. else if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
  13933. {
  13934. write_bson_entry_header(name, 0x12 /* int64 */);
  13935. write_number<std::int64_t>(static_cast<std::int64_t>(j.m_value.number_unsigned), true);
  13936. }
  13937. else
  13938. {
  13939. JSON_THROW(out_of_range::create(407, concat("integer number ", std::to_string(j.m_value.number_unsigned), " cannot be represented by BSON as it does not fit int64"), &j));
  13940. }
  13941. }
  13942. /*!
  13943. @brief Writes a BSON element with key @a name and object @a value
  13944. */
  13945. void write_bson_object_entry(const string_t& name,
  13946. const typename BasicJsonType::object_t& value)
  13947. {
  13948. write_bson_entry_header(name, 0x03); // object
  13949. write_bson_object(value);
  13950. }
  13951. /*!
  13952. @return The size of the BSON-encoded array @a value
  13953. */
  13954. static std::size_t calc_bson_array_size(const typename BasicJsonType::array_t& value)
  13955. {
  13956. std::size_t array_index = 0ul;
  13957. 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)
  13958. {
  13959. return result + calc_bson_element_size(std::to_string(array_index++), el);
  13960. });
  13961. return sizeof(std::int32_t) + embedded_document_size + 1ul;
  13962. }
  13963. /*!
  13964. @return The size of the BSON-encoded binary array @a value
  13965. */
  13966. static std::size_t calc_bson_binary_size(const typename BasicJsonType::binary_t& value)
  13967. {
  13968. return sizeof(std::int32_t) + value.size() + 1ul;
  13969. }
  13970. /*!
  13971. @brief Writes a BSON element with key @a name and array @a value
  13972. */
  13973. void write_bson_array(const string_t& name,
  13974. const typename BasicJsonType::array_t& value)
  13975. {
  13976. write_bson_entry_header(name, 0x04); // array
  13977. write_number<std::int32_t>(static_cast<std::int32_t>(calc_bson_array_size(value)), true);
  13978. std::size_t array_index = 0ul;
  13979. for (const auto& el : value)
  13980. {
  13981. write_bson_element(std::to_string(array_index++), el);
  13982. }
  13983. oa->write_character(to_char_type(0x00));
  13984. }
  13985. /*!
  13986. @brief Writes a BSON element with key @a name and binary value @a value
  13987. */
  13988. void write_bson_binary(const string_t& name,
  13989. const binary_t& value)
  13990. {
  13991. write_bson_entry_header(name, 0x05);
  13992. write_number<std::int32_t>(static_cast<std::int32_t>(value.size()), true);
  13993. write_number(value.has_subtype() ? static_cast<std::uint8_t>(value.subtype()) : static_cast<std::uint8_t>(0x00));
  13994. oa->write_characters(reinterpret_cast<const CharType*>(value.data()), value.size());
  13995. }
  13996. /*!
  13997. @brief Calculates the size necessary to serialize the JSON value @a j with its @a name
  13998. @return The calculated size for the BSON document entry for @a j with the given @a name.
  13999. */
  14000. static std::size_t calc_bson_element_size(const string_t& name,
  14001. const BasicJsonType& j)
  14002. {
  14003. const auto header_size = calc_bson_entry_header_size(name, j);
  14004. switch (j.type())
  14005. {
  14006. case value_t::object:
  14007. return header_size + calc_bson_object_size(*j.m_value.object);
  14008. case value_t::array:
  14009. return header_size + calc_bson_array_size(*j.m_value.array);
  14010. case value_t::binary:
  14011. return header_size + calc_bson_binary_size(*j.m_value.binary);
  14012. case value_t::boolean:
  14013. return header_size + 1ul;
  14014. case value_t::number_float:
  14015. return header_size + 8ul;
  14016. case value_t::number_integer:
  14017. return header_size + calc_bson_integer_size(j.m_value.number_integer);
  14018. case value_t::number_unsigned:
  14019. return header_size + calc_bson_unsigned_size(j.m_value.number_unsigned);
  14020. case value_t::string:
  14021. return header_size + calc_bson_string_size(*j.m_value.string);
  14022. case value_t::null:
  14023. return header_size + 0ul;
  14024. // LCOV_EXCL_START
  14025. case value_t::discarded:
  14026. default:
  14027. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert)
  14028. return 0ul;
  14029. // LCOV_EXCL_STOP
  14030. }
  14031. }
  14032. /*!
  14033. @brief Serializes the JSON value @a j to BSON and associates it with the
  14034. key @a name.
  14035. @param name The name to associate with the JSON entity @a j within the
  14036. current BSON document
  14037. */
  14038. void write_bson_element(const string_t& name,
  14039. const BasicJsonType& j)
  14040. {
  14041. switch (j.type())
  14042. {
  14043. case value_t::object:
  14044. return write_bson_object_entry(name, *j.m_value.object);
  14045. case value_t::array:
  14046. return write_bson_array(name, *j.m_value.array);
  14047. case value_t::binary:
  14048. return write_bson_binary(name, *j.m_value.binary);
  14049. case value_t::boolean:
  14050. return write_bson_boolean(name, j.m_value.boolean);
  14051. case value_t::number_float:
  14052. return write_bson_double(name, j.m_value.number_float);
  14053. case value_t::number_integer:
  14054. return write_bson_integer(name, j.m_value.number_integer);
  14055. case value_t::number_unsigned:
  14056. return write_bson_unsigned(name, j);
  14057. case value_t::string:
  14058. return write_bson_string(name, *j.m_value.string);
  14059. case value_t::null:
  14060. return write_bson_null(name);
  14061. // LCOV_EXCL_START
  14062. case value_t::discarded:
  14063. default:
  14064. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert)
  14065. return;
  14066. // LCOV_EXCL_STOP
  14067. }
  14068. }
  14069. /*!
  14070. @brief Calculates the size of the BSON serialization of the given
  14071. JSON-object @a j.
  14072. @param[in] value JSON value to serialize
  14073. @pre value.type() == value_t::object
  14074. */
  14075. static std::size_t calc_bson_object_size(const typename BasicJsonType::object_t& value)
  14076. {
  14077. std::size_t document_size = std::accumulate(value.begin(), value.end(), static_cast<std::size_t>(0),
  14078. [](size_t result, const typename BasicJsonType::object_t::value_type & el)
  14079. {
  14080. return result += calc_bson_element_size(el.first, el.second);
  14081. });
  14082. return sizeof(std::int32_t) + document_size + 1ul;
  14083. }
  14084. /*!
  14085. @param[in] value JSON value to serialize
  14086. @pre value.type() == value_t::object
  14087. */
  14088. void write_bson_object(const typename BasicJsonType::object_t& value)
  14089. {
  14090. write_number<std::int32_t>(static_cast<std::int32_t>(calc_bson_object_size(value)), true);
  14091. for (const auto& el : value)
  14092. {
  14093. write_bson_element(el.first, el.second);
  14094. }
  14095. oa->write_character(to_char_type(0x00));
  14096. }
  14097. //////////
  14098. // CBOR //
  14099. //////////
  14100. static constexpr CharType get_cbor_float_prefix(float /*unused*/)
  14101. {
  14102. return to_char_type(0xFA); // Single-Precision Float
  14103. }
  14104. static constexpr CharType get_cbor_float_prefix(double /*unused*/)
  14105. {
  14106. return to_char_type(0xFB); // Double-Precision Float
  14107. }
  14108. /////////////
  14109. // MsgPack //
  14110. /////////////
  14111. static constexpr CharType get_msgpack_float_prefix(float /*unused*/)
  14112. {
  14113. return to_char_type(0xCA); // float 32
  14114. }
  14115. static constexpr CharType get_msgpack_float_prefix(double /*unused*/)
  14116. {
  14117. return to_char_type(0xCB); // float 64
  14118. }
  14119. ////////////
  14120. // UBJSON //
  14121. ////////////
  14122. // UBJSON: write number (floating point)
  14123. template<typename NumberType, typename std::enable_if<
  14124. std::is_floating_point<NumberType>::value, int>::type = 0>
  14125. void write_number_with_ubjson_prefix(const NumberType n,
  14126. const bool add_prefix,
  14127. const bool use_bjdata)
  14128. {
  14129. if (add_prefix)
  14130. {
  14131. oa->write_character(get_ubjson_float_prefix(n));
  14132. }
  14133. write_number(n, use_bjdata);
  14134. }
  14135. // UBJSON: write number (unsigned integer)
  14136. template<typename NumberType, typename std::enable_if<
  14137. std::is_unsigned<NumberType>::value, int>::type = 0>
  14138. void write_number_with_ubjson_prefix(const NumberType n,
  14139. const bool add_prefix,
  14140. const bool use_bjdata)
  14141. {
  14142. if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int8_t>::max)()))
  14143. {
  14144. if (add_prefix)
  14145. {
  14146. oa->write_character(to_char_type('i')); // int8
  14147. }
  14148. write_number(static_cast<std::uint8_t>(n), use_bjdata);
  14149. }
  14150. else if (n <= (std::numeric_limits<std::uint8_t>::max)())
  14151. {
  14152. if (add_prefix)
  14153. {
  14154. oa->write_character(to_char_type('U')); // uint8
  14155. }
  14156. write_number(static_cast<std::uint8_t>(n), use_bjdata);
  14157. }
  14158. else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int16_t>::max)()))
  14159. {
  14160. if (add_prefix)
  14161. {
  14162. oa->write_character(to_char_type('I')); // int16
  14163. }
  14164. write_number(static_cast<std::int16_t>(n), use_bjdata);
  14165. }
  14166. else if (use_bjdata && n <= static_cast<uint64_t>((std::numeric_limits<uint16_t>::max)()))
  14167. {
  14168. if (add_prefix)
  14169. {
  14170. oa->write_character(to_char_type('u')); // uint16 - bjdata only
  14171. }
  14172. write_number(static_cast<std::uint16_t>(n), use_bjdata);
  14173. }
  14174. else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  14175. {
  14176. if (add_prefix)
  14177. {
  14178. oa->write_character(to_char_type('l')); // int32
  14179. }
  14180. write_number(static_cast<std::int32_t>(n), use_bjdata);
  14181. }
  14182. else if (use_bjdata && n <= static_cast<uint64_t>((std::numeric_limits<uint32_t>::max)()))
  14183. {
  14184. if (add_prefix)
  14185. {
  14186. oa->write_character(to_char_type('m')); // uint32 - bjdata only
  14187. }
  14188. write_number(static_cast<std::uint32_t>(n), use_bjdata);
  14189. }
  14190. else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
  14191. {
  14192. if (add_prefix)
  14193. {
  14194. oa->write_character(to_char_type('L')); // int64
  14195. }
  14196. write_number(static_cast<std::int64_t>(n), use_bjdata);
  14197. }
  14198. else if (use_bjdata && n <= (std::numeric_limits<uint64_t>::max)())
  14199. {
  14200. if (add_prefix)
  14201. {
  14202. oa->write_character(to_char_type('M')); // uint64 - bjdata only
  14203. }
  14204. write_number(static_cast<std::uint64_t>(n), use_bjdata);
  14205. }
  14206. else
  14207. {
  14208. if (add_prefix)
  14209. {
  14210. oa->write_character(to_char_type('H')); // high-precision number
  14211. }
  14212. const auto number = BasicJsonType(n).dump();
  14213. write_number_with_ubjson_prefix(number.size(), true, use_bjdata);
  14214. for (std::size_t i = 0; i < number.size(); ++i)
  14215. {
  14216. oa->write_character(to_char_type(static_cast<std::uint8_t>(number[i])));
  14217. }
  14218. }
  14219. }
  14220. // UBJSON: write number (signed integer)
  14221. template < typename NumberType, typename std::enable_if <
  14222. std::is_signed<NumberType>::value&&
  14223. !std::is_floating_point<NumberType>::value, int >::type = 0 >
  14224. void write_number_with_ubjson_prefix(const NumberType n,
  14225. const bool add_prefix,
  14226. const bool use_bjdata)
  14227. {
  14228. if ((std::numeric_limits<std::int8_t>::min)() <= n && n <= (std::numeric_limits<std::int8_t>::max)())
  14229. {
  14230. if (add_prefix)
  14231. {
  14232. oa->write_character(to_char_type('i')); // int8
  14233. }
  14234. write_number(static_cast<std::int8_t>(n), use_bjdata);
  14235. }
  14236. 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)()))
  14237. {
  14238. if (add_prefix)
  14239. {
  14240. oa->write_character(to_char_type('U')); // uint8
  14241. }
  14242. write_number(static_cast<std::uint8_t>(n), use_bjdata);
  14243. }
  14244. else if ((std::numeric_limits<std::int16_t>::min)() <= n && n <= (std::numeric_limits<std::int16_t>::max)())
  14245. {
  14246. if (add_prefix)
  14247. {
  14248. oa->write_character(to_char_type('I')); // int16
  14249. }
  14250. write_number(static_cast<std::int16_t>(n), use_bjdata);
  14251. }
  14252. 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)())))
  14253. {
  14254. if (add_prefix)
  14255. {
  14256. oa->write_character(to_char_type('u')); // uint16 - bjdata only
  14257. }
  14258. write_number(static_cast<uint16_t>(n), use_bjdata);
  14259. }
  14260. else if ((std::numeric_limits<std::int32_t>::min)() <= n && n <= (std::numeric_limits<std::int32_t>::max)())
  14261. {
  14262. if (add_prefix)
  14263. {
  14264. oa->write_character(to_char_type('l')); // int32
  14265. }
  14266. write_number(static_cast<std::int32_t>(n), use_bjdata);
  14267. }
  14268. 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)())))
  14269. {
  14270. if (add_prefix)
  14271. {
  14272. oa->write_character(to_char_type('m')); // uint32 - bjdata only
  14273. }
  14274. write_number(static_cast<uint32_t>(n), use_bjdata);
  14275. }
  14276. else if ((std::numeric_limits<std::int64_t>::min)() <= n && n <= (std::numeric_limits<std::int64_t>::max)())
  14277. {
  14278. if (add_prefix)
  14279. {
  14280. oa->write_character(to_char_type('L')); // int64
  14281. }
  14282. write_number(static_cast<std::int64_t>(n), use_bjdata);
  14283. }
  14284. // LCOV_EXCL_START
  14285. else
  14286. {
  14287. if (add_prefix)
  14288. {
  14289. oa->write_character(to_char_type('H')); // high-precision number
  14290. }
  14291. const auto number = BasicJsonType(n).dump();
  14292. write_number_with_ubjson_prefix(number.size(), true, use_bjdata);
  14293. for (std::size_t i = 0; i < number.size(); ++i)
  14294. {
  14295. oa->write_character(to_char_type(static_cast<std::uint8_t>(number[i])));
  14296. }
  14297. }
  14298. // LCOV_EXCL_STOP
  14299. }
  14300. /*!
  14301. @brief determine the type prefix of container values
  14302. */
  14303. CharType ubjson_prefix(const BasicJsonType& j, const bool use_bjdata) const noexcept
  14304. {
  14305. switch (j.type())
  14306. {
  14307. case value_t::null:
  14308. return 'Z';
  14309. case value_t::boolean:
  14310. return j.m_value.boolean ? 'T' : 'F';
  14311. case value_t::number_integer:
  14312. {
  14313. if ((std::numeric_limits<std::int8_t>::min)() <= j.m_value.number_integer && j.m_value.number_integer <= (std::numeric_limits<std::int8_t>::max)())
  14314. {
  14315. return 'i';
  14316. }
  14317. if ((std::numeric_limits<std::uint8_t>::min)() <= j.m_value.number_integer && j.m_value.number_integer <= (std::numeric_limits<std::uint8_t>::max)())
  14318. {
  14319. return 'U';
  14320. }
  14321. if ((std::numeric_limits<std::int16_t>::min)() <= j.m_value.number_integer && j.m_value.number_integer <= (std::numeric_limits<std::int16_t>::max)())
  14322. {
  14323. return 'I';
  14324. }
  14325. if (use_bjdata && ((std::numeric_limits<std::uint16_t>::min)() <= j.m_value.number_integer && j.m_value.number_integer <= (std::numeric_limits<std::uint16_t>::max)()))
  14326. {
  14327. return 'u';
  14328. }
  14329. if ((std::numeric_limits<std::int32_t>::min)() <= j.m_value.number_integer && j.m_value.number_integer <= (std::numeric_limits<std::int32_t>::max)())
  14330. {
  14331. return 'l';
  14332. }
  14333. if (use_bjdata && ((std::numeric_limits<std::uint32_t>::min)() <= j.m_value.number_integer && j.m_value.number_integer <= (std::numeric_limits<std::uint32_t>::max)()))
  14334. {
  14335. return 'm';
  14336. }
  14337. if ((std::numeric_limits<std::int64_t>::min)() <= j.m_value.number_integer && j.m_value.number_integer <= (std::numeric_limits<std::int64_t>::max)())
  14338. {
  14339. return 'L';
  14340. }
  14341. // anything else is treated as high-precision number
  14342. return 'H'; // LCOV_EXCL_LINE
  14343. }
  14344. case value_t::number_unsigned:
  14345. {
  14346. if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int8_t>::max)()))
  14347. {
  14348. return 'i';
  14349. }
  14350. if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint8_t>::max)()))
  14351. {
  14352. return 'U';
  14353. }
  14354. if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int16_t>::max)()))
  14355. {
  14356. return 'I';
  14357. }
  14358. if (use_bjdata && j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint16_t>::max)()))
  14359. {
  14360. return 'u';
  14361. }
  14362. if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  14363. {
  14364. return 'l';
  14365. }
  14366. if (use_bjdata && j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint32_t>::max)()))
  14367. {
  14368. return 'm';
  14369. }
  14370. if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
  14371. {
  14372. return 'L';
  14373. }
  14374. if (use_bjdata && j.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
  14375. {
  14376. return 'M';
  14377. }
  14378. // anything else is treated as high-precision number
  14379. return 'H'; // LCOV_EXCL_LINE
  14380. }
  14381. case value_t::number_float:
  14382. return get_ubjson_float_prefix(j.m_value.number_float);
  14383. case value_t::string:
  14384. return 'S';
  14385. case value_t::array: // fallthrough
  14386. case value_t::binary:
  14387. return '[';
  14388. case value_t::object:
  14389. return '{';
  14390. case value_t::discarded:
  14391. default: // discarded values
  14392. return 'N';
  14393. }
  14394. }
  14395. static constexpr CharType get_ubjson_float_prefix(float /*unused*/)
  14396. {
  14397. return 'd'; // float 32
  14398. }
  14399. static constexpr CharType get_ubjson_float_prefix(double /*unused*/)
  14400. {
  14401. return 'D'; // float 64
  14402. }
  14403. /*!
  14404. @return false if the object is successfully converted to a bjdata ndarray, true if the type or size is invalid
  14405. */
  14406. bool write_bjdata_ndarray(const typename BasicJsonType::object_t& value, const bool use_count, const bool use_type)
  14407. {
  14408. std::map<string_t, CharType> bjdtype = {{"uint8", 'U'}, {"int8", 'i'}, {"uint16", 'u'}, {"int16", 'I'},
  14409. {"uint32", 'm'}, {"int32", 'l'}, {"uint64", 'M'}, {"int64", 'L'}, {"single", 'd'}, {"double", 'D'}, {"char", 'C'}
  14410. };
  14411. string_t key = "_ArrayType_";
  14412. auto it = bjdtype.find(static_cast<string_t>(value.at(key)));
  14413. if (it == bjdtype.end())
  14414. {
  14415. return true;
  14416. }
  14417. CharType dtype = it->second;
  14418. key = "_ArraySize_";
  14419. std::size_t len = (value.at(key).empty() ? 0 : 1);
  14420. for (const auto& el : value.at(key))
  14421. {
  14422. len *= static_cast<std::size_t>(el.m_value.number_unsigned);
  14423. }
  14424. key = "_ArrayData_";
  14425. if (value.at(key).size() != len)
  14426. {
  14427. return true;
  14428. }
  14429. oa->write_character('[');
  14430. oa->write_character('$');
  14431. oa->write_character(dtype);
  14432. oa->write_character('#');
  14433. key = "_ArraySize_";
  14434. write_ubjson(value.at(key), use_count, use_type, true, true);
  14435. key = "_ArrayData_";
  14436. if (dtype == 'U' || dtype == 'C')
  14437. {
  14438. for (const auto& el : value.at(key))
  14439. {
  14440. write_number(static_cast<std::uint8_t>(el.m_value.number_unsigned), true);
  14441. }
  14442. }
  14443. else if (dtype == 'i')
  14444. {
  14445. for (const auto& el : value.at(key))
  14446. {
  14447. write_number(static_cast<std::int8_t>(el.m_value.number_integer), true);
  14448. }
  14449. }
  14450. else if (dtype == 'u')
  14451. {
  14452. for (const auto& el : value.at(key))
  14453. {
  14454. write_number(static_cast<std::uint16_t>(el.m_value.number_unsigned), true);
  14455. }
  14456. }
  14457. else if (dtype == 'I')
  14458. {
  14459. for (const auto& el : value.at(key))
  14460. {
  14461. write_number(static_cast<std::int16_t>(el.m_value.number_integer), true);
  14462. }
  14463. }
  14464. else if (dtype == 'm')
  14465. {
  14466. for (const auto& el : value.at(key))
  14467. {
  14468. write_number(static_cast<std::uint32_t>(el.m_value.number_unsigned), true);
  14469. }
  14470. }
  14471. else if (dtype == 'l')
  14472. {
  14473. for (const auto& el : value.at(key))
  14474. {
  14475. write_number(static_cast<std::int32_t>(el.m_value.number_integer), true);
  14476. }
  14477. }
  14478. else if (dtype == 'M')
  14479. {
  14480. for (const auto& el : value.at(key))
  14481. {
  14482. write_number(static_cast<std::uint64_t>(el.m_value.number_unsigned), true);
  14483. }
  14484. }
  14485. else if (dtype == 'L')
  14486. {
  14487. for (const auto& el : value.at(key))
  14488. {
  14489. write_number(static_cast<std::int64_t>(el.m_value.number_integer), true);
  14490. }
  14491. }
  14492. else if (dtype == 'd')
  14493. {
  14494. for (const auto& el : value.at(key))
  14495. {
  14496. write_number(static_cast<float>(el.m_value.number_float), true);
  14497. }
  14498. }
  14499. else if (dtype == 'D')
  14500. {
  14501. for (const auto& el : value.at(key))
  14502. {
  14503. write_number(static_cast<double>(el.m_value.number_float), true);
  14504. }
  14505. }
  14506. return false;
  14507. }
  14508. ///////////////////////
  14509. // Utility functions //
  14510. ///////////////////////
  14511. /*
  14512. @brief write a number to output input
  14513. @param[in] n number of type @a NumberType
  14514. @param[in] OutputIsLittleEndian Set to true if output data is
  14515. required to be little endian
  14516. @tparam NumberType the type of the number
  14517. @note This function needs to respect the system's endianness, because bytes
  14518. in CBOR, MessagePack, and UBJSON are stored in network order (big
  14519. endian) and therefore need reordering on little endian systems.
  14520. On the other hand, BSON and BJData use little endian and should reorder
  14521. on big endian systems.
  14522. */
  14523. template<typename NumberType>
  14524. void write_number(const NumberType n, const bool OutputIsLittleEndian = false)
  14525. {
  14526. // step 1: write number to array of length NumberType
  14527. std::array<CharType, sizeof(NumberType)> vec{};
  14528. std::memcpy(vec.data(), &n, sizeof(NumberType));
  14529. // step 2: write array to output (with possible reordering)
  14530. if (is_little_endian != OutputIsLittleEndian)
  14531. {
  14532. // reverse byte order prior to conversion if necessary
  14533. std::reverse(vec.begin(), vec.end());
  14534. }
  14535. oa->write_characters(vec.data(), sizeof(NumberType));
  14536. }
  14537. void write_compact_float(const number_float_t n, detail::input_format_t format)
  14538. {
  14539. #ifdef __GNUC__
  14540. #pragma GCC diagnostic push
  14541. #pragma GCC diagnostic ignored "-Wfloat-equal"
  14542. #endif
  14543. if (static_cast<double>(n) >= static_cast<double>(std::numeric_limits<float>::lowest()) &&
  14544. static_cast<double>(n) <= static_cast<double>((std::numeric_limits<float>::max)()) &&
  14545. static_cast<double>(static_cast<float>(n)) == static_cast<double>(n))
  14546. {
  14547. oa->write_character(format == detail::input_format_t::cbor
  14548. ? get_cbor_float_prefix(static_cast<float>(n))
  14549. : get_msgpack_float_prefix(static_cast<float>(n)));
  14550. write_number(static_cast<float>(n));
  14551. }
  14552. else
  14553. {
  14554. oa->write_character(format == detail::input_format_t::cbor
  14555. ? get_cbor_float_prefix(n)
  14556. : get_msgpack_float_prefix(n));
  14557. write_number(n);
  14558. }
  14559. #ifdef __GNUC__
  14560. #pragma GCC diagnostic pop
  14561. #endif
  14562. }
  14563. public:
  14564. // The following to_char_type functions are implement the conversion
  14565. // between uint8_t and CharType. In case CharType is not unsigned,
  14566. // such a conversion is required to allow values greater than 128.
  14567. // See <https://github.com/nlohmann/json/issues/1286> for a discussion.
  14568. template < typename C = CharType,
  14569. enable_if_t < std::is_signed<C>::value && std::is_signed<char>::value > * = nullptr >
  14570. static constexpr CharType to_char_type(std::uint8_t x) noexcept
  14571. {
  14572. return *reinterpret_cast<char*>(&x);
  14573. }
  14574. template < typename C = CharType,
  14575. enable_if_t < std::is_signed<C>::value && std::is_unsigned<char>::value > * = nullptr >
  14576. static CharType to_char_type(std::uint8_t x) noexcept
  14577. {
  14578. static_assert(sizeof(std::uint8_t) == sizeof(CharType), "size of CharType must be equal to std::uint8_t");
  14579. static_assert(std::is_trivial<CharType>::value, "CharType must be trivial");
  14580. CharType result;
  14581. std::memcpy(&result, &x, sizeof(x));
  14582. return result;
  14583. }
  14584. template<typename C = CharType,
  14585. enable_if_t<std::is_unsigned<C>::value>* = nullptr>
  14586. static constexpr CharType to_char_type(std::uint8_t x) noexcept
  14587. {
  14588. return x;
  14589. }
  14590. template < typename InputCharType, typename C = CharType,
  14591. enable_if_t <
  14592. std::is_signed<C>::value &&
  14593. std::is_signed<char>::value &&
  14594. std::is_same<char, typename std::remove_cv<InputCharType>::type>::value
  14595. > * = nullptr >
  14596. static constexpr CharType to_char_type(InputCharType x) noexcept
  14597. {
  14598. return x;
  14599. }
  14600. private:
  14601. /// whether we can assume little endianness
  14602. const bool is_little_endian = little_endianness();
  14603. /// the output
  14604. output_adapter_t<CharType> oa = nullptr;
  14605. };
  14606. } // namespace detail
  14607. NLOHMANN_JSON_NAMESPACE_END
  14608. // #include <nlohmann/detail/output/output_adapters.hpp>
  14609. // #include <nlohmann/detail/output/serializer.hpp>
  14610. // __ _____ _____ _____
  14611. // __| | __| | | | JSON for Modern C++
  14612. // | | |__ | | | | | | version 3.11.2
  14613. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  14614. //
  14615. // SPDX-FileCopyrightText: 2008-2009 Björn Hoehrmann <bjoern@hoehrmann.de>
  14616. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  14617. // SPDX-License-Identifier: MIT
  14618. #include <algorithm> // reverse, remove, fill, find, none_of
  14619. #include <array> // array
  14620. #include <clocale> // localeconv, lconv
  14621. #include <cmath> // labs, isfinite, isnan, signbit
  14622. #include <cstddef> // size_t, ptrdiff_t
  14623. #include <cstdint> // uint8_t
  14624. #include <cstdio> // snprintf
  14625. #include <limits> // numeric_limits
  14626. #include <string> // string, char_traits
  14627. #include <iomanip> // setfill, setw
  14628. #include <type_traits> // is_same
  14629. #include <utility> // move
  14630. // #include <nlohmann/detail/conversions/to_chars.hpp>
  14631. // __ _____ _____ _____
  14632. // __| | __| | | | JSON for Modern C++
  14633. // | | |__ | | | | | | version 3.11.2
  14634. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  14635. //
  14636. // SPDX-FileCopyrightText: 2009 Florian Loitsch <https://florian.loitsch.com/>
  14637. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  14638. // SPDX-License-Identifier: MIT
  14639. #include <array> // array
  14640. #include <cmath> // signbit, isfinite
  14641. #include <cstdint> // intN_t, uintN_t
  14642. #include <cstring> // memcpy, memmove
  14643. #include <limits> // numeric_limits
  14644. #include <type_traits> // conditional
  14645. // #include <nlohmann/detail/macro_scope.hpp>
  14646. NLOHMANN_JSON_NAMESPACE_BEGIN
  14647. namespace detail
  14648. {
  14649. /*!
  14650. @brief implements the Grisu2 algorithm for binary to decimal floating-point
  14651. conversion.
  14652. This implementation is a slightly modified version of the reference
  14653. implementation which may be obtained from
  14654. http://florian.loitsch.com/publications (bench.tar.gz).
  14655. The code is distributed under the MIT license, Copyright (c) 2009 Florian Loitsch.
  14656. For a detailed description of the algorithm see:
  14657. [1] Loitsch, "Printing Floating-Point Numbers Quickly and Accurately with
  14658. Integers", Proceedings of the ACM SIGPLAN 2010 Conference on Programming
  14659. Language Design and Implementation, PLDI 2010
  14660. [2] Burger, Dybvig, "Printing Floating-Point Numbers Quickly and Accurately",
  14661. Proceedings of the ACM SIGPLAN 1996 Conference on Programming Language
  14662. Design and Implementation, PLDI 1996
  14663. */
  14664. namespace dtoa_impl
  14665. {
  14666. template<typename Target, typename Source>
  14667. Target reinterpret_bits(const Source source)
  14668. {
  14669. static_assert(sizeof(Target) == sizeof(Source), "size mismatch");
  14670. Target target;
  14671. std::memcpy(&target, &source, sizeof(Source));
  14672. return target;
  14673. }
  14674. struct diyfp // f * 2^e
  14675. {
  14676. static constexpr int kPrecision = 64; // = q
  14677. std::uint64_t f = 0;
  14678. int e = 0;
  14679. constexpr diyfp(std::uint64_t f_, int e_) noexcept : f(f_), e(e_) {}
  14680. /*!
  14681. @brief returns x - y
  14682. @pre x.e == y.e and x.f >= y.f
  14683. */
  14684. static diyfp sub(const diyfp& x, const diyfp& y) noexcept
  14685. {
  14686. JSON_ASSERT(x.e == y.e);
  14687. JSON_ASSERT(x.f >= y.f);
  14688. return {x.f - y.f, x.e};
  14689. }
  14690. /*!
  14691. @brief returns x * y
  14692. @note The result is rounded. (Only the upper q bits are returned.)
  14693. */
  14694. static diyfp mul(const diyfp& x, const diyfp& y) noexcept
  14695. {
  14696. static_assert(kPrecision == 64, "internal error");
  14697. // Computes:
  14698. // f = round((x.f * y.f) / 2^q)
  14699. // e = x.e + y.e + q
  14700. // Emulate the 64-bit * 64-bit multiplication:
  14701. //
  14702. // p = u * v
  14703. // = (u_lo + 2^32 u_hi) (v_lo + 2^32 v_hi)
  14704. // = (u_lo v_lo ) + 2^32 ((u_lo v_hi ) + (u_hi v_lo )) + 2^64 (u_hi v_hi )
  14705. // = (p0 ) + 2^32 ((p1 ) + (p2 )) + 2^64 (p3 )
  14706. // = (p0_lo + 2^32 p0_hi) + 2^32 ((p1_lo + 2^32 p1_hi) + (p2_lo + 2^32 p2_hi)) + 2^64 (p3 )
  14707. // = (p0_lo ) + 2^32 (p0_hi + p1_lo + p2_lo ) + 2^64 (p1_hi + p2_hi + p3)
  14708. // = (p0_lo ) + 2^32 (Q ) + 2^64 (H )
  14709. // = (p0_lo ) + 2^32 (Q_lo + 2^32 Q_hi ) + 2^64 (H )
  14710. //
  14711. // (Since Q might be larger than 2^32 - 1)
  14712. //
  14713. // = (p0_lo + 2^32 Q_lo) + 2^64 (Q_hi + H)
  14714. //
  14715. // (Q_hi + H does not overflow a 64-bit int)
  14716. //
  14717. // = p_lo + 2^64 p_hi
  14718. const std::uint64_t u_lo = x.f & 0xFFFFFFFFu;
  14719. const std::uint64_t u_hi = x.f >> 32u;
  14720. const std::uint64_t v_lo = y.f & 0xFFFFFFFFu;
  14721. const std::uint64_t v_hi = y.f >> 32u;
  14722. const std::uint64_t p0 = u_lo * v_lo;
  14723. const std::uint64_t p1 = u_lo * v_hi;
  14724. const std::uint64_t p2 = u_hi * v_lo;
  14725. const std::uint64_t p3 = u_hi * v_hi;
  14726. const std::uint64_t p0_hi = p0 >> 32u;
  14727. const std::uint64_t p1_lo = p1 & 0xFFFFFFFFu;
  14728. const std::uint64_t p1_hi = p1 >> 32u;
  14729. const std::uint64_t p2_lo = p2 & 0xFFFFFFFFu;
  14730. const std::uint64_t p2_hi = p2 >> 32u;
  14731. std::uint64_t Q = p0_hi + p1_lo + p2_lo;
  14732. // The full product might now be computed as
  14733. //
  14734. // p_hi = p3 + p2_hi + p1_hi + (Q >> 32)
  14735. // p_lo = p0_lo + (Q << 32)
  14736. //
  14737. // But in this particular case here, the full p_lo is not required.
  14738. // Effectively we only need to add the highest bit in p_lo to p_hi (and
  14739. // Q_hi + 1 does not overflow).
  14740. Q += std::uint64_t{1} << (64u - 32u - 1u); // round, ties up
  14741. const std::uint64_t h = p3 + p2_hi + p1_hi + (Q >> 32u);
  14742. return {h, x.e + y.e + 64};
  14743. }
  14744. /*!
  14745. @brief normalize x such that the significand is >= 2^(q-1)
  14746. @pre x.f != 0
  14747. */
  14748. static diyfp normalize(diyfp x) noexcept
  14749. {
  14750. JSON_ASSERT(x.f != 0);
  14751. while ((x.f >> 63u) == 0)
  14752. {
  14753. x.f <<= 1u;
  14754. x.e--;
  14755. }
  14756. return x;
  14757. }
  14758. /*!
  14759. @brief normalize x such that the result has the exponent E
  14760. @pre e >= x.e and the upper e - x.e bits of x.f must be zero.
  14761. */
  14762. static diyfp normalize_to(const diyfp& x, const int target_exponent) noexcept
  14763. {
  14764. const int delta = x.e - target_exponent;
  14765. JSON_ASSERT(delta >= 0);
  14766. JSON_ASSERT(((x.f << delta) >> delta) == x.f);
  14767. return {x.f << delta, target_exponent};
  14768. }
  14769. };
  14770. struct boundaries
  14771. {
  14772. diyfp w;
  14773. diyfp minus;
  14774. diyfp plus;
  14775. };
  14776. /*!
  14777. Compute the (normalized) diyfp representing the input number 'value' and its
  14778. boundaries.
  14779. @pre value must be finite and positive
  14780. */
  14781. template<typename FloatType>
  14782. boundaries compute_boundaries(FloatType value)
  14783. {
  14784. JSON_ASSERT(std::isfinite(value));
  14785. JSON_ASSERT(value > 0);
  14786. // Convert the IEEE representation into a diyfp.
  14787. //
  14788. // If v is denormal:
  14789. // value = 0.F * 2^(1 - bias) = ( F) * 2^(1 - bias - (p-1))
  14790. // If v is normalized:
  14791. // value = 1.F * 2^(E - bias) = (2^(p-1) + F) * 2^(E - bias - (p-1))
  14792. static_assert(std::numeric_limits<FloatType>::is_iec559,
  14793. "internal error: dtoa_short requires an IEEE-754 floating-point implementation");
  14794. constexpr int kPrecision = std::numeric_limits<FloatType>::digits; // = p (includes the hidden bit)
  14795. constexpr int kBias = std::numeric_limits<FloatType>::max_exponent - 1 + (kPrecision - 1);
  14796. constexpr int kMinExp = 1 - kBias;
  14797. constexpr std::uint64_t kHiddenBit = std::uint64_t{1} << (kPrecision - 1); // = 2^(p-1)
  14798. using bits_type = typename std::conditional<kPrecision == 24, std::uint32_t, std::uint64_t >::type;
  14799. const auto bits = static_cast<std::uint64_t>(reinterpret_bits<bits_type>(value));
  14800. const std::uint64_t E = bits >> (kPrecision - 1);
  14801. const std::uint64_t F = bits & (kHiddenBit - 1);
  14802. const bool is_denormal = E == 0;
  14803. const diyfp v = is_denormal
  14804. ? diyfp(F, kMinExp)
  14805. : diyfp(F + kHiddenBit, static_cast<int>(E) - kBias);
  14806. // Compute the boundaries m- and m+ of the floating-point value
  14807. // v = f * 2^e.
  14808. //
  14809. // Determine v- and v+, the floating-point predecessor and successor if v,
  14810. // respectively.
  14811. //
  14812. // v- = v - 2^e if f != 2^(p-1) or e == e_min (A)
  14813. // = v - 2^(e-1) if f == 2^(p-1) and e > e_min (B)
  14814. //
  14815. // v+ = v + 2^e
  14816. //
  14817. // Let m- = (v- + v) / 2 and m+ = (v + v+) / 2. All real numbers _strictly_
  14818. // between m- and m+ round to v, regardless of how the input rounding
  14819. // algorithm breaks ties.
  14820. //
  14821. // ---+-------------+-------------+-------------+-------------+--- (A)
  14822. // v- m- v m+ v+
  14823. //
  14824. // -----------------+------+------+-------------+-------------+--- (B)
  14825. // v- m- v m+ v+
  14826. const bool lower_boundary_is_closer = F == 0 && E > 1;
  14827. const diyfp m_plus = diyfp(2 * v.f + 1, v.e - 1);
  14828. const diyfp m_minus = lower_boundary_is_closer
  14829. ? diyfp(4 * v.f - 1, v.e - 2) // (B)
  14830. : diyfp(2 * v.f - 1, v.e - 1); // (A)
  14831. // Determine the normalized w+ = m+.
  14832. const diyfp w_plus = diyfp::normalize(m_plus);
  14833. // Determine w- = m- such that e_(w-) = e_(w+).
  14834. const diyfp w_minus = diyfp::normalize_to(m_minus, w_plus.e);
  14835. return {diyfp::normalize(v), w_minus, w_plus};
  14836. }
  14837. // Given normalized diyfp w, Grisu needs to find a (normalized) cached
  14838. // power-of-ten c, such that the exponent of the product c * w = f * 2^e lies
  14839. // within a certain range [alpha, gamma] (Definition 3.2 from [1])
  14840. //
  14841. // alpha <= e = e_c + e_w + q <= gamma
  14842. //
  14843. // or
  14844. //
  14845. // f_c * f_w * 2^alpha <= f_c 2^(e_c) * f_w 2^(e_w) * 2^q
  14846. // <= f_c * f_w * 2^gamma
  14847. //
  14848. // Since c and w are normalized, i.e. 2^(q-1) <= f < 2^q, this implies
  14849. //
  14850. // 2^(q-1) * 2^(q-1) * 2^alpha <= c * w * 2^q < 2^q * 2^q * 2^gamma
  14851. //
  14852. // or
  14853. //
  14854. // 2^(q - 2 + alpha) <= c * w < 2^(q + gamma)
  14855. //
  14856. // The choice of (alpha,gamma) determines the size of the table and the form of
  14857. // the digit generation procedure. Using (alpha,gamma)=(-60,-32) works out well
  14858. // in practice:
  14859. //
  14860. // The idea is to cut the number c * w = f * 2^e into two parts, which can be
  14861. // processed independently: An integral part p1, and a fractional part p2:
  14862. //
  14863. // f * 2^e = ( (f div 2^-e) * 2^-e + (f mod 2^-e) ) * 2^e
  14864. // = (f div 2^-e) + (f mod 2^-e) * 2^e
  14865. // = p1 + p2 * 2^e
  14866. //
  14867. // The conversion of p1 into decimal form requires a series of divisions and
  14868. // modulos by (a power of) 10. These operations are faster for 32-bit than for
  14869. // 64-bit integers, so p1 should ideally fit into a 32-bit integer. This can be
  14870. // achieved by choosing
  14871. //
  14872. // -e >= 32 or e <= -32 := gamma
  14873. //
  14874. // In order to convert the fractional part
  14875. //
  14876. // p2 * 2^e = p2 / 2^-e = d[-1] / 10^1 + d[-2] / 10^2 + ...
  14877. //
  14878. // into decimal form, the fraction is repeatedly multiplied by 10 and the digits
  14879. // d[-i] are extracted in order:
  14880. //
  14881. // (10 * p2) div 2^-e = d[-1]
  14882. // (10 * p2) mod 2^-e = d[-2] / 10^1 + ...
  14883. //
  14884. // The multiplication by 10 must not overflow. It is sufficient to choose
  14885. //
  14886. // 10 * p2 < 16 * p2 = 2^4 * p2 <= 2^64.
  14887. //
  14888. // Since p2 = f mod 2^-e < 2^-e,
  14889. //
  14890. // -e <= 60 or e >= -60 := alpha
  14891. constexpr int kAlpha = -60;
  14892. constexpr int kGamma = -32;
  14893. struct cached_power // c = f * 2^e ~= 10^k
  14894. {
  14895. std::uint64_t f;
  14896. int e;
  14897. int k;
  14898. };
  14899. /*!
  14900. For a normalized diyfp w = f * 2^e, this function returns a (normalized) cached
  14901. power-of-ten c = f_c * 2^e_c, such that the exponent of the product w * c
  14902. satisfies (Definition 3.2 from [1])
  14903. alpha <= e_c + e + q <= gamma.
  14904. */
  14905. inline cached_power get_cached_power_for_binary_exponent(int e)
  14906. {
  14907. // Now
  14908. //
  14909. // alpha <= e_c + e + q <= gamma (1)
  14910. // ==> f_c * 2^alpha <= c * 2^e * 2^q
  14911. //
  14912. // and since the c's are normalized, 2^(q-1) <= f_c,
  14913. //
  14914. // ==> 2^(q - 1 + alpha) <= c * 2^(e + q)
  14915. // ==> 2^(alpha - e - 1) <= c
  14916. //
  14917. // If c were an exact power of ten, i.e. c = 10^k, one may determine k as
  14918. //
  14919. // k = ceil( log_10( 2^(alpha - e - 1) ) )
  14920. // = ceil( (alpha - e - 1) * log_10(2) )
  14921. //
  14922. // From the paper:
  14923. // "In theory the result of the procedure could be wrong since c is rounded,
  14924. // and the computation itself is approximated [...]. In practice, however,
  14925. // this simple function is sufficient."
  14926. //
  14927. // For IEEE double precision floating-point numbers converted into
  14928. // normalized diyfp's w = f * 2^e, with q = 64,
  14929. //
  14930. // e >= -1022 (min IEEE exponent)
  14931. // -52 (p - 1)
  14932. // -52 (p - 1, possibly normalize denormal IEEE numbers)
  14933. // -11 (normalize the diyfp)
  14934. // = -1137
  14935. //
  14936. // and
  14937. //
  14938. // e <= +1023 (max IEEE exponent)
  14939. // -52 (p - 1)
  14940. // -11 (normalize the diyfp)
  14941. // = 960
  14942. //
  14943. // This binary exponent range [-1137,960] results in a decimal exponent
  14944. // range [-307,324]. One does not need to store a cached power for each
  14945. // k in this range. For each such k it suffices to find a cached power
  14946. // such that the exponent of the product lies in [alpha,gamma].
  14947. // This implies that the difference of the decimal exponents of adjacent
  14948. // table entries must be less than or equal to
  14949. //
  14950. // floor( (gamma - alpha) * log_10(2) ) = 8.
  14951. //
  14952. // (A smaller distance gamma-alpha would require a larger table.)
  14953. // NB:
  14954. // Actually this function returns c, such that -60 <= e_c + e + 64 <= -34.
  14955. constexpr int kCachedPowersMinDecExp = -300;
  14956. constexpr int kCachedPowersDecStep = 8;
  14957. static constexpr std::array<cached_power, 79> kCachedPowers =
  14958. {
  14959. {
  14960. { 0xAB70FE17C79AC6CA, -1060, -300 },
  14961. { 0xFF77B1FCBEBCDC4F, -1034, -292 },
  14962. { 0xBE5691EF416BD60C, -1007, -284 },
  14963. { 0x8DD01FAD907FFC3C, -980, -276 },
  14964. { 0xD3515C2831559A83, -954, -268 },
  14965. { 0x9D71AC8FADA6C9B5, -927, -260 },
  14966. { 0xEA9C227723EE8BCB, -901, -252 },
  14967. { 0xAECC49914078536D, -874, -244 },
  14968. { 0x823C12795DB6CE57, -847, -236 },
  14969. { 0xC21094364DFB5637, -821, -228 },
  14970. { 0x9096EA6F3848984F, -794, -220 },
  14971. { 0xD77485CB25823AC7, -768, -212 },
  14972. { 0xA086CFCD97BF97F4, -741, -204 },
  14973. { 0xEF340A98172AACE5, -715, -196 },
  14974. { 0xB23867FB2A35B28E, -688, -188 },
  14975. { 0x84C8D4DFD2C63F3B, -661, -180 },
  14976. { 0xC5DD44271AD3CDBA, -635, -172 },
  14977. { 0x936B9FCEBB25C996, -608, -164 },
  14978. { 0xDBAC6C247D62A584, -582, -156 },
  14979. { 0xA3AB66580D5FDAF6, -555, -148 },
  14980. { 0xF3E2F893DEC3F126, -529, -140 },
  14981. { 0xB5B5ADA8AAFF80B8, -502, -132 },
  14982. { 0x87625F056C7C4A8B, -475, -124 },
  14983. { 0xC9BCFF6034C13053, -449, -116 },
  14984. { 0x964E858C91BA2655, -422, -108 },
  14985. { 0xDFF9772470297EBD, -396, -100 },
  14986. { 0xA6DFBD9FB8E5B88F, -369, -92 },
  14987. { 0xF8A95FCF88747D94, -343, -84 },
  14988. { 0xB94470938FA89BCF, -316, -76 },
  14989. { 0x8A08F0F8BF0F156B, -289, -68 },
  14990. { 0xCDB02555653131B6, -263, -60 },
  14991. { 0x993FE2C6D07B7FAC, -236, -52 },
  14992. { 0xE45C10C42A2B3B06, -210, -44 },
  14993. { 0xAA242499697392D3, -183, -36 },
  14994. { 0xFD87B5F28300CA0E, -157, -28 },
  14995. { 0xBCE5086492111AEB, -130, -20 },
  14996. { 0x8CBCCC096F5088CC, -103, -12 },
  14997. { 0xD1B71758E219652C, -77, -4 },
  14998. { 0x9C40000000000000, -50, 4 },
  14999. { 0xE8D4A51000000000, -24, 12 },
  15000. { 0xAD78EBC5AC620000, 3, 20 },
  15001. { 0x813F3978F8940984, 30, 28 },
  15002. { 0xC097CE7BC90715B3, 56, 36 },
  15003. { 0x8F7E32CE7BEA5C70, 83, 44 },
  15004. { 0xD5D238A4ABE98068, 109, 52 },
  15005. { 0x9F4F2726179A2245, 136, 60 },
  15006. { 0xED63A231D4C4FB27, 162, 68 },
  15007. { 0xB0DE65388CC8ADA8, 189, 76 },
  15008. { 0x83C7088E1AAB65DB, 216, 84 },
  15009. { 0xC45D1DF942711D9A, 242, 92 },
  15010. { 0x924D692CA61BE758, 269, 100 },
  15011. { 0xDA01EE641A708DEA, 295, 108 },
  15012. { 0xA26DA3999AEF774A, 322, 116 },
  15013. { 0xF209787BB47D6B85, 348, 124 },
  15014. { 0xB454E4A179DD1877, 375, 132 },
  15015. { 0x865B86925B9BC5C2, 402, 140 },
  15016. { 0xC83553C5C8965D3D, 428, 148 },
  15017. { 0x952AB45CFA97A0B3, 455, 156 },
  15018. { 0xDE469FBD99A05FE3, 481, 164 },
  15019. { 0xA59BC234DB398C25, 508, 172 },
  15020. { 0xF6C69A72A3989F5C, 534, 180 },
  15021. { 0xB7DCBF5354E9BECE, 561, 188 },
  15022. { 0x88FCF317F22241E2, 588, 196 },
  15023. { 0xCC20CE9BD35C78A5, 614, 204 },
  15024. { 0x98165AF37B2153DF, 641, 212 },
  15025. { 0xE2A0B5DC971F303A, 667, 220 },
  15026. { 0xA8D9D1535CE3B396, 694, 228 },
  15027. { 0xFB9B7CD9A4A7443C, 720, 236 },
  15028. { 0xBB764C4CA7A44410, 747, 244 },
  15029. { 0x8BAB8EEFB6409C1A, 774, 252 },
  15030. { 0xD01FEF10A657842C, 800, 260 },
  15031. { 0x9B10A4E5E9913129, 827, 268 },
  15032. { 0xE7109BFBA19C0C9D, 853, 276 },
  15033. { 0xAC2820D9623BF429, 880, 284 },
  15034. { 0x80444B5E7AA7CF85, 907, 292 },
  15035. { 0xBF21E44003ACDD2D, 933, 300 },
  15036. { 0x8E679C2F5E44FF8F, 960, 308 },
  15037. { 0xD433179D9C8CB841, 986, 316 },
  15038. { 0x9E19DB92B4E31BA9, 1013, 324 },
  15039. }
  15040. };
  15041. // This computation gives exactly the same results for k as
  15042. // k = ceil((kAlpha - e - 1) * 0.30102999566398114)
  15043. // for |e| <= 1500, but doesn't require floating-point operations.
  15044. // NB: log_10(2) ~= 78913 / 2^18
  15045. JSON_ASSERT(e >= -1500);
  15046. JSON_ASSERT(e <= 1500);
  15047. const int f = kAlpha - e - 1;
  15048. const int k = (f * 78913) / (1 << 18) + static_cast<int>(f > 0);
  15049. const int index = (-kCachedPowersMinDecExp + k + (kCachedPowersDecStep - 1)) / kCachedPowersDecStep;
  15050. JSON_ASSERT(index >= 0);
  15051. JSON_ASSERT(static_cast<std::size_t>(index) < kCachedPowers.size());
  15052. const cached_power cached = kCachedPowers[static_cast<std::size_t>(index)];
  15053. JSON_ASSERT(kAlpha <= cached.e + e + 64);
  15054. JSON_ASSERT(kGamma >= cached.e + e + 64);
  15055. return cached;
  15056. }
  15057. /*!
  15058. For n != 0, returns k, such that pow10 := 10^(k-1) <= n < 10^k.
  15059. For n == 0, returns 1 and sets pow10 := 1.
  15060. */
  15061. inline int find_largest_pow10(const std::uint32_t n, std::uint32_t& pow10)
  15062. {
  15063. // LCOV_EXCL_START
  15064. if (n >= 1000000000)
  15065. {
  15066. pow10 = 1000000000;
  15067. return 10;
  15068. }
  15069. // LCOV_EXCL_STOP
  15070. if (n >= 100000000)
  15071. {
  15072. pow10 = 100000000;
  15073. return 9;
  15074. }
  15075. if (n >= 10000000)
  15076. {
  15077. pow10 = 10000000;
  15078. return 8;
  15079. }
  15080. if (n >= 1000000)
  15081. {
  15082. pow10 = 1000000;
  15083. return 7;
  15084. }
  15085. if (n >= 100000)
  15086. {
  15087. pow10 = 100000;
  15088. return 6;
  15089. }
  15090. if (n >= 10000)
  15091. {
  15092. pow10 = 10000;
  15093. return 5;
  15094. }
  15095. if (n >= 1000)
  15096. {
  15097. pow10 = 1000;
  15098. return 4;
  15099. }
  15100. if (n >= 100)
  15101. {
  15102. pow10 = 100;
  15103. return 3;
  15104. }
  15105. if (n >= 10)
  15106. {
  15107. pow10 = 10;
  15108. return 2;
  15109. }
  15110. pow10 = 1;
  15111. return 1;
  15112. }
  15113. inline void grisu2_round(char* buf, int len, std::uint64_t dist, std::uint64_t delta,
  15114. std::uint64_t rest, std::uint64_t ten_k)
  15115. {
  15116. JSON_ASSERT(len >= 1);
  15117. JSON_ASSERT(dist <= delta);
  15118. JSON_ASSERT(rest <= delta);
  15119. JSON_ASSERT(ten_k > 0);
  15120. // <--------------------------- delta ---->
  15121. // <---- dist --------->
  15122. // --------------[------------------+-------------------]--------------
  15123. // M- w M+
  15124. //
  15125. // ten_k
  15126. // <------>
  15127. // <---- rest ---->
  15128. // --------------[------------------+----+--------------]--------------
  15129. // w V
  15130. // = buf * 10^k
  15131. //
  15132. // ten_k represents a unit-in-the-last-place in the decimal representation
  15133. // stored in buf.
  15134. // Decrement buf by ten_k while this takes buf closer to w.
  15135. // The tests are written in this order to avoid overflow in unsigned
  15136. // integer arithmetic.
  15137. while (rest < dist
  15138. && delta - rest >= ten_k
  15139. && (rest + ten_k < dist || dist - rest > rest + ten_k - dist))
  15140. {
  15141. JSON_ASSERT(buf[len - 1] != '0');
  15142. buf[len - 1]--;
  15143. rest += ten_k;
  15144. }
  15145. }
  15146. /*!
  15147. Generates V = buffer * 10^decimal_exponent, such that M- <= V <= M+.
  15148. M- and M+ must be normalized and share the same exponent -60 <= e <= -32.
  15149. */
  15150. inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent,
  15151. diyfp M_minus, diyfp w, diyfp M_plus)
  15152. {
  15153. static_assert(kAlpha >= -60, "internal error");
  15154. static_assert(kGamma <= -32, "internal error");
  15155. // Generates the digits (and the exponent) of a decimal floating-point
  15156. // number V = buffer * 10^decimal_exponent in the range [M-, M+]. The diyfp's
  15157. // w, M- and M+ share the same exponent e, which satisfies alpha <= e <= gamma.
  15158. //
  15159. // <--------------------------- delta ---->
  15160. // <---- dist --------->
  15161. // --------------[------------------+-------------------]--------------
  15162. // M- w M+
  15163. //
  15164. // Grisu2 generates the digits of M+ from left to right and stops as soon as
  15165. // V is in [M-,M+].
  15166. JSON_ASSERT(M_plus.e >= kAlpha);
  15167. JSON_ASSERT(M_plus.e <= kGamma);
  15168. std::uint64_t delta = diyfp::sub(M_plus, M_minus).f; // (significand of (M+ - M-), implicit exponent is e)
  15169. std::uint64_t dist = diyfp::sub(M_plus, w ).f; // (significand of (M+ - w ), implicit exponent is e)
  15170. // Split M+ = f * 2^e into two parts p1 and p2 (note: e < 0):
  15171. //
  15172. // M+ = f * 2^e
  15173. // = ((f div 2^-e) * 2^-e + (f mod 2^-e)) * 2^e
  15174. // = ((p1 ) * 2^-e + (p2 )) * 2^e
  15175. // = p1 + p2 * 2^e
  15176. const diyfp one(std::uint64_t{1} << -M_plus.e, M_plus.e);
  15177. 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.)
  15178. std::uint64_t p2 = M_plus.f & (one.f - 1); // p2 = f mod 2^-e
  15179. // 1)
  15180. //
  15181. // Generate the digits of the integral part p1 = d[n-1]...d[1]d[0]
  15182. JSON_ASSERT(p1 > 0);
  15183. std::uint32_t pow10{};
  15184. const int k = find_largest_pow10(p1, pow10);
  15185. // 10^(k-1) <= p1 < 10^k, pow10 = 10^(k-1)
  15186. //
  15187. // p1 = (p1 div 10^(k-1)) * 10^(k-1) + (p1 mod 10^(k-1))
  15188. // = (d[k-1] ) * 10^(k-1) + (p1 mod 10^(k-1))
  15189. //
  15190. // M+ = p1 + p2 * 2^e
  15191. // = d[k-1] * 10^(k-1) + (p1 mod 10^(k-1)) + p2 * 2^e
  15192. // = d[k-1] * 10^(k-1) + ((p1 mod 10^(k-1)) * 2^-e + p2) * 2^e
  15193. // = d[k-1] * 10^(k-1) + ( rest) * 2^e
  15194. //
  15195. // Now generate the digits d[n] of p1 from left to right (n = k-1,...,0)
  15196. //
  15197. // p1 = d[k-1]...d[n] * 10^n + d[n-1]...d[0]
  15198. //
  15199. // but stop as soon as
  15200. //
  15201. // rest * 2^e = (d[n-1]...d[0] * 2^-e + p2) * 2^e <= delta * 2^e
  15202. int n = k;
  15203. while (n > 0)
  15204. {
  15205. // Invariants:
  15206. // M+ = buffer * 10^n + (p1 + p2 * 2^e) (buffer = 0 for n = k)
  15207. // pow10 = 10^(n-1) <= p1 < 10^n
  15208. //
  15209. const std::uint32_t d = p1 / pow10; // d = p1 div 10^(n-1)
  15210. const std::uint32_t r = p1 % pow10; // r = p1 mod 10^(n-1)
  15211. //
  15212. // M+ = buffer * 10^n + (d * 10^(n-1) + r) + p2 * 2^e
  15213. // = (buffer * 10 + d) * 10^(n-1) + (r + p2 * 2^e)
  15214. //
  15215. JSON_ASSERT(d <= 9);
  15216. buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
  15217. //
  15218. // M+ = buffer * 10^(n-1) + (r + p2 * 2^e)
  15219. //
  15220. p1 = r;
  15221. n--;
  15222. //
  15223. // M+ = buffer * 10^n + (p1 + p2 * 2^e)
  15224. // pow10 = 10^n
  15225. //
  15226. // Now check if enough digits have been generated.
  15227. // Compute
  15228. //
  15229. // p1 + p2 * 2^e = (p1 * 2^-e + p2) * 2^e = rest * 2^e
  15230. //
  15231. // Note:
  15232. // Since rest and delta share the same exponent e, it suffices to
  15233. // compare the significands.
  15234. const std::uint64_t rest = (std::uint64_t{p1} << -one.e) + p2;
  15235. if (rest <= delta)
  15236. {
  15237. // V = buffer * 10^n, with M- <= V <= M+.
  15238. decimal_exponent += n;
  15239. // We may now just stop. But instead look if the buffer could be
  15240. // decremented to bring V closer to w.
  15241. //
  15242. // pow10 = 10^n is now 1 ulp in the decimal representation V.
  15243. // The rounding procedure works with diyfp's with an implicit
  15244. // exponent of e.
  15245. //
  15246. // 10^n = (10^n * 2^-e) * 2^e = ulp * 2^e
  15247. //
  15248. const std::uint64_t ten_n = std::uint64_t{pow10} << -one.e;
  15249. grisu2_round(buffer, length, dist, delta, rest, ten_n);
  15250. return;
  15251. }
  15252. pow10 /= 10;
  15253. //
  15254. // pow10 = 10^(n-1) <= p1 < 10^n
  15255. // Invariants restored.
  15256. }
  15257. // 2)
  15258. //
  15259. // The digits of the integral part have been generated:
  15260. //
  15261. // M+ = d[k-1]...d[1]d[0] + p2 * 2^e
  15262. // = buffer + p2 * 2^e
  15263. //
  15264. // Now generate the digits of the fractional part p2 * 2^e.
  15265. //
  15266. // Note:
  15267. // No decimal point is generated: the exponent is adjusted instead.
  15268. //
  15269. // p2 actually represents the fraction
  15270. //
  15271. // p2 * 2^e
  15272. // = p2 / 2^-e
  15273. // = d[-1] / 10^1 + d[-2] / 10^2 + ...
  15274. //
  15275. // Now generate the digits d[-m] of p1 from left to right (m = 1,2,...)
  15276. //
  15277. // p2 * 2^e = d[-1]d[-2]...d[-m] * 10^-m
  15278. // + 10^-m * (d[-m-1] / 10^1 + d[-m-2] / 10^2 + ...)
  15279. //
  15280. // using
  15281. //
  15282. // 10^m * p2 = ((10^m * p2) div 2^-e) * 2^-e + ((10^m * p2) mod 2^-e)
  15283. // = ( d) * 2^-e + ( r)
  15284. //
  15285. // or
  15286. // 10^m * p2 * 2^e = d + r * 2^e
  15287. //
  15288. // i.e.
  15289. //
  15290. // M+ = buffer + p2 * 2^e
  15291. // = buffer + 10^-m * (d + r * 2^e)
  15292. // = (buffer * 10^m + d) * 10^-m + 10^-m * r * 2^e
  15293. //
  15294. // and stop as soon as 10^-m * r * 2^e <= delta * 2^e
  15295. JSON_ASSERT(p2 > delta);
  15296. int m = 0;
  15297. for (;;)
  15298. {
  15299. // Invariant:
  15300. // M+ = buffer * 10^-m + 10^-m * (d[-m-1] / 10 + d[-m-2] / 10^2 + ...) * 2^e
  15301. // = buffer * 10^-m + 10^-m * (p2 ) * 2^e
  15302. // = buffer * 10^-m + 10^-m * (1/10 * (10 * p2) ) * 2^e
  15303. // = buffer * 10^-m + 10^-m * (1/10 * ((10*p2 div 2^-e) * 2^-e + (10*p2 mod 2^-e)) * 2^e
  15304. //
  15305. JSON_ASSERT(p2 <= (std::numeric_limits<std::uint64_t>::max)() / 10);
  15306. p2 *= 10;
  15307. const std::uint64_t d = p2 >> -one.e; // d = (10 * p2) div 2^-e
  15308. const std::uint64_t r = p2 & (one.f - 1); // r = (10 * p2) mod 2^-e
  15309. //
  15310. // M+ = buffer * 10^-m + 10^-m * (1/10 * (d * 2^-e + r) * 2^e
  15311. // = buffer * 10^-m + 10^-m * (1/10 * (d + r * 2^e))
  15312. // = (buffer * 10 + d) * 10^(-m-1) + 10^(-m-1) * r * 2^e
  15313. //
  15314. JSON_ASSERT(d <= 9);
  15315. buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
  15316. //
  15317. // M+ = buffer * 10^(-m-1) + 10^(-m-1) * r * 2^e
  15318. //
  15319. p2 = r;
  15320. m++;
  15321. //
  15322. // M+ = buffer * 10^-m + 10^-m * p2 * 2^e
  15323. // Invariant restored.
  15324. // Check if enough digits have been generated.
  15325. //
  15326. // 10^-m * p2 * 2^e <= delta * 2^e
  15327. // p2 * 2^e <= 10^m * delta * 2^e
  15328. // p2 <= 10^m * delta
  15329. delta *= 10;
  15330. dist *= 10;
  15331. if (p2 <= delta)
  15332. {
  15333. break;
  15334. }
  15335. }
  15336. // V = buffer * 10^-m, with M- <= V <= M+.
  15337. decimal_exponent -= m;
  15338. // 1 ulp in the decimal representation is now 10^-m.
  15339. // Since delta and dist are now scaled by 10^m, we need to do the
  15340. // same with ulp in order to keep the units in sync.
  15341. //
  15342. // 10^m * 10^-m = 1 = 2^-e * 2^e = ten_m * 2^e
  15343. //
  15344. const std::uint64_t ten_m = one.f;
  15345. grisu2_round(buffer, length, dist, delta, p2, ten_m);
  15346. // By construction this algorithm generates the shortest possible decimal
  15347. // number (Loitsch, Theorem 6.2) which rounds back to w.
  15348. // For an input number of precision p, at least
  15349. //
  15350. // N = 1 + ceil(p * log_10(2))
  15351. //
  15352. // decimal digits are sufficient to identify all binary floating-point
  15353. // numbers (Matula, "In-and-Out conversions").
  15354. // This implies that the algorithm does not produce more than N decimal
  15355. // digits.
  15356. //
  15357. // N = 17 for p = 53 (IEEE double precision)
  15358. // N = 9 for p = 24 (IEEE single precision)
  15359. }
  15360. /*!
  15361. v = buf * 10^decimal_exponent
  15362. len is the length of the buffer (number of decimal digits)
  15363. The buffer must be large enough, i.e. >= max_digits10.
  15364. */
  15365. JSON_HEDLEY_NON_NULL(1)
  15366. inline void grisu2(char* buf, int& len, int& decimal_exponent,
  15367. diyfp m_minus, diyfp v, diyfp m_plus)
  15368. {
  15369. JSON_ASSERT(m_plus.e == m_minus.e);
  15370. JSON_ASSERT(m_plus.e == v.e);
  15371. // --------(-----------------------+-----------------------)-------- (A)
  15372. // m- v m+
  15373. //
  15374. // --------------------(-----------+-----------------------)-------- (B)
  15375. // m- v m+
  15376. //
  15377. // First scale v (and m- and m+) such that the exponent is in the range
  15378. // [alpha, gamma].
  15379. const cached_power cached = get_cached_power_for_binary_exponent(m_plus.e);
  15380. const diyfp c_minus_k(cached.f, cached.e); // = c ~= 10^-k
  15381. // The exponent of the products is = v.e + c_minus_k.e + q and is in the range [alpha,gamma]
  15382. const diyfp w = diyfp::mul(v, c_minus_k);
  15383. const diyfp w_minus = diyfp::mul(m_minus, c_minus_k);
  15384. const diyfp w_plus = diyfp::mul(m_plus, c_minus_k);
  15385. // ----(---+---)---------------(---+---)---------------(---+---)----
  15386. // w- w w+
  15387. // = c*m- = c*v = c*m+
  15388. //
  15389. // diyfp::mul rounds its result and c_minus_k is approximated too. w, w- and
  15390. // w+ are now off by a small amount.
  15391. // In fact:
  15392. //
  15393. // w - v * 10^k < 1 ulp
  15394. //
  15395. // To account for this inaccuracy, add resp. subtract 1 ulp.
  15396. //
  15397. // --------+---[---------------(---+---)---------------]---+--------
  15398. // w- M- w M+ w+
  15399. //
  15400. // Now any number in [M-, M+] (bounds included) will round to w when input,
  15401. // regardless of how the input rounding algorithm breaks ties.
  15402. //
  15403. // And digit_gen generates the shortest possible such number in [M-, M+].
  15404. // Note that this does not mean that Grisu2 always generates the shortest
  15405. // possible number in the interval (m-, m+).
  15406. const diyfp M_minus(w_minus.f + 1, w_minus.e);
  15407. const diyfp M_plus (w_plus.f - 1, w_plus.e );
  15408. decimal_exponent = -cached.k; // = -(-k) = k
  15409. grisu2_digit_gen(buf, len, decimal_exponent, M_minus, w, M_plus);
  15410. }
  15411. /*!
  15412. v = buf * 10^decimal_exponent
  15413. len is the length of the buffer (number of decimal digits)
  15414. The buffer must be large enough, i.e. >= max_digits10.
  15415. */
  15416. template<typename FloatType>
  15417. JSON_HEDLEY_NON_NULL(1)
  15418. void grisu2(char* buf, int& len, int& decimal_exponent, FloatType value)
  15419. {
  15420. static_assert(diyfp::kPrecision >= std::numeric_limits<FloatType>::digits + 3,
  15421. "internal error: not enough precision");
  15422. JSON_ASSERT(std::isfinite(value));
  15423. JSON_ASSERT(value > 0);
  15424. // If the neighbors (and boundaries) of 'value' are always computed for double-precision
  15425. // numbers, all float's can be recovered using strtod (and strtof). However, the resulting
  15426. // decimal representations are not exactly "short".
  15427. //
  15428. // The documentation for 'std::to_chars' (https://en.cppreference.com/w/cpp/utility/to_chars)
  15429. // says "value is converted to a string as if by std::sprintf in the default ("C") locale"
  15430. // and since sprintf promotes floats to doubles, I think this is exactly what 'std::to_chars'
  15431. // does.
  15432. // On the other hand, the documentation for 'std::to_chars' requires that "parsing the
  15433. // representation using the corresponding std::from_chars function recovers value exactly". That
  15434. // indicates that single precision floating-point numbers should be recovered using
  15435. // 'std::strtof'.
  15436. //
  15437. // NB: If the neighbors are computed for single-precision numbers, there is a single float
  15438. // (7.0385307e-26f) which can't be recovered using strtod. The resulting double precision
  15439. // value is off by 1 ulp.
  15440. #if 0
  15441. const boundaries w = compute_boundaries(static_cast<double>(value));
  15442. #else
  15443. const boundaries w = compute_boundaries(value);
  15444. #endif
  15445. grisu2(buf, len, decimal_exponent, w.minus, w.w, w.plus);
  15446. }
  15447. /*!
  15448. @brief appends a decimal representation of e to buf
  15449. @return a pointer to the element following the exponent.
  15450. @pre -1000 < e < 1000
  15451. */
  15452. JSON_HEDLEY_NON_NULL(1)
  15453. JSON_HEDLEY_RETURNS_NON_NULL
  15454. inline char* append_exponent(char* buf, int e)
  15455. {
  15456. JSON_ASSERT(e > -1000);
  15457. JSON_ASSERT(e < 1000);
  15458. if (e < 0)
  15459. {
  15460. e = -e;
  15461. *buf++ = '-';
  15462. }
  15463. else
  15464. {
  15465. *buf++ = '+';
  15466. }
  15467. auto k = static_cast<std::uint32_t>(e);
  15468. if (k < 10)
  15469. {
  15470. // Always print at least two digits in the exponent.
  15471. // This is for compatibility with printf("%g").
  15472. *buf++ = '0';
  15473. *buf++ = static_cast<char>('0' + k);
  15474. }
  15475. else if (k < 100)
  15476. {
  15477. *buf++ = static_cast<char>('0' + k / 10);
  15478. k %= 10;
  15479. *buf++ = static_cast<char>('0' + k);
  15480. }
  15481. else
  15482. {
  15483. *buf++ = static_cast<char>('0' + k / 100);
  15484. k %= 100;
  15485. *buf++ = static_cast<char>('0' + k / 10);
  15486. k %= 10;
  15487. *buf++ = static_cast<char>('0' + k);
  15488. }
  15489. return buf;
  15490. }
  15491. /*!
  15492. @brief prettify v = buf * 10^decimal_exponent
  15493. If v is in the range [10^min_exp, 10^max_exp) it will be printed in fixed-point
  15494. notation. Otherwise it will be printed in exponential notation.
  15495. @pre min_exp < 0
  15496. @pre max_exp > 0
  15497. */
  15498. JSON_HEDLEY_NON_NULL(1)
  15499. JSON_HEDLEY_RETURNS_NON_NULL
  15500. inline char* format_buffer(char* buf, int len, int decimal_exponent,
  15501. int min_exp, int max_exp)
  15502. {
  15503. JSON_ASSERT(min_exp < 0);
  15504. JSON_ASSERT(max_exp > 0);
  15505. const int k = len;
  15506. const int n = len + decimal_exponent;
  15507. // v = buf * 10^(n-k)
  15508. // k is the length of the buffer (number of decimal digits)
  15509. // n is the position of the decimal point relative to the start of the buffer.
  15510. if (k <= n && n <= max_exp)
  15511. {
  15512. // digits[000]
  15513. // len <= max_exp + 2
  15514. std::memset(buf + k, '0', static_cast<size_t>(n) - static_cast<size_t>(k));
  15515. // Make it look like a floating-point number (#362, #378)
  15516. buf[n + 0] = '.';
  15517. buf[n + 1] = '0';
  15518. return buf + (static_cast<size_t>(n) + 2);
  15519. }
  15520. if (0 < n && n <= max_exp)
  15521. {
  15522. // dig.its
  15523. // len <= max_digits10 + 1
  15524. JSON_ASSERT(k > n);
  15525. std::memmove(buf + (static_cast<size_t>(n) + 1), buf + n, static_cast<size_t>(k) - static_cast<size_t>(n));
  15526. buf[n] = '.';
  15527. return buf + (static_cast<size_t>(k) + 1U);
  15528. }
  15529. if (min_exp < n && n <= 0)
  15530. {
  15531. // 0.[000]digits
  15532. // len <= 2 + (-min_exp - 1) + max_digits10
  15533. std::memmove(buf + (2 + static_cast<size_t>(-n)), buf, static_cast<size_t>(k));
  15534. buf[0] = '0';
  15535. buf[1] = '.';
  15536. std::memset(buf + 2, '0', static_cast<size_t>(-n));
  15537. return buf + (2U + static_cast<size_t>(-n) + static_cast<size_t>(k));
  15538. }
  15539. if (k == 1)
  15540. {
  15541. // dE+123
  15542. // len <= 1 + 5
  15543. buf += 1;
  15544. }
  15545. else
  15546. {
  15547. // d.igitsE+123
  15548. // len <= max_digits10 + 1 + 5
  15549. std::memmove(buf + 2, buf + 1, static_cast<size_t>(k) - 1);
  15550. buf[1] = '.';
  15551. buf += 1 + static_cast<size_t>(k);
  15552. }
  15553. *buf++ = 'e';
  15554. return append_exponent(buf, n - 1);
  15555. }
  15556. } // namespace dtoa_impl
  15557. /*!
  15558. @brief generates a decimal representation of the floating-point number value in [first, last).
  15559. The format of the resulting decimal representation is similar to printf's %g
  15560. format. Returns an iterator pointing past-the-end of the decimal representation.
  15561. @note The input number must be finite, i.e. NaN's and Inf's are not supported.
  15562. @note The buffer must be large enough.
  15563. @note The result is NOT null-terminated.
  15564. */
  15565. template<typename FloatType>
  15566. JSON_HEDLEY_NON_NULL(1, 2)
  15567. JSON_HEDLEY_RETURNS_NON_NULL
  15568. char* to_chars(char* first, const char* last, FloatType value)
  15569. {
  15570. static_cast<void>(last); // maybe unused - fix warning
  15571. JSON_ASSERT(std::isfinite(value));
  15572. // Use signbit(value) instead of (value < 0) since signbit works for -0.
  15573. if (std::signbit(value))
  15574. {
  15575. value = -value;
  15576. *first++ = '-';
  15577. }
  15578. #ifdef __GNUC__
  15579. #pragma GCC diagnostic push
  15580. #pragma GCC diagnostic ignored "-Wfloat-equal"
  15581. #endif
  15582. if (value == 0) // +-0
  15583. {
  15584. *first++ = '0';
  15585. // Make it look like a floating-point number (#362, #378)
  15586. *first++ = '.';
  15587. *first++ = '0';
  15588. return first;
  15589. }
  15590. #ifdef __GNUC__
  15591. #pragma GCC diagnostic pop
  15592. #endif
  15593. JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10);
  15594. // Compute v = buffer * 10^decimal_exponent.
  15595. // The decimal digits are stored in the buffer, which needs to be interpreted
  15596. // as an unsigned decimal integer.
  15597. // len is the length of the buffer, i.e. the number of decimal digits.
  15598. int len = 0;
  15599. int decimal_exponent = 0;
  15600. dtoa_impl::grisu2(first, len, decimal_exponent, value);
  15601. JSON_ASSERT(len <= std::numeric_limits<FloatType>::max_digits10);
  15602. // Format the buffer like printf("%.*g", prec, value)
  15603. constexpr int kMinExp = -4;
  15604. // Use digits10 here to increase compatibility with version 2.
  15605. constexpr int kMaxExp = std::numeric_limits<FloatType>::digits10;
  15606. JSON_ASSERT(last - first >= kMaxExp + 2);
  15607. JSON_ASSERT(last - first >= 2 + (-kMinExp - 1) + std::numeric_limits<FloatType>::max_digits10);
  15608. JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10 + 6);
  15609. return dtoa_impl::format_buffer(first, len, decimal_exponent, kMinExp, kMaxExp);
  15610. }
  15611. } // namespace detail
  15612. NLOHMANN_JSON_NAMESPACE_END
  15613. // #include <nlohmann/detail/exceptions.hpp>
  15614. // #include <nlohmann/detail/macro_scope.hpp>
  15615. // #include <nlohmann/detail/meta/cpp_future.hpp>
  15616. // #include <nlohmann/detail/output/binary_writer.hpp>
  15617. // #include <nlohmann/detail/output/output_adapters.hpp>
  15618. // #include <nlohmann/detail/string_concat.hpp>
  15619. // #include <nlohmann/detail/value_t.hpp>
  15620. NLOHMANN_JSON_NAMESPACE_BEGIN
  15621. namespace detail
  15622. {
  15623. ///////////////////
  15624. // serialization //
  15625. ///////////////////
  15626. /// how to treat decoding errors
  15627. enum class error_handler_t
  15628. {
  15629. strict, ///< throw a type_error exception in case of invalid UTF-8
  15630. replace, ///< replace invalid UTF-8 sequences with U+FFFD
  15631. ignore ///< ignore invalid UTF-8 sequences
  15632. };
  15633. template<typename BasicJsonType>
  15634. class serializer
  15635. {
  15636. using string_t = typename BasicJsonType::string_t;
  15637. using number_float_t = typename BasicJsonType::number_float_t;
  15638. using number_integer_t = typename BasicJsonType::number_integer_t;
  15639. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  15640. using binary_char_t = typename BasicJsonType::binary_t::value_type;
  15641. static constexpr std::uint8_t UTF8_ACCEPT = 0;
  15642. static constexpr std::uint8_t UTF8_REJECT = 1;
  15643. public:
  15644. /*!
  15645. @param[in] s output stream to serialize to
  15646. @param[in] ichar indentation character to use
  15647. @param[in] error_handler_ how to react on decoding errors
  15648. */
  15649. serializer(output_adapter_t<char> s, const char ichar,
  15650. error_handler_t error_handler_ = error_handler_t::strict)
  15651. : o(std::move(s))
  15652. , loc(std::localeconv())
  15653. , thousands_sep(loc->thousands_sep == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->thousands_sep)))
  15654. , decimal_point(loc->decimal_point == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->decimal_point)))
  15655. , indent_char(ichar)
  15656. , indent_string(512, indent_char)
  15657. , error_handler(error_handler_)
  15658. {}
  15659. // delete because of pointer members
  15660. serializer(const serializer&) = delete;
  15661. serializer& operator=(const serializer&) = delete;
  15662. serializer(serializer&&) = delete;
  15663. serializer& operator=(serializer&&) = delete;
  15664. ~serializer() = default;
  15665. /*!
  15666. @brief internal implementation of the serialization function
  15667. This function is called by the public member function dump and organizes
  15668. the serialization internally. The indentation level is propagated as
  15669. additional parameter. In case of arrays and objects, the function is
  15670. called recursively.
  15671. - strings and object keys are escaped using `escape_string()`
  15672. - integer numbers are converted implicitly via `operator<<`
  15673. - floating-point numbers are converted to a string using `"%g"` format
  15674. - binary values are serialized as objects containing the subtype and the
  15675. byte array
  15676. @param[in] val value to serialize
  15677. @param[in] pretty_print whether the output shall be pretty-printed
  15678. @param[in] ensure_ascii If @a ensure_ascii is true, all non-ASCII characters
  15679. in the output are escaped with `\uXXXX` sequences, and the result consists
  15680. of ASCII characters only.
  15681. @param[in] indent_step the indent level
  15682. @param[in] current_indent the current indent level (only used internally)
  15683. */
  15684. void dump(const BasicJsonType& val,
  15685. const bool pretty_print,
  15686. const bool ensure_ascii,
  15687. const unsigned int indent_step,
  15688. const unsigned int current_indent = 0)
  15689. {
  15690. switch (val.m_type)
  15691. {
  15692. case value_t::object:
  15693. {
  15694. if (val.m_value.object->empty())
  15695. {
  15696. o->write_characters("{}", 2);
  15697. return;
  15698. }
  15699. if (pretty_print)
  15700. {
  15701. o->write_characters("{\n", 2);
  15702. // variable to hold indentation for recursive calls
  15703. const auto new_indent = current_indent + indent_step;
  15704. if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
  15705. {
  15706. indent_string.resize(indent_string.size() * 2, ' ');
  15707. }
  15708. // first n-1 elements
  15709. auto i = val.m_value.object->cbegin();
  15710. for (std::size_t cnt = 0; cnt < val.m_value.object->size() - 1; ++cnt, ++i)
  15711. {
  15712. o->write_characters(indent_string.c_str(), new_indent);
  15713. o->write_character('\"');
  15714. dump_escaped(i->first, ensure_ascii);
  15715. o->write_characters("\": ", 3);
  15716. dump(i->second, true, ensure_ascii, indent_step, new_indent);
  15717. o->write_characters(",\n", 2);
  15718. }
  15719. // last element
  15720. JSON_ASSERT(i != val.m_value.object->cend());
  15721. JSON_ASSERT(std::next(i) == val.m_value.object->cend());
  15722. o->write_characters(indent_string.c_str(), new_indent);
  15723. o->write_character('\"');
  15724. dump_escaped(i->first, ensure_ascii);
  15725. o->write_characters("\": ", 3);
  15726. dump(i->second, true, ensure_ascii, indent_step, new_indent);
  15727. o->write_character('\n');
  15728. o->write_characters(indent_string.c_str(), current_indent);
  15729. o->write_character('}');
  15730. }
  15731. else
  15732. {
  15733. o->write_character('{');
  15734. // first n-1 elements
  15735. auto i = val.m_value.object->cbegin();
  15736. for (std::size_t cnt = 0; cnt < val.m_value.object->size() - 1; ++cnt, ++i)
  15737. {
  15738. o->write_character('\"');
  15739. dump_escaped(i->first, ensure_ascii);
  15740. o->write_characters("\":", 2);
  15741. dump(i->second, false, ensure_ascii, indent_step, current_indent);
  15742. o->write_character(',');
  15743. }
  15744. // last element
  15745. JSON_ASSERT(i != val.m_value.object->cend());
  15746. JSON_ASSERT(std::next(i) == val.m_value.object->cend());
  15747. o->write_character('\"');
  15748. dump_escaped(i->first, ensure_ascii);
  15749. o->write_characters("\":", 2);
  15750. dump(i->second, false, ensure_ascii, indent_step, current_indent);
  15751. o->write_character('}');
  15752. }
  15753. return;
  15754. }
  15755. case value_t::array:
  15756. {
  15757. if (val.m_value.array->empty())
  15758. {
  15759. o->write_characters("[]", 2);
  15760. return;
  15761. }
  15762. if (pretty_print)
  15763. {
  15764. o->write_characters("[\n", 2);
  15765. // variable to hold indentation for recursive calls
  15766. const auto new_indent = current_indent + indent_step;
  15767. if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
  15768. {
  15769. indent_string.resize(indent_string.size() * 2, ' ');
  15770. }
  15771. // first n-1 elements
  15772. for (auto i = val.m_value.array->cbegin();
  15773. i != val.m_value.array->cend() - 1; ++i)
  15774. {
  15775. o->write_characters(indent_string.c_str(), new_indent);
  15776. dump(*i, true, ensure_ascii, indent_step, new_indent);
  15777. o->write_characters(",\n", 2);
  15778. }
  15779. // last element
  15780. JSON_ASSERT(!val.m_value.array->empty());
  15781. o->write_characters(indent_string.c_str(), new_indent);
  15782. dump(val.m_value.array->back(), true, ensure_ascii, indent_step, new_indent);
  15783. o->write_character('\n');
  15784. o->write_characters(indent_string.c_str(), current_indent);
  15785. o->write_character(']');
  15786. }
  15787. else
  15788. {
  15789. o->write_character('[');
  15790. // first n-1 elements
  15791. for (auto i = val.m_value.array->cbegin();
  15792. i != val.m_value.array->cend() - 1; ++i)
  15793. {
  15794. dump(*i, false, ensure_ascii, indent_step, current_indent);
  15795. o->write_character(',');
  15796. }
  15797. // last element
  15798. JSON_ASSERT(!val.m_value.array->empty());
  15799. dump(val.m_value.array->back(), false, ensure_ascii, indent_step, current_indent);
  15800. o->write_character(']');
  15801. }
  15802. return;
  15803. }
  15804. case value_t::string:
  15805. {
  15806. o->write_character('\"');
  15807. dump_escaped(*val.m_value.string, ensure_ascii);
  15808. o->write_character('\"');
  15809. return;
  15810. }
  15811. case value_t::binary:
  15812. {
  15813. if (pretty_print)
  15814. {
  15815. o->write_characters("{\n", 2);
  15816. // variable to hold indentation for recursive calls
  15817. const auto new_indent = current_indent + indent_step;
  15818. if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
  15819. {
  15820. indent_string.resize(indent_string.size() * 2, ' ');
  15821. }
  15822. o->write_characters(indent_string.c_str(), new_indent);
  15823. o->write_characters("\"bytes\": [", 10);
  15824. if (!val.m_value.binary->empty())
  15825. {
  15826. for (auto i = val.m_value.binary->cbegin();
  15827. i != val.m_value.binary->cend() - 1; ++i)
  15828. {
  15829. dump_integer(*i);
  15830. o->write_characters(", ", 2);
  15831. }
  15832. dump_integer(val.m_value.binary->back());
  15833. }
  15834. o->write_characters("],\n", 3);
  15835. o->write_characters(indent_string.c_str(), new_indent);
  15836. o->write_characters("\"subtype\": ", 11);
  15837. if (val.m_value.binary->has_subtype())
  15838. {
  15839. dump_integer(val.m_value.binary->subtype());
  15840. }
  15841. else
  15842. {
  15843. o->write_characters("null", 4);
  15844. }
  15845. o->write_character('\n');
  15846. o->write_characters(indent_string.c_str(), current_indent);
  15847. o->write_character('}');
  15848. }
  15849. else
  15850. {
  15851. o->write_characters("{\"bytes\":[", 10);
  15852. if (!val.m_value.binary->empty())
  15853. {
  15854. for (auto i = val.m_value.binary->cbegin();
  15855. i != val.m_value.binary->cend() - 1; ++i)
  15856. {
  15857. dump_integer(*i);
  15858. o->write_character(',');
  15859. }
  15860. dump_integer(val.m_value.binary->back());
  15861. }
  15862. o->write_characters("],\"subtype\":", 12);
  15863. if (val.m_value.binary->has_subtype())
  15864. {
  15865. dump_integer(val.m_value.binary->subtype());
  15866. o->write_character('}');
  15867. }
  15868. else
  15869. {
  15870. o->write_characters("null}", 5);
  15871. }
  15872. }
  15873. return;
  15874. }
  15875. case value_t::boolean:
  15876. {
  15877. if (val.m_value.boolean)
  15878. {
  15879. o->write_characters("true", 4);
  15880. }
  15881. else
  15882. {
  15883. o->write_characters("false", 5);
  15884. }
  15885. return;
  15886. }
  15887. case value_t::number_integer:
  15888. {
  15889. dump_integer(val.m_value.number_integer);
  15890. return;
  15891. }
  15892. case value_t::number_unsigned:
  15893. {
  15894. dump_integer(val.m_value.number_unsigned);
  15895. return;
  15896. }
  15897. case value_t::number_float:
  15898. {
  15899. dump_float(val.m_value.number_float);
  15900. return;
  15901. }
  15902. case value_t::discarded:
  15903. {
  15904. o->write_characters("<discarded>", 11);
  15905. return;
  15906. }
  15907. case value_t::null:
  15908. {
  15909. o->write_characters("null", 4);
  15910. return;
  15911. }
  15912. default: // LCOV_EXCL_LINE
  15913. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  15914. }
  15915. }
  15916. JSON_PRIVATE_UNLESS_TESTED:
  15917. /*!
  15918. @brief dump escaped string
  15919. Escape a string by replacing certain special characters by a sequence of an
  15920. escape character (backslash) and another character and other control
  15921. characters by a sequence of "\u" followed by a four-digit hex
  15922. representation. The escaped string is written to output stream @a o.
  15923. @param[in] s the string to escape
  15924. @param[in] ensure_ascii whether to escape non-ASCII characters with
  15925. \uXXXX sequences
  15926. @complexity Linear in the length of string @a s.
  15927. */
  15928. void dump_escaped(const string_t& s, const bool ensure_ascii)
  15929. {
  15930. std::uint32_t codepoint{};
  15931. std::uint8_t state = UTF8_ACCEPT;
  15932. std::size_t bytes = 0; // number of bytes written to string_buffer
  15933. // number of bytes written at the point of the last valid byte
  15934. std::size_t bytes_after_last_accept = 0;
  15935. std::size_t undumped_chars = 0;
  15936. for (std::size_t i = 0; i < s.size(); ++i)
  15937. {
  15938. const auto byte = static_cast<std::uint8_t>(s[i]);
  15939. switch (decode(state, codepoint, byte))
  15940. {
  15941. case UTF8_ACCEPT: // decode found a new code point
  15942. {
  15943. switch (codepoint)
  15944. {
  15945. case 0x08: // backspace
  15946. {
  15947. string_buffer[bytes++] = '\\';
  15948. string_buffer[bytes++] = 'b';
  15949. break;
  15950. }
  15951. case 0x09: // horizontal tab
  15952. {
  15953. string_buffer[bytes++] = '\\';
  15954. string_buffer[bytes++] = 't';
  15955. break;
  15956. }
  15957. case 0x0A: // newline
  15958. {
  15959. string_buffer[bytes++] = '\\';
  15960. string_buffer[bytes++] = 'n';
  15961. break;
  15962. }
  15963. case 0x0C: // formfeed
  15964. {
  15965. string_buffer[bytes++] = '\\';
  15966. string_buffer[bytes++] = 'f';
  15967. break;
  15968. }
  15969. case 0x0D: // carriage return
  15970. {
  15971. string_buffer[bytes++] = '\\';
  15972. string_buffer[bytes++] = 'r';
  15973. break;
  15974. }
  15975. case 0x22: // quotation mark
  15976. {
  15977. string_buffer[bytes++] = '\\';
  15978. string_buffer[bytes++] = '\"';
  15979. break;
  15980. }
  15981. case 0x5C: // reverse solidus
  15982. {
  15983. string_buffer[bytes++] = '\\';
  15984. string_buffer[bytes++] = '\\';
  15985. break;
  15986. }
  15987. default:
  15988. {
  15989. // escape control characters (0x00..0x1F) or, if
  15990. // ensure_ascii parameter is used, non-ASCII characters
  15991. if ((codepoint <= 0x1F) || (ensure_ascii && (codepoint >= 0x7F)))
  15992. {
  15993. if (codepoint <= 0xFFFF)
  15994. {
  15995. // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  15996. static_cast<void>((std::snprintf)(string_buffer.data() + bytes, 7, "\\u%04x",
  15997. static_cast<std::uint16_t>(codepoint)));
  15998. bytes += 6;
  15999. }
  16000. else
  16001. {
  16002. // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  16003. static_cast<void>((std::snprintf)(string_buffer.data() + bytes, 13, "\\u%04x\\u%04x",
  16004. static_cast<std::uint16_t>(0xD7C0u + (codepoint >> 10u)),
  16005. static_cast<std::uint16_t>(0xDC00u + (codepoint & 0x3FFu))));
  16006. bytes += 12;
  16007. }
  16008. }
  16009. else
  16010. {
  16011. // copy byte to buffer (all previous bytes
  16012. // been copied have in default case above)
  16013. string_buffer[bytes++] = s[i];
  16014. }
  16015. break;
  16016. }
  16017. }
  16018. // write buffer and reset index; there must be 13 bytes
  16019. // left, as this is the maximal number of bytes to be
  16020. // written ("\uxxxx\uxxxx\0") for one code point
  16021. if (string_buffer.size() - bytes < 13)
  16022. {
  16023. o->write_characters(string_buffer.data(), bytes);
  16024. bytes = 0;
  16025. }
  16026. // remember the byte position of this accept
  16027. bytes_after_last_accept = bytes;
  16028. undumped_chars = 0;
  16029. break;
  16030. }
  16031. case UTF8_REJECT: // decode found invalid UTF-8 byte
  16032. {
  16033. switch (error_handler)
  16034. {
  16035. case error_handler_t::strict:
  16036. {
  16037. JSON_THROW(type_error::create(316, concat("invalid UTF-8 byte at index ", std::to_string(i), ": 0x", hex_bytes(byte | 0)), nullptr));
  16038. }
  16039. case error_handler_t::ignore:
  16040. case error_handler_t::replace:
  16041. {
  16042. // in case we saw this character the first time, we
  16043. // would like to read it again, because the byte
  16044. // may be OK for itself, but just not OK for the
  16045. // previous sequence
  16046. if (undumped_chars > 0)
  16047. {
  16048. --i;
  16049. }
  16050. // reset length buffer to the last accepted index;
  16051. // thus removing/ignoring the invalid characters
  16052. bytes = bytes_after_last_accept;
  16053. if (error_handler == error_handler_t::replace)
  16054. {
  16055. // add a replacement character
  16056. if (ensure_ascii)
  16057. {
  16058. string_buffer[bytes++] = '\\';
  16059. string_buffer[bytes++] = 'u';
  16060. string_buffer[bytes++] = 'f';
  16061. string_buffer[bytes++] = 'f';
  16062. string_buffer[bytes++] = 'f';
  16063. string_buffer[bytes++] = 'd';
  16064. }
  16065. else
  16066. {
  16067. string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xEF');
  16068. string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xBF');
  16069. string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xBD');
  16070. }
  16071. // write buffer and reset index; there must be 13 bytes
  16072. // left, as this is the maximal number of bytes to be
  16073. // written ("\uxxxx\uxxxx\0") for one code point
  16074. if (string_buffer.size() - bytes < 13)
  16075. {
  16076. o->write_characters(string_buffer.data(), bytes);
  16077. bytes = 0;
  16078. }
  16079. bytes_after_last_accept = bytes;
  16080. }
  16081. undumped_chars = 0;
  16082. // continue processing the string
  16083. state = UTF8_ACCEPT;
  16084. break;
  16085. }
  16086. default: // LCOV_EXCL_LINE
  16087. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  16088. }
  16089. break;
  16090. }
  16091. default: // decode found yet incomplete multi-byte code point
  16092. {
  16093. if (!ensure_ascii)
  16094. {
  16095. // code point will not be escaped - copy byte to buffer
  16096. string_buffer[bytes++] = s[i];
  16097. }
  16098. ++undumped_chars;
  16099. break;
  16100. }
  16101. }
  16102. }
  16103. // we finished processing the string
  16104. if (JSON_HEDLEY_LIKELY(state == UTF8_ACCEPT))
  16105. {
  16106. // write buffer
  16107. if (bytes > 0)
  16108. {
  16109. o->write_characters(string_buffer.data(), bytes);
  16110. }
  16111. }
  16112. else
  16113. {
  16114. // we finish reading, but do not accept: string was incomplete
  16115. switch (error_handler)
  16116. {
  16117. case error_handler_t::strict:
  16118. {
  16119. 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));
  16120. }
  16121. case error_handler_t::ignore:
  16122. {
  16123. // write all accepted bytes
  16124. o->write_characters(string_buffer.data(), bytes_after_last_accept);
  16125. break;
  16126. }
  16127. case error_handler_t::replace:
  16128. {
  16129. // write all accepted bytes
  16130. o->write_characters(string_buffer.data(), bytes_after_last_accept);
  16131. // add a replacement character
  16132. if (ensure_ascii)
  16133. {
  16134. o->write_characters("\\ufffd", 6);
  16135. }
  16136. else
  16137. {
  16138. o->write_characters("\xEF\xBF\xBD", 3);
  16139. }
  16140. break;
  16141. }
  16142. default: // LCOV_EXCL_LINE
  16143. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  16144. }
  16145. }
  16146. }
  16147. private:
  16148. /*!
  16149. @brief count digits
  16150. Count the number of decimal (base 10) digits for an input unsigned integer.
  16151. @param[in] x unsigned integer number to count its digits
  16152. @return number of decimal digits
  16153. */
  16154. inline unsigned int count_digits(number_unsigned_t x) noexcept
  16155. {
  16156. unsigned int n_digits = 1;
  16157. for (;;)
  16158. {
  16159. if (x < 10)
  16160. {
  16161. return n_digits;
  16162. }
  16163. if (x < 100)
  16164. {
  16165. return n_digits + 1;
  16166. }
  16167. if (x < 1000)
  16168. {
  16169. return n_digits + 2;
  16170. }
  16171. if (x < 10000)
  16172. {
  16173. return n_digits + 3;
  16174. }
  16175. x = x / 10000u;
  16176. n_digits += 4;
  16177. }
  16178. }
  16179. /*!
  16180. * @brief convert a byte to a uppercase hex representation
  16181. * @param[in] byte byte to represent
  16182. * @return representation ("00".."FF")
  16183. */
  16184. static std::string hex_bytes(std::uint8_t byte)
  16185. {
  16186. std::string result = "FF";
  16187. constexpr const char* nibble_to_hex = "0123456789ABCDEF";
  16188. result[0] = nibble_to_hex[byte / 16];
  16189. result[1] = nibble_to_hex[byte % 16];
  16190. return result;
  16191. }
  16192. // templates to avoid warnings about useless casts
  16193. template <typename NumberType, enable_if_t<std::is_signed<NumberType>::value, int> = 0>
  16194. bool is_negative_number(NumberType x)
  16195. {
  16196. return x < 0;
  16197. }
  16198. template < typename NumberType, enable_if_t <std::is_unsigned<NumberType>::value, int > = 0 >
  16199. bool is_negative_number(NumberType /*unused*/)
  16200. {
  16201. return false;
  16202. }
  16203. /*!
  16204. @brief dump an integer
  16205. Dump a given integer to output stream @a o. Works internally with
  16206. @a number_buffer.
  16207. @param[in] x integer number (signed or unsigned) to dump
  16208. @tparam NumberType either @a number_integer_t or @a number_unsigned_t
  16209. */
  16210. template < typename NumberType, detail::enable_if_t <
  16211. std::is_integral<NumberType>::value ||
  16212. std::is_same<NumberType, number_unsigned_t>::value ||
  16213. std::is_same<NumberType, number_integer_t>::value ||
  16214. std::is_same<NumberType, binary_char_t>::value,
  16215. int > = 0 >
  16216. void dump_integer(NumberType x)
  16217. {
  16218. static constexpr std::array<std::array<char, 2>, 100> digits_to_99
  16219. {
  16220. {
  16221. {{'0', '0'}}, {{'0', '1'}}, {{'0', '2'}}, {{'0', '3'}}, {{'0', '4'}}, {{'0', '5'}}, {{'0', '6'}}, {{'0', '7'}}, {{'0', '8'}}, {{'0', '9'}},
  16222. {{'1', '0'}}, {{'1', '1'}}, {{'1', '2'}}, {{'1', '3'}}, {{'1', '4'}}, {{'1', '5'}}, {{'1', '6'}}, {{'1', '7'}}, {{'1', '8'}}, {{'1', '9'}},
  16223. {{'2', '0'}}, {{'2', '1'}}, {{'2', '2'}}, {{'2', '3'}}, {{'2', '4'}}, {{'2', '5'}}, {{'2', '6'}}, {{'2', '7'}}, {{'2', '8'}}, {{'2', '9'}},
  16224. {{'3', '0'}}, {{'3', '1'}}, {{'3', '2'}}, {{'3', '3'}}, {{'3', '4'}}, {{'3', '5'}}, {{'3', '6'}}, {{'3', '7'}}, {{'3', '8'}}, {{'3', '9'}},
  16225. {{'4', '0'}}, {{'4', '1'}}, {{'4', '2'}}, {{'4', '3'}}, {{'4', '4'}}, {{'4', '5'}}, {{'4', '6'}}, {{'4', '7'}}, {{'4', '8'}}, {{'4', '9'}},
  16226. {{'5', '0'}}, {{'5', '1'}}, {{'5', '2'}}, {{'5', '3'}}, {{'5', '4'}}, {{'5', '5'}}, {{'5', '6'}}, {{'5', '7'}}, {{'5', '8'}}, {{'5', '9'}},
  16227. {{'6', '0'}}, {{'6', '1'}}, {{'6', '2'}}, {{'6', '3'}}, {{'6', '4'}}, {{'6', '5'}}, {{'6', '6'}}, {{'6', '7'}}, {{'6', '8'}}, {{'6', '9'}},
  16228. {{'7', '0'}}, {{'7', '1'}}, {{'7', '2'}}, {{'7', '3'}}, {{'7', '4'}}, {{'7', '5'}}, {{'7', '6'}}, {{'7', '7'}}, {{'7', '8'}}, {{'7', '9'}},
  16229. {{'8', '0'}}, {{'8', '1'}}, {{'8', '2'}}, {{'8', '3'}}, {{'8', '4'}}, {{'8', '5'}}, {{'8', '6'}}, {{'8', '7'}}, {{'8', '8'}}, {{'8', '9'}},
  16230. {{'9', '0'}}, {{'9', '1'}}, {{'9', '2'}}, {{'9', '3'}}, {{'9', '4'}}, {{'9', '5'}}, {{'9', '6'}}, {{'9', '7'}}, {{'9', '8'}}, {{'9', '9'}},
  16231. }
  16232. };
  16233. // special case for "0"
  16234. if (x == 0)
  16235. {
  16236. o->write_character('0');
  16237. return;
  16238. }
  16239. // use a pointer to fill the buffer
  16240. auto buffer_ptr = number_buffer.begin(); // NOLINT(llvm-qualified-auto,readability-qualified-auto,cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  16241. number_unsigned_t abs_value;
  16242. unsigned int n_chars{};
  16243. if (is_negative_number(x))
  16244. {
  16245. *buffer_ptr = '-';
  16246. abs_value = remove_sign(static_cast<number_integer_t>(x));
  16247. // account one more byte for the minus sign
  16248. n_chars = 1 + count_digits(abs_value);
  16249. }
  16250. else
  16251. {
  16252. abs_value = static_cast<number_unsigned_t>(x);
  16253. n_chars = count_digits(abs_value);
  16254. }
  16255. // spare 1 byte for '\0'
  16256. JSON_ASSERT(n_chars < number_buffer.size() - 1);
  16257. // jump to the end to generate the string from backward,
  16258. // so we later avoid reversing the result
  16259. buffer_ptr += n_chars;
  16260. // Fast int2ascii implementation inspired by "Fastware" talk by Andrei Alexandrescu
  16261. // See: https://www.youtube.com/watch?v=o4-CwDo2zpg
  16262. while (abs_value >= 100)
  16263. {
  16264. const auto digits_index = static_cast<unsigned>((abs_value % 100));
  16265. abs_value /= 100;
  16266. *(--buffer_ptr) = digits_to_99[digits_index][1];
  16267. *(--buffer_ptr) = digits_to_99[digits_index][0];
  16268. }
  16269. if (abs_value >= 10)
  16270. {
  16271. const auto digits_index = static_cast<unsigned>(abs_value);
  16272. *(--buffer_ptr) = digits_to_99[digits_index][1];
  16273. *(--buffer_ptr) = digits_to_99[digits_index][0];
  16274. }
  16275. else
  16276. {
  16277. *(--buffer_ptr) = static_cast<char>('0' + abs_value);
  16278. }
  16279. o->write_characters(number_buffer.data(), n_chars);
  16280. }
  16281. /*!
  16282. @brief dump a floating-point number
  16283. Dump a given floating-point number to output stream @a o. Works internally
  16284. with @a number_buffer.
  16285. @param[in] x floating-point number to dump
  16286. */
  16287. void dump_float(number_float_t x)
  16288. {
  16289. // NaN / inf
  16290. if (!std::isfinite(x))
  16291. {
  16292. o->write_characters("null", 4);
  16293. return;
  16294. }
  16295. // If number_float_t is an IEEE-754 single or double precision number,
  16296. // use the Grisu2 algorithm to produce short numbers which are
  16297. // guaranteed to round-trip, using strtof and strtod, resp.
  16298. //
  16299. // NB: The test below works if <long double> == <double>.
  16300. static constexpr bool is_ieee_single_or_double
  16301. = (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) ||
  16302. (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);
  16303. dump_float(x, std::integral_constant<bool, is_ieee_single_or_double>());
  16304. }
  16305. void dump_float(number_float_t x, std::true_type /*is_ieee_single_or_double*/)
  16306. {
  16307. auto* begin = number_buffer.data();
  16308. auto* end = ::nlohmann::detail::to_chars(begin, begin + number_buffer.size(), x);
  16309. o->write_characters(begin, static_cast<size_t>(end - begin));
  16310. }
  16311. void dump_float(number_float_t x, std::false_type /*is_ieee_single_or_double*/)
  16312. {
  16313. // get number of digits for a float -> text -> float round-trip
  16314. static constexpr auto d = std::numeric_limits<number_float_t>::max_digits10;
  16315. // the actual conversion
  16316. // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  16317. std::ptrdiff_t len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*g", d, x);
  16318. // negative value indicates an error
  16319. JSON_ASSERT(len > 0);
  16320. // check if buffer was large enough
  16321. JSON_ASSERT(static_cast<std::size_t>(len) < number_buffer.size());
  16322. // erase thousands separator
  16323. if (thousands_sep != '\0')
  16324. {
  16325. // NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::remove returns an iterator, see https://github.com/nlohmann/json/issues/3081
  16326. const auto end = std::remove(number_buffer.begin(), number_buffer.begin() + len, thousands_sep);
  16327. std::fill(end, number_buffer.end(), '\0');
  16328. JSON_ASSERT((end - number_buffer.begin()) <= len);
  16329. len = (end - number_buffer.begin());
  16330. }
  16331. // convert decimal point to '.'
  16332. if (decimal_point != '\0' && decimal_point != '.')
  16333. {
  16334. // NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::find returns an iterator, see https://github.com/nlohmann/json/issues/3081
  16335. const auto dec_pos = std::find(number_buffer.begin(), number_buffer.end(), decimal_point);
  16336. if (dec_pos != number_buffer.end())
  16337. {
  16338. *dec_pos = '.';
  16339. }
  16340. }
  16341. o->write_characters(number_buffer.data(), static_cast<std::size_t>(len));
  16342. // determine if we need to append ".0"
  16343. const bool value_is_int_like =
  16344. std::none_of(number_buffer.begin(), number_buffer.begin() + len + 1,
  16345. [](char c)
  16346. {
  16347. return c == '.' || c == 'e';
  16348. });
  16349. if (value_is_int_like)
  16350. {
  16351. o->write_characters(".0", 2);
  16352. }
  16353. }
  16354. /*!
  16355. @brief check whether a string is UTF-8 encoded
  16356. The function checks each byte of a string whether it is UTF-8 encoded. The
  16357. result of the check is stored in the @a state parameter. The function must
  16358. be called initially with state 0 (accept). State 1 means the string must
  16359. be rejected, because the current byte is not allowed. If the string is
  16360. completely processed, but the state is non-zero, the string ended
  16361. prematurely; that is, the last byte indicated more bytes should have
  16362. followed.
  16363. @param[in,out] state the state of the decoding
  16364. @param[in,out] codep codepoint (valid only if resulting state is UTF8_ACCEPT)
  16365. @param[in] byte next byte to decode
  16366. @return new state
  16367. @note The function has been edited: a std::array is used.
  16368. @copyright Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
  16369. @sa http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
  16370. */
  16371. static std::uint8_t decode(std::uint8_t& state, std::uint32_t& codep, const std::uint8_t byte) noexcept
  16372. {
  16373. static const std::array<std::uint8_t, 400> utf8d =
  16374. {
  16375. {
  16376. 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
  16377. 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
  16378. 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
  16379. 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
  16380. 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
  16381. 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
  16382. 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
  16383. 0xA, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x4, 0x3, 0x3, // E0..EF
  16384. 0xB, 0x6, 0x6, 0x6, 0x5, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, // F0..FF
  16385. 0x0, 0x1, 0x2, 0x3, 0x5, 0x8, 0x7, 0x1, 0x1, 0x1, 0x4, 0x6, 0x1, 0x1, 0x1, 0x1, // s0..s0
  16386. 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
  16387. 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
  16388. 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
  16389. 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
  16390. }
  16391. };
  16392. JSON_ASSERT(byte < utf8d.size());
  16393. const std::uint8_t type = utf8d[byte];
  16394. codep = (state != UTF8_ACCEPT)
  16395. ? (byte & 0x3fu) | (codep << 6u)
  16396. : (0xFFu >> type) & (byte);
  16397. std::size_t index = 256u + static_cast<size_t>(state) * 16u + static_cast<size_t>(type);
  16398. JSON_ASSERT(index < 400);
  16399. state = utf8d[index];
  16400. return state;
  16401. }
  16402. /*
  16403. * Overload to make the compiler happy while it is instantiating
  16404. * dump_integer for number_unsigned_t.
  16405. * Must never be called.
  16406. */
  16407. number_unsigned_t remove_sign(number_unsigned_t x)
  16408. {
  16409. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  16410. return x; // LCOV_EXCL_LINE
  16411. }
  16412. /*
  16413. * Helper function for dump_integer
  16414. *
  16415. * This function takes a negative signed integer and returns its absolute
  16416. * value as unsigned integer. The plus/minus shuffling is necessary as we can
  16417. * not directly remove the sign of an arbitrary signed integer as the
  16418. * absolute values of INT_MIN and INT_MAX are usually not the same. See
  16419. * #1708 for details.
  16420. */
  16421. inline number_unsigned_t remove_sign(number_integer_t x) noexcept
  16422. {
  16423. JSON_ASSERT(x < 0 && x < (std::numeric_limits<number_integer_t>::max)()); // NOLINT(misc-redundant-expression)
  16424. return static_cast<number_unsigned_t>(-(x + 1)) + 1;
  16425. }
  16426. private:
  16427. /// the output of the serializer
  16428. output_adapter_t<char> o = nullptr;
  16429. /// a (hopefully) large enough character buffer
  16430. std::array<char, 64> number_buffer{{}};
  16431. /// the locale
  16432. const std::lconv* loc = nullptr;
  16433. /// the locale's thousand separator character
  16434. const char thousands_sep = '\0';
  16435. /// the locale's decimal point character
  16436. const char decimal_point = '\0';
  16437. /// string buffer
  16438. std::array<char, 512> string_buffer{{}};
  16439. /// the indentation character
  16440. const char indent_char;
  16441. /// the indentation string
  16442. string_t indent_string;
  16443. /// error_handler how to react on decoding errors
  16444. const error_handler_t error_handler;
  16445. };
  16446. } // namespace detail
  16447. NLOHMANN_JSON_NAMESPACE_END
  16448. // #include <nlohmann/detail/value_t.hpp>
  16449. // #include <nlohmann/json_fwd.hpp>
  16450. // #include <nlohmann/ordered_map.hpp>
  16451. // __ _____ _____ _____
  16452. // __| | __| | | | JSON for Modern C++
  16453. // | | |__ | | | | | | version 3.11.2
  16454. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  16455. //
  16456. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  16457. // SPDX-License-Identifier: MIT
  16458. #include <functional> // equal_to, less
  16459. #include <initializer_list> // initializer_list
  16460. #include <iterator> // input_iterator_tag, iterator_traits
  16461. #include <memory> // allocator
  16462. #include <stdexcept> // for out_of_range
  16463. #include <type_traits> // enable_if, is_convertible
  16464. #include <utility> // pair
  16465. #include <vector> // vector
  16466. // #include <nlohmann/detail/macro_scope.hpp>
  16467. // #include <nlohmann/detail/meta/type_traits.hpp>
  16468. NLOHMANN_JSON_NAMESPACE_BEGIN
  16469. /// ordered_map: a minimal map-like container that preserves insertion order
  16470. /// for use within nlohmann::basic_json<ordered_map>
  16471. template <class Key, class T, class IgnoredLess = std::less<Key>,
  16472. class Allocator = std::allocator<std::pair<const Key, T>>>
  16473. struct ordered_map : std::vector<std::pair<const Key, T>, Allocator>
  16474. {
  16475. using key_type = Key;
  16476. using mapped_type = T;
  16477. using Container = std::vector<std::pair<const Key, T>, Allocator>;
  16478. using iterator = typename Container::iterator;
  16479. using const_iterator = typename Container::const_iterator;
  16480. using size_type = typename Container::size_type;
  16481. using value_type = typename Container::value_type;
  16482. #ifdef JSON_HAS_CPP_14
  16483. using key_compare = std::equal_to<>;
  16484. #else
  16485. using key_compare = std::equal_to<Key>;
  16486. #endif
  16487. // Explicit constructors instead of `using Container::Container`
  16488. // otherwise older compilers choke on it (GCC <= 5.5, xcode <= 9.4)
  16489. ordered_map() noexcept(noexcept(Container())) : Container{} {}
  16490. explicit ordered_map(const Allocator& alloc) noexcept(noexcept(Container(alloc))) : Container{alloc} {}
  16491. template <class It>
  16492. ordered_map(It first, It last, const Allocator& alloc = Allocator())
  16493. : Container{first, last, alloc} {}
  16494. ordered_map(std::initializer_list<value_type> init, const Allocator& alloc = Allocator() )
  16495. : Container{init, alloc} {}
  16496. std::pair<iterator, bool> emplace(const key_type& key, T&& t)
  16497. {
  16498. for (auto it = this->begin(); it != this->end(); ++it)
  16499. {
  16500. if (m_compare(it->first, key))
  16501. {
  16502. return {it, false};
  16503. }
  16504. }
  16505. Container::emplace_back(key, std::forward<T>(t));
  16506. return {std::prev(this->end()), true};
  16507. }
  16508. template<class KeyType, detail::enable_if_t<
  16509. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16510. std::pair<iterator, bool> emplace(KeyType && key, T && t)
  16511. {
  16512. for (auto it = this->begin(); it != this->end(); ++it)
  16513. {
  16514. if (m_compare(it->first, key))
  16515. {
  16516. return {it, false};
  16517. }
  16518. }
  16519. Container::emplace_back(std::forward<KeyType>(key), std::forward<T>(t));
  16520. return {std::prev(this->end()), true};
  16521. }
  16522. T& operator[](const key_type& key)
  16523. {
  16524. return emplace(key, T{}).first->second;
  16525. }
  16526. template<class KeyType, detail::enable_if_t<
  16527. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16528. T & operator[](KeyType && key)
  16529. {
  16530. return emplace(std::forward<KeyType>(key), T{}).first->second;
  16531. }
  16532. const T& operator[](const key_type& key) const
  16533. {
  16534. return at(key);
  16535. }
  16536. template<class KeyType, detail::enable_if_t<
  16537. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16538. const T & operator[](KeyType && key) const
  16539. {
  16540. return at(std::forward<KeyType>(key));
  16541. }
  16542. T& at(const key_type& key)
  16543. {
  16544. for (auto it = this->begin(); it != this->end(); ++it)
  16545. {
  16546. if (m_compare(it->first, key))
  16547. {
  16548. return it->second;
  16549. }
  16550. }
  16551. JSON_THROW(std::out_of_range("key not found"));
  16552. }
  16553. template<class KeyType, detail::enable_if_t<
  16554. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16555. T & at(KeyType && key)
  16556. {
  16557. for (auto it = this->begin(); it != this->end(); ++it)
  16558. {
  16559. if (m_compare(it->first, key))
  16560. {
  16561. return it->second;
  16562. }
  16563. }
  16564. JSON_THROW(std::out_of_range("key not found"));
  16565. }
  16566. const T& at(const key_type& key) const
  16567. {
  16568. for (auto it = this->begin(); it != this->end(); ++it)
  16569. {
  16570. if (m_compare(it->first, key))
  16571. {
  16572. return it->second;
  16573. }
  16574. }
  16575. JSON_THROW(std::out_of_range("key not found"));
  16576. }
  16577. template<class KeyType, detail::enable_if_t<
  16578. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16579. const T & at(KeyType && key) const
  16580. {
  16581. for (auto it = this->begin(); it != this->end(); ++it)
  16582. {
  16583. if (m_compare(it->first, key))
  16584. {
  16585. return it->second;
  16586. }
  16587. }
  16588. JSON_THROW(std::out_of_range("key not found"));
  16589. }
  16590. size_type erase(const key_type& key)
  16591. {
  16592. for (auto it = this->begin(); it != this->end(); ++it)
  16593. {
  16594. if (m_compare(it->first, key))
  16595. {
  16596. // Since we cannot move const Keys, re-construct them in place
  16597. for (auto next = it; ++next != this->end(); ++it)
  16598. {
  16599. it->~value_type(); // Destroy but keep allocation
  16600. new (&*it) value_type{std::move(*next)};
  16601. }
  16602. Container::pop_back();
  16603. return 1;
  16604. }
  16605. }
  16606. return 0;
  16607. }
  16608. template<class KeyType, detail::enable_if_t<
  16609. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16610. size_type erase(KeyType && key)
  16611. {
  16612. for (auto it = this->begin(); it != this->end(); ++it)
  16613. {
  16614. if (m_compare(it->first, key))
  16615. {
  16616. // Since we cannot move const Keys, re-construct them in place
  16617. for (auto next = it; ++next != this->end(); ++it)
  16618. {
  16619. it->~value_type(); // Destroy but keep allocation
  16620. new (&*it) value_type{std::move(*next)};
  16621. }
  16622. Container::pop_back();
  16623. return 1;
  16624. }
  16625. }
  16626. return 0;
  16627. }
  16628. iterator erase(iterator pos)
  16629. {
  16630. return erase(pos, std::next(pos));
  16631. }
  16632. iterator erase(iterator first, iterator last)
  16633. {
  16634. if (first == last)
  16635. {
  16636. return first;
  16637. }
  16638. const auto elements_affected = std::distance(first, last);
  16639. const auto offset = std::distance(Container::begin(), first);
  16640. // This is the start situation. We need to delete elements_affected
  16641. // elements (3 in this example: e, f, g), and need to return an
  16642. // iterator past the last deleted element (h in this example).
  16643. // Note that offset is the distance from the start of the vector
  16644. // to first. We will need this later.
  16645. // [ a, b, c, d, e, f, g, h, i, j ]
  16646. // ^ ^
  16647. // first last
  16648. // Since we cannot move const Keys, we re-construct them in place.
  16649. // We start at first and re-construct (viz. copy) the elements from
  16650. // the back of the vector. Example for first iteration:
  16651. // ,--------.
  16652. // v | destroy e and re-construct with h
  16653. // [ a, b, c, d, e, f, g, h, i, j ]
  16654. // ^ ^
  16655. // it it + elements_affected
  16656. for (auto it = first; std::next(it, elements_affected) != Container::end(); ++it)
  16657. {
  16658. it->~value_type(); // destroy but keep allocation
  16659. new (&*it) value_type{std::move(*std::next(it, elements_affected))}; // "move" next element to it
  16660. }
  16661. // [ a, b, c, d, h, i, j, h, i, j ]
  16662. // ^ ^
  16663. // first last
  16664. // remove the unneeded elements at the end of the vector
  16665. Container::resize(this->size() - static_cast<size_type>(elements_affected));
  16666. // [ a, b, c, d, h, i, j ]
  16667. // ^ ^
  16668. // first last
  16669. // first is now pointing past the last deleted element, but we cannot
  16670. // use this iterator, because it may have been invalidated by the
  16671. // resize call. Instead, we can return begin() + offset.
  16672. return Container::begin() + offset;
  16673. }
  16674. size_type count(const key_type& key) const
  16675. {
  16676. for (auto it = this->begin(); it != this->end(); ++it)
  16677. {
  16678. if (m_compare(it->first, key))
  16679. {
  16680. return 1;
  16681. }
  16682. }
  16683. return 0;
  16684. }
  16685. template<class KeyType, detail::enable_if_t<
  16686. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16687. size_type count(KeyType && key) const
  16688. {
  16689. for (auto it = this->begin(); it != this->end(); ++it)
  16690. {
  16691. if (m_compare(it->first, key))
  16692. {
  16693. return 1;
  16694. }
  16695. }
  16696. return 0;
  16697. }
  16698. iterator find(const key_type& key)
  16699. {
  16700. for (auto it = this->begin(); it != this->end(); ++it)
  16701. {
  16702. if (m_compare(it->first, key))
  16703. {
  16704. return it;
  16705. }
  16706. }
  16707. return Container::end();
  16708. }
  16709. template<class KeyType, detail::enable_if_t<
  16710. detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
  16711. iterator find(KeyType && key)
  16712. {
  16713. for (auto it = this->begin(); it != this->end(); ++it)
  16714. {
  16715. if (m_compare(it->first, key))
  16716. {
  16717. return it;
  16718. }
  16719. }
  16720. return Container::end();
  16721. }
  16722. const_iterator find(const key_type& key) const
  16723. {
  16724. for (auto it = this->begin(); it != this->end(); ++it)
  16725. {
  16726. if (m_compare(it->first, key))
  16727. {
  16728. return it;
  16729. }
  16730. }
  16731. return Container::end();
  16732. }
  16733. std::pair<iterator, bool> insert( value_type&& value )
  16734. {
  16735. return emplace(value.first, std::move(value.second));
  16736. }
  16737. std::pair<iterator, bool> insert( const value_type& value )
  16738. {
  16739. for (auto it = this->begin(); it != this->end(); ++it)
  16740. {
  16741. if (m_compare(it->first, value.first))
  16742. {
  16743. return {it, false};
  16744. }
  16745. }
  16746. Container::push_back(value);
  16747. return {--this->end(), true};
  16748. }
  16749. template<typename InputIt>
  16750. using require_input_iter = typename std::enable_if<std::is_convertible<typename std::iterator_traits<InputIt>::iterator_category,
  16751. std::input_iterator_tag>::value>::type;
  16752. template<typename InputIt, typename = require_input_iter<InputIt>>
  16753. void insert(InputIt first, InputIt last)
  16754. {
  16755. for (auto it = first; it != last; ++it)
  16756. {
  16757. insert(*it);
  16758. }
  16759. }
  16760. private:
  16761. JSON_NO_UNIQUE_ADDRESS key_compare m_compare = key_compare();
  16762. };
  16763. NLOHMANN_JSON_NAMESPACE_END
  16764. #if defined(JSON_HAS_CPP_17)
  16765. #include <any>
  16766. #include <string_view>
  16767. #endif
  16768. /*!
  16769. @brief namespace for Niels Lohmann
  16770. @see https://github.com/nlohmann
  16771. @since version 1.0.0
  16772. */
  16773. NLOHMANN_JSON_NAMESPACE_BEGIN
  16774. /*!
  16775. @brief a class to store JSON values
  16776. @internal
  16777. @invariant The member variables @a m_value and @a m_type have the following
  16778. relationship:
  16779. - If `m_type == value_t::object`, then `m_value.object != nullptr`.
  16780. - If `m_type == value_t::array`, then `m_value.array != nullptr`.
  16781. - If `m_type == value_t::string`, then `m_value.string != nullptr`.
  16782. The invariants are checked by member function assert_invariant().
  16783. @note ObjectType trick from https://stackoverflow.com/a/9860911
  16784. @endinternal
  16785. @since version 1.0.0
  16786. @nosubgrouping
  16787. */
  16788. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  16789. class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions)
  16790. {
  16791. private:
  16792. template<detail::value_t> friend struct detail::external_constructor;
  16793. template<typename>
  16794. friend class ::nlohmann::json_pointer;
  16795. // can be restored when json_pointer backwards compatibility is removed
  16796. // friend ::nlohmann::json_pointer<StringType>;
  16797. template<typename BasicJsonType, typename InputType>
  16798. friend class ::nlohmann::detail::parser;
  16799. friend ::nlohmann::detail::serializer<basic_json>;
  16800. template<typename BasicJsonType>
  16801. friend class ::nlohmann::detail::iter_impl;
  16802. template<typename BasicJsonType, typename CharType>
  16803. friend class ::nlohmann::detail::binary_writer;
  16804. template<typename BasicJsonType, typename InputType, typename SAX>
  16805. friend class ::nlohmann::detail::binary_reader;
  16806. template<typename BasicJsonType>
  16807. friend class ::nlohmann::detail::json_sax_dom_parser;
  16808. template<typename BasicJsonType>
  16809. friend class ::nlohmann::detail::json_sax_dom_callback_parser;
  16810. friend class ::nlohmann::detail::exception;
  16811. /// workaround type for MSVC
  16812. using basic_json_t = NLOHMANN_BASIC_JSON_TPL;
  16813. JSON_PRIVATE_UNLESS_TESTED:
  16814. // convenience aliases for types residing in namespace detail;
  16815. using lexer = ::nlohmann::detail::lexer_base<basic_json>;
  16816. template<typename InputAdapterType>
  16817. static ::nlohmann::detail::parser<basic_json, InputAdapterType> parser(
  16818. InputAdapterType adapter,
  16819. detail::parser_callback_t<basic_json>cb = nullptr,
  16820. const bool allow_exceptions = true,
  16821. const bool ignore_comments = false
  16822. )
  16823. {
  16824. return ::nlohmann::detail::parser<basic_json, InputAdapterType>(std::move(adapter),
  16825. std::move(cb), allow_exceptions, ignore_comments);
  16826. }
  16827. private:
  16828. using primitive_iterator_t = ::nlohmann::detail::primitive_iterator_t;
  16829. template<typename BasicJsonType>
  16830. using internal_iterator = ::nlohmann::detail::internal_iterator<BasicJsonType>;
  16831. template<typename BasicJsonType>
  16832. using iter_impl = ::nlohmann::detail::iter_impl<BasicJsonType>;
  16833. template<typename Iterator>
  16834. using iteration_proxy = ::nlohmann::detail::iteration_proxy<Iterator>;
  16835. template<typename Base> using json_reverse_iterator = ::nlohmann::detail::json_reverse_iterator<Base>;
  16836. template<typename CharType>
  16837. using output_adapter_t = ::nlohmann::detail::output_adapter_t<CharType>;
  16838. template<typename InputType>
  16839. using binary_reader = ::nlohmann::detail::binary_reader<basic_json, InputType>;
  16840. template<typename CharType> using binary_writer = ::nlohmann::detail::binary_writer<basic_json, CharType>;
  16841. JSON_PRIVATE_UNLESS_TESTED:
  16842. using serializer = ::nlohmann::detail::serializer<basic_json>;
  16843. public:
  16844. using value_t = detail::value_t;
  16845. /// JSON Pointer, see @ref nlohmann::json_pointer
  16846. using json_pointer = ::nlohmann::json_pointer<StringType>;
  16847. template<typename T, typename SFINAE>
  16848. using json_serializer = JSONSerializer<T, SFINAE>;
  16849. /// how to treat decoding errors
  16850. using error_handler_t = detail::error_handler_t;
  16851. /// how to treat CBOR tags
  16852. using cbor_tag_handler_t = detail::cbor_tag_handler_t;
  16853. /// helper type for initializer lists of basic_json values
  16854. using initializer_list_t = std::initializer_list<detail::json_ref<basic_json>>;
  16855. using input_format_t = detail::input_format_t;
  16856. /// SAX interface type, see @ref nlohmann::json_sax
  16857. using json_sax_t = json_sax<basic_json>;
  16858. ////////////////
  16859. // exceptions //
  16860. ////////////////
  16861. /// @name exceptions
  16862. /// Classes to implement user-defined exceptions.
  16863. /// @{
  16864. using exception = detail::exception;
  16865. using parse_error = detail::parse_error;
  16866. using invalid_iterator = detail::invalid_iterator;
  16867. using type_error = detail::type_error;
  16868. using out_of_range = detail::out_of_range;
  16869. using other_error = detail::other_error;
  16870. /// @}
  16871. /////////////////////
  16872. // container types //
  16873. /////////////////////
  16874. /// @name container types
  16875. /// The canonic container types to use @ref basic_json like any other STL
  16876. /// container.
  16877. /// @{
  16878. /// the type of elements in a basic_json container
  16879. using value_type = basic_json;
  16880. /// the type of an element reference
  16881. using reference = value_type&;
  16882. /// the type of an element const reference
  16883. using const_reference = const value_type&;
  16884. /// a type to represent differences between iterators
  16885. using difference_type = std::ptrdiff_t;
  16886. /// a type to represent container sizes
  16887. using size_type = std::size_t;
  16888. /// the allocator type
  16889. using allocator_type = AllocatorType<basic_json>;
  16890. /// the type of an element pointer
  16891. using pointer = typename std::allocator_traits<allocator_type>::pointer;
  16892. /// the type of an element const pointer
  16893. using const_pointer = typename std::allocator_traits<allocator_type>::const_pointer;
  16894. /// an iterator for a basic_json container
  16895. using iterator = iter_impl<basic_json>;
  16896. /// a const iterator for a basic_json container
  16897. using const_iterator = iter_impl<const basic_json>;
  16898. /// a reverse iterator for a basic_json container
  16899. using reverse_iterator = json_reverse_iterator<typename basic_json::iterator>;
  16900. /// a const reverse iterator for a basic_json container
  16901. using const_reverse_iterator = json_reverse_iterator<typename basic_json::const_iterator>;
  16902. /// @}
  16903. /// @brief returns the allocator associated with the container
  16904. /// @sa https://json.nlohmann.me/api/basic_json/get_allocator/
  16905. static allocator_type get_allocator()
  16906. {
  16907. return allocator_type();
  16908. }
  16909. /// @brief returns version information on the library
  16910. /// @sa https://json.nlohmann.me/api/basic_json/meta/
  16911. JSON_HEDLEY_WARN_UNUSED_RESULT
  16912. static basic_json meta()
  16913. {
  16914. basic_json result;
  16915. result["copyright"] = "(C) 2013-2022 Niels Lohmann";
  16916. result["name"] = "JSON for Modern C++";
  16917. result["url"] = "https://github.com/nlohmann/json";
  16918. result["version"]["string"] =
  16919. detail::concat(std::to_string(NLOHMANN_JSON_VERSION_MAJOR), '.',
  16920. std::to_string(NLOHMANN_JSON_VERSION_MINOR), '.',
  16921. std::to_string(NLOHMANN_JSON_VERSION_PATCH));
  16922. result["version"]["major"] = NLOHMANN_JSON_VERSION_MAJOR;
  16923. result["version"]["minor"] = NLOHMANN_JSON_VERSION_MINOR;
  16924. result["version"]["patch"] = NLOHMANN_JSON_VERSION_PATCH;
  16925. #ifdef _WIN32
  16926. result["platform"] = "win32";
  16927. #elif defined __linux__
  16928. result["platform"] = "linux";
  16929. #elif defined __APPLE__
  16930. result["platform"] = "apple";
  16931. #elif defined __unix__
  16932. result["platform"] = "unix";
  16933. #else
  16934. result["platform"] = "unknown";
  16935. #endif
  16936. #if defined(__ICC) || defined(__INTEL_COMPILER)
  16937. result["compiler"] = {{"family", "icc"}, {"version", __INTEL_COMPILER}};
  16938. #elif defined(__clang__)
  16939. result["compiler"] = {{"family", "clang"}, {"version", __clang_version__}};
  16940. #elif defined(__GNUC__) || defined(__GNUG__)
  16941. result["compiler"] = {{"family", "gcc"}, {"version", detail::concat(
  16942. std::to_string(__GNUC__), '.',
  16943. std::to_string(__GNUC_MINOR__), '.',
  16944. std::to_string(__GNUC_PATCHLEVEL__))
  16945. }
  16946. };
  16947. #elif defined(__HP_cc) || defined(__HP_aCC)
  16948. result["compiler"] = "hp"
  16949. #elif defined(__IBMCPP__)
  16950. result["compiler"] = {{"family", "ilecpp"}, {"version", __IBMCPP__}};
  16951. #elif defined(_MSC_VER)
  16952. result["compiler"] = {{"family", "msvc"}, {"version", _MSC_VER}};
  16953. #elif defined(__PGI)
  16954. result["compiler"] = {{"family", "pgcpp"}, {"version", __PGI}};
  16955. #elif defined(__SUNPRO_CC)
  16956. result["compiler"] = {{"family", "sunpro"}, {"version", __SUNPRO_CC}};
  16957. #else
  16958. result["compiler"] = {{"family", "unknown"}, {"version", "unknown"}};
  16959. #endif
  16960. #if defined(_MSVC_LANG)
  16961. result["compiler"]["c++"] = std::to_string(_MSVC_LANG);
  16962. #elif defined(__cplusplus)
  16963. result["compiler"]["c++"] = std::to_string(__cplusplus);
  16964. #else
  16965. result["compiler"]["c++"] = "unknown";
  16966. #endif
  16967. return result;
  16968. }
  16969. ///////////////////////////
  16970. // JSON value data types //
  16971. ///////////////////////////
  16972. /// @name JSON value data types
  16973. /// The data types to store a JSON value. These types are derived from
  16974. /// the template arguments passed to class @ref basic_json.
  16975. /// @{
  16976. /// @brief default object key comparator type
  16977. /// The actual object key comparator type (@ref object_comparator_t) may be
  16978. /// different.
  16979. /// @sa https://json.nlohmann.me/api/basic_json/default_object_comparator_t/
  16980. #if defined(JSON_HAS_CPP_14)
  16981. // use of transparent comparator avoids unnecessary repeated construction of temporaries
  16982. // in functions involving lookup by key with types other than object_t::key_type (aka. StringType)
  16983. using default_object_comparator_t = std::less<>;
  16984. #else
  16985. using default_object_comparator_t = std::less<StringType>;
  16986. #endif
  16987. /// @brief a type for an object
  16988. /// @sa https://json.nlohmann.me/api/basic_json/object_t/
  16989. using object_t = ObjectType<StringType,
  16990. basic_json,
  16991. default_object_comparator_t,
  16992. AllocatorType<std::pair<const StringType,
  16993. basic_json>>>;
  16994. /// @brief a type for an array
  16995. /// @sa https://json.nlohmann.me/api/basic_json/array_t/
  16996. using array_t = ArrayType<basic_json, AllocatorType<basic_json>>;
  16997. /// @brief a type for a string
  16998. /// @sa https://json.nlohmann.me/api/basic_json/string_t/
  16999. using string_t = StringType;
  17000. /// @brief a type for a boolean
  17001. /// @sa https://json.nlohmann.me/api/basic_json/boolean_t/
  17002. using boolean_t = BooleanType;
  17003. /// @brief a type for a number (integer)
  17004. /// @sa https://json.nlohmann.me/api/basic_json/number_integer_t/
  17005. using number_integer_t = NumberIntegerType;
  17006. /// @brief a type for a number (unsigned)
  17007. /// @sa https://json.nlohmann.me/api/basic_json/number_unsigned_t/
  17008. using number_unsigned_t = NumberUnsignedType;
  17009. /// @brief a type for a number (floating-point)
  17010. /// @sa https://json.nlohmann.me/api/basic_json/number_float_t/
  17011. using number_float_t = NumberFloatType;
  17012. /// @brief a type for a packed binary type
  17013. /// @sa https://json.nlohmann.me/api/basic_json/binary_t/
  17014. using binary_t = nlohmann::byte_container_with_subtype<BinaryType>;
  17015. /// @brief object key comparator type
  17016. /// @sa https://json.nlohmann.me/api/basic_json/object_comparator_t/
  17017. using object_comparator_t = detail::actual_object_comparator_t<basic_json>;
  17018. /// @}
  17019. private:
  17020. /// helper for exception-safe object creation
  17021. template<typename T, typename... Args>
  17022. JSON_HEDLEY_RETURNS_NON_NULL
  17023. static T* create(Args&& ... args)
  17024. {
  17025. AllocatorType<T> alloc;
  17026. using AllocatorTraits = std::allocator_traits<AllocatorType<T>>;
  17027. auto deleter = [&](T * obj)
  17028. {
  17029. AllocatorTraits::deallocate(alloc, obj, 1);
  17030. };
  17031. std::unique_ptr<T, decltype(deleter)> obj(AllocatorTraits::allocate(alloc, 1), deleter);
  17032. AllocatorTraits::construct(alloc, obj.get(), std::forward<Args>(args)...);
  17033. JSON_ASSERT(obj != nullptr);
  17034. return obj.release();
  17035. }
  17036. ////////////////////////
  17037. // JSON value storage //
  17038. ////////////////////////
  17039. JSON_PRIVATE_UNLESS_TESTED:
  17040. /*!
  17041. @brief a JSON value
  17042. The actual storage for a JSON value of the @ref basic_json class. This
  17043. union combines the different storage types for the JSON value types
  17044. defined in @ref value_t.
  17045. JSON type | value_t type | used type
  17046. --------- | --------------- | ------------------------
  17047. object | object | pointer to @ref object_t
  17048. array | array | pointer to @ref array_t
  17049. string | string | pointer to @ref string_t
  17050. boolean | boolean | @ref boolean_t
  17051. number | number_integer | @ref number_integer_t
  17052. number | number_unsigned | @ref number_unsigned_t
  17053. number | number_float | @ref number_float_t
  17054. binary | binary | pointer to @ref binary_t
  17055. null | null | *no value is stored*
  17056. @note Variable-length types (objects, arrays, and strings) are stored as
  17057. pointers. The size of the union should not exceed 64 bits if the default
  17058. value types are used.
  17059. @since version 1.0.0
  17060. */
  17061. union json_value
  17062. {
  17063. /// object (stored with pointer to save storage)
  17064. object_t* object;
  17065. /// array (stored with pointer to save storage)
  17066. array_t* array;
  17067. /// string (stored with pointer to save storage)
  17068. string_t* string;
  17069. /// binary (stored with pointer to save storage)
  17070. binary_t* binary;
  17071. /// boolean
  17072. boolean_t boolean;
  17073. /// number (integer)
  17074. number_integer_t number_integer;
  17075. /// number (unsigned integer)
  17076. number_unsigned_t number_unsigned;
  17077. /// number (floating-point)
  17078. number_float_t number_float;
  17079. /// default constructor (for null values)
  17080. json_value() = default;
  17081. /// constructor for booleans
  17082. json_value(boolean_t v) noexcept : boolean(v) {}
  17083. /// constructor for numbers (integer)
  17084. json_value(number_integer_t v) noexcept : number_integer(v) {}
  17085. /// constructor for numbers (unsigned)
  17086. json_value(number_unsigned_t v) noexcept : number_unsigned(v) {}
  17087. /// constructor for numbers (floating-point)
  17088. json_value(number_float_t v) noexcept : number_float(v) {}
  17089. /// constructor for empty values of a given type
  17090. json_value(value_t t)
  17091. {
  17092. switch (t)
  17093. {
  17094. case value_t::object:
  17095. {
  17096. object = create<object_t>();
  17097. break;
  17098. }
  17099. case value_t::array:
  17100. {
  17101. array = create<array_t>();
  17102. break;
  17103. }
  17104. case value_t::string:
  17105. {
  17106. string = create<string_t>("");
  17107. break;
  17108. }
  17109. case value_t::binary:
  17110. {
  17111. binary = create<binary_t>();
  17112. break;
  17113. }
  17114. case value_t::boolean:
  17115. {
  17116. boolean = static_cast<boolean_t>(false);
  17117. break;
  17118. }
  17119. case value_t::number_integer:
  17120. {
  17121. number_integer = static_cast<number_integer_t>(0);
  17122. break;
  17123. }
  17124. case value_t::number_unsigned:
  17125. {
  17126. number_unsigned = static_cast<number_unsigned_t>(0);
  17127. break;
  17128. }
  17129. case value_t::number_float:
  17130. {
  17131. number_float = static_cast<number_float_t>(0.0);
  17132. break;
  17133. }
  17134. case value_t::null:
  17135. {
  17136. object = nullptr; // silence warning, see #821
  17137. break;
  17138. }
  17139. case value_t::discarded:
  17140. default:
  17141. {
  17142. object = nullptr; // silence warning, see #821
  17143. if (JSON_HEDLEY_UNLIKELY(t == value_t::null))
  17144. {
  17145. JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.11.2", nullptr)); // LCOV_EXCL_LINE
  17146. }
  17147. break;
  17148. }
  17149. }
  17150. }
  17151. /// constructor for strings
  17152. json_value(const string_t& value) : string(create<string_t>(value)) {}
  17153. /// constructor for rvalue strings
  17154. json_value(string_t&& value) : string(create<string_t>(std::move(value))) {}
  17155. /// constructor for objects
  17156. json_value(const object_t& value) : object(create<object_t>(value)) {}
  17157. /// constructor for rvalue objects
  17158. json_value(object_t&& value) : object(create<object_t>(std::move(value))) {}
  17159. /// constructor for arrays
  17160. json_value(const array_t& value) : array(create<array_t>(value)) {}
  17161. /// constructor for rvalue arrays
  17162. json_value(array_t&& value) : array(create<array_t>(std::move(value))) {}
  17163. /// constructor for binary arrays
  17164. json_value(const typename binary_t::container_type& value) : binary(create<binary_t>(value)) {}
  17165. /// constructor for rvalue binary arrays
  17166. json_value(typename binary_t::container_type&& value) : binary(create<binary_t>(std::move(value))) {}
  17167. /// constructor for binary arrays (internal type)
  17168. json_value(const binary_t& value) : binary(create<binary_t>(value)) {}
  17169. /// constructor for rvalue binary arrays (internal type)
  17170. json_value(binary_t&& value) : binary(create<binary_t>(std::move(value))) {}
  17171. void destroy(value_t t)
  17172. {
  17173. if (t == value_t::array || t == value_t::object)
  17174. {
  17175. // flatten the current json_value to a heap-allocated stack
  17176. std::vector<basic_json> stack;
  17177. // move the top-level items to stack
  17178. if (t == value_t::array)
  17179. {
  17180. stack.reserve(array->size());
  17181. std::move(array->begin(), array->end(), std::back_inserter(stack));
  17182. }
  17183. else
  17184. {
  17185. stack.reserve(object->size());
  17186. for (auto&& it : *object)
  17187. {
  17188. stack.push_back(std::move(it.second));
  17189. }
  17190. }
  17191. while (!stack.empty())
  17192. {
  17193. // move the last item to local variable to be processed
  17194. basic_json current_item(std::move(stack.back()));
  17195. stack.pop_back();
  17196. // if current_item is array/object, move
  17197. // its children to the stack to be processed later
  17198. if (current_item.is_array())
  17199. {
  17200. std::move(current_item.m_value.array->begin(), current_item.m_value.array->end(), std::back_inserter(stack));
  17201. current_item.m_value.array->clear();
  17202. }
  17203. else if (current_item.is_object())
  17204. {
  17205. for (auto&& it : *current_item.m_value.object)
  17206. {
  17207. stack.push_back(std::move(it.second));
  17208. }
  17209. current_item.m_value.object->clear();
  17210. }
  17211. // it's now safe that current_item get destructed
  17212. // since it doesn't have any children
  17213. }
  17214. }
  17215. switch (t)
  17216. {
  17217. case value_t::object:
  17218. {
  17219. AllocatorType<object_t> alloc;
  17220. std::allocator_traits<decltype(alloc)>::destroy(alloc, object);
  17221. std::allocator_traits<decltype(alloc)>::deallocate(alloc, object, 1);
  17222. break;
  17223. }
  17224. case value_t::array:
  17225. {
  17226. AllocatorType<array_t> alloc;
  17227. std::allocator_traits<decltype(alloc)>::destroy(alloc, array);
  17228. std::allocator_traits<decltype(alloc)>::deallocate(alloc, array, 1);
  17229. break;
  17230. }
  17231. case value_t::string:
  17232. {
  17233. AllocatorType<string_t> alloc;
  17234. std::allocator_traits<decltype(alloc)>::destroy(alloc, string);
  17235. std::allocator_traits<decltype(alloc)>::deallocate(alloc, string, 1);
  17236. break;
  17237. }
  17238. case value_t::binary:
  17239. {
  17240. AllocatorType<binary_t> alloc;
  17241. std::allocator_traits<decltype(alloc)>::destroy(alloc, binary);
  17242. std::allocator_traits<decltype(alloc)>::deallocate(alloc, binary, 1);
  17243. break;
  17244. }
  17245. case value_t::null:
  17246. case value_t::boolean:
  17247. case value_t::number_integer:
  17248. case value_t::number_unsigned:
  17249. case value_t::number_float:
  17250. case value_t::discarded:
  17251. default:
  17252. {
  17253. break;
  17254. }
  17255. }
  17256. }
  17257. };
  17258. private:
  17259. /*!
  17260. @brief checks the class invariants
  17261. This function asserts the class invariants. It needs to be called at the
  17262. end of every constructor to make sure that created objects respect the
  17263. invariant. Furthermore, it has to be called each time the type of a JSON
  17264. value is changed, because the invariant expresses a relationship between
  17265. @a m_type and @a m_value.
  17266. Furthermore, the parent relation is checked for arrays and objects: If
  17267. @a check_parents true and the value is an array or object, then the
  17268. container's elements must have the current value as parent.
  17269. @param[in] check_parents whether the parent relation should be checked.
  17270. The value is true by default and should only be set to false
  17271. during destruction of objects when the invariant does not
  17272. need to hold.
  17273. */
  17274. void assert_invariant(bool check_parents = true) const noexcept
  17275. {
  17276. JSON_ASSERT(m_type != value_t::object || m_value.object != nullptr);
  17277. JSON_ASSERT(m_type != value_t::array || m_value.array != nullptr);
  17278. JSON_ASSERT(m_type != value_t::string || m_value.string != nullptr);
  17279. JSON_ASSERT(m_type != value_t::binary || m_value.binary != nullptr);
  17280. #if JSON_DIAGNOSTICS
  17281. JSON_TRY
  17282. {
  17283. // cppcheck-suppress assertWithSideEffect
  17284. JSON_ASSERT(!check_parents || !is_structured() || std::all_of(begin(), end(), [this](const basic_json & j)
  17285. {
  17286. return j.m_parent == this;
  17287. }));
  17288. }
  17289. JSON_CATCH(...) {} // LCOV_EXCL_LINE
  17290. #endif
  17291. static_cast<void>(check_parents);
  17292. }
  17293. void set_parents()
  17294. {
  17295. #if JSON_DIAGNOSTICS
  17296. switch (m_type)
  17297. {
  17298. case value_t::array:
  17299. {
  17300. for (auto& element : *m_value.array)
  17301. {
  17302. element.m_parent = this;
  17303. }
  17304. break;
  17305. }
  17306. case value_t::object:
  17307. {
  17308. for (auto& element : *m_value.object)
  17309. {
  17310. element.second.m_parent = this;
  17311. }
  17312. break;
  17313. }
  17314. case value_t::null:
  17315. case value_t::string:
  17316. case value_t::boolean:
  17317. case value_t::number_integer:
  17318. case value_t::number_unsigned:
  17319. case value_t::number_float:
  17320. case value_t::binary:
  17321. case value_t::discarded:
  17322. default:
  17323. break;
  17324. }
  17325. #endif
  17326. }
  17327. iterator set_parents(iterator it, typename iterator::difference_type count_set_parents)
  17328. {
  17329. #if JSON_DIAGNOSTICS
  17330. for (typename iterator::difference_type i = 0; i < count_set_parents; ++i)
  17331. {
  17332. (it + i)->m_parent = this;
  17333. }
  17334. #else
  17335. static_cast<void>(count_set_parents);
  17336. #endif
  17337. return it;
  17338. }
  17339. reference set_parent(reference j, std::size_t old_capacity = static_cast<std::size_t>(-1))
  17340. {
  17341. #if JSON_DIAGNOSTICS
  17342. if (old_capacity != static_cast<std::size_t>(-1))
  17343. {
  17344. // see https://github.com/nlohmann/json/issues/2838
  17345. JSON_ASSERT(type() == value_t::array);
  17346. if (JSON_HEDLEY_UNLIKELY(m_value.array->capacity() != old_capacity))
  17347. {
  17348. // capacity has changed: update all parents
  17349. set_parents();
  17350. return j;
  17351. }
  17352. }
  17353. // ordered_json uses a vector internally, so pointers could have
  17354. // been invalidated; see https://github.com/nlohmann/json/issues/2962
  17355. #ifdef JSON_HEDLEY_MSVC_VERSION
  17356. #pragma warning(push )
  17357. #pragma warning(disable : 4127) // ignore warning to replace if with if constexpr
  17358. #endif
  17359. if (detail::is_ordered_map<object_t>::value)
  17360. {
  17361. set_parents();
  17362. return j;
  17363. }
  17364. #ifdef JSON_HEDLEY_MSVC_VERSION
  17365. #pragma warning( pop )
  17366. #endif
  17367. j.m_parent = this;
  17368. #else
  17369. static_cast<void>(j);
  17370. static_cast<void>(old_capacity);
  17371. #endif
  17372. return j;
  17373. }
  17374. public:
  17375. //////////////////////////
  17376. // JSON parser callback //
  17377. //////////////////////////
  17378. /// @brief parser event types
  17379. /// @sa https://json.nlohmann.me/api/basic_json/parse_event_t/
  17380. using parse_event_t = detail::parse_event_t;
  17381. /// @brief per-element parser callback type
  17382. /// @sa https://json.nlohmann.me/api/basic_json/parser_callback_t/
  17383. using parser_callback_t = detail::parser_callback_t<basic_json>;
  17384. //////////////////
  17385. // constructors //
  17386. //////////////////
  17387. /// @name constructors and destructors
  17388. /// Constructors of class @ref basic_json, copy/move constructor, copy
  17389. /// assignment, static functions creating objects, and the destructor.
  17390. /// @{
  17391. /// @brief create an empty value with a given type
  17392. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17393. basic_json(const value_t v)
  17394. : m_type(v), m_value(v)
  17395. {
  17396. assert_invariant();
  17397. }
  17398. /// @brief create a null object
  17399. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17400. basic_json(std::nullptr_t = nullptr) noexcept // NOLINT(bugprone-exception-escape)
  17401. : basic_json(value_t::null)
  17402. {
  17403. assert_invariant();
  17404. }
  17405. /// @brief create a JSON value from compatible types
  17406. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17407. template < typename CompatibleType,
  17408. typename U = detail::uncvref_t<CompatibleType>,
  17409. detail::enable_if_t <
  17410. !detail::is_basic_json<U>::value && detail::is_compatible_type<basic_json_t, U>::value, int > = 0 >
  17411. basic_json(CompatibleType && val) noexcept(noexcept( // NOLINT(bugprone-forwarding-reference-overload,bugprone-exception-escape)
  17412. JSONSerializer<U>::to_json(std::declval<basic_json_t&>(),
  17413. std::forward<CompatibleType>(val))))
  17414. {
  17415. JSONSerializer<U>::to_json(*this, std::forward<CompatibleType>(val));
  17416. set_parents();
  17417. assert_invariant();
  17418. }
  17419. /// @brief create a JSON value from an existing one
  17420. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17421. template < typename BasicJsonType,
  17422. detail::enable_if_t <
  17423. detail::is_basic_json<BasicJsonType>::value&& !std::is_same<basic_json, BasicJsonType>::value, int > = 0 >
  17424. basic_json(const BasicJsonType& val)
  17425. {
  17426. using other_boolean_t = typename BasicJsonType::boolean_t;
  17427. using other_number_float_t = typename BasicJsonType::number_float_t;
  17428. using other_number_integer_t = typename BasicJsonType::number_integer_t;
  17429. using other_number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  17430. using other_string_t = typename BasicJsonType::string_t;
  17431. using other_object_t = typename BasicJsonType::object_t;
  17432. using other_array_t = typename BasicJsonType::array_t;
  17433. using other_binary_t = typename BasicJsonType::binary_t;
  17434. switch (val.type())
  17435. {
  17436. case value_t::boolean:
  17437. JSONSerializer<other_boolean_t>::to_json(*this, val.template get<other_boolean_t>());
  17438. break;
  17439. case value_t::number_float:
  17440. JSONSerializer<other_number_float_t>::to_json(*this, val.template get<other_number_float_t>());
  17441. break;
  17442. case value_t::number_integer:
  17443. JSONSerializer<other_number_integer_t>::to_json(*this, val.template get<other_number_integer_t>());
  17444. break;
  17445. case value_t::number_unsigned:
  17446. JSONSerializer<other_number_unsigned_t>::to_json(*this, val.template get<other_number_unsigned_t>());
  17447. break;
  17448. case value_t::string:
  17449. JSONSerializer<other_string_t>::to_json(*this, val.template get_ref<const other_string_t&>());
  17450. break;
  17451. case value_t::object:
  17452. JSONSerializer<other_object_t>::to_json(*this, val.template get_ref<const other_object_t&>());
  17453. break;
  17454. case value_t::array:
  17455. JSONSerializer<other_array_t>::to_json(*this, val.template get_ref<const other_array_t&>());
  17456. break;
  17457. case value_t::binary:
  17458. JSONSerializer<other_binary_t>::to_json(*this, val.template get_ref<const other_binary_t&>());
  17459. break;
  17460. case value_t::null:
  17461. *this = nullptr;
  17462. break;
  17463. case value_t::discarded:
  17464. m_type = value_t::discarded;
  17465. break;
  17466. default: // LCOV_EXCL_LINE
  17467. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  17468. }
  17469. JSON_ASSERT(m_type == val.type());
  17470. set_parents();
  17471. assert_invariant();
  17472. }
  17473. /// @brief create a container (array or object) from an initializer list
  17474. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17475. basic_json(initializer_list_t init,
  17476. bool type_deduction = true,
  17477. value_t manual_type = value_t::array)
  17478. {
  17479. // check if each element is an array with two elements whose first
  17480. // element is a string
  17481. bool is_an_object = std::all_of(init.begin(), init.end(),
  17482. [](const detail::json_ref<basic_json>& element_ref)
  17483. {
  17484. return element_ref->is_array() && element_ref->size() == 2 && (*element_ref)[0].is_string();
  17485. });
  17486. // adjust type if type deduction is not wanted
  17487. if (!type_deduction)
  17488. {
  17489. // if array is wanted, do not create an object though possible
  17490. if (manual_type == value_t::array)
  17491. {
  17492. is_an_object = false;
  17493. }
  17494. // if object is wanted but impossible, throw an exception
  17495. if (JSON_HEDLEY_UNLIKELY(manual_type == value_t::object && !is_an_object))
  17496. {
  17497. JSON_THROW(type_error::create(301, "cannot create object from initializer list", nullptr));
  17498. }
  17499. }
  17500. if (is_an_object)
  17501. {
  17502. // the initializer list is a list of pairs -> create object
  17503. m_type = value_t::object;
  17504. m_value = value_t::object;
  17505. for (auto& element_ref : init)
  17506. {
  17507. auto element = element_ref.moved_or_copied();
  17508. m_value.object->emplace(
  17509. std::move(*((*element.m_value.array)[0].m_value.string)),
  17510. std::move((*element.m_value.array)[1]));
  17511. }
  17512. }
  17513. else
  17514. {
  17515. // the initializer list describes an array -> create array
  17516. m_type = value_t::array;
  17517. m_value.array = create<array_t>(init.begin(), init.end());
  17518. }
  17519. set_parents();
  17520. assert_invariant();
  17521. }
  17522. /// @brief explicitly create a binary array (without subtype)
  17523. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  17524. JSON_HEDLEY_WARN_UNUSED_RESULT
  17525. static basic_json binary(const typename binary_t::container_type& init)
  17526. {
  17527. auto res = basic_json();
  17528. res.m_type = value_t::binary;
  17529. res.m_value = init;
  17530. return res;
  17531. }
  17532. /// @brief explicitly create a binary array (with subtype)
  17533. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  17534. JSON_HEDLEY_WARN_UNUSED_RESULT
  17535. static basic_json binary(const typename binary_t::container_type& init, typename binary_t::subtype_type subtype)
  17536. {
  17537. auto res = basic_json();
  17538. res.m_type = value_t::binary;
  17539. res.m_value = binary_t(init, subtype);
  17540. return res;
  17541. }
  17542. /// @brief explicitly create a binary array
  17543. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  17544. JSON_HEDLEY_WARN_UNUSED_RESULT
  17545. static basic_json binary(typename binary_t::container_type&& init)
  17546. {
  17547. auto res = basic_json();
  17548. res.m_type = value_t::binary;
  17549. res.m_value = std::move(init);
  17550. return res;
  17551. }
  17552. /// @brief explicitly create a binary array (with subtype)
  17553. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  17554. JSON_HEDLEY_WARN_UNUSED_RESULT
  17555. static basic_json binary(typename binary_t::container_type&& init, typename binary_t::subtype_type subtype)
  17556. {
  17557. auto res = basic_json();
  17558. res.m_type = value_t::binary;
  17559. res.m_value = binary_t(std::move(init), subtype);
  17560. return res;
  17561. }
  17562. /// @brief explicitly create an array from an initializer list
  17563. /// @sa https://json.nlohmann.me/api/basic_json/array/
  17564. JSON_HEDLEY_WARN_UNUSED_RESULT
  17565. static basic_json array(initializer_list_t init = {})
  17566. {
  17567. return basic_json(init, false, value_t::array);
  17568. }
  17569. /// @brief explicitly create an object from an initializer list
  17570. /// @sa https://json.nlohmann.me/api/basic_json/object/
  17571. JSON_HEDLEY_WARN_UNUSED_RESULT
  17572. static basic_json object(initializer_list_t init = {})
  17573. {
  17574. return basic_json(init, false, value_t::object);
  17575. }
  17576. /// @brief construct an array with count copies of given value
  17577. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17578. basic_json(size_type cnt, const basic_json& val)
  17579. : m_type(value_t::array)
  17580. {
  17581. m_value.array = create<array_t>(cnt, val);
  17582. set_parents();
  17583. assert_invariant();
  17584. }
  17585. /// @brief construct a JSON container given an iterator range
  17586. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17587. template < class InputIT, typename std::enable_if <
  17588. std::is_same<InputIT, typename basic_json_t::iterator>::value ||
  17589. std::is_same<InputIT, typename basic_json_t::const_iterator>::value, int >::type = 0 >
  17590. basic_json(InputIT first, InputIT last)
  17591. {
  17592. JSON_ASSERT(first.m_object != nullptr);
  17593. JSON_ASSERT(last.m_object != nullptr);
  17594. // make sure iterator fits the current value
  17595. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  17596. {
  17597. JSON_THROW(invalid_iterator::create(201, "iterators are not compatible", nullptr));
  17598. }
  17599. // copy type from first iterator
  17600. m_type = first.m_object->m_type;
  17601. // check if iterator range is complete for primitive values
  17602. switch (m_type)
  17603. {
  17604. case value_t::boolean:
  17605. case value_t::number_float:
  17606. case value_t::number_integer:
  17607. case value_t::number_unsigned:
  17608. case value_t::string:
  17609. {
  17610. if (JSON_HEDLEY_UNLIKELY(!first.m_it.primitive_iterator.is_begin()
  17611. || !last.m_it.primitive_iterator.is_end()))
  17612. {
  17613. JSON_THROW(invalid_iterator::create(204, "iterators out of range", first.m_object));
  17614. }
  17615. break;
  17616. }
  17617. case value_t::null:
  17618. case value_t::object:
  17619. case value_t::array:
  17620. case value_t::binary:
  17621. case value_t::discarded:
  17622. default:
  17623. break;
  17624. }
  17625. switch (m_type)
  17626. {
  17627. case value_t::number_integer:
  17628. {
  17629. m_value.number_integer = first.m_object->m_value.number_integer;
  17630. break;
  17631. }
  17632. case value_t::number_unsigned:
  17633. {
  17634. m_value.number_unsigned = first.m_object->m_value.number_unsigned;
  17635. break;
  17636. }
  17637. case value_t::number_float:
  17638. {
  17639. m_value.number_float = first.m_object->m_value.number_float;
  17640. break;
  17641. }
  17642. case value_t::boolean:
  17643. {
  17644. m_value.boolean = first.m_object->m_value.boolean;
  17645. break;
  17646. }
  17647. case value_t::string:
  17648. {
  17649. m_value = *first.m_object->m_value.string;
  17650. break;
  17651. }
  17652. case value_t::object:
  17653. {
  17654. m_value.object = create<object_t>(first.m_it.object_iterator,
  17655. last.m_it.object_iterator);
  17656. break;
  17657. }
  17658. case value_t::array:
  17659. {
  17660. m_value.array = create<array_t>(first.m_it.array_iterator,
  17661. last.m_it.array_iterator);
  17662. break;
  17663. }
  17664. case value_t::binary:
  17665. {
  17666. m_value = *first.m_object->m_value.binary;
  17667. break;
  17668. }
  17669. case value_t::null:
  17670. case value_t::discarded:
  17671. default:
  17672. JSON_THROW(invalid_iterator::create(206, detail::concat("cannot construct with iterators from ", first.m_object->type_name()), first.m_object));
  17673. }
  17674. set_parents();
  17675. assert_invariant();
  17676. }
  17677. ///////////////////////////////////////
  17678. // other constructors and destructor //
  17679. ///////////////////////////////////////
  17680. template<typename JsonRef,
  17681. detail::enable_if_t<detail::conjunction<detail::is_json_ref<JsonRef>,
  17682. std::is_same<typename JsonRef::value_type, basic_json>>::value, int> = 0 >
  17683. basic_json(const JsonRef& ref) : basic_json(ref.moved_or_copied()) {}
  17684. /// @brief copy constructor
  17685. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17686. basic_json(const basic_json& other)
  17687. : m_type(other.m_type)
  17688. {
  17689. // check of passed value is valid
  17690. other.assert_invariant();
  17691. switch (m_type)
  17692. {
  17693. case value_t::object:
  17694. {
  17695. m_value = *other.m_value.object;
  17696. break;
  17697. }
  17698. case value_t::array:
  17699. {
  17700. m_value = *other.m_value.array;
  17701. break;
  17702. }
  17703. case value_t::string:
  17704. {
  17705. m_value = *other.m_value.string;
  17706. break;
  17707. }
  17708. case value_t::boolean:
  17709. {
  17710. m_value = other.m_value.boolean;
  17711. break;
  17712. }
  17713. case value_t::number_integer:
  17714. {
  17715. m_value = other.m_value.number_integer;
  17716. break;
  17717. }
  17718. case value_t::number_unsigned:
  17719. {
  17720. m_value = other.m_value.number_unsigned;
  17721. break;
  17722. }
  17723. case value_t::number_float:
  17724. {
  17725. m_value = other.m_value.number_float;
  17726. break;
  17727. }
  17728. case value_t::binary:
  17729. {
  17730. m_value = *other.m_value.binary;
  17731. break;
  17732. }
  17733. case value_t::null:
  17734. case value_t::discarded:
  17735. default:
  17736. break;
  17737. }
  17738. set_parents();
  17739. assert_invariant();
  17740. }
  17741. /// @brief move constructor
  17742. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17743. basic_json(basic_json&& other) noexcept
  17744. : m_type(std::move(other.m_type)),
  17745. m_value(std::move(other.m_value))
  17746. {
  17747. // check that passed value is valid
  17748. other.assert_invariant(false);
  17749. // invalidate payload
  17750. other.m_type = value_t::null;
  17751. other.m_value = {};
  17752. set_parents();
  17753. assert_invariant();
  17754. }
  17755. /// @brief copy assignment
  17756. /// @sa https://json.nlohmann.me/api/basic_json/operator=/
  17757. basic_json& operator=(basic_json other) noexcept (
  17758. std::is_nothrow_move_constructible<value_t>::value&&
  17759. std::is_nothrow_move_assignable<value_t>::value&&
  17760. std::is_nothrow_move_constructible<json_value>::value&&
  17761. std::is_nothrow_move_assignable<json_value>::value
  17762. )
  17763. {
  17764. // check that passed value is valid
  17765. other.assert_invariant();
  17766. using std::swap;
  17767. swap(m_type, other.m_type);
  17768. swap(m_value, other.m_value);
  17769. set_parents();
  17770. assert_invariant();
  17771. return *this;
  17772. }
  17773. /// @brief destructor
  17774. /// @sa https://json.nlohmann.me/api/basic_json/~basic_json/
  17775. ~basic_json() noexcept
  17776. {
  17777. assert_invariant(false);
  17778. m_value.destroy(m_type);
  17779. }
  17780. /// @}
  17781. public:
  17782. ///////////////////////
  17783. // object inspection //
  17784. ///////////////////////
  17785. /// @name object inspection
  17786. /// Functions to inspect the type of a JSON value.
  17787. /// @{
  17788. /// @brief serialization
  17789. /// @sa https://json.nlohmann.me/api/basic_json/dump/
  17790. string_t dump(const int indent = -1,
  17791. const char indent_char = ' ',
  17792. const bool ensure_ascii = false,
  17793. const error_handler_t error_handler = error_handler_t::strict) const
  17794. {
  17795. string_t result;
  17796. serializer s(detail::output_adapter<char, string_t>(result), indent_char, error_handler);
  17797. if (indent >= 0)
  17798. {
  17799. s.dump(*this, true, ensure_ascii, static_cast<unsigned int>(indent));
  17800. }
  17801. else
  17802. {
  17803. s.dump(*this, false, ensure_ascii, 0);
  17804. }
  17805. return result;
  17806. }
  17807. /// @brief return the type of the JSON value (explicit)
  17808. /// @sa https://json.nlohmann.me/api/basic_json/type/
  17809. constexpr value_t type() const noexcept
  17810. {
  17811. return m_type;
  17812. }
  17813. /// @brief return whether type is primitive
  17814. /// @sa https://json.nlohmann.me/api/basic_json/is_primitive/
  17815. constexpr bool is_primitive() const noexcept
  17816. {
  17817. return is_null() || is_string() || is_boolean() || is_number() || is_binary();
  17818. }
  17819. /// @brief return whether type is structured
  17820. /// @sa https://json.nlohmann.me/api/basic_json/is_structured/
  17821. constexpr bool is_structured() const noexcept
  17822. {
  17823. return is_array() || is_object();
  17824. }
  17825. /// @brief return whether value is null
  17826. /// @sa https://json.nlohmann.me/api/basic_json/is_null/
  17827. constexpr bool is_null() const noexcept
  17828. {
  17829. return m_type == value_t::null;
  17830. }
  17831. /// @brief return whether value is a boolean
  17832. /// @sa https://json.nlohmann.me/api/basic_json/is_boolean/
  17833. constexpr bool is_boolean() const noexcept
  17834. {
  17835. return m_type == value_t::boolean;
  17836. }
  17837. /// @brief return whether value is a number
  17838. /// @sa https://json.nlohmann.me/api/basic_json/is_number/
  17839. constexpr bool is_number() const noexcept
  17840. {
  17841. return is_number_integer() || is_number_float();
  17842. }
  17843. /// @brief return whether value is an integer number
  17844. /// @sa https://json.nlohmann.me/api/basic_json/is_number_integer/
  17845. constexpr bool is_number_integer() const noexcept
  17846. {
  17847. return m_type == value_t::number_integer || m_type == value_t::number_unsigned;
  17848. }
  17849. /// @brief return whether value is an unsigned integer number
  17850. /// @sa https://json.nlohmann.me/api/basic_json/is_number_unsigned/
  17851. constexpr bool is_number_unsigned() const noexcept
  17852. {
  17853. return m_type == value_t::number_unsigned;
  17854. }
  17855. /// @brief return whether value is a floating-point number
  17856. /// @sa https://json.nlohmann.me/api/basic_json/is_number_float/
  17857. constexpr bool is_number_float() const noexcept
  17858. {
  17859. return m_type == value_t::number_float;
  17860. }
  17861. /// @brief return whether value is an object
  17862. /// @sa https://json.nlohmann.me/api/basic_json/is_object/
  17863. constexpr bool is_object() const noexcept
  17864. {
  17865. return m_type == value_t::object;
  17866. }
  17867. /// @brief return whether value is an array
  17868. /// @sa https://json.nlohmann.me/api/basic_json/is_array/
  17869. constexpr bool is_array() const noexcept
  17870. {
  17871. return m_type == value_t::array;
  17872. }
  17873. /// @brief return whether value is a string
  17874. /// @sa https://json.nlohmann.me/api/basic_json/is_string/
  17875. constexpr bool is_string() const noexcept
  17876. {
  17877. return m_type == value_t::string;
  17878. }
  17879. /// @brief return whether value is a binary array
  17880. /// @sa https://json.nlohmann.me/api/basic_json/is_binary/
  17881. constexpr bool is_binary() const noexcept
  17882. {
  17883. return m_type == value_t::binary;
  17884. }
  17885. /// @brief return whether value is discarded
  17886. /// @sa https://json.nlohmann.me/api/basic_json/is_discarded/
  17887. constexpr bool is_discarded() const noexcept
  17888. {
  17889. return m_type == value_t::discarded;
  17890. }
  17891. /// @brief return the type of the JSON value (implicit)
  17892. /// @sa https://json.nlohmann.me/api/basic_json/operator_value_t/
  17893. constexpr operator value_t() const noexcept
  17894. {
  17895. return m_type;
  17896. }
  17897. /// @}
  17898. private:
  17899. //////////////////
  17900. // value access //
  17901. //////////////////
  17902. /// get a boolean (explicit)
  17903. boolean_t get_impl(boolean_t* /*unused*/) const
  17904. {
  17905. if (JSON_HEDLEY_LIKELY(is_boolean()))
  17906. {
  17907. return m_value.boolean;
  17908. }
  17909. JSON_THROW(type_error::create(302, detail::concat("type must be boolean, but is ", type_name()), this));
  17910. }
  17911. /// get a pointer to the value (object)
  17912. object_t* get_impl_ptr(object_t* /*unused*/) noexcept
  17913. {
  17914. return is_object() ? m_value.object : nullptr;
  17915. }
  17916. /// get a pointer to the value (object)
  17917. constexpr const object_t* get_impl_ptr(const object_t* /*unused*/) const noexcept
  17918. {
  17919. return is_object() ? m_value.object : nullptr;
  17920. }
  17921. /// get a pointer to the value (array)
  17922. array_t* get_impl_ptr(array_t* /*unused*/) noexcept
  17923. {
  17924. return is_array() ? m_value.array : nullptr;
  17925. }
  17926. /// get a pointer to the value (array)
  17927. constexpr const array_t* get_impl_ptr(const array_t* /*unused*/) const noexcept
  17928. {
  17929. return is_array() ? m_value.array : nullptr;
  17930. }
  17931. /// get a pointer to the value (string)
  17932. string_t* get_impl_ptr(string_t* /*unused*/) noexcept
  17933. {
  17934. return is_string() ? m_value.string : nullptr;
  17935. }
  17936. /// get a pointer to the value (string)
  17937. constexpr const string_t* get_impl_ptr(const string_t* /*unused*/) const noexcept
  17938. {
  17939. return is_string() ? m_value.string : nullptr;
  17940. }
  17941. /// get a pointer to the value (boolean)
  17942. boolean_t* get_impl_ptr(boolean_t* /*unused*/) noexcept
  17943. {
  17944. return is_boolean() ? &m_value.boolean : nullptr;
  17945. }
  17946. /// get a pointer to the value (boolean)
  17947. constexpr const boolean_t* get_impl_ptr(const boolean_t* /*unused*/) const noexcept
  17948. {
  17949. return is_boolean() ? &m_value.boolean : nullptr;
  17950. }
  17951. /// get a pointer to the value (integer number)
  17952. number_integer_t* get_impl_ptr(number_integer_t* /*unused*/) noexcept
  17953. {
  17954. return is_number_integer() ? &m_value.number_integer : nullptr;
  17955. }
  17956. /// get a pointer to the value (integer number)
  17957. constexpr const number_integer_t* get_impl_ptr(const number_integer_t* /*unused*/) const noexcept
  17958. {
  17959. return is_number_integer() ? &m_value.number_integer : nullptr;
  17960. }
  17961. /// get a pointer to the value (unsigned number)
  17962. number_unsigned_t* get_impl_ptr(number_unsigned_t* /*unused*/) noexcept
  17963. {
  17964. return is_number_unsigned() ? &m_value.number_unsigned : nullptr;
  17965. }
  17966. /// get a pointer to the value (unsigned number)
  17967. constexpr const number_unsigned_t* get_impl_ptr(const number_unsigned_t* /*unused*/) const noexcept
  17968. {
  17969. return is_number_unsigned() ? &m_value.number_unsigned : nullptr;
  17970. }
  17971. /// get a pointer to the value (floating-point number)
  17972. number_float_t* get_impl_ptr(number_float_t* /*unused*/) noexcept
  17973. {
  17974. return is_number_float() ? &m_value.number_float : nullptr;
  17975. }
  17976. /// get a pointer to the value (floating-point number)
  17977. constexpr const number_float_t* get_impl_ptr(const number_float_t* /*unused*/) const noexcept
  17978. {
  17979. return is_number_float() ? &m_value.number_float : nullptr;
  17980. }
  17981. /// get a pointer to the value (binary)
  17982. binary_t* get_impl_ptr(binary_t* /*unused*/) noexcept
  17983. {
  17984. return is_binary() ? m_value.binary : nullptr;
  17985. }
  17986. /// get a pointer to the value (binary)
  17987. constexpr const binary_t* get_impl_ptr(const binary_t* /*unused*/) const noexcept
  17988. {
  17989. return is_binary() ? m_value.binary : nullptr;
  17990. }
  17991. /*!
  17992. @brief helper function to implement get_ref()
  17993. This function helps to implement get_ref() without code duplication for
  17994. const and non-const overloads
  17995. @tparam ThisType will be deduced as `basic_json` or `const basic_json`
  17996. @throw type_error.303 if ReferenceType does not match underlying value
  17997. type of the current JSON
  17998. */
  17999. template<typename ReferenceType, typename ThisType>
  18000. static ReferenceType get_ref_impl(ThisType& obj)
  18001. {
  18002. // delegate the call to get_ptr<>()
  18003. auto* ptr = obj.template get_ptr<typename std::add_pointer<ReferenceType>::type>();
  18004. if (JSON_HEDLEY_LIKELY(ptr != nullptr))
  18005. {
  18006. return *ptr;
  18007. }
  18008. JSON_THROW(type_error::create(303, detail::concat("incompatible ReferenceType for get_ref, actual type is ", obj.type_name()), &obj));
  18009. }
  18010. public:
  18011. /// @name value access
  18012. /// Direct access to the stored value of a JSON value.
  18013. /// @{
  18014. /// @brief get a pointer value (implicit)
  18015. /// @sa https://json.nlohmann.me/api/basic_json/get_ptr/
  18016. template<typename PointerType, typename std::enable_if<
  18017. std::is_pointer<PointerType>::value, int>::type = 0>
  18018. auto get_ptr() noexcept -> decltype(std::declval<basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
  18019. {
  18020. // delegate the call to get_impl_ptr<>()
  18021. return get_impl_ptr(static_cast<PointerType>(nullptr));
  18022. }
  18023. /// @brief get a pointer value (implicit)
  18024. /// @sa https://json.nlohmann.me/api/basic_json/get_ptr/
  18025. template < typename PointerType, typename std::enable_if <
  18026. std::is_pointer<PointerType>::value&&
  18027. std::is_const<typename std::remove_pointer<PointerType>::type>::value, int >::type = 0 >
  18028. constexpr auto get_ptr() const noexcept -> decltype(std::declval<const basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
  18029. {
  18030. // delegate the call to get_impl_ptr<>() const
  18031. return get_impl_ptr(static_cast<PointerType>(nullptr));
  18032. }
  18033. private:
  18034. /*!
  18035. @brief get a value (explicit)
  18036. Explicit type conversion between the JSON value and a compatible value
  18037. which is [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)
  18038. and [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible).
  18039. The value is converted by calling the @ref json_serializer<ValueType>
  18040. `from_json()` method.
  18041. The function is equivalent to executing
  18042. @code {.cpp}
  18043. ValueType ret;
  18044. JSONSerializer<ValueType>::from_json(*this, ret);
  18045. return ret;
  18046. @endcode
  18047. This overloads is chosen if:
  18048. - @a ValueType is not @ref basic_json,
  18049. - @ref json_serializer<ValueType> has a `from_json()` method of the form
  18050. `void from_json(const basic_json&, ValueType&)`, and
  18051. - @ref json_serializer<ValueType> does not have a `from_json()` method of
  18052. the form `ValueType from_json(const basic_json&)`
  18053. @tparam ValueType the returned value type
  18054. @return copy of the JSON value, converted to @a ValueType
  18055. @throw what @ref json_serializer<ValueType> `from_json()` method throws
  18056. @liveexample{The example below shows several conversions from JSON values
  18057. to other types. There a few things to note: (1) Floating-point numbers can
  18058. be converted to integers\, (2) A JSON array can be converted to a standard
  18059. `std::vector<short>`\, (3) A JSON object can be converted to C++
  18060. associative containers such as `std::unordered_map<std::string\,
  18061. json>`.,get__ValueType_const}
  18062. @since version 2.1.0
  18063. */
  18064. template < typename ValueType,
  18065. detail::enable_if_t <
  18066. detail::is_default_constructible<ValueType>::value&&
  18067. detail::has_from_json<basic_json_t, ValueType>::value,
  18068. int > = 0 >
  18069. ValueType get_impl(detail::priority_tag<0> /*unused*/) const noexcept(noexcept(
  18070. JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), std::declval<ValueType&>())))
  18071. {
  18072. auto ret = ValueType();
  18073. JSONSerializer<ValueType>::from_json(*this, ret);
  18074. return ret;
  18075. }
  18076. /*!
  18077. @brief get a value (explicit); special case
  18078. Explicit type conversion between the JSON value and a compatible value
  18079. which is **not** [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)
  18080. and **not** [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible).
  18081. The value is converted by calling the @ref json_serializer<ValueType>
  18082. `from_json()` method.
  18083. The function is equivalent to executing
  18084. @code {.cpp}
  18085. return JSONSerializer<ValueType>::from_json(*this);
  18086. @endcode
  18087. This overloads is chosen if:
  18088. - @a ValueType is not @ref basic_json and
  18089. - @ref json_serializer<ValueType> has a `from_json()` method of the form
  18090. `ValueType from_json(const basic_json&)`
  18091. @note If @ref json_serializer<ValueType> has both overloads of
  18092. `from_json()`, this one is chosen.
  18093. @tparam ValueType the returned value type
  18094. @return copy of the JSON value, converted to @a ValueType
  18095. @throw what @ref json_serializer<ValueType> `from_json()` method throws
  18096. @since version 2.1.0
  18097. */
  18098. template < typename ValueType,
  18099. detail::enable_if_t <
  18100. detail::has_non_default_from_json<basic_json_t, ValueType>::value,
  18101. int > = 0 >
  18102. ValueType get_impl(detail::priority_tag<1> /*unused*/) const noexcept(noexcept(
  18103. JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>())))
  18104. {
  18105. return JSONSerializer<ValueType>::from_json(*this);
  18106. }
  18107. /*!
  18108. @brief get special-case overload
  18109. This overloads converts the current @ref basic_json in a different
  18110. @ref basic_json type
  18111. @tparam BasicJsonType == @ref basic_json
  18112. @return a copy of *this, converted into @a BasicJsonType
  18113. @complexity Depending on the implementation of the called `from_json()`
  18114. method.
  18115. @since version 3.2.0
  18116. */
  18117. template < typename BasicJsonType,
  18118. detail::enable_if_t <
  18119. detail::is_basic_json<BasicJsonType>::value,
  18120. int > = 0 >
  18121. BasicJsonType get_impl(detail::priority_tag<2> /*unused*/) const
  18122. {
  18123. return *this;
  18124. }
  18125. /*!
  18126. @brief get special-case overload
  18127. This overloads avoids a lot of template boilerplate, it can be seen as the
  18128. identity method
  18129. @tparam BasicJsonType == @ref basic_json
  18130. @return a copy of *this
  18131. @complexity Constant.
  18132. @since version 2.1.0
  18133. */
  18134. template<typename BasicJsonType,
  18135. detail::enable_if_t<
  18136. std::is_same<BasicJsonType, basic_json_t>::value,
  18137. int> = 0>
  18138. basic_json get_impl(detail::priority_tag<3> /*unused*/) const
  18139. {
  18140. return *this;
  18141. }
  18142. /*!
  18143. @brief get a pointer value (explicit)
  18144. @copydoc get()
  18145. */
  18146. template<typename PointerType,
  18147. detail::enable_if_t<
  18148. std::is_pointer<PointerType>::value,
  18149. int> = 0>
  18150. constexpr auto get_impl(detail::priority_tag<4> /*unused*/) const noexcept
  18151. -> decltype(std::declval<const basic_json_t&>().template get_ptr<PointerType>())
  18152. {
  18153. // delegate the call to get_ptr
  18154. return get_ptr<PointerType>();
  18155. }
  18156. public:
  18157. /*!
  18158. @brief get a (pointer) value (explicit)
  18159. Performs explicit type conversion between the JSON value and a compatible value if required.
  18160. - If the requested type is a pointer to the internally stored JSON value that pointer is returned.
  18161. No copies are made.
  18162. - If the requested type is the current @ref basic_json, or a different @ref basic_json convertible
  18163. from the current @ref basic_json.
  18164. - Otherwise the value is converted by calling the @ref json_serializer<ValueType> `from_json()`
  18165. method.
  18166. @tparam ValueTypeCV the provided value type
  18167. @tparam ValueType the returned value type
  18168. @return copy of the JSON value, converted to @tparam ValueType if necessary
  18169. @throw what @ref json_serializer<ValueType> `from_json()` method throws if conversion is required
  18170. @since version 2.1.0
  18171. */
  18172. template < typename ValueTypeCV, typename ValueType = detail::uncvref_t<ValueTypeCV>>
  18173. #if defined(JSON_HAS_CPP_14)
  18174. constexpr
  18175. #endif
  18176. auto get() const noexcept(
  18177. noexcept(std::declval<const basic_json_t&>().template get_impl<ValueType>(detail::priority_tag<4> {})))
  18178. -> decltype(std::declval<const basic_json_t&>().template get_impl<ValueType>(detail::priority_tag<4> {}))
  18179. {
  18180. // we cannot static_assert on ValueTypeCV being non-const, because
  18181. // there is support for get<const basic_json_t>(), which is why we
  18182. // still need the uncvref
  18183. static_assert(!std::is_reference<ValueTypeCV>::value,
  18184. "get() cannot be used with reference types, you might want to use get_ref()");
  18185. return get_impl<ValueType>(detail::priority_tag<4> {});
  18186. }
  18187. /*!
  18188. @brief get a pointer value (explicit)
  18189. Explicit pointer access to the internally stored JSON value. No copies are
  18190. made.
  18191. @warning The pointer becomes invalid if the underlying JSON object
  18192. changes.
  18193. @tparam PointerType pointer type; must be a pointer to @ref array_t, @ref
  18194. object_t, @ref string_t, @ref boolean_t, @ref number_integer_t,
  18195. @ref number_unsigned_t, or @ref number_float_t.
  18196. @return pointer to the internally stored JSON value if the requested
  18197. pointer type @a PointerType fits to the JSON value; `nullptr` otherwise
  18198. @complexity Constant.
  18199. @liveexample{The example below shows how pointers to internal values of a
  18200. JSON value can be requested. Note that no type conversions are made and a
  18201. `nullptr` is returned if the value and the requested pointer type does not
  18202. match.,get__PointerType}
  18203. @sa see @ref get_ptr() for explicit pointer-member access
  18204. @since version 1.0.0
  18205. */
  18206. template<typename PointerType, typename std::enable_if<
  18207. std::is_pointer<PointerType>::value, int>::type = 0>
  18208. auto get() noexcept -> decltype(std::declval<basic_json_t&>().template get_ptr<PointerType>())
  18209. {
  18210. // delegate the call to get_ptr
  18211. return get_ptr<PointerType>();
  18212. }
  18213. /// @brief get a value (explicit)
  18214. /// @sa https://json.nlohmann.me/api/basic_json/get_to/
  18215. template < typename ValueType,
  18216. detail::enable_if_t <
  18217. !detail::is_basic_json<ValueType>::value&&
  18218. detail::has_from_json<basic_json_t, ValueType>::value,
  18219. int > = 0 >
  18220. ValueType & get_to(ValueType& v) const noexcept(noexcept(
  18221. JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), v)))
  18222. {
  18223. JSONSerializer<ValueType>::from_json(*this, v);
  18224. return v;
  18225. }
  18226. // specialization to allow calling get_to with a basic_json value
  18227. // see https://github.com/nlohmann/json/issues/2175
  18228. template<typename ValueType,
  18229. detail::enable_if_t <
  18230. detail::is_basic_json<ValueType>::value,
  18231. int> = 0>
  18232. ValueType & get_to(ValueType& v) const
  18233. {
  18234. v = *this;
  18235. return v;
  18236. }
  18237. template <
  18238. typename T, std::size_t N,
  18239. typename Array = T (&)[N], // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  18240. detail::enable_if_t <
  18241. detail::has_from_json<basic_json_t, Array>::value, int > = 0 >
  18242. Array get_to(T (&v)[N]) const // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  18243. noexcept(noexcept(JSONSerializer<Array>::from_json(
  18244. std::declval<const basic_json_t&>(), v)))
  18245. {
  18246. JSONSerializer<Array>::from_json(*this, v);
  18247. return v;
  18248. }
  18249. /// @brief get a reference value (implicit)
  18250. /// @sa https://json.nlohmann.me/api/basic_json/get_ref/
  18251. template<typename ReferenceType, typename std::enable_if<
  18252. std::is_reference<ReferenceType>::value, int>::type = 0>
  18253. ReferenceType get_ref()
  18254. {
  18255. // delegate call to get_ref_impl
  18256. return get_ref_impl<ReferenceType>(*this);
  18257. }
  18258. /// @brief get a reference value (implicit)
  18259. /// @sa https://json.nlohmann.me/api/basic_json/get_ref/
  18260. template < typename ReferenceType, typename std::enable_if <
  18261. std::is_reference<ReferenceType>::value&&
  18262. std::is_const<typename std::remove_reference<ReferenceType>::type>::value, int >::type = 0 >
  18263. ReferenceType get_ref() const
  18264. {
  18265. // delegate call to get_ref_impl
  18266. return get_ref_impl<ReferenceType>(*this);
  18267. }
  18268. /*!
  18269. @brief get a value (implicit)
  18270. Implicit type conversion between the JSON value and a compatible value.
  18271. The call is realized by calling @ref get() const.
  18272. @tparam ValueType non-pointer type compatible to the JSON value, for
  18273. instance `int` for JSON integer numbers, `bool` for JSON booleans, or
  18274. `std::vector` types for JSON arrays. The character type of @ref string_t
  18275. as well as an initializer list of this type is excluded to avoid
  18276. ambiguities as these types implicitly convert to `std::string`.
  18277. @return copy of the JSON value, converted to type @a ValueType
  18278. @throw type_error.302 in case passed type @a ValueType is incompatible
  18279. to the JSON value type (e.g., the JSON value is of type boolean, but a
  18280. string is requested); see example below
  18281. @complexity Linear in the size of the JSON value.
  18282. @liveexample{The example below shows several conversions from JSON values
  18283. to other types. There a few things to note: (1) Floating-point numbers can
  18284. be converted to integers\, (2) A JSON array can be converted to a standard
  18285. `std::vector<short>`\, (3) A JSON object can be converted to C++
  18286. associative containers such as `std::unordered_map<std::string\,
  18287. json>`.,operator__ValueType}
  18288. @since version 1.0.0
  18289. */
  18290. template < typename ValueType, typename std::enable_if <
  18291. detail::conjunction <
  18292. detail::negation<std::is_pointer<ValueType>>,
  18293. detail::negation<std::is_same<ValueType, std::nullptr_t>>,
  18294. detail::negation<std::is_same<ValueType, detail::json_ref<basic_json>>>,
  18295. detail::negation<std::is_same<ValueType, typename string_t::value_type>>,
  18296. detail::negation<detail::is_basic_json<ValueType>>,
  18297. detail::negation<std::is_same<ValueType, std::initializer_list<typename string_t::value_type>>>,
  18298. #if defined(JSON_HAS_CPP_17) && (defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER <= 1914))
  18299. detail::negation<std::is_same<ValueType, std::string_view>>,
  18300. #endif
  18301. #if defined(JSON_HAS_CPP_17)
  18302. detail::negation<std::is_same<ValueType, std::any>>,
  18303. #endif
  18304. detail::is_detected_lazy<detail::get_template_function, const basic_json_t&, ValueType>
  18305. >::value, int >::type = 0 >
  18306. JSON_EXPLICIT operator ValueType() const
  18307. {
  18308. // delegate the call to get<>() const
  18309. return get<ValueType>();
  18310. }
  18311. /// @brief get a binary value
  18312. /// @sa https://json.nlohmann.me/api/basic_json/get_binary/
  18313. binary_t& get_binary()
  18314. {
  18315. if (!is_binary())
  18316. {
  18317. JSON_THROW(type_error::create(302, detail::concat("type must be binary, but is ", type_name()), this));
  18318. }
  18319. return *get_ptr<binary_t*>();
  18320. }
  18321. /// @brief get a binary value
  18322. /// @sa https://json.nlohmann.me/api/basic_json/get_binary/
  18323. const binary_t& get_binary() const
  18324. {
  18325. if (!is_binary())
  18326. {
  18327. JSON_THROW(type_error::create(302, detail::concat("type must be binary, but is ", type_name()), this));
  18328. }
  18329. return *get_ptr<const binary_t*>();
  18330. }
  18331. /// @}
  18332. ////////////////////
  18333. // element access //
  18334. ////////////////////
  18335. /// @name element access
  18336. /// Access to the JSON value.
  18337. /// @{
  18338. /// @brief access specified array element with bounds checking
  18339. /// @sa https://json.nlohmann.me/api/basic_json/at/
  18340. reference at(size_type idx)
  18341. {
  18342. // at only works for arrays
  18343. if (JSON_HEDLEY_LIKELY(is_array()))
  18344. {
  18345. JSON_TRY
  18346. {
  18347. return set_parent(m_value.array->at(idx));
  18348. }
  18349. JSON_CATCH (std::out_of_range&)
  18350. {
  18351. // create better exception explanation
  18352. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
  18353. }
  18354. }
  18355. else
  18356. {
  18357. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  18358. }
  18359. }
  18360. /// @brief access specified array element with bounds checking
  18361. /// @sa https://json.nlohmann.me/api/basic_json/at/
  18362. const_reference at(size_type idx) const
  18363. {
  18364. // at only works for arrays
  18365. if (JSON_HEDLEY_LIKELY(is_array()))
  18366. {
  18367. JSON_TRY
  18368. {
  18369. return m_value.array->at(idx);
  18370. }
  18371. JSON_CATCH (std::out_of_range&)
  18372. {
  18373. // create better exception explanation
  18374. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
  18375. }
  18376. }
  18377. else
  18378. {
  18379. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  18380. }
  18381. }
  18382. /// @brief access specified object element with bounds checking
  18383. /// @sa https://json.nlohmann.me/api/basic_json/at/
  18384. reference at(const typename object_t::key_type& key)
  18385. {
  18386. // at only works for objects
  18387. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  18388. {
  18389. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  18390. }
  18391. auto it = m_value.object->find(key);
  18392. if (it == m_value.object->end())
  18393. {
  18394. JSON_THROW(out_of_range::create(403, detail::concat("key '", key, "' not found"), this));
  18395. }
  18396. return set_parent(it->second);
  18397. }
  18398. /// @brief access specified object element with bounds checking
  18399. /// @sa https://json.nlohmann.me/api/basic_json/at/
  18400. template<class KeyType, detail::enable_if_t<
  18401. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  18402. reference at(KeyType && key)
  18403. {
  18404. // at only works for objects
  18405. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  18406. {
  18407. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  18408. }
  18409. auto it = m_value.object->find(std::forward<KeyType>(key));
  18410. if (it == m_value.object->end())
  18411. {
  18412. JSON_THROW(out_of_range::create(403, detail::concat("key '", string_t(std::forward<KeyType>(key)), "' not found"), this));
  18413. }
  18414. return set_parent(it->second);
  18415. }
  18416. /// @brief access specified object element with bounds checking
  18417. /// @sa https://json.nlohmann.me/api/basic_json/at/
  18418. const_reference at(const typename object_t::key_type& key) const
  18419. {
  18420. // at only works for objects
  18421. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  18422. {
  18423. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  18424. }
  18425. auto it = m_value.object->find(key);
  18426. if (it == m_value.object->end())
  18427. {
  18428. JSON_THROW(out_of_range::create(403, detail::concat("key '", key, "' not found"), this));
  18429. }
  18430. return it->second;
  18431. }
  18432. /// @brief access specified object element with bounds checking
  18433. /// @sa https://json.nlohmann.me/api/basic_json/at/
  18434. template<class KeyType, detail::enable_if_t<
  18435. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  18436. const_reference at(KeyType && key) const
  18437. {
  18438. // at only works for objects
  18439. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  18440. {
  18441. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  18442. }
  18443. auto it = m_value.object->find(std::forward<KeyType>(key));
  18444. if (it == m_value.object->end())
  18445. {
  18446. JSON_THROW(out_of_range::create(403, detail::concat("key '", string_t(std::forward<KeyType>(key)), "' not found"), this));
  18447. }
  18448. return it->second;
  18449. }
  18450. /// @brief access specified array element
  18451. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  18452. reference operator[](size_type idx)
  18453. {
  18454. // implicitly convert null value to an empty array
  18455. if (is_null())
  18456. {
  18457. m_type = value_t::array;
  18458. m_value.array = create<array_t>();
  18459. assert_invariant();
  18460. }
  18461. // operator[] only works for arrays
  18462. if (JSON_HEDLEY_LIKELY(is_array()))
  18463. {
  18464. // fill up array with null values if given idx is outside range
  18465. if (idx >= m_value.array->size())
  18466. {
  18467. #if JSON_DIAGNOSTICS
  18468. // remember array size & capacity before resizing
  18469. const auto old_size = m_value.array->size();
  18470. const auto old_capacity = m_value.array->capacity();
  18471. #endif
  18472. m_value.array->resize(idx + 1);
  18473. #if JSON_DIAGNOSTICS
  18474. if (JSON_HEDLEY_UNLIKELY(m_value.array->capacity() != old_capacity))
  18475. {
  18476. // capacity has changed: update all parents
  18477. set_parents();
  18478. }
  18479. else
  18480. {
  18481. // set parent for values added above
  18482. set_parents(begin() + static_cast<typename iterator::difference_type>(old_size), static_cast<typename iterator::difference_type>(idx + 1 - old_size));
  18483. }
  18484. #endif
  18485. assert_invariant();
  18486. }
  18487. return m_value.array->operator[](idx);
  18488. }
  18489. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a numeric argument with ", type_name()), this));
  18490. }
  18491. /// @brief access specified array element
  18492. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  18493. const_reference operator[](size_type idx) const
  18494. {
  18495. // const operator[] only works for arrays
  18496. if (JSON_HEDLEY_LIKELY(is_array()))
  18497. {
  18498. return m_value.array->operator[](idx);
  18499. }
  18500. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a numeric argument with ", type_name()), this));
  18501. }
  18502. /// @brief access specified object element
  18503. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  18504. reference operator[](typename object_t::key_type key)
  18505. {
  18506. // implicitly convert null value to an empty object
  18507. if (is_null())
  18508. {
  18509. m_type = value_t::object;
  18510. m_value.object = create<object_t>();
  18511. assert_invariant();
  18512. }
  18513. // operator[] only works for objects
  18514. if (JSON_HEDLEY_LIKELY(is_object()))
  18515. {
  18516. auto result = m_value.object->emplace(std::move(key), nullptr);
  18517. return set_parent(result.first->second);
  18518. }
  18519. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  18520. }
  18521. /// @brief access specified object element
  18522. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  18523. const_reference operator[](const typename object_t::key_type& key) const
  18524. {
  18525. // const operator[] only works for objects
  18526. if (JSON_HEDLEY_LIKELY(is_object()))
  18527. {
  18528. auto it = m_value.object->find(key);
  18529. JSON_ASSERT(it != m_value.object->end());
  18530. return it->second;
  18531. }
  18532. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  18533. }
  18534. // these two functions resolve a (const) char * ambiguity affecting Clang and MSVC
  18535. // (they seemingly cannot be constrained to resolve the ambiguity)
  18536. template<typename T>
  18537. reference operator[](T* key)
  18538. {
  18539. return operator[](typename object_t::key_type(key));
  18540. }
  18541. template<typename T>
  18542. const_reference operator[](T* key) const
  18543. {
  18544. return operator[](typename object_t::key_type(key));
  18545. }
  18546. /// @brief access specified object element
  18547. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  18548. template<class KeyType, detail::enable_if_t<
  18549. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int > = 0 >
  18550. reference operator[](KeyType && key)
  18551. {
  18552. // implicitly convert null value to an empty object
  18553. if (is_null())
  18554. {
  18555. m_type = value_t::object;
  18556. m_value.object = create<object_t>();
  18557. assert_invariant();
  18558. }
  18559. // operator[] only works for objects
  18560. if (JSON_HEDLEY_LIKELY(is_object()))
  18561. {
  18562. auto result = m_value.object->emplace(std::forward<KeyType>(key), nullptr);
  18563. return set_parent(result.first->second);
  18564. }
  18565. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  18566. }
  18567. /// @brief access specified object element
  18568. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  18569. template<class KeyType, detail::enable_if_t<
  18570. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int > = 0 >
  18571. const_reference operator[](KeyType && key) const
  18572. {
  18573. // const operator[] only works for objects
  18574. if (JSON_HEDLEY_LIKELY(is_object()))
  18575. {
  18576. auto it = m_value.object->find(std::forward<KeyType>(key));
  18577. JSON_ASSERT(it != m_value.object->end());
  18578. return it->second;
  18579. }
  18580. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  18581. }
  18582. private:
  18583. template<typename KeyType>
  18584. using is_comparable_with_object_key = detail::is_comparable <
  18585. object_comparator_t, const typename object_t::key_type&, KeyType >;
  18586. template<typename ValueType>
  18587. using value_return_type = std::conditional <
  18588. detail::is_c_string_uncvref<ValueType>::value,
  18589. string_t, typename std::decay<ValueType>::type >;
  18590. public:
  18591. /// @brief access specified object element with default value
  18592. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18593. template < class ValueType, detail::enable_if_t <
  18594. !detail::is_transparent<object_comparator_t>::value
  18595. && detail::is_getable<basic_json_t, ValueType>::value
  18596. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18597. ValueType value(const typename object_t::key_type& key, const ValueType& default_value) const
  18598. {
  18599. // value only works for objects
  18600. if (JSON_HEDLEY_LIKELY(is_object()))
  18601. {
  18602. // if key is found, return value and given default value otherwise
  18603. const auto it = find(key);
  18604. if (it != end())
  18605. {
  18606. return it->template get<ValueType>();
  18607. }
  18608. return default_value;
  18609. }
  18610. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  18611. }
  18612. /// @brief access specified object element with default value
  18613. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18614. template < class ValueType, class ReturnType = typename value_return_type<ValueType>::type,
  18615. detail::enable_if_t <
  18616. !detail::is_transparent<object_comparator_t>::value
  18617. && detail::is_getable<basic_json_t, ReturnType>::value
  18618. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18619. ReturnType value(const typename object_t::key_type& key, ValueType && default_value) const
  18620. {
  18621. // value only works for objects
  18622. if (JSON_HEDLEY_LIKELY(is_object()))
  18623. {
  18624. // if key is found, return value and given default value otherwise
  18625. const auto it = find(key);
  18626. if (it != end())
  18627. {
  18628. return it->template get<ReturnType>();
  18629. }
  18630. return std::forward<ValueType>(default_value);
  18631. }
  18632. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  18633. }
  18634. /// @brief access specified object element with default value
  18635. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18636. template < class ValueType, class KeyType, detail::enable_if_t <
  18637. detail::is_transparent<object_comparator_t>::value
  18638. && !detail::is_json_pointer<KeyType>::value
  18639. && is_comparable_with_object_key<KeyType>::value
  18640. && detail::is_getable<basic_json_t, ValueType>::value
  18641. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18642. ValueType value(KeyType && key, const ValueType& default_value) const
  18643. {
  18644. // value only works for objects
  18645. if (JSON_HEDLEY_LIKELY(is_object()))
  18646. {
  18647. // if key is found, return value and given default value otherwise
  18648. const auto it = find(std::forward<KeyType>(key));
  18649. if (it != end())
  18650. {
  18651. return it->template get<ValueType>();
  18652. }
  18653. return default_value;
  18654. }
  18655. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  18656. }
  18657. /// @brief access specified object element via JSON Pointer with default value
  18658. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18659. template < class ValueType, class KeyType, class ReturnType = typename value_return_type<ValueType>::type,
  18660. detail::enable_if_t <
  18661. detail::is_transparent<object_comparator_t>::value
  18662. && !detail::is_json_pointer<KeyType>::value
  18663. && is_comparable_with_object_key<KeyType>::value
  18664. && detail::is_getable<basic_json_t, ReturnType>::value
  18665. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18666. ReturnType value(KeyType && key, ValueType && default_value) const
  18667. {
  18668. // value only works for objects
  18669. if (JSON_HEDLEY_LIKELY(is_object()))
  18670. {
  18671. // if key is found, return value and given default value otherwise
  18672. const auto it = find(std::forward<KeyType>(key));
  18673. if (it != end())
  18674. {
  18675. return it->template get<ReturnType>();
  18676. }
  18677. return std::forward<ValueType>(default_value);
  18678. }
  18679. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  18680. }
  18681. /// @brief access specified object element via JSON Pointer with default value
  18682. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18683. template < class ValueType, detail::enable_if_t <
  18684. detail::is_getable<basic_json_t, ValueType>::value
  18685. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18686. ValueType value(const json_pointer& ptr, const ValueType& default_value) const
  18687. {
  18688. // value only works for objects
  18689. if (JSON_HEDLEY_LIKELY(is_object()))
  18690. {
  18691. // if pointer resolves a value, return it or use default value
  18692. JSON_TRY
  18693. {
  18694. return ptr.get_checked(this).template get<ValueType>();
  18695. }
  18696. JSON_INTERNAL_CATCH (out_of_range&)
  18697. {
  18698. return default_value;
  18699. }
  18700. }
  18701. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  18702. }
  18703. /// @brief access specified object element via JSON Pointer with default value
  18704. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18705. template < class ValueType, class ReturnType = typename value_return_type<ValueType>::type,
  18706. detail::enable_if_t <
  18707. detail::is_getable<basic_json_t, ReturnType>::value
  18708. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18709. ReturnType value(const json_pointer& ptr, ValueType && default_value) const
  18710. {
  18711. // value only works for objects
  18712. if (JSON_HEDLEY_LIKELY(is_object()))
  18713. {
  18714. // if pointer resolves a value, return it or use default value
  18715. JSON_TRY
  18716. {
  18717. return ptr.get_checked(this).template get<ReturnType>();
  18718. }
  18719. JSON_INTERNAL_CATCH (out_of_range&)
  18720. {
  18721. return std::forward<ValueType>(default_value);
  18722. }
  18723. }
  18724. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  18725. }
  18726. template < class ValueType, class BasicJsonType, detail::enable_if_t <
  18727. detail::is_basic_json<BasicJsonType>::value
  18728. && detail::is_getable<basic_json_t, ValueType>::value
  18729. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18730. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  18731. ValueType value(const ::nlohmann::json_pointer<BasicJsonType>& ptr, const ValueType& default_value) const
  18732. {
  18733. return value(ptr.convert(), default_value);
  18734. }
  18735. template < class ValueType, class BasicJsonType, class ReturnType = typename value_return_type<ValueType>::type,
  18736. detail::enable_if_t <
  18737. detail::is_basic_json<BasicJsonType>::value
  18738. && detail::is_getable<basic_json_t, ReturnType>::value
  18739. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  18740. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  18741. ReturnType value(const ::nlohmann::json_pointer<BasicJsonType>& ptr, ValueType && default_value) const
  18742. {
  18743. return value(ptr.convert(), std::forward<ValueType>(default_value));
  18744. }
  18745. /// @brief access the first element
  18746. /// @sa https://json.nlohmann.me/api/basic_json/front/
  18747. reference front()
  18748. {
  18749. return *begin();
  18750. }
  18751. /// @brief access the first element
  18752. /// @sa https://json.nlohmann.me/api/basic_json/front/
  18753. const_reference front() const
  18754. {
  18755. return *cbegin();
  18756. }
  18757. /// @brief access the last element
  18758. /// @sa https://json.nlohmann.me/api/basic_json/back/
  18759. reference back()
  18760. {
  18761. auto tmp = end();
  18762. --tmp;
  18763. return *tmp;
  18764. }
  18765. /// @brief access the last element
  18766. /// @sa https://json.nlohmann.me/api/basic_json/back/
  18767. const_reference back() const
  18768. {
  18769. auto tmp = cend();
  18770. --tmp;
  18771. return *tmp;
  18772. }
  18773. /// @brief remove element given an iterator
  18774. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  18775. template < class IteratorType, detail::enable_if_t <
  18776. std::is_same<IteratorType, typename basic_json_t::iterator>::value ||
  18777. std::is_same<IteratorType, typename basic_json_t::const_iterator>::value, int > = 0 >
  18778. IteratorType erase(IteratorType pos)
  18779. {
  18780. // make sure iterator fits the current value
  18781. if (JSON_HEDLEY_UNLIKELY(this != pos.m_object))
  18782. {
  18783. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  18784. }
  18785. IteratorType result = end();
  18786. switch (m_type)
  18787. {
  18788. case value_t::boolean:
  18789. case value_t::number_float:
  18790. case value_t::number_integer:
  18791. case value_t::number_unsigned:
  18792. case value_t::string:
  18793. case value_t::binary:
  18794. {
  18795. if (JSON_HEDLEY_UNLIKELY(!pos.m_it.primitive_iterator.is_begin()))
  18796. {
  18797. JSON_THROW(invalid_iterator::create(205, "iterator out of range", this));
  18798. }
  18799. if (is_string())
  18800. {
  18801. AllocatorType<string_t> alloc;
  18802. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.string);
  18803. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.string, 1);
  18804. m_value.string = nullptr;
  18805. }
  18806. else if (is_binary())
  18807. {
  18808. AllocatorType<binary_t> alloc;
  18809. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.binary);
  18810. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.binary, 1);
  18811. m_value.binary = nullptr;
  18812. }
  18813. m_type = value_t::null;
  18814. assert_invariant();
  18815. break;
  18816. }
  18817. case value_t::object:
  18818. {
  18819. result.m_it.object_iterator = m_value.object->erase(pos.m_it.object_iterator);
  18820. break;
  18821. }
  18822. case value_t::array:
  18823. {
  18824. result.m_it.array_iterator = m_value.array->erase(pos.m_it.array_iterator);
  18825. break;
  18826. }
  18827. case value_t::null:
  18828. case value_t::discarded:
  18829. default:
  18830. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  18831. }
  18832. return result;
  18833. }
  18834. /// @brief remove elements given an iterator range
  18835. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  18836. template < class IteratorType, detail::enable_if_t <
  18837. std::is_same<IteratorType, typename basic_json_t::iterator>::value ||
  18838. std::is_same<IteratorType, typename basic_json_t::const_iterator>::value, int > = 0 >
  18839. IteratorType erase(IteratorType first, IteratorType last)
  18840. {
  18841. // make sure iterator fits the current value
  18842. if (JSON_HEDLEY_UNLIKELY(this != first.m_object || this != last.m_object))
  18843. {
  18844. JSON_THROW(invalid_iterator::create(203, "iterators do not fit current value", this));
  18845. }
  18846. IteratorType result = end();
  18847. switch (m_type)
  18848. {
  18849. case value_t::boolean:
  18850. case value_t::number_float:
  18851. case value_t::number_integer:
  18852. case value_t::number_unsigned:
  18853. case value_t::string:
  18854. case value_t::binary:
  18855. {
  18856. if (JSON_HEDLEY_LIKELY(!first.m_it.primitive_iterator.is_begin()
  18857. || !last.m_it.primitive_iterator.is_end()))
  18858. {
  18859. JSON_THROW(invalid_iterator::create(204, "iterators out of range", this));
  18860. }
  18861. if (is_string())
  18862. {
  18863. AllocatorType<string_t> alloc;
  18864. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.string);
  18865. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.string, 1);
  18866. m_value.string = nullptr;
  18867. }
  18868. else if (is_binary())
  18869. {
  18870. AllocatorType<binary_t> alloc;
  18871. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.binary);
  18872. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.binary, 1);
  18873. m_value.binary = nullptr;
  18874. }
  18875. m_type = value_t::null;
  18876. assert_invariant();
  18877. break;
  18878. }
  18879. case value_t::object:
  18880. {
  18881. result.m_it.object_iterator = m_value.object->erase(first.m_it.object_iterator,
  18882. last.m_it.object_iterator);
  18883. break;
  18884. }
  18885. case value_t::array:
  18886. {
  18887. result.m_it.array_iterator = m_value.array->erase(first.m_it.array_iterator,
  18888. last.m_it.array_iterator);
  18889. break;
  18890. }
  18891. case value_t::null:
  18892. case value_t::discarded:
  18893. default:
  18894. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  18895. }
  18896. return result;
  18897. }
  18898. private:
  18899. template < typename KeyType, detail::enable_if_t <
  18900. detail::has_erase_with_key_type<basic_json_t, KeyType>::value, int > = 0 >
  18901. size_type erase_internal(KeyType && key)
  18902. {
  18903. // this erase only works for objects
  18904. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  18905. {
  18906. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  18907. }
  18908. return m_value.object->erase(std::forward<KeyType>(key));
  18909. }
  18910. template < typename KeyType, detail::enable_if_t <
  18911. !detail::has_erase_with_key_type<basic_json_t, KeyType>::value, int > = 0 >
  18912. size_type erase_internal(KeyType && key)
  18913. {
  18914. // this erase only works for objects
  18915. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  18916. {
  18917. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  18918. }
  18919. const auto it = m_value.object->find(std::forward<KeyType>(key));
  18920. if (it != m_value.object->end())
  18921. {
  18922. m_value.object->erase(it);
  18923. return 1;
  18924. }
  18925. return 0;
  18926. }
  18927. public:
  18928. /// @brief remove element from a JSON object given a key
  18929. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  18930. size_type erase(const typename object_t::key_type& key)
  18931. {
  18932. // the indirection via erase_internal() is added to avoid making this
  18933. // function a template and thus de-rank it during overload resolution
  18934. return erase_internal(key);
  18935. }
  18936. /// @brief remove element from a JSON object given a key
  18937. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  18938. template<class KeyType, detail::enable_if_t<
  18939. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  18940. size_type erase(KeyType && key)
  18941. {
  18942. return erase_internal(std::forward<KeyType>(key));
  18943. }
  18944. /// @brief remove element from a JSON array given an index
  18945. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  18946. void erase(const size_type idx)
  18947. {
  18948. // this erase only works for arrays
  18949. if (JSON_HEDLEY_LIKELY(is_array()))
  18950. {
  18951. if (JSON_HEDLEY_UNLIKELY(idx >= size()))
  18952. {
  18953. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
  18954. }
  18955. m_value.array->erase(m_value.array->begin() + static_cast<difference_type>(idx));
  18956. }
  18957. else
  18958. {
  18959. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  18960. }
  18961. }
  18962. /// @}
  18963. ////////////
  18964. // lookup //
  18965. ////////////
  18966. /// @name lookup
  18967. /// @{
  18968. /// @brief find an element in a JSON object
  18969. /// @sa https://json.nlohmann.me/api/basic_json/find/
  18970. iterator find(const typename object_t::key_type& key)
  18971. {
  18972. auto result = end();
  18973. if (is_object())
  18974. {
  18975. result.m_it.object_iterator = m_value.object->find(key);
  18976. }
  18977. return result;
  18978. }
  18979. /// @brief find an element in a JSON object
  18980. /// @sa https://json.nlohmann.me/api/basic_json/find/
  18981. const_iterator find(const typename object_t::key_type& key) const
  18982. {
  18983. auto result = cend();
  18984. if (is_object())
  18985. {
  18986. result.m_it.object_iterator = m_value.object->find(key);
  18987. }
  18988. return result;
  18989. }
  18990. /// @brief find an element in a JSON object
  18991. /// @sa https://json.nlohmann.me/api/basic_json/find/
  18992. template<class KeyType, detail::enable_if_t<
  18993. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  18994. iterator find(KeyType && key)
  18995. {
  18996. auto result = end();
  18997. if (is_object())
  18998. {
  18999. result.m_it.object_iterator = m_value.object->find(std::forward<KeyType>(key));
  19000. }
  19001. return result;
  19002. }
  19003. /// @brief find an element in a JSON object
  19004. /// @sa https://json.nlohmann.me/api/basic_json/find/
  19005. template<class KeyType, detail::enable_if_t<
  19006. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  19007. const_iterator find(KeyType && key) const
  19008. {
  19009. auto result = cend();
  19010. if (is_object())
  19011. {
  19012. result.m_it.object_iterator = m_value.object->find(std::forward<KeyType>(key));
  19013. }
  19014. return result;
  19015. }
  19016. /// @brief returns the number of occurrences of a key in a JSON object
  19017. /// @sa https://json.nlohmann.me/api/basic_json/count/
  19018. size_type count(const typename object_t::key_type& key) const
  19019. {
  19020. // return 0 for all nonobject types
  19021. return is_object() ? m_value.object->count(key) : 0;
  19022. }
  19023. /// @brief returns the number of occurrences of a key in a JSON object
  19024. /// @sa https://json.nlohmann.me/api/basic_json/count/
  19025. template<class KeyType, detail::enable_if_t<
  19026. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  19027. size_type count(KeyType && key) const
  19028. {
  19029. // return 0 for all nonobject types
  19030. return is_object() ? m_value.object->count(std::forward<KeyType>(key)) : 0;
  19031. }
  19032. /// @brief check the existence of an element in a JSON object
  19033. /// @sa https://json.nlohmann.me/api/basic_json/contains/
  19034. bool contains(const typename object_t::key_type& key) const
  19035. {
  19036. return is_object() && m_value.object->find(key) != m_value.object->end();
  19037. }
  19038. /// @brief check the existence of an element in a JSON object
  19039. /// @sa https://json.nlohmann.me/api/basic_json/contains/
  19040. template<class KeyType, detail::enable_if_t<
  19041. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  19042. bool contains(KeyType && key) const
  19043. {
  19044. return is_object() && m_value.object->find(std::forward<KeyType>(key)) != m_value.object->end();
  19045. }
  19046. /// @brief check the existence of an element in a JSON object given a JSON pointer
  19047. /// @sa https://json.nlohmann.me/api/basic_json/contains/
  19048. bool contains(const json_pointer& ptr) const
  19049. {
  19050. return ptr.contains(this);
  19051. }
  19052. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  19053. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  19054. bool contains(const typename ::nlohmann::json_pointer<BasicJsonType>& ptr) const
  19055. {
  19056. return ptr.contains(this);
  19057. }
  19058. /// @}
  19059. ///////////////
  19060. // iterators //
  19061. ///////////////
  19062. /// @name iterators
  19063. /// @{
  19064. /// @brief returns an iterator to the first element
  19065. /// @sa https://json.nlohmann.me/api/basic_json/begin/
  19066. iterator begin() noexcept
  19067. {
  19068. iterator result(this);
  19069. result.set_begin();
  19070. return result;
  19071. }
  19072. /// @brief returns an iterator to the first element
  19073. /// @sa https://json.nlohmann.me/api/basic_json/begin/
  19074. const_iterator begin() const noexcept
  19075. {
  19076. return cbegin();
  19077. }
  19078. /// @brief returns a const iterator to the first element
  19079. /// @sa https://json.nlohmann.me/api/basic_json/cbegin/
  19080. const_iterator cbegin() const noexcept
  19081. {
  19082. const_iterator result(this);
  19083. result.set_begin();
  19084. return result;
  19085. }
  19086. /// @brief returns an iterator to one past the last element
  19087. /// @sa https://json.nlohmann.me/api/basic_json/end/
  19088. iterator end() noexcept
  19089. {
  19090. iterator result(this);
  19091. result.set_end();
  19092. return result;
  19093. }
  19094. /// @brief returns an iterator to one past the last element
  19095. /// @sa https://json.nlohmann.me/api/basic_json/end/
  19096. const_iterator end() const noexcept
  19097. {
  19098. return cend();
  19099. }
  19100. /// @brief returns an iterator to one past the last element
  19101. /// @sa https://json.nlohmann.me/api/basic_json/cend/
  19102. const_iterator cend() const noexcept
  19103. {
  19104. const_iterator result(this);
  19105. result.set_end();
  19106. return result;
  19107. }
  19108. /// @brief returns an iterator to the reverse-beginning
  19109. /// @sa https://json.nlohmann.me/api/basic_json/rbegin/
  19110. reverse_iterator rbegin() noexcept
  19111. {
  19112. return reverse_iterator(end());
  19113. }
  19114. /// @brief returns an iterator to the reverse-beginning
  19115. /// @sa https://json.nlohmann.me/api/basic_json/rbegin/
  19116. const_reverse_iterator rbegin() const noexcept
  19117. {
  19118. return crbegin();
  19119. }
  19120. /// @brief returns an iterator to the reverse-end
  19121. /// @sa https://json.nlohmann.me/api/basic_json/rend/
  19122. reverse_iterator rend() noexcept
  19123. {
  19124. return reverse_iterator(begin());
  19125. }
  19126. /// @brief returns an iterator to the reverse-end
  19127. /// @sa https://json.nlohmann.me/api/basic_json/rend/
  19128. const_reverse_iterator rend() const noexcept
  19129. {
  19130. return crend();
  19131. }
  19132. /// @brief returns a const reverse iterator to the last element
  19133. /// @sa https://json.nlohmann.me/api/basic_json/crbegin/
  19134. const_reverse_iterator crbegin() const noexcept
  19135. {
  19136. return const_reverse_iterator(cend());
  19137. }
  19138. /// @brief returns a const reverse iterator to one before the first
  19139. /// @sa https://json.nlohmann.me/api/basic_json/crend/
  19140. const_reverse_iterator crend() const noexcept
  19141. {
  19142. return const_reverse_iterator(cbegin());
  19143. }
  19144. public:
  19145. /// @brief wrapper to access iterator member functions in range-based for
  19146. /// @sa https://json.nlohmann.me/api/basic_json/items/
  19147. /// @deprecated This function is deprecated since 3.1.0 and will be removed in
  19148. /// version 4.0.0 of the library. Please use @ref items() instead;
  19149. /// that is, replace `json::iterator_wrapper(j)` with `j.items()`.
  19150. JSON_HEDLEY_DEPRECATED_FOR(3.1.0, items())
  19151. static iteration_proxy<iterator> iterator_wrapper(reference ref) noexcept
  19152. {
  19153. return ref.items();
  19154. }
  19155. /// @brief wrapper to access iterator member functions in range-based for
  19156. /// @sa https://json.nlohmann.me/api/basic_json/items/
  19157. /// @deprecated This function is deprecated since 3.1.0 and will be removed in
  19158. /// version 4.0.0 of the library. Please use @ref items() instead;
  19159. /// that is, replace `json::iterator_wrapper(j)` with `j.items()`.
  19160. JSON_HEDLEY_DEPRECATED_FOR(3.1.0, items())
  19161. static iteration_proxy<const_iterator> iterator_wrapper(const_reference ref) noexcept
  19162. {
  19163. return ref.items();
  19164. }
  19165. /// @brief helper to access iterator member functions in range-based for
  19166. /// @sa https://json.nlohmann.me/api/basic_json/items/
  19167. iteration_proxy<iterator> items() noexcept
  19168. {
  19169. return iteration_proxy<iterator>(*this);
  19170. }
  19171. /// @brief helper to access iterator member functions in range-based for
  19172. /// @sa https://json.nlohmann.me/api/basic_json/items/
  19173. iteration_proxy<const_iterator> items() const noexcept
  19174. {
  19175. return iteration_proxy<const_iterator>(*this);
  19176. }
  19177. /// @}
  19178. //////////////
  19179. // capacity //
  19180. //////////////
  19181. /// @name capacity
  19182. /// @{
  19183. /// @brief checks whether the container is empty.
  19184. /// @sa https://json.nlohmann.me/api/basic_json/empty/
  19185. bool empty() const noexcept
  19186. {
  19187. switch (m_type)
  19188. {
  19189. case value_t::null:
  19190. {
  19191. // null values are empty
  19192. return true;
  19193. }
  19194. case value_t::array:
  19195. {
  19196. // delegate call to array_t::empty()
  19197. return m_value.array->empty();
  19198. }
  19199. case value_t::object:
  19200. {
  19201. // delegate call to object_t::empty()
  19202. return m_value.object->empty();
  19203. }
  19204. case value_t::string:
  19205. case value_t::boolean:
  19206. case value_t::number_integer:
  19207. case value_t::number_unsigned:
  19208. case value_t::number_float:
  19209. case value_t::binary:
  19210. case value_t::discarded:
  19211. default:
  19212. {
  19213. // all other types are nonempty
  19214. return false;
  19215. }
  19216. }
  19217. }
  19218. /// @brief returns the number of elements
  19219. /// @sa https://json.nlohmann.me/api/basic_json/size/
  19220. size_type size() const noexcept
  19221. {
  19222. switch (m_type)
  19223. {
  19224. case value_t::null:
  19225. {
  19226. // null values are empty
  19227. return 0;
  19228. }
  19229. case value_t::array:
  19230. {
  19231. // delegate call to array_t::size()
  19232. return m_value.array->size();
  19233. }
  19234. case value_t::object:
  19235. {
  19236. // delegate call to object_t::size()
  19237. return m_value.object->size();
  19238. }
  19239. case value_t::string:
  19240. case value_t::boolean:
  19241. case value_t::number_integer:
  19242. case value_t::number_unsigned:
  19243. case value_t::number_float:
  19244. case value_t::binary:
  19245. case value_t::discarded:
  19246. default:
  19247. {
  19248. // all other types have size 1
  19249. return 1;
  19250. }
  19251. }
  19252. }
  19253. /// @brief returns the maximum possible number of elements
  19254. /// @sa https://json.nlohmann.me/api/basic_json/max_size/
  19255. size_type max_size() const noexcept
  19256. {
  19257. switch (m_type)
  19258. {
  19259. case value_t::array:
  19260. {
  19261. // delegate call to array_t::max_size()
  19262. return m_value.array->max_size();
  19263. }
  19264. case value_t::object:
  19265. {
  19266. // delegate call to object_t::max_size()
  19267. return m_value.object->max_size();
  19268. }
  19269. case value_t::null:
  19270. case value_t::string:
  19271. case value_t::boolean:
  19272. case value_t::number_integer:
  19273. case value_t::number_unsigned:
  19274. case value_t::number_float:
  19275. case value_t::binary:
  19276. case value_t::discarded:
  19277. default:
  19278. {
  19279. // all other types have max_size() == size()
  19280. return size();
  19281. }
  19282. }
  19283. }
  19284. /// @}
  19285. ///////////////
  19286. // modifiers //
  19287. ///////////////
  19288. /// @name modifiers
  19289. /// @{
  19290. /// @brief clears the contents
  19291. /// @sa https://json.nlohmann.me/api/basic_json/clear/
  19292. void clear() noexcept
  19293. {
  19294. switch (m_type)
  19295. {
  19296. case value_t::number_integer:
  19297. {
  19298. m_value.number_integer = 0;
  19299. break;
  19300. }
  19301. case value_t::number_unsigned:
  19302. {
  19303. m_value.number_unsigned = 0;
  19304. break;
  19305. }
  19306. case value_t::number_float:
  19307. {
  19308. m_value.number_float = 0.0;
  19309. break;
  19310. }
  19311. case value_t::boolean:
  19312. {
  19313. m_value.boolean = false;
  19314. break;
  19315. }
  19316. case value_t::string:
  19317. {
  19318. m_value.string->clear();
  19319. break;
  19320. }
  19321. case value_t::binary:
  19322. {
  19323. m_value.binary->clear();
  19324. break;
  19325. }
  19326. case value_t::array:
  19327. {
  19328. m_value.array->clear();
  19329. break;
  19330. }
  19331. case value_t::object:
  19332. {
  19333. m_value.object->clear();
  19334. break;
  19335. }
  19336. case value_t::null:
  19337. case value_t::discarded:
  19338. default:
  19339. break;
  19340. }
  19341. }
  19342. /// @brief add an object to an array
  19343. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  19344. void push_back(basic_json&& val)
  19345. {
  19346. // push_back only works for null objects or arrays
  19347. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
  19348. {
  19349. JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
  19350. }
  19351. // transform null object into an array
  19352. if (is_null())
  19353. {
  19354. m_type = value_t::array;
  19355. m_value = value_t::array;
  19356. assert_invariant();
  19357. }
  19358. // add element to array (move semantics)
  19359. const auto old_capacity = m_value.array->capacity();
  19360. m_value.array->push_back(std::move(val));
  19361. set_parent(m_value.array->back(), old_capacity);
  19362. // if val is moved from, basic_json move constructor marks it null, so we do not call the destructor
  19363. }
  19364. /// @brief add an object to an array
  19365. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  19366. reference operator+=(basic_json&& val)
  19367. {
  19368. push_back(std::move(val));
  19369. return *this;
  19370. }
  19371. /// @brief add an object to an array
  19372. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  19373. void push_back(const basic_json& val)
  19374. {
  19375. // push_back only works for null objects or arrays
  19376. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
  19377. {
  19378. JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
  19379. }
  19380. // transform null object into an array
  19381. if (is_null())
  19382. {
  19383. m_type = value_t::array;
  19384. m_value = value_t::array;
  19385. assert_invariant();
  19386. }
  19387. // add element to array
  19388. const auto old_capacity = m_value.array->capacity();
  19389. m_value.array->push_back(val);
  19390. set_parent(m_value.array->back(), old_capacity);
  19391. }
  19392. /// @brief add an object to an array
  19393. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  19394. reference operator+=(const basic_json& val)
  19395. {
  19396. push_back(val);
  19397. return *this;
  19398. }
  19399. /// @brief add an object to an object
  19400. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  19401. void push_back(const typename object_t::value_type& val)
  19402. {
  19403. // push_back only works for null objects or objects
  19404. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object())))
  19405. {
  19406. JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
  19407. }
  19408. // transform null object into an object
  19409. if (is_null())
  19410. {
  19411. m_type = value_t::object;
  19412. m_value = value_t::object;
  19413. assert_invariant();
  19414. }
  19415. // add element to object
  19416. auto res = m_value.object->insert(val);
  19417. set_parent(res.first->second);
  19418. }
  19419. /// @brief add an object to an object
  19420. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  19421. reference operator+=(const typename object_t::value_type& val)
  19422. {
  19423. push_back(val);
  19424. return *this;
  19425. }
  19426. /// @brief add an object to an object
  19427. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  19428. void push_back(initializer_list_t init)
  19429. {
  19430. if (is_object() && init.size() == 2 && (*init.begin())->is_string())
  19431. {
  19432. basic_json&& key = init.begin()->moved_or_copied();
  19433. push_back(typename object_t::value_type(
  19434. std::move(key.get_ref<string_t&>()), (init.begin() + 1)->moved_or_copied()));
  19435. }
  19436. else
  19437. {
  19438. push_back(basic_json(init));
  19439. }
  19440. }
  19441. /// @brief add an object to an object
  19442. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  19443. reference operator+=(initializer_list_t init)
  19444. {
  19445. push_back(init);
  19446. return *this;
  19447. }
  19448. /// @brief add an object to an array
  19449. /// @sa https://json.nlohmann.me/api/basic_json/emplace_back/
  19450. template<class... Args>
  19451. reference emplace_back(Args&& ... args)
  19452. {
  19453. // emplace_back only works for null objects or arrays
  19454. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
  19455. {
  19456. JSON_THROW(type_error::create(311, detail::concat("cannot use emplace_back() with ", type_name()), this));
  19457. }
  19458. // transform null object into an array
  19459. if (is_null())
  19460. {
  19461. m_type = value_t::array;
  19462. m_value = value_t::array;
  19463. assert_invariant();
  19464. }
  19465. // add element to array (perfect forwarding)
  19466. const auto old_capacity = m_value.array->capacity();
  19467. m_value.array->emplace_back(std::forward<Args>(args)...);
  19468. return set_parent(m_value.array->back(), old_capacity);
  19469. }
  19470. /// @brief add an object to an object if key does not exist
  19471. /// @sa https://json.nlohmann.me/api/basic_json/emplace/
  19472. template<class... Args>
  19473. std::pair<iterator, bool> emplace(Args&& ... args)
  19474. {
  19475. // emplace only works for null objects or arrays
  19476. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object())))
  19477. {
  19478. JSON_THROW(type_error::create(311, detail::concat("cannot use emplace() with ", type_name()), this));
  19479. }
  19480. // transform null object into an object
  19481. if (is_null())
  19482. {
  19483. m_type = value_t::object;
  19484. m_value = value_t::object;
  19485. assert_invariant();
  19486. }
  19487. // add element to array (perfect forwarding)
  19488. auto res = m_value.object->emplace(std::forward<Args>(args)...);
  19489. set_parent(res.first->second);
  19490. // create result iterator and set iterator to the result of emplace
  19491. auto it = begin();
  19492. it.m_it.object_iterator = res.first;
  19493. // return pair of iterator and boolean
  19494. return {it, res.second};
  19495. }
  19496. /// Helper for insertion of an iterator
  19497. /// @note: This uses std::distance to support GCC 4.8,
  19498. /// see https://github.com/nlohmann/json/pull/1257
  19499. template<typename... Args>
  19500. iterator insert_iterator(const_iterator pos, Args&& ... args)
  19501. {
  19502. iterator result(this);
  19503. JSON_ASSERT(m_value.array != nullptr);
  19504. auto insert_pos = std::distance(m_value.array->begin(), pos.m_it.array_iterator);
  19505. m_value.array->insert(pos.m_it.array_iterator, std::forward<Args>(args)...);
  19506. result.m_it.array_iterator = m_value.array->begin() + insert_pos;
  19507. // This could have been written as:
  19508. // result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, cnt, val);
  19509. // but the return value of insert is missing in GCC 4.8, so it is written this way instead.
  19510. set_parents();
  19511. return result;
  19512. }
  19513. /// @brief inserts element into array
  19514. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  19515. iterator insert(const_iterator pos, const basic_json& val)
  19516. {
  19517. // insert only works for arrays
  19518. if (JSON_HEDLEY_LIKELY(is_array()))
  19519. {
  19520. // check if iterator pos fits to this JSON value
  19521. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  19522. {
  19523. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  19524. }
  19525. // insert to array and return iterator
  19526. return insert_iterator(pos, val);
  19527. }
  19528. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  19529. }
  19530. /// @brief inserts element into array
  19531. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  19532. iterator insert(const_iterator pos, basic_json&& val)
  19533. {
  19534. return insert(pos, val);
  19535. }
  19536. /// @brief inserts copies of element into array
  19537. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  19538. iterator insert(const_iterator pos, size_type cnt, const basic_json& val)
  19539. {
  19540. // insert only works for arrays
  19541. if (JSON_HEDLEY_LIKELY(is_array()))
  19542. {
  19543. // check if iterator pos fits to this JSON value
  19544. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  19545. {
  19546. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  19547. }
  19548. // insert to array and return iterator
  19549. return insert_iterator(pos, cnt, val);
  19550. }
  19551. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  19552. }
  19553. /// @brief inserts range of elements into array
  19554. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  19555. iterator insert(const_iterator pos, const_iterator first, const_iterator last)
  19556. {
  19557. // insert only works for arrays
  19558. if (JSON_HEDLEY_UNLIKELY(!is_array()))
  19559. {
  19560. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  19561. }
  19562. // check if iterator pos fits to this JSON value
  19563. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  19564. {
  19565. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  19566. }
  19567. // check if range iterators belong to the same JSON object
  19568. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  19569. {
  19570. JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
  19571. }
  19572. if (JSON_HEDLEY_UNLIKELY(first.m_object == this))
  19573. {
  19574. JSON_THROW(invalid_iterator::create(211, "passed iterators may not belong to container", this));
  19575. }
  19576. // insert to array and return iterator
  19577. return insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator);
  19578. }
  19579. /// @brief inserts elements from initializer list into array
  19580. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  19581. iterator insert(const_iterator pos, initializer_list_t ilist)
  19582. {
  19583. // insert only works for arrays
  19584. if (JSON_HEDLEY_UNLIKELY(!is_array()))
  19585. {
  19586. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  19587. }
  19588. // check if iterator pos fits to this JSON value
  19589. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  19590. {
  19591. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  19592. }
  19593. // insert to array and return iterator
  19594. return insert_iterator(pos, ilist.begin(), ilist.end());
  19595. }
  19596. /// @brief inserts range of elements into object
  19597. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  19598. void insert(const_iterator first, const_iterator last)
  19599. {
  19600. // insert only works for objects
  19601. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  19602. {
  19603. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  19604. }
  19605. // check if range iterators belong to the same JSON object
  19606. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  19607. {
  19608. JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
  19609. }
  19610. // passed iterators must belong to objects
  19611. if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()))
  19612. {
  19613. JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", this));
  19614. }
  19615. m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator);
  19616. }
  19617. /// @brief updates a JSON object from another object, overwriting existing keys
  19618. /// @sa https://json.nlohmann.me/api/basic_json/update/
  19619. void update(const_reference j, bool merge_objects = false)
  19620. {
  19621. update(j.begin(), j.end(), merge_objects);
  19622. }
  19623. /// @brief updates a JSON object from another object, overwriting existing keys
  19624. /// @sa https://json.nlohmann.me/api/basic_json/update/
  19625. void update(const_iterator first, const_iterator last, bool merge_objects = false)
  19626. {
  19627. // implicitly convert null value to an empty object
  19628. if (is_null())
  19629. {
  19630. m_type = value_t::object;
  19631. m_value.object = create<object_t>();
  19632. assert_invariant();
  19633. }
  19634. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  19635. {
  19636. JSON_THROW(type_error::create(312, detail::concat("cannot use update() with ", type_name()), this));
  19637. }
  19638. // check if range iterators belong to the same JSON object
  19639. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  19640. {
  19641. JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
  19642. }
  19643. // passed iterators must belong to objects
  19644. if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()))
  19645. {
  19646. JSON_THROW(type_error::create(312, detail::concat("cannot use update() with ", first.m_object->type_name()), first.m_object));
  19647. }
  19648. for (auto it = first; it != last; ++it)
  19649. {
  19650. if (merge_objects && it.value().is_object())
  19651. {
  19652. auto it2 = m_value.object->find(it.key());
  19653. if (it2 != m_value.object->end())
  19654. {
  19655. it2->second.update(it.value(), true);
  19656. continue;
  19657. }
  19658. }
  19659. m_value.object->operator[](it.key()) = it.value();
  19660. #if JSON_DIAGNOSTICS
  19661. m_value.object->operator[](it.key()).m_parent = this;
  19662. #endif
  19663. }
  19664. }
  19665. /// @brief exchanges the values
  19666. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19667. void swap(reference other) noexcept (
  19668. std::is_nothrow_move_constructible<value_t>::value&&
  19669. std::is_nothrow_move_assignable<value_t>::value&&
  19670. std::is_nothrow_move_constructible<json_value>::value&&
  19671. std::is_nothrow_move_assignable<json_value>::value
  19672. )
  19673. {
  19674. std::swap(m_type, other.m_type);
  19675. std::swap(m_value, other.m_value);
  19676. set_parents();
  19677. other.set_parents();
  19678. assert_invariant();
  19679. }
  19680. /// @brief exchanges the values
  19681. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19682. friend void swap(reference left, reference right) noexcept (
  19683. std::is_nothrow_move_constructible<value_t>::value&&
  19684. std::is_nothrow_move_assignable<value_t>::value&&
  19685. std::is_nothrow_move_constructible<json_value>::value&&
  19686. std::is_nothrow_move_assignable<json_value>::value
  19687. )
  19688. {
  19689. left.swap(right);
  19690. }
  19691. /// @brief exchanges the values
  19692. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19693. void swap(array_t& other) // NOLINT(bugprone-exception-escape)
  19694. {
  19695. // swap only works for arrays
  19696. if (JSON_HEDLEY_LIKELY(is_array()))
  19697. {
  19698. using std::swap;
  19699. swap(*(m_value.array), other);
  19700. }
  19701. else
  19702. {
  19703. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(array_t&) with ", type_name()), this));
  19704. }
  19705. }
  19706. /// @brief exchanges the values
  19707. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19708. void swap(object_t& other) // NOLINT(bugprone-exception-escape)
  19709. {
  19710. // swap only works for objects
  19711. if (JSON_HEDLEY_LIKELY(is_object()))
  19712. {
  19713. using std::swap;
  19714. swap(*(m_value.object), other);
  19715. }
  19716. else
  19717. {
  19718. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(object_t&) with ", type_name()), this));
  19719. }
  19720. }
  19721. /// @brief exchanges the values
  19722. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19723. void swap(string_t& other) // NOLINT(bugprone-exception-escape)
  19724. {
  19725. // swap only works for strings
  19726. if (JSON_HEDLEY_LIKELY(is_string()))
  19727. {
  19728. using std::swap;
  19729. swap(*(m_value.string), other);
  19730. }
  19731. else
  19732. {
  19733. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(string_t&) with ", type_name()), this));
  19734. }
  19735. }
  19736. /// @brief exchanges the values
  19737. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19738. void swap(binary_t& other) // NOLINT(bugprone-exception-escape)
  19739. {
  19740. // swap only works for strings
  19741. if (JSON_HEDLEY_LIKELY(is_binary()))
  19742. {
  19743. using std::swap;
  19744. swap(*(m_value.binary), other);
  19745. }
  19746. else
  19747. {
  19748. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(binary_t&) with ", type_name()), this));
  19749. }
  19750. }
  19751. /// @brief exchanges the values
  19752. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19753. void swap(typename binary_t::container_type& other) // NOLINT(bugprone-exception-escape)
  19754. {
  19755. // swap only works for strings
  19756. if (JSON_HEDLEY_LIKELY(is_binary()))
  19757. {
  19758. using std::swap;
  19759. swap(*(m_value.binary), other);
  19760. }
  19761. else
  19762. {
  19763. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(binary_t::container_type&) with ", type_name()), this));
  19764. }
  19765. }
  19766. /// @}
  19767. //////////////////////////////////////////
  19768. // lexicographical comparison operators //
  19769. //////////////////////////////////////////
  19770. /// @name lexicographical comparison operators
  19771. /// @{
  19772. // note parentheses around operands are necessary; see
  19773. // https://github.com/nlohmann/json/issues/1530
  19774. #define JSON_IMPLEMENT_OPERATOR(op, null_result, unordered_result, default_result) \
  19775. const auto lhs_type = lhs.type(); \
  19776. const auto rhs_type = rhs.type(); \
  19777. \
  19778. if (lhs_type == rhs_type) /* NOLINT(readability/braces) */ \
  19779. { \
  19780. switch (lhs_type) \
  19781. { \
  19782. case value_t::array: \
  19783. return (*lhs.m_value.array) op (*rhs.m_value.array); \
  19784. \
  19785. case value_t::object: \
  19786. return (*lhs.m_value.object) op (*rhs.m_value.object); \
  19787. \
  19788. case value_t::null: \
  19789. return (null_result); \
  19790. \
  19791. case value_t::string: \
  19792. return (*lhs.m_value.string) op (*rhs.m_value.string); \
  19793. \
  19794. case value_t::boolean: \
  19795. return (lhs.m_value.boolean) op (rhs.m_value.boolean); \
  19796. \
  19797. case value_t::number_integer: \
  19798. return (lhs.m_value.number_integer) op (rhs.m_value.number_integer); \
  19799. \
  19800. case value_t::number_unsigned: \
  19801. return (lhs.m_value.number_unsigned) op (rhs.m_value.number_unsigned); \
  19802. \
  19803. case value_t::number_float: \
  19804. return (lhs.m_value.number_float) op (rhs.m_value.number_float); \
  19805. \
  19806. case value_t::binary: \
  19807. return (*lhs.m_value.binary) op (*rhs.m_value.binary); \
  19808. \
  19809. case value_t::discarded: \
  19810. default: \
  19811. return (unordered_result); \
  19812. } \
  19813. } \
  19814. else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_float) \
  19815. { \
  19816. return static_cast<number_float_t>(lhs.m_value.number_integer) op rhs.m_value.number_float; \
  19817. } \
  19818. else if (lhs_type == value_t::number_float && rhs_type == value_t::number_integer) \
  19819. { \
  19820. return lhs.m_value.number_float op static_cast<number_float_t>(rhs.m_value.number_integer); \
  19821. } \
  19822. else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_float) \
  19823. { \
  19824. return static_cast<number_float_t>(lhs.m_value.number_unsigned) op rhs.m_value.number_float; \
  19825. } \
  19826. else if (lhs_type == value_t::number_float && rhs_type == value_t::number_unsigned) \
  19827. { \
  19828. return lhs.m_value.number_float op static_cast<number_float_t>(rhs.m_value.number_unsigned); \
  19829. } \
  19830. else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_integer) \
  19831. { \
  19832. return static_cast<number_integer_t>(lhs.m_value.number_unsigned) op rhs.m_value.number_integer; \
  19833. } \
  19834. else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_unsigned) \
  19835. { \
  19836. return lhs.m_value.number_integer op static_cast<number_integer_t>(rhs.m_value.number_unsigned); \
  19837. } \
  19838. else if(compares_unordered(lhs, rhs))\
  19839. {\
  19840. return (unordered_result);\
  19841. }\
  19842. \
  19843. return (default_result);
  19844. JSON_PRIVATE_UNLESS_TESTED:
  19845. // returns true if:
  19846. // - any operand is NaN and the other operand is of number type
  19847. // - any operand is discarded
  19848. // in legacy mode, discarded values are considered ordered if
  19849. // an operation is computed as an odd number of inverses of others
  19850. static bool compares_unordered(const_reference lhs, const_reference rhs, bool inverse = false) noexcept
  19851. {
  19852. if ((lhs.is_number_float() && std::isnan(lhs.m_value.number_float) && rhs.is_number())
  19853. || (rhs.is_number_float() && std::isnan(rhs.m_value.number_float) && lhs.is_number()))
  19854. {
  19855. return true;
  19856. }
  19857. #if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  19858. return (lhs.is_discarded() || rhs.is_discarded()) && !inverse;
  19859. #else
  19860. static_cast<void>(inverse);
  19861. return lhs.is_discarded() || rhs.is_discarded();
  19862. #endif
  19863. }
  19864. private:
  19865. bool compares_unordered(const_reference rhs, bool inverse = false) const noexcept
  19866. {
  19867. return compares_unordered(*this, rhs, inverse);
  19868. }
  19869. public:
  19870. #if JSON_HAS_THREE_WAY_COMPARISON
  19871. /// @brief comparison: equal
  19872. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  19873. bool operator==(const_reference rhs) const noexcept
  19874. {
  19875. #ifdef __GNUC__
  19876. #pragma GCC diagnostic push
  19877. #pragma GCC diagnostic ignored "-Wfloat-equal"
  19878. #endif
  19879. const_reference lhs = *this;
  19880. JSON_IMPLEMENT_OPERATOR( ==, true, false, false)
  19881. #ifdef __GNUC__
  19882. #pragma GCC diagnostic pop
  19883. #endif
  19884. }
  19885. /// @brief comparison: equal
  19886. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  19887. template<typename ScalarType>
  19888. requires std::is_scalar_v<ScalarType>
  19889. bool operator==(ScalarType rhs) const noexcept
  19890. {
  19891. return *this == basic_json(rhs);
  19892. }
  19893. /// @brief comparison: not equal
  19894. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  19895. bool operator!=(const_reference rhs) const noexcept
  19896. {
  19897. if (compares_unordered(rhs, true))
  19898. {
  19899. return false;
  19900. }
  19901. return !operator==(rhs);
  19902. }
  19903. /// @brief comparison: 3-way
  19904. /// @sa https://json.nlohmann.me/api/basic_json/operator_spaceship/
  19905. std::partial_ordering operator<=>(const_reference rhs) const noexcept // *NOPAD*
  19906. {
  19907. const_reference lhs = *this;
  19908. // default_result is used if we cannot compare values. In that case,
  19909. // we compare types.
  19910. JSON_IMPLEMENT_OPERATOR(<=>, // *NOPAD*
  19911. std::partial_ordering::equivalent,
  19912. std::partial_ordering::unordered,
  19913. lhs_type <=> rhs_type) // *NOPAD*
  19914. }
  19915. /// @brief comparison: 3-way
  19916. /// @sa https://json.nlohmann.me/api/basic_json/operator_spaceship/
  19917. template<typename ScalarType>
  19918. requires std::is_scalar_v<ScalarType>
  19919. std::partial_ordering operator<=>(ScalarType rhs) const noexcept // *NOPAD*
  19920. {
  19921. return *this <=> basic_json(rhs); // *NOPAD*
  19922. }
  19923. #if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  19924. // all operators that are computed as an odd number of inverses of others
  19925. // need to be overloaded to emulate the legacy comparison behavior
  19926. /// @brief comparison: less than or equal
  19927. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  19928. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON)
  19929. bool operator<=(const_reference rhs) const noexcept
  19930. {
  19931. if (compares_unordered(rhs, true))
  19932. {
  19933. return false;
  19934. }
  19935. return !(rhs < *this);
  19936. }
  19937. /// @brief comparison: less than or equal
  19938. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  19939. template<typename ScalarType>
  19940. requires std::is_scalar_v<ScalarType>
  19941. bool operator<=(ScalarType rhs) const noexcept
  19942. {
  19943. return *this <= basic_json(rhs);
  19944. }
  19945. /// @brief comparison: greater than or equal
  19946. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  19947. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON)
  19948. bool operator>=(const_reference rhs) const noexcept
  19949. {
  19950. if (compares_unordered(rhs, true))
  19951. {
  19952. return false;
  19953. }
  19954. return !(*this < rhs);
  19955. }
  19956. /// @brief comparison: greater than or equal
  19957. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  19958. template<typename ScalarType>
  19959. requires std::is_scalar_v<ScalarType>
  19960. bool operator>=(ScalarType rhs) const noexcept
  19961. {
  19962. return *this >= basic_json(rhs);
  19963. }
  19964. #endif
  19965. #else
  19966. /// @brief comparison: equal
  19967. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  19968. friend bool operator==(const_reference lhs, const_reference rhs) noexcept
  19969. {
  19970. #ifdef __GNUC__
  19971. #pragma GCC diagnostic push
  19972. #pragma GCC diagnostic ignored "-Wfloat-equal"
  19973. #endif
  19974. JSON_IMPLEMENT_OPERATOR( ==, true, false, false)
  19975. #ifdef __GNUC__
  19976. #pragma GCC diagnostic pop
  19977. #endif
  19978. }
  19979. /// @brief comparison: equal
  19980. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  19981. template<typename ScalarType, typename std::enable_if<
  19982. std::is_scalar<ScalarType>::value, int>::type = 0>
  19983. friend bool operator==(const_reference lhs, ScalarType rhs) noexcept
  19984. {
  19985. return lhs == basic_json(rhs);
  19986. }
  19987. /// @brief comparison: equal
  19988. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  19989. template<typename ScalarType, typename std::enable_if<
  19990. std::is_scalar<ScalarType>::value, int>::type = 0>
  19991. friend bool operator==(ScalarType lhs, const_reference rhs) noexcept
  19992. {
  19993. return basic_json(lhs) == rhs;
  19994. }
  19995. /// @brief comparison: not equal
  19996. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  19997. friend bool operator!=(const_reference lhs, const_reference rhs) noexcept
  19998. {
  19999. if (compares_unordered(lhs, rhs, true))
  20000. {
  20001. return false;
  20002. }
  20003. return !(lhs == rhs);
  20004. }
  20005. /// @brief comparison: not equal
  20006. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  20007. template<typename ScalarType, typename std::enable_if<
  20008. std::is_scalar<ScalarType>::value, int>::type = 0>
  20009. friend bool operator!=(const_reference lhs, ScalarType rhs) noexcept
  20010. {
  20011. return lhs != basic_json(rhs);
  20012. }
  20013. /// @brief comparison: not equal
  20014. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  20015. template<typename ScalarType, typename std::enable_if<
  20016. std::is_scalar<ScalarType>::value, int>::type = 0>
  20017. friend bool operator!=(ScalarType lhs, const_reference rhs) noexcept
  20018. {
  20019. return basic_json(lhs) != rhs;
  20020. }
  20021. /// @brief comparison: less than
  20022. /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
  20023. friend bool operator<(const_reference lhs, const_reference rhs) noexcept
  20024. {
  20025. // default_result is used if we cannot compare values. In that case,
  20026. // we compare types. Note we have to call the operator explicitly,
  20027. // because MSVC has problems otherwise.
  20028. JSON_IMPLEMENT_OPERATOR( <, false, false, operator<(lhs_type, rhs_type))
  20029. }
  20030. /// @brief comparison: less than
  20031. /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
  20032. template<typename ScalarType, typename std::enable_if<
  20033. std::is_scalar<ScalarType>::value, int>::type = 0>
  20034. friend bool operator<(const_reference lhs, ScalarType rhs) noexcept
  20035. {
  20036. return lhs < basic_json(rhs);
  20037. }
  20038. /// @brief comparison: less than
  20039. /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
  20040. template<typename ScalarType, typename std::enable_if<
  20041. std::is_scalar<ScalarType>::value, int>::type = 0>
  20042. friend bool operator<(ScalarType lhs, const_reference rhs) noexcept
  20043. {
  20044. return basic_json(lhs) < rhs;
  20045. }
  20046. /// @brief comparison: less than or equal
  20047. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  20048. friend bool operator<=(const_reference lhs, const_reference rhs) noexcept
  20049. {
  20050. if (compares_unordered(lhs, rhs, true))
  20051. {
  20052. return false;
  20053. }
  20054. return !(rhs < lhs);
  20055. }
  20056. /// @brief comparison: less than or equal
  20057. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  20058. template<typename ScalarType, typename std::enable_if<
  20059. std::is_scalar<ScalarType>::value, int>::type = 0>
  20060. friend bool operator<=(const_reference lhs, ScalarType rhs) noexcept
  20061. {
  20062. return lhs <= basic_json(rhs);
  20063. }
  20064. /// @brief comparison: less than or equal
  20065. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  20066. template<typename ScalarType, typename std::enable_if<
  20067. std::is_scalar<ScalarType>::value, int>::type = 0>
  20068. friend bool operator<=(ScalarType lhs, const_reference rhs) noexcept
  20069. {
  20070. return basic_json(lhs) <= rhs;
  20071. }
  20072. /// @brief comparison: greater than
  20073. /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
  20074. friend bool operator>(const_reference lhs, const_reference rhs) noexcept
  20075. {
  20076. // double inverse
  20077. if (compares_unordered(lhs, rhs))
  20078. {
  20079. return false;
  20080. }
  20081. return !(lhs <= rhs);
  20082. }
  20083. /// @brief comparison: greater than
  20084. /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
  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: greater than
  20092. /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
  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: greater than or equal
  20100. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  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 !(lhs < rhs);
  20108. }
  20109. /// @brief comparison: greater than or equal
  20110. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  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: greater than or equal
  20118. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  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. #endif
  20126. #undef JSON_IMPLEMENT_OPERATOR
  20127. /// @}
  20128. ///////////////////
  20129. // serialization //
  20130. ///////////////////
  20131. /// @name serialization
  20132. /// @{
  20133. #ifndef JSON_NO_IO
  20134. /// @brief serialize to stream
  20135. /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
  20136. friend std::ostream& operator<<(std::ostream& o, const basic_json& j)
  20137. {
  20138. // read width member and use it as indentation parameter if nonzero
  20139. const bool pretty_print = o.width() > 0;
  20140. const auto indentation = pretty_print ? o.width() : 0;
  20141. // reset width to 0 for subsequent calls to this stream
  20142. o.width(0);
  20143. // do the actual serialization
  20144. serializer s(detail::output_adapter<char>(o), o.fill());
  20145. s.dump(j, pretty_print, false, static_cast<unsigned int>(indentation));
  20146. return o;
  20147. }
  20148. /// @brief serialize to stream
  20149. /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
  20150. /// @deprecated This function is deprecated since 3.0.0 and will be removed in
  20151. /// version 4.0.0 of the library. Please use
  20152. /// operator<<(std::ostream&, const basic_json&) instead; that is,
  20153. /// replace calls like `j >> o;` with `o << j;`.
  20154. JSON_HEDLEY_DEPRECATED_FOR(3.0.0, operator<<(std::ostream&, const basic_json&))
  20155. friend std::ostream& operator>>(const basic_json& j, std::ostream& o)
  20156. {
  20157. return o << j;
  20158. }
  20159. #endif // JSON_NO_IO
  20160. /// @}
  20161. /////////////////////
  20162. // deserialization //
  20163. /////////////////////
  20164. /// @name deserialization
  20165. /// @{
  20166. /// @brief deserialize from a compatible input
  20167. /// @sa https://json.nlohmann.me/api/basic_json/parse/
  20168. template<typename InputType>
  20169. JSON_HEDLEY_WARN_UNUSED_RESULT
  20170. static basic_json parse(InputType&& i,
  20171. const parser_callback_t cb = nullptr,
  20172. const bool allow_exceptions = true,
  20173. const bool ignore_comments = false)
  20174. {
  20175. basic_json result;
  20176. parser(detail::input_adapter(std::forward<InputType>(i)), cb, allow_exceptions, ignore_comments).parse(true, result);
  20177. return result;
  20178. }
  20179. /// @brief deserialize from a pair of character iterators
  20180. /// @sa https://json.nlohmann.me/api/basic_json/parse/
  20181. template<typename IteratorType>
  20182. JSON_HEDLEY_WARN_UNUSED_RESULT
  20183. static basic_json parse(IteratorType first,
  20184. IteratorType last,
  20185. const parser_callback_t cb = nullptr,
  20186. const bool allow_exceptions = true,
  20187. const bool ignore_comments = false)
  20188. {
  20189. basic_json result;
  20190. parser(detail::input_adapter(std::move(first), std::move(last)), cb, allow_exceptions, ignore_comments).parse(true, result);
  20191. return result;
  20192. }
  20193. JSON_HEDLEY_WARN_UNUSED_RESULT
  20194. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, parse(ptr, ptr + len))
  20195. static basic_json parse(detail::span_input_adapter&& i,
  20196. const parser_callback_t cb = nullptr,
  20197. const bool allow_exceptions = true,
  20198. const bool ignore_comments = false)
  20199. {
  20200. basic_json result;
  20201. parser(i.get(), cb, allow_exceptions, ignore_comments).parse(true, result);
  20202. return result;
  20203. }
  20204. /// @brief check if the input is valid JSON
  20205. /// @sa https://json.nlohmann.me/api/basic_json/accept/
  20206. template<typename InputType>
  20207. static bool accept(InputType&& i,
  20208. const bool ignore_comments = false)
  20209. {
  20210. return parser(detail::input_adapter(std::forward<InputType>(i)), nullptr, false, ignore_comments).accept(true);
  20211. }
  20212. /// @brief check if the input is valid JSON
  20213. /// @sa https://json.nlohmann.me/api/basic_json/accept/
  20214. template<typename IteratorType>
  20215. static bool accept(IteratorType first, IteratorType last,
  20216. const bool ignore_comments = false)
  20217. {
  20218. return parser(detail::input_adapter(std::move(first), std::move(last)), nullptr, false, ignore_comments).accept(true);
  20219. }
  20220. JSON_HEDLEY_WARN_UNUSED_RESULT
  20221. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, accept(ptr, ptr + len))
  20222. static bool accept(detail::span_input_adapter&& i,
  20223. const bool ignore_comments = false)
  20224. {
  20225. return parser(i.get(), nullptr, false, ignore_comments).accept(true);
  20226. }
  20227. /// @brief generate SAX events
  20228. /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
  20229. template <typename InputType, typename SAX>
  20230. JSON_HEDLEY_NON_NULL(2)
  20231. static bool sax_parse(InputType&& i, SAX* sax,
  20232. input_format_t format = input_format_t::json,
  20233. const bool strict = true,
  20234. const bool ignore_comments = false)
  20235. {
  20236. auto ia = detail::input_adapter(std::forward<InputType>(i));
  20237. return format == input_format_t::json
  20238. ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
  20239. : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
  20240. }
  20241. /// @brief generate SAX events
  20242. /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
  20243. template<class IteratorType, class SAX>
  20244. JSON_HEDLEY_NON_NULL(3)
  20245. static bool sax_parse(IteratorType first, IteratorType last, SAX* sax,
  20246. input_format_t format = input_format_t::json,
  20247. const bool strict = true,
  20248. const bool ignore_comments = false)
  20249. {
  20250. auto ia = detail::input_adapter(std::move(first), std::move(last));
  20251. return format == input_format_t::json
  20252. ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
  20253. : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
  20254. }
  20255. /// @brief generate SAX events
  20256. /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
  20257. /// @deprecated This function is deprecated since 3.8.0 and will be removed in
  20258. /// version 4.0.0 of the library. Please use
  20259. /// sax_parse(ptr, ptr + len) instead.
  20260. template <typename SAX>
  20261. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, sax_parse(ptr, ptr + len, ...))
  20262. JSON_HEDLEY_NON_NULL(2)
  20263. static bool sax_parse(detail::span_input_adapter&& i, SAX* sax,
  20264. input_format_t format = input_format_t::json,
  20265. const bool strict = true,
  20266. const bool ignore_comments = false)
  20267. {
  20268. auto ia = i.get();
  20269. return format == input_format_t::json
  20270. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  20271. ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
  20272. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  20273. : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
  20274. }
  20275. #ifndef JSON_NO_IO
  20276. /// @brief deserialize from stream
  20277. /// @sa https://json.nlohmann.me/api/basic_json/operator_gtgt/
  20278. /// @deprecated This stream operator is deprecated since 3.0.0 and will be removed in
  20279. /// version 4.0.0 of the library. Please use
  20280. /// operator>>(std::istream&, basic_json&) instead; that is,
  20281. /// replace calls like `j << i;` with `i >> j;`.
  20282. JSON_HEDLEY_DEPRECATED_FOR(3.0.0, operator>>(std::istream&, basic_json&))
  20283. friend std::istream& operator<<(basic_json& j, std::istream& i)
  20284. {
  20285. return operator>>(i, j);
  20286. }
  20287. /// @brief deserialize from stream
  20288. /// @sa https://json.nlohmann.me/api/basic_json/operator_gtgt/
  20289. friend std::istream& operator>>(std::istream& i, basic_json& j)
  20290. {
  20291. parser(detail::input_adapter(i)).parse(false, j);
  20292. return i;
  20293. }
  20294. #endif // JSON_NO_IO
  20295. /// @}
  20296. ///////////////////////////
  20297. // convenience functions //
  20298. ///////////////////////////
  20299. /// @brief return the type as string
  20300. /// @sa https://json.nlohmann.me/api/basic_json/type_name/
  20301. JSON_HEDLEY_RETURNS_NON_NULL
  20302. const char* type_name() const noexcept
  20303. {
  20304. switch (m_type)
  20305. {
  20306. case value_t::null:
  20307. return "null";
  20308. case value_t::object:
  20309. return "object";
  20310. case value_t::array:
  20311. return "array";
  20312. case value_t::string:
  20313. return "string";
  20314. case value_t::boolean:
  20315. return "boolean";
  20316. case value_t::binary:
  20317. return "binary";
  20318. case value_t::discarded:
  20319. return "discarded";
  20320. case value_t::number_integer:
  20321. case value_t::number_unsigned:
  20322. case value_t::number_float:
  20323. default:
  20324. return "number";
  20325. }
  20326. }
  20327. JSON_PRIVATE_UNLESS_TESTED:
  20328. //////////////////////
  20329. // member variables //
  20330. //////////////////////
  20331. /// the type of the current element
  20332. value_t m_type = value_t::null;
  20333. /// the value of the current element
  20334. json_value m_value = {};
  20335. #if JSON_DIAGNOSTICS
  20336. /// a pointer to a parent value (for debugging purposes)
  20337. basic_json* m_parent = nullptr;
  20338. #endif
  20339. //////////////////////////////////////////
  20340. // binary serialization/deserialization //
  20341. //////////////////////////////////////////
  20342. /// @name binary serialization/deserialization support
  20343. /// @{
  20344. public:
  20345. /// @brief create a CBOR serialization of a given JSON value
  20346. /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
  20347. static std::vector<std::uint8_t> to_cbor(const basic_json& j)
  20348. {
  20349. std::vector<std::uint8_t> result;
  20350. to_cbor(j, result);
  20351. return result;
  20352. }
  20353. /// @brief create a CBOR serialization of a given JSON value
  20354. /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
  20355. static void to_cbor(const basic_json& j, detail::output_adapter<std::uint8_t> o)
  20356. {
  20357. binary_writer<std::uint8_t>(o).write_cbor(j);
  20358. }
  20359. /// @brief create a CBOR serialization of a given JSON value
  20360. /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
  20361. static void to_cbor(const basic_json& j, detail::output_adapter<char> o)
  20362. {
  20363. binary_writer<char>(o).write_cbor(j);
  20364. }
  20365. /// @brief create a MessagePack serialization of a given JSON value
  20366. /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
  20367. static std::vector<std::uint8_t> to_msgpack(const basic_json& j)
  20368. {
  20369. std::vector<std::uint8_t> result;
  20370. to_msgpack(j, result);
  20371. return result;
  20372. }
  20373. /// @brief create a MessagePack serialization of a given JSON value
  20374. /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
  20375. static void to_msgpack(const basic_json& j, detail::output_adapter<std::uint8_t> o)
  20376. {
  20377. binary_writer<std::uint8_t>(o).write_msgpack(j);
  20378. }
  20379. /// @brief create a MessagePack serialization of a given JSON value
  20380. /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
  20381. static void to_msgpack(const basic_json& j, detail::output_adapter<char> o)
  20382. {
  20383. binary_writer<char>(o).write_msgpack(j);
  20384. }
  20385. /// @brief create a UBJSON serialization of a given JSON value
  20386. /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
  20387. static std::vector<std::uint8_t> to_ubjson(const basic_json& j,
  20388. const bool use_size = false,
  20389. const bool use_type = false)
  20390. {
  20391. std::vector<std::uint8_t> result;
  20392. to_ubjson(j, result, use_size, use_type);
  20393. return result;
  20394. }
  20395. /// @brief create a UBJSON serialization of a given JSON value
  20396. /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
  20397. static void to_ubjson(const basic_json& j, detail::output_adapter<std::uint8_t> o,
  20398. const bool use_size = false, const bool use_type = false)
  20399. {
  20400. binary_writer<std::uint8_t>(o).write_ubjson(j, use_size, use_type);
  20401. }
  20402. /// @brief create a UBJSON serialization of a given JSON value
  20403. /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
  20404. static void to_ubjson(const basic_json& j, detail::output_adapter<char> o,
  20405. const bool use_size = false, const bool use_type = false)
  20406. {
  20407. binary_writer<char>(o).write_ubjson(j, use_size, use_type);
  20408. }
  20409. /// @brief create a BJData serialization of a given JSON value
  20410. /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
  20411. static std::vector<std::uint8_t> to_bjdata(const basic_json& j,
  20412. const bool use_size = false,
  20413. const bool use_type = false)
  20414. {
  20415. std::vector<std::uint8_t> result;
  20416. to_bjdata(j, result, use_size, use_type);
  20417. return result;
  20418. }
  20419. /// @brief create a BJData serialization of a given JSON value
  20420. /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
  20421. static void to_bjdata(const basic_json& j, detail::output_adapter<std::uint8_t> o,
  20422. const bool use_size = false, const bool use_type = false)
  20423. {
  20424. binary_writer<std::uint8_t>(o).write_ubjson(j, use_size, use_type, true, true);
  20425. }
  20426. /// @brief create a BJData serialization of a given JSON value
  20427. /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
  20428. static void to_bjdata(const basic_json& j, detail::output_adapter<char> o,
  20429. const bool use_size = false, const bool use_type = false)
  20430. {
  20431. binary_writer<char>(o).write_ubjson(j, use_size, use_type, true, true);
  20432. }
  20433. /// @brief create a BSON serialization of a given JSON value
  20434. /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
  20435. static std::vector<std::uint8_t> to_bson(const basic_json& j)
  20436. {
  20437. std::vector<std::uint8_t> result;
  20438. to_bson(j, result);
  20439. return result;
  20440. }
  20441. /// @brief create a BSON serialization of a given JSON value
  20442. /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
  20443. static void to_bson(const basic_json& j, detail::output_adapter<std::uint8_t> o)
  20444. {
  20445. binary_writer<std::uint8_t>(o).write_bson(j);
  20446. }
  20447. /// @brief create a BSON serialization of a given JSON value
  20448. /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
  20449. static void to_bson(const basic_json& j, detail::output_adapter<char> o)
  20450. {
  20451. binary_writer<char>(o).write_bson(j);
  20452. }
  20453. /// @brief create a JSON value from an input in CBOR format
  20454. /// @sa https://json.nlohmann.me/api/basic_json/from_cbor/
  20455. template<typename InputType>
  20456. JSON_HEDLEY_WARN_UNUSED_RESULT
  20457. static basic_json from_cbor(InputType&& i,
  20458. const bool strict = true,
  20459. const bool allow_exceptions = true,
  20460. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  20461. {
  20462. basic_json result;
  20463. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20464. auto ia = detail::input_adapter(std::forward<InputType>(i));
  20465. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler);
  20466. return res ? result : basic_json(value_t::discarded);
  20467. }
  20468. /// @brief create a JSON value from an input in CBOR format
  20469. /// @sa https://json.nlohmann.me/api/basic_json/from_cbor/
  20470. template<typename IteratorType>
  20471. JSON_HEDLEY_WARN_UNUSED_RESULT
  20472. static basic_json from_cbor(IteratorType first, IteratorType last,
  20473. const bool strict = true,
  20474. const bool allow_exceptions = true,
  20475. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  20476. {
  20477. basic_json result;
  20478. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20479. auto ia = detail::input_adapter(std::move(first), std::move(last));
  20480. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler);
  20481. return res ? result : basic_json(value_t::discarded);
  20482. }
  20483. template<typename T>
  20484. JSON_HEDLEY_WARN_UNUSED_RESULT
  20485. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_cbor(ptr, ptr + len))
  20486. static basic_json from_cbor(const T* ptr, std::size_t len,
  20487. const bool strict = true,
  20488. const bool allow_exceptions = true,
  20489. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  20490. {
  20491. return from_cbor(ptr, ptr + len, strict, allow_exceptions, tag_handler);
  20492. }
  20493. JSON_HEDLEY_WARN_UNUSED_RESULT
  20494. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_cbor(ptr, ptr + len))
  20495. static basic_json from_cbor(detail::span_input_adapter&& i,
  20496. const bool strict = true,
  20497. const bool allow_exceptions = true,
  20498. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  20499. {
  20500. basic_json result;
  20501. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20502. auto ia = i.get();
  20503. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  20504. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler);
  20505. return res ? result : basic_json(value_t::discarded);
  20506. }
  20507. /// @brief create a JSON value from an input in MessagePack format
  20508. /// @sa https://json.nlohmann.me/api/basic_json/from_msgpack/
  20509. template<typename InputType>
  20510. JSON_HEDLEY_WARN_UNUSED_RESULT
  20511. static basic_json from_msgpack(InputType&& i,
  20512. const bool strict = true,
  20513. const bool allow_exceptions = true)
  20514. {
  20515. basic_json result;
  20516. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20517. auto ia = detail::input_adapter(std::forward<InputType>(i));
  20518. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict);
  20519. return res ? result : basic_json(value_t::discarded);
  20520. }
  20521. /// @brief create a JSON value from an input in MessagePack format
  20522. /// @sa https://json.nlohmann.me/api/basic_json/from_msgpack/
  20523. template<typename IteratorType>
  20524. JSON_HEDLEY_WARN_UNUSED_RESULT
  20525. static basic_json from_msgpack(IteratorType first, IteratorType last,
  20526. const bool strict = true,
  20527. const bool allow_exceptions = true)
  20528. {
  20529. basic_json result;
  20530. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20531. auto ia = detail::input_adapter(std::move(first), std::move(last));
  20532. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict);
  20533. return res ? result : basic_json(value_t::discarded);
  20534. }
  20535. template<typename T>
  20536. JSON_HEDLEY_WARN_UNUSED_RESULT
  20537. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_msgpack(ptr, ptr + len))
  20538. static basic_json from_msgpack(const T* ptr, std::size_t len,
  20539. const bool strict = true,
  20540. const bool allow_exceptions = true)
  20541. {
  20542. return from_msgpack(ptr, ptr + len, strict, allow_exceptions);
  20543. }
  20544. JSON_HEDLEY_WARN_UNUSED_RESULT
  20545. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_msgpack(ptr, ptr + len))
  20546. static basic_json from_msgpack(detail::span_input_adapter&& i,
  20547. const bool strict = true,
  20548. const bool allow_exceptions = true)
  20549. {
  20550. basic_json result;
  20551. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20552. auto ia = i.get();
  20553. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  20554. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict);
  20555. return res ? result : basic_json(value_t::discarded);
  20556. }
  20557. /// @brief create a JSON value from an input in UBJSON format
  20558. /// @sa https://json.nlohmann.me/api/basic_json/from_ubjson/
  20559. template<typename InputType>
  20560. JSON_HEDLEY_WARN_UNUSED_RESULT
  20561. static basic_json from_ubjson(InputType&& i,
  20562. const bool strict = true,
  20563. const bool allow_exceptions = true)
  20564. {
  20565. basic_json result;
  20566. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20567. auto ia = detail::input_adapter(std::forward<InputType>(i));
  20568. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict);
  20569. return res ? result : basic_json(value_t::discarded);
  20570. }
  20571. /// @brief create a JSON value from an input in UBJSON format
  20572. /// @sa https://json.nlohmann.me/api/basic_json/from_ubjson/
  20573. template<typename IteratorType>
  20574. JSON_HEDLEY_WARN_UNUSED_RESULT
  20575. static basic_json from_ubjson(IteratorType first, IteratorType last,
  20576. const bool strict = true,
  20577. const bool allow_exceptions = true)
  20578. {
  20579. basic_json result;
  20580. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20581. auto ia = detail::input_adapter(std::move(first), std::move(last));
  20582. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict);
  20583. return res ? result : basic_json(value_t::discarded);
  20584. }
  20585. template<typename T>
  20586. JSON_HEDLEY_WARN_UNUSED_RESULT
  20587. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_ubjson(ptr, ptr + len))
  20588. static basic_json from_ubjson(const T* ptr, std::size_t len,
  20589. const bool strict = true,
  20590. const bool allow_exceptions = true)
  20591. {
  20592. return from_ubjson(ptr, ptr + len, strict, allow_exceptions);
  20593. }
  20594. JSON_HEDLEY_WARN_UNUSED_RESULT
  20595. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_ubjson(ptr, ptr + len))
  20596. static basic_json from_ubjson(detail::span_input_adapter&& i,
  20597. const bool strict = true,
  20598. const bool allow_exceptions = true)
  20599. {
  20600. basic_json result;
  20601. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20602. auto ia = i.get();
  20603. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  20604. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict);
  20605. return res ? result : basic_json(value_t::discarded);
  20606. }
  20607. /// @brief create a JSON value from an input in BJData format
  20608. /// @sa https://json.nlohmann.me/api/basic_json/from_bjdata/
  20609. template<typename InputType>
  20610. JSON_HEDLEY_WARN_UNUSED_RESULT
  20611. static basic_json from_bjdata(InputType&& i,
  20612. const bool strict = true,
  20613. const bool allow_exceptions = true)
  20614. {
  20615. basic_json result;
  20616. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20617. auto ia = detail::input_adapter(std::forward<InputType>(i));
  20618. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict);
  20619. return res ? result : basic_json(value_t::discarded);
  20620. }
  20621. /// @brief create a JSON value from an input in BJData format
  20622. /// @sa https://json.nlohmann.me/api/basic_json/from_bjdata/
  20623. template<typename IteratorType>
  20624. JSON_HEDLEY_WARN_UNUSED_RESULT
  20625. static basic_json from_bjdata(IteratorType first, IteratorType last,
  20626. const bool strict = true,
  20627. const bool allow_exceptions = true)
  20628. {
  20629. basic_json result;
  20630. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20631. auto ia = detail::input_adapter(std::move(first), std::move(last));
  20632. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict);
  20633. return res ? result : basic_json(value_t::discarded);
  20634. }
  20635. /// @brief create a JSON value from an input in BSON format
  20636. /// @sa https://json.nlohmann.me/api/basic_json/from_bson/
  20637. template<typename InputType>
  20638. JSON_HEDLEY_WARN_UNUSED_RESULT
  20639. static basic_json from_bson(InputType&& i,
  20640. const bool strict = true,
  20641. const bool allow_exceptions = true)
  20642. {
  20643. basic_json result;
  20644. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20645. auto ia = detail::input_adapter(std::forward<InputType>(i));
  20646. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict);
  20647. return res ? result : basic_json(value_t::discarded);
  20648. }
  20649. /// @brief create a JSON value from an input in BSON format
  20650. /// @sa https://json.nlohmann.me/api/basic_json/from_bson/
  20651. template<typename IteratorType>
  20652. JSON_HEDLEY_WARN_UNUSED_RESULT
  20653. static basic_json from_bson(IteratorType first, IteratorType last,
  20654. const bool strict = true,
  20655. const bool allow_exceptions = true)
  20656. {
  20657. basic_json result;
  20658. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20659. auto ia = detail::input_adapter(std::move(first), std::move(last));
  20660. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict);
  20661. return res ? result : basic_json(value_t::discarded);
  20662. }
  20663. template<typename T>
  20664. JSON_HEDLEY_WARN_UNUSED_RESULT
  20665. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_bson(ptr, ptr + len))
  20666. static basic_json from_bson(const T* ptr, std::size_t len,
  20667. const bool strict = true,
  20668. const bool allow_exceptions = true)
  20669. {
  20670. return from_bson(ptr, ptr + len, strict, allow_exceptions);
  20671. }
  20672. JSON_HEDLEY_WARN_UNUSED_RESULT
  20673. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_bson(ptr, ptr + len))
  20674. static basic_json from_bson(detail::span_input_adapter&& i,
  20675. const bool strict = true,
  20676. const bool allow_exceptions = true)
  20677. {
  20678. basic_json result;
  20679. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20680. auto ia = i.get();
  20681. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  20682. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict);
  20683. return res ? result : basic_json(value_t::discarded);
  20684. }
  20685. /// @}
  20686. //////////////////////////
  20687. // JSON Pointer support //
  20688. //////////////////////////
  20689. /// @name JSON Pointer functions
  20690. /// @{
  20691. /// @brief access specified element via JSON Pointer
  20692. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  20693. reference operator[](const json_pointer& ptr)
  20694. {
  20695. return ptr.get_unchecked(this);
  20696. }
  20697. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  20698. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  20699. reference operator[](const ::nlohmann::json_pointer<BasicJsonType>& ptr)
  20700. {
  20701. return ptr.get_unchecked(this);
  20702. }
  20703. /// @brief access specified element via JSON Pointer
  20704. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  20705. const_reference operator[](const json_pointer& ptr) const
  20706. {
  20707. return ptr.get_unchecked(this);
  20708. }
  20709. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  20710. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  20711. const_reference operator[](const ::nlohmann::json_pointer<BasicJsonType>& ptr) const
  20712. {
  20713. return ptr.get_unchecked(this);
  20714. }
  20715. /// @brief access specified element via JSON Pointer
  20716. /// @sa https://json.nlohmann.me/api/basic_json/at/
  20717. reference at(const json_pointer& ptr)
  20718. {
  20719. return ptr.get_checked(this);
  20720. }
  20721. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  20722. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  20723. reference at(const ::nlohmann::json_pointer<BasicJsonType>& ptr)
  20724. {
  20725. return ptr.get_checked(this);
  20726. }
  20727. /// @brief access specified element via JSON Pointer
  20728. /// @sa https://json.nlohmann.me/api/basic_json/at/
  20729. const_reference at(const json_pointer& ptr) const
  20730. {
  20731. return ptr.get_checked(this);
  20732. }
  20733. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  20734. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  20735. const_reference at(const ::nlohmann::json_pointer<BasicJsonType>& ptr) const
  20736. {
  20737. return ptr.get_checked(this);
  20738. }
  20739. /// @brief return flattened JSON value
  20740. /// @sa https://json.nlohmann.me/api/basic_json/flatten/
  20741. basic_json flatten() const
  20742. {
  20743. basic_json result(value_t::object);
  20744. json_pointer::flatten("", *this, result);
  20745. return result;
  20746. }
  20747. /// @brief unflatten a previously flattened JSON value
  20748. /// @sa https://json.nlohmann.me/api/basic_json/unflatten/
  20749. basic_json unflatten() const
  20750. {
  20751. return json_pointer::unflatten(*this);
  20752. }
  20753. /// @}
  20754. //////////////////////////
  20755. // JSON Patch functions //
  20756. //////////////////////////
  20757. /// @name JSON Patch functions
  20758. /// @{
  20759. /// @brief applies a JSON patch in-place without copying the object
  20760. /// @sa https://json.nlohmann.me/api/basic_json/patch/
  20761. void patch_inplace(const basic_json& json_patch)
  20762. {
  20763. basic_json& result = *this;
  20764. // the valid JSON Patch operations
  20765. enum class patch_operations {add, remove, replace, move, copy, test, invalid};
  20766. const auto get_op = [](const std::string & op)
  20767. {
  20768. if (op == "add")
  20769. {
  20770. return patch_operations::add;
  20771. }
  20772. if (op == "remove")
  20773. {
  20774. return patch_operations::remove;
  20775. }
  20776. if (op == "replace")
  20777. {
  20778. return patch_operations::replace;
  20779. }
  20780. if (op == "move")
  20781. {
  20782. return patch_operations::move;
  20783. }
  20784. if (op == "copy")
  20785. {
  20786. return patch_operations::copy;
  20787. }
  20788. if (op == "test")
  20789. {
  20790. return patch_operations::test;
  20791. }
  20792. return patch_operations::invalid;
  20793. };
  20794. // wrapper for "add" operation; add value at ptr
  20795. const auto operation_add = [&result](json_pointer & ptr, basic_json val)
  20796. {
  20797. // adding to the root of the target document means replacing it
  20798. if (ptr.empty())
  20799. {
  20800. result = val;
  20801. return;
  20802. }
  20803. // make sure the top element of the pointer exists
  20804. json_pointer top_pointer = ptr.top();
  20805. if (top_pointer != ptr)
  20806. {
  20807. result.at(top_pointer);
  20808. }
  20809. // get reference to parent of JSON pointer ptr
  20810. const auto last_path = ptr.back();
  20811. ptr.pop_back();
  20812. // parent must exist when performing patch add per RFC6902 specs
  20813. basic_json& parent = result.at(ptr);
  20814. switch (parent.m_type)
  20815. {
  20816. case value_t::null:
  20817. case value_t::object:
  20818. {
  20819. // use operator[] to add value
  20820. parent[last_path] = val;
  20821. break;
  20822. }
  20823. case value_t::array:
  20824. {
  20825. if (last_path == "-")
  20826. {
  20827. // special case: append to back
  20828. parent.push_back(val);
  20829. }
  20830. else
  20831. {
  20832. const auto idx = json_pointer::template array_index<basic_json_t>(last_path);
  20833. if (JSON_HEDLEY_UNLIKELY(idx > parent.size()))
  20834. {
  20835. // avoid undefined behavior
  20836. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), &parent));
  20837. }
  20838. // default case: insert add offset
  20839. parent.insert(parent.begin() + static_cast<difference_type>(idx), val);
  20840. }
  20841. break;
  20842. }
  20843. // if there exists a parent it cannot be primitive
  20844. case value_t::string: // LCOV_EXCL_LINE
  20845. case value_t::boolean: // LCOV_EXCL_LINE
  20846. case value_t::number_integer: // LCOV_EXCL_LINE
  20847. case value_t::number_unsigned: // LCOV_EXCL_LINE
  20848. case value_t::number_float: // LCOV_EXCL_LINE
  20849. case value_t::binary: // LCOV_EXCL_LINE
  20850. case value_t::discarded: // LCOV_EXCL_LINE
  20851. default: // LCOV_EXCL_LINE
  20852. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  20853. }
  20854. };
  20855. // wrapper for "remove" operation; remove value at ptr
  20856. const auto operation_remove = [this, &result](json_pointer & ptr)
  20857. {
  20858. // get reference to parent of JSON pointer ptr
  20859. const auto last_path = ptr.back();
  20860. ptr.pop_back();
  20861. basic_json& parent = result.at(ptr);
  20862. // remove child
  20863. if (parent.is_object())
  20864. {
  20865. // perform range check
  20866. auto it = parent.find(last_path);
  20867. if (JSON_HEDLEY_LIKELY(it != parent.end()))
  20868. {
  20869. parent.erase(it);
  20870. }
  20871. else
  20872. {
  20873. JSON_THROW(out_of_range::create(403, detail::concat("key '", last_path, "' not found"), this));
  20874. }
  20875. }
  20876. else if (parent.is_array())
  20877. {
  20878. // note erase performs range check
  20879. parent.erase(json_pointer::template array_index<basic_json_t>(last_path));
  20880. }
  20881. };
  20882. // type check: top level value must be an array
  20883. if (JSON_HEDLEY_UNLIKELY(!json_patch.is_array()))
  20884. {
  20885. JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", &json_patch));
  20886. }
  20887. // iterate and apply the operations
  20888. for (const auto& val : json_patch)
  20889. {
  20890. // wrapper to get a value for an operation
  20891. const auto get_value = [&val](const std::string & op,
  20892. const std::string & member,
  20893. bool string_type) -> basic_json &
  20894. {
  20895. // find value
  20896. auto it = val.m_value.object->find(member);
  20897. // context-sensitive error message
  20898. const auto error_msg = (op == "op") ? "operation" : detail::concat("operation '", op, '\'');
  20899. // check if desired value is present
  20900. if (JSON_HEDLEY_UNLIKELY(it == val.m_value.object->end()))
  20901. {
  20902. // NOLINTNEXTLINE(performance-inefficient-string-concatenation)
  20903. JSON_THROW(parse_error::create(105, 0, detail::concat(error_msg, " must have member '", member, "'"), &val));
  20904. }
  20905. // check if result is of type string
  20906. if (JSON_HEDLEY_UNLIKELY(string_type && !it->second.is_string()))
  20907. {
  20908. // NOLINTNEXTLINE(performance-inefficient-string-concatenation)
  20909. JSON_THROW(parse_error::create(105, 0, detail::concat(error_msg, " must have string member '", member, "'"), &val));
  20910. }
  20911. // no error: return value
  20912. return it->second;
  20913. };
  20914. // type check: every element of the array must be an object
  20915. if (JSON_HEDLEY_UNLIKELY(!val.is_object()))
  20916. {
  20917. JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", &val));
  20918. }
  20919. // collect mandatory members
  20920. const auto op = get_value("op", "op", true).template get<std::string>();
  20921. const auto path = get_value(op, "path", true).template get<std::string>();
  20922. json_pointer ptr(path);
  20923. switch (get_op(op))
  20924. {
  20925. case patch_operations::add:
  20926. {
  20927. operation_add(ptr, get_value("add", "value", false));
  20928. break;
  20929. }
  20930. case patch_operations::remove:
  20931. {
  20932. operation_remove(ptr);
  20933. break;
  20934. }
  20935. case patch_operations::replace:
  20936. {
  20937. // the "path" location must exist - use at()
  20938. result.at(ptr) = get_value("replace", "value", false);
  20939. break;
  20940. }
  20941. case patch_operations::move:
  20942. {
  20943. const auto from_path = get_value("move", "from", true).template get<std::string>();
  20944. json_pointer from_ptr(from_path);
  20945. // the "from" location must exist - use at()
  20946. basic_json v = result.at(from_ptr);
  20947. // The move operation is functionally identical to a
  20948. // "remove" operation on the "from" location, followed
  20949. // immediately by an "add" operation at the target
  20950. // location with the value that was just removed.
  20951. operation_remove(from_ptr);
  20952. operation_add(ptr, v);
  20953. break;
  20954. }
  20955. case patch_operations::copy:
  20956. {
  20957. const auto from_path = get_value("copy", "from", true).template get<std::string>();
  20958. const json_pointer from_ptr(from_path);
  20959. // the "from" location must exist - use at()
  20960. basic_json v = result.at(from_ptr);
  20961. // The copy is functionally identical to an "add"
  20962. // operation at the target location using the value
  20963. // specified in the "from" member.
  20964. operation_add(ptr, v);
  20965. break;
  20966. }
  20967. case patch_operations::test:
  20968. {
  20969. bool success = false;
  20970. JSON_TRY
  20971. {
  20972. // check if "value" matches the one at "path"
  20973. // the "path" location must exist - use at()
  20974. success = (result.at(ptr) == get_value("test", "value", false));
  20975. }
  20976. JSON_INTERNAL_CATCH (out_of_range&)
  20977. {
  20978. // ignore out of range errors: success remains false
  20979. }
  20980. // throw an exception if test fails
  20981. if (JSON_HEDLEY_UNLIKELY(!success))
  20982. {
  20983. JSON_THROW(other_error::create(501, detail::concat("unsuccessful: ", val.dump()), &val));
  20984. }
  20985. break;
  20986. }
  20987. case patch_operations::invalid:
  20988. default:
  20989. {
  20990. // op must be "add", "remove", "replace", "move", "copy", or
  20991. // "test"
  20992. JSON_THROW(parse_error::create(105, 0, detail::concat("operation value '", op, "' is invalid"), &val));
  20993. }
  20994. }
  20995. }
  20996. }
  20997. /// @brief applies a JSON patch to a copy of the current object
  20998. /// @sa https://json.nlohmann.me/api/basic_json/patch/
  20999. basic_json patch(const basic_json& json_patch) const
  21000. {
  21001. basic_json result = *this;
  21002. result.patch_inplace(json_patch);
  21003. return result;
  21004. }
  21005. /// @brief creates a diff as a JSON patch
  21006. /// @sa https://json.nlohmann.me/api/basic_json/diff/
  21007. JSON_HEDLEY_WARN_UNUSED_RESULT
  21008. static basic_json diff(const basic_json& source, const basic_json& target,
  21009. const std::string& path = "")
  21010. {
  21011. // the patch
  21012. basic_json result(value_t::array);
  21013. // if the values are the same, return empty patch
  21014. if (source == target)
  21015. {
  21016. return result;
  21017. }
  21018. if (source.type() != target.type())
  21019. {
  21020. // different types: replace value
  21021. result.push_back(
  21022. {
  21023. {"op", "replace"}, {"path", path}, {"value", target}
  21024. });
  21025. return result;
  21026. }
  21027. switch (source.type())
  21028. {
  21029. case value_t::array:
  21030. {
  21031. // first pass: traverse common elements
  21032. std::size_t i = 0;
  21033. while (i < source.size() && i < target.size())
  21034. {
  21035. // recursive call to compare array values at index i
  21036. auto temp_diff = diff(source[i], target[i], detail::concat(path, '/', std::to_string(i)));
  21037. result.insert(result.end(), temp_diff.begin(), temp_diff.end());
  21038. ++i;
  21039. }
  21040. // We now reached the end of at least one array
  21041. // in a second pass, traverse the remaining elements
  21042. // remove my remaining elements
  21043. const auto end_index = static_cast<difference_type>(result.size());
  21044. while (i < source.size())
  21045. {
  21046. // add operations in reverse order to avoid invalid
  21047. // indices
  21048. result.insert(result.begin() + end_index, object(
  21049. {
  21050. {"op", "remove"},
  21051. {"path", detail::concat(path, '/', std::to_string(i))}
  21052. }));
  21053. ++i;
  21054. }
  21055. // add other remaining elements
  21056. while (i < target.size())
  21057. {
  21058. result.push_back(
  21059. {
  21060. {"op", "add"},
  21061. {"path", detail::concat(path, "/-")},
  21062. {"value", target[i]}
  21063. });
  21064. ++i;
  21065. }
  21066. break;
  21067. }
  21068. case value_t::object:
  21069. {
  21070. // first pass: traverse this object's elements
  21071. for (auto it = source.cbegin(); it != source.cend(); ++it)
  21072. {
  21073. // escape the key name to be used in a JSON patch
  21074. const auto path_key = detail::concat(path, '/', detail::escape(it.key()));
  21075. if (target.find(it.key()) != target.end())
  21076. {
  21077. // recursive call to compare object values at key it
  21078. auto temp_diff = diff(it.value(), target[it.key()], path_key);
  21079. result.insert(result.end(), temp_diff.begin(), temp_diff.end());
  21080. }
  21081. else
  21082. {
  21083. // found a key that is not in o -> remove it
  21084. result.push_back(object(
  21085. {
  21086. {"op", "remove"}, {"path", path_key}
  21087. }));
  21088. }
  21089. }
  21090. // second pass: traverse other object's elements
  21091. for (auto it = target.cbegin(); it != target.cend(); ++it)
  21092. {
  21093. if (source.find(it.key()) == source.end())
  21094. {
  21095. // found a key that is not in this -> add it
  21096. const auto path_key = detail::concat(path, '/', detail::escape(it.key()));
  21097. result.push_back(
  21098. {
  21099. {"op", "add"}, {"path", path_key},
  21100. {"value", it.value()}
  21101. });
  21102. }
  21103. }
  21104. break;
  21105. }
  21106. case value_t::null:
  21107. case value_t::string:
  21108. case value_t::boolean:
  21109. case value_t::number_integer:
  21110. case value_t::number_unsigned:
  21111. case value_t::number_float:
  21112. case value_t::binary:
  21113. case value_t::discarded:
  21114. default:
  21115. {
  21116. // both primitive type: replace value
  21117. result.push_back(
  21118. {
  21119. {"op", "replace"}, {"path", path}, {"value", target}
  21120. });
  21121. break;
  21122. }
  21123. }
  21124. return result;
  21125. }
  21126. /// @}
  21127. ////////////////////////////////
  21128. // JSON Merge Patch functions //
  21129. ////////////////////////////////
  21130. /// @name JSON Merge Patch functions
  21131. /// @{
  21132. /// @brief applies a JSON Merge Patch
  21133. /// @sa https://json.nlohmann.me/api/basic_json/merge_patch/
  21134. void merge_patch(const basic_json& apply_patch)
  21135. {
  21136. if (apply_patch.is_object())
  21137. {
  21138. if (!is_object())
  21139. {
  21140. *this = object();
  21141. }
  21142. for (auto it = apply_patch.begin(); it != apply_patch.end(); ++it)
  21143. {
  21144. if (it.value().is_null())
  21145. {
  21146. erase(it.key());
  21147. }
  21148. else
  21149. {
  21150. operator[](it.key()).merge_patch(it.value());
  21151. }
  21152. }
  21153. }
  21154. else
  21155. {
  21156. *this = apply_patch;
  21157. }
  21158. }
  21159. /// @}
  21160. };
  21161. /// @brief user-defined to_string function for JSON values
  21162. /// @sa https://json.nlohmann.me/api/basic_json/to_string/
  21163. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  21164. std::string to_string(const NLOHMANN_BASIC_JSON_TPL& j)
  21165. {
  21166. return j.dump();
  21167. }
  21168. inline namespace literals
  21169. {
  21170. inline namespace json_literals
  21171. {
  21172. /// @brief user-defined string literal for JSON values
  21173. /// @sa https://json.nlohmann.me/api/basic_json/operator_literal_json/
  21174. JSON_HEDLEY_NON_NULL(1)
  21175. inline nlohmann::json operator "" _json(const char* s, std::size_t n)
  21176. {
  21177. return nlohmann::json::parse(s, s + n);
  21178. }
  21179. /// @brief user-defined string literal for JSON pointer
  21180. /// @sa https://json.nlohmann.me/api/basic_json/operator_literal_json_pointer/
  21181. JSON_HEDLEY_NON_NULL(1)
  21182. inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std::size_t n)
  21183. {
  21184. return nlohmann::json::json_pointer(std::string(s, n));
  21185. }
  21186. } // namespace json_literals
  21187. } // namespace literals
  21188. NLOHMANN_JSON_NAMESPACE_END
  21189. ///////////////////////
  21190. // nonmember support //
  21191. ///////////////////////
  21192. namespace std // NOLINT(cert-dcl58-cpp)
  21193. {
  21194. /// @brief hash value for JSON objects
  21195. /// @sa https://json.nlohmann.me/api/basic_json/std_hash/
  21196. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  21197. struct hash<nlohmann::NLOHMANN_BASIC_JSON_TPL>
  21198. {
  21199. std::size_t operator()(const nlohmann::NLOHMANN_BASIC_JSON_TPL& j) const
  21200. {
  21201. return nlohmann::detail::hash(j);
  21202. }
  21203. };
  21204. // specialization for std::less<value_t>
  21205. template<>
  21206. struct less< ::nlohmann::detail::value_t> // do not remove the space after '<', see https://github.com/nlohmann/json/pull/679
  21207. {
  21208. /*!
  21209. @brief compare two value_t enum values
  21210. @since version 3.0.0
  21211. */
  21212. bool operator()(::nlohmann::detail::value_t lhs,
  21213. ::nlohmann::detail::value_t rhs) const noexcept
  21214. {
  21215. #if JSON_HAS_THREE_WAY_COMPARISON
  21216. return std::is_lt(lhs <=> rhs); // *NOPAD*
  21217. #else
  21218. return ::nlohmann::detail::operator<(lhs, rhs);
  21219. #endif
  21220. }
  21221. };
  21222. // C++20 prohibit function specialization in the std namespace.
  21223. #ifndef JSON_HAS_CPP_20
  21224. /// @brief exchanges the values of two JSON objects
  21225. /// @sa https://json.nlohmann.me/api/basic_json/std_swap/
  21226. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  21227. inline void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL& j1, nlohmann::NLOHMANN_BASIC_JSON_TPL& j2) noexcept( // NOLINT(readability-inconsistent-declaration-parameter-name)
  21228. is_nothrow_move_constructible<nlohmann::NLOHMANN_BASIC_JSON_TPL>::value&& // NOLINT(misc-redundant-expression)
  21229. is_nothrow_move_assignable<nlohmann::NLOHMANN_BASIC_JSON_TPL>::value)
  21230. {
  21231. j1.swap(j2);
  21232. }
  21233. #endif
  21234. } // namespace std
  21235. #if JSON_USE_GLOBAL_UDLS
  21236. using nlohmann::literals::json_literals::operator "" _json; // NOLINT(misc-unused-using-decls,google-global-names-in-headers)
  21237. using nlohmann::literals::json_literals::operator "" _json_pointer; //NOLINT(misc-unused-using-decls,google-global-names-in-headers)
  21238. #endif
  21239. // #include <nlohmann/detail/macro_unscope.hpp>
  21240. // __ _____ _____ _____
  21241. // __| | __| | | | JSON for Modern C++
  21242. // | | |__ | | | | | | version 3.11.2
  21243. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  21244. //
  21245. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  21246. // SPDX-License-Identifier: MIT
  21247. // restore clang diagnostic settings
  21248. #if defined(__clang__)
  21249. #pragma clang diagnostic pop
  21250. #endif
  21251. // clean up
  21252. #undef JSON_ASSERT
  21253. #undef JSON_INTERNAL_CATCH
  21254. #undef JSON_THROW
  21255. #undef JSON_PRIVATE_UNLESS_TESTED
  21256. #undef NLOHMANN_BASIC_JSON_TPL_DECLARATION
  21257. #undef NLOHMANN_BASIC_JSON_TPL
  21258. #undef JSON_EXPLICIT
  21259. #undef NLOHMANN_CAN_CALL_STD_FUNC_IMPL
  21260. #undef JSON_INLINE_VARIABLE
  21261. #undef JSON_NO_UNIQUE_ADDRESS
  21262. #undef JSON_DISABLE_ENUM_SERIALIZATION
  21263. #undef JSON_USE_GLOBAL_UDLS
  21264. #ifndef JSON_TEST_KEEP_MACROS
  21265. #undef JSON_CATCH
  21266. #undef JSON_TRY
  21267. #undef JSON_HAS_CPP_11
  21268. #undef JSON_HAS_CPP_14
  21269. #undef JSON_HAS_CPP_17
  21270. #undef JSON_HAS_CPP_20
  21271. #undef JSON_HAS_FILESYSTEM
  21272. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  21273. #undef JSON_HAS_THREE_WAY_COMPARISON
  21274. #undef JSON_HAS_RANGES
  21275. #undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  21276. #endif
  21277. // #include <nlohmann/thirdparty/hedley/hedley_undef.hpp>
  21278. // __ _____ _____ _____
  21279. // __| | __| | | | JSON for Modern C++
  21280. // | | |__ | | | | | | version 3.11.2
  21281. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  21282. //
  21283. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  21284. // SPDX-License-Identifier: MIT
  21285. #undef JSON_HEDLEY_ALWAYS_INLINE
  21286. #undef JSON_HEDLEY_ARM_VERSION
  21287. #undef JSON_HEDLEY_ARM_VERSION_CHECK
  21288. #undef JSON_HEDLEY_ARRAY_PARAM
  21289. #undef JSON_HEDLEY_ASSUME
  21290. #undef JSON_HEDLEY_BEGIN_C_DECLS
  21291. #undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE
  21292. #undef JSON_HEDLEY_CLANG_HAS_BUILTIN
  21293. #undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE
  21294. #undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE
  21295. #undef JSON_HEDLEY_CLANG_HAS_EXTENSION
  21296. #undef JSON_HEDLEY_CLANG_HAS_FEATURE
  21297. #undef JSON_HEDLEY_CLANG_HAS_WARNING
  21298. #undef JSON_HEDLEY_COMPCERT_VERSION
  21299. #undef JSON_HEDLEY_COMPCERT_VERSION_CHECK
  21300. #undef JSON_HEDLEY_CONCAT
  21301. #undef JSON_HEDLEY_CONCAT3
  21302. #undef JSON_HEDLEY_CONCAT3_EX
  21303. #undef JSON_HEDLEY_CONCAT_EX
  21304. #undef JSON_HEDLEY_CONST
  21305. #undef JSON_HEDLEY_CONSTEXPR
  21306. #undef JSON_HEDLEY_CONST_CAST
  21307. #undef JSON_HEDLEY_CPP_CAST
  21308. #undef JSON_HEDLEY_CRAY_VERSION
  21309. #undef JSON_HEDLEY_CRAY_VERSION_CHECK
  21310. #undef JSON_HEDLEY_C_DECL
  21311. #undef JSON_HEDLEY_DEPRECATED
  21312. #undef JSON_HEDLEY_DEPRECATED_FOR
  21313. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
  21314. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_
  21315. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
  21316. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
  21317. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
  21318. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
  21319. #undef JSON_HEDLEY_DIAGNOSTIC_POP
  21320. #undef JSON_HEDLEY_DIAGNOSTIC_PUSH
  21321. #undef JSON_HEDLEY_DMC_VERSION
  21322. #undef JSON_HEDLEY_DMC_VERSION_CHECK
  21323. #undef JSON_HEDLEY_EMPTY_BASES
  21324. #undef JSON_HEDLEY_EMSCRIPTEN_VERSION
  21325. #undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK
  21326. #undef JSON_HEDLEY_END_C_DECLS
  21327. #undef JSON_HEDLEY_FLAGS
  21328. #undef JSON_HEDLEY_FLAGS_CAST
  21329. #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE
  21330. #undef JSON_HEDLEY_GCC_HAS_BUILTIN
  21331. #undef JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE
  21332. #undef JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE
  21333. #undef JSON_HEDLEY_GCC_HAS_EXTENSION
  21334. #undef JSON_HEDLEY_GCC_HAS_FEATURE
  21335. #undef JSON_HEDLEY_GCC_HAS_WARNING
  21336. #undef JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK
  21337. #undef JSON_HEDLEY_GCC_VERSION
  21338. #undef JSON_HEDLEY_GCC_VERSION_CHECK
  21339. #undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE
  21340. #undef JSON_HEDLEY_GNUC_HAS_BUILTIN
  21341. #undef JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE
  21342. #undef JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE
  21343. #undef JSON_HEDLEY_GNUC_HAS_EXTENSION
  21344. #undef JSON_HEDLEY_GNUC_HAS_FEATURE
  21345. #undef JSON_HEDLEY_GNUC_HAS_WARNING
  21346. #undef JSON_HEDLEY_GNUC_VERSION
  21347. #undef JSON_HEDLEY_GNUC_VERSION_CHECK
  21348. #undef JSON_HEDLEY_HAS_ATTRIBUTE
  21349. #undef JSON_HEDLEY_HAS_BUILTIN
  21350. #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE
  21351. #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS
  21352. #undef JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE
  21353. #undef JSON_HEDLEY_HAS_EXTENSION
  21354. #undef JSON_HEDLEY_HAS_FEATURE
  21355. #undef JSON_HEDLEY_HAS_WARNING
  21356. #undef JSON_HEDLEY_IAR_VERSION
  21357. #undef JSON_HEDLEY_IAR_VERSION_CHECK
  21358. #undef JSON_HEDLEY_IBM_VERSION
  21359. #undef JSON_HEDLEY_IBM_VERSION_CHECK
  21360. #undef JSON_HEDLEY_IMPORT
  21361. #undef JSON_HEDLEY_INLINE
  21362. #undef JSON_HEDLEY_INTEL_CL_VERSION
  21363. #undef JSON_HEDLEY_INTEL_CL_VERSION_CHECK
  21364. #undef JSON_HEDLEY_INTEL_VERSION
  21365. #undef JSON_HEDLEY_INTEL_VERSION_CHECK
  21366. #undef JSON_HEDLEY_IS_CONSTANT
  21367. #undef JSON_HEDLEY_IS_CONSTEXPR_
  21368. #undef JSON_HEDLEY_LIKELY
  21369. #undef JSON_HEDLEY_MALLOC
  21370. #undef JSON_HEDLEY_MCST_LCC_VERSION
  21371. #undef JSON_HEDLEY_MCST_LCC_VERSION_CHECK
  21372. #undef JSON_HEDLEY_MESSAGE
  21373. #undef JSON_HEDLEY_MSVC_VERSION
  21374. #undef JSON_HEDLEY_MSVC_VERSION_CHECK
  21375. #undef JSON_HEDLEY_NEVER_INLINE
  21376. #undef JSON_HEDLEY_NON_NULL
  21377. #undef JSON_HEDLEY_NO_ESCAPE
  21378. #undef JSON_HEDLEY_NO_RETURN
  21379. #undef JSON_HEDLEY_NO_THROW
  21380. #undef JSON_HEDLEY_NULL
  21381. #undef JSON_HEDLEY_PELLES_VERSION
  21382. #undef JSON_HEDLEY_PELLES_VERSION_CHECK
  21383. #undef JSON_HEDLEY_PGI_VERSION
  21384. #undef JSON_HEDLEY_PGI_VERSION_CHECK
  21385. #undef JSON_HEDLEY_PREDICT
  21386. #undef JSON_HEDLEY_PRINTF_FORMAT
  21387. #undef JSON_HEDLEY_PRIVATE
  21388. #undef JSON_HEDLEY_PUBLIC
  21389. #undef JSON_HEDLEY_PURE
  21390. #undef JSON_HEDLEY_REINTERPRET_CAST
  21391. #undef JSON_HEDLEY_REQUIRE
  21392. #undef JSON_HEDLEY_REQUIRE_CONSTEXPR
  21393. #undef JSON_HEDLEY_REQUIRE_MSG
  21394. #undef JSON_HEDLEY_RESTRICT
  21395. #undef JSON_HEDLEY_RETURNS_NON_NULL
  21396. #undef JSON_HEDLEY_SENTINEL
  21397. #undef JSON_HEDLEY_STATIC_ASSERT
  21398. #undef JSON_HEDLEY_STATIC_CAST
  21399. #undef JSON_HEDLEY_STRINGIFY
  21400. #undef JSON_HEDLEY_STRINGIFY_EX
  21401. #undef JSON_HEDLEY_SUNPRO_VERSION
  21402. #undef JSON_HEDLEY_SUNPRO_VERSION_CHECK
  21403. #undef JSON_HEDLEY_TINYC_VERSION
  21404. #undef JSON_HEDLEY_TINYC_VERSION_CHECK
  21405. #undef JSON_HEDLEY_TI_ARMCL_VERSION
  21406. #undef JSON_HEDLEY_TI_ARMCL_VERSION_CHECK
  21407. #undef JSON_HEDLEY_TI_CL2000_VERSION
  21408. #undef JSON_HEDLEY_TI_CL2000_VERSION_CHECK
  21409. #undef JSON_HEDLEY_TI_CL430_VERSION
  21410. #undef JSON_HEDLEY_TI_CL430_VERSION_CHECK
  21411. #undef JSON_HEDLEY_TI_CL6X_VERSION
  21412. #undef JSON_HEDLEY_TI_CL6X_VERSION_CHECK
  21413. #undef JSON_HEDLEY_TI_CL7X_VERSION
  21414. #undef JSON_HEDLEY_TI_CL7X_VERSION_CHECK
  21415. #undef JSON_HEDLEY_TI_CLPRU_VERSION
  21416. #undef JSON_HEDLEY_TI_CLPRU_VERSION_CHECK
  21417. #undef JSON_HEDLEY_TI_VERSION
  21418. #undef JSON_HEDLEY_TI_VERSION_CHECK
  21419. #undef JSON_HEDLEY_UNAVAILABLE
  21420. #undef JSON_HEDLEY_UNLIKELY
  21421. #undef JSON_HEDLEY_UNPREDICTABLE
  21422. #undef JSON_HEDLEY_UNREACHABLE
  21423. #undef JSON_HEDLEY_UNREACHABLE_RETURN
  21424. #undef JSON_HEDLEY_VERSION
  21425. #undef JSON_HEDLEY_VERSION_DECODE_MAJOR
  21426. #undef JSON_HEDLEY_VERSION_DECODE_MINOR
  21427. #undef JSON_HEDLEY_VERSION_DECODE_REVISION
  21428. #undef JSON_HEDLEY_VERSION_ENCODE
  21429. #undef JSON_HEDLEY_WARNING
  21430. #undef JSON_HEDLEY_WARN_UNUSED_RESULT
  21431. #undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG
  21432. #undef JSON_HEDLEY_FALL_THROUGH
  21433. #endif // INCLUDE_NLOHMANN_JSON_HPP_