json.hpp 856 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761
  1. /*
  2. __ _____ _____ _____
  3. __| | __| | | | JSON for Modern C++
  4. | | |__ | | | | | | version 3.10.5
  5. |_____|_____|_____|_|___| https://github.com/nlohmann/json
  6. Licensed under the MIT License <http://opensource.org/licenses/MIT>.
  7. SPDX-License-Identifier: MIT
  8. Copyright (c) 2013-2022 Niels Lohmann <http://nlohmann.me>.
  9. Permission is hereby granted, free of charge, to any person obtaining a copy
  10. of this software and associated documentation files (the "Software"), to deal
  11. in the Software without restriction, including without limitation the rights
  12. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. copies of the Software, and to permit persons to whom the Software is
  14. furnished to do so, subject to the following conditions:
  15. The above copyright notice and this permission notice shall be included in all
  16. copies or substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. SOFTWARE.
  24. */
  25. /****************************************************************************\
  26. * Note on documentation: The source files contain links to the online *
  27. * documentation of the public API at https://json.nlohmann.me. This URL *
  28. * contains the most recent documentation and should also be applicable to *
  29. * previous versions; documentation for deprecated functions is not *
  30. * removed, but marked deprecated. See "Generate documentation" section in *
  31. * file docs/README.md. *
  32. \****************************************************************************/
  33. #ifndef INCLUDE_NLOHMANN_JSON_HPP_
  34. #define INCLUDE_NLOHMANN_JSON_HPP_
  35. #ifndef JSON_SKIP_LIBRARY_VERSION_CHECK
  36. #if defined(NLOHMANN_JSON_VERSION_MAJOR) && defined(NLOHMANN_JSON_VERSION_MINOR) && defined(NLOHMANN_JSON_VERSION_PATCH)
  37. #if NLOHMANN_JSON_VERSION_MAJOR != 3 || NLOHMANN_JSON_VERSION_MINOR != 10 || NLOHMANN_JSON_VERSION_PATCH != 5
  38. #warning "Already included a different version of the library!"
  39. #endif
  40. #endif
  41. #endif
  42. #define NLOHMANN_JSON_VERSION_MAJOR 3 // NOLINT(modernize-macro-to-enum)
  43. #define NLOHMANN_JSON_VERSION_MINOR 10 // NOLINT(modernize-macro-to-enum)
  44. #define NLOHMANN_JSON_VERSION_PATCH 5 // NOLINT(modernize-macro-to-enum)
  45. #include <algorithm> // all_of, find, for_each
  46. #include <cstddef> // nullptr_t, ptrdiff_t, size_t
  47. #include <functional> // hash, less
  48. #include <initializer_list> // initializer_list
  49. #ifndef JSON_NO_IO
  50. #include <iosfwd> // istream, ostream
  51. #endif // JSON_NO_IO
  52. #include <iterator> // random_access_iterator_tag
  53. #include <memory> // unique_ptr
  54. #include <numeric> // accumulate
  55. #include <string> // string, stoi, to_string
  56. #include <utility> // declval, forward, move, pair, swap
  57. #include <vector> // vector
  58. // #include <nlohmann/adl_serializer.hpp>
  59. #include <type_traits>
  60. #include <utility>
  61. // #include <nlohmann/detail/conversions/from_json.hpp>
  62. #include <algorithm> // transform
  63. #include <array> // array
  64. #include <forward_list> // forward_list
  65. #include <iterator> // inserter, front_inserter, end
  66. #include <map> // map
  67. #include <string> // string
  68. #include <tuple> // tuple, make_tuple
  69. #include <type_traits> // is_arithmetic, is_same, is_enum, underlying_type, is_convertible
  70. #include <unordered_map> // unordered_map
  71. #include <utility> // pair, declval
  72. #include <valarray> // valarray
  73. // #include <nlohmann/detail/exceptions.hpp>
  74. #include <cstddef> // nullptr_t
  75. #include <exception> // exception
  76. #include <stdexcept> // runtime_error
  77. #include <string> // to_string
  78. #include <vector> // vector
  79. // #include <nlohmann/detail/value_t.hpp>
  80. #include <array> // array
  81. #include <cstddef> // size_t
  82. #include <cstdint> // uint8_t
  83. #include <string> // string
  84. // #include <nlohmann/detail/macro_scope.hpp>
  85. #include <utility> // declval, pair
  86. // #include <nlohmann/thirdparty/hedley/hedley.hpp>
  87. /* Hedley - https://nemequ.github.io/hedley
  88. * Created by Evan Nemerson <evan@nemerson.com>
  89. *
  90. * To the extent possible under law, the author(s) have dedicated all
  91. * copyright and related and neighboring rights to this software to
  92. * the public domain worldwide. This software is distributed without
  93. * any warranty.
  94. *
  95. * For details, see <http://creativecommons.org/publicdomain/zero/1.0/>.
  96. * SPDX-License-Identifier: CC0-1.0
  97. */
  98. #if !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < 15)
  99. #if defined(JSON_HEDLEY_VERSION)
  100. #undef JSON_HEDLEY_VERSION
  101. #endif
  102. #define JSON_HEDLEY_VERSION 15
  103. #if defined(JSON_HEDLEY_STRINGIFY_EX)
  104. #undef JSON_HEDLEY_STRINGIFY_EX
  105. #endif
  106. #define JSON_HEDLEY_STRINGIFY_EX(x) #x
  107. #if defined(JSON_HEDLEY_STRINGIFY)
  108. #undef JSON_HEDLEY_STRINGIFY
  109. #endif
  110. #define JSON_HEDLEY_STRINGIFY(x) JSON_HEDLEY_STRINGIFY_EX(x)
  111. #if defined(JSON_HEDLEY_CONCAT_EX)
  112. #undef JSON_HEDLEY_CONCAT_EX
  113. #endif
  114. #define JSON_HEDLEY_CONCAT_EX(a,b) a##b
  115. #if defined(JSON_HEDLEY_CONCAT)
  116. #undef JSON_HEDLEY_CONCAT
  117. #endif
  118. #define JSON_HEDLEY_CONCAT(a,b) JSON_HEDLEY_CONCAT_EX(a,b)
  119. #if defined(JSON_HEDLEY_CONCAT3_EX)
  120. #undef JSON_HEDLEY_CONCAT3_EX
  121. #endif
  122. #define JSON_HEDLEY_CONCAT3_EX(a,b,c) a##b##c
  123. #if defined(JSON_HEDLEY_CONCAT3)
  124. #undef JSON_HEDLEY_CONCAT3
  125. #endif
  126. #define JSON_HEDLEY_CONCAT3(a,b,c) JSON_HEDLEY_CONCAT3_EX(a,b,c)
  127. #if defined(JSON_HEDLEY_VERSION_ENCODE)
  128. #undef JSON_HEDLEY_VERSION_ENCODE
  129. #endif
  130. #define JSON_HEDLEY_VERSION_ENCODE(major,minor,revision) (((major) * 1000000) + ((minor) * 1000) + (revision))
  131. #if defined(JSON_HEDLEY_VERSION_DECODE_MAJOR)
  132. #undef JSON_HEDLEY_VERSION_DECODE_MAJOR
  133. #endif
  134. #define JSON_HEDLEY_VERSION_DECODE_MAJOR(version) ((version) / 1000000)
  135. #if defined(JSON_HEDLEY_VERSION_DECODE_MINOR)
  136. #undef JSON_HEDLEY_VERSION_DECODE_MINOR
  137. #endif
  138. #define JSON_HEDLEY_VERSION_DECODE_MINOR(version) (((version) % 1000000) / 1000)
  139. #if defined(JSON_HEDLEY_VERSION_DECODE_REVISION)
  140. #undef JSON_HEDLEY_VERSION_DECODE_REVISION
  141. #endif
  142. #define JSON_HEDLEY_VERSION_DECODE_REVISION(version) ((version) % 1000)
  143. #if defined(JSON_HEDLEY_GNUC_VERSION)
  144. #undef JSON_HEDLEY_GNUC_VERSION
  145. #endif
  146. #if defined(__GNUC__) && defined(__GNUC_PATCHLEVEL__)
  147. #define JSON_HEDLEY_GNUC_VERSION JSON_HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
  148. #elif defined(__GNUC__)
  149. #define JSON_HEDLEY_GNUC_VERSION JSON_HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, 0)
  150. #endif
  151. #if defined(JSON_HEDLEY_GNUC_VERSION_CHECK)
  152. #undef JSON_HEDLEY_GNUC_VERSION_CHECK
  153. #endif
  154. #if defined(JSON_HEDLEY_GNUC_VERSION)
  155. #define JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_GNUC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  156. #else
  157. #define JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) (0)
  158. #endif
  159. #if defined(JSON_HEDLEY_MSVC_VERSION)
  160. #undef JSON_HEDLEY_MSVC_VERSION
  161. #endif
  162. #if defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 140000000) && !defined(__ICL)
  163. #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 10000000, (_MSC_FULL_VER % 10000000) / 100000, (_MSC_FULL_VER % 100000) / 100)
  164. #elif defined(_MSC_FULL_VER) && !defined(__ICL)
  165. #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 1000000, (_MSC_FULL_VER % 1000000) / 10000, (_MSC_FULL_VER % 10000) / 10)
  166. #elif defined(_MSC_VER) && !defined(__ICL)
  167. #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_VER / 100, _MSC_VER % 100, 0)
  168. #endif
  169. #if defined(JSON_HEDLEY_MSVC_VERSION_CHECK)
  170. #undef JSON_HEDLEY_MSVC_VERSION_CHECK
  171. #endif
  172. #if !defined(JSON_HEDLEY_MSVC_VERSION)
  173. #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (0)
  174. #elif defined(_MSC_VER) && (_MSC_VER >= 1400)
  175. #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 10000000) + (minor * 100000) + (patch)))
  176. #elif defined(_MSC_VER) && (_MSC_VER >= 1200)
  177. #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 1000000) + (minor * 10000) + (patch)))
  178. #else
  179. #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_VER >= ((major * 100) + (minor)))
  180. #endif
  181. #if defined(JSON_HEDLEY_INTEL_VERSION)
  182. #undef JSON_HEDLEY_INTEL_VERSION
  183. #endif
  184. #if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) && !defined(__ICL)
  185. #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, __INTEL_COMPILER_UPDATE)
  186. #elif defined(__INTEL_COMPILER) && !defined(__ICL)
  187. #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, 0)
  188. #endif
  189. #if defined(JSON_HEDLEY_INTEL_VERSION_CHECK)
  190. #undef JSON_HEDLEY_INTEL_VERSION_CHECK
  191. #endif
  192. #if defined(JSON_HEDLEY_INTEL_VERSION)
  193. #define JSON_HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_INTEL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  194. #else
  195. #define JSON_HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (0)
  196. #endif
  197. #if defined(JSON_HEDLEY_INTEL_CL_VERSION)
  198. #undef JSON_HEDLEY_INTEL_CL_VERSION
  199. #endif
  200. #if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) && defined(__ICL)
  201. #define JSON_HEDLEY_INTEL_CL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER, __INTEL_COMPILER_UPDATE, 0)
  202. #endif
  203. #if defined(JSON_HEDLEY_INTEL_CL_VERSION_CHECK)
  204. #undef JSON_HEDLEY_INTEL_CL_VERSION_CHECK
  205. #endif
  206. #if defined(JSON_HEDLEY_INTEL_CL_VERSION)
  207. #define JSON_HEDLEY_INTEL_CL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_INTEL_CL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  208. #else
  209. #define JSON_HEDLEY_INTEL_CL_VERSION_CHECK(major,minor,patch) (0)
  210. #endif
  211. #if defined(JSON_HEDLEY_PGI_VERSION)
  212. #undef JSON_HEDLEY_PGI_VERSION
  213. #endif
  214. #if defined(__PGI) && defined(__PGIC__) && defined(__PGIC_MINOR__) && defined(__PGIC_PATCHLEVEL__)
  215. #define JSON_HEDLEY_PGI_VERSION JSON_HEDLEY_VERSION_ENCODE(__PGIC__, __PGIC_MINOR__, __PGIC_PATCHLEVEL__)
  216. #endif
  217. #if defined(JSON_HEDLEY_PGI_VERSION_CHECK)
  218. #undef JSON_HEDLEY_PGI_VERSION_CHECK
  219. #endif
  220. #if defined(JSON_HEDLEY_PGI_VERSION)
  221. #define JSON_HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_PGI_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  222. #else
  223. #define JSON_HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (0)
  224. #endif
  225. #if defined(JSON_HEDLEY_SUNPRO_VERSION)
  226. #undef JSON_HEDLEY_SUNPRO_VERSION
  227. #endif
  228. #if defined(__SUNPRO_C) && (__SUNPRO_C > 0x1000)
  229. #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)
  230. #elif defined(__SUNPRO_C)
  231. #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((__SUNPRO_C >> 8) & 0xf, (__SUNPRO_C >> 4) & 0xf, (__SUNPRO_C) & 0xf)
  232. #elif defined(__SUNPRO_CC) && (__SUNPRO_CC > 0x1000)
  233. #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)
  234. #elif defined(__SUNPRO_CC)
  235. #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((__SUNPRO_CC >> 8) & 0xf, (__SUNPRO_CC >> 4) & 0xf, (__SUNPRO_CC) & 0xf)
  236. #endif
  237. #if defined(JSON_HEDLEY_SUNPRO_VERSION_CHECK)
  238. #undef JSON_HEDLEY_SUNPRO_VERSION_CHECK
  239. #endif
  240. #if defined(JSON_HEDLEY_SUNPRO_VERSION)
  241. #define JSON_HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_SUNPRO_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  242. #else
  243. #define JSON_HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) (0)
  244. #endif
  245. #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION)
  246. #undef JSON_HEDLEY_EMSCRIPTEN_VERSION
  247. #endif
  248. #if defined(__EMSCRIPTEN__)
  249. #define JSON_HEDLEY_EMSCRIPTEN_VERSION JSON_HEDLEY_VERSION_ENCODE(__EMSCRIPTEN_major__, __EMSCRIPTEN_minor__, __EMSCRIPTEN_tiny__)
  250. #endif
  251. #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK)
  252. #undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK
  253. #endif
  254. #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION)
  255. #define JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_EMSCRIPTEN_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  256. #else
  257. #define JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) (0)
  258. #endif
  259. #if defined(JSON_HEDLEY_ARM_VERSION)
  260. #undef JSON_HEDLEY_ARM_VERSION
  261. #endif
  262. #if defined(__CC_ARM) && defined(__ARMCOMPILER_VERSION)
  263. #define JSON_HEDLEY_ARM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ARMCOMPILER_VERSION / 1000000, (__ARMCOMPILER_VERSION % 1000000) / 10000, (__ARMCOMPILER_VERSION % 10000) / 100)
  264. #elif defined(__CC_ARM) && defined(__ARMCC_VERSION)
  265. #define JSON_HEDLEY_ARM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ARMCC_VERSION / 1000000, (__ARMCC_VERSION % 1000000) / 10000, (__ARMCC_VERSION % 10000) / 100)
  266. #endif
  267. #if defined(JSON_HEDLEY_ARM_VERSION_CHECK)
  268. #undef JSON_HEDLEY_ARM_VERSION_CHECK
  269. #endif
  270. #if defined(JSON_HEDLEY_ARM_VERSION)
  271. #define JSON_HEDLEY_ARM_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_ARM_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  272. #else
  273. #define JSON_HEDLEY_ARM_VERSION_CHECK(major,minor,patch) (0)
  274. #endif
  275. #if defined(JSON_HEDLEY_IBM_VERSION)
  276. #undef JSON_HEDLEY_IBM_VERSION
  277. #endif
  278. #if defined(__ibmxl__)
  279. #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ibmxl_version__, __ibmxl_release__, __ibmxl_modification__)
  280. #elif defined(__xlC__) && defined(__xlC_ver__)
  281. #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__xlC__ >> 8, __xlC__ & 0xff, (__xlC_ver__ >> 8) & 0xff)
  282. #elif defined(__xlC__)
  283. #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__xlC__ >> 8, __xlC__ & 0xff, 0)
  284. #endif
  285. #if defined(JSON_HEDLEY_IBM_VERSION_CHECK)
  286. #undef JSON_HEDLEY_IBM_VERSION_CHECK
  287. #endif
  288. #if defined(JSON_HEDLEY_IBM_VERSION)
  289. #define JSON_HEDLEY_IBM_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_IBM_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  290. #else
  291. #define JSON_HEDLEY_IBM_VERSION_CHECK(major,minor,patch) (0)
  292. #endif
  293. #if defined(JSON_HEDLEY_TI_VERSION)
  294. #undef JSON_HEDLEY_TI_VERSION
  295. #endif
  296. #if \
  297. defined(__TI_COMPILER_VERSION__) && \
  298. ( \
  299. defined(__TMS470__) || defined(__TI_ARM__) || \
  300. defined(__MSP430__) || \
  301. defined(__TMS320C2000__) \
  302. )
  303. #if (__TI_COMPILER_VERSION__ >= 16000000)
  304. #define JSON_HEDLEY_TI_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  305. #endif
  306. #endif
  307. #if defined(JSON_HEDLEY_TI_VERSION_CHECK)
  308. #undef JSON_HEDLEY_TI_VERSION_CHECK
  309. #endif
  310. #if defined(JSON_HEDLEY_TI_VERSION)
  311. #define JSON_HEDLEY_TI_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  312. #else
  313. #define JSON_HEDLEY_TI_VERSION_CHECK(major,minor,patch) (0)
  314. #endif
  315. #if defined(JSON_HEDLEY_TI_CL2000_VERSION)
  316. #undef JSON_HEDLEY_TI_CL2000_VERSION
  317. #endif
  318. #if defined(__TI_COMPILER_VERSION__) && defined(__TMS320C2000__)
  319. #define JSON_HEDLEY_TI_CL2000_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  320. #endif
  321. #if defined(JSON_HEDLEY_TI_CL2000_VERSION_CHECK)
  322. #undef JSON_HEDLEY_TI_CL2000_VERSION_CHECK
  323. #endif
  324. #if defined(JSON_HEDLEY_TI_CL2000_VERSION)
  325. #define JSON_HEDLEY_TI_CL2000_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL2000_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  326. #else
  327. #define JSON_HEDLEY_TI_CL2000_VERSION_CHECK(major,minor,patch) (0)
  328. #endif
  329. #if defined(JSON_HEDLEY_TI_CL430_VERSION)
  330. #undef JSON_HEDLEY_TI_CL430_VERSION
  331. #endif
  332. #if defined(__TI_COMPILER_VERSION__) && defined(__MSP430__)
  333. #define JSON_HEDLEY_TI_CL430_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  334. #endif
  335. #if defined(JSON_HEDLEY_TI_CL430_VERSION_CHECK)
  336. #undef JSON_HEDLEY_TI_CL430_VERSION_CHECK
  337. #endif
  338. #if defined(JSON_HEDLEY_TI_CL430_VERSION)
  339. #define JSON_HEDLEY_TI_CL430_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL430_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  340. #else
  341. #define JSON_HEDLEY_TI_CL430_VERSION_CHECK(major,minor,patch) (0)
  342. #endif
  343. #if defined(JSON_HEDLEY_TI_ARMCL_VERSION)
  344. #undef JSON_HEDLEY_TI_ARMCL_VERSION
  345. #endif
  346. #if defined(__TI_COMPILER_VERSION__) && (defined(__TMS470__) || defined(__TI_ARM__))
  347. #define JSON_HEDLEY_TI_ARMCL_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  348. #endif
  349. #if defined(JSON_HEDLEY_TI_ARMCL_VERSION_CHECK)
  350. #undef JSON_HEDLEY_TI_ARMCL_VERSION_CHECK
  351. #endif
  352. #if defined(JSON_HEDLEY_TI_ARMCL_VERSION)
  353. #define JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_ARMCL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  354. #else
  355. #define JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(major,minor,patch) (0)
  356. #endif
  357. #if defined(JSON_HEDLEY_TI_CL6X_VERSION)
  358. #undef JSON_HEDLEY_TI_CL6X_VERSION
  359. #endif
  360. #if defined(__TI_COMPILER_VERSION__) && defined(__TMS320C6X__)
  361. #define JSON_HEDLEY_TI_CL6X_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  362. #endif
  363. #if defined(JSON_HEDLEY_TI_CL6X_VERSION_CHECK)
  364. #undef JSON_HEDLEY_TI_CL6X_VERSION_CHECK
  365. #endif
  366. #if defined(JSON_HEDLEY_TI_CL6X_VERSION)
  367. #define JSON_HEDLEY_TI_CL6X_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL6X_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  368. #else
  369. #define JSON_HEDLEY_TI_CL6X_VERSION_CHECK(major,minor,patch) (0)
  370. #endif
  371. #if defined(JSON_HEDLEY_TI_CL7X_VERSION)
  372. #undef JSON_HEDLEY_TI_CL7X_VERSION
  373. #endif
  374. #if defined(__TI_COMPILER_VERSION__) && defined(__C7000__)
  375. #define JSON_HEDLEY_TI_CL7X_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  376. #endif
  377. #if defined(JSON_HEDLEY_TI_CL7X_VERSION_CHECK)
  378. #undef JSON_HEDLEY_TI_CL7X_VERSION_CHECK
  379. #endif
  380. #if defined(JSON_HEDLEY_TI_CL7X_VERSION)
  381. #define JSON_HEDLEY_TI_CL7X_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL7X_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  382. #else
  383. #define JSON_HEDLEY_TI_CL7X_VERSION_CHECK(major,minor,patch) (0)
  384. #endif
  385. #if defined(JSON_HEDLEY_TI_CLPRU_VERSION)
  386. #undef JSON_HEDLEY_TI_CLPRU_VERSION
  387. #endif
  388. #if defined(__TI_COMPILER_VERSION__) && defined(__PRU__)
  389. #define JSON_HEDLEY_TI_CLPRU_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  390. #endif
  391. #if defined(JSON_HEDLEY_TI_CLPRU_VERSION_CHECK)
  392. #undef JSON_HEDLEY_TI_CLPRU_VERSION_CHECK
  393. #endif
  394. #if defined(JSON_HEDLEY_TI_CLPRU_VERSION)
  395. #define JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CLPRU_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  396. #else
  397. #define JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(major,minor,patch) (0)
  398. #endif
  399. #if defined(JSON_HEDLEY_CRAY_VERSION)
  400. #undef JSON_HEDLEY_CRAY_VERSION
  401. #endif
  402. #if defined(_CRAYC)
  403. #if defined(_RELEASE_PATCHLEVEL)
  404. #define JSON_HEDLEY_CRAY_VERSION JSON_HEDLEY_VERSION_ENCODE(_RELEASE_MAJOR, _RELEASE_MINOR, _RELEASE_PATCHLEVEL)
  405. #else
  406. #define JSON_HEDLEY_CRAY_VERSION JSON_HEDLEY_VERSION_ENCODE(_RELEASE_MAJOR, _RELEASE_MINOR, 0)
  407. #endif
  408. #endif
  409. #if defined(JSON_HEDLEY_CRAY_VERSION_CHECK)
  410. #undef JSON_HEDLEY_CRAY_VERSION_CHECK
  411. #endif
  412. #if defined(JSON_HEDLEY_CRAY_VERSION)
  413. #define JSON_HEDLEY_CRAY_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_CRAY_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  414. #else
  415. #define JSON_HEDLEY_CRAY_VERSION_CHECK(major,minor,patch) (0)
  416. #endif
  417. #if defined(JSON_HEDLEY_IAR_VERSION)
  418. #undef JSON_HEDLEY_IAR_VERSION
  419. #endif
  420. #if defined(__IAR_SYSTEMS_ICC__)
  421. #if __VER__ > 1000
  422. #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE((__VER__ / 1000000), ((__VER__ / 1000) % 1000), (__VER__ % 1000))
  423. #else
  424. #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE(__VER__ / 100, __VER__ % 100, 0)
  425. #endif
  426. #endif
  427. #if defined(JSON_HEDLEY_IAR_VERSION_CHECK)
  428. #undef JSON_HEDLEY_IAR_VERSION_CHECK
  429. #endif
  430. #if defined(JSON_HEDLEY_IAR_VERSION)
  431. #define JSON_HEDLEY_IAR_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_IAR_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  432. #else
  433. #define JSON_HEDLEY_IAR_VERSION_CHECK(major,minor,patch) (0)
  434. #endif
  435. #if defined(JSON_HEDLEY_TINYC_VERSION)
  436. #undef JSON_HEDLEY_TINYC_VERSION
  437. #endif
  438. #if defined(__TINYC__)
  439. #define JSON_HEDLEY_TINYC_VERSION JSON_HEDLEY_VERSION_ENCODE(__TINYC__ / 1000, (__TINYC__ / 100) % 10, __TINYC__ % 100)
  440. #endif
  441. #if defined(JSON_HEDLEY_TINYC_VERSION_CHECK)
  442. #undef JSON_HEDLEY_TINYC_VERSION_CHECK
  443. #endif
  444. #if defined(JSON_HEDLEY_TINYC_VERSION)
  445. #define JSON_HEDLEY_TINYC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TINYC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  446. #else
  447. #define JSON_HEDLEY_TINYC_VERSION_CHECK(major,minor,patch) (0)
  448. #endif
  449. #if defined(JSON_HEDLEY_DMC_VERSION)
  450. #undef JSON_HEDLEY_DMC_VERSION
  451. #endif
  452. #if defined(__DMC__)
  453. #define JSON_HEDLEY_DMC_VERSION JSON_HEDLEY_VERSION_ENCODE(__DMC__ >> 8, (__DMC__ >> 4) & 0xf, __DMC__ & 0xf)
  454. #endif
  455. #if defined(JSON_HEDLEY_DMC_VERSION_CHECK)
  456. #undef JSON_HEDLEY_DMC_VERSION_CHECK
  457. #endif
  458. #if defined(JSON_HEDLEY_DMC_VERSION)
  459. #define JSON_HEDLEY_DMC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_DMC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  460. #else
  461. #define JSON_HEDLEY_DMC_VERSION_CHECK(major,minor,patch) (0)
  462. #endif
  463. #if defined(JSON_HEDLEY_COMPCERT_VERSION)
  464. #undef JSON_HEDLEY_COMPCERT_VERSION
  465. #endif
  466. #if defined(__COMPCERT_VERSION__)
  467. #define JSON_HEDLEY_COMPCERT_VERSION JSON_HEDLEY_VERSION_ENCODE(__COMPCERT_VERSION__ / 10000, (__COMPCERT_VERSION__ / 100) % 100, __COMPCERT_VERSION__ % 100)
  468. #endif
  469. #if defined(JSON_HEDLEY_COMPCERT_VERSION_CHECK)
  470. #undef JSON_HEDLEY_COMPCERT_VERSION_CHECK
  471. #endif
  472. #if defined(JSON_HEDLEY_COMPCERT_VERSION)
  473. #define JSON_HEDLEY_COMPCERT_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_COMPCERT_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  474. #else
  475. #define JSON_HEDLEY_COMPCERT_VERSION_CHECK(major,minor,patch) (0)
  476. #endif
  477. #if defined(JSON_HEDLEY_PELLES_VERSION)
  478. #undef JSON_HEDLEY_PELLES_VERSION
  479. #endif
  480. #if defined(__POCC__)
  481. #define JSON_HEDLEY_PELLES_VERSION JSON_HEDLEY_VERSION_ENCODE(__POCC__ / 100, __POCC__ % 100, 0)
  482. #endif
  483. #if defined(JSON_HEDLEY_PELLES_VERSION_CHECK)
  484. #undef JSON_HEDLEY_PELLES_VERSION_CHECK
  485. #endif
  486. #if defined(JSON_HEDLEY_PELLES_VERSION)
  487. #define JSON_HEDLEY_PELLES_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_PELLES_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  488. #else
  489. #define JSON_HEDLEY_PELLES_VERSION_CHECK(major,minor,patch) (0)
  490. #endif
  491. #if defined(JSON_HEDLEY_MCST_LCC_VERSION)
  492. #undef JSON_HEDLEY_MCST_LCC_VERSION
  493. #endif
  494. #if defined(__LCC__) && defined(__LCC_MINOR__)
  495. #define JSON_HEDLEY_MCST_LCC_VERSION JSON_HEDLEY_VERSION_ENCODE(__LCC__ / 100, __LCC__ % 100, __LCC_MINOR__)
  496. #endif
  497. #if defined(JSON_HEDLEY_MCST_LCC_VERSION_CHECK)
  498. #undef JSON_HEDLEY_MCST_LCC_VERSION_CHECK
  499. #endif
  500. #if defined(JSON_HEDLEY_MCST_LCC_VERSION)
  501. #define JSON_HEDLEY_MCST_LCC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_MCST_LCC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  502. #else
  503. #define JSON_HEDLEY_MCST_LCC_VERSION_CHECK(major,minor,patch) (0)
  504. #endif
  505. #if defined(JSON_HEDLEY_GCC_VERSION)
  506. #undef JSON_HEDLEY_GCC_VERSION
  507. #endif
  508. #if \
  509. defined(JSON_HEDLEY_GNUC_VERSION) && \
  510. !defined(__clang__) && \
  511. !defined(JSON_HEDLEY_INTEL_VERSION) && \
  512. !defined(JSON_HEDLEY_PGI_VERSION) && \
  513. !defined(JSON_HEDLEY_ARM_VERSION) && \
  514. !defined(JSON_HEDLEY_CRAY_VERSION) && \
  515. !defined(JSON_HEDLEY_TI_VERSION) && \
  516. !defined(JSON_HEDLEY_TI_ARMCL_VERSION) && \
  517. !defined(JSON_HEDLEY_TI_CL430_VERSION) && \
  518. !defined(JSON_HEDLEY_TI_CL2000_VERSION) && \
  519. !defined(JSON_HEDLEY_TI_CL6X_VERSION) && \
  520. !defined(JSON_HEDLEY_TI_CL7X_VERSION) && \
  521. !defined(JSON_HEDLEY_TI_CLPRU_VERSION) && \
  522. !defined(__COMPCERT__) && \
  523. !defined(JSON_HEDLEY_MCST_LCC_VERSION)
  524. #define JSON_HEDLEY_GCC_VERSION JSON_HEDLEY_GNUC_VERSION
  525. #endif
  526. #if defined(JSON_HEDLEY_GCC_VERSION_CHECK)
  527. #undef JSON_HEDLEY_GCC_VERSION_CHECK
  528. #endif
  529. #if defined(JSON_HEDLEY_GCC_VERSION)
  530. #define JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_GCC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  531. #else
  532. #define JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (0)
  533. #endif
  534. #if defined(JSON_HEDLEY_HAS_ATTRIBUTE)
  535. #undef JSON_HEDLEY_HAS_ATTRIBUTE
  536. #endif
  537. #if \
  538. defined(__has_attribute) && \
  539. ( \
  540. (!defined(JSON_HEDLEY_IAR_VERSION) || JSON_HEDLEY_IAR_VERSION_CHECK(8,5,9)) \
  541. )
  542. # define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) __has_attribute(attribute)
  543. #else
  544. # define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) (0)
  545. #endif
  546. #if defined(JSON_HEDLEY_GNUC_HAS_ATTRIBUTE)
  547. #undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE
  548. #endif
  549. #if defined(__has_attribute)
  550. #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
  551. #else
  552. #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  553. #endif
  554. #if defined(JSON_HEDLEY_GCC_HAS_ATTRIBUTE)
  555. #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE
  556. #endif
  557. #if defined(__has_attribute)
  558. #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
  559. #else
  560. #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  561. #endif
  562. #if defined(JSON_HEDLEY_HAS_CPP_ATTRIBUTE)
  563. #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE
  564. #endif
  565. #if \
  566. defined(__has_cpp_attribute) && \
  567. defined(__cplusplus) && \
  568. (!defined(JSON_HEDLEY_SUNPRO_VERSION) || JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0))
  569. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) __has_cpp_attribute(attribute)
  570. #else
  571. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) (0)
  572. #endif
  573. #if defined(JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS)
  574. #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS
  575. #endif
  576. #if !defined(__cplusplus) || !defined(__has_cpp_attribute)
  577. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) (0)
  578. #elif \
  579. !defined(JSON_HEDLEY_PGI_VERSION) && \
  580. !defined(JSON_HEDLEY_IAR_VERSION) && \
  581. (!defined(JSON_HEDLEY_SUNPRO_VERSION) || JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0)) && \
  582. (!defined(JSON_HEDLEY_MSVC_VERSION) || JSON_HEDLEY_MSVC_VERSION_CHECK(19,20,0))
  583. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) JSON_HEDLEY_HAS_CPP_ATTRIBUTE(ns::attribute)
  584. #else
  585. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) (0)
  586. #endif
  587. #if defined(JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE)
  588. #undef JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE
  589. #endif
  590. #if defined(__has_cpp_attribute) && defined(__cplusplus)
  591. #define JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) __has_cpp_attribute(attribute)
  592. #else
  593. #define JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  594. #endif
  595. #if defined(JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE)
  596. #undef JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE
  597. #endif
  598. #if defined(__has_cpp_attribute) && defined(__cplusplus)
  599. #define JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) __has_cpp_attribute(attribute)
  600. #else
  601. #define JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  602. #endif
  603. #if defined(JSON_HEDLEY_HAS_BUILTIN)
  604. #undef JSON_HEDLEY_HAS_BUILTIN
  605. #endif
  606. #if defined(__has_builtin)
  607. #define JSON_HEDLEY_HAS_BUILTIN(builtin) __has_builtin(builtin)
  608. #else
  609. #define JSON_HEDLEY_HAS_BUILTIN(builtin) (0)
  610. #endif
  611. #if defined(JSON_HEDLEY_GNUC_HAS_BUILTIN)
  612. #undef JSON_HEDLEY_GNUC_HAS_BUILTIN
  613. #endif
  614. #if defined(__has_builtin)
  615. #define JSON_HEDLEY_GNUC_HAS_BUILTIN(builtin,major,minor,patch) __has_builtin(builtin)
  616. #else
  617. #define JSON_HEDLEY_GNUC_HAS_BUILTIN(builtin,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  618. #endif
  619. #if defined(JSON_HEDLEY_GCC_HAS_BUILTIN)
  620. #undef JSON_HEDLEY_GCC_HAS_BUILTIN
  621. #endif
  622. #if defined(__has_builtin)
  623. #define JSON_HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) __has_builtin(builtin)
  624. #else
  625. #define JSON_HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  626. #endif
  627. #if defined(JSON_HEDLEY_HAS_FEATURE)
  628. #undef JSON_HEDLEY_HAS_FEATURE
  629. #endif
  630. #if defined(__has_feature)
  631. #define JSON_HEDLEY_HAS_FEATURE(feature) __has_feature(feature)
  632. #else
  633. #define JSON_HEDLEY_HAS_FEATURE(feature) (0)
  634. #endif
  635. #if defined(JSON_HEDLEY_GNUC_HAS_FEATURE)
  636. #undef JSON_HEDLEY_GNUC_HAS_FEATURE
  637. #endif
  638. #if defined(__has_feature)
  639. #define JSON_HEDLEY_GNUC_HAS_FEATURE(feature,major,minor,patch) __has_feature(feature)
  640. #else
  641. #define JSON_HEDLEY_GNUC_HAS_FEATURE(feature,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  642. #endif
  643. #if defined(JSON_HEDLEY_GCC_HAS_FEATURE)
  644. #undef JSON_HEDLEY_GCC_HAS_FEATURE
  645. #endif
  646. #if defined(__has_feature)
  647. #define JSON_HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) __has_feature(feature)
  648. #else
  649. #define JSON_HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  650. #endif
  651. #if defined(JSON_HEDLEY_HAS_EXTENSION)
  652. #undef JSON_HEDLEY_HAS_EXTENSION
  653. #endif
  654. #if defined(__has_extension)
  655. #define JSON_HEDLEY_HAS_EXTENSION(extension) __has_extension(extension)
  656. #else
  657. #define JSON_HEDLEY_HAS_EXTENSION(extension) (0)
  658. #endif
  659. #if defined(JSON_HEDLEY_GNUC_HAS_EXTENSION)
  660. #undef JSON_HEDLEY_GNUC_HAS_EXTENSION
  661. #endif
  662. #if defined(__has_extension)
  663. #define JSON_HEDLEY_GNUC_HAS_EXTENSION(extension,major,minor,patch) __has_extension(extension)
  664. #else
  665. #define JSON_HEDLEY_GNUC_HAS_EXTENSION(extension,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  666. #endif
  667. #if defined(JSON_HEDLEY_GCC_HAS_EXTENSION)
  668. #undef JSON_HEDLEY_GCC_HAS_EXTENSION
  669. #endif
  670. #if defined(__has_extension)
  671. #define JSON_HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) __has_extension(extension)
  672. #else
  673. #define JSON_HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  674. #endif
  675. #if defined(JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE)
  676. #undef JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE
  677. #endif
  678. #if defined(__has_declspec_attribute)
  679. #define JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) __has_declspec_attribute(attribute)
  680. #else
  681. #define JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) (0)
  682. #endif
  683. #if defined(JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE)
  684. #undef JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE
  685. #endif
  686. #if defined(__has_declspec_attribute)
  687. #define JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) __has_declspec_attribute(attribute)
  688. #else
  689. #define JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  690. #endif
  691. #if defined(JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE)
  692. #undef JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE
  693. #endif
  694. #if defined(__has_declspec_attribute)
  695. #define JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) __has_declspec_attribute(attribute)
  696. #else
  697. #define JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  698. #endif
  699. #if defined(JSON_HEDLEY_HAS_WARNING)
  700. #undef JSON_HEDLEY_HAS_WARNING
  701. #endif
  702. #if defined(__has_warning)
  703. #define JSON_HEDLEY_HAS_WARNING(warning) __has_warning(warning)
  704. #else
  705. #define JSON_HEDLEY_HAS_WARNING(warning) (0)
  706. #endif
  707. #if defined(JSON_HEDLEY_GNUC_HAS_WARNING)
  708. #undef JSON_HEDLEY_GNUC_HAS_WARNING
  709. #endif
  710. #if defined(__has_warning)
  711. #define JSON_HEDLEY_GNUC_HAS_WARNING(warning,major,minor,patch) __has_warning(warning)
  712. #else
  713. #define JSON_HEDLEY_GNUC_HAS_WARNING(warning,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  714. #endif
  715. #if defined(JSON_HEDLEY_GCC_HAS_WARNING)
  716. #undef JSON_HEDLEY_GCC_HAS_WARNING
  717. #endif
  718. #if defined(__has_warning)
  719. #define JSON_HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) __has_warning(warning)
  720. #else
  721. #define JSON_HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  722. #endif
  723. #if \
  724. (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
  725. defined(__clang__) || \
  726. JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \
  727. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  728. JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \
  729. JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \
  730. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  731. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  732. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,7,0) || \
  733. JSON_HEDLEY_TI_CL430_VERSION_CHECK(2,0,1) || \
  734. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,1,0) || \
  735. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,0,0) || \
  736. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  737. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  738. JSON_HEDLEY_CRAY_VERSION_CHECK(5,0,0) || \
  739. JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,17) || \
  740. JSON_HEDLEY_SUNPRO_VERSION_CHECK(8,0,0) || \
  741. (JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) && defined(__C99_PRAGMA_OPERATOR))
  742. #define JSON_HEDLEY_PRAGMA(value) _Pragma(#value)
  743. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
  744. #define JSON_HEDLEY_PRAGMA(value) __pragma(value)
  745. #else
  746. #define JSON_HEDLEY_PRAGMA(value)
  747. #endif
  748. #if defined(JSON_HEDLEY_DIAGNOSTIC_PUSH)
  749. #undef JSON_HEDLEY_DIAGNOSTIC_PUSH
  750. #endif
  751. #if defined(JSON_HEDLEY_DIAGNOSTIC_POP)
  752. #undef JSON_HEDLEY_DIAGNOSTIC_POP
  753. #endif
  754. #if defined(__clang__)
  755. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("clang diagnostic push")
  756. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("clang diagnostic pop")
  757. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  758. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)")
  759. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)")
  760. #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0)
  761. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push")
  762. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("GCC diagnostic pop")
  763. #elif \
  764. JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) || \
  765. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  766. #define JSON_HEDLEY_DIAGNOSTIC_PUSH __pragma(warning(push))
  767. #define JSON_HEDLEY_DIAGNOSTIC_POP __pragma(warning(pop))
  768. #elif JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0)
  769. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("push")
  770. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("pop")
  771. #elif \
  772. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  773. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  774. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,4,0) || \
  775. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,1,0) || \
  776. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  777. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0)
  778. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("diag_push")
  779. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("diag_pop")
  780. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,90,0)
  781. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)")
  782. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)")
  783. #else
  784. #define JSON_HEDLEY_DIAGNOSTIC_PUSH
  785. #define JSON_HEDLEY_DIAGNOSTIC_POP
  786. #endif
  787. /* JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_ is for
  788. HEDLEY INTERNAL USE ONLY. API subject to change without notice. */
  789. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_)
  790. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_
  791. #endif
  792. #if defined(__cplusplus)
  793. # if JSON_HEDLEY_HAS_WARNING("-Wc++98-compat")
  794. # if JSON_HEDLEY_HAS_WARNING("-Wc++17-extensions")
  795. # if JSON_HEDLEY_HAS_WARNING("-Wc++1z-extensions")
  796. # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
  797. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  798. _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
  799. _Pragma("clang diagnostic ignored \"-Wc++17-extensions\"") \
  800. _Pragma("clang diagnostic ignored \"-Wc++1z-extensions\"") \
  801. xpr \
  802. JSON_HEDLEY_DIAGNOSTIC_POP
  803. # else
  804. # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
  805. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  806. _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
  807. _Pragma("clang diagnostic ignored \"-Wc++17-extensions\"") \
  808. xpr \
  809. JSON_HEDLEY_DIAGNOSTIC_POP
  810. # endif
  811. # else
  812. # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
  813. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  814. _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
  815. xpr \
  816. JSON_HEDLEY_DIAGNOSTIC_POP
  817. # endif
  818. # endif
  819. #endif
  820. #if !defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_)
  821. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(x) x
  822. #endif
  823. #if defined(JSON_HEDLEY_CONST_CAST)
  824. #undef JSON_HEDLEY_CONST_CAST
  825. #endif
  826. #if defined(__cplusplus)
  827. # define JSON_HEDLEY_CONST_CAST(T, expr) (const_cast<T>(expr))
  828. #elif \
  829. JSON_HEDLEY_HAS_WARNING("-Wcast-qual") || \
  830. JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0) || \
  831. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  832. # define JSON_HEDLEY_CONST_CAST(T, expr) (__extension__ ({ \
  833. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  834. JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL \
  835. ((T) (expr)); \
  836. JSON_HEDLEY_DIAGNOSTIC_POP \
  837. }))
  838. #else
  839. # define JSON_HEDLEY_CONST_CAST(T, expr) ((T) (expr))
  840. #endif
  841. #if defined(JSON_HEDLEY_REINTERPRET_CAST)
  842. #undef JSON_HEDLEY_REINTERPRET_CAST
  843. #endif
  844. #if defined(__cplusplus)
  845. #define JSON_HEDLEY_REINTERPRET_CAST(T, expr) (reinterpret_cast<T>(expr))
  846. #else
  847. #define JSON_HEDLEY_REINTERPRET_CAST(T, expr) ((T) (expr))
  848. #endif
  849. #if defined(JSON_HEDLEY_STATIC_CAST)
  850. #undef JSON_HEDLEY_STATIC_CAST
  851. #endif
  852. #if defined(__cplusplus)
  853. #define JSON_HEDLEY_STATIC_CAST(T, expr) (static_cast<T>(expr))
  854. #else
  855. #define JSON_HEDLEY_STATIC_CAST(T, expr) ((T) (expr))
  856. #endif
  857. #if defined(JSON_HEDLEY_CPP_CAST)
  858. #undef JSON_HEDLEY_CPP_CAST
  859. #endif
  860. #if defined(__cplusplus)
  861. # if JSON_HEDLEY_HAS_WARNING("-Wold-style-cast")
  862. # define JSON_HEDLEY_CPP_CAST(T, expr) \
  863. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  864. _Pragma("clang diagnostic ignored \"-Wold-style-cast\"") \
  865. ((T) (expr)) \
  866. JSON_HEDLEY_DIAGNOSTIC_POP
  867. # elif JSON_HEDLEY_IAR_VERSION_CHECK(8,3,0)
  868. # define JSON_HEDLEY_CPP_CAST(T, expr) \
  869. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  870. _Pragma("diag_suppress=Pe137") \
  871. JSON_HEDLEY_DIAGNOSTIC_POP
  872. # else
  873. # define JSON_HEDLEY_CPP_CAST(T, expr) ((T) (expr))
  874. # endif
  875. #else
  876. # define JSON_HEDLEY_CPP_CAST(T, expr) (expr)
  877. #endif
  878. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED)
  879. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
  880. #endif
  881. #if JSON_HEDLEY_HAS_WARNING("-Wdeprecated-declarations")
  882. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"")
  883. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  884. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warning(disable:1478 1786)")
  885. #elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  886. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:1478 1786))
  887. #elif JSON_HEDLEY_PGI_VERSION_CHECK(20,7,0)
  888. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1216,1444,1445")
  889. #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
  890. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1444")
  891. #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0)
  892. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
  893. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
  894. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:4996))
  895. #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  896. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1444")
  897. #elif \
  898. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  899. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  900. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  901. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  902. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  903. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  904. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  905. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  906. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  907. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  908. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0)
  909. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1291,1718")
  910. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) && !defined(__cplusplus)
  911. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("error_messages(off,E_DEPRECATED_ATT,E_DEPRECATED_ATT_MESS)")
  912. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) && defined(__cplusplus)
  913. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("error_messages(off,symdeprecated,symdeprecated2)")
  914. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  915. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress=Pe1444,Pe1215")
  916. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,90,0)
  917. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warn(disable:2241)")
  918. #else
  919. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
  920. #endif
  921. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS)
  922. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
  923. #endif
  924. #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
  925. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("clang diagnostic ignored \"-Wunknown-pragmas\"")
  926. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  927. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("warning(disable:161)")
  928. #elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  929. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS __pragma(warning(disable:161))
  930. #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
  931. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 1675")
  932. #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0)
  933. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("GCC diagnostic ignored \"-Wunknown-pragmas\"")
  934. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
  935. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS __pragma(warning(disable:4068))
  936. #elif \
  937. JSON_HEDLEY_TI_VERSION_CHECK(16,9,0) || \
  938. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) || \
  939. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  940. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0)
  941. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163")
  942. #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0)
  943. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163")
  944. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  945. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress=Pe161")
  946. #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  947. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 161")
  948. #else
  949. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
  950. #endif
  951. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES)
  952. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
  953. #endif
  954. #if JSON_HEDLEY_HAS_WARNING("-Wunknown-attributes")
  955. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("clang diagnostic ignored \"-Wunknown-attributes\"")
  956. #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0)
  957. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
  958. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(17,0,0)
  959. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("warning(disable:1292)")
  960. #elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  961. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES __pragma(warning(disable:1292))
  962. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,0)
  963. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES __pragma(warning(disable:5030))
  964. #elif JSON_HEDLEY_PGI_VERSION_CHECK(20,7,0)
  965. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097,1098")
  966. #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
  967. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097")
  968. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,14,0) && defined(__cplusplus)
  969. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("error_messages(off,attrskipunsup)")
  970. #elif \
  971. JSON_HEDLEY_TI_VERSION_CHECK(18,1,0) || \
  972. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,3,0) || \
  973. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0)
  974. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1173")
  975. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  976. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress=Pe1097")
  977. #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  978. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097")
  979. #else
  980. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
  981. #endif
  982. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL)
  983. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
  984. #endif
  985. #if JSON_HEDLEY_HAS_WARNING("-Wcast-qual")
  986. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("clang diagnostic ignored \"-Wcast-qual\"")
  987. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  988. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("warning(disable:2203 2331)")
  989. #elif JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0)
  990. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("GCC diagnostic ignored \"-Wcast-qual\"")
  991. #else
  992. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
  993. #endif
  994. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION)
  995. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
  996. #endif
  997. #if JSON_HEDLEY_HAS_WARNING("-Wunused-function")
  998. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("clang diagnostic ignored \"-Wunused-function\"")
  999. #elif JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0)
  1000. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("GCC diagnostic ignored \"-Wunused-function\"")
  1001. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(1,0,0)
  1002. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION __pragma(warning(disable:4505))
  1003. #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1004. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("diag_suppress 3142")
  1005. #else
  1006. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
  1007. #endif
  1008. #if defined(JSON_HEDLEY_DEPRECATED)
  1009. #undef JSON_HEDLEY_DEPRECATED
  1010. #endif
  1011. #if defined(JSON_HEDLEY_DEPRECATED_FOR)
  1012. #undef JSON_HEDLEY_DEPRECATED_FOR
  1013. #endif
  1014. #if \
  1015. JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
  1016. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1017. #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated("Since " # since))
  1018. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated("Since " #since "; use " #replacement))
  1019. #elif \
  1020. (JSON_HEDLEY_HAS_EXTENSION(attribute_deprecated_with_message) && !defined(JSON_HEDLEY_IAR_VERSION)) || \
  1021. JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \
  1022. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1023. JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) || \
  1024. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) || \
  1025. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1026. JSON_HEDLEY_TI_VERSION_CHECK(18,1,0) || \
  1027. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(18,1,0) || \
  1028. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,3,0) || \
  1029. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1030. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0) || \
  1031. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1032. #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__("Since " #since)))
  1033. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__("Since " #since "; use " #replacement)))
  1034. #elif defined(__cplusplus) && (__cplusplus >= 201402L)
  1035. #define JSON_HEDLEY_DEPRECATED(since) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since)]])
  1036. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since "; use " #replacement)]])
  1037. #elif \
  1038. JSON_HEDLEY_HAS_ATTRIBUTE(deprecated) || \
  1039. JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
  1040. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1041. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1042. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1043. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1044. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1045. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1046. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1047. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1048. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1049. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1050. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1051. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1052. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \
  1053. JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
  1054. #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__))
  1055. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__))
  1056. #elif \
  1057. JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
  1058. JSON_HEDLEY_PELLES_VERSION_CHECK(6,50,0) || \
  1059. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1060. #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated)
  1061. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated)
  1062. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1063. #define JSON_HEDLEY_DEPRECATED(since) _Pragma("deprecated")
  1064. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) _Pragma("deprecated")
  1065. #else
  1066. #define JSON_HEDLEY_DEPRECATED(since)
  1067. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement)
  1068. #endif
  1069. #if defined(JSON_HEDLEY_UNAVAILABLE)
  1070. #undef JSON_HEDLEY_UNAVAILABLE
  1071. #endif
  1072. #if \
  1073. JSON_HEDLEY_HAS_ATTRIBUTE(warning) || \
  1074. JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0) || \
  1075. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1076. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1077. #define JSON_HEDLEY_UNAVAILABLE(available_since) __attribute__((__warning__("Not available until " #available_since)))
  1078. #else
  1079. #define JSON_HEDLEY_UNAVAILABLE(available_since)
  1080. #endif
  1081. #if defined(JSON_HEDLEY_WARN_UNUSED_RESULT)
  1082. #undef JSON_HEDLEY_WARN_UNUSED_RESULT
  1083. #endif
  1084. #if defined(JSON_HEDLEY_WARN_UNUSED_RESULT_MSG)
  1085. #undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG
  1086. #endif
  1087. #if \
  1088. JSON_HEDLEY_HAS_ATTRIBUTE(warn_unused_result) || \
  1089. JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
  1090. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1091. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1092. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1093. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1094. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1095. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1096. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1097. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1098. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1099. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1100. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1101. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1102. (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)) || \
  1103. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1104. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1105. #define JSON_HEDLEY_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
  1106. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) __attribute__((__warn_unused_result__))
  1107. #elif (JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard) >= 201907L)
  1108. #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
  1109. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard(msg)]])
  1110. #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard)
  1111. #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
  1112. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
  1113. #elif defined(_Check_return_) /* SAL */
  1114. #define JSON_HEDLEY_WARN_UNUSED_RESULT _Check_return_
  1115. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) _Check_return_
  1116. #else
  1117. #define JSON_HEDLEY_WARN_UNUSED_RESULT
  1118. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg)
  1119. #endif
  1120. #if defined(JSON_HEDLEY_SENTINEL)
  1121. #undef JSON_HEDLEY_SENTINEL
  1122. #endif
  1123. #if \
  1124. JSON_HEDLEY_HAS_ATTRIBUTE(sentinel) || \
  1125. JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
  1126. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1127. JSON_HEDLEY_ARM_VERSION_CHECK(5,4,0) || \
  1128. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1129. #define JSON_HEDLEY_SENTINEL(position) __attribute__((__sentinel__(position)))
  1130. #else
  1131. #define JSON_HEDLEY_SENTINEL(position)
  1132. #endif
  1133. #if defined(JSON_HEDLEY_NO_RETURN)
  1134. #undef JSON_HEDLEY_NO_RETURN
  1135. #endif
  1136. #if JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1137. #define JSON_HEDLEY_NO_RETURN __noreturn
  1138. #elif \
  1139. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1140. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1141. #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__))
  1142. #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
  1143. #define JSON_HEDLEY_NO_RETURN _Noreturn
  1144. #elif defined(__cplusplus) && (__cplusplus >= 201103L)
  1145. #define JSON_HEDLEY_NO_RETURN JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[noreturn]])
  1146. #elif \
  1147. JSON_HEDLEY_HAS_ATTRIBUTE(noreturn) || \
  1148. JSON_HEDLEY_GCC_VERSION_CHECK(3,2,0) || \
  1149. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1150. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1151. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1152. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1153. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1154. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1155. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1156. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1157. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1158. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1159. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1160. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1161. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1162. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1163. JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
  1164. #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__))
  1165. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
  1166. #define JSON_HEDLEY_NO_RETURN _Pragma("does_not_return")
  1167. #elif \
  1168. JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
  1169. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1170. #define JSON_HEDLEY_NO_RETURN __declspec(noreturn)
  1171. #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,0,0) && defined(__cplusplus)
  1172. #define JSON_HEDLEY_NO_RETURN _Pragma("FUNC_NEVER_RETURNS;")
  1173. #elif JSON_HEDLEY_COMPCERT_VERSION_CHECK(3,2,0)
  1174. #define JSON_HEDLEY_NO_RETURN __attribute((noreturn))
  1175. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(9,0,0)
  1176. #define JSON_HEDLEY_NO_RETURN __declspec(noreturn)
  1177. #else
  1178. #define JSON_HEDLEY_NO_RETURN
  1179. #endif
  1180. #if defined(JSON_HEDLEY_NO_ESCAPE)
  1181. #undef JSON_HEDLEY_NO_ESCAPE
  1182. #endif
  1183. #if JSON_HEDLEY_HAS_ATTRIBUTE(noescape)
  1184. #define JSON_HEDLEY_NO_ESCAPE __attribute__((__noescape__))
  1185. #else
  1186. #define JSON_HEDLEY_NO_ESCAPE
  1187. #endif
  1188. #if defined(JSON_HEDLEY_UNREACHABLE)
  1189. #undef JSON_HEDLEY_UNREACHABLE
  1190. #endif
  1191. #if defined(JSON_HEDLEY_UNREACHABLE_RETURN)
  1192. #undef JSON_HEDLEY_UNREACHABLE_RETURN
  1193. #endif
  1194. #if defined(JSON_HEDLEY_ASSUME)
  1195. #undef JSON_HEDLEY_ASSUME
  1196. #endif
  1197. #if \
  1198. JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
  1199. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1200. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1201. #define JSON_HEDLEY_ASSUME(expr) __assume(expr)
  1202. #elif JSON_HEDLEY_HAS_BUILTIN(__builtin_assume)
  1203. #define JSON_HEDLEY_ASSUME(expr) __builtin_assume(expr)
  1204. #elif \
  1205. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
  1206. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0)
  1207. #if defined(__cplusplus)
  1208. #define JSON_HEDLEY_ASSUME(expr) std::_nassert(expr)
  1209. #else
  1210. #define JSON_HEDLEY_ASSUME(expr) _nassert(expr)
  1211. #endif
  1212. #endif
  1213. #if \
  1214. (JSON_HEDLEY_HAS_BUILTIN(__builtin_unreachable) && (!defined(JSON_HEDLEY_ARM_VERSION))) || \
  1215. JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \
  1216. JSON_HEDLEY_PGI_VERSION_CHECK(18,10,0) || \
  1217. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1218. JSON_HEDLEY_IBM_VERSION_CHECK(13,1,5) || \
  1219. JSON_HEDLEY_CRAY_VERSION_CHECK(10,0,0) || \
  1220. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1221. #define JSON_HEDLEY_UNREACHABLE() __builtin_unreachable()
  1222. #elif defined(JSON_HEDLEY_ASSUME)
  1223. #define JSON_HEDLEY_UNREACHABLE() JSON_HEDLEY_ASSUME(0)
  1224. #endif
  1225. #if !defined(JSON_HEDLEY_ASSUME)
  1226. #if defined(JSON_HEDLEY_UNREACHABLE)
  1227. #define JSON_HEDLEY_ASSUME(expr) JSON_HEDLEY_STATIC_CAST(void, ((expr) ? 1 : (JSON_HEDLEY_UNREACHABLE(), 1)))
  1228. #else
  1229. #define JSON_HEDLEY_ASSUME(expr) JSON_HEDLEY_STATIC_CAST(void, expr)
  1230. #endif
  1231. #endif
  1232. #if defined(JSON_HEDLEY_UNREACHABLE)
  1233. #if \
  1234. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
  1235. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0)
  1236. #define JSON_HEDLEY_UNREACHABLE_RETURN(value) return (JSON_HEDLEY_STATIC_CAST(void, JSON_HEDLEY_ASSUME(0)), (value))
  1237. #else
  1238. #define JSON_HEDLEY_UNREACHABLE_RETURN(value) JSON_HEDLEY_UNREACHABLE()
  1239. #endif
  1240. #else
  1241. #define JSON_HEDLEY_UNREACHABLE_RETURN(value) return (value)
  1242. #endif
  1243. #if !defined(JSON_HEDLEY_UNREACHABLE)
  1244. #define JSON_HEDLEY_UNREACHABLE() JSON_HEDLEY_ASSUME(0)
  1245. #endif
  1246. JSON_HEDLEY_DIAGNOSTIC_PUSH
  1247. #if JSON_HEDLEY_HAS_WARNING("-Wpedantic")
  1248. #pragma clang diagnostic ignored "-Wpedantic"
  1249. #endif
  1250. #if JSON_HEDLEY_HAS_WARNING("-Wc++98-compat-pedantic") && defined(__cplusplus)
  1251. #pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
  1252. #endif
  1253. #if JSON_HEDLEY_GCC_HAS_WARNING("-Wvariadic-macros",4,0,0)
  1254. #if defined(__clang__)
  1255. #pragma clang diagnostic ignored "-Wvariadic-macros"
  1256. #elif defined(JSON_HEDLEY_GCC_VERSION)
  1257. #pragma GCC diagnostic ignored "-Wvariadic-macros"
  1258. #endif
  1259. #endif
  1260. #if defined(JSON_HEDLEY_NON_NULL)
  1261. #undef JSON_HEDLEY_NON_NULL
  1262. #endif
  1263. #if \
  1264. JSON_HEDLEY_HAS_ATTRIBUTE(nonnull) || \
  1265. JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
  1266. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1267. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0)
  1268. #define JSON_HEDLEY_NON_NULL(...) __attribute__((__nonnull__(__VA_ARGS__)))
  1269. #else
  1270. #define JSON_HEDLEY_NON_NULL(...)
  1271. #endif
  1272. JSON_HEDLEY_DIAGNOSTIC_POP
  1273. #if defined(JSON_HEDLEY_PRINTF_FORMAT)
  1274. #undef JSON_HEDLEY_PRINTF_FORMAT
  1275. #endif
  1276. #if defined(__MINGW32__) && JSON_HEDLEY_GCC_HAS_ATTRIBUTE(format,4,4,0) && !defined(__USE_MINGW_ANSI_STDIO)
  1277. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(ms_printf, string_idx, first_to_check)))
  1278. #elif defined(__MINGW32__) && JSON_HEDLEY_GCC_HAS_ATTRIBUTE(format,4,4,0) && defined(__USE_MINGW_ANSI_STDIO)
  1279. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(gnu_printf, string_idx, first_to_check)))
  1280. #elif \
  1281. JSON_HEDLEY_HAS_ATTRIBUTE(format) || \
  1282. JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
  1283. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1284. JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) || \
  1285. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1286. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1287. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1288. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1289. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1290. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1291. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1292. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1293. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1294. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1295. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1296. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1297. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1298. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(__printf__, string_idx, first_to_check)))
  1299. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(6,0,0)
  1300. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __declspec(vaformat(printf,string_idx,first_to_check))
  1301. #else
  1302. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check)
  1303. #endif
  1304. #if defined(JSON_HEDLEY_CONSTEXPR)
  1305. #undef JSON_HEDLEY_CONSTEXPR
  1306. #endif
  1307. #if defined(__cplusplus)
  1308. #if __cplusplus >= 201103L
  1309. #define JSON_HEDLEY_CONSTEXPR JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(constexpr)
  1310. #endif
  1311. #endif
  1312. #if !defined(JSON_HEDLEY_CONSTEXPR)
  1313. #define JSON_HEDLEY_CONSTEXPR
  1314. #endif
  1315. #if defined(JSON_HEDLEY_PREDICT)
  1316. #undef JSON_HEDLEY_PREDICT
  1317. #endif
  1318. #if defined(JSON_HEDLEY_LIKELY)
  1319. #undef JSON_HEDLEY_LIKELY
  1320. #endif
  1321. #if defined(JSON_HEDLEY_UNLIKELY)
  1322. #undef JSON_HEDLEY_UNLIKELY
  1323. #endif
  1324. #if defined(JSON_HEDLEY_UNPREDICTABLE)
  1325. #undef JSON_HEDLEY_UNPREDICTABLE
  1326. #endif
  1327. #if JSON_HEDLEY_HAS_BUILTIN(__builtin_unpredictable)
  1328. #define JSON_HEDLEY_UNPREDICTABLE(expr) __builtin_unpredictable((expr))
  1329. #endif
  1330. #if \
  1331. (JSON_HEDLEY_HAS_BUILTIN(__builtin_expect_with_probability) && !defined(JSON_HEDLEY_PGI_VERSION)) || \
  1332. JSON_HEDLEY_GCC_VERSION_CHECK(9,0,0) || \
  1333. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1334. # define JSON_HEDLEY_PREDICT(expr, value, probability) __builtin_expect_with_probability( (expr), (value), (probability))
  1335. # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) __builtin_expect_with_probability(!!(expr), 1 , (probability))
  1336. # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) __builtin_expect_with_probability(!!(expr), 0 , (probability))
  1337. # define JSON_HEDLEY_LIKELY(expr) __builtin_expect (!!(expr), 1 )
  1338. # define JSON_HEDLEY_UNLIKELY(expr) __builtin_expect (!!(expr), 0 )
  1339. #elif \
  1340. (JSON_HEDLEY_HAS_BUILTIN(__builtin_expect) && !defined(JSON_HEDLEY_INTEL_CL_VERSION)) || \
  1341. JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \
  1342. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1343. (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)) || \
  1344. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1345. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1346. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1347. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,7,0) || \
  1348. JSON_HEDLEY_TI_CL430_VERSION_CHECK(3,1,0) || \
  1349. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,1,0) || \
  1350. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
  1351. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1352. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1353. JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,27) || \
  1354. JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \
  1355. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1356. # define JSON_HEDLEY_PREDICT(expr, expected, probability) \
  1357. (((probability) >= 0.9) ? __builtin_expect((expr), (expected)) : (JSON_HEDLEY_STATIC_CAST(void, expected), (expr)))
  1358. # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) \
  1359. (__extension__ ({ \
  1360. double hedley_probability_ = (probability); \
  1361. ((hedley_probability_ >= 0.9) ? __builtin_expect(!!(expr), 1) : ((hedley_probability_ <= 0.1) ? __builtin_expect(!!(expr), 0) : !!(expr))); \
  1362. }))
  1363. # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) \
  1364. (__extension__ ({ \
  1365. double hedley_probability_ = (probability); \
  1366. ((hedley_probability_ >= 0.9) ? __builtin_expect(!!(expr), 0) : ((hedley_probability_ <= 0.1) ? __builtin_expect(!!(expr), 1) : !!(expr))); \
  1367. }))
  1368. # define JSON_HEDLEY_LIKELY(expr) __builtin_expect(!!(expr), 1)
  1369. # define JSON_HEDLEY_UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  1370. #else
  1371. # define JSON_HEDLEY_PREDICT(expr, expected, probability) (JSON_HEDLEY_STATIC_CAST(void, expected), (expr))
  1372. # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) (!!(expr))
  1373. # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) (!!(expr))
  1374. # define JSON_HEDLEY_LIKELY(expr) (!!(expr))
  1375. # define JSON_HEDLEY_UNLIKELY(expr) (!!(expr))
  1376. #endif
  1377. #if !defined(JSON_HEDLEY_UNPREDICTABLE)
  1378. #define JSON_HEDLEY_UNPREDICTABLE(expr) JSON_HEDLEY_PREDICT(expr, 1, 0.5)
  1379. #endif
  1380. #if defined(JSON_HEDLEY_MALLOC)
  1381. #undef JSON_HEDLEY_MALLOC
  1382. #endif
  1383. #if \
  1384. JSON_HEDLEY_HAS_ATTRIBUTE(malloc) || \
  1385. JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
  1386. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1387. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1388. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1389. JSON_HEDLEY_IBM_VERSION_CHECK(12,1,0) || \
  1390. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1391. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1392. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1393. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1394. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1395. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1396. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1397. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1398. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1399. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1400. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1401. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1402. #define JSON_HEDLEY_MALLOC __attribute__((__malloc__))
  1403. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
  1404. #define JSON_HEDLEY_MALLOC _Pragma("returns_new_memory")
  1405. #elif \
  1406. JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
  1407. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1408. #define JSON_HEDLEY_MALLOC __declspec(restrict)
  1409. #else
  1410. #define JSON_HEDLEY_MALLOC
  1411. #endif
  1412. #if defined(JSON_HEDLEY_PURE)
  1413. #undef JSON_HEDLEY_PURE
  1414. #endif
  1415. #if \
  1416. JSON_HEDLEY_HAS_ATTRIBUTE(pure) || \
  1417. JSON_HEDLEY_GCC_VERSION_CHECK(2,96,0) || \
  1418. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1419. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1420. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1421. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1422. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1423. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1424. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1425. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1426. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1427. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1428. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1429. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1430. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1431. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1432. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1433. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1434. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1435. # define JSON_HEDLEY_PURE __attribute__((__pure__))
  1436. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
  1437. # define JSON_HEDLEY_PURE _Pragma("does_not_write_global_data")
  1438. #elif defined(__cplusplus) && \
  1439. ( \
  1440. JSON_HEDLEY_TI_CL430_VERSION_CHECK(2,0,1) || \
  1441. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0) || \
  1442. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) \
  1443. )
  1444. # define JSON_HEDLEY_PURE _Pragma("FUNC_IS_PURE;")
  1445. #else
  1446. # define JSON_HEDLEY_PURE
  1447. #endif
  1448. #if defined(JSON_HEDLEY_CONST)
  1449. #undef JSON_HEDLEY_CONST
  1450. #endif
  1451. #if \
  1452. JSON_HEDLEY_HAS_ATTRIBUTE(const) || \
  1453. JSON_HEDLEY_GCC_VERSION_CHECK(2,5,0) || \
  1454. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1455. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1456. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1457. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1458. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1459. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1460. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1461. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1462. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1463. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1464. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1465. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1466. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1467. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1468. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1469. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1470. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1471. #define JSON_HEDLEY_CONST __attribute__((__const__))
  1472. #elif \
  1473. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
  1474. #define JSON_HEDLEY_CONST _Pragma("no_side_effect")
  1475. #else
  1476. #define JSON_HEDLEY_CONST JSON_HEDLEY_PURE
  1477. #endif
  1478. #if defined(JSON_HEDLEY_RESTRICT)
  1479. #undef JSON_HEDLEY_RESTRICT
  1480. #endif
  1481. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && !defined(__cplusplus)
  1482. #define JSON_HEDLEY_RESTRICT restrict
  1483. #elif \
  1484. JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
  1485. JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
  1486. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1487. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \
  1488. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1489. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1490. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1491. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1492. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,4) || \
  1493. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,1,0) || \
  1494. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1495. (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,14,0) && defined(__cplusplus)) || \
  1496. JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \
  1497. defined(__clang__) || \
  1498. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1499. #define JSON_HEDLEY_RESTRICT __restrict
  1500. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,3,0) && !defined(__cplusplus)
  1501. #define JSON_HEDLEY_RESTRICT _Restrict
  1502. #else
  1503. #define JSON_HEDLEY_RESTRICT
  1504. #endif
  1505. #if defined(JSON_HEDLEY_INLINE)
  1506. #undef JSON_HEDLEY_INLINE
  1507. #endif
  1508. #if \
  1509. (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
  1510. (defined(__cplusplus) && (__cplusplus >= 199711L))
  1511. #define JSON_HEDLEY_INLINE inline
  1512. #elif \
  1513. defined(JSON_HEDLEY_GCC_VERSION) || \
  1514. JSON_HEDLEY_ARM_VERSION_CHECK(6,2,0)
  1515. #define JSON_HEDLEY_INLINE __inline__
  1516. #elif \
  1517. JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \
  1518. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \
  1519. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1520. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,1,0) || \
  1521. JSON_HEDLEY_TI_CL430_VERSION_CHECK(3,1,0) || \
  1522. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
  1523. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) || \
  1524. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1525. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1526. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1527. #define JSON_HEDLEY_INLINE __inline
  1528. #else
  1529. #define JSON_HEDLEY_INLINE
  1530. #endif
  1531. #if defined(JSON_HEDLEY_ALWAYS_INLINE)
  1532. #undef JSON_HEDLEY_ALWAYS_INLINE
  1533. #endif
  1534. #if \
  1535. JSON_HEDLEY_HAS_ATTRIBUTE(always_inline) || \
  1536. JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
  1537. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1538. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1539. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1540. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1541. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1542. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1543. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1544. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1545. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1546. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1547. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1548. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1549. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1550. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1551. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1552. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \
  1553. JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
  1554. # define JSON_HEDLEY_ALWAYS_INLINE __attribute__((__always_inline__)) JSON_HEDLEY_INLINE
  1555. #elif \
  1556. JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \
  1557. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1558. # define JSON_HEDLEY_ALWAYS_INLINE __forceinline
  1559. #elif defined(__cplusplus) && \
  1560. ( \
  1561. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1562. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1563. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1564. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
  1565. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1566. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) \
  1567. )
  1568. # define JSON_HEDLEY_ALWAYS_INLINE _Pragma("FUNC_ALWAYS_INLINE;")
  1569. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1570. # define JSON_HEDLEY_ALWAYS_INLINE _Pragma("inline=forced")
  1571. #else
  1572. # define JSON_HEDLEY_ALWAYS_INLINE JSON_HEDLEY_INLINE
  1573. #endif
  1574. #if defined(JSON_HEDLEY_NEVER_INLINE)
  1575. #undef JSON_HEDLEY_NEVER_INLINE
  1576. #endif
  1577. #if \
  1578. JSON_HEDLEY_HAS_ATTRIBUTE(noinline) || \
  1579. JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
  1580. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1581. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1582. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1583. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1584. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1585. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1586. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1587. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1588. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1589. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1590. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1591. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1592. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1593. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1594. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1595. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \
  1596. JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
  1597. #define JSON_HEDLEY_NEVER_INLINE __attribute__((__noinline__))
  1598. #elif \
  1599. JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
  1600. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1601. #define JSON_HEDLEY_NEVER_INLINE __declspec(noinline)
  1602. #elif JSON_HEDLEY_PGI_VERSION_CHECK(10,2,0)
  1603. #define JSON_HEDLEY_NEVER_INLINE _Pragma("noinline")
  1604. #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,0,0) && defined(__cplusplus)
  1605. #define JSON_HEDLEY_NEVER_INLINE _Pragma("FUNC_CANNOT_INLINE;")
  1606. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1607. #define JSON_HEDLEY_NEVER_INLINE _Pragma("inline=never")
  1608. #elif JSON_HEDLEY_COMPCERT_VERSION_CHECK(3,2,0)
  1609. #define JSON_HEDLEY_NEVER_INLINE __attribute((noinline))
  1610. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(9,0,0)
  1611. #define JSON_HEDLEY_NEVER_INLINE __declspec(noinline)
  1612. #else
  1613. #define JSON_HEDLEY_NEVER_INLINE
  1614. #endif
  1615. #if defined(JSON_HEDLEY_PRIVATE)
  1616. #undef JSON_HEDLEY_PRIVATE
  1617. #endif
  1618. #if defined(JSON_HEDLEY_PUBLIC)
  1619. #undef JSON_HEDLEY_PUBLIC
  1620. #endif
  1621. #if defined(JSON_HEDLEY_IMPORT)
  1622. #undef JSON_HEDLEY_IMPORT
  1623. #endif
  1624. #if defined(_WIN32) || defined(__CYGWIN__)
  1625. # define JSON_HEDLEY_PRIVATE
  1626. # define JSON_HEDLEY_PUBLIC __declspec(dllexport)
  1627. # define JSON_HEDLEY_IMPORT __declspec(dllimport)
  1628. #else
  1629. # if \
  1630. JSON_HEDLEY_HAS_ATTRIBUTE(visibility) || \
  1631. JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
  1632. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1633. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1634. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1635. JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
  1636. ( \
  1637. defined(__TI_EABI__) && \
  1638. ( \
  1639. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1640. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) \
  1641. ) \
  1642. ) || \
  1643. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1644. # define JSON_HEDLEY_PRIVATE __attribute__((__visibility__("hidden")))
  1645. # define JSON_HEDLEY_PUBLIC __attribute__((__visibility__("default")))
  1646. # else
  1647. # define JSON_HEDLEY_PRIVATE
  1648. # define JSON_HEDLEY_PUBLIC
  1649. # endif
  1650. # define JSON_HEDLEY_IMPORT extern
  1651. #endif
  1652. #if defined(JSON_HEDLEY_NO_THROW)
  1653. #undef JSON_HEDLEY_NO_THROW
  1654. #endif
  1655. #if \
  1656. JSON_HEDLEY_HAS_ATTRIBUTE(nothrow) || \
  1657. JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
  1658. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1659. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1660. #define JSON_HEDLEY_NO_THROW __attribute__((__nothrow__))
  1661. #elif \
  1662. JSON_HEDLEY_MSVC_VERSION_CHECK(13,1,0) || \
  1663. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \
  1664. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0)
  1665. #define JSON_HEDLEY_NO_THROW __declspec(nothrow)
  1666. #else
  1667. #define JSON_HEDLEY_NO_THROW
  1668. #endif
  1669. #if defined(JSON_HEDLEY_FALL_THROUGH)
  1670. #undef JSON_HEDLEY_FALL_THROUGH
  1671. #endif
  1672. #if \
  1673. JSON_HEDLEY_HAS_ATTRIBUTE(fallthrough) || \
  1674. JSON_HEDLEY_GCC_VERSION_CHECK(7,0,0) || \
  1675. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1676. #define JSON_HEDLEY_FALL_THROUGH __attribute__((__fallthrough__))
  1677. #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(clang,fallthrough)
  1678. #define JSON_HEDLEY_FALL_THROUGH JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[clang::fallthrough]])
  1679. #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE(fallthrough)
  1680. #define JSON_HEDLEY_FALL_THROUGH JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[fallthrough]])
  1681. #elif defined(__fallthrough) /* SAL */
  1682. #define JSON_HEDLEY_FALL_THROUGH __fallthrough
  1683. #else
  1684. #define JSON_HEDLEY_FALL_THROUGH
  1685. #endif
  1686. #if defined(JSON_HEDLEY_RETURNS_NON_NULL)
  1687. #undef JSON_HEDLEY_RETURNS_NON_NULL
  1688. #endif
  1689. #if \
  1690. JSON_HEDLEY_HAS_ATTRIBUTE(returns_nonnull) || \
  1691. JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) || \
  1692. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1693. #define JSON_HEDLEY_RETURNS_NON_NULL __attribute__((__returns_nonnull__))
  1694. #elif defined(_Ret_notnull_) /* SAL */
  1695. #define JSON_HEDLEY_RETURNS_NON_NULL _Ret_notnull_
  1696. #else
  1697. #define JSON_HEDLEY_RETURNS_NON_NULL
  1698. #endif
  1699. #if defined(JSON_HEDLEY_ARRAY_PARAM)
  1700. #undef JSON_HEDLEY_ARRAY_PARAM
  1701. #endif
  1702. #if \
  1703. defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
  1704. !defined(__STDC_NO_VLA__) && \
  1705. !defined(__cplusplus) && \
  1706. !defined(JSON_HEDLEY_PGI_VERSION) && \
  1707. !defined(JSON_HEDLEY_TINYC_VERSION)
  1708. #define JSON_HEDLEY_ARRAY_PARAM(name) (name)
  1709. #else
  1710. #define JSON_HEDLEY_ARRAY_PARAM(name)
  1711. #endif
  1712. #if defined(JSON_HEDLEY_IS_CONSTANT)
  1713. #undef JSON_HEDLEY_IS_CONSTANT
  1714. #endif
  1715. #if defined(JSON_HEDLEY_REQUIRE_CONSTEXPR)
  1716. #undef JSON_HEDLEY_REQUIRE_CONSTEXPR
  1717. #endif
  1718. /* JSON_HEDLEY_IS_CONSTEXPR_ is for
  1719. HEDLEY INTERNAL USE ONLY. API subject to change without notice. */
  1720. #if defined(JSON_HEDLEY_IS_CONSTEXPR_)
  1721. #undef JSON_HEDLEY_IS_CONSTEXPR_
  1722. #endif
  1723. #if \
  1724. JSON_HEDLEY_HAS_BUILTIN(__builtin_constant_p) || \
  1725. JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
  1726. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1727. JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,19) || \
  1728. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1729. JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
  1730. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
  1731. (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) && !defined(__cplusplus)) || \
  1732. JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \
  1733. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1734. #define JSON_HEDLEY_IS_CONSTANT(expr) __builtin_constant_p(expr)
  1735. #endif
  1736. #if !defined(__cplusplus)
  1737. # if \
  1738. JSON_HEDLEY_HAS_BUILTIN(__builtin_types_compatible_p) || \
  1739. JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
  1740. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1741. JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
  1742. JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \
  1743. JSON_HEDLEY_ARM_VERSION_CHECK(5,4,0) || \
  1744. JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,24)
  1745. #if defined(__INTPTR_TYPE__)
  1746. #define JSON_HEDLEY_IS_CONSTEXPR_(expr) __builtin_types_compatible_p(__typeof__((1 ? (void*) ((__INTPTR_TYPE__) ((expr) * 0)) : (int*) 0)), int*)
  1747. #else
  1748. #include <stdint.h>
  1749. #define JSON_HEDLEY_IS_CONSTEXPR_(expr) __builtin_types_compatible_p(__typeof__((1 ? (void*) ((intptr_t) ((expr) * 0)) : (int*) 0)), int*)
  1750. #endif
  1751. # elif \
  1752. ( \
  1753. defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) && \
  1754. !defined(JSON_HEDLEY_SUNPRO_VERSION) && \
  1755. !defined(JSON_HEDLEY_PGI_VERSION) && \
  1756. !defined(JSON_HEDLEY_IAR_VERSION)) || \
  1757. (JSON_HEDLEY_HAS_EXTENSION(c_generic_selections) && !defined(JSON_HEDLEY_IAR_VERSION)) || \
  1758. JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) || \
  1759. JSON_HEDLEY_INTEL_VERSION_CHECK(17,0,0) || \
  1760. JSON_HEDLEY_IBM_VERSION_CHECK(12,1,0) || \
  1761. JSON_HEDLEY_ARM_VERSION_CHECK(5,3,0)
  1762. #if defined(__INTPTR_TYPE__)
  1763. #define JSON_HEDLEY_IS_CONSTEXPR_(expr) _Generic((1 ? (void*) ((__INTPTR_TYPE__) ((expr) * 0)) : (int*) 0), int*: 1, void*: 0)
  1764. #else
  1765. #include <stdint.h>
  1766. #define JSON_HEDLEY_IS_CONSTEXPR_(expr) _Generic((1 ? (void*) ((intptr_t) * 0) : (int*) 0), int*: 1, void*: 0)
  1767. #endif
  1768. # elif \
  1769. defined(JSON_HEDLEY_GCC_VERSION) || \
  1770. defined(JSON_HEDLEY_INTEL_VERSION) || \
  1771. defined(JSON_HEDLEY_TINYC_VERSION) || \
  1772. defined(JSON_HEDLEY_TI_ARMCL_VERSION) || \
  1773. JSON_HEDLEY_TI_CL430_VERSION_CHECK(18,12,0) || \
  1774. defined(JSON_HEDLEY_TI_CL2000_VERSION) || \
  1775. defined(JSON_HEDLEY_TI_CL6X_VERSION) || \
  1776. defined(JSON_HEDLEY_TI_CL7X_VERSION) || \
  1777. defined(JSON_HEDLEY_TI_CLPRU_VERSION) || \
  1778. defined(__clang__)
  1779. # define JSON_HEDLEY_IS_CONSTEXPR_(expr) ( \
  1780. sizeof(void) != \
  1781. sizeof(*( \
  1782. 1 ? \
  1783. ((void*) ((expr) * 0L) ) : \
  1784. ((struct { char v[sizeof(void) * 2]; } *) 1) \
  1785. ) \
  1786. ) \
  1787. )
  1788. # endif
  1789. #endif
  1790. #if defined(JSON_HEDLEY_IS_CONSTEXPR_)
  1791. #if !defined(JSON_HEDLEY_IS_CONSTANT)
  1792. #define JSON_HEDLEY_IS_CONSTANT(expr) JSON_HEDLEY_IS_CONSTEXPR_(expr)
  1793. #endif
  1794. #define JSON_HEDLEY_REQUIRE_CONSTEXPR(expr) (JSON_HEDLEY_IS_CONSTEXPR_(expr) ? (expr) : (-1))
  1795. #else
  1796. #if !defined(JSON_HEDLEY_IS_CONSTANT)
  1797. #define JSON_HEDLEY_IS_CONSTANT(expr) (0)
  1798. #endif
  1799. #define JSON_HEDLEY_REQUIRE_CONSTEXPR(expr) (expr)
  1800. #endif
  1801. #if defined(JSON_HEDLEY_BEGIN_C_DECLS)
  1802. #undef JSON_HEDLEY_BEGIN_C_DECLS
  1803. #endif
  1804. #if defined(JSON_HEDLEY_END_C_DECLS)
  1805. #undef JSON_HEDLEY_END_C_DECLS
  1806. #endif
  1807. #if defined(JSON_HEDLEY_C_DECL)
  1808. #undef JSON_HEDLEY_C_DECL
  1809. #endif
  1810. #if defined(__cplusplus)
  1811. #define JSON_HEDLEY_BEGIN_C_DECLS extern "C" {
  1812. #define JSON_HEDLEY_END_C_DECLS }
  1813. #define JSON_HEDLEY_C_DECL extern "C"
  1814. #else
  1815. #define JSON_HEDLEY_BEGIN_C_DECLS
  1816. #define JSON_HEDLEY_END_C_DECLS
  1817. #define JSON_HEDLEY_C_DECL
  1818. #endif
  1819. #if defined(JSON_HEDLEY_STATIC_ASSERT)
  1820. #undef JSON_HEDLEY_STATIC_ASSERT
  1821. #endif
  1822. #if \
  1823. !defined(__cplusplus) && ( \
  1824. (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)) || \
  1825. (JSON_HEDLEY_HAS_FEATURE(c_static_assert) && !defined(JSON_HEDLEY_INTEL_CL_VERSION)) || \
  1826. JSON_HEDLEY_GCC_VERSION_CHECK(6,0,0) || \
  1827. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1828. defined(_Static_assert) \
  1829. )
  1830. # define JSON_HEDLEY_STATIC_ASSERT(expr, message) _Static_assert(expr, message)
  1831. #elif \
  1832. (defined(__cplusplus) && (__cplusplus >= 201103L)) || \
  1833. JSON_HEDLEY_MSVC_VERSION_CHECK(16,0,0) || \
  1834. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1835. # define JSON_HEDLEY_STATIC_ASSERT(expr, message) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(static_assert(expr, message))
  1836. #else
  1837. # define JSON_HEDLEY_STATIC_ASSERT(expr, message)
  1838. #endif
  1839. #if defined(JSON_HEDLEY_NULL)
  1840. #undef JSON_HEDLEY_NULL
  1841. #endif
  1842. #if defined(__cplusplus)
  1843. #if __cplusplus >= 201103L
  1844. #define JSON_HEDLEY_NULL JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(nullptr)
  1845. #elif defined(NULL)
  1846. #define JSON_HEDLEY_NULL NULL
  1847. #else
  1848. #define JSON_HEDLEY_NULL JSON_HEDLEY_STATIC_CAST(void*, 0)
  1849. #endif
  1850. #elif defined(NULL)
  1851. #define JSON_HEDLEY_NULL NULL
  1852. #else
  1853. #define JSON_HEDLEY_NULL ((void*) 0)
  1854. #endif
  1855. #if defined(JSON_HEDLEY_MESSAGE)
  1856. #undef JSON_HEDLEY_MESSAGE
  1857. #endif
  1858. #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
  1859. # define JSON_HEDLEY_MESSAGE(msg) \
  1860. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  1861. JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS \
  1862. JSON_HEDLEY_PRAGMA(message msg) \
  1863. JSON_HEDLEY_DIAGNOSTIC_POP
  1864. #elif \
  1865. JSON_HEDLEY_GCC_VERSION_CHECK(4,4,0) || \
  1866. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  1867. # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message msg)
  1868. #elif JSON_HEDLEY_CRAY_VERSION_CHECK(5,0,0)
  1869. # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(_CRI message msg)
  1870. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1871. # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message(msg))
  1872. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,0,0)
  1873. # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message(msg))
  1874. #else
  1875. # define JSON_HEDLEY_MESSAGE(msg)
  1876. #endif
  1877. #if defined(JSON_HEDLEY_WARNING)
  1878. #undef JSON_HEDLEY_WARNING
  1879. #endif
  1880. #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
  1881. # define JSON_HEDLEY_WARNING(msg) \
  1882. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  1883. JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS \
  1884. JSON_HEDLEY_PRAGMA(clang warning msg) \
  1885. JSON_HEDLEY_DIAGNOSTIC_POP
  1886. #elif \
  1887. JSON_HEDLEY_GCC_VERSION_CHECK(4,8,0) || \
  1888. JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \
  1889. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  1890. # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(GCC warning msg)
  1891. #elif \
  1892. JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) || \
  1893. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1894. # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(message(msg))
  1895. #else
  1896. # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_MESSAGE(msg)
  1897. #endif
  1898. #if defined(JSON_HEDLEY_REQUIRE)
  1899. #undef JSON_HEDLEY_REQUIRE
  1900. #endif
  1901. #if defined(JSON_HEDLEY_REQUIRE_MSG)
  1902. #undef JSON_HEDLEY_REQUIRE_MSG
  1903. #endif
  1904. #if JSON_HEDLEY_HAS_ATTRIBUTE(diagnose_if)
  1905. # if JSON_HEDLEY_HAS_WARNING("-Wgcc-compat")
  1906. # define JSON_HEDLEY_REQUIRE(expr) \
  1907. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  1908. _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \
  1909. __attribute__((diagnose_if(!(expr), #expr, "error"))) \
  1910. JSON_HEDLEY_DIAGNOSTIC_POP
  1911. # define JSON_HEDLEY_REQUIRE_MSG(expr,msg) \
  1912. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  1913. _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \
  1914. __attribute__((diagnose_if(!(expr), msg, "error"))) \
  1915. JSON_HEDLEY_DIAGNOSTIC_POP
  1916. # else
  1917. # define JSON_HEDLEY_REQUIRE(expr) __attribute__((diagnose_if(!(expr), #expr, "error")))
  1918. # define JSON_HEDLEY_REQUIRE_MSG(expr,msg) __attribute__((diagnose_if(!(expr), msg, "error")))
  1919. # endif
  1920. #else
  1921. # define JSON_HEDLEY_REQUIRE(expr)
  1922. # define JSON_HEDLEY_REQUIRE_MSG(expr,msg)
  1923. #endif
  1924. #if defined(JSON_HEDLEY_FLAGS)
  1925. #undef JSON_HEDLEY_FLAGS
  1926. #endif
  1927. #if JSON_HEDLEY_HAS_ATTRIBUTE(flag_enum) && (!defined(__cplusplus) || JSON_HEDLEY_HAS_WARNING("-Wbitfield-enum-conversion"))
  1928. #define JSON_HEDLEY_FLAGS __attribute__((__flag_enum__))
  1929. #else
  1930. #define JSON_HEDLEY_FLAGS
  1931. #endif
  1932. #if defined(JSON_HEDLEY_FLAGS_CAST)
  1933. #undef JSON_HEDLEY_FLAGS_CAST
  1934. #endif
  1935. #if JSON_HEDLEY_INTEL_VERSION_CHECK(19,0,0)
  1936. # define JSON_HEDLEY_FLAGS_CAST(T, expr) (__extension__ ({ \
  1937. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  1938. _Pragma("warning(disable:188)") \
  1939. ((T) (expr)); \
  1940. JSON_HEDLEY_DIAGNOSTIC_POP \
  1941. }))
  1942. #else
  1943. # define JSON_HEDLEY_FLAGS_CAST(T, expr) JSON_HEDLEY_STATIC_CAST(T, expr)
  1944. #endif
  1945. #if defined(JSON_HEDLEY_EMPTY_BASES)
  1946. #undef JSON_HEDLEY_EMPTY_BASES
  1947. #endif
  1948. #if \
  1949. (JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,23918) && !JSON_HEDLEY_MSVC_VERSION_CHECK(20,0,0)) || \
  1950. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1951. #define JSON_HEDLEY_EMPTY_BASES __declspec(empty_bases)
  1952. #else
  1953. #define JSON_HEDLEY_EMPTY_BASES
  1954. #endif
  1955. /* Remaining macros are deprecated. */
  1956. #if defined(JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK)
  1957. #undef JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK
  1958. #endif
  1959. #if defined(__clang__)
  1960. #define JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) (0)
  1961. #else
  1962. #define JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  1963. #endif
  1964. #if defined(JSON_HEDLEY_CLANG_HAS_ATTRIBUTE)
  1965. #undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE
  1966. #endif
  1967. #define JSON_HEDLEY_CLANG_HAS_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
  1968. #if defined(JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE)
  1969. #undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE
  1970. #endif
  1971. #define JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute)
  1972. #if defined(JSON_HEDLEY_CLANG_HAS_BUILTIN)
  1973. #undef JSON_HEDLEY_CLANG_HAS_BUILTIN
  1974. #endif
  1975. #define JSON_HEDLEY_CLANG_HAS_BUILTIN(builtin) JSON_HEDLEY_HAS_BUILTIN(builtin)
  1976. #if defined(JSON_HEDLEY_CLANG_HAS_FEATURE)
  1977. #undef JSON_HEDLEY_CLANG_HAS_FEATURE
  1978. #endif
  1979. #define JSON_HEDLEY_CLANG_HAS_FEATURE(feature) JSON_HEDLEY_HAS_FEATURE(feature)
  1980. #if defined(JSON_HEDLEY_CLANG_HAS_EXTENSION)
  1981. #undef JSON_HEDLEY_CLANG_HAS_EXTENSION
  1982. #endif
  1983. #define JSON_HEDLEY_CLANG_HAS_EXTENSION(extension) JSON_HEDLEY_HAS_EXTENSION(extension)
  1984. #if defined(JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE)
  1985. #undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE
  1986. #endif
  1987. #define JSON_HEDLEY_CLANG_HAS_DECLSPEC_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute)
  1988. #if defined(JSON_HEDLEY_CLANG_HAS_WARNING)
  1989. #undef JSON_HEDLEY_CLANG_HAS_WARNING
  1990. #endif
  1991. #define JSON_HEDLEY_CLANG_HAS_WARNING(warning) JSON_HEDLEY_HAS_WARNING(warning)
  1992. #endif /* !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < X) */
  1993. // #include <nlohmann/detail/meta/detected.hpp>
  1994. #include <type_traits>
  1995. // #include <nlohmann/detail/meta/void_t.hpp>
  1996. namespace nlohmann
  1997. {
  1998. namespace detail
  1999. {
  2000. template<typename ...Ts> struct make_void
  2001. {
  2002. using type = void;
  2003. };
  2004. template<typename ...Ts> using void_t = typename make_void<Ts...>::type;
  2005. } // namespace detail
  2006. } // namespace nlohmann
  2007. // https://en.cppreference.com/w/cpp/experimental/is_detected
  2008. namespace nlohmann
  2009. {
  2010. namespace detail
  2011. {
  2012. struct nonesuch
  2013. {
  2014. nonesuch() = delete;
  2015. ~nonesuch() = delete;
  2016. nonesuch(nonesuch const&) = delete;
  2017. nonesuch(nonesuch const&&) = delete;
  2018. void operator=(nonesuch const&) = delete;
  2019. void operator=(nonesuch&&) = delete;
  2020. };
  2021. template<class Default,
  2022. class AlwaysVoid,
  2023. template<class...> class Op,
  2024. class... Args>
  2025. struct detector
  2026. {
  2027. using value_t = std::false_type;
  2028. using type = Default;
  2029. };
  2030. template<class Default, template<class...> class Op, class... Args>
  2031. struct detector<Default, void_t<Op<Args...>>, Op, Args...>
  2032. {
  2033. using value_t = std::true_type;
  2034. using type = Op<Args...>;
  2035. };
  2036. template<template<class...> class Op, class... Args>
  2037. using is_detected = typename detector<nonesuch, void, Op, Args...>::value_t;
  2038. template<template<class...> class Op, class... Args>
  2039. struct is_detected_lazy : is_detected<Op, Args...> { };
  2040. template<template<class...> class Op, class... Args>
  2041. using detected_t = typename detector<nonesuch, void, Op, Args...>::type;
  2042. template<class Default, template<class...> class Op, class... Args>
  2043. using detected_or = detector<Default, void, Op, Args...>;
  2044. template<class Default, template<class...> class Op, class... Args>
  2045. using detected_or_t = typename detected_or<Default, Op, Args...>::type;
  2046. template<class Expected, template<class...> class Op, class... Args>
  2047. using is_detected_exact = std::is_same<Expected, detected_t<Op, Args...>>;
  2048. template<class To, template<class...> class Op, class... Args>
  2049. using is_detected_convertible =
  2050. std::is_convertible<detected_t<Op, Args...>, To>;
  2051. } // namespace detail
  2052. } // namespace nlohmann
  2053. // This file contains all internal macro definitions
  2054. // You MUST include macro_unscope.hpp at the end of json.hpp to undef all of them
  2055. // exclude unsupported compilers
  2056. #if !defined(JSON_SKIP_UNSUPPORTED_COMPILER_CHECK)
  2057. #if defined(__clang__)
  2058. #if (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) < 30400
  2059. #error "unsupported Clang version - see https://github.com/nlohmann/json#supported-compilers"
  2060. #endif
  2061. #elif defined(__GNUC__) && !(defined(__ICC) || defined(__INTEL_COMPILER))
  2062. #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40800
  2063. #error "unsupported GCC version - see https://github.com/nlohmann/json#supported-compilers"
  2064. #endif
  2065. #endif
  2066. #endif
  2067. // C++ language standard detection
  2068. // if the user manually specified the used c++ version this is skipped
  2069. #if !defined(JSON_HAS_CPP_20) && !defined(JSON_HAS_CPP_17) && !defined(JSON_HAS_CPP_14) && !defined(JSON_HAS_CPP_11)
  2070. #if (defined(__cplusplus) && __cplusplus >= 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L)
  2071. #define JSON_HAS_CPP_20
  2072. #define JSON_HAS_CPP_17
  2073. #define JSON_HAS_CPP_14
  2074. #elif (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464
  2075. #define JSON_HAS_CPP_17
  2076. #define JSON_HAS_CPP_14
  2077. #elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1)
  2078. #define JSON_HAS_CPP_14
  2079. #endif
  2080. // the cpp 11 flag is always specified because it is the minimal required version
  2081. #define JSON_HAS_CPP_11
  2082. #endif
  2083. #ifdef __has_include
  2084. #if __has_include(<version>)
  2085. #include <version>
  2086. #endif
  2087. #endif
  2088. #if !defined(JSON_HAS_FILESYSTEM) && !defined(JSON_HAS_EXPERIMENTAL_FILESYSTEM)
  2089. #ifdef JSON_HAS_CPP_17
  2090. #if defined(__cpp_lib_filesystem)
  2091. #define JSON_HAS_FILESYSTEM 1
  2092. #elif defined(__cpp_lib_experimental_filesystem)
  2093. #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
  2094. #elif !defined(__has_include)
  2095. #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
  2096. #elif __has_include(<filesystem>)
  2097. #define JSON_HAS_FILESYSTEM 1
  2098. #elif __has_include(<experimental/filesystem>)
  2099. #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
  2100. #endif
  2101. // std::filesystem does not work on MinGW GCC 8: https://sourceforge.net/p/mingw-w64/bugs/737/
  2102. #if defined(__MINGW32__) && defined(__GNUC__) && __GNUC__ == 8
  2103. #undef JSON_HAS_FILESYSTEM
  2104. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2105. #endif
  2106. // no filesystem support before GCC 8: https://en.cppreference.com/w/cpp/compiler_support
  2107. #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 8
  2108. #undef JSON_HAS_FILESYSTEM
  2109. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2110. #endif
  2111. // no filesystem support before Clang 7: https://en.cppreference.com/w/cpp/compiler_support
  2112. #if defined(__clang_major__) && __clang_major__ < 7
  2113. #undef JSON_HAS_FILESYSTEM
  2114. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2115. #endif
  2116. // no filesystem support before MSVC 19.14: https://en.cppreference.com/w/cpp/compiler_support
  2117. #if defined(_MSC_VER) && _MSC_VER < 1914
  2118. #undef JSON_HAS_FILESYSTEM
  2119. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2120. #endif
  2121. // no filesystem support before iOS 13
  2122. #if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED < 130000
  2123. #undef JSON_HAS_FILESYSTEM
  2124. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2125. #endif
  2126. // no filesystem support before macOS Catalina
  2127. #if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED < 101500
  2128. #undef JSON_HAS_FILESYSTEM
  2129. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2130. #endif
  2131. #endif
  2132. #endif
  2133. #ifndef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  2134. #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 0
  2135. #endif
  2136. #ifndef JSON_HAS_FILESYSTEM
  2137. #define JSON_HAS_FILESYSTEM 0
  2138. #endif
  2139. #ifndef JSON_HAS_THREE_WAY_COMPARISON
  2140. #if defined(__cpp_impl_three_way_comparison) && __cpp_impl_three_way_comparison >= 201907L \
  2141. && defined(__cpp_lib_three_way_comparison) && __cpp_lib_three_way_comparison >= 201907L
  2142. #define JSON_HAS_THREE_WAY_COMPARISON 1
  2143. #else
  2144. #define JSON_HAS_THREE_WAY_COMPARISON 0
  2145. #endif
  2146. #endif
  2147. #ifndef JSON_HAS_RANGES
  2148. // ranges header shipping in GCC 11.1.0 (released 2021-04-27) has syntax error
  2149. #if defined(__GLIBCXX__) && __GLIBCXX__ == 20210427
  2150. #define JSON_HAS_RANGES 0
  2151. #elif defined(__cpp_lib_ranges)
  2152. #define JSON_HAS_RANGES 1
  2153. #else
  2154. #define JSON_HAS_RANGES 0
  2155. #endif
  2156. #endif
  2157. #ifdef JSON_HAS_CPP_17
  2158. #define JSON_INLINE_VARIABLE inline
  2159. #else
  2160. #define JSON_INLINE_VARIABLE
  2161. #endif
  2162. #if JSON_HEDLEY_HAS_ATTRIBUTE(no_unique_address)
  2163. #define JSON_NO_UNIQUE_ADDRESS [[no_unique_address]]
  2164. #else
  2165. #define JSON_NO_UNIQUE_ADDRESS
  2166. #endif
  2167. // disable documentation warnings on clang
  2168. #if defined(__clang__)
  2169. #pragma clang diagnostic push
  2170. #pragma clang diagnostic ignored "-Wdocumentation"
  2171. #pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
  2172. #endif
  2173. // allow disabling exceptions
  2174. #if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION)
  2175. #define JSON_THROW(exception) throw exception
  2176. #define JSON_TRY try
  2177. #define JSON_CATCH(exception) catch(exception)
  2178. #define JSON_INTERNAL_CATCH(exception) catch(exception)
  2179. #else
  2180. #include <cstdlib>
  2181. #define JSON_THROW(exception) std::abort()
  2182. #define JSON_TRY if(true)
  2183. #define JSON_CATCH(exception) if(false)
  2184. #define JSON_INTERNAL_CATCH(exception) if(false)
  2185. #endif
  2186. // override exception macros
  2187. #if defined(JSON_THROW_USER)
  2188. #undef JSON_THROW
  2189. #define JSON_THROW JSON_THROW_USER
  2190. #endif
  2191. #if defined(JSON_TRY_USER)
  2192. #undef JSON_TRY
  2193. #define JSON_TRY JSON_TRY_USER
  2194. #endif
  2195. #if defined(JSON_CATCH_USER)
  2196. #undef JSON_CATCH
  2197. #define JSON_CATCH JSON_CATCH_USER
  2198. #undef JSON_INTERNAL_CATCH
  2199. #define JSON_INTERNAL_CATCH JSON_CATCH_USER
  2200. #endif
  2201. #if defined(JSON_INTERNAL_CATCH_USER)
  2202. #undef JSON_INTERNAL_CATCH
  2203. #define JSON_INTERNAL_CATCH JSON_INTERNAL_CATCH_USER
  2204. #endif
  2205. // allow overriding assert
  2206. #if !defined(JSON_ASSERT)
  2207. #include <cassert> // assert
  2208. #define JSON_ASSERT(x) assert(x)
  2209. #endif
  2210. // allow to access some private functions (needed by the test suite)
  2211. #if defined(JSON_TESTS_PRIVATE)
  2212. #define JSON_PRIVATE_UNLESS_TESTED public
  2213. #else
  2214. #define JSON_PRIVATE_UNLESS_TESTED private
  2215. #endif
  2216. /*!
  2217. @brief macro to briefly define a mapping between an enum and JSON
  2218. @def NLOHMANN_JSON_SERIALIZE_ENUM
  2219. @since version 3.4.0
  2220. */
  2221. #define NLOHMANN_JSON_SERIALIZE_ENUM(ENUM_TYPE, ...) \
  2222. template<typename BasicJsonType> \
  2223. inline void to_json(BasicJsonType& j, const ENUM_TYPE& e) \
  2224. { \
  2225. static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
  2226. static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
  2227. auto it = std::find_if(std::begin(m), std::end(m), \
  2228. [e](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
  2229. { \
  2230. return ej_pair.first == e; \
  2231. }); \
  2232. j = ((it != std::end(m)) ? it : std::begin(m))->second; \
  2233. } \
  2234. template<typename BasicJsonType> \
  2235. inline void from_json(const BasicJsonType& j, ENUM_TYPE& e) \
  2236. { \
  2237. static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
  2238. static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
  2239. auto it = std::find_if(std::begin(m), std::end(m), \
  2240. [&j](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
  2241. { \
  2242. return ej_pair.second == j; \
  2243. }); \
  2244. e = ((it != std::end(m)) ? it : std::begin(m))->first; \
  2245. }
  2246. // Ugly macros to avoid uglier copy-paste when specializing basic_json. They
  2247. // may be removed in the future once the class is split.
  2248. #define NLOHMANN_BASIC_JSON_TPL_DECLARATION \
  2249. template<template<typename, typename, typename...> class ObjectType, \
  2250. template<typename, typename...> class ArrayType, \
  2251. class StringType, class BooleanType, class NumberIntegerType, \
  2252. class NumberUnsignedType, class NumberFloatType, \
  2253. template<typename> class AllocatorType, \
  2254. template<typename, typename = void> class JSONSerializer, \
  2255. class BinaryType>
  2256. #define NLOHMANN_BASIC_JSON_TPL \
  2257. basic_json<ObjectType, ArrayType, StringType, BooleanType, \
  2258. NumberIntegerType, NumberUnsignedType, NumberFloatType, \
  2259. AllocatorType, JSONSerializer, BinaryType>
  2260. // Macros to simplify conversion from/to types
  2261. #define NLOHMANN_JSON_EXPAND( x ) x
  2262. #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
  2263. #define NLOHMANN_JSON_PASTE(...) NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_GET_MACRO(__VA_ARGS__, \
  2264. NLOHMANN_JSON_PASTE64, \
  2265. NLOHMANN_JSON_PASTE63, \
  2266. NLOHMANN_JSON_PASTE62, \
  2267. NLOHMANN_JSON_PASTE61, \
  2268. NLOHMANN_JSON_PASTE60, \
  2269. NLOHMANN_JSON_PASTE59, \
  2270. NLOHMANN_JSON_PASTE58, \
  2271. NLOHMANN_JSON_PASTE57, \
  2272. NLOHMANN_JSON_PASTE56, \
  2273. NLOHMANN_JSON_PASTE55, \
  2274. NLOHMANN_JSON_PASTE54, \
  2275. NLOHMANN_JSON_PASTE53, \
  2276. NLOHMANN_JSON_PASTE52, \
  2277. NLOHMANN_JSON_PASTE51, \
  2278. NLOHMANN_JSON_PASTE50, \
  2279. NLOHMANN_JSON_PASTE49, \
  2280. NLOHMANN_JSON_PASTE48, \
  2281. NLOHMANN_JSON_PASTE47, \
  2282. NLOHMANN_JSON_PASTE46, \
  2283. NLOHMANN_JSON_PASTE45, \
  2284. NLOHMANN_JSON_PASTE44, \
  2285. NLOHMANN_JSON_PASTE43, \
  2286. NLOHMANN_JSON_PASTE42, \
  2287. NLOHMANN_JSON_PASTE41, \
  2288. NLOHMANN_JSON_PASTE40, \
  2289. NLOHMANN_JSON_PASTE39, \
  2290. NLOHMANN_JSON_PASTE38, \
  2291. NLOHMANN_JSON_PASTE37, \
  2292. NLOHMANN_JSON_PASTE36, \
  2293. NLOHMANN_JSON_PASTE35, \
  2294. NLOHMANN_JSON_PASTE34, \
  2295. NLOHMANN_JSON_PASTE33, \
  2296. NLOHMANN_JSON_PASTE32, \
  2297. NLOHMANN_JSON_PASTE31, \
  2298. NLOHMANN_JSON_PASTE30, \
  2299. NLOHMANN_JSON_PASTE29, \
  2300. NLOHMANN_JSON_PASTE28, \
  2301. NLOHMANN_JSON_PASTE27, \
  2302. NLOHMANN_JSON_PASTE26, \
  2303. NLOHMANN_JSON_PASTE25, \
  2304. NLOHMANN_JSON_PASTE24, \
  2305. NLOHMANN_JSON_PASTE23, \
  2306. NLOHMANN_JSON_PASTE22, \
  2307. NLOHMANN_JSON_PASTE21, \
  2308. NLOHMANN_JSON_PASTE20, \
  2309. NLOHMANN_JSON_PASTE19, \
  2310. NLOHMANN_JSON_PASTE18, \
  2311. NLOHMANN_JSON_PASTE17, \
  2312. NLOHMANN_JSON_PASTE16, \
  2313. NLOHMANN_JSON_PASTE15, \
  2314. NLOHMANN_JSON_PASTE14, \
  2315. NLOHMANN_JSON_PASTE13, \
  2316. NLOHMANN_JSON_PASTE12, \
  2317. NLOHMANN_JSON_PASTE11, \
  2318. NLOHMANN_JSON_PASTE10, \
  2319. NLOHMANN_JSON_PASTE9, \
  2320. NLOHMANN_JSON_PASTE8, \
  2321. NLOHMANN_JSON_PASTE7, \
  2322. NLOHMANN_JSON_PASTE6, \
  2323. NLOHMANN_JSON_PASTE5, \
  2324. NLOHMANN_JSON_PASTE4, \
  2325. NLOHMANN_JSON_PASTE3, \
  2326. NLOHMANN_JSON_PASTE2, \
  2327. NLOHMANN_JSON_PASTE1)(__VA_ARGS__))
  2328. #define NLOHMANN_JSON_PASTE2(func, v1) func(v1)
  2329. #define NLOHMANN_JSON_PASTE3(func, v1, v2) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE2(func, v2)
  2330. #define NLOHMANN_JSON_PASTE4(func, v1, v2, v3) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE3(func, v2, v3)
  2331. #define NLOHMANN_JSON_PASTE5(func, v1, v2, v3, v4) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE4(func, v2, v3, v4)
  2332. #define NLOHMANN_JSON_PASTE6(func, v1, v2, v3, v4, v5) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE5(func, v2, v3, v4, v5)
  2333. #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)
  2334. #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)
  2335. #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)
  2336. #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)
  2337. #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)
  2338. #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)
  2339. #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)
  2340. #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)
  2341. #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)
  2342. #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)
  2343. #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)
  2344. #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)
  2345. #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)
  2346. #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)
  2347. #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)
  2348. #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)
  2349. #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)
  2350. #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)
  2351. #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)
  2352. #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)
  2353. #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)
  2354. #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)
  2355. #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)
  2356. #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)
  2357. #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)
  2358. #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)
  2359. #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)
  2360. #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)
  2361. #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)
  2362. #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)
  2363. #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)
  2364. #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)
  2365. #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)
  2366. #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)
  2367. #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)
  2368. #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)
  2369. #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)
  2370. #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)
  2371. #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)
  2372. #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)
  2373. #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)
  2374. #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)
  2375. #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)
  2376. #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)
  2377. #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)
  2378. #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)
  2379. #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)
  2380. #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)
  2381. #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)
  2382. #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)
  2383. #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)
  2384. #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)
  2385. #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)
  2386. #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)
  2387. #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)
  2388. #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)
  2389. #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)
  2390. #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)
  2391. #define NLOHMANN_JSON_TO(v1) nlohmann_json_j[#v1] = nlohmann_json_t.v1;
  2392. #define NLOHMANN_JSON_FROM(v1) nlohmann_json_j.at(#v1).get_to(nlohmann_json_t.v1);
  2393. #define NLOHMANN_JSON_FROM_WITH_DEFAULT(v1) nlohmann_json_t.v1 = nlohmann_json_j.value(#v1, nlohmann_json_default_obj.v1);
  2394. /*!
  2395. @brief macro
  2396. @def NLOHMANN_DEFINE_TYPE_INTRUSIVE
  2397. @since version 3.9.0
  2398. */
  2399. #define NLOHMANN_DEFINE_TYPE_INTRUSIVE(Type, ...) \
  2400. 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__)) } \
  2401. 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__)) }
  2402. #define NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(Type, ...) \
  2403. 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__)) } \
  2404. 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__)) }
  2405. /*!
  2406. @brief macro
  2407. @def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE
  2408. @since version 3.9.0
  2409. */
  2410. #define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Type, ...) \
  2411. 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__)) } \
  2412. 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__)) }
  2413. #define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Type, ...) \
  2414. 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__)) } \
  2415. 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__)) }
  2416. // inspired from https://stackoverflow.com/a/26745591
  2417. // allows to call any std function as if (e.g. with begin):
  2418. // using std::begin; begin(x);
  2419. //
  2420. // it allows using the detected idiom to retrieve the return type
  2421. // of such an expression
  2422. #define NLOHMANN_CAN_CALL_STD_FUNC_IMPL(std_name) \
  2423. namespace detail { \
  2424. using std::std_name; \
  2425. \
  2426. template<typename... T> \
  2427. using result_of_##std_name = decltype(std_name(std::declval<T>()...)); \
  2428. } \
  2429. \
  2430. namespace detail2 { \
  2431. struct std_name##_tag \
  2432. { \
  2433. }; \
  2434. \
  2435. template<typename... T> \
  2436. std_name##_tag std_name(T&&...); \
  2437. \
  2438. template<typename... T> \
  2439. using result_of_##std_name = decltype(std_name(std::declval<T>()...)); \
  2440. \
  2441. template<typename... T> \
  2442. struct would_call_std_##std_name \
  2443. { \
  2444. static constexpr auto const value = ::nlohmann::detail:: \
  2445. is_detected_exact<std_name##_tag, result_of_##std_name, T...>::value; \
  2446. }; \
  2447. } /* namespace detail2 */ \
  2448. \
  2449. template<typename... T> \
  2450. struct would_call_std_##std_name : detail2::would_call_std_##std_name<T...> \
  2451. { \
  2452. }
  2453. #ifndef JSON_USE_IMPLICIT_CONVERSIONS
  2454. #define JSON_USE_IMPLICIT_CONVERSIONS 1
  2455. #endif
  2456. #if JSON_USE_IMPLICIT_CONVERSIONS
  2457. #define JSON_EXPLICIT
  2458. #else
  2459. #define JSON_EXPLICIT explicit
  2460. #endif
  2461. #ifndef JSON_DIAGNOSTICS
  2462. #define JSON_DIAGNOSTICS 0
  2463. #endif
  2464. #ifndef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  2465. #define JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON 0
  2466. #endif
  2467. #ifndef JSON_DISABLE_ENUM_SERIALIZATION
  2468. #define JSON_DISABLE_ENUM_SERIALIZATION 0
  2469. #endif
  2470. #if JSON_HAS_THREE_WAY_COMPARISON
  2471. #include <compare> // partial_ordering
  2472. #endif
  2473. namespace nlohmann
  2474. {
  2475. namespace detail
  2476. {
  2477. ///////////////////////////
  2478. // JSON type enumeration //
  2479. ///////////////////////////
  2480. /*!
  2481. @brief the JSON type enumeration
  2482. This enumeration collects the different JSON types. It is internally used to
  2483. distinguish the stored values, and the functions @ref basic_json::is_null(),
  2484. @ref basic_json::is_object(), @ref basic_json::is_array(),
  2485. @ref basic_json::is_string(), @ref basic_json::is_boolean(),
  2486. @ref basic_json::is_number() (with @ref basic_json::is_number_integer(),
  2487. @ref basic_json::is_number_unsigned(), and @ref basic_json::is_number_float()),
  2488. @ref basic_json::is_discarded(), @ref basic_json::is_primitive(), and
  2489. @ref basic_json::is_structured() rely on it.
  2490. @note There are three enumeration entries (number_integer, number_unsigned, and
  2491. number_float), because the library distinguishes these three types for numbers:
  2492. @ref basic_json::number_unsigned_t is used for unsigned integers,
  2493. @ref basic_json::number_integer_t is used for signed integers, and
  2494. @ref basic_json::number_float_t is used for floating-point numbers or to
  2495. approximate integers which do not fit in the limits of their respective type.
  2496. @sa see @ref basic_json::basic_json(const value_t value_type) -- create a JSON
  2497. value with the default value for a given type
  2498. @since version 1.0.0
  2499. */
  2500. enum class value_t : std::uint8_t
  2501. {
  2502. null, ///< null value
  2503. object, ///< object (unordered set of name/value pairs)
  2504. array, ///< array (ordered collection of values)
  2505. string, ///< string value
  2506. boolean, ///< boolean value
  2507. number_integer, ///< number value (signed integer)
  2508. number_unsigned, ///< number value (unsigned integer)
  2509. number_float, ///< number value (floating-point)
  2510. binary, ///< binary array (ordered collection of bytes)
  2511. discarded ///< discarded by the parser callback function
  2512. };
  2513. /*!
  2514. @brief comparison operator for JSON types
  2515. Returns an ordering that is similar to Python:
  2516. - order: null < boolean < number < object < array < string < binary
  2517. - furthermore, each type is not smaller than itself
  2518. - discarded values are not comparable
  2519. - binary is represented as a b"" string in python and directly comparable to a
  2520. string; however, making a binary array directly comparable with a string would
  2521. be surprising behavior in a JSON file.
  2522. @since version 1.0.0
  2523. */
  2524. #if JSON_HAS_THREE_WAY_COMPARISON
  2525. inline std::partial_ordering operator<=>(const value_t lhs, const value_t rhs) noexcept // *NOPAD*
  2526. #else
  2527. inline bool operator<(const value_t lhs, const value_t rhs) noexcept
  2528. #endif
  2529. {
  2530. static constexpr std::array<std::uint8_t, 9> order = {{
  2531. 0 /* null */, 3 /* object */, 4 /* array */, 5 /* string */,
  2532. 1 /* boolean */, 2 /* integer */, 2 /* unsigned */, 2 /* float */,
  2533. 6 /* binary */
  2534. }
  2535. };
  2536. const auto l_index = static_cast<std::size_t>(lhs);
  2537. const auto r_index = static_cast<std::size_t>(rhs);
  2538. #if JSON_HAS_THREE_WAY_COMPARISON
  2539. if (l_index < order.size() && r_index < order.size())
  2540. {
  2541. return order[l_index] <=> order[r_index]; // *NOPAD*
  2542. }
  2543. return std::partial_ordering::unordered;
  2544. #else
  2545. return l_index < order.size() && r_index < order.size() && order[l_index] < order[r_index];
  2546. #endif
  2547. }
  2548. // GCC selects the built-in operator< over an operator rewritten from
  2549. // a user-defined spaceship operator
  2550. // Clang, MSVC, and ICC select the rewritten candidate
  2551. // (see GCC bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105200)
  2552. #if JSON_HAS_THREE_WAY_COMPARISON && defined(__GNUC__)
  2553. inline bool operator<(const value_t lhs, const value_t rhs) noexcept
  2554. {
  2555. return std::is_lt(lhs <=> rhs); // *NOPAD*
  2556. }
  2557. #endif
  2558. } // namespace detail
  2559. } // namespace nlohmann
  2560. // #include <nlohmann/detail/string_escape.hpp>
  2561. // #include <nlohmann/detail/macro_scope.hpp>
  2562. namespace nlohmann
  2563. {
  2564. namespace detail
  2565. {
  2566. /*!
  2567. @brief replace all occurrences of a substring by another string
  2568. @param[in,out] s the string to manipulate; changed so that all
  2569. occurrences of @a f are replaced with @a t
  2570. @param[in] f the substring to replace with @a t
  2571. @param[in] t the string to replace @a f
  2572. @pre The search string @a f must not be empty. **This precondition is
  2573. enforced with an assertion.**
  2574. @since version 2.0.0
  2575. */
  2576. template<typename StringType>
  2577. inline void replace_substring(StringType& s, const StringType& f,
  2578. const StringType& t)
  2579. {
  2580. JSON_ASSERT(!f.empty());
  2581. for (auto pos = s.find(f); // find first occurrence of f
  2582. pos != StringType::npos; // make sure f was found
  2583. s.replace(pos, f.size(), t), // replace with t, and
  2584. pos = s.find(f, pos + t.size())) // find next occurrence of f
  2585. {}
  2586. }
  2587. /*!
  2588. * @brief string escaping as described in RFC 6901 (Sect. 4)
  2589. * @param[in] s string to escape
  2590. * @return escaped string
  2591. *
  2592. * Note the order of escaping "~" to "~0" and "/" to "~1" is important.
  2593. */
  2594. template<typename StringType>
  2595. inline StringType escape(StringType s)
  2596. {
  2597. replace_substring(s, StringType{"~"}, StringType{"~0"});
  2598. replace_substring(s, StringType{"/"}, StringType{"~1"});
  2599. return s;
  2600. }
  2601. /*!
  2602. * @brief string unescaping as described in RFC 6901 (Sect. 4)
  2603. * @param[in] s string to unescape
  2604. * @return unescaped string
  2605. *
  2606. * Note the order of escaping "~1" to "/" and "~0" to "~" is important.
  2607. */
  2608. template<typename StringType>
  2609. static void unescape(StringType& s)
  2610. {
  2611. replace_substring(s, StringType{"~1"}, StringType{"/"});
  2612. replace_substring(s, StringType{"~0"}, StringType{"~"});
  2613. }
  2614. } // namespace detail
  2615. } // namespace nlohmann
  2616. // #include <nlohmann/detail/input/position_t.hpp>
  2617. #include <cstddef> // size_t
  2618. namespace nlohmann
  2619. {
  2620. namespace detail
  2621. {
  2622. /// struct to capture the start position of the current token
  2623. struct position_t
  2624. {
  2625. /// the total number of characters read
  2626. std::size_t chars_read_total = 0;
  2627. /// the number of characters read in the current line
  2628. std::size_t chars_read_current_line = 0;
  2629. /// the number of lines read
  2630. std::size_t lines_read = 0;
  2631. /// conversion to size_t to preserve SAX interface
  2632. constexpr operator size_t() const
  2633. {
  2634. return chars_read_total;
  2635. }
  2636. };
  2637. } // namespace detail
  2638. } // namespace nlohmann
  2639. // #include <nlohmann/detail/macro_scope.hpp>
  2640. // #include <nlohmann/detail/meta/cpp_future.hpp>
  2641. #include <cstddef> // size_t
  2642. #include <type_traits> // conditional, enable_if, false_type, integral_constant, is_constructible, is_integral, is_same, remove_cv, remove_reference, true_type
  2643. #include <utility> // index_sequence, make_index_sequence, index_sequence_for
  2644. // #include <nlohmann/detail/macro_scope.hpp>
  2645. namespace nlohmann
  2646. {
  2647. namespace detail
  2648. {
  2649. template<typename T>
  2650. using uncvref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
  2651. #ifdef JSON_HAS_CPP_14
  2652. // the following utilities are natively available in C++14
  2653. using std::enable_if_t;
  2654. using std::index_sequence;
  2655. using std::make_index_sequence;
  2656. using std::index_sequence_for;
  2657. #else
  2658. // alias templates to reduce boilerplate
  2659. template<bool B, typename T = void>
  2660. using enable_if_t = typename std::enable_if<B, T>::type;
  2661. // The following code is taken from https://github.com/abseil/abseil-cpp/blob/10cb35e459f5ecca5b2ff107635da0bfa41011b4/absl/utility/utility.h
  2662. // which is part of Google Abseil (https://github.com/abseil/abseil-cpp), licensed under the Apache License 2.0.
  2663. //// START OF CODE FROM GOOGLE ABSEIL
  2664. // integer_sequence
  2665. //
  2666. // Class template representing a compile-time integer sequence. An instantiation
  2667. // of `integer_sequence<T, Ints...>` has a sequence of integers encoded in its
  2668. // type through its template arguments (which is a common need when
  2669. // working with C++11 variadic templates). `absl::integer_sequence` is designed
  2670. // to be a drop-in replacement for C++14's `std::integer_sequence`.
  2671. //
  2672. // Example:
  2673. //
  2674. // template< class T, T... Ints >
  2675. // void user_function(integer_sequence<T, Ints...>);
  2676. //
  2677. // int main()
  2678. // {
  2679. // // user_function's `T` will be deduced to `int` and `Ints...`
  2680. // // will be deduced to `0, 1, 2, 3, 4`.
  2681. // user_function(make_integer_sequence<int, 5>());
  2682. // }
  2683. template <typename T, T... Ints>
  2684. struct integer_sequence
  2685. {
  2686. using value_type = T;
  2687. static constexpr std::size_t size() noexcept
  2688. {
  2689. return sizeof...(Ints);
  2690. }
  2691. };
  2692. // index_sequence
  2693. //
  2694. // A helper template for an `integer_sequence` of `size_t`,
  2695. // `absl::index_sequence` is designed to be a drop-in replacement for C++14's
  2696. // `std::index_sequence`.
  2697. template <size_t... Ints>
  2698. using index_sequence = integer_sequence<size_t, Ints...>;
  2699. namespace utility_internal
  2700. {
  2701. template <typename Seq, size_t SeqSize, size_t Rem>
  2702. struct Extend;
  2703. // Note that SeqSize == sizeof...(Ints). It's passed explicitly for efficiency.
  2704. template <typename T, T... Ints, size_t SeqSize>
  2705. struct Extend<integer_sequence<T, Ints...>, SeqSize, 0>
  2706. {
  2707. using type = integer_sequence < T, Ints..., (Ints + SeqSize)... >;
  2708. };
  2709. template <typename T, T... Ints, size_t SeqSize>
  2710. struct Extend<integer_sequence<T, Ints...>, SeqSize, 1>
  2711. {
  2712. using type = integer_sequence < T, Ints..., (Ints + SeqSize)..., 2 * SeqSize >;
  2713. };
  2714. // Recursion helper for 'make_integer_sequence<T, N>'.
  2715. // 'Gen<T, N>::type' is an alias for 'integer_sequence<T, 0, 1, ... N-1>'.
  2716. template <typename T, size_t N>
  2717. struct Gen
  2718. {
  2719. using type =
  2720. typename Extend < typename Gen < T, N / 2 >::type, N / 2, N % 2 >::type;
  2721. };
  2722. template <typename T>
  2723. struct Gen<T, 0>
  2724. {
  2725. using type = integer_sequence<T>;
  2726. };
  2727. } // namespace utility_internal
  2728. // Compile-time sequences of integers
  2729. // make_integer_sequence
  2730. //
  2731. // This template alias is equivalent to
  2732. // `integer_sequence<int, 0, 1, ..., N-1>`, and is designed to be a drop-in
  2733. // replacement for C++14's `std::make_integer_sequence`.
  2734. template <typename T, T N>
  2735. using make_integer_sequence = typename utility_internal::Gen<T, N>::type;
  2736. // make_index_sequence
  2737. //
  2738. // This template alias is equivalent to `index_sequence<0, 1, ..., N-1>`,
  2739. // and is designed to be a drop-in replacement for C++14's
  2740. // `std::make_index_sequence`.
  2741. template <size_t N>
  2742. using make_index_sequence = make_integer_sequence<size_t, N>;
  2743. // index_sequence_for
  2744. //
  2745. // Converts a typename pack into an index sequence of the same length, and
  2746. // is designed to be a drop-in replacement for C++14's
  2747. // `std::index_sequence_for()`
  2748. template <typename... Ts>
  2749. using index_sequence_for = make_index_sequence<sizeof...(Ts)>;
  2750. //// END OF CODE FROM GOOGLE ABSEIL
  2751. #endif
  2752. // dispatch utility (taken from ranges-v3)
  2753. template<unsigned N> struct priority_tag : priority_tag < N - 1 > {};
  2754. template<> struct priority_tag<0> {};
  2755. // taken from ranges-v3
  2756. template<typename T>
  2757. struct static_const
  2758. {
  2759. static constexpr T value{};
  2760. };
  2761. #ifndef JSON_HAS_CPP_17
  2762. template<typename T>
  2763. constexpr T static_const<T>::value; // NOLINT(readability-redundant-declaration)
  2764. #endif
  2765. } // namespace detail
  2766. } // namespace nlohmann
  2767. // #include <nlohmann/detail/meta/type_traits.hpp>
  2768. #include <limits> // numeric_limits
  2769. #include <type_traits> // false_type, is_constructible, is_integral, is_same, true_type
  2770. #include <utility> // declval
  2771. #include <tuple> // tuple
  2772. // #include <nlohmann/detail/macro_scope.hpp>
  2773. // #include <nlohmann/detail/iterators/iterator_traits.hpp>
  2774. #include <iterator> // random_access_iterator_tag
  2775. // #include <nlohmann/detail/meta/void_t.hpp>
  2776. // #include <nlohmann/detail/meta/cpp_future.hpp>
  2777. namespace nlohmann
  2778. {
  2779. namespace detail
  2780. {
  2781. template<typename It, typename = void>
  2782. struct iterator_types {};
  2783. template<typename It>
  2784. struct iterator_types <
  2785. It,
  2786. void_t<typename It::difference_type, typename It::value_type, typename It::pointer,
  2787. typename It::reference, typename It::iterator_category >>
  2788. {
  2789. using difference_type = typename It::difference_type;
  2790. using value_type = typename It::value_type;
  2791. using pointer = typename It::pointer;
  2792. using reference = typename It::reference;
  2793. using iterator_category = typename It::iterator_category;
  2794. };
  2795. // This is required as some compilers implement std::iterator_traits in a way that
  2796. // doesn't work with SFINAE. See https://github.com/nlohmann/json/issues/1341.
  2797. template<typename T, typename = void>
  2798. struct iterator_traits
  2799. {
  2800. };
  2801. template<typename T>
  2802. struct iterator_traits < T, enable_if_t < !std::is_pointer<T>::value >>
  2803. : iterator_types<T>
  2804. {
  2805. };
  2806. template<typename T>
  2807. struct iterator_traits<T*, enable_if_t<std::is_object<T>::value>>
  2808. {
  2809. using iterator_category = std::random_access_iterator_tag;
  2810. using value_type = T;
  2811. using difference_type = ptrdiff_t;
  2812. using pointer = T*;
  2813. using reference = T&;
  2814. };
  2815. } // namespace detail
  2816. } // namespace nlohmann
  2817. // #include <nlohmann/detail/meta/call_std/begin.hpp>
  2818. // #include <nlohmann/detail/macro_scope.hpp>
  2819. namespace nlohmann
  2820. {
  2821. NLOHMANN_CAN_CALL_STD_FUNC_IMPL(begin);
  2822. } // namespace nlohmann
  2823. // #include <nlohmann/detail/meta/call_std/end.hpp>
  2824. // #include <nlohmann/detail/macro_scope.hpp>
  2825. namespace nlohmann
  2826. {
  2827. NLOHMANN_CAN_CALL_STD_FUNC_IMPL(end);
  2828. } // namespace nlohmann
  2829. // #include <nlohmann/detail/meta/cpp_future.hpp>
  2830. // #include <nlohmann/detail/meta/detected.hpp>
  2831. // #include <nlohmann/json_fwd.hpp>
  2832. #ifndef INCLUDE_NLOHMANN_JSON_FWD_HPP_
  2833. #define INCLUDE_NLOHMANN_JSON_FWD_HPP_
  2834. #include <cstdint> // int64_t, uint64_t
  2835. #include <map> // map
  2836. #include <memory> // allocator
  2837. #include <string> // string
  2838. #include <vector> // vector
  2839. /*!
  2840. @brief namespace for Niels Lohmann
  2841. @see https://github.com/nlohmann
  2842. @since version 1.0.0
  2843. */
  2844. namespace nlohmann
  2845. {
  2846. /*!
  2847. @brief default JSONSerializer template argument
  2848. This serializer ignores the template arguments and uses ADL
  2849. ([argument-dependent lookup](https://en.cppreference.com/w/cpp/language/adl))
  2850. for serialization.
  2851. */
  2852. template<typename T = void, typename SFINAE = void>
  2853. struct adl_serializer;
  2854. /// a class to store JSON values
  2855. /// @sa https://json.nlohmann.me/api/basic_json/
  2856. template<template<typename U, typename V, typename... Args> class ObjectType =
  2857. std::map,
  2858. template<typename U, typename... Args> class ArrayType = std::vector,
  2859. class StringType = std::string, class BooleanType = bool,
  2860. class NumberIntegerType = std::int64_t,
  2861. class NumberUnsignedType = std::uint64_t,
  2862. class NumberFloatType = double,
  2863. template<typename U> class AllocatorType = std::allocator,
  2864. template<typename T, typename SFINAE = void> class JSONSerializer =
  2865. adl_serializer,
  2866. class BinaryType = std::vector<std::uint8_t>>
  2867. class basic_json;
  2868. /// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document
  2869. /// @sa https://json.nlohmann.me/api/json_pointer/
  2870. template<typename BasicJsonType>
  2871. class json_pointer;
  2872. /*!
  2873. @brief default specialization
  2874. @sa https://json.nlohmann.me/api/json/
  2875. */
  2876. using json = basic_json<>;
  2877. /// @brief a minimal map-like container that preserves insertion order
  2878. /// @sa https://json.nlohmann.me/api/ordered_map/
  2879. template<class Key, class T, class IgnoredLess, class Allocator>
  2880. struct ordered_map;
  2881. /// @brief specialization that maintains the insertion order of object keys
  2882. /// @sa https://json.nlohmann.me/api/ordered_json/
  2883. using ordered_json = basic_json<nlohmann::ordered_map>;
  2884. } // namespace nlohmann
  2885. #endif // INCLUDE_NLOHMANN_JSON_FWD_HPP_
  2886. namespace nlohmann
  2887. {
  2888. /*!
  2889. @brief detail namespace with internal helper functions
  2890. This namespace collects functions that should not be exposed,
  2891. implementations of some @ref basic_json methods, and meta-programming helpers.
  2892. @since version 2.1.0
  2893. */
  2894. namespace detail
  2895. {
  2896. /////////////
  2897. // helpers //
  2898. /////////////
  2899. // Note to maintainers:
  2900. //
  2901. // Every trait in this file expects a non CV-qualified type.
  2902. // The only exceptions are in the 'aliases for detected' section
  2903. // (i.e. those of the form: decltype(T::member_function(std::declval<T>())))
  2904. //
  2905. // In this case, T has to be properly CV-qualified to constraint the function arguments
  2906. // (e.g. to_json(BasicJsonType&, const T&))
  2907. template<typename> struct is_basic_json : std::false_type {};
  2908. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  2909. struct is_basic_json<NLOHMANN_BASIC_JSON_TPL> : std::true_type {};
  2910. // used by exceptions create() member functions
  2911. // true_type for pointer to possibly cv-qualified basic_json or std::nullptr_t
  2912. // false_type otherwise
  2913. template<typename BasicJsonContext>
  2914. struct is_basic_json_context :
  2915. std::integral_constant < bool,
  2916. is_basic_json<typename std::remove_cv<typename std::remove_pointer<BasicJsonContext>::type>::type>::value
  2917. || std::is_same<BasicJsonContext, std::nullptr_t>::value >
  2918. {};
  2919. //////////////////////
  2920. // json_ref helpers //
  2921. //////////////////////
  2922. template<typename>
  2923. class json_ref;
  2924. template<typename>
  2925. struct is_json_ref : std::false_type {};
  2926. template<typename T>
  2927. struct is_json_ref<json_ref<T>> : std::true_type {};
  2928. //////////////////////////
  2929. // aliases for detected //
  2930. //////////////////////////
  2931. template<typename T>
  2932. using mapped_type_t = typename T::mapped_type;
  2933. template<typename T>
  2934. using key_type_t = typename T::key_type;
  2935. template<typename T>
  2936. using value_type_t = typename T::value_type;
  2937. template<typename T>
  2938. using difference_type_t = typename T::difference_type;
  2939. template<typename T>
  2940. using pointer_t = typename T::pointer;
  2941. template<typename T>
  2942. using reference_t = typename T::reference;
  2943. template<typename T>
  2944. using iterator_category_t = typename T::iterator_category;
  2945. template<typename T, typename... Args>
  2946. using to_json_function = decltype(T::to_json(std::declval<Args>()...));
  2947. template<typename T, typename... Args>
  2948. using from_json_function = decltype(T::from_json(std::declval<Args>()...));
  2949. template<typename T, typename U>
  2950. using get_template_function = decltype(std::declval<T>().template get<U>());
  2951. // trait checking if JSONSerializer<T>::from_json(json const&, udt&) exists
  2952. template<typename BasicJsonType, typename T, typename = void>
  2953. struct has_from_json : std::false_type {};
  2954. // trait checking if j.get<T> is valid
  2955. // use this trait instead of std::is_constructible or std::is_convertible,
  2956. // both rely on, or make use of implicit conversions, and thus fail when T
  2957. // has several constructors/operator= (see https://github.com/nlohmann/json/issues/958)
  2958. template <typename BasicJsonType, typename T>
  2959. struct is_getable
  2960. {
  2961. static constexpr bool value = is_detected<get_template_function, const BasicJsonType&, T>::value;
  2962. };
  2963. template<typename BasicJsonType, typename T>
  2964. struct has_from_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
  2965. {
  2966. using serializer = typename BasicJsonType::template json_serializer<T, void>;
  2967. static constexpr bool value =
  2968. is_detected_exact<void, from_json_function, serializer,
  2969. const BasicJsonType&, T&>::value;
  2970. };
  2971. // This trait checks if JSONSerializer<T>::from_json(json const&) exists
  2972. // this overload is used for non-default-constructible user-defined-types
  2973. template<typename BasicJsonType, typename T, typename = void>
  2974. struct has_non_default_from_json : std::false_type {};
  2975. template<typename BasicJsonType, typename T>
  2976. struct has_non_default_from_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
  2977. {
  2978. using serializer = typename BasicJsonType::template json_serializer<T, void>;
  2979. static constexpr bool value =
  2980. is_detected_exact<T, from_json_function, serializer,
  2981. const BasicJsonType&>::value;
  2982. };
  2983. // This trait checks if BasicJsonType::json_serializer<T>::to_json exists
  2984. // Do not evaluate the trait when T is a basic_json type, to avoid template instantiation infinite recursion.
  2985. template<typename BasicJsonType, typename T, typename = void>
  2986. struct has_to_json : std::false_type {};
  2987. template<typename BasicJsonType, typename T>
  2988. struct has_to_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
  2989. {
  2990. using serializer = typename BasicJsonType::template json_serializer<T, void>;
  2991. static constexpr bool value =
  2992. is_detected_exact<void, to_json_function, serializer, BasicJsonType&,
  2993. T>::value;
  2994. };
  2995. template<typename T>
  2996. using detect_key_compare = typename T::key_compare;
  2997. template<typename T>
  2998. struct has_key_compare : std::integral_constant<bool, is_detected<detect_key_compare, T>::value> {};
  2999. // obtains the actual object key comparator
  3000. template<typename BasicJsonType>
  3001. struct actual_object_comparator
  3002. {
  3003. using object_t = typename BasicJsonType::object_t;
  3004. using object_comparator_t = typename BasicJsonType::default_object_comparator_t;
  3005. using type = typename std::conditional < has_key_compare<object_t>::value,
  3006. typename object_t::key_compare, object_comparator_t>::type;
  3007. };
  3008. template<typename BasicJsonType>
  3009. using actual_object_comparator_t = typename actual_object_comparator<BasicJsonType>::type;
  3010. ///////////////////
  3011. // is_ functions //
  3012. ///////////////////
  3013. // https://en.cppreference.com/w/cpp/types/conjunction
  3014. template<class...> struct conjunction : std::true_type { };
  3015. template<class B> struct conjunction<B> : B { };
  3016. template<class B, class... Bn>
  3017. struct conjunction<B, Bn...>
  3018. : std::conditional<static_cast<bool>(B::value), conjunction<Bn...>, B>::type {};
  3019. // https://en.cppreference.com/w/cpp/types/negation
  3020. template<class B> struct negation : std::integral_constant < bool, !B::value > { };
  3021. // Reimplementation of is_constructible and is_default_constructible, due to them being broken for
  3022. // std::pair and std::tuple until LWG 2367 fix (see https://cplusplus.github.io/LWG/lwg-defects.html#2367).
  3023. // This causes compile errors in e.g. clang 3.5 or gcc 4.9.
  3024. template <typename T>
  3025. struct is_default_constructible : std::is_default_constructible<T> {};
  3026. template <typename T1, typename T2>
  3027. struct is_default_constructible<std::pair<T1, T2>>
  3028. : conjunction<is_default_constructible<T1>, is_default_constructible<T2>> {};
  3029. template <typename T1, typename T2>
  3030. struct is_default_constructible<const std::pair<T1, T2>>
  3031. : conjunction<is_default_constructible<T1>, is_default_constructible<T2>> {};
  3032. template <typename... Ts>
  3033. struct is_default_constructible<std::tuple<Ts...>>
  3034. : conjunction<is_default_constructible<Ts>...> {};
  3035. template <typename... Ts>
  3036. struct is_default_constructible<const std::tuple<Ts...>>
  3037. : conjunction<is_default_constructible<Ts>...> {};
  3038. template <typename T, typename... Args>
  3039. struct is_constructible : std::is_constructible<T, Args...> {};
  3040. template <typename T1, typename T2>
  3041. struct is_constructible<std::pair<T1, T2>> : is_default_constructible<std::pair<T1, T2>> {};
  3042. template <typename T1, typename T2>
  3043. struct is_constructible<const std::pair<T1, T2>> : is_default_constructible<const std::pair<T1, T2>> {};
  3044. template <typename... Ts>
  3045. struct is_constructible<std::tuple<Ts...>> : is_default_constructible<std::tuple<Ts...>> {};
  3046. template <typename... Ts>
  3047. struct is_constructible<const std::tuple<Ts...>> : is_default_constructible<const std::tuple<Ts...>> {};
  3048. template<typename T, typename = void>
  3049. struct is_iterator_traits : std::false_type {};
  3050. template<typename T>
  3051. struct is_iterator_traits<iterator_traits<T>>
  3052. {
  3053. private:
  3054. using traits = iterator_traits<T>;
  3055. public:
  3056. static constexpr auto value =
  3057. is_detected<value_type_t, traits>::value &&
  3058. is_detected<difference_type_t, traits>::value &&
  3059. is_detected<pointer_t, traits>::value &&
  3060. is_detected<iterator_category_t, traits>::value &&
  3061. is_detected<reference_t, traits>::value;
  3062. };
  3063. template<typename T>
  3064. struct is_range
  3065. {
  3066. private:
  3067. using t_ref = typename std::add_lvalue_reference<T>::type;
  3068. using iterator = detected_t<result_of_begin, t_ref>;
  3069. using sentinel = detected_t<result_of_end, t_ref>;
  3070. // to be 100% correct, it should use https://en.cppreference.com/w/cpp/iterator/input_or_output_iterator
  3071. // and https://en.cppreference.com/w/cpp/iterator/sentinel_for
  3072. // but reimplementing these would be too much work, as a lot of other concepts are used underneath
  3073. static constexpr auto is_iterator_begin =
  3074. is_iterator_traits<iterator_traits<iterator>>::value;
  3075. public:
  3076. static constexpr bool value = !std::is_same<iterator, nonesuch>::value && !std::is_same<sentinel, nonesuch>::value && is_iterator_begin;
  3077. };
  3078. template<typename R>
  3079. using iterator_t = enable_if_t<is_range<R>::value, result_of_begin<decltype(std::declval<R&>())>>;
  3080. template<typename T>
  3081. using range_value_t = value_type_t<iterator_traits<iterator_t<T>>>;
  3082. // The following implementation of is_complete_type is taken from
  3083. // https://blogs.msdn.microsoft.com/vcblog/2015/12/02/partial-support-for-expression-sfinae-in-vs-2015-update-1/
  3084. // and is written by Xiang Fan who agreed to using it in this library.
  3085. template<typename T, typename = void>
  3086. struct is_complete_type : std::false_type {};
  3087. template<typename T>
  3088. struct is_complete_type<T, decltype(void(sizeof(T)))> : std::true_type {};
  3089. template<typename BasicJsonType, typename CompatibleObjectType,
  3090. typename = void>
  3091. struct is_compatible_object_type_impl : std::false_type {};
  3092. template<typename BasicJsonType, typename CompatibleObjectType>
  3093. struct is_compatible_object_type_impl <
  3094. BasicJsonType, CompatibleObjectType,
  3095. enable_if_t < is_detected<mapped_type_t, CompatibleObjectType>::value&&
  3096. is_detected<key_type_t, CompatibleObjectType>::value >>
  3097. {
  3098. using object_t = typename BasicJsonType::object_t;
  3099. // macOS's is_constructible does not play well with nonesuch...
  3100. static constexpr bool value =
  3101. is_constructible<typename object_t::key_type,
  3102. typename CompatibleObjectType::key_type>::value &&
  3103. is_constructible<typename object_t::mapped_type,
  3104. typename CompatibleObjectType::mapped_type>::value;
  3105. };
  3106. template<typename BasicJsonType, typename CompatibleObjectType>
  3107. struct is_compatible_object_type
  3108. : is_compatible_object_type_impl<BasicJsonType, CompatibleObjectType> {};
  3109. template<typename BasicJsonType, typename ConstructibleObjectType,
  3110. typename = void>
  3111. struct is_constructible_object_type_impl : std::false_type {};
  3112. template<typename BasicJsonType, typename ConstructibleObjectType>
  3113. struct is_constructible_object_type_impl <
  3114. BasicJsonType, ConstructibleObjectType,
  3115. enable_if_t < is_detected<mapped_type_t, ConstructibleObjectType>::value&&
  3116. is_detected<key_type_t, ConstructibleObjectType>::value >>
  3117. {
  3118. using object_t = typename BasicJsonType::object_t;
  3119. static constexpr bool value =
  3120. (is_default_constructible<ConstructibleObjectType>::value &&
  3121. (std::is_move_assignable<ConstructibleObjectType>::value ||
  3122. std::is_copy_assignable<ConstructibleObjectType>::value) &&
  3123. (is_constructible<typename ConstructibleObjectType::key_type,
  3124. typename object_t::key_type>::value &&
  3125. std::is_same <
  3126. typename object_t::mapped_type,
  3127. typename ConstructibleObjectType::mapped_type >::value)) ||
  3128. (has_from_json<BasicJsonType,
  3129. typename ConstructibleObjectType::mapped_type>::value ||
  3130. has_non_default_from_json <
  3131. BasicJsonType,
  3132. typename ConstructibleObjectType::mapped_type >::value);
  3133. };
  3134. template<typename BasicJsonType, typename ConstructibleObjectType>
  3135. struct is_constructible_object_type
  3136. : is_constructible_object_type_impl<BasicJsonType,
  3137. ConstructibleObjectType> {};
  3138. template<typename BasicJsonType, typename CompatibleStringType>
  3139. struct is_compatible_string_type
  3140. {
  3141. static constexpr auto value =
  3142. is_constructible<typename BasicJsonType::string_t, CompatibleStringType>::value;
  3143. };
  3144. template<typename BasicJsonType, typename ConstructibleStringType>
  3145. struct is_constructible_string_type
  3146. {
  3147. // launder type through decltype() to fix compilation failure on ICPC
  3148. #ifdef __INTEL_COMPILER
  3149. using laundered_type = decltype(std::declval<ConstructibleStringType>());
  3150. #else
  3151. using laundered_type = ConstructibleStringType;
  3152. #endif
  3153. static constexpr auto value =
  3154. is_constructible<laundered_type,
  3155. typename BasicJsonType::string_t>::value;
  3156. };
  3157. template<typename BasicJsonType, typename CompatibleArrayType, typename = void>
  3158. struct is_compatible_array_type_impl : std::false_type {};
  3159. template<typename BasicJsonType, typename CompatibleArrayType>
  3160. struct is_compatible_array_type_impl <
  3161. BasicJsonType, CompatibleArrayType,
  3162. enable_if_t <
  3163. is_detected<iterator_t, CompatibleArrayType>::value&&
  3164. is_iterator_traits<iterator_traits<detected_t<iterator_t, CompatibleArrayType>>>::value&&
  3165. // special case for types like std::filesystem::path whose iterator's value_type are themselves
  3166. // c.f. https://github.com/nlohmann/json/pull/3073
  3167. !std::is_same<CompatibleArrayType, detected_t<range_value_t, CompatibleArrayType>>::value >>
  3168. {
  3169. static constexpr bool value =
  3170. is_constructible<BasicJsonType,
  3171. range_value_t<CompatibleArrayType>>::value;
  3172. };
  3173. template<typename BasicJsonType, typename CompatibleArrayType>
  3174. struct is_compatible_array_type
  3175. : is_compatible_array_type_impl<BasicJsonType, CompatibleArrayType> {};
  3176. template<typename BasicJsonType, typename ConstructibleArrayType, typename = void>
  3177. struct is_constructible_array_type_impl : std::false_type {};
  3178. template<typename BasicJsonType, typename ConstructibleArrayType>
  3179. struct is_constructible_array_type_impl <
  3180. BasicJsonType, ConstructibleArrayType,
  3181. enable_if_t<std::is_same<ConstructibleArrayType,
  3182. typename BasicJsonType::value_type>::value >>
  3183. : std::true_type {};
  3184. template<typename BasicJsonType, typename ConstructibleArrayType>
  3185. struct is_constructible_array_type_impl <
  3186. BasicJsonType, ConstructibleArrayType,
  3187. enable_if_t < !std::is_same<ConstructibleArrayType,
  3188. typename BasicJsonType::value_type>::value&&
  3189. !is_compatible_string_type<BasicJsonType, ConstructibleArrayType>::value&&
  3190. is_default_constructible<ConstructibleArrayType>::value&&
  3191. (std::is_move_assignable<ConstructibleArrayType>::value ||
  3192. std::is_copy_assignable<ConstructibleArrayType>::value)&&
  3193. is_detected<iterator_t, ConstructibleArrayType>::value&&
  3194. is_iterator_traits<iterator_traits<detected_t<iterator_t, ConstructibleArrayType>>>::value&&
  3195. is_detected<range_value_t, ConstructibleArrayType>::value&&
  3196. // special case for types like std::filesystem::path whose iterator's value_type are themselves
  3197. // c.f. https://github.com/nlohmann/json/pull/3073
  3198. !std::is_same<ConstructibleArrayType, detected_t<range_value_t, ConstructibleArrayType>>::value&&
  3199. is_complete_type <
  3200. detected_t<range_value_t, ConstructibleArrayType >>::value >>
  3201. {
  3202. using value_type = range_value_t<ConstructibleArrayType>;
  3203. static constexpr bool value =
  3204. std::is_same<value_type,
  3205. typename BasicJsonType::array_t::value_type>::value ||
  3206. has_from_json<BasicJsonType,
  3207. value_type>::value ||
  3208. has_non_default_from_json <
  3209. BasicJsonType,
  3210. value_type >::value;
  3211. };
  3212. template<typename BasicJsonType, typename ConstructibleArrayType>
  3213. struct is_constructible_array_type
  3214. : is_constructible_array_type_impl<BasicJsonType, ConstructibleArrayType> {};
  3215. template<typename RealIntegerType, typename CompatibleNumberIntegerType,
  3216. typename = void>
  3217. struct is_compatible_integer_type_impl : std::false_type {};
  3218. template<typename RealIntegerType, typename CompatibleNumberIntegerType>
  3219. struct is_compatible_integer_type_impl <
  3220. RealIntegerType, CompatibleNumberIntegerType,
  3221. enable_if_t < std::is_integral<RealIntegerType>::value&&
  3222. std::is_integral<CompatibleNumberIntegerType>::value&&
  3223. !std::is_same<bool, CompatibleNumberIntegerType>::value >>
  3224. {
  3225. // is there an assert somewhere on overflows?
  3226. using RealLimits = std::numeric_limits<RealIntegerType>;
  3227. using CompatibleLimits = std::numeric_limits<CompatibleNumberIntegerType>;
  3228. static constexpr auto value =
  3229. is_constructible<RealIntegerType,
  3230. CompatibleNumberIntegerType>::value &&
  3231. CompatibleLimits::is_integer &&
  3232. RealLimits::is_signed == CompatibleLimits::is_signed;
  3233. };
  3234. template<typename RealIntegerType, typename CompatibleNumberIntegerType>
  3235. struct is_compatible_integer_type
  3236. : is_compatible_integer_type_impl<RealIntegerType,
  3237. CompatibleNumberIntegerType> {};
  3238. template<typename BasicJsonType, typename CompatibleType, typename = void>
  3239. struct is_compatible_type_impl: std::false_type {};
  3240. template<typename BasicJsonType, typename CompatibleType>
  3241. struct is_compatible_type_impl <
  3242. BasicJsonType, CompatibleType,
  3243. enable_if_t<is_complete_type<CompatibleType>::value >>
  3244. {
  3245. static constexpr bool value =
  3246. has_to_json<BasicJsonType, CompatibleType>::value;
  3247. };
  3248. template<typename BasicJsonType, typename CompatibleType>
  3249. struct is_compatible_type
  3250. : is_compatible_type_impl<BasicJsonType, CompatibleType> {};
  3251. template<typename T1, typename T2>
  3252. struct is_constructible_tuple : std::false_type {};
  3253. template<typename T1, typename... Args>
  3254. struct is_constructible_tuple<T1, std::tuple<Args...>> : conjunction<is_constructible<T1, Args>...> {};
  3255. template<typename BasicJsonType, typename T>
  3256. struct is_json_iterator_of : std::false_type {};
  3257. template<typename BasicJsonType>
  3258. struct is_json_iterator_of<BasicJsonType, typename BasicJsonType::iterator> : std::true_type {};
  3259. template<typename BasicJsonType>
  3260. struct is_json_iterator_of<BasicJsonType, typename BasicJsonType::const_iterator> : std::true_type
  3261. {};
  3262. // checks if a given type T is a template specialization of Primary
  3263. template<template <typename...> class Primary, typename T>
  3264. struct is_specialization_of : std::false_type {};
  3265. template<template <typename...> class Primary, typename... Args>
  3266. struct is_specialization_of<Primary, Primary<Args...>> : std::true_type {};
  3267. template<typename T>
  3268. using is_json_pointer = is_specialization_of<::nlohmann::json_pointer, uncvref_t<T>>;
  3269. // checks if A and B are comparable using Compare functor
  3270. template<typename Compare, typename A, typename B, typename = void>
  3271. struct is_comparable : std::false_type {};
  3272. template<typename Compare, typename A, typename B>
  3273. struct is_comparable<Compare, A, B, void_t<
  3274. decltype(std::declval<Compare>()(std::declval<A>(), std::declval<B>())),
  3275. decltype(std::declval<Compare>()(std::declval<B>(), std::declval<A>()))
  3276. >> : std::true_type {};
  3277. // checks if BasicJsonType::object_t::key_type and KeyType are comparable using Compare functor
  3278. template<typename BasicJsonType, typename KeyType>
  3279. using is_key_type_comparable = typename is_comparable <
  3280. typename BasicJsonType::object_comparator_t,
  3281. const key_type_t<typename BasicJsonType::object_t>&,
  3282. KeyType >::type;
  3283. template<typename T>
  3284. using detect_is_transparent = typename T::is_transparent;
  3285. // type trait to check if KeyType can be used as object key
  3286. // true if:
  3287. // - KeyType is comparable with BasicJsonType::object_t::key_type
  3288. // - if ExcludeObjectKeyType is true, KeyType is not BasicJsonType::object_t::key_type
  3289. // - the comparator is transparent or RequireTransparentComparator is false
  3290. // - KeyType is not a JSON iterator or json_pointer
  3291. template<typename BasicJsonType, typename KeyTypeCVRef, bool RequireTransparentComparator = true,
  3292. bool ExcludeObjectKeyType = RequireTransparentComparator, typename KeyType = uncvref_t<KeyTypeCVRef>>
  3293. using is_usable_as_key_type = typename std::conditional <
  3294. is_key_type_comparable<BasicJsonType, KeyTypeCVRef>::value
  3295. && !(ExcludeObjectKeyType && std::is_same<KeyType,
  3296. typename BasicJsonType::object_t::key_type>::value)
  3297. && (!RequireTransparentComparator || is_detected <
  3298. detect_is_transparent,
  3299. typename BasicJsonType::object_comparator_t >::value)
  3300. && !is_json_iterator_of<BasicJsonType, KeyType>::value
  3301. && !is_json_pointer<KeyType>::value,
  3302. std::true_type,
  3303. std::false_type >::type;
  3304. template<typename ObjectType, typename KeyType>
  3305. using detect_erase_with_key_type = decltype(std::declval<ObjectType&>().erase(std::declval<KeyType>()));
  3306. // type trait to check if object_t has an erase() member functions accepting KeyType
  3307. template<typename BasicJsonType, typename KeyType>
  3308. using has_erase_with_key_type = typename std::conditional <
  3309. is_detected <
  3310. detect_erase_with_key_type,
  3311. typename BasicJsonType::object_t, KeyType >::value,
  3312. std::true_type,
  3313. std::false_type >::type;
  3314. // a naive helper to check if a type is an ordered_map (exploits the fact that
  3315. // ordered_map inherits capacity() from std::vector)
  3316. template <typename T>
  3317. struct is_ordered_map
  3318. {
  3319. using one = char;
  3320. struct two
  3321. {
  3322. char x[2]; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  3323. };
  3324. template <typename C> static one test( decltype(&C::capacity) ) ;
  3325. template <typename C> static two test(...);
  3326. enum { value = sizeof(test<T>(nullptr)) == sizeof(char) }; // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  3327. };
  3328. // to avoid useless casts (see https://github.com/nlohmann/json/issues/2893#issuecomment-889152324)
  3329. template < typename T, typename U, enable_if_t < !std::is_same<T, U>::value, int > = 0 >
  3330. T conditional_static_cast(U value)
  3331. {
  3332. return static_cast<T>(value);
  3333. }
  3334. template<typename T, typename U, enable_if_t<std::is_same<T, U>::value, int> = 0>
  3335. T conditional_static_cast(U value)
  3336. {
  3337. return value;
  3338. }
  3339. template<typename... Types>
  3340. using all_integral = conjunction<std::is_integral<Types>...>;
  3341. template<typename... Types>
  3342. using all_signed = conjunction<std::is_signed<Types>...>;
  3343. template<typename... Types>
  3344. using all_unsigned = conjunction<std::is_unsigned<Types>...>;
  3345. // there's a disjunction trait in another PR; replace when merged
  3346. template<typename... Types>
  3347. using same_sign = std::integral_constant < bool,
  3348. all_signed<Types...>::value || all_unsigned<Types...>::value >;
  3349. template<typename OfType, typename T>
  3350. using never_out_of_range = std::integral_constant < bool,
  3351. (std::is_signed<OfType>::value && (sizeof(T) < sizeof(OfType)))
  3352. || (same_sign<OfType, T>::value && sizeof(OfType) == sizeof(T)) >;
  3353. template<typename OfType, typename T,
  3354. bool OfTypeSigned = std::is_signed<OfType>::value,
  3355. bool TSigned = std::is_signed<T>::value>
  3356. struct value_in_range_of_impl2;
  3357. template<typename OfType, typename T>
  3358. struct value_in_range_of_impl2<OfType, T, false, false>
  3359. {
  3360. static constexpr bool test(T val)
  3361. {
  3362. using CommonType = typename std::common_type<OfType, T>::type;
  3363. return static_cast<CommonType>(val) <= static_cast<CommonType>(std::numeric_limits<OfType>::max());
  3364. }
  3365. };
  3366. template<typename OfType, typename T>
  3367. struct value_in_range_of_impl2<OfType, T, true, false>
  3368. {
  3369. static constexpr bool test(T val)
  3370. {
  3371. using CommonType = typename std::common_type<OfType, T>::type;
  3372. return static_cast<CommonType>(val) <= static_cast<CommonType>(std::numeric_limits<OfType>::max());
  3373. }
  3374. };
  3375. template<typename OfType, typename T>
  3376. struct value_in_range_of_impl2<OfType, T, false, true>
  3377. {
  3378. static constexpr bool test(T val)
  3379. {
  3380. using CommonType = typename std::common_type<OfType, T>::type;
  3381. return val >= 0 && static_cast<CommonType>(val) <= static_cast<CommonType>(std::numeric_limits<OfType>::max());
  3382. }
  3383. };
  3384. template<typename OfType, typename T>
  3385. struct value_in_range_of_impl2<OfType, T, true, true>
  3386. {
  3387. static constexpr bool test(T val)
  3388. {
  3389. using CommonType = typename std::common_type<OfType, T>::type;
  3390. return static_cast<CommonType>(val) >= static_cast<CommonType>(std::numeric_limits<OfType>::min())
  3391. && static_cast<CommonType>(val) <= static_cast<CommonType>(std::numeric_limits<OfType>::max());
  3392. }
  3393. };
  3394. template<typename OfType, typename T,
  3395. bool NeverOutOfRange = never_out_of_range<OfType, T>::value,
  3396. typename = detail::enable_if_t<all_integral<OfType, T>::value>>
  3397. struct value_in_range_of_impl1;
  3398. template<typename OfType, typename T>
  3399. struct value_in_range_of_impl1<OfType, T, false>
  3400. {
  3401. static constexpr bool test(T val)
  3402. {
  3403. return value_in_range_of_impl2<OfType, T>::test(val);
  3404. }
  3405. };
  3406. template<typename OfType, typename T>
  3407. struct value_in_range_of_impl1<OfType, T, true>
  3408. {
  3409. static constexpr bool test(T /*val*/)
  3410. {
  3411. return true;
  3412. }
  3413. };
  3414. template<typename OfType, typename T>
  3415. inline constexpr bool value_in_range_of(T val)
  3416. {
  3417. return value_in_range_of_impl1<OfType, T>::test(val);
  3418. }
  3419. } // namespace detail
  3420. } // namespace nlohmann
  3421. // #include <nlohmann/detail/string_concat.hpp>
  3422. #include <cstring> // strlen
  3423. #include <string> // string
  3424. #include <utility> // forward
  3425. // #include <nlohmann/detail/meta/cpp_future.hpp>
  3426. // #include <nlohmann/detail/meta/detected.hpp>
  3427. namespace nlohmann
  3428. {
  3429. namespace detail
  3430. {
  3431. inline std::size_t concat_length()
  3432. {
  3433. return 0;
  3434. }
  3435. template<typename... Args>
  3436. inline std::size_t concat_length(const char* cstr, Args&& ... rest);
  3437. template<typename StringType, typename... Args>
  3438. inline std::size_t concat_length(const StringType& str, Args&& ... rest);
  3439. template<typename... Args>
  3440. inline std::size_t concat_length(const char /*c*/, Args&& ... rest)
  3441. {
  3442. return 1 + concat_length(std::forward<Args>(rest)...);
  3443. }
  3444. template<typename... Args>
  3445. inline std::size_t concat_length(const char* cstr, Args&& ... rest)
  3446. {
  3447. // cppcheck-suppress ignoredReturnValue
  3448. return ::strlen(cstr) + concat_length(std::forward<Args>(rest)...);
  3449. }
  3450. template<typename StringType, typename... Args>
  3451. inline std::size_t concat_length(const StringType& str, Args&& ... rest)
  3452. {
  3453. return str.size() + concat_length(std::forward<Args>(rest)...);
  3454. }
  3455. template<typename OutStringType>
  3456. inline void concat_into(OutStringType& /*out*/)
  3457. {}
  3458. template<typename StringType, typename Arg>
  3459. using string_can_append = decltype(std::declval<StringType&>().append(std::declval < Arg && > ()));
  3460. template<typename StringType, typename Arg>
  3461. using detect_string_can_append = is_detected<string_can_append, StringType, Arg>;
  3462. template<typename StringType, typename Arg>
  3463. using string_can_append_op = decltype(std::declval<StringType&>() += std::declval < Arg && > ());
  3464. template<typename StringType, typename Arg>
  3465. using detect_string_can_append_op = is_detected<string_can_append_op, StringType, Arg>;
  3466. template<typename StringType, typename Arg>
  3467. using string_can_append_iter = decltype(std::declval<StringType&>().append(std::declval<const Arg&>().begin(), std::declval<const Arg&>().end()));
  3468. template<typename StringType, typename Arg>
  3469. using detect_string_can_append_iter = is_detected<string_can_append_iter, StringType, Arg>;
  3470. template<typename StringType, typename Arg>
  3471. using string_can_append_data = decltype(std::declval<StringType&>().append(std::declval<const Arg&>().data(), std::declval<const Arg&>().size()));
  3472. template<typename StringType, typename Arg>
  3473. using detect_string_can_append_data = is_detected<string_can_append_data, StringType, Arg>;
  3474. template < typename OutStringType, typename Arg, typename... Args,
  3475. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3476. && detect_string_can_append_op<OutStringType, Arg>::value, int > = 0 >
  3477. inline void concat_into(OutStringType& out, Arg && arg, Args && ... rest);
  3478. template < typename OutStringType, typename Arg, typename... Args,
  3479. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3480. && !detect_string_can_append_op<OutStringType, Arg>::value
  3481. && detect_string_can_append_iter<OutStringType, Arg>::value, int > = 0 >
  3482. inline void concat_into(OutStringType& out, const Arg& arg, Args && ... rest);
  3483. template < typename OutStringType, typename Arg, typename... Args,
  3484. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3485. && !detect_string_can_append_op<OutStringType, Arg>::value
  3486. && !detect_string_can_append_iter<OutStringType, Arg>::value
  3487. && detect_string_can_append_data<OutStringType, Arg>::value, int > = 0 >
  3488. inline void concat_into(OutStringType& out, const Arg& arg, Args && ... rest);
  3489. template<typename OutStringType, typename Arg, typename... Args,
  3490. enable_if_t<detect_string_can_append<OutStringType, Arg>::value, int> = 0>
  3491. inline void concat_into(OutStringType& out, Arg && arg, Args && ... rest)
  3492. {
  3493. out.append(std::forward<Arg>(arg));
  3494. concat_into(out, std::forward<Args>(rest)...);
  3495. }
  3496. template < typename OutStringType, typename Arg, typename... Args,
  3497. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3498. && detect_string_can_append_op<OutStringType, Arg>::value, int > >
  3499. inline void concat_into(OutStringType& out, Arg&& arg, Args&& ... rest)
  3500. {
  3501. out += std::forward<Arg>(arg);
  3502. concat_into(out, std::forward<Args>(rest)...);
  3503. }
  3504. template < typename OutStringType, typename Arg, typename... Args,
  3505. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3506. && !detect_string_can_append_op<OutStringType, Arg>::value
  3507. && detect_string_can_append_iter<OutStringType, Arg>::value, int > >
  3508. inline void concat_into(OutStringType& out, const Arg& arg, Args&& ... rest)
  3509. {
  3510. out.append(arg.begin(), arg.end());
  3511. concat_into(out, std::forward<Args>(rest)...);
  3512. }
  3513. template < typename OutStringType, typename Arg, typename... Args,
  3514. enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
  3515. && !detect_string_can_append_op<OutStringType, Arg>::value
  3516. && !detect_string_can_append_iter<OutStringType, Arg>::value
  3517. && detect_string_can_append_data<OutStringType, Arg>::value, int > >
  3518. inline void concat_into(OutStringType& out, const Arg& arg, Args&& ... rest)
  3519. {
  3520. out.append(arg.data(), arg.size());
  3521. concat_into(out, std::forward<Args>(rest)...);
  3522. }
  3523. template<typename OutStringType = std::string, typename... Args>
  3524. inline OutStringType concat(Args && ... args)
  3525. {
  3526. OutStringType str;
  3527. str.reserve(concat_length(std::forward<Args>(args)...));
  3528. concat_into(str, std::forward<Args>(args)...);
  3529. return str;
  3530. }
  3531. } // namespace detail
  3532. } // namespace nlohmann
  3533. namespace nlohmann
  3534. {
  3535. namespace detail
  3536. {
  3537. ////////////////
  3538. // exceptions //
  3539. ////////////////
  3540. /// @brief general exception of the @ref basic_json class
  3541. /// @sa https://json.nlohmann.me/api/basic_json/exception/
  3542. class exception : public std::exception
  3543. {
  3544. public:
  3545. /// returns the explanatory string
  3546. const char* what() const noexcept override
  3547. {
  3548. return m.what();
  3549. }
  3550. /// the id of the exception
  3551. const int id; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes)
  3552. protected:
  3553. JSON_HEDLEY_NON_NULL(3)
  3554. exception(int id_, const char* what_arg) : id(id_), m(what_arg) {} // NOLINT(bugprone-throw-keyword-missing)
  3555. static std::string name(const std::string& ename, int id_)
  3556. {
  3557. return concat("[json.exception.", ename, '.', std::to_string(id_), "] ");
  3558. }
  3559. static std::string diagnostics(std::nullptr_t /*leaf_element*/)
  3560. {
  3561. return "";
  3562. }
  3563. template<typename BasicJsonType>
  3564. static std::string diagnostics(const BasicJsonType* leaf_element)
  3565. {
  3566. #if JSON_DIAGNOSTICS
  3567. std::vector<std::string> tokens;
  3568. for (const auto* current = leaf_element; current != nullptr && current->m_parent != nullptr; current = current->m_parent)
  3569. {
  3570. switch (current->m_parent->type())
  3571. {
  3572. case value_t::array:
  3573. {
  3574. for (std::size_t i = 0; i < current->m_parent->m_value.array->size(); ++i)
  3575. {
  3576. if (&current->m_parent->m_value.array->operator[](i) == current)
  3577. {
  3578. tokens.emplace_back(std::to_string(i));
  3579. break;
  3580. }
  3581. }
  3582. break;
  3583. }
  3584. case value_t::object:
  3585. {
  3586. for (const auto& element : *current->m_parent->m_value.object)
  3587. {
  3588. if (&element.second == current)
  3589. {
  3590. tokens.emplace_back(element.first.c_str());
  3591. break;
  3592. }
  3593. }
  3594. break;
  3595. }
  3596. case value_t::null: // LCOV_EXCL_LINE
  3597. case value_t::string: // LCOV_EXCL_LINE
  3598. case value_t::boolean: // LCOV_EXCL_LINE
  3599. case value_t::number_integer: // LCOV_EXCL_LINE
  3600. case value_t::number_unsigned: // LCOV_EXCL_LINE
  3601. case value_t::number_float: // LCOV_EXCL_LINE
  3602. case value_t::binary: // LCOV_EXCL_LINE
  3603. case value_t::discarded: // LCOV_EXCL_LINE
  3604. default: // LCOV_EXCL_LINE
  3605. break; // LCOV_EXCL_LINE
  3606. }
  3607. }
  3608. if (tokens.empty())
  3609. {
  3610. return "";
  3611. }
  3612. auto str = std::accumulate(tokens.rbegin(), tokens.rend(), std::string{},
  3613. [](const std::string & a, const std::string & b)
  3614. {
  3615. return concat(a, '/', detail::escape(b));
  3616. });
  3617. return concat('(', str, ") ");
  3618. #else
  3619. static_cast<void>(leaf_element);
  3620. return "";
  3621. #endif
  3622. }
  3623. private:
  3624. /// an exception object as storage for error messages
  3625. std::runtime_error m;
  3626. };
  3627. /// @brief exception indicating a parse error
  3628. /// @sa https://json.nlohmann.me/api/basic_json/parse_error/
  3629. class parse_error : public exception
  3630. {
  3631. public:
  3632. /*!
  3633. @brief create a parse error exception
  3634. @param[in] id_ the id of the exception
  3635. @param[in] pos the position where the error occurred (or with
  3636. chars_read_total=0 if the position cannot be
  3637. determined)
  3638. @param[in] what_arg the explanatory string
  3639. @return parse_error object
  3640. */
  3641. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3642. static parse_error create(int id_, const position_t& pos, const std::string& what_arg, BasicJsonContext context)
  3643. {
  3644. std::string w = concat(exception::name("parse_error", id_), "parse error",
  3645. position_string(pos), ": ", exception::diagnostics(context), what_arg);
  3646. return {id_, pos.chars_read_total, w.c_str()};
  3647. }
  3648. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3649. static parse_error create(int id_, std::size_t byte_, const std::string& what_arg, BasicJsonContext context)
  3650. {
  3651. std::string w = concat(exception::name("parse_error", id_), "parse error",
  3652. (byte_ != 0 ? (concat(" at byte ", std::to_string(byte_))) : ""),
  3653. ": ", exception::diagnostics(context), what_arg);
  3654. return {id_, byte_, w.c_str()};
  3655. }
  3656. /*!
  3657. @brief byte index of the parse error
  3658. The byte index of the last read character in the input file.
  3659. @note For an input with n bytes, 1 is the index of the first character and
  3660. n+1 is the index of the terminating null byte or the end of file.
  3661. This also holds true when reading a byte vector (CBOR or MessagePack).
  3662. */
  3663. const std::size_t byte;
  3664. private:
  3665. parse_error(int id_, std::size_t byte_, const char* what_arg)
  3666. : exception(id_, what_arg), byte(byte_) {}
  3667. static std::string position_string(const position_t& pos)
  3668. {
  3669. return concat(" at line ", std::to_string(pos.lines_read + 1),
  3670. ", column ", std::to_string(pos.chars_read_current_line));
  3671. }
  3672. };
  3673. /// @brief exception indicating errors with iterators
  3674. /// @sa https://json.nlohmann.me/api/basic_json/invalid_iterator/
  3675. class invalid_iterator : public exception
  3676. {
  3677. public:
  3678. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3679. static invalid_iterator create(int id_, const std::string& what_arg, BasicJsonContext context)
  3680. {
  3681. std::string w = concat(exception::name("invalid_iterator", id_), exception::diagnostics(context), what_arg);
  3682. return {id_, w.c_str()};
  3683. }
  3684. private:
  3685. JSON_HEDLEY_NON_NULL(3)
  3686. invalid_iterator(int id_, const char* what_arg)
  3687. : exception(id_, what_arg) {}
  3688. };
  3689. /// @brief exception indicating executing a member function with a wrong type
  3690. /// @sa https://json.nlohmann.me/api/basic_json/type_error/
  3691. class type_error : public exception
  3692. {
  3693. public:
  3694. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3695. static type_error create(int id_, const std::string& what_arg, BasicJsonContext context)
  3696. {
  3697. std::string w = concat(exception::name("type_error", id_), exception::diagnostics(context), what_arg);
  3698. return {id_, w.c_str()};
  3699. }
  3700. private:
  3701. JSON_HEDLEY_NON_NULL(3)
  3702. type_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
  3703. };
  3704. /// @brief exception indicating access out of the defined range
  3705. /// @sa https://json.nlohmann.me/api/basic_json/out_of_range/
  3706. class out_of_range : public exception
  3707. {
  3708. public:
  3709. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3710. static out_of_range create(int id_, const std::string& what_arg, BasicJsonContext context)
  3711. {
  3712. std::string w = concat(exception::name("out_of_range", id_), exception::diagnostics(context), what_arg);
  3713. return {id_, w.c_str()};
  3714. }
  3715. private:
  3716. JSON_HEDLEY_NON_NULL(3)
  3717. out_of_range(int id_, const char* what_arg) : exception(id_, what_arg) {}
  3718. };
  3719. /// @brief exception indicating other library errors
  3720. /// @sa https://json.nlohmann.me/api/basic_json/other_error/
  3721. class other_error : public exception
  3722. {
  3723. public:
  3724. template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
  3725. static other_error create(int id_, const std::string& what_arg, BasicJsonContext context)
  3726. {
  3727. std::string w = concat(exception::name("other_error", id_), exception::diagnostics(context), what_arg);
  3728. return {id_, w.c_str()};
  3729. }
  3730. private:
  3731. JSON_HEDLEY_NON_NULL(3)
  3732. other_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
  3733. };
  3734. } // namespace detail
  3735. } // namespace nlohmann
  3736. // #include <nlohmann/detail/macro_scope.hpp>
  3737. // #include <nlohmann/detail/meta/cpp_future.hpp>
  3738. // #include <nlohmann/detail/meta/identity_tag.hpp>
  3739. namespace nlohmann
  3740. {
  3741. namespace detail
  3742. {
  3743. // dispatching helper struct
  3744. template <class T> struct identity_tag {};
  3745. } // namespace detail
  3746. } // namespace nlohmann
  3747. // #include <nlohmann/detail/meta/type_traits.hpp>
  3748. // #include <nlohmann/detail/string_concat.hpp>
  3749. // #include <nlohmann/detail/value_t.hpp>
  3750. #if JSON_HAS_EXPERIMENTAL_FILESYSTEM
  3751. #include <experimental/filesystem>
  3752. namespace nlohmann::detail
  3753. {
  3754. namespace std_fs = std::experimental::filesystem;
  3755. } // namespace nlohmann::detail
  3756. #elif JSON_HAS_FILESYSTEM
  3757. #include <filesystem>
  3758. namespace nlohmann::detail
  3759. {
  3760. namespace std_fs = std::filesystem;
  3761. } // namespace nlohmann::detail
  3762. #endif
  3763. namespace nlohmann
  3764. {
  3765. namespace detail
  3766. {
  3767. template<typename BasicJsonType>
  3768. inline void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
  3769. {
  3770. if (JSON_HEDLEY_UNLIKELY(!j.is_null()))
  3771. {
  3772. JSON_THROW(type_error::create(302, concat("type must be null, but is ", j.type_name()), &j));
  3773. }
  3774. n = nullptr;
  3775. }
  3776. // overloads for basic_json template parameters
  3777. template < typename BasicJsonType, typename ArithmeticType,
  3778. enable_if_t < std::is_arithmetic<ArithmeticType>::value&&
  3779. !std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
  3780. int > = 0 >
  3781. void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val)
  3782. {
  3783. switch (static_cast<value_t>(j))
  3784. {
  3785. case value_t::number_unsigned:
  3786. {
  3787. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
  3788. break;
  3789. }
  3790. case value_t::number_integer:
  3791. {
  3792. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
  3793. break;
  3794. }
  3795. case value_t::number_float:
  3796. {
  3797. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
  3798. break;
  3799. }
  3800. case value_t::null:
  3801. case value_t::object:
  3802. case value_t::array:
  3803. case value_t::string:
  3804. case value_t::boolean:
  3805. case value_t::binary:
  3806. case value_t::discarded:
  3807. default:
  3808. JSON_THROW(type_error::create(302, concat("type must be number, but is ", j.type_name()), &j));
  3809. }
  3810. }
  3811. template<typename BasicJsonType>
  3812. inline void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
  3813. {
  3814. if (JSON_HEDLEY_UNLIKELY(!j.is_boolean()))
  3815. {
  3816. JSON_THROW(type_error::create(302, concat("type must be boolean, but is ", j.type_name()), &j));
  3817. }
  3818. b = *j.template get_ptr<const typename BasicJsonType::boolean_t*>();
  3819. }
  3820. template<typename BasicJsonType>
  3821. inline void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s)
  3822. {
  3823. if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
  3824. {
  3825. JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
  3826. }
  3827. s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
  3828. }
  3829. template <
  3830. typename BasicJsonType, typename StringType,
  3831. enable_if_t <
  3832. std::is_assignable<StringType&, const typename BasicJsonType::string_t>::value
  3833. && !std::is_same<typename BasicJsonType::string_t, StringType>::value
  3834. && !is_json_ref<StringType>::value, int > = 0 >
  3835. inline void from_json(const BasicJsonType& j, StringType& s)
  3836. {
  3837. if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
  3838. {
  3839. JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
  3840. }
  3841. s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
  3842. }
  3843. template<typename BasicJsonType>
  3844. inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val)
  3845. {
  3846. get_arithmetic_value(j, val);
  3847. }
  3848. template<typename BasicJsonType>
  3849. inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_unsigned_t& val)
  3850. {
  3851. get_arithmetic_value(j, val);
  3852. }
  3853. template<typename BasicJsonType>
  3854. inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& val)
  3855. {
  3856. get_arithmetic_value(j, val);
  3857. }
  3858. #if !JSON_DISABLE_ENUM_SERIALIZATION
  3859. template<typename BasicJsonType, typename EnumType,
  3860. enable_if_t<std::is_enum<EnumType>::value, int> = 0>
  3861. inline void from_json(const BasicJsonType& j, EnumType& e)
  3862. {
  3863. typename std::underlying_type<EnumType>::type val;
  3864. get_arithmetic_value(j, val);
  3865. e = static_cast<EnumType>(val);
  3866. }
  3867. #endif // JSON_DISABLE_ENUM_SERIALIZATION
  3868. // forward_list doesn't have an insert method
  3869. template<typename BasicJsonType, typename T, typename Allocator,
  3870. enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
  3871. inline void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
  3872. {
  3873. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  3874. {
  3875. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  3876. }
  3877. l.clear();
  3878. std::transform(j.rbegin(), j.rend(),
  3879. std::front_inserter(l), [](const BasicJsonType & i)
  3880. {
  3881. return i.template get<T>();
  3882. });
  3883. }
  3884. // valarray doesn't have an insert method
  3885. template<typename BasicJsonType, typename T,
  3886. enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
  3887. inline void from_json(const BasicJsonType& j, std::valarray<T>& l)
  3888. {
  3889. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  3890. {
  3891. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  3892. }
  3893. l.resize(j.size());
  3894. std::transform(j.begin(), j.end(), std::begin(l),
  3895. [](const BasicJsonType & elem)
  3896. {
  3897. return elem.template get<T>();
  3898. });
  3899. }
  3900. template<typename BasicJsonType, typename T, std::size_t N>
  3901. auto from_json(const BasicJsonType& j, T (&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  3902. -> decltype(j.template get<T>(), void())
  3903. {
  3904. for (std::size_t i = 0; i < N; ++i)
  3905. {
  3906. arr[i] = j.at(i).template get<T>();
  3907. }
  3908. }
  3909. template<typename BasicJsonType>
  3910. inline void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType::array_t& arr, priority_tag<3> /*unused*/)
  3911. {
  3912. arr = *j.template get_ptr<const typename BasicJsonType::array_t*>();
  3913. }
  3914. template<typename BasicJsonType, typename T, std::size_t N>
  3915. auto from_json_array_impl(const BasicJsonType& j, std::array<T, N>& arr,
  3916. priority_tag<2> /*unused*/)
  3917. -> decltype(j.template get<T>(), void())
  3918. {
  3919. for (std::size_t i = 0; i < N; ++i)
  3920. {
  3921. arr[i] = j.at(i).template get<T>();
  3922. }
  3923. }
  3924. template<typename BasicJsonType, typename ConstructibleArrayType,
  3925. enable_if_t<
  3926. std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
  3927. int> = 0>
  3928. auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, priority_tag<1> /*unused*/)
  3929. -> decltype(
  3930. arr.reserve(std::declval<typename ConstructibleArrayType::size_type>()),
  3931. j.template get<typename ConstructibleArrayType::value_type>(),
  3932. void())
  3933. {
  3934. using std::end;
  3935. ConstructibleArrayType ret;
  3936. ret.reserve(j.size());
  3937. std::transform(j.begin(), j.end(),
  3938. std::inserter(ret, end(ret)), [](const BasicJsonType & i)
  3939. {
  3940. // get<BasicJsonType>() returns *this, this won't call a from_json
  3941. // method when value_type is BasicJsonType
  3942. return i.template get<typename ConstructibleArrayType::value_type>();
  3943. });
  3944. arr = std::move(ret);
  3945. }
  3946. template<typename BasicJsonType, typename ConstructibleArrayType,
  3947. enable_if_t<
  3948. std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
  3949. int> = 0>
  3950. inline void from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr,
  3951. priority_tag<0> /*unused*/)
  3952. {
  3953. using std::end;
  3954. ConstructibleArrayType ret;
  3955. std::transform(
  3956. j.begin(), j.end(), std::inserter(ret, end(ret)),
  3957. [](const BasicJsonType & i)
  3958. {
  3959. // get<BasicJsonType>() returns *this, this won't call a from_json
  3960. // method when value_type is BasicJsonType
  3961. return i.template get<typename ConstructibleArrayType::value_type>();
  3962. });
  3963. arr = std::move(ret);
  3964. }
  3965. template < typename BasicJsonType, typename ConstructibleArrayType,
  3966. enable_if_t <
  3967. is_constructible_array_type<BasicJsonType, ConstructibleArrayType>::value&&
  3968. !is_constructible_object_type<BasicJsonType, ConstructibleArrayType>::value&&
  3969. !is_constructible_string_type<BasicJsonType, ConstructibleArrayType>::value&&
  3970. !std::is_same<ConstructibleArrayType, typename BasicJsonType::binary_t>::value&&
  3971. !is_basic_json<ConstructibleArrayType>::value,
  3972. int > = 0 >
  3973. auto from_json(const BasicJsonType& j, ConstructibleArrayType& arr)
  3974. -> decltype(from_json_array_impl(j, arr, priority_tag<3> {}),
  3975. j.template get<typename ConstructibleArrayType::value_type>(),
  3976. void())
  3977. {
  3978. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  3979. {
  3980. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  3981. }
  3982. from_json_array_impl(j, arr, priority_tag<3> {});
  3983. }
  3984. template < typename BasicJsonType, typename T, std::size_t... Idx >
  3985. std::array<T, sizeof...(Idx)> from_json_inplace_array_impl(BasicJsonType&& j,
  3986. identity_tag<std::array<T, sizeof...(Idx)>> /*unused*/, index_sequence<Idx...> /*unused*/)
  3987. {
  3988. return { { std::forward<BasicJsonType>(j).at(Idx).template get<T>()... } };
  3989. }
  3990. template < typename BasicJsonType, typename T, std::size_t N >
  3991. auto from_json(BasicJsonType&& j, identity_tag<std::array<T, N>> tag)
  3992. -> decltype(from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {}))
  3993. {
  3994. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  3995. {
  3996. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  3997. }
  3998. return from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {});
  3999. }
  4000. template<typename BasicJsonType>
  4001. inline void from_json(const BasicJsonType& j, typename BasicJsonType::binary_t& bin)
  4002. {
  4003. if (JSON_HEDLEY_UNLIKELY(!j.is_binary()))
  4004. {
  4005. JSON_THROW(type_error::create(302, concat("type must be binary, but is ", j.type_name()), &j));
  4006. }
  4007. bin = *j.template get_ptr<const typename BasicJsonType::binary_t*>();
  4008. }
  4009. template<typename BasicJsonType, typename ConstructibleObjectType,
  4010. enable_if_t<is_constructible_object_type<BasicJsonType, ConstructibleObjectType>::value, int> = 0>
  4011. inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
  4012. {
  4013. if (JSON_HEDLEY_UNLIKELY(!j.is_object()))
  4014. {
  4015. JSON_THROW(type_error::create(302, concat("type must be object, but is ", j.type_name()), &j));
  4016. }
  4017. ConstructibleObjectType ret;
  4018. const auto* inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
  4019. using value_type = typename ConstructibleObjectType::value_type;
  4020. std::transform(
  4021. inner_object->begin(), inner_object->end(),
  4022. std::inserter(ret, ret.begin()),
  4023. [](typename BasicJsonType::object_t::value_type const & p)
  4024. {
  4025. return value_type(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
  4026. });
  4027. obj = std::move(ret);
  4028. }
  4029. // overload for arithmetic types, not chosen for basic_json template arguments
  4030. // (BooleanType, etc..); note: Is it really necessary to provide explicit
  4031. // overloads for boolean_t etc. in case of a custom BooleanType which is not
  4032. // an arithmetic type?
  4033. template < typename BasicJsonType, typename ArithmeticType,
  4034. enable_if_t <
  4035. std::is_arithmetic<ArithmeticType>::value&&
  4036. !std::is_same<ArithmeticType, typename BasicJsonType::number_unsigned_t>::value&&
  4037. !std::is_same<ArithmeticType, typename BasicJsonType::number_integer_t>::value&&
  4038. !std::is_same<ArithmeticType, typename BasicJsonType::number_float_t>::value&&
  4039. !std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
  4040. int > = 0 >
  4041. inline void from_json(const BasicJsonType& j, ArithmeticType& val)
  4042. {
  4043. switch (static_cast<value_t>(j))
  4044. {
  4045. case value_t::number_unsigned:
  4046. {
  4047. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
  4048. break;
  4049. }
  4050. case value_t::number_integer:
  4051. {
  4052. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
  4053. break;
  4054. }
  4055. case value_t::number_float:
  4056. {
  4057. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
  4058. break;
  4059. }
  4060. case value_t::boolean:
  4061. {
  4062. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::boolean_t*>());
  4063. break;
  4064. }
  4065. case value_t::null:
  4066. case value_t::object:
  4067. case value_t::array:
  4068. case value_t::string:
  4069. case value_t::binary:
  4070. case value_t::discarded:
  4071. default:
  4072. JSON_THROW(type_error::create(302, concat("type must be number, but is ", j.type_name()), &j));
  4073. }
  4074. }
  4075. template<typename BasicJsonType, typename... Args, std::size_t... Idx>
  4076. std::tuple<Args...> from_json_tuple_impl_base(BasicJsonType&& j, index_sequence<Idx...> /*unused*/)
  4077. {
  4078. return std::make_tuple(std::forward<BasicJsonType>(j).at(Idx).template get<Args>()...);
  4079. }
  4080. template < typename BasicJsonType, class A1, class A2 >
  4081. std::pair<A1, A2> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::pair<A1, A2>> /*unused*/, priority_tag<0> /*unused*/)
  4082. {
  4083. return {std::forward<BasicJsonType>(j).at(0).template get<A1>(),
  4084. std::forward<BasicJsonType>(j).at(1).template get<A2>()};
  4085. }
  4086. template<typename BasicJsonType, typename A1, typename A2>
  4087. inline void from_json_tuple_impl(BasicJsonType&& j, std::pair<A1, A2>& p, priority_tag<1> /*unused*/)
  4088. {
  4089. p = from_json_tuple_impl(std::forward<BasicJsonType>(j), identity_tag<std::pair<A1, A2>> {}, priority_tag<0> {});
  4090. }
  4091. template<typename BasicJsonType, typename... Args>
  4092. std::tuple<Args...> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::tuple<Args...>> /*unused*/, priority_tag<2> /*unused*/)
  4093. {
  4094. return from_json_tuple_impl_base<BasicJsonType, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
  4095. }
  4096. template<typename BasicJsonType, typename... Args>
  4097. inline void from_json_tuple_impl(BasicJsonType&& j, std::tuple<Args...>& t, priority_tag<3> /*unused*/)
  4098. {
  4099. t = from_json_tuple_impl_base<BasicJsonType, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
  4100. }
  4101. template<typename BasicJsonType, typename TupleRelated>
  4102. auto from_json(BasicJsonType&& j, TupleRelated&& t)
  4103. -> decltype(from_json_tuple_impl(std::forward<BasicJsonType>(j), std::forward<TupleRelated>(t), priority_tag<3> {}))
  4104. {
  4105. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4106. {
  4107. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4108. }
  4109. return from_json_tuple_impl(std::forward<BasicJsonType>(j), std::forward<TupleRelated>(t), priority_tag<3> {});
  4110. }
  4111. template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator,
  4112. typename = enable_if_t < !std::is_constructible <
  4113. typename BasicJsonType::string_t, Key >::value >>
  4114. inline void from_json(const BasicJsonType& j, std::map<Key, Value, Compare, Allocator>& m)
  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. m.clear();
  4121. for (const auto& p : j)
  4122. {
  4123. if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
  4124. {
  4125. JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &j));
  4126. }
  4127. m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
  4128. }
  4129. }
  4130. template < typename BasicJsonType, typename Key, typename Value, typename Hash, typename KeyEqual, typename Allocator,
  4131. typename = enable_if_t < !std::is_constructible <
  4132. typename BasicJsonType::string_t, Key >::value >>
  4133. inline void from_json(const BasicJsonType& j, std::unordered_map<Key, Value, Hash, KeyEqual, Allocator>& m)
  4134. {
  4135. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  4136. {
  4137. JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
  4138. }
  4139. m.clear();
  4140. for (const auto& p : j)
  4141. {
  4142. if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
  4143. {
  4144. JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &j));
  4145. }
  4146. m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
  4147. }
  4148. }
  4149. #if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
  4150. template<typename BasicJsonType>
  4151. inline void from_json(const BasicJsonType& j, std_fs::path& p)
  4152. {
  4153. if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
  4154. {
  4155. JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
  4156. }
  4157. p = *j.template get_ptr<const typename BasicJsonType::string_t*>();
  4158. }
  4159. #endif
  4160. struct from_json_fn
  4161. {
  4162. template<typename BasicJsonType, typename T>
  4163. auto operator()(const BasicJsonType& j, T&& val) const
  4164. noexcept(noexcept(from_json(j, std::forward<T>(val))))
  4165. -> decltype(from_json(j, std::forward<T>(val)))
  4166. {
  4167. return from_json(j, std::forward<T>(val));
  4168. }
  4169. };
  4170. } // namespace detail
  4171. #ifndef JSON_HAS_CPP_17
  4172. /// namespace to hold default `from_json` function
  4173. /// to see why this is required:
  4174. /// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
  4175. namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces)
  4176. {
  4177. #endif
  4178. JSON_INLINE_VARIABLE constexpr const auto& from_json = // NOLINT(misc-definitions-in-headers)
  4179. detail::static_const<detail::from_json_fn>::value;
  4180. #ifndef JSON_HAS_CPP_17
  4181. } // namespace
  4182. #endif
  4183. } // namespace nlohmann
  4184. // #include <nlohmann/detail/conversions/to_json.hpp>
  4185. #include <algorithm> // copy
  4186. #include <iterator> // begin, end
  4187. #include <string> // string
  4188. #include <tuple> // tuple, get
  4189. #include <type_traits> // is_same, is_constructible, is_floating_point, is_enum, underlying_type
  4190. #include <utility> // move, forward, declval, pair
  4191. #include <valarray> // valarray
  4192. #include <vector> // vector
  4193. // #include <nlohmann/detail/macro_scope.hpp>
  4194. // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
  4195. #include <cstddef> // size_t
  4196. #include <iterator> // input_iterator_tag
  4197. #include <string> // string, to_string
  4198. #include <tuple> // tuple_size, get, tuple_element
  4199. #include <utility> // move
  4200. #if JSON_HAS_RANGES
  4201. #include <ranges> // enable_borrowed_range
  4202. #endif
  4203. // #include <nlohmann/detail/meta/type_traits.hpp>
  4204. // #include <nlohmann/detail/value_t.hpp>
  4205. namespace nlohmann
  4206. {
  4207. namespace detail
  4208. {
  4209. template<typename string_type>
  4210. void int_to_string( string_type& target, std::size_t value )
  4211. {
  4212. // For ADL
  4213. using std::to_string;
  4214. target = to_string(value);
  4215. }
  4216. template<typename IteratorType> class iteration_proxy_value
  4217. {
  4218. public:
  4219. using difference_type = std::ptrdiff_t;
  4220. using value_type = iteration_proxy_value;
  4221. using pointer = value_type *;
  4222. using reference = value_type &;
  4223. using iterator_category = std::input_iterator_tag;
  4224. using string_type = typename std::remove_cv< typename std::remove_reference<decltype( std::declval<IteratorType>().key() ) >::type >::type;
  4225. private:
  4226. /// the iterator
  4227. IteratorType anchor{};
  4228. /// an index for arrays (used to create key names)
  4229. std::size_t array_index = 0;
  4230. /// last stringified array index
  4231. mutable std::size_t array_index_last = 0;
  4232. /// a string representation of the array index
  4233. mutable string_type array_index_str = "0";
  4234. /// an empty string (to return a reference for primitive values)
  4235. string_type empty_str{};
  4236. public:
  4237. explicit iteration_proxy_value() = default;
  4238. explicit iteration_proxy_value(IteratorType it, std::size_t array_index_ = 0)
  4239. noexcept(std::is_nothrow_move_constructible<IteratorType>::value
  4240. && std::is_nothrow_default_constructible<string_type>::value)
  4241. : anchor(std::move(it))
  4242. , array_index(array_index_)
  4243. {}
  4244. iteration_proxy_value(iteration_proxy_value const&) = default;
  4245. iteration_proxy_value& operator=(iteration_proxy_value const&) = default;
  4246. // older GCCs are a bit fussy and require explicit noexcept specifiers on defaulted functions
  4247. iteration_proxy_value(iteration_proxy_value&&)
  4248. noexcept(std::is_nothrow_move_constructible<IteratorType>::value
  4249. && std::is_nothrow_move_constructible<string_type>::value) = default;
  4250. iteration_proxy_value& operator=(iteration_proxy_value&&)
  4251. noexcept(std::is_nothrow_move_assignable<IteratorType>::value
  4252. && std::is_nothrow_move_assignable<string_type>::value) = default;
  4253. ~iteration_proxy_value() = default;
  4254. /// dereference operator (needed for range-based for)
  4255. const iteration_proxy_value& operator*() const
  4256. {
  4257. return *this;
  4258. }
  4259. /// increment operator (needed for range-based for)
  4260. iteration_proxy_value& operator++()
  4261. {
  4262. ++anchor;
  4263. ++array_index;
  4264. return *this;
  4265. }
  4266. iteration_proxy_value operator++(int)& // NOLINT(cert-dcl21-cpp)
  4267. {
  4268. auto tmp = iteration_proxy_value(anchor, array_index);
  4269. ++anchor;
  4270. ++array_index;
  4271. return tmp;
  4272. }
  4273. /// equality operator (needed for InputIterator)
  4274. bool operator==(const iteration_proxy_value& o) const
  4275. {
  4276. return anchor == o.anchor;
  4277. }
  4278. /// inequality operator (needed for range-based for)
  4279. bool operator!=(const iteration_proxy_value& o) const
  4280. {
  4281. return anchor != o.anchor;
  4282. }
  4283. /// return key of the iterator
  4284. const string_type& key() const
  4285. {
  4286. JSON_ASSERT(anchor.m_object != nullptr);
  4287. switch (anchor.m_object->type())
  4288. {
  4289. // use integer array index as key
  4290. case value_t::array:
  4291. {
  4292. if (array_index != array_index_last)
  4293. {
  4294. int_to_string( array_index_str, array_index );
  4295. array_index_last = array_index;
  4296. }
  4297. return array_index_str;
  4298. }
  4299. // use key from the object
  4300. case value_t::object:
  4301. return anchor.key();
  4302. // use an empty key for all primitive types
  4303. case value_t::null:
  4304. case value_t::string:
  4305. case value_t::boolean:
  4306. case value_t::number_integer:
  4307. case value_t::number_unsigned:
  4308. case value_t::number_float:
  4309. case value_t::binary:
  4310. case value_t::discarded:
  4311. default:
  4312. return empty_str;
  4313. }
  4314. }
  4315. /// return value of the iterator
  4316. typename IteratorType::reference value() const
  4317. {
  4318. return anchor.value();
  4319. }
  4320. };
  4321. /// proxy class for the items() function
  4322. template<typename IteratorType> class iteration_proxy
  4323. {
  4324. private:
  4325. /// the container to iterate
  4326. typename IteratorType::pointer container = nullptr;
  4327. public:
  4328. explicit iteration_proxy() = default;
  4329. /// construct iteration proxy from a container
  4330. explicit iteration_proxy(typename IteratorType::reference cont) noexcept
  4331. : container(&cont) {}
  4332. iteration_proxy(iteration_proxy const&) = default;
  4333. iteration_proxy& operator=(iteration_proxy const&) = default;
  4334. iteration_proxy(iteration_proxy&&) noexcept = default;
  4335. iteration_proxy& operator=(iteration_proxy&&) noexcept = default;
  4336. ~iteration_proxy() = default;
  4337. /// return iterator begin (needed for range-based for)
  4338. iteration_proxy_value<IteratorType> begin() const noexcept
  4339. {
  4340. return iteration_proxy_value<IteratorType>(container->begin());
  4341. }
  4342. /// return iterator end (needed for range-based for)
  4343. iteration_proxy_value<IteratorType> end() const noexcept
  4344. {
  4345. return iteration_proxy_value<IteratorType>(container->end());
  4346. }
  4347. };
  4348. // Structured Bindings Support
  4349. // For further reference see https://blog.tartanllama.xyz/structured-bindings/
  4350. // And see https://github.com/nlohmann/json/pull/1391
  4351. template<std::size_t N, typename IteratorType, enable_if_t<N == 0, int> = 0>
  4352. auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.key())
  4353. {
  4354. return i.key();
  4355. }
  4356. // Structured Bindings Support
  4357. // For further reference see https://blog.tartanllama.xyz/structured-bindings/
  4358. // And see https://github.com/nlohmann/json/pull/1391
  4359. template<std::size_t N, typename IteratorType, enable_if_t<N == 1, int> = 0>
  4360. auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.value())
  4361. {
  4362. return i.value();
  4363. }
  4364. } // namespace detail
  4365. } // namespace nlohmann
  4366. // The Addition to the STD Namespace is required to add
  4367. // Structured Bindings Support to the iteration_proxy_value class
  4368. // For further reference see https://blog.tartanllama.xyz/structured-bindings/
  4369. // And see https://github.com/nlohmann/json/pull/1391
  4370. namespace std
  4371. {
  4372. #if defined(__clang__)
  4373. // Fix: https://github.com/nlohmann/json/issues/1401
  4374. #pragma clang diagnostic push
  4375. #pragma clang diagnostic ignored "-Wmismatched-tags"
  4376. #endif
  4377. template<typename IteratorType>
  4378. class tuple_size<::nlohmann::detail::iteration_proxy_value<IteratorType>>
  4379. : public std::integral_constant<std::size_t, 2> {};
  4380. template<std::size_t N, typename IteratorType>
  4381. class tuple_element<N, ::nlohmann::detail::iteration_proxy_value<IteratorType >>
  4382. {
  4383. public:
  4384. using type = decltype(
  4385. get<N>(std::declval <
  4386. ::nlohmann::detail::iteration_proxy_value<IteratorType >> ()));
  4387. };
  4388. #if defined(__clang__)
  4389. #pragma clang diagnostic pop
  4390. #endif
  4391. } // namespace std
  4392. #if JSON_HAS_RANGES
  4393. template <typename IteratorType>
  4394. inline constexpr bool ::std::ranges::enable_borrowed_range<::nlohmann::detail::iteration_proxy<IteratorType>> = true;
  4395. #endif
  4396. // #include <nlohmann/detail/meta/cpp_future.hpp>
  4397. // #include <nlohmann/detail/meta/type_traits.hpp>
  4398. // #include <nlohmann/detail/value_t.hpp>
  4399. #if JSON_HAS_EXPERIMENTAL_FILESYSTEM
  4400. #include <experimental/filesystem>
  4401. namespace nlohmann::detail
  4402. {
  4403. namespace std_fs = std::experimental::filesystem;
  4404. } // namespace nlohmann::detail
  4405. #elif JSON_HAS_FILESYSTEM
  4406. #include <filesystem>
  4407. namespace nlohmann::detail
  4408. {
  4409. namespace std_fs = std::filesystem;
  4410. } // namespace nlohmann::detail
  4411. #endif
  4412. namespace nlohmann
  4413. {
  4414. namespace detail
  4415. {
  4416. //////////////////
  4417. // constructors //
  4418. //////////////////
  4419. /*
  4420. * Note all external_constructor<>::construct functions need to call
  4421. * j.m_value.destroy(j.m_type) to avoid a memory leak in case j contains an
  4422. * allocated value (e.g., a string). See bug issue
  4423. * https://github.com/nlohmann/json/issues/2865 for more information.
  4424. */
  4425. template<value_t> struct external_constructor;
  4426. template<>
  4427. struct external_constructor<value_t::boolean>
  4428. {
  4429. template<typename BasicJsonType>
  4430. static void construct(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept
  4431. {
  4432. j.m_value.destroy(j.m_type);
  4433. j.m_type = value_t::boolean;
  4434. j.m_value = b;
  4435. j.assert_invariant();
  4436. }
  4437. };
  4438. template<>
  4439. struct external_constructor<value_t::string>
  4440. {
  4441. template<typename BasicJsonType>
  4442. static void construct(BasicJsonType& j, const typename BasicJsonType::string_t& s)
  4443. {
  4444. j.m_value.destroy(j.m_type);
  4445. j.m_type = value_t::string;
  4446. j.m_value = s;
  4447. j.assert_invariant();
  4448. }
  4449. template<typename BasicJsonType>
  4450. static void construct(BasicJsonType& j, typename BasicJsonType::string_t&& s)
  4451. {
  4452. j.m_value.destroy(j.m_type);
  4453. j.m_type = value_t::string;
  4454. j.m_value = std::move(s);
  4455. j.assert_invariant();
  4456. }
  4457. template < typename BasicJsonType, typename CompatibleStringType,
  4458. enable_if_t < !std::is_same<CompatibleStringType, typename BasicJsonType::string_t>::value,
  4459. int > = 0 >
  4460. static void construct(BasicJsonType& j, const CompatibleStringType& str)
  4461. {
  4462. j.m_value.destroy(j.m_type);
  4463. j.m_type = value_t::string;
  4464. j.m_value.string = j.template create<typename BasicJsonType::string_t>(str);
  4465. j.assert_invariant();
  4466. }
  4467. };
  4468. template<>
  4469. struct external_constructor<value_t::binary>
  4470. {
  4471. template<typename BasicJsonType>
  4472. static void construct(BasicJsonType& j, const typename BasicJsonType::binary_t& b)
  4473. {
  4474. j.m_value.destroy(j.m_type);
  4475. j.m_type = value_t::binary;
  4476. j.m_value = typename BasicJsonType::binary_t(b);
  4477. j.assert_invariant();
  4478. }
  4479. template<typename BasicJsonType>
  4480. static void construct(BasicJsonType& j, typename BasicJsonType::binary_t&& b)
  4481. {
  4482. j.m_value.destroy(j.m_type);
  4483. j.m_type = value_t::binary;
  4484. j.m_value = typename BasicJsonType::binary_t(std::move(b));
  4485. j.assert_invariant();
  4486. }
  4487. };
  4488. template<>
  4489. struct external_constructor<value_t::number_float>
  4490. {
  4491. template<typename BasicJsonType>
  4492. static void construct(BasicJsonType& j, typename BasicJsonType::number_float_t val) noexcept
  4493. {
  4494. j.m_value.destroy(j.m_type);
  4495. j.m_type = value_t::number_float;
  4496. j.m_value = val;
  4497. j.assert_invariant();
  4498. }
  4499. };
  4500. template<>
  4501. struct external_constructor<value_t::number_unsigned>
  4502. {
  4503. template<typename BasicJsonType>
  4504. static void construct(BasicJsonType& j, typename BasicJsonType::number_unsigned_t val) noexcept
  4505. {
  4506. j.m_value.destroy(j.m_type);
  4507. j.m_type = value_t::number_unsigned;
  4508. j.m_value = val;
  4509. j.assert_invariant();
  4510. }
  4511. };
  4512. template<>
  4513. struct external_constructor<value_t::number_integer>
  4514. {
  4515. template<typename BasicJsonType>
  4516. static void construct(BasicJsonType& j, typename BasicJsonType::number_integer_t val) noexcept
  4517. {
  4518. j.m_value.destroy(j.m_type);
  4519. j.m_type = value_t::number_integer;
  4520. j.m_value = val;
  4521. j.assert_invariant();
  4522. }
  4523. };
  4524. template<>
  4525. struct external_constructor<value_t::array>
  4526. {
  4527. template<typename BasicJsonType>
  4528. static void construct(BasicJsonType& j, const typename BasicJsonType::array_t& arr)
  4529. {
  4530. j.m_value.destroy(j.m_type);
  4531. j.m_type = value_t::array;
  4532. j.m_value = arr;
  4533. j.set_parents();
  4534. j.assert_invariant();
  4535. }
  4536. template<typename BasicJsonType>
  4537. static void construct(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
  4538. {
  4539. j.m_value.destroy(j.m_type);
  4540. j.m_type = value_t::array;
  4541. j.m_value = std::move(arr);
  4542. j.set_parents();
  4543. j.assert_invariant();
  4544. }
  4545. template < typename BasicJsonType, typename CompatibleArrayType,
  4546. enable_if_t < !std::is_same<CompatibleArrayType, typename BasicJsonType::array_t>::value,
  4547. int > = 0 >
  4548. static void construct(BasicJsonType& j, const CompatibleArrayType& arr)
  4549. {
  4550. using std::begin;
  4551. using std::end;
  4552. j.m_value.destroy(j.m_type);
  4553. j.m_type = value_t::array;
  4554. j.m_value.array = j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
  4555. j.set_parents();
  4556. j.assert_invariant();
  4557. }
  4558. template<typename BasicJsonType>
  4559. static void construct(BasicJsonType& j, const std::vector<bool>& arr)
  4560. {
  4561. j.m_value.destroy(j.m_type);
  4562. j.m_type = value_t::array;
  4563. j.m_value = value_t::array;
  4564. j.m_value.array->reserve(arr.size());
  4565. for (const bool x : arr)
  4566. {
  4567. j.m_value.array->push_back(x);
  4568. j.set_parent(j.m_value.array->back());
  4569. }
  4570. j.assert_invariant();
  4571. }
  4572. template<typename BasicJsonType, typename T,
  4573. enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
  4574. static void construct(BasicJsonType& j, const std::valarray<T>& arr)
  4575. {
  4576. j.m_value.destroy(j.m_type);
  4577. j.m_type = value_t::array;
  4578. j.m_value = value_t::array;
  4579. j.m_value.array->resize(arr.size());
  4580. if (arr.size() > 0)
  4581. {
  4582. std::copy(std::begin(arr), std::end(arr), j.m_value.array->begin());
  4583. }
  4584. j.set_parents();
  4585. j.assert_invariant();
  4586. }
  4587. };
  4588. template<>
  4589. struct external_constructor<value_t::object>
  4590. {
  4591. template<typename BasicJsonType>
  4592. static void construct(BasicJsonType& j, const typename BasicJsonType::object_t& obj)
  4593. {
  4594. j.m_value.destroy(j.m_type);
  4595. j.m_type = value_t::object;
  4596. j.m_value = obj;
  4597. j.set_parents();
  4598. j.assert_invariant();
  4599. }
  4600. template<typename BasicJsonType>
  4601. static void construct(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
  4602. {
  4603. j.m_value.destroy(j.m_type);
  4604. j.m_type = value_t::object;
  4605. j.m_value = std::move(obj);
  4606. j.set_parents();
  4607. j.assert_invariant();
  4608. }
  4609. template < typename BasicJsonType, typename CompatibleObjectType,
  4610. enable_if_t < !std::is_same<CompatibleObjectType, typename BasicJsonType::object_t>::value, int > = 0 >
  4611. static void construct(BasicJsonType& j, const CompatibleObjectType& obj)
  4612. {
  4613. using std::begin;
  4614. using std::end;
  4615. j.m_value.destroy(j.m_type);
  4616. j.m_type = value_t::object;
  4617. j.m_value.object = j.template create<typename BasicJsonType::object_t>(begin(obj), end(obj));
  4618. j.set_parents();
  4619. j.assert_invariant();
  4620. }
  4621. };
  4622. /////////////
  4623. // to_json //
  4624. /////////////
  4625. template<typename BasicJsonType, typename T,
  4626. enable_if_t<std::is_same<T, typename BasicJsonType::boolean_t>::value, int> = 0>
  4627. inline void to_json(BasicJsonType& j, T b) noexcept
  4628. {
  4629. external_constructor<value_t::boolean>::construct(j, b);
  4630. }
  4631. template<typename BasicJsonType,
  4632. enable_if_t<std::is_convertible<const std::vector<bool>::reference&, typename BasicJsonType::boolean_t>::value, int> = 0>
  4633. inline void to_json(BasicJsonType& j, const std::vector<bool>::reference& b) noexcept
  4634. {
  4635. external_constructor<value_t::boolean>::construct(j, static_cast<typename BasicJsonType::boolean_t>(b));
  4636. }
  4637. template<typename BasicJsonType, typename CompatibleString,
  4638. enable_if_t<std::is_constructible<typename BasicJsonType::string_t, CompatibleString>::value, int> = 0>
  4639. inline void to_json(BasicJsonType& j, const CompatibleString& s)
  4640. {
  4641. external_constructor<value_t::string>::construct(j, s);
  4642. }
  4643. template<typename BasicJsonType>
  4644. inline void to_json(BasicJsonType& j, typename BasicJsonType::string_t&& s)
  4645. {
  4646. external_constructor<value_t::string>::construct(j, std::move(s));
  4647. }
  4648. template<typename BasicJsonType, typename FloatType,
  4649. enable_if_t<std::is_floating_point<FloatType>::value, int> = 0>
  4650. inline void to_json(BasicJsonType& j, FloatType val) noexcept
  4651. {
  4652. external_constructor<value_t::number_float>::construct(j, static_cast<typename BasicJsonType::number_float_t>(val));
  4653. }
  4654. template<typename BasicJsonType, typename CompatibleNumberUnsignedType,
  4655. enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_unsigned_t, CompatibleNumberUnsignedType>::value, int> = 0>
  4656. inline void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val) noexcept
  4657. {
  4658. external_constructor<value_t::number_unsigned>::construct(j, static_cast<typename BasicJsonType::number_unsigned_t>(val));
  4659. }
  4660. template<typename BasicJsonType, typename CompatibleNumberIntegerType,
  4661. enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_integer_t, CompatibleNumberIntegerType>::value, int> = 0>
  4662. inline void to_json(BasicJsonType& j, CompatibleNumberIntegerType val) noexcept
  4663. {
  4664. external_constructor<value_t::number_integer>::construct(j, static_cast<typename BasicJsonType::number_integer_t>(val));
  4665. }
  4666. #if !JSON_DISABLE_ENUM_SERIALIZATION
  4667. template<typename BasicJsonType, typename EnumType,
  4668. enable_if_t<std::is_enum<EnumType>::value, int> = 0>
  4669. inline void to_json(BasicJsonType& j, EnumType e) noexcept
  4670. {
  4671. using underlying_type = typename std::underlying_type<EnumType>::type;
  4672. external_constructor<value_t::number_integer>::construct(j, static_cast<underlying_type>(e));
  4673. }
  4674. #endif // JSON_DISABLE_ENUM_SERIALIZATION
  4675. template<typename BasicJsonType>
  4676. inline void to_json(BasicJsonType& j, const std::vector<bool>& e)
  4677. {
  4678. external_constructor<value_t::array>::construct(j, e);
  4679. }
  4680. template < typename BasicJsonType, typename CompatibleArrayType,
  4681. enable_if_t < is_compatible_array_type<BasicJsonType,
  4682. CompatibleArrayType>::value&&
  4683. !is_compatible_object_type<BasicJsonType, CompatibleArrayType>::value&&
  4684. !is_compatible_string_type<BasicJsonType, CompatibleArrayType>::value&&
  4685. !std::is_same<typename BasicJsonType::binary_t, CompatibleArrayType>::value&&
  4686. !is_basic_json<CompatibleArrayType>::value,
  4687. int > = 0 >
  4688. inline void to_json(BasicJsonType& j, const CompatibleArrayType& arr)
  4689. {
  4690. external_constructor<value_t::array>::construct(j, arr);
  4691. }
  4692. template<typename BasicJsonType>
  4693. inline void to_json(BasicJsonType& j, const typename BasicJsonType::binary_t& bin)
  4694. {
  4695. external_constructor<value_t::binary>::construct(j, bin);
  4696. }
  4697. template<typename BasicJsonType, typename T,
  4698. enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
  4699. inline void to_json(BasicJsonType& j, const std::valarray<T>& arr)
  4700. {
  4701. external_constructor<value_t::array>::construct(j, std::move(arr));
  4702. }
  4703. template<typename BasicJsonType>
  4704. inline void to_json(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
  4705. {
  4706. external_constructor<value_t::array>::construct(j, std::move(arr));
  4707. }
  4708. template < typename BasicJsonType, typename CompatibleObjectType,
  4709. enable_if_t < is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value&& !is_basic_json<CompatibleObjectType>::value, int > = 0 >
  4710. inline void to_json(BasicJsonType& j, const CompatibleObjectType& obj)
  4711. {
  4712. external_constructor<value_t::object>::construct(j, obj);
  4713. }
  4714. template<typename BasicJsonType>
  4715. inline void to_json(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
  4716. {
  4717. external_constructor<value_t::object>::construct(j, std::move(obj));
  4718. }
  4719. template <
  4720. typename BasicJsonType, typename T, std::size_t N,
  4721. enable_if_t < !std::is_constructible<typename BasicJsonType::string_t,
  4722. const T(&)[N]>::value, // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  4723. int > = 0 >
  4724. inline void to_json(BasicJsonType& j, const T(&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  4725. {
  4726. external_constructor<value_t::array>::construct(j, arr);
  4727. }
  4728. template < typename BasicJsonType, typename T1, typename T2, enable_if_t < std::is_constructible<BasicJsonType, T1>::value&& std::is_constructible<BasicJsonType, T2>::value, int > = 0 >
  4729. inline void to_json(BasicJsonType& j, const std::pair<T1, T2>& p)
  4730. {
  4731. j = { p.first, p.second };
  4732. }
  4733. // for https://github.com/nlohmann/json/pull/1134
  4734. template<typename BasicJsonType, typename T,
  4735. enable_if_t<std::is_same<T, iteration_proxy_value<typename BasicJsonType::iterator>>::value, int> = 0>
  4736. inline void to_json(BasicJsonType& j, const T& b)
  4737. {
  4738. j = { {b.key(), b.value()} };
  4739. }
  4740. template<typename BasicJsonType, typename Tuple, std::size_t... Idx>
  4741. inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence<Idx...> /*unused*/)
  4742. {
  4743. j = { std::get<Idx>(t)... };
  4744. }
  4745. template<typename BasicJsonType, typename T, enable_if_t<is_constructible_tuple<BasicJsonType, T>::value, int > = 0>
  4746. inline void to_json(BasicJsonType& j, const T& t)
  4747. {
  4748. to_json_tuple_impl(j, t, make_index_sequence<std::tuple_size<T>::value> {});
  4749. }
  4750. #if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
  4751. template<typename BasicJsonType>
  4752. inline void to_json(BasicJsonType& j, const std_fs::path& p)
  4753. {
  4754. j = p.string();
  4755. }
  4756. #endif
  4757. struct to_json_fn
  4758. {
  4759. template<typename BasicJsonType, typename T>
  4760. auto operator()(BasicJsonType& j, T&& val) const noexcept(noexcept(to_json(j, std::forward<T>(val))))
  4761. -> decltype(to_json(j, std::forward<T>(val)), void())
  4762. {
  4763. return to_json(j, std::forward<T>(val));
  4764. }
  4765. };
  4766. } // namespace detail
  4767. #ifndef JSON_HAS_CPP_17
  4768. /// namespace to hold default `to_json` function
  4769. /// to see why this is required:
  4770. /// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
  4771. namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces)
  4772. {
  4773. #endif
  4774. JSON_INLINE_VARIABLE constexpr const auto& to_json = // NOLINT(misc-definitions-in-headers)
  4775. detail::static_const<detail::to_json_fn>::value;
  4776. #ifndef JSON_HAS_CPP_17
  4777. } // namespace
  4778. #endif
  4779. } // namespace nlohmann
  4780. // #include <nlohmann/detail/meta/identity_tag.hpp>
  4781. // #include <nlohmann/detail/meta/type_traits.hpp>
  4782. namespace nlohmann
  4783. {
  4784. /// @sa https://json.nlohmann.me/api/adl_serializer/
  4785. template<typename ValueType, typename>
  4786. struct adl_serializer
  4787. {
  4788. /// @brief convert a JSON value to any value type
  4789. /// @sa https://json.nlohmann.me/api/adl_serializer/from_json/
  4790. template<typename BasicJsonType, typename TargetType = ValueType>
  4791. static auto from_json(BasicJsonType && j, TargetType& val) noexcept(
  4792. noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), val)))
  4793. -> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), val), void())
  4794. {
  4795. ::nlohmann::from_json(std::forward<BasicJsonType>(j), val);
  4796. }
  4797. /// @brief convert a JSON value to any value type
  4798. /// @sa https://json.nlohmann.me/api/adl_serializer/from_json/
  4799. template<typename BasicJsonType, typename TargetType = ValueType>
  4800. static auto from_json(BasicJsonType && j) noexcept(
  4801. noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {})))
  4802. -> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {}))
  4803. {
  4804. return ::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {});
  4805. }
  4806. /// @brief convert any value type to a JSON value
  4807. /// @sa https://json.nlohmann.me/api/adl_serializer/to_json/
  4808. template<typename BasicJsonType, typename TargetType = ValueType>
  4809. static auto to_json(BasicJsonType& j, TargetType && val) noexcept(
  4810. noexcept(::nlohmann::to_json(j, std::forward<TargetType>(val))))
  4811. -> decltype(::nlohmann::to_json(j, std::forward<TargetType>(val)), void())
  4812. {
  4813. ::nlohmann::to_json(j, std::forward<TargetType>(val));
  4814. }
  4815. };
  4816. } // namespace nlohmann
  4817. // #include <nlohmann/byte_container_with_subtype.hpp>
  4818. #include <cstdint> // uint8_t, uint64_t
  4819. #include <tuple> // tie
  4820. #include <utility> // move
  4821. namespace nlohmann
  4822. {
  4823. /// @brief an internal type for a backed binary type
  4824. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/
  4825. template<typename BinaryType>
  4826. class byte_container_with_subtype : public BinaryType
  4827. {
  4828. public:
  4829. using container_type = BinaryType;
  4830. using subtype_type = std::uint64_t;
  4831. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  4832. byte_container_with_subtype() noexcept(noexcept(container_type()))
  4833. : container_type()
  4834. {}
  4835. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  4836. byte_container_with_subtype(const container_type& b) noexcept(noexcept(container_type(b)))
  4837. : container_type(b)
  4838. {}
  4839. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  4840. byte_container_with_subtype(container_type&& b) noexcept(noexcept(container_type(std::move(b))))
  4841. : container_type(std::move(b))
  4842. {}
  4843. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  4844. byte_container_with_subtype(const container_type& b, subtype_type subtype_) noexcept(noexcept(container_type(b)))
  4845. : container_type(b)
  4846. , m_subtype(subtype_)
  4847. , m_has_subtype(true)
  4848. {}
  4849. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
  4850. byte_container_with_subtype(container_type&& b, subtype_type subtype_) noexcept(noexcept(container_type(std::move(b))))
  4851. : container_type(std::move(b))
  4852. , m_subtype(subtype_)
  4853. , m_has_subtype(true)
  4854. {}
  4855. bool operator==(const byte_container_with_subtype& rhs) const
  4856. {
  4857. return std::tie(static_cast<const BinaryType&>(*this), m_subtype, m_has_subtype) ==
  4858. std::tie(static_cast<const BinaryType&>(rhs), rhs.m_subtype, rhs.m_has_subtype);
  4859. }
  4860. bool operator!=(const byte_container_with_subtype& rhs) const
  4861. {
  4862. return !(rhs == *this);
  4863. }
  4864. /// @brief sets the binary subtype
  4865. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/set_subtype/
  4866. void set_subtype(subtype_type subtype_) noexcept
  4867. {
  4868. m_subtype = subtype_;
  4869. m_has_subtype = true;
  4870. }
  4871. /// @brief return the binary subtype
  4872. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/subtype/
  4873. constexpr subtype_type subtype() const noexcept
  4874. {
  4875. return m_has_subtype ? m_subtype : static_cast<subtype_type>(-1);
  4876. }
  4877. /// @brief return whether the value has a subtype
  4878. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/has_subtype/
  4879. constexpr bool has_subtype() const noexcept
  4880. {
  4881. return m_has_subtype;
  4882. }
  4883. /// @brief clears the binary subtype
  4884. /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/clear_subtype/
  4885. void clear_subtype() noexcept
  4886. {
  4887. m_subtype = 0;
  4888. m_has_subtype = false;
  4889. }
  4890. private:
  4891. subtype_type m_subtype = 0;
  4892. bool m_has_subtype = false;
  4893. };
  4894. } // namespace nlohmann
  4895. // #include <nlohmann/detail/conversions/from_json.hpp>
  4896. // #include <nlohmann/detail/conversions/to_json.hpp>
  4897. // #include <nlohmann/detail/exceptions.hpp>
  4898. // #include <nlohmann/detail/hash.hpp>
  4899. #include <cstdint> // uint8_t
  4900. #include <cstddef> // size_t
  4901. #include <functional> // hash
  4902. // #include <nlohmann/detail/macro_scope.hpp>
  4903. // #include <nlohmann/detail/value_t.hpp>
  4904. namespace nlohmann
  4905. {
  4906. namespace detail
  4907. {
  4908. // boost::hash_combine
  4909. inline std::size_t combine(std::size_t seed, std::size_t h) noexcept
  4910. {
  4911. seed ^= h + 0x9e3779b9 + (seed << 6U) + (seed >> 2U);
  4912. return seed;
  4913. }
  4914. /*!
  4915. @brief hash a JSON value
  4916. The hash function tries to rely on std::hash where possible. Furthermore, the
  4917. type of the JSON value is taken into account to have different hash values for
  4918. null, 0, 0U, and false, etc.
  4919. @tparam BasicJsonType basic_json specialization
  4920. @param j JSON value to hash
  4921. @return hash value of j
  4922. */
  4923. template<typename BasicJsonType>
  4924. std::size_t hash(const BasicJsonType& j)
  4925. {
  4926. using string_t = typename BasicJsonType::string_t;
  4927. using number_integer_t = typename BasicJsonType::number_integer_t;
  4928. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  4929. using number_float_t = typename BasicJsonType::number_float_t;
  4930. const auto type = static_cast<std::size_t>(j.type());
  4931. switch (j.type())
  4932. {
  4933. case BasicJsonType::value_t::null:
  4934. case BasicJsonType::value_t::discarded:
  4935. {
  4936. return combine(type, 0);
  4937. }
  4938. case BasicJsonType::value_t::object:
  4939. {
  4940. auto seed = combine(type, j.size());
  4941. for (const auto& element : j.items())
  4942. {
  4943. const auto h = std::hash<string_t> {}(element.key());
  4944. seed = combine(seed, h);
  4945. seed = combine(seed, hash(element.value()));
  4946. }
  4947. return seed;
  4948. }
  4949. case BasicJsonType::value_t::array:
  4950. {
  4951. auto seed = combine(type, j.size());
  4952. for (const auto& element : j)
  4953. {
  4954. seed = combine(seed, hash(element));
  4955. }
  4956. return seed;
  4957. }
  4958. case BasicJsonType::value_t::string:
  4959. {
  4960. const auto h = std::hash<string_t> {}(j.template get_ref<const string_t&>());
  4961. return combine(type, h);
  4962. }
  4963. case BasicJsonType::value_t::boolean:
  4964. {
  4965. const auto h = std::hash<bool> {}(j.template get<bool>());
  4966. return combine(type, h);
  4967. }
  4968. case BasicJsonType::value_t::number_integer:
  4969. {
  4970. const auto h = std::hash<number_integer_t> {}(j.template get<number_integer_t>());
  4971. return combine(type, h);
  4972. }
  4973. case BasicJsonType::value_t::number_unsigned:
  4974. {
  4975. const auto h = std::hash<number_unsigned_t> {}(j.template get<number_unsigned_t>());
  4976. return combine(type, h);
  4977. }
  4978. case BasicJsonType::value_t::number_float:
  4979. {
  4980. const auto h = std::hash<number_float_t> {}(j.template get<number_float_t>());
  4981. return combine(type, h);
  4982. }
  4983. case BasicJsonType::value_t::binary:
  4984. {
  4985. auto seed = combine(type, j.get_binary().size());
  4986. const auto h = std::hash<bool> {}(j.get_binary().has_subtype());
  4987. seed = combine(seed, h);
  4988. seed = combine(seed, static_cast<std::size_t>(j.get_binary().subtype()));
  4989. for (const auto byte : j.get_binary())
  4990. {
  4991. seed = combine(seed, std::hash<std::uint8_t> {}(byte));
  4992. }
  4993. return seed;
  4994. }
  4995. default: // LCOV_EXCL_LINE
  4996. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  4997. return 0; // LCOV_EXCL_LINE
  4998. }
  4999. }
  5000. } // namespace detail
  5001. } // namespace nlohmann
  5002. // #include <nlohmann/detail/input/binary_reader.hpp>
  5003. #include <algorithm> // generate_n
  5004. #include <array> // array
  5005. #include <cmath> // ldexp
  5006. #include <cstddef> // size_t
  5007. #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
  5008. #include <cstdio> // snprintf
  5009. #include <cstring> // memcpy
  5010. #include <iterator> // back_inserter
  5011. #include <limits> // numeric_limits
  5012. #include <string> // char_traits, string
  5013. #include <utility> // make_pair, move
  5014. #include <vector> // vector
  5015. #include <map> // map
  5016. // #include <nlohmann/detail/exceptions.hpp>
  5017. // #include <nlohmann/detail/input/input_adapters.hpp>
  5018. #include <array> // array
  5019. #include <cstddef> // size_t
  5020. #include <cstring> // strlen
  5021. #include <iterator> // begin, end, iterator_traits, random_access_iterator_tag, distance, next
  5022. #include <memory> // shared_ptr, make_shared, addressof
  5023. #include <numeric> // accumulate
  5024. #include <string> // string, char_traits
  5025. #include <type_traits> // enable_if, is_base_of, is_pointer, is_integral, remove_pointer
  5026. #include <utility> // pair, declval
  5027. #ifndef JSON_NO_IO
  5028. #include <cstdio> // FILE *
  5029. #include <istream> // istream
  5030. #endif // JSON_NO_IO
  5031. // #include <nlohmann/detail/iterators/iterator_traits.hpp>
  5032. // #include <nlohmann/detail/macro_scope.hpp>
  5033. namespace nlohmann
  5034. {
  5035. namespace detail
  5036. {
  5037. /// the supported input formats
  5038. enum class input_format_t { json, cbor, msgpack, ubjson, bson, bjdata };
  5039. ////////////////////
  5040. // input adapters //
  5041. ////////////////////
  5042. #ifndef JSON_NO_IO
  5043. /*!
  5044. Input adapter for stdio file access. This adapter read only 1 byte and do not use any
  5045. buffer. This adapter is a very low level adapter.
  5046. */
  5047. class file_input_adapter
  5048. {
  5049. public:
  5050. using char_type = char;
  5051. JSON_HEDLEY_NON_NULL(2)
  5052. explicit file_input_adapter(std::FILE* f) noexcept
  5053. : m_file(f)
  5054. {}
  5055. // make class move-only
  5056. file_input_adapter(const file_input_adapter&) = delete;
  5057. file_input_adapter(file_input_adapter&&) noexcept = default;
  5058. file_input_adapter& operator=(const file_input_adapter&) = delete;
  5059. file_input_adapter& operator=(file_input_adapter&&) = delete;
  5060. ~file_input_adapter() = default;
  5061. std::char_traits<char>::int_type get_character() noexcept
  5062. {
  5063. return std::fgetc(m_file);
  5064. }
  5065. private:
  5066. /// the file pointer to read from
  5067. std::FILE* m_file;
  5068. };
  5069. /*!
  5070. Input adapter for a (caching) istream. Ignores a UFT Byte Order Mark at
  5071. beginning of input. Does not support changing the underlying std::streambuf
  5072. in mid-input. Maintains underlying std::istream and std::streambuf to support
  5073. subsequent use of standard std::istream operations to process any input
  5074. characters following those used in parsing the JSON input. Clears the
  5075. std::istream flags; any input errors (e.g., EOF) will be detected by the first
  5076. subsequent call for input from the std::istream.
  5077. */
  5078. class input_stream_adapter
  5079. {
  5080. public:
  5081. using char_type = char;
  5082. ~input_stream_adapter()
  5083. {
  5084. // clear stream flags; we use underlying streambuf I/O, do not
  5085. // maintain ifstream flags, except eof
  5086. if (is != nullptr)
  5087. {
  5088. is->clear(is->rdstate() & std::ios::eofbit);
  5089. }
  5090. }
  5091. explicit input_stream_adapter(std::istream& i)
  5092. : is(&i), sb(i.rdbuf())
  5093. {}
  5094. // delete because of pointer members
  5095. input_stream_adapter(const input_stream_adapter&) = delete;
  5096. input_stream_adapter& operator=(input_stream_adapter&) = delete;
  5097. input_stream_adapter& operator=(input_stream_adapter&&) = delete;
  5098. input_stream_adapter(input_stream_adapter&& rhs) noexcept
  5099. : is(rhs.is), sb(rhs.sb)
  5100. {
  5101. rhs.is = nullptr;
  5102. rhs.sb = nullptr;
  5103. }
  5104. // std::istream/std::streambuf use std::char_traits<char>::to_int_type, to
  5105. // ensure that std::char_traits<char>::eof() and the character 0xFF do not
  5106. // end up as the same value, e.g. 0xFFFFFFFF.
  5107. std::char_traits<char>::int_type get_character()
  5108. {
  5109. auto res = sb->sbumpc();
  5110. // set eof manually, as we don't use the istream interface.
  5111. if (JSON_HEDLEY_UNLIKELY(res == std::char_traits<char>::eof()))
  5112. {
  5113. is->clear(is->rdstate() | std::ios::eofbit);
  5114. }
  5115. return res;
  5116. }
  5117. private:
  5118. /// the associated input stream
  5119. std::istream* is = nullptr;
  5120. std::streambuf* sb = nullptr;
  5121. };
  5122. #endif // JSON_NO_IO
  5123. // General-purpose iterator-based adapter. It might not be as fast as
  5124. // theoretically possible for some containers, but it is extremely versatile.
  5125. template<typename IteratorType>
  5126. class iterator_input_adapter
  5127. {
  5128. public:
  5129. using char_type = typename std::iterator_traits<IteratorType>::value_type;
  5130. iterator_input_adapter(IteratorType first, IteratorType last)
  5131. : current(std::move(first)), end(std::move(last))
  5132. {}
  5133. typename std::char_traits<char_type>::int_type get_character()
  5134. {
  5135. if (JSON_HEDLEY_LIKELY(current != end))
  5136. {
  5137. auto result = std::char_traits<char_type>::to_int_type(*current);
  5138. std::advance(current, 1);
  5139. return result;
  5140. }
  5141. return std::char_traits<char_type>::eof();
  5142. }
  5143. private:
  5144. IteratorType current;
  5145. IteratorType end;
  5146. template<typename BaseInputAdapter, size_t T>
  5147. friend struct wide_string_input_helper;
  5148. bool empty() const
  5149. {
  5150. return current == end;
  5151. }
  5152. };
  5153. template<typename BaseInputAdapter, size_t T>
  5154. struct wide_string_input_helper;
  5155. template<typename BaseInputAdapter>
  5156. struct wide_string_input_helper<BaseInputAdapter, 4>
  5157. {
  5158. // UTF-32
  5159. static void fill_buffer(BaseInputAdapter& input,
  5160. std::array<std::char_traits<char>::int_type, 4>& utf8_bytes,
  5161. size_t& utf8_bytes_index,
  5162. size_t& utf8_bytes_filled)
  5163. {
  5164. utf8_bytes_index = 0;
  5165. if (JSON_HEDLEY_UNLIKELY(input.empty()))
  5166. {
  5167. utf8_bytes[0] = std::char_traits<char>::eof();
  5168. utf8_bytes_filled = 1;
  5169. }
  5170. else
  5171. {
  5172. // get the current character
  5173. const auto wc = input.get_character();
  5174. // UTF-32 to UTF-8 encoding
  5175. if (wc < 0x80)
  5176. {
  5177. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  5178. utf8_bytes_filled = 1;
  5179. }
  5180. else if (wc <= 0x7FF)
  5181. {
  5182. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xC0u | ((static_cast<unsigned int>(wc) >> 6u) & 0x1Fu));
  5183. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5184. utf8_bytes_filled = 2;
  5185. }
  5186. else if (wc <= 0xFFFF)
  5187. {
  5188. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xE0u | ((static_cast<unsigned int>(wc) >> 12u) & 0x0Fu));
  5189. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
  5190. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5191. utf8_bytes_filled = 3;
  5192. }
  5193. else if (wc <= 0x10FFFF)
  5194. {
  5195. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xF0u | ((static_cast<unsigned int>(wc) >> 18u) & 0x07u));
  5196. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 12u) & 0x3Fu));
  5197. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
  5198. utf8_bytes[3] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5199. utf8_bytes_filled = 4;
  5200. }
  5201. else
  5202. {
  5203. // unknown character
  5204. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  5205. utf8_bytes_filled = 1;
  5206. }
  5207. }
  5208. }
  5209. };
  5210. template<typename BaseInputAdapter>
  5211. struct wide_string_input_helper<BaseInputAdapter, 2>
  5212. {
  5213. // UTF-16
  5214. static void fill_buffer(BaseInputAdapter& input,
  5215. std::array<std::char_traits<char>::int_type, 4>& utf8_bytes,
  5216. size_t& utf8_bytes_index,
  5217. size_t& utf8_bytes_filled)
  5218. {
  5219. utf8_bytes_index = 0;
  5220. if (JSON_HEDLEY_UNLIKELY(input.empty()))
  5221. {
  5222. utf8_bytes[0] = std::char_traits<char>::eof();
  5223. utf8_bytes_filled = 1;
  5224. }
  5225. else
  5226. {
  5227. // get the current character
  5228. const auto wc = input.get_character();
  5229. // UTF-16 to UTF-8 encoding
  5230. if (wc < 0x80)
  5231. {
  5232. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  5233. utf8_bytes_filled = 1;
  5234. }
  5235. else if (wc <= 0x7FF)
  5236. {
  5237. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xC0u | ((static_cast<unsigned int>(wc) >> 6u)));
  5238. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5239. utf8_bytes_filled = 2;
  5240. }
  5241. else if (0xD800 > wc || wc >= 0xE000)
  5242. {
  5243. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xE0u | ((static_cast<unsigned int>(wc) >> 12u)));
  5244. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
  5245. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  5246. utf8_bytes_filled = 3;
  5247. }
  5248. else
  5249. {
  5250. if (JSON_HEDLEY_UNLIKELY(!input.empty()))
  5251. {
  5252. const auto wc2 = static_cast<unsigned int>(input.get_character());
  5253. const auto charcode = 0x10000u + (((static_cast<unsigned int>(wc) & 0x3FFu) << 10u) | (wc2 & 0x3FFu));
  5254. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xF0u | (charcode >> 18u));
  5255. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((charcode >> 12u) & 0x3Fu));
  5256. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | ((charcode >> 6u) & 0x3Fu));
  5257. utf8_bytes[3] = static_cast<std::char_traits<char>::int_type>(0x80u | (charcode & 0x3Fu));
  5258. utf8_bytes_filled = 4;
  5259. }
  5260. else
  5261. {
  5262. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  5263. utf8_bytes_filled = 1;
  5264. }
  5265. }
  5266. }
  5267. }
  5268. };
  5269. // Wraps another input apdater to convert wide character types into individual bytes.
  5270. template<typename BaseInputAdapter, typename WideCharType>
  5271. class wide_string_input_adapter
  5272. {
  5273. public:
  5274. using char_type = char;
  5275. wide_string_input_adapter(BaseInputAdapter base)
  5276. : base_adapter(base) {}
  5277. typename std::char_traits<char>::int_type get_character() noexcept
  5278. {
  5279. // check if buffer needs to be filled
  5280. if (utf8_bytes_index == utf8_bytes_filled)
  5281. {
  5282. fill_buffer<sizeof(WideCharType)>();
  5283. JSON_ASSERT(utf8_bytes_filled > 0);
  5284. JSON_ASSERT(utf8_bytes_index == 0);
  5285. }
  5286. // use buffer
  5287. JSON_ASSERT(utf8_bytes_filled > 0);
  5288. JSON_ASSERT(utf8_bytes_index < utf8_bytes_filled);
  5289. return utf8_bytes[utf8_bytes_index++];
  5290. }
  5291. private:
  5292. BaseInputAdapter base_adapter;
  5293. template<size_t T>
  5294. void fill_buffer()
  5295. {
  5296. wide_string_input_helper<BaseInputAdapter, T>::fill_buffer(base_adapter, utf8_bytes, utf8_bytes_index, utf8_bytes_filled);
  5297. }
  5298. /// a buffer for UTF-8 bytes
  5299. std::array<std::char_traits<char>::int_type, 4> utf8_bytes = {{0, 0, 0, 0}};
  5300. /// index to the utf8_codes array for the next valid byte
  5301. std::size_t utf8_bytes_index = 0;
  5302. /// number of valid bytes in the utf8_codes array
  5303. std::size_t utf8_bytes_filled = 0;
  5304. };
  5305. template<typename IteratorType, typename Enable = void>
  5306. struct iterator_input_adapter_factory
  5307. {
  5308. using iterator_type = IteratorType;
  5309. using char_type = typename std::iterator_traits<iterator_type>::value_type;
  5310. using adapter_type = iterator_input_adapter<iterator_type>;
  5311. static adapter_type create(IteratorType first, IteratorType last)
  5312. {
  5313. return adapter_type(std::move(first), std::move(last));
  5314. }
  5315. };
  5316. template<typename T>
  5317. struct is_iterator_of_multibyte
  5318. {
  5319. using value_type = typename std::iterator_traits<T>::value_type;
  5320. enum
  5321. {
  5322. value = sizeof(value_type) > 1
  5323. };
  5324. };
  5325. template<typename IteratorType>
  5326. struct iterator_input_adapter_factory<IteratorType, enable_if_t<is_iterator_of_multibyte<IteratorType>::value>>
  5327. {
  5328. using iterator_type = IteratorType;
  5329. using char_type = typename std::iterator_traits<iterator_type>::value_type;
  5330. using base_adapter_type = iterator_input_adapter<iterator_type>;
  5331. using adapter_type = wide_string_input_adapter<base_adapter_type, char_type>;
  5332. static adapter_type create(IteratorType first, IteratorType last)
  5333. {
  5334. return adapter_type(base_adapter_type(std::move(first), std::move(last)));
  5335. }
  5336. };
  5337. // General purpose iterator-based input
  5338. template<typename IteratorType>
  5339. typename iterator_input_adapter_factory<IteratorType>::adapter_type input_adapter(IteratorType first, IteratorType last)
  5340. {
  5341. using factory_type = iterator_input_adapter_factory<IteratorType>;
  5342. return factory_type::create(first, last);
  5343. }
  5344. // Convenience shorthand from container to iterator
  5345. // Enables ADL on begin(container) and end(container)
  5346. // Encloses the using declarations in namespace for not to leak them to outside scope
  5347. namespace container_input_adapter_factory_impl
  5348. {
  5349. using std::begin;
  5350. using std::end;
  5351. template<typename ContainerType, typename Enable = void>
  5352. struct container_input_adapter_factory {};
  5353. template<typename ContainerType>
  5354. struct container_input_adapter_factory< ContainerType,
  5355. void_t<decltype(begin(std::declval<ContainerType>()), end(std::declval<ContainerType>()))>>
  5356. {
  5357. using adapter_type = decltype(input_adapter(begin(std::declval<ContainerType>()), end(std::declval<ContainerType>())));
  5358. static adapter_type create(const ContainerType& container)
  5359. {
  5360. return input_adapter(begin(container), end(container));
  5361. }
  5362. };
  5363. } // namespace container_input_adapter_factory_impl
  5364. template<typename ContainerType>
  5365. typename container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::adapter_type input_adapter(const ContainerType& container)
  5366. {
  5367. return container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::create(container);
  5368. }
  5369. #ifndef JSON_NO_IO
  5370. // Special cases with fast paths
  5371. inline file_input_adapter input_adapter(std::FILE* file)
  5372. {
  5373. return file_input_adapter(file);
  5374. }
  5375. inline input_stream_adapter input_adapter(std::istream& stream)
  5376. {
  5377. return input_stream_adapter(stream);
  5378. }
  5379. inline input_stream_adapter input_adapter(std::istream&& stream)
  5380. {
  5381. return input_stream_adapter(stream);
  5382. }
  5383. #endif // JSON_NO_IO
  5384. using contiguous_bytes_input_adapter = decltype(input_adapter(std::declval<const char*>(), std::declval<const char*>()));
  5385. // Null-delimited strings, and the like.
  5386. template < typename CharT,
  5387. typename std::enable_if <
  5388. std::is_pointer<CharT>::value&&
  5389. !std::is_array<CharT>::value&&
  5390. std::is_integral<typename std::remove_pointer<CharT>::type>::value&&
  5391. sizeof(typename std::remove_pointer<CharT>::type) == 1,
  5392. int >::type = 0 >
  5393. contiguous_bytes_input_adapter input_adapter(CharT b)
  5394. {
  5395. auto length = std::strlen(reinterpret_cast<const char*>(b));
  5396. const auto* ptr = reinterpret_cast<const char*>(b);
  5397. return input_adapter(ptr, ptr + length);
  5398. }
  5399. template<typename T, std::size_t N>
  5400. 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)
  5401. {
  5402. return input_adapter(array, array + N);
  5403. }
  5404. // This class only handles inputs of input_buffer_adapter type.
  5405. // It's required so that expressions like {ptr, len} can be implicitly cast
  5406. // to the correct adapter.
  5407. class span_input_adapter
  5408. {
  5409. public:
  5410. template < typename CharT,
  5411. typename std::enable_if <
  5412. std::is_pointer<CharT>::value&&
  5413. std::is_integral<typename std::remove_pointer<CharT>::type>::value&&
  5414. sizeof(typename std::remove_pointer<CharT>::type) == 1,
  5415. int >::type = 0 >
  5416. span_input_adapter(CharT b, std::size_t l)
  5417. : ia(reinterpret_cast<const char*>(b), reinterpret_cast<const char*>(b) + l) {}
  5418. template<class IteratorType,
  5419. typename std::enable_if<
  5420. std::is_same<typename iterator_traits<IteratorType>::iterator_category, std::random_access_iterator_tag>::value,
  5421. int>::type = 0>
  5422. span_input_adapter(IteratorType first, IteratorType last)
  5423. : ia(input_adapter(first, last)) {}
  5424. contiguous_bytes_input_adapter&& get()
  5425. {
  5426. return std::move(ia); // NOLINT(hicpp-move-const-arg,performance-move-const-arg)
  5427. }
  5428. private:
  5429. contiguous_bytes_input_adapter ia;
  5430. };
  5431. } // namespace detail
  5432. } // namespace nlohmann
  5433. // #include <nlohmann/detail/input/json_sax.hpp>
  5434. #include <cstddef>
  5435. #include <string> // string
  5436. #include <utility> // move
  5437. #include <vector> // vector
  5438. // #include <nlohmann/detail/exceptions.hpp>
  5439. // #include <nlohmann/detail/macro_scope.hpp>
  5440. // #include <nlohmann/detail/string_concat.hpp>
  5441. namespace nlohmann
  5442. {
  5443. /*!
  5444. @brief SAX interface
  5445. This class describes the SAX interface used by @ref nlohmann::json::sax_parse.
  5446. Each function is called in different situations while the input is parsed. The
  5447. boolean return value informs the parser whether to continue processing the
  5448. input.
  5449. */
  5450. template<typename BasicJsonType>
  5451. struct json_sax
  5452. {
  5453. using number_integer_t = typename BasicJsonType::number_integer_t;
  5454. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  5455. using number_float_t = typename BasicJsonType::number_float_t;
  5456. using string_t = typename BasicJsonType::string_t;
  5457. using binary_t = typename BasicJsonType::binary_t;
  5458. /*!
  5459. @brief a null value was read
  5460. @return whether parsing should proceed
  5461. */
  5462. virtual bool null() = 0;
  5463. /*!
  5464. @brief a boolean value was read
  5465. @param[in] val boolean value
  5466. @return whether parsing should proceed
  5467. */
  5468. virtual bool boolean(bool val) = 0;
  5469. /*!
  5470. @brief an integer number was read
  5471. @param[in] val integer value
  5472. @return whether parsing should proceed
  5473. */
  5474. virtual bool number_integer(number_integer_t val) = 0;
  5475. /*!
  5476. @brief an unsigned integer number was read
  5477. @param[in] val unsigned integer value
  5478. @return whether parsing should proceed
  5479. */
  5480. virtual bool number_unsigned(number_unsigned_t val) = 0;
  5481. /*!
  5482. @brief a floating-point number was read
  5483. @param[in] val floating-point value
  5484. @param[in] s raw token value
  5485. @return whether parsing should proceed
  5486. */
  5487. virtual bool number_float(number_float_t val, const string_t& s) = 0;
  5488. /*!
  5489. @brief a string value was read
  5490. @param[in] val string value
  5491. @return whether parsing should proceed
  5492. @note It is safe to move the passed string value.
  5493. */
  5494. virtual bool string(string_t& val) = 0;
  5495. /*!
  5496. @brief a binary value was read
  5497. @param[in] val binary value
  5498. @return whether parsing should proceed
  5499. @note It is safe to move the passed binary value.
  5500. */
  5501. virtual bool binary(binary_t& val) = 0;
  5502. /*!
  5503. @brief the beginning of an object was read
  5504. @param[in] elements number of object elements or -1 if unknown
  5505. @return whether parsing should proceed
  5506. @note binary formats may report the number of elements
  5507. */
  5508. virtual bool start_object(std::size_t elements) = 0;
  5509. /*!
  5510. @brief an object key was read
  5511. @param[in] val object key
  5512. @return whether parsing should proceed
  5513. @note It is safe to move the passed string.
  5514. */
  5515. virtual bool key(string_t& val) = 0;
  5516. /*!
  5517. @brief the end of an object was read
  5518. @return whether parsing should proceed
  5519. */
  5520. virtual bool end_object() = 0;
  5521. /*!
  5522. @brief the beginning of an array was read
  5523. @param[in] elements number of array elements or -1 if unknown
  5524. @return whether parsing should proceed
  5525. @note binary formats may report the number of elements
  5526. */
  5527. virtual bool start_array(std::size_t elements) = 0;
  5528. /*!
  5529. @brief the end of an array was read
  5530. @return whether parsing should proceed
  5531. */
  5532. virtual bool end_array() = 0;
  5533. /*!
  5534. @brief a parse error occurred
  5535. @param[in] position the position in the input where the error occurs
  5536. @param[in] last_token the last read token
  5537. @param[in] ex an exception object describing the error
  5538. @return whether parsing should proceed (must return false)
  5539. */
  5540. virtual bool parse_error(std::size_t position,
  5541. const std::string& last_token,
  5542. const detail::exception& ex) = 0;
  5543. json_sax() = default;
  5544. json_sax(const json_sax&) = default;
  5545. json_sax(json_sax&&) noexcept = default;
  5546. json_sax& operator=(const json_sax&) = default;
  5547. json_sax& operator=(json_sax&&) noexcept = default;
  5548. virtual ~json_sax() = default;
  5549. };
  5550. namespace detail
  5551. {
  5552. /*!
  5553. @brief SAX implementation to create a JSON value from SAX events
  5554. This class implements the @ref json_sax interface and processes the SAX events
  5555. to create a JSON value which makes it basically a DOM parser. The structure or
  5556. hierarchy of the JSON value is managed by the stack `ref_stack` which contains
  5557. a pointer to the respective array or object for each recursion depth.
  5558. After successful parsing, the value that is passed by reference to the
  5559. constructor contains the parsed value.
  5560. @tparam BasicJsonType the JSON type
  5561. */
  5562. template<typename BasicJsonType>
  5563. class json_sax_dom_parser
  5564. {
  5565. public:
  5566. using number_integer_t = typename BasicJsonType::number_integer_t;
  5567. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  5568. using number_float_t = typename BasicJsonType::number_float_t;
  5569. using string_t = typename BasicJsonType::string_t;
  5570. using binary_t = typename BasicJsonType::binary_t;
  5571. /*!
  5572. @param[in,out] r reference to a JSON value that is manipulated while
  5573. parsing
  5574. @param[in] allow_exceptions_ whether parse errors yield exceptions
  5575. */
  5576. explicit json_sax_dom_parser(BasicJsonType& r, const bool allow_exceptions_ = true)
  5577. : root(r), allow_exceptions(allow_exceptions_)
  5578. {}
  5579. // make class move-only
  5580. json_sax_dom_parser(const json_sax_dom_parser&) = delete;
  5581. json_sax_dom_parser(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  5582. json_sax_dom_parser& operator=(const json_sax_dom_parser&) = delete;
  5583. json_sax_dom_parser& operator=(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  5584. ~json_sax_dom_parser() = default;
  5585. bool null()
  5586. {
  5587. handle_value(nullptr);
  5588. return true;
  5589. }
  5590. bool boolean(bool val)
  5591. {
  5592. handle_value(val);
  5593. return true;
  5594. }
  5595. bool number_integer(number_integer_t val)
  5596. {
  5597. handle_value(val);
  5598. return true;
  5599. }
  5600. bool number_unsigned(number_unsigned_t val)
  5601. {
  5602. handle_value(val);
  5603. return true;
  5604. }
  5605. bool number_float(number_float_t val, const string_t& /*unused*/)
  5606. {
  5607. handle_value(val);
  5608. return true;
  5609. }
  5610. bool string(string_t& val)
  5611. {
  5612. handle_value(val);
  5613. return true;
  5614. }
  5615. bool binary(binary_t& val)
  5616. {
  5617. handle_value(std::move(val));
  5618. return true;
  5619. }
  5620. bool start_object(std::size_t len)
  5621. {
  5622. ref_stack.push_back(handle_value(BasicJsonType::value_t::object));
  5623. if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
  5624. {
  5625. JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
  5626. }
  5627. return true;
  5628. }
  5629. bool key(string_t& val)
  5630. {
  5631. JSON_ASSERT(!ref_stack.empty());
  5632. JSON_ASSERT(ref_stack.back()->is_object());
  5633. // add null at given key and store the reference for later
  5634. object_element = &(ref_stack.back()->m_value.object->operator[](val));
  5635. return true;
  5636. }
  5637. bool end_object()
  5638. {
  5639. JSON_ASSERT(!ref_stack.empty());
  5640. JSON_ASSERT(ref_stack.back()->is_object());
  5641. ref_stack.back()->set_parents();
  5642. ref_stack.pop_back();
  5643. return true;
  5644. }
  5645. bool start_array(std::size_t len)
  5646. {
  5647. ref_stack.push_back(handle_value(BasicJsonType::value_t::array));
  5648. if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
  5649. {
  5650. JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
  5651. }
  5652. return true;
  5653. }
  5654. bool end_array()
  5655. {
  5656. JSON_ASSERT(!ref_stack.empty());
  5657. JSON_ASSERT(ref_stack.back()->is_array());
  5658. ref_stack.back()->set_parents();
  5659. ref_stack.pop_back();
  5660. return true;
  5661. }
  5662. template<class Exception>
  5663. bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
  5664. const Exception& ex)
  5665. {
  5666. errored = true;
  5667. static_cast<void>(ex);
  5668. if (allow_exceptions)
  5669. {
  5670. JSON_THROW(ex);
  5671. }
  5672. return false;
  5673. }
  5674. constexpr bool is_errored() const
  5675. {
  5676. return errored;
  5677. }
  5678. private:
  5679. /*!
  5680. @invariant If the ref stack is empty, then the passed value will be the new
  5681. root.
  5682. @invariant If the ref stack contains a value, then it is an array or an
  5683. object to which we can add elements
  5684. */
  5685. template<typename Value>
  5686. JSON_HEDLEY_RETURNS_NON_NULL
  5687. BasicJsonType* handle_value(Value&& v)
  5688. {
  5689. if (ref_stack.empty())
  5690. {
  5691. root = BasicJsonType(std::forward<Value>(v));
  5692. return &root;
  5693. }
  5694. JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
  5695. if (ref_stack.back()->is_array())
  5696. {
  5697. ref_stack.back()->m_value.array->emplace_back(std::forward<Value>(v));
  5698. return &(ref_stack.back()->m_value.array->back());
  5699. }
  5700. JSON_ASSERT(ref_stack.back()->is_object());
  5701. JSON_ASSERT(object_element);
  5702. *object_element = BasicJsonType(std::forward<Value>(v));
  5703. return object_element;
  5704. }
  5705. /// the parsed JSON value
  5706. BasicJsonType& root;
  5707. /// stack to model hierarchy of values
  5708. std::vector<BasicJsonType*> ref_stack {};
  5709. /// helper to hold the reference for the next object element
  5710. BasicJsonType* object_element = nullptr;
  5711. /// whether a syntax error occurred
  5712. bool errored = false;
  5713. /// whether to throw exceptions in case of errors
  5714. const bool allow_exceptions = true;
  5715. };
  5716. template<typename BasicJsonType>
  5717. class json_sax_dom_callback_parser
  5718. {
  5719. public:
  5720. using number_integer_t = typename BasicJsonType::number_integer_t;
  5721. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  5722. using number_float_t = typename BasicJsonType::number_float_t;
  5723. using string_t = typename BasicJsonType::string_t;
  5724. using binary_t = typename BasicJsonType::binary_t;
  5725. using parser_callback_t = typename BasicJsonType::parser_callback_t;
  5726. using parse_event_t = typename BasicJsonType::parse_event_t;
  5727. json_sax_dom_callback_parser(BasicJsonType& r,
  5728. const parser_callback_t cb,
  5729. const bool allow_exceptions_ = true)
  5730. : root(r), callback(cb), allow_exceptions(allow_exceptions_)
  5731. {
  5732. keep_stack.push_back(true);
  5733. }
  5734. // make class move-only
  5735. json_sax_dom_callback_parser(const json_sax_dom_callback_parser&) = delete;
  5736. json_sax_dom_callback_parser(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  5737. json_sax_dom_callback_parser& operator=(const json_sax_dom_callback_parser&) = delete;
  5738. json_sax_dom_callback_parser& operator=(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  5739. ~json_sax_dom_callback_parser() = default;
  5740. bool null()
  5741. {
  5742. handle_value(nullptr);
  5743. return true;
  5744. }
  5745. bool boolean(bool val)
  5746. {
  5747. handle_value(val);
  5748. return true;
  5749. }
  5750. bool number_integer(number_integer_t val)
  5751. {
  5752. handle_value(val);
  5753. return true;
  5754. }
  5755. bool number_unsigned(number_unsigned_t val)
  5756. {
  5757. handle_value(val);
  5758. return true;
  5759. }
  5760. bool number_float(number_float_t val, const string_t& /*unused*/)
  5761. {
  5762. handle_value(val);
  5763. return true;
  5764. }
  5765. bool string(string_t& val)
  5766. {
  5767. handle_value(val);
  5768. return true;
  5769. }
  5770. bool binary(binary_t& val)
  5771. {
  5772. handle_value(std::move(val));
  5773. return true;
  5774. }
  5775. bool start_object(std::size_t len)
  5776. {
  5777. // check callback for object start
  5778. const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::object_start, discarded);
  5779. keep_stack.push_back(keep);
  5780. auto val = handle_value(BasicJsonType::value_t::object, true);
  5781. ref_stack.push_back(val.second);
  5782. // check object limit
  5783. if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
  5784. {
  5785. JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
  5786. }
  5787. return true;
  5788. }
  5789. bool key(string_t& val)
  5790. {
  5791. BasicJsonType k = BasicJsonType(val);
  5792. // check callback for key
  5793. const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::key, k);
  5794. key_keep_stack.push_back(keep);
  5795. // add discarded value at given key and store the reference for later
  5796. if (keep && ref_stack.back())
  5797. {
  5798. object_element = &(ref_stack.back()->m_value.object->operator[](val) = discarded);
  5799. }
  5800. return true;
  5801. }
  5802. bool end_object()
  5803. {
  5804. if (ref_stack.back())
  5805. {
  5806. if (!callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back()))
  5807. {
  5808. // discard object
  5809. *ref_stack.back() = discarded;
  5810. }
  5811. else
  5812. {
  5813. ref_stack.back()->set_parents();
  5814. }
  5815. }
  5816. JSON_ASSERT(!ref_stack.empty());
  5817. JSON_ASSERT(!keep_stack.empty());
  5818. ref_stack.pop_back();
  5819. keep_stack.pop_back();
  5820. if (!ref_stack.empty() && ref_stack.back() && ref_stack.back()->is_structured())
  5821. {
  5822. // remove discarded value
  5823. for (auto it = ref_stack.back()->begin(); it != ref_stack.back()->end(); ++it)
  5824. {
  5825. if (it->is_discarded())
  5826. {
  5827. ref_stack.back()->erase(it);
  5828. break;
  5829. }
  5830. }
  5831. }
  5832. return true;
  5833. }
  5834. bool start_array(std::size_t len)
  5835. {
  5836. const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::array_start, discarded);
  5837. keep_stack.push_back(keep);
  5838. auto val = handle_value(BasicJsonType::value_t::array, true);
  5839. ref_stack.push_back(val.second);
  5840. // check array limit
  5841. if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
  5842. {
  5843. JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
  5844. }
  5845. return true;
  5846. }
  5847. bool end_array()
  5848. {
  5849. bool keep = true;
  5850. if (ref_stack.back())
  5851. {
  5852. keep = callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::array_end, *ref_stack.back());
  5853. if (keep)
  5854. {
  5855. ref_stack.back()->set_parents();
  5856. }
  5857. else
  5858. {
  5859. // discard array
  5860. *ref_stack.back() = discarded;
  5861. }
  5862. }
  5863. JSON_ASSERT(!ref_stack.empty());
  5864. JSON_ASSERT(!keep_stack.empty());
  5865. ref_stack.pop_back();
  5866. keep_stack.pop_back();
  5867. // remove discarded value
  5868. if (!keep && !ref_stack.empty() && ref_stack.back()->is_array())
  5869. {
  5870. ref_stack.back()->m_value.array->pop_back();
  5871. }
  5872. return true;
  5873. }
  5874. template<class Exception>
  5875. bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
  5876. const Exception& ex)
  5877. {
  5878. errored = true;
  5879. static_cast<void>(ex);
  5880. if (allow_exceptions)
  5881. {
  5882. JSON_THROW(ex);
  5883. }
  5884. return false;
  5885. }
  5886. constexpr bool is_errored() const
  5887. {
  5888. return errored;
  5889. }
  5890. private:
  5891. /*!
  5892. @param[in] v value to add to the JSON value we build during parsing
  5893. @param[in] skip_callback whether we should skip calling the callback
  5894. function; this is required after start_array() and
  5895. start_object() SAX events, because otherwise we would call the
  5896. callback function with an empty array or object, respectively.
  5897. @invariant If the ref stack is empty, then the passed value will be the new
  5898. root.
  5899. @invariant If the ref stack contains a value, then it is an array or an
  5900. object to which we can add elements
  5901. @return pair of boolean (whether value should be kept) and pointer (to the
  5902. passed value in the ref_stack hierarchy; nullptr if not kept)
  5903. */
  5904. template<typename Value>
  5905. std::pair<bool, BasicJsonType*> handle_value(Value&& v, const bool skip_callback = false)
  5906. {
  5907. JSON_ASSERT(!keep_stack.empty());
  5908. // do not handle this value if we know it would be added to a discarded
  5909. // container
  5910. if (!keep_stack.back())
  5911. {
  5912. return {false, nullptr};
  5913. }
  5914. // create value
  5915. auto value = BasicJsonType(std::forward<Value>(v));
  5916. // check callback
  5917. const bool keep = skip_callback || callback(static_cast<int>(ref_stack.size()), parse_event_t::value, value);
  5918. // do not handle this value if we just learnt it shall be discarded
  5919. if (!keep)
  5920. {
  5921. return {false, nullptr};
  5922. }
  5923. if (ref_stack.empty())
  5924. {
  5925. root = std::move(value);
  5926. return {true, &root};
  5927. }
  5928. // skip this value if we already decided to skip the parent
  5929. // (https://github.com/nlohmann/json/issues/971#issuecomment-413678360)
  5930. if (!ref_stack.back())
  5931. {
  5932. return {false, nullptr};
  5933. }
  5934. // we now only expect arrays and objects
  5935. JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
  5936. // array
  5937. if (ref_stack.back()->is_array())
  5938. {
  5939. ref_stack.back()->m_value.array->emplace_back(std::move(value));
  5940. return {true, &(ref_stack.back()->m_value.array->back())};
  5941. }
  5942. // object
  5943. JSON_ASSERT(ref_stack.back()->is_object());
  5944. // check if we should store an element for the current key
  5945. JSON_ASSERT(!key_keep_stack.empty());
  5946. const bool store_element = key_keep_stack.back();
  5947. key_keep_stack.pop_back();
  5948. if (!store_element)
  5949. {
  5950. return {false, nullptr};
  5951. }
  5952. JSON_ASSERT(object_element);
  5953. *object_element = std::move(value);
  5954. return {true, object_element};
  5955. }
  5956. /// the parsed JSON value
  5957. BasicJsonType& root;
  5958. /// stack to model hierarchy of values
  5959. std::vector<BasicJsonType*> ref_stack {};
  5960. /// stack to manage which values to keep
  5961. std::vector<bool> keep_stack {};
  5962. /// stack to manage which object keys to keep
  5963. std::vector<bool> key_keep_stack {};
  5964. /// helper to hold the reference for the next object element
  5965. BasicJsonType* object_element = nullptr;
  5966. /// whether a syntax error occurred
  5967. bool errored = false;
  5968. /// callback function
  5969. const parser_callback_t callback = nullptr;
  5970. /// whether to throw exceptions in case of errors
  5971. const bool allow_exceptions = true;
  5972. /// a discarded value for the callback
  5973. BasicJsonType discarded = BasicJsonType::value_t::discarded;
  5974. };
  5975. template<typename BasicJsonType>
  5976. class json_sax_acceptor
  5977. {
  5978. public:
  5979. using number_integer_t = typename BasicJsonType::number_integer_t;
  5980. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  5981. using number_float_t = typename BasicJsonType::number_float_t;
  5982. using string_t = typename BasicJsonType::string_t;
  5983. using binary_t = typename BasicJsonType::binary_t;
  5984. bool null()
  5985. {
  5986. return true;
  5987. }
  5988. bool boolean(bool /*unused*/)
  5989. {
  5990. return true;
  5991. }
  5992. bool number_integer(number_integer_t /*unused*/)
  5993. {
  5994. return true;
  5995. }
  5996. bool number_unsigned(number_unsigned_t /*unused*/)
  5997. {
  5998. return true;
  5999. }
  6000. bool number_float(number_float_t /*unused*/, const string_t& /*unused*/)
  6001. {
  6002. return true;
  6003. }
  6004. bool string(string_t& /*unused*/)
  6005. {
  6006. return true;
  6007. }
  6008. bool binary(binary_t& /*unused*/)
  6009. {
  6010. return true;
  6011. }
  6012. bool start_object(std::size_t /*unused*/ = static_cast<std::size_t>(-1))
  6013. {
  6014. return true;
  6015. }
  6016. bool key(string_t& /*unused*/)
  6017. {
  6018. return true;
  6019. }
  6020. bool end_object()
  6021. {
  6022. return true;
  6023. }
  6024. bool start_array(std::size_t /*unused*/ = static_cast<std::size_t>(-1))
  6025. {
  6026. return true;
  6027. }
  6028. bool end_array()
  6029. {
  6030. return true;
  6031. }
  6032. bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const detail::exception& /*unused*/)
  6033. {
  6034. return false;
  6035. }
  6036. };
  6037. } // namespace detail
  6038. } // namespace nlohmann
  6039. // #include <nlohmann/detail/input/lexer.hpp>
  6040. #include <array> // array
  6041. #include <clocale> // localeconv
  6042. #include <cstddef> // size_t
  6043. #include <cstdio> // snprintf
  6044. #include <cstdlib> // strtof, strtod, strtold, strtoll, strtoull
  6045. #include <initializer_list> // initializer_list
  6046. #include <string> // char_traits, string
  6047. #include <utility> // move
  6048. #include <vector> // vector
  6049. // #include <nlohmann/detail/input/input_adapters.hpp>
  6050. // #include <nlohmann/detail/input/position_t.hpp>
  6051. // #include <nlohmann/detail/macro_scope.hpp>
  6052. namespace nlohmann
  6053. {
  6054. namespace detail
  6055. {
  6056. ///////////
  6057. // lexer //
  6058. ///////////
  6059. template<typename BasicJsonType>
  6060. class lexer_base
  6061. {
  6062. public:
  6063. /// token types for the parser
  6064. enum class token_type
  6065. {
  6066. uninitialized, ///< indicating the scanner is uninitialized
  6067. literal_true, ///< the `true` literal
  6068. literal_false, ///< the `false` literal
  6069. literal_null, ///< the `null` literal
  6070. value_string, ///< a string -- use get_string() for actual value
  6071. value_unsigned, ///< an unsigned integer -- use get_number_unsigned() for actual value
  6072. value_integer, ///< a signed integer -- use get_number_integer() for actual value
  6073. value_float, ///< an floating point number -- use get_number_float() for actual value
  6074. begin_array, ///< the character for array begin `[`
  6075. begin_object, ///< the character for object begin `{`
  6076. end_array, ///< the character for array end `]`
  6077. end_object, ///< the character for object end `}`
  6078. name_separator, ///< the name separator `:`
  6079. value_separator, ///< the value separator `,`
  6080. parse_error, ///< indicating a parse error
  6081. end_of_input, ///< indicating the end of the input buffer
  6082. literal_or_value ///< a literal or the begin of a value (only for diagnostics)
  6083. };
  6084. /// return name of values of type token_type (only used for errors)
  6085. JSON_HEDLEY_RETURNS_NON_NULL
  6086. JSON_HEDLEY_CONST
  6087. static const char* token_type_name(const token_type t) noexcept
  6088. {
  6089. switch (t)
  6090. {
  6091. case token_type::uninitialized:
  6092. return "<uninitialized>";
  6093. case token_type::literal_true:
  6094. return "true literal";
  6095. case token_type::literal_false:
  6096. return "false literal";
  6097. case token_type::literal_null:
  6098. return "null literal";
  6099. case token_type::value_string:
  6100. return "string literal";
  6101. case token_type::value_unsigned:
  6102. case token_type::value_integer:
  6103. case token_type::value_float:
  6104. return "number literal";
  6105. case token_type::begin_array:
  6106. return "'['";
  6107. case token_type::begin_object:
  6108. return "'{'";
  6109. case token_type::end_array:
  6110. return "']'";
  6111. case token_type::end_object:
  6112. return "'}'";
  6113. case token_type::name_separator:
  6114. return "':'";
  6115. case token_type::value_separator:
  6116. return "','";
  6117. case token_type::parse_error:
  6118. return "<parse error>";
  6119. case token_type::end_of_input:
  6120. return "end of input";
  6121. case token_type::literal_or_value:
  6122. return "'[', '{', or a literal";
  6123. // LCOV_EXCL_START
  6124. default: // catch non-enum values
  6125. return "unknown token";
  6126. // LCOV_EXCL_STOP
  6127. }
  6128. }
  6129. };
  6130. /*!
  6131. @brief lexical analysis
  6132. This class organizes the lexical analysis during JSON deserialization.
  6133. */
  6134. template<typename BasicJsonType, typename InputAdapterType>
  6135. class lexer : public lexer_base<BasicJsonType>
  6136. {
  6137. using number_integer_t = typename BasicJsonType::number_integer_t;
  6138. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  6139. using number_float_t = typename BasicJsonType::number_float_t;
  6140. using string_t = typename BasicJsonType::string_t;
  6141. using char_type = typename InputAdapterType::char_type;
  6142. using char_int_type = typename std::char_traits<char_type>::int_type;
  6143. public:
  6144. using token_type = typename lexer_base<BasicJsonType>::token_type;
  6145. explicit lexer(InputAdapterType&& adapter, bool ignore_comments_ = false) noexcept
  6146. : ia(std::move(adapter))
  6147. , ignore_comments(ignore_comments_)
  6148. , decimal_point_char(static_cast<char_int_type>(get_decimal_point()))
  6149. {}
  6150. // delete because of pointer members
  6151. lexer(const lexer&) = delete;
  6152. lexer(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  6153. lexer& operator=(lexer&) = delete;
  6154. lexer& operator=(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  6155. ~lexer() = default;
  6156. private:
  6157. /////////////////////
  6158. // locales
  6159. /////////////////////
  6160. /// return the locale-dependent decimal point
  6161. JSON_HEDLEY_PURE
  6162. static char get_decimal_point() noexcept
  6163. {
  6164. const auto* loc = localeconv();
  6165. JSON_ASSERT(loc != nullptr);
  6166. return (loc->decimal_point == nullptr) ? '.' : *(loc->decimal_point);
  6167. }
  6168. /////////////////////
  6169. // scan functions
  6170. /////////////////////
  6171. /*!
  6172. @brief get codepoint from 4 hex characters following `\u`
  6173. For input "\u c1 c2 c3 c4" the codepoint is:
  6174. (c1 * 0x1000) + (c2 * 0x0100) + (c3 * 0x0010) + c4
  6175. = (c1 << 12) + (c2 << 8) + (c3 << 4) + (c4 << 0)
  6176. Furthermore, the possible characters '0'..'9', 'A'..'F', and 'a'..'f'
  6177. must be converted to the integers 0x0..0x9, 0xA..0xF, 0xA..0xF, resp. The
  6178. conversion is done by subtracting the offset (0x30, 0x37, and 0x57)
  6179. between the ASCII value of the character and the desired integer value.
  6180. @return codepoint (0x0000..0xFFFF) or -1 in case of an error (e.g. EOF or
  6181. non-hex character)
  6182. */
  6183. int get_codepoint()
  6184. {
  6185. // this function only makes sense after reading `\u`
  6186. JSON_ASSERT(current == 'u');
  6187. int codepoint = 0;
  6188. const auto factors = { 12u, 8u, 4u, 0u };
  6189. for (const auto factor : factors)
  6190. {
  6191. get();
  6192. if (current >= '0' && current <= '9')
  6193. {
  6194. codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x30u) << factor);
  6195. }
  6196. else if (current >= 'A' && current <= 'F')
  6197. {
  6198. codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x37u) << factor);
  6199. }
  6200. else if (current >= 'a' && current <= 'f')
  6201. {
  6202. codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x57u) << factor);
  6203. }
  6204. else
  6205. {
  6206. return -1;
  6207. }
  6208. }
  6209. JSON_ASSERT(0x0000 <= codepoint && codepoint <= 0xFFFF);
  6210. return codepoint;
  6211. }
  6212. /*!
  6213. @brief check if the next byte(s) are inside a given range
  6214. Adds the current byte and, for each passed range, reads a new byte and
  6215. checks if it is inside the range. If a violation was detected, set up an
  6216. error message and return false. Otherwise, return true.
  6217. @param[in] ranges list of integers; interpreted as list of pairs of
  6218. inclusive lower and upper bound, respectively
  6219. @pre The passed list @a ranges must have 2, 4, or 6 elements; that is,
  6220. 1, 2, or 3 pairs. This precondition is enforced by an assertion.
  6221. @return true if and only if no range violation was detected
  6222. */
  6223. bool next_byte_in_range(std::initializer_list<char_int_type> ranges)
  6224. {
  6225. JSON_ASSERT(ranges.size() == 2 || ranges.size() == 4 || ranges.size() == 6);
  6226. add(current);
  6227. for (auto range = ranges.begin(); range != ranges.end(); ++range)
  6228. {
  6229. get();
  6230. if (JSON_HEDLEY_LIKELY(*range <= current && current <= *(++range)))
  6231. {
  6232. add(current);
  6233. }
  6234. else
  6235. {
  6236. error_message = "invalid string: ill-formed UTF-8 byte";
  6237. return false;
  6238. }
  6239. }
  6240. return true;
  6241. }
  6242. /*!
  6243. @brief scan a string literal
  6244. This function scans a string according to Sect. 7 of RFC 8259. While
  6245. scanning, bytes are escaped and copied into buffer token_buffer. Then the
  6246. function returns successfully, token_buffer is *not* null-terminated (as it
  6247. may contain \0 bytes), and token_buffer.size() is the number of bytes in the
  6248. string.
  6249. @return token_type::value_string if string could be successfully scanned,
  6250. token_type::parse_error otherwise
  6251. @note In case of errors, variable error_message contains a textual
  6252. description.
  6253. */
  6254. token_type scan_string()
  6255. {
  6256. // reset token_buffer (ignore opening quote)
  6257. reset();
  6258. // we entered the function by reading an open quote
  6259. JSON_ASSERT(current == '\"');
  6260. while (true)
  6261. {
  6262. // get next character
  6263. switch (get())
  6264. {
  6265. // end of file while parsing string
  6266. case std::char_traits<char_type>::eof():
  6267. {
  6268. error_message = "invalid string: missing closing quote";
  6269. return token_type::parse_error;
  6270. }
  6271. // closing quote
  6272. case '\"':
  6273. {
  6274. return token_type::value_string;
  6275. }
  6276. // escapes
  6277. case '\\':
  6278. {
  6279. switch (get())
  6280. {
  6281. // quotation mark
  6282. case '\"':
  6283. add('\"');
  6284. break;
  6285. // reverse solidus
  6286. case '\\':
  6287. add('\\');
  6288. break;
  6289. // solidus
  6290. case '/':
  6291. add('/');
  6292. break;
  6293. // backspace
  6294. case 'b':
  6295. add('\b');
  6296. break;
  6297. // form feed
  6298. case 'f':
  6299. add('\f');
  6300. break;
  6301. // line feed
  6302. case 'n':
  6303. add('\n');
  6304. break;
  6305. // carriage return
  6306. case 'r':
  6307. add('\r');
  6308. break;
  6309. // tab
  6310. case 't':
  6311. add('\t');
  6312. break;
  6313. // unicode escapes
  6314. case 'u':
  6315. {
  6316. const int codepoint1 = get_codepoint();
  6317. int codepoint = codepoint1; // start with codepoint1
  6318. if (JSON_HEDLEY_UNLIKELY(codepoint1 == -1))
  6319. {
  6320. error_message = "invalid string: '\\u' must be followed by 4 hex digits";
  6321. return token_type::parse_error;
  6322. }
  6323. // check if code point is a high surrogate
  6324. if (0xD800 <= codepoint1 && codepoint1 <= 0xDBFF)
  6325. {
  6326. // expect next \uxxxx entry
  6327. if (JSON_HEDLEY_LIKELY(get() == '\\' && get() == 'u'))
  6328. {
  6329. const int codepoint2 = get_codepoint();
  6330. if (JSON_HEDLEY_UNLIKELY(codepoint2 == -1))
  6331. {
  6332. error_message = "invalid string: '\\u' must be followed by 4 hex digits";
  6333. return token_type::parse_error;
  6334. }
  6335. // check if codepoint2 is a low surrogate
  6336. if (JSON_HEDLEY_LIKELY(0xDC00 <= codepoint2 && codepoint2 <= 0xDFFF))
  6337. {
  6338. // overwrite codepoint
  6339. codepoint = static_cast<int>(
  6340. // high surrogate occupies the most significant 22 bits
  6341. (static_cast<unsigned int>(codepoint1) << 10u)
  6342. // low surrogate occupies the least significant 15 bits
  6343. + static_cast<unsigned int>(codepoint2)
  6344. // there is still the 0xD800, 0xDC00 and 0x10000 noise
  6345. // in the result, so we have to subtract with:
  6346. // (0xD800 << 10) + DC00 - 0x10000 = 0x35FDC00
  6347. - 0x35FDC00u);
  6348. }
  6349. else
  6350. {
  6351. error_message = "invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF";
  6352. return token_type::parse_error;
  6353. }
  6354. }
  6355. else
  6356. {
  6357. error_message = "invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF";
  6358. return token_type::parse_error;
  6359. }
  6360. }
  6361. else
  6362. {
  6363. if (JSON_HEDLEY_UNLIKELY(0xDC00 <= codepoint1 && codepoint1 <= 0xDFFF))
  6364. {
  6365. error_message = "invalid string: surrogate U+DC00..U+DFFF must follow U+D800..U+DBFF";
  6366. return token_type::parse_error;
  6367. }
  6368. }
  6369. // result of the above calculation yields a proper codepoint
  6370. JSON_ASSERT(0x00 <= codepoint && codepoint <= 0x10FFFF);
  6371. // translate codepoint into bytes
  6372. if (codepoint < 0x80)
  6373. {
  6374. // 1-byte characters: 0xxxxxxx (ASCII)
  6375. add(static_cast<char_int_type>(codepoint));
  6376. }
  6377. else if (codepoint <= 0x7FF)
  6378. {
  6379. // 2-byte characters: 110xxxxx 10xxxxxx
  6380. add(static_cast<char_int_type>(0xC0u | (static_cast<unsigned int>(codepoint) >> 6u)));
  6381. add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
  6382. }
  6383. else if (codepoint <= 0xFFFF)
  6384. {
  6385. // 3-byte characters: 1110xxxx 10xxxxxx 10xxxxxx
  6386. add(static_cast<char_int_type>(0xE0u | (static_cast<unsigned int>(codepoint) >> 12u)));
  6387. add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 6u) & 0x3Fu)));
  6388. add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
  6389. }
  6390. else
  6391. {
  6392. // 4-byte characters: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  6393. add(static_cast<char_int_type>(0xF0u | (static_cast<unsigned int>(codepoint) >> 18u)));
  6394. add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 12u) & 0x3Fu)));
  6395. add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 6u) & 0x3Fu)));
  6396. add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
  6397. }
  6398. break;
  6399. }
  6400. // other characters after escape
  6401. default:
  6402. error_message = "invalid string: forbidden character after backslash";
  6403. return token_type::parse_error;
  6404. }
  6405. break;
  6406. }
  6407. // invalid control characters
  6408. case 0x00:
  6409. {
  6410. error_message = "invalid string: control character U+0000 (NUL) must be escaped to \\u0000";
  6411. return token_type::parse_error;
  6412. }
  6413. case 0x01:
  6414. {
  6415. error_message = "invalid string: control character U+0001 (SOH) must be escaped to \\u0001";
  6416. return token_type::parse_error;
  6417. }
  6418. case 0x02:
  6419. {
  6420. error_message = "invalid string: control character U+0002 (STX) must be escaped to \\u0002";
  6421. return token_type::parse_error;
  6422. }
  6423. case 0x03:
  6424. {
  6425. error_message = "invalid string: control character U+0003 (ETX) must be escaped to \\u0003";
  6426. return token_type::parse_error;
  6427. }
  6428. case 0x04:
  6429. {
  6430. error_message = "invalid string: control character U+0004 (EOT) must be escaped to \\u0004";
  6431. return token_type::parse_error;
  6432. }
  6433. case 0x05:
  6434. {
  6435. error_message = "invalid string: control character U+0005 (ENQ) must be escaped to \\u0005";
  6436. return token_type::parse_error;
  6437. }
  6438. case 0x06:
  6439. {
  6440. error_message = "invalid string: control character U+0006 (ACK) must be escaped to \\u0006";
  6441. return token_type::parse_error;
  6442. }
  6443. case 0x07:
  6444. {
  6445. error_message = "invalid string: control character U+0007 (BEL) must be escaped to \\u0007";
  6446. return token_type::parse_error;
  6447. }
  6448. case 0x08:
  6449. {
  6450. error_message = "invalid string: control character U+0008 (BS) must be escaped to \\u0008 or \\b";
  6451. return token_type::parse_error;
  6452. }
  6453. case 0x09:
  6454. {
  6455. error_message = "invalid string: control character U+0009 (HT) must be escaped to \\u0009 or \\t";
  6456. return token_type::parse_error;
  6457. }
  6458. case 0x0A:
  6459. {
  6460. error_message = "invalid string: control character U+000A (LF) must be escaped to \\u000A or \\n";
  6461. return token_type::parse_error;
  6462. }
  6463. case 0x0B:
  6464. {
  6465. error_message = "invalid string: control character U+000B (VT) must be escaped to \\u000B";
  6466. return token_type::parse_error;
  6467. }
  6468. case 0x0C:
  6469. {
  6470. error_message = "invalid string: control character U+000C (FF) must be escaped to \\u000C or \\f";
  6471. return token_type::parse_error;
  6472. }
  6473. case 0x0D:
  6474. {
  6475. error_message = "invalid string: control character U+000D (CR) must be escaped to \\u000D or \\r";
  6476. return token_type::parse_error;
  6477. }
  6478. case 0x0E:
  6479. {
  6480. error_message = "invalid string: control character U+000E (SO) must be escaped to \\u000E";
  6481. return token_type::parse_error;
  6482. }
  6483. case 0x0F:
  6484. {
  6485. error_message = "invalid string: control character U+000F (SI) must be escaped to \\u000F";
  6486. return token_type::parse_error;
  6487. }
  6488. case 0x10:
  6489. {
  6490. error_message = "invalid string: control character U+0010 (DLE) must be escaped to \\u0010";
  6491. return token_type::parse_error;
  6492. }
  6493. case 0x11:
  6494. {
  6495. error_message = "invalid string: control character U+0011 (DC1) must be escaped to \\u0011";
  6496. return token_type::parse_error;
  6497. }
  6498. case 0x12:
  6499. {
  6500. error_message = "invalid string: control character U+0012 (DC2) must be escaped to \\u0012";
  6501. return token_type::parse_error;
  6502. }
  6503. case 0x13:
  6504. {
  6505. error_message = "invalid string: control character U+0013 (DC3) must be escaped to \\u0013";
  6506. return token_type::parse_error;
  6507. }
  6508. case 0x14:
  6509. {
  6510. error_message = "invalid string: control character U+0014 (DC4) must be escaped to \\u0014";
  6511. return token_type::parse_error;
  6512. }
  6513. case 0x15:
  6514. {
  6515. error_message = "invalid string: control character U+0015 (NAK) must be escaped to \\u0015";
  6516. return token_type::parse_error;
  6517. }
  6518. case 0x16:
  6519. {
  6520. error_message = "invalid string: control character U+0016 (SYN) must be escaped to \\u0016";
  6521. return token_type::parse_error;
  6522. }
  6523. case 0x17:
  6524. {
  6525. error_message = "invalid string: control character U+0017 (ETB) must be escaped to \\u0017";
  6526. return token_type::parse_error;
  6527. }
  6528. case 0x18:
  6529. {
  6530. error_message = "invalid string: control character U+0018 (CAN) must be escaped to \\u0018";
  6531. return token_type::parse_error;
  6532. }
  6533. case 0x19:
  6534. {
  6535. error_message = "invalid string: control character U+0019 (EM) must be escaped to \\u0019";
  6536. return token_type::parse_error;
  6537. }
  6538. case 0x1A:
  6539. {
  6540. error_message = "invalid string: control character U+001A (SUB) must be escaped to \\u001A";
  6541. return token_type::parse_error;
  6542. }
  6543. case 0x1B:
  6544. {
  6545. error_message = "invalid string: control character U+001B (ESC) must be escaped to \\u001B";
  6546. return token_type::parse_error;
  6547. }
  6548. case 0x1C:
  6549. {
  6550. error_message = "invalid string: control character U+001C (FS) must be escaped to \\u001C";
  6551. return token_type::parse_error;
  6552. }
  6553. case 0x1D:
  6554. {
  6555. error_message = "invalid string: control character U+001D (GS) must be escaped to \\u001D";
  6556. return token_type::parse_error;
  6557. }
  6558. case 0x1E:
  6559. {
  6560. error_message = "invalid string: control character U+001E (RS) must be escaped to \\u001E";
  6561. return token_type::parse_error;
  6562. }
  6563. case 0x1F:
  6564. {
  6565. error_message = "invalid string: control character U+001F (US) must be escaped to \\u001F";
  6566. return token_type::parse_error;
  6567. }
  6568. // U+0020..U+007F (except U+0022 (quote) and U+005C (backspace))
  6569. case 0x20:
  6570. case 0x21:
  6571. case 0x23:
  6572. case 0x24:
  6573. case 0x25:
  6574. case 0x26:
  6575. case 0x27:
  6576. case 0x28:
  6577. case 0x29:
  6578. case 0x2A:
  6579. case 0x2B:
  6580. case 0x2C:
  6581. case 0x2D:
  6582. case 0x2E:
  6583. case 0x2F:
  6584. case 0x30:
  6585. case 0x31:
  6586. case 0x32:
  6587. case 0x33:
  6588. case 0x34:
  6589. case 0x35:
  6590. case 0x36:
  6591. case 0x37:
  6592. case 0x38:
  6593. case 0x39:
  6594. case 0x3A:
  6595. case 0x3B:
  6596. case 0x3C:
  6597. case 0x3D:
  6598. case 0x3E:
  6599. case 0x3F:
  6600. case 0x40:
  6601. case 0x41:
  6602. case 0x42:
  6603. case 0x43:
  6604. case 0x44:
  6605. case 0x45:
  6606. case 0x46:
  6607. case 0x47:
  6608. case 0x48:
  6609. case 0x49:
  6610. case 0x4A:
  6611. case 0x4B:
  6612. case 0x4C:
  6613. case 0x4D:
  6614. case 0x4E:
  6615. case 0x4F:
  6616. case 0x50:
  6617. case 0x51:
  6618. case 0x52:
  6619. case 0x53:
  6620. case 0x54:
  6621. case 0x55:
  6622. case 0x56:
  6623. case 0x57:
  6624. case 0x58:
  6625. case 0x59:
  6626. case 0x5A:
  6627. case 0x5B:
  6628. case 0x5D:
  6629. case 0x5E:
  6630. case 0x5F:
  6631. case 0x60:
  6632. case 0x61:
  6633. case 0x62:
  6634. case 0x63:
  6635. case 0x64:
  6636. case 0x65:
  6637. case 0x66:
  6638. case 0x67:
  6639. case 0x68:
  6640. case 0x69:
  6641. case 0x6A:
  6642. case 0x6B:
  6643. case 0x6C:
  6644. case 0x6D:
  6645. case 0x6E:
  6646. case 0x6F:
  6647. case 0x70:
  6648. case 0x71:
  6649. case 0x72:
  6650. case 0x73:
  6651. case 0x74:
  6652. case 0x75:
  6653. case 0x76:
  6654. case 0x77:
  6655. case 0x78:
  6656. case 0x79:
  6657. case 0x7A:
  6658. case 0x7B:
  6659. case 0x7C:
  6660. case 0x7D:
  6661. case 0x7E:
  6662. case 0x7F:
  6663. {
  6664. add(current);
  6665. break;
  6666. }
  6667. // U+0080..U+07FF: bytes C2..DF 80..BF
  6668. case 0xC2:
  6669. case 0xC3:
  6670. case 0xC4:
  6671. case 0xC5:
  6672. case 0xC6:
  6673. case 0xC7:
  6674. case 0xC8:
  6675. case 0xC9:
  6676. case 0xCA:
  6677. case 0xCB:
  6678. case 0xCC:
  6679. case 0xCD:
  6680. case 0xCE:
  6681. case 0xCF:
  6682. case 0xD0:
  6683. case 0xD1:
  6684. case 0xD2:
  6685. case 0xD3:
  6686. case 0xD4:
  6687. case 0xD5:
  6688. case 0xD6:
  6689. case 0xD7:
  6690. case 0xD8:
  6691. case 0xD9:
  6692. case 0xDA:
  6693. case 0xDB:
  6694. case 0xDC:
  6695. case 0xDD:
  6696. case 0xDE:
  6697. case 0xDF:
  6698. {
  6699. if (JSON_HEDLEY_UNLIKELY(!next_byte_in_range({0x80, 0xBF})))
  6700. {
  6701. return token_type::parse_error;
  6702. }
  6703. break;
  6704. }
  6705. // U+0800..U+0FFF: bytes E0 A0..BF 80..BF
  6706. case 0xE0:
  6707. {
  6708. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0xA0, 0xBF, 0x80, 0xBF}))))
  6709. {
  6710. return token_type::parse_error;
  6711. }
  6712. break;
  6713. }
  6714. // U+1000..U+CFFF: bytes E1..EC 80..BF 80..BF
  6715. // U+E000..U+FFFF: bytes EE..EF 80..BF 80..BF
  6716. case 0xE1:
  6717. case 0xE2:
  6718. case 0xE3:
  6719. case 0xE4:
  6720. case 0xE5:
  6721. case 0xE6:
  6722. case 0xE7:
  6723. case 0xE8:
  6724. case 0xE9:
  6725. case 0xEA:
  6726. case 0xEB:
  6727. case 0xEC:
  6728. case 0xEE:
  6729. case 0xEF:
  6730. {
  6731. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0xBF, 0x80, 0xBF}))))
  6732. {
  6733. return token_type::parse_error;
  6734. }
  6735. break;
  6736. }
  6737. // U+D000..U+D7FF: bytes ED 80..9F 80..BF
  6738. case 0xED:
  6739. {
  6740. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0x9F, 0x80, 0xBF}))))
  6741. {
  6742. return token_type::parse_error;
  6743. }
  6744. break;
  6745. }
  6746. // U+10000..U+3FFFF F0 90..BF 80..BF 80..BF
  6747. case 0xF0:
  6748. {
  6749. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x90, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
  6750. {
  6751. return token_type::parse_error;
  6752. }
  6753. break;
  6754. }
  6755. // U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF
  6756. case 0xF1:
  6757. case 0xF2:
  6758. case 0xF3:
  6759. {
  6760. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
  6761. {
  6762. return token_type::parse_error;
  6763. }
  6764. break;
  6765. }
  6766. // U+100000..U+10FFFF F4 80..8F 80..BF 80..BF
  6767. case 0xF4:
  6768. {
  6769. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0x8F, 0x80, 0xBF, 0x80, 0xBF}))))
  6770. {
  6771. return token_type::parse_error;
  6772. }
  6773. break;
  6774. }
  6775. // remaining bytes (80..C1 and F5..FF) are ill-formed
  6776. default:
  6777. {
  6778. error_message = "invalid string: ill-formed UTF-8 byte";
  6779. return token_type::parse_error;
  6780. }
  6781. }
  6782. }
  6783. }
  6784. /*!
  6785. * @brief scan a comment
  6786. * @return whether comment could be scanned successfully
  6787. */
  6788. bool scan_comment()
  6789. {
  6790. switch (get())
  6791. {
  6792. // single-line comments skip input until a newline or EOF is read
  6793. case '/':
  6794. {
  6795. while (true)
  6796. {
  6797. switch (get())
  6798. {
  6799. case '\n':
  6800. case '\r':
  6801. case std::char_traits<char_type>::eof():
  6802. case '\0':
  6803. return true;
  6804. default:
  6805. break;
  6806. }
  6807. }
  6808. }
  6809. // multi-line comments skip input until */ is read
  6810. case '*':
  6811. {
  6812. while (true)
  6813. {
  6814. switch (get())
  6815. {
  6816. case std::char_traits<char_type>::eof():
  6817. case '\0':
  6818. {
  6819. error_message = "invalid comment; missing closing '*/'";
  6820. return false;
  6821. }
  6822. case '*':
  6823. {
  6824. switch (get())
  6825. {
  6826. case '/':
  6827. return true;
  6828. default:
  6829. {
  6830. unget();
  6831. continue;
  6832. }
  6833. }
  6834. }
  6835. default:
  6836. continue;
  6837. }
  6838. }
  6839. }
  6840. // unexpected character after reading '/'
  6841. default:
  6842. {
  6843. error_message = "invalid comment; expecting '/' or '*' after '/'";
  6844. return false;
  6845. }
  6846. }
  6847. }
  6848. JSON_HEDLEY_NON_NULL(2)
  6849. static void strtof(float& f, const char* str, char** endptr) noexcept
  6850. {
  6851. f = std::strtof(str, endptr);
  6852. }
  6853. JSON_HEDLEY_NON_NULL(2)
  6854. static void strtof(double& f, const char* str, char** endptr) noexcept
  6855. {
  6856. f = std::strtod(str, endptr);
  6857. }
  6858. JSON_HEDLEY_NON_NULL(2)
  6859. static void strtof(long double& f, const char* str, char** endptr) noexcept
  6860. {
  6861. f = std::strtold(str, endptr);
  6862. }
  6863. /*!
  6864. @brief scan a number literal
  6865. This function scans a string according to Sect. 6 of RFC 8259.
  6866. The function is realized with a deterministic finite state machine derived
  6867. from the grammar described in RFC 8259. Starting in state "init", the
  6868. input is read and used to determined the next state. Only state "done"
  6869. accepts the number. State "error" is a trap state to model errors. In the
  6870. table below, "anything" means any character but the ones listed before.
  6871. state | 0 | 1-9 | e E | + | - | . | anything
  6872. ---------|----------|----------|----------|---------|---------|----------|-----------
  6873. init | zero | any1 | [error] | [error] | minus | [error] | [error]
  6874. minus | zero | any1 | [error] | [error] | [error] | [error] | [error]
  6875. zero | done | done | exponent | done | done | decimal1 | done
  6876. any1 | any1 | any1 | exponent | done | done | decimal1 | done
  6877. decimal1 | decimal2 | decimal2 | [error] | [error] | [error] | [error] | [error]
  6878. decimal2 | decimal2 | decimal2 | exponent | done | done | done | done
  6879. exponent | any2 | any2 | [error] | sign | sign | [error] | [error]
  6880. sign | any2 | any2 | [error] | [error] | [error] | [error] | [error]
  6881. any2 | any2 | any2 | done | done | done | done | done
  6882. The state machine is realized with one label per state (prefixed with
  6883. "scan_number_") and `goto` statements between them. The state machine
  6884. contains cycles, but any cycle can be left when EOF is read. Therefore,
  6885. the function is guaranteed to terminate.
  6886. During scanning, the read bytes are stored in token_buffer. This string is
  6887. then converted to a signed integer, an unsigned integer, or a
  6888. floating-point number.
  6889. @return token_type::value_unsigned, token_type::value_integer, or
  6890. token_type::value_float if number could be successfully scanned,
  6891. token_type::parse_error otherwise
  6892. @note The scanner is independent of the current locale. Internally, the
  6893. locale's decimal point is used instead of `.` to work with the
  6894. locale-dependent converters.
  6895. */
  6896. token_type scan_number() // lgtm [cpp/use-of-goto]
  6897. {
  6898. // reset token_buffer to store the number's bytes
  6899. reset();
  6900. // the type of the parsed number; initially set to unsigned; will be
  6901. // changed if minus sign, decimal point or exponent is read
  6902. token_type number_type = token_type::value_unsigned;
  6903. // state (init): we just found out we need to scan a number
  6904. switch (current)
  6905. {
  6906. case '-':
  6907. {
  6908. add(current);
  6909. goto scan_number_minus;
  6910. }
  6911. case '0':
  6912. {
  6913. add(current);
  6914. goto scan_number_zero;
  6915. }
  6916. case '1':
  6917. case '2':
  6918. case '3':
  6919. case '4':
  6920. case '5':
  6921. case '6':
  6922. case '7':
  6923. case '8':
  6924. case '9':
  6925. {
  6926. add(current);
  6927. goto scan_number_any1;
  6928. }
  6929. // all other characters are rejected outside scan_number()
  6930. default: // LCOV_EXCL_LINE
  6931. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  6932. }
  6933. scan_number_minus:
  6934. // state: we just parsed a leading minus sign
  6935. number_type = token_type::value_integer;
  6936. switch (get())
  6937. {
  6938. case '0':
  6939. {
  6940. add(current);
  6941. goto scan_number_zero;
  6942. }
  6943. case '1':
  6944. case '2':
  6945. case '3':
  6946. case '4':
  6947. case '5':
  6948. case '6':
  6949. case '7':
  6950. case '8':
  6951. case '9':
  6952. {
  6953. add(current);
  6954. goto scan_number_any1;
  6955. }
  6956. default:
  6957. {
  6958. error_message = "invalid number; expected digit after '-'";
  6959. return token_type::parse_error;
  6960. }
  6961. }
  6962. scan_number_zero:
  6963. // state: we just parse a zero (maybe with a leading minus sign)
  6964. switch (get())
  6965. {
  6966. case '.':
  6967. {
  6968. add(decimal_point_char);
  6969. goto scan_number_decimal1;
  6970. }
  6971. case 'e':
  6972. case 'E':
  6973. {
  6974. add(current);
  6975. goto scan_number_exponent;
  6976. }
  6977. default:
  6978. goto scan_number_done;
  6979. }
  6980. scan_number_any1:
  6981. // state: we just parsed a number 0-9 (maybe with a leading minus sign)
  6982. switch (get())
  6983. {
  6984. case '0':
  6985. case '1':
  6986. case '2':
  6987. case '3':
  6988. case '4':
  6989. case '5':
  6990. case '6':
  6991. case '7':
  6992. case '8':
  6993. case '9':
  6994. {
  6995. add(current);
  6996. goto scan_number_any1;
  6997. }
  6998. case '.':
  6999. {
  7000. add(decimal_point_char);
  7001. goto scan_number_decimal1;
  7002. }
  7003. case 'e':
  7004. case 'E':
  7005. {
  7006. add(current);
  7007. goto scan_number_exponent;
  7008. }
  7009. default:
  7010. goto scan_number_done;
  7011. }
  7012. scan_number_decimal1:
  7013. // state: we just parsed a decimal point
  7014. number_type = token_type::value_float;
  7015. switch (get())
  7016. {
  7017. case '0':
  7018. case '1':
  7019. case '2':
  7020. case '3':
  7021. case '4':
  7022. case '5':
  7023. case '6':
  7024. case '7':
  7025. case '8':
  7026. case '9':
  7027. {
  7028. add(current);
  7029. goto scan_number_decimal2;
  7030. }
  7031. default:
  7032. {
  7033. error_message = "invalid number; expected digit after '.'";
  7034. return token_type::parse_error;
  7035. }
  7036. }
  7037. scan_number_decimal2:
  7038. // we just parsed at least one number after a decimal point
  7039. switch (get())
  7040. {
  7041. case '0':
  7042. case '1':
  7043. case '2':
  7044. case '3':
  7045. case '4':
  7046. case '5':
  7047. case '6':
  7048. case '7':
  7049. case '8':
  7050. case '9':
  7051. {
  7052. add(current);
  7053. goto scan_number_decimal2;
  7054. }
  7055. case 'e':
  7056. case 'E':
  7057. {
  7058. add(current);
  7059. goto scan_number_exponent;
  7060. }
  7061. default:
  7062. goto scan_number_done;
  7063. }
  7064. scan_number_exponent:
  7065. // we just parsed an exponent
  7066. number_type = token_type::value_float;
  7067. switch (get())
  7068. {
  7069. case '+':
  7070. case '-':
  7071. {
  7072. add(current);
  7073. goto scan_number_sign;
  7074. }
  7075. case '0':
  7076. case '1':
  7077. case '2':
  7078. case '3':
  7079. case '4':
  7080. case '5':
  7081. case '6':
  7082. case '7':
  7083. case '8':
  7084. case '9':
  7085. {
  7086. add(current);
  7087. goto scan_number_any2;
  7088. }
  7089. default:
  7090. {
  7091. error_message =
  7092. "invalid number; expected '+', '-', or digit after exponent";
  7093. return token_type::parse_error;
  7094. }
  7095. }
  7096. scan_number_sign:
  7097. // we just parsed an exponent sign
  7098. switch (get())
  7099. {
  7100. case '0':
  7101. case '1':
  7102. case '2':
  7103. case '3':
  7104. case '4':
  7105. case '5':
  7106. case '6':
  7107. case '7':
  7108. case '8':
  7109. case '9':
  7110. {
  7111. add(current);
  7112. goto scan_number_any2;
  7113. }
  7114. default:
  7115. {
  7116. error_message = "invalid number; expected digit after exponent sign";
  7117. return token_type::parse_error;
  7118. }
  7119. }
  7120. scan_number_any2:
  7121. // we just parsed a number after the exponent or exponent sign
  7122. switch (get())
  7123. {
  7124. case '0':
  7125. case '1':
  7126. case '2':
  7127. case '3':
  7128. case '4':
  7129. case '5':
  7130. case '6':
  7131. case '7':
  7132. case '8':
  7133. case '9':
  7134. {
  7135. add(current);
  7136. goto scan_number_any2;
  7137. }
  7138. default:
  7139. goto scan_number_done;
  7140. }
  7141. scan_number_done:
  7142. // unget the character after the number (we only read it to know that
  7143. // we are done scanning a number)
  7144. unget();
  7145. char* endptr = nullptr; // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  7146. errno = 0;
  7147. // try to parse integers first and fall back to floats
  7148. if (number_type == token_type::value_unsigned)
  7149. {
  7150. const auto x = std::strtoull(token_buffer.data(), &endptr, 10);
  7151. // we checked the number format before
  7152. JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
  7153. if (errno == 0)
  7154. {
  7155. value_unsigned = static_cast<number_unsigned_t>(x);
  7156. if (value_unsigned == x)
  7157. {
  7158. return token_type::value_unsigned;
  7159. }
  7160. }
  7161. }
  7162. else if (number_type == token_type::value_integer)
  7163. {
  7164. const auto x = std::strtoll(token_buffer.data(), &endptr, 10);
  7165. // we checked the number format before
  7166. JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
  7167. if (errno == 0)
  7168. {
  7169. value_integer = static_cast<number_integer_t>(x);
  7170. if (value_integer == x)
  7171. {
  7172. return token_type::value_integer;
  7173. }
  7174. }
  7175. }
  7176. // this code is reached if we parse a floating-point number or if an
  7177. // integer conversion above failed
  7178. strtof(value_float, token_buffer.data(), &endptr);
  7179. // we checked the number format before
  7180. JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
  7181. return token_type::value_float;
  7182. }
  7183. /*!
  7184. @param[in] literal_text the literal text to expect
  7185. @param[in] length the length of the passed literal text
  7186. @param[in] return_type the token type to return on success
  7187. */
  7188. JSON_HEDLEY_NON_NULL(2)
  7189. token_type scan_literal(const char_type* literal_text, const std::size_t length,
  7190. token_type return_type)
  7191. {
  7192. JSON_ASSERT(std::char_traits<char_type>::to_char_type(current) == literal_text[0]);
  7193. for (std::size_t i = 1; i < length; ++i)
  7194. {
  7195. if (JSON_HEDLEY_UNLIKELY(std::char_traits<char_type>::to_char_type(get()) != literal_text[i]))
  7196. {
  7197. error_message = "invalid literal";
  7198. return token_type::parse_error;
  7199. }
  7200. }
  7201. return return_type;
  7202. }
  7203. /////////////////////
  7204. // input management
  7205. /////////////////////
  7206. /// reset token_buffer; current character is beginning of token
  7207. void reset() noexcept
  7208. {
  7209. token_buffer.clear();
  7210. token_string.clear();
  7211. token_string.push_back(std::char_traits<char_type>::to_char_type(current));
  7212. }
  7213. /*
  7214. @brief get next character from the input
  7215. This function provides the interface to the used input adapter. It does
  7216. not throw in case the input reached EOF, but returns a
  7217. `std::char_traits<char>::eof()` in that case. Stores the scanned characters
  7218. for use in error messages.
  7219. @return character read from the input
  7220. */
  7221. char_int_type get()
  7222. {
  7223. ++position.chars_read_total;
  7224. ++position.chars_read_current_line;
  7225. if (next_unget)
  7226. {
  7227. // just reset the next_unget variable and work with current
  7228. next_unget = false;
  7229. }
  7230. else
  7231. {
  7232. current = ia.get_character();
  7233. }
  7234. if (JSON_HEDLEY_LIKELY(current != std::char_traits<char_type>::eof()))
  7235. {
  7236. token_string.push_back(std::char_traits<char_type>::to_char_type(current));
  7237. }
  7238. if (current == '\n')
  7239. {
  7240. ++position.lines_read;
  7241. position.chars_read_current_line = 0;
  7242. }
  7243. return current;
  7244. }
  7245. /*!
  7246. @brief unget current character (read it again on next get)
  7247. We implement unget by setting variable next_unget to true. The input is not
  7248. changed - we just simulate ungetting by modifying chars_read_total,
  7249. chars_read_current_line, and token_string. The next call to get() will
  7250. behave as if the unget character is read again.
  7251. */
  7252. void unget()
  7253. {
  7254. next_unget = true;
  7255. --position.chars_read_total;
  7256. // in case we "unget" a newline, we have to also decrement the lines_read
  7257. if (position.chars_read_current_line == 0)
  7258. {
  7259. if (position.lines_read > 0)
  7260. {
  7261. --position.lines_read;
  7262. }
  7263. }
  7264. else
  7265. {
  7266. --position.chars_read_current_line;
  7267. }
  7268. if (JSON_HEDLEY_LIKELY(current != std::char_traits<char_type>::eof()))
  7269. {
  7270. JSON_ASSERT(!token_string.empty());
  7271. token_string.pop_back();
  7272. }
  7273. }
  7274. /// add a character to token_buffer
  7275. void add(char_int_type c)
  7276. {
  7277. token_buffer.push_back(static_cast<typename string_t::value_type>(c));
  7278. }
  7279. public:
  7280. /////////////////////
  7281. // value getters
  7282. /////////////////////
  7283. /// return integer value
  7284. constexpr number_integer_t get_number_integer() const noexcept
  7285. {
  7286. return value_integer;
  7287. }
  7288. /// return unsigned integer value
  7289. constexpr number_unsigned_t get_number_unsigned() const noexcept
  7290. {
  7291. return value_unsigned;
  7292. }
  7293. /// return floating-point value
  7294. constexpr number_float_t get_number_float() const noexcept
  7295. {
  7296. return value_float;
  7297. }
  7298. /// return current string value (implicitly resets the token; useful only once)
  7299. string_t& get_string()
  7300. {
  7301. return token_buffer;
  7302. }
  7303. /////////////////////
  7304. // diagnostics
  7305. /////////////////////
  7306. /// return position of last read token
  7307. constexpr position_t get_position() const noexcept
  7308. {
  7309. return position;
  7310. }
  7311. /// return the last read token (for errors only). Will never contain EOF
  7312. /// (an arbitrary value that is not a valid char value, often -1), because
  7313. /// 255 may legitimately occur. May contain NUL, which should be escaped.
  7314. std::string get_token_string() const
  7315. {
  7316. // escape control characters
  7317. std::string result;
  7318. for (const auto c : token_string)
  7319. {
  7320. if (static_cast<unsigned char>(c) <= '\x1F')
  7321. {
  7322. // escape control characters
  7323. std::array<char, 9> cs{{}};
  7324. static_cast<void>((std::snprintf)(cs.data(), cs.size(), "<U+%.4X>", static_cast<unsigned char>(c))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  7325. result += cs.data();
  7326. }
  7327. else
  7328. {
  7329. // add character as is
  7330. result.push_back(static_cast<std::string::value_type>(c));
  7331. }
  7332. }
  7333. return result;
  7334. }
  7335. /// return syntax error message
  7336. JSON_HEDLEY_RETURNS_NON_NULL
  7337. constexpr const char* get_error_message() const noexcept
  7338. {
  7339. return error_message;
  7340. }
  7341. /////////////////////
  7342. // actual scanner
  7343. /////////////////////
  7344. /*!
  7345. @brief skip the UTF-8 byte order mark
  7346. @return true iff there is no BOM or the correct BOM has been skipped
  7347. */
  7348. bool skip_bom()
  7349. {
  7350. if (get() == 0xEF)
  7351. {
  7352. // check if we completely parse the BOM
  7353. return get() == 0xBB && get() == 0xBF;
  7354. }
  7355. // the first character is not the beginning of the BOM; unget it to
  7356. // process is later
  7357. unget();
  7358. return true;
  7359. }
  7360. void skip_whitespace()
  7361. {
  7362. do
  7363. {
  7364. get();
  7365. }
  7366. while (current == ' ' || current == '\t' || current == '\n' || current == '\r');
  7367. }
  7368. token_type scan()
  7369. {
  7370. // initially, skip the BOM
  7371. if (position.chars_read_total == 0 && !skip_bom())
  7372. {
  7373. error_message = "invalid BOM; must be 0xEF 0xBB 0xBF if given";
  7374. return token_type::parse_error;
  7375. }
  7376. // read next character and ignore whitespace
  7377. skip_whitespace();
  7378. // ignore comments
  7379. while (ignore_comments && current == '/')
  7380. {
  7381. if (!scan_comment())
  7382. {
  7383. return token_type::parse_error;
  7384. }
  7385. // skip following whitespace
  7386. skip_whitespace();
  7387. }
  7388. switch (current)
  7389. {
  7390. // structural characters
  7391. case '[':
  7392. return token_type::begin_array;
  7393. case ']':
  7394. return token_type::end_array;
  7395. case '{':
  7396. return token_type::begin_object;
  7397. case '}':
  7398. return token_type::end_object;
  7399. case ':':
  7400. return token_type::name_separator;
  7401. case ',':
  7402. return token_type::value_separator;
  7403. // literals
  7404. case 't':
  7405. {
  7406. 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')}};
  7407. return scan_literal(true_literal.data(), true_literal.size(), token_type::literal_true);
  7408. }
  7409. case 'f':
  7410. {
  7411. 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')}};
  7412. return scan_literal(false_literal.data(), false_literal.size(), token_type::literal_false);
  7413. }
  7414. case 'n':
  7415. {
  7416. 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')}};
  7417. return scan_literal(null_literal.data(), null_literal.size(), token_type::literal_null);
  7418. }
  7419. // string
  7420. case '\"':
  7421. return scan_string();
  7422. // number
  7423. case '-':
  7424. case '0':
  7425. case '1':
  7426. case '2':
  7427. case '3':
  7428. case '4':
  7429. case '5':
  7430. case '6':
  7431. case '7':
  7432. case '8':
  7433. case '9':
  7434. return scan_number();
  7435. // end of input (the null byte is needed when parsing from
  7436. // string literals)
  7437. case '\0':
  7438. case std::char_traits<char_type>::eof():
  7439. return token_type::end_of_input;
  7440. // error
  7441. default:
  7442. error_message = "invalid literal";
  7443. return token_type::parse_error;
  7444. }
  7445. }
  7446. private:
  7447. /// input adapter
  7448. InputAdapterType ia;
  7449. /// whether comments should be ignored (true) or signaled as errors (false)
  7450. const bool ignore_comments = false;
  7451. /// the current character
  7452. char_int_type current = std::char_traits<char_type>::eof();
  7453. /// whether the next get() call should just return current
  7454. bool next_unget = false;
  7455. /// the start position of the current token
  7456. position_t position {};
  7457. /// raw input token string (for error messages)
  7458. std::vector<char_type> token_string {};
  7459. /// buffer for variable-length tokens (numbers, strings)
  7460. string_t token_buffer {};
  7461. /// a description of occurred lexer errors
  7462. const char* error_message = "";
  7463. // number values
  7464. number_integer_t value_integer = 0;
  7465. number_unsigned_t value_unsigned = 0;
  7466. number_float_t value_float = 0;
  7467. /// the decimal point
  7468. const char_int_type decimal_point_char = '.';
  7469. };
  7470. } // namespace detail
  7471. } // namespace nlohmann
  7472. // #include <nlohmann/detail/macro_scope.hpp>
  7473. // #include <nlohmann/detail/meta/is_sax.hpp>
  7474. #include <cstdint> // size_t
  7475. #include <utility> // declval
  7476. #include <string> // string
  7477. // #include <nlohmann/detail/meta/detected.hpp>
  7478. // #include <nlohmann/detail/meta/type_traits.hpp>
  7479. namespace nlohmann
  7480. {
  7481. namespace detail
  7482. {
  7483. template<typename T>
  7484. using null_function_t = decltype(std::declval<T&>().null());
  7485. template<typename T>
  7486. using boolean_function_t =
  7487. decltype(std::declval<T&>().boolean(std::declval<bool>()));
  7488. template<typename T, typename Integer>
  7489. using number_integer_function_t =
  7490. decltype(std::declval<T&>().number_integer(std::declval<Integer>()));
  7491. template<typename T, typename Unsigned>
  7492. using number_unsigned_function_t =
  7493. decltype(std::declval<T&>().number_unsigned(std::declval<Unsigned>()));
  7494. template<typename T, typename Float, typename String>
  7495. using number_float_function_t = decltype(std::declval<T&>().number_float(
  7496. std::declval<Float>(), std::declval<const String&>()));
  7497. template<typename T, typename String>
  7498. using string_function_t =
  7499. decltype(std::declval<T&>().string(std::declval<String&>()));
  7500. template<typename T, typename Binary>
  7501. using binary_function_t =
  7502. decltype(std::declval<T&>().binary(std::declval<Binary&>()));
  7503. template<typename T>
  7504. using start_object_function_t =
  7505. decltype(std::declval<T&>().start_object(std::declval<std::size_t>()));
  7506. template<typename T, typename String>
  7507. using key_function_t =
  7508. decltype(std::declval<T&>().key(std::declval<String&>()));
  7509. template<typename T>
  7510. using end_object_function_t = decltype(std::declval<T&>().end_object());
  7511. template<typename T>
  7512. using start_array_function_t =
  7513. decltype(std::declval<T&>().start_array(std::declval<std::size_t>()));
  7514. template<typename T>
  7515. using end_array_function_t = decltype(std::declval<T&>().end_array());
  7516. template<typename T, typename Exception>
  7517. using parse_error_function_t = decltype(std::declval<T&>().parse_error(
  7518. std::declval<std::size_t>(), std::declval<const std::string&>(),
  7519. std::declval<const Exception&>()));
  7520. template<typename SAX, typename BasicJsonType>
  7521. struct is_sax
  7522. {
  7523. private:
  7524. static_assert(is_basic_json<BasicJsonType>::value,
  7525. "BasicJsonType must be of type basic_json<...>");
  7526. using number_integer_t = typename BasicJsonType::number_integer_t;
  7527. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  7528. using number_float_t = typename BasicJsonType::number_float_t;
  7529. using string_t = typename BasicJsonType::string_t;
  7530. using binary_t = typename BasicJsonType::binary_t;
  7531. using exception_t = typename BasicJsonType::exception;
  7532. public:
  7533. static constexpr bool value =
  7534. is_detected_exact<bool, null_function_t, SAX>::value &&
  7535. is_detected_exact<bool, boolean_function_t, SAX>::value &&
  7536. is_detected_exact<bool, number_integer_function_t, SAX, number_integer_t>::value &&
  7537. is_detected_exact<bool, number_unsigned_function_t, SAX, number_unsigned_t>::value &&
  7538. is_detected_exact<bool, number_float_function_t, SAX, number_float_t, string_t>::value &&
  7539. is_detected_exact<bool, string_function_t, SAX, string_t>::value &&
  7540. is_detected_exact<bool, binary_function_t, SAX, binary_t>::value &&
  7541. is_detected_exact<bool, start_object_function_t, SAX>::value &&
  7542. is_detected_exact<bool, key_function_t, SAX, string_t>::value &&
  7543. is_detected_exact<bool, end_object_function_t, SAX>::value &&
  7544. is_detected_exact<bool, start_array_function_t, SAX>::value &&
  7545. is_detected_exact<bool, end_array_function_t, SAX>::value &&
  7546. is_detected_exact<bool, parse_error_function_t, SAX, exception_t>::value;
  7547. };
  7548. template<typename SAX, typename BasicJsonType>
  7549. struct is_sax_static_asserts
  7550. {
  7551. private:
  7552. static_assert(is_basic_json<BasicJsonType>::value,
  7553. "BasicJsonType must be of type basic_json<...>");
  7554. using number_integer_t = typename BasicJsonType::number_integer_t;
  7555. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  7556. using number_float_t = typename BasicJsonType::number_float_t;
  7557. using string_t = typename BasicJsonType::string_t;
  7558. using binary_t = typename BasicJsonType::binary_t;
  7559. using exception_t = typename BasicJsonType::exception;
  7560. public:
  7561. static_assert(is_detected_exact<bool, null_function_t, SAX>::value,
  7562. "Missing/invalid function: bool null()");
  7563. static_assert(is_detected_exact<bool, boolean_function_t, SAX>::value,
  7564. "Missing/invalid function: bool boolean(bool)");
  7565. static_assert(is_detected_exact<bool, boolean_function_t, SAX>::value,
  7566. "Missing/invalid function: bool boolean(bool)");
  7567. static_assert(
  7568. is_detected_exact<bool, number_integer_function_t, SAX,
  7569. number_integer_t>::value,
  7570. "Missing/invalid function: bool number_integer(number_integer_t)");
  7571. static_assert(
  7572. is_detected_exact<bool, number_unsigned_function_t, SAX,
  7573. number_unsigned_t>::value,
  7574. "Missing/invalid function: bool number_unsigned(number_unsigned_t)");
  7575. static_assert(is_detected_exact<bool, number_float_function_t, SAX,
  7576. number_float_t, string_t>::value,
  7577. "Missing/invalid function: bool number_float(number_float_t, const string_t&)");
  7578. static_assert(
  7579. is_detected_exact<bool, string_function_t, SAX, string_t>::value,
  7580. "Missing/invalid function: bool string(string_t&)");
  7581. static_assert(
  7582. is_detected_exact<bool, binary_function_t, SAX, binary_t>::value,
  7583. "Missing/invalid function: bool binary(binary_t&)");
  7584. static_assert(is_detected_exact<bool, start_object_function_t, SAX>::value,
  7585. "Missing/invalid function: bool start_object(std::size_t)");
  7586. static_assert(is_detected_exact<bool, key_function_t, SAX, string_t>::value,
  7587. "Missing/invalid function: bool key(string_t&)");
  7588. static_assert(is_detected_exact<bool, end_object_function_t, SAX>::value,
  7589. "Missing/invalid function: bool end_object()");
  7590. static_assert(is_detected_exact<bool, start_array_function_t, SAX>::value,
  7591. "Missing/invalid function: bool start_array(std::size_t)");
  7592. static_assert(is_detected_exact<bool, end_array_function_t, SAX>::value,
  7593. "Missing/invalid function: bool end_array()");
  7594. static_assert(
  7595. is_detected_exact<bool, parse_error_function_t, SAX, exception_t>::value,
  7596. "Missing/invalid function: bool parse_error(std::size_t, const "
  7597. "std::string&, const exception&)");
  7598. };
  7599. } // namespace detail
  7600. } // namespace nlohmann
  7601. // #include <nlohmann/detail/meta/type_traits.hpp>
  7602. // #include <nlohmann/detail/string_concat.hpp>
  7603. // #include <nlohmann/detail/value_t.hpp>
  7604. namespace nlohmann
  7605. {
  7606. namespace detail
  7607. {
  7608. /// how to treat CBOR tags
  7609. enum class cbor_tag_handler_t
  7610. {
  7611. error, ///< throw a parse_error exception in case of a tag
  7612. ignore, ///< ignore tags
  7613. store ///< store tags as binary type
  7614. };
  7615. /*!
  7616. @brief determine system byte order
  7617. @return true if and only if system's byte order is little endian
  7618. @note from https://stackoverflow.com/a/1001328/266378
  7619. */
  7620. static inline bool little_endianness(int num = 1) noexcept
  7621. {
  7622. return *reinterpret_cast<char*>(&num) == 1;
  7623. }
  7624. ///////////////////
  7625. // binary reader //
  7626. ///////////////////
  7627. /*!
  7628. @brief deserialization of CBOR, MessagePack, and UBJSON values
  7629. */
  7630. template<typename BasicJsonType, typename InputAdapterType, typename SAX = json_sax_dom_parser<BasicJsonType>>
  7631. class binary_reader
  7632. {
  7633. using number_integer_t = typename BasicJsonType::number_integer_t;
  7634. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  7635. using number_float_t = typename BasicJsonType::number_float_t;
  7636. using string_t = typename BasicJsonType::string_t;
  7637. using binary_t = typename BasicJsonType::binary_t;
  7638. using json_sax_t = SAX;
  7639. using char_type = typename InputAdapterType::char_type;
  7640. using char_int_type = typename std::char_traits<char_type>::int_type;
  7641. public:
  7642. /*!
  7643. @brief create a binary reader
  7644. @param[in] adapter input adapter to read from
  7645. */
  7646. explicit binary_reader(InputAdapterType&& adapter, const input_format_t format = input_format_t::json) noexcept : ia(std::move(adapter)), input_format(format)
  7647. {
  7648. (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
  7649. }
  7650. // make class move-only
  7651. binary_reader(const binary_reader&) = delete;
  7652. binary_reader(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  7653. binary_reader& operator=(const binary_reader&) = delete;
  7654. binary_reader& operator=(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  7655. ~binary_reader() = default;
  7656. /*!
  7657. @param[in] format the binary format to parse
  7658. @param[in] sax_ a SAX event processor
  7659. @param[in] strict whether to expect the input to be consumed completed
  7660. @param[in] tag_handler how to treat CBOR tags
  7661. @return whether parsing was successful
  7662. */
  7663. JSON_HEDLEY_NON_NULL(3)
  7664. bool sax_parse(const input_format_t format,
  7665. json_sax_t* sax_,
  7666. const bool strict = true,
  7667. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  7668. {
  7669. sax = sax_;
  7670. bool result = false;
  7671. switch (format)
  7672. {
  7673. case input_format_t::bson:
  7674. result = parse_bson_internal();
  7675. break;
  7676. case input_format_t::cbor:
  7677. result = parse_cbor_internal(true, tag_handler);
  7678. break;
  7679. case input_format_t::msgpack:
  7680. result = parse_msgpack_internal();
  7681. break;
  7682. case input_format_t::ubjson:
  7683. case input_format_t::bjdata:
  7684. result = parse_ubjson_internal();
  7685. break;
  7686. case input_format_t::json: // LCOV_EXCL_LINE
  7687. default: // LCOV_EXCL_LINE
  7688. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  7689. }
  7690. // strict mode: next byte must be EOF
  7691. if (result && strict)
  7692. {
  7693. if (input_format == input_format_t::ubjson || input_format == input_format_t::bjdata)
  7694. {
  7695. get_ignore_noop();
  7696. }
  7697. else
  7698. {
  7699. get();
  7700. }
  7701. if (JSON_HEDLEY_UNLIKELY(current != std::char_traits<char_type>::eof()))
  7702. {
  7703. return sax->parse_error(chars_read, get_token_string(), parse_error::create(110, chars_read,
  7704. exception_message(input_format, concat("expected end of input; last byte: 0x", get_token_string()), "value"), nullptr));
  7705. }
  7706. }
  7707. return result;
  7708. }
  7709. private:
  7710. //////////
  7711. // BSON //
  7712. //////////
  7713. /*!
  7714. @brief Reads in a BSON-object and passes it to the SAX-parser.
  7715. @return whether a valid BSON-value was passed to the SAX parser
  7716. */
  7717. bool parse_bson_internal()
  7718. {
  7719. std::int32_t document_size{};
  7720. get_number<std::int32_t, true>(input_format_t::bson, document_size);
  7721. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast<std::size_t>(-1))))
  7722. {
  7723. return false;
  7724. }
  7725. if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/false)))
  7726. {
  7727. return false;
  7728. }
  7729. return sax->end_object();
  7730. }
  7731. /*!
  7732. @brief Parses a C-style string from the BSON input.
  7733. @param[in,out] result A reference to the string variable where the read
  7734. string is to be stored.
  7735. @return `true` if the \x00-byte indicating the end of the string was
  7736. encountered before the EOF; false` indicates an unexpected EOF.
  7737. */
  7738. bool get_bson_cstr(string_t& result)
  7739. {
  7740. auto out = std::back_inserter(result);
  7741. while (true)
  7742. {
  7743. get();
  7744. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "cstring")))
  7745. {
  7746. return false;
  7747. }
  7748. if (current == 0x00)
  7749. {
  7750. return true;
  7751. }
  7752. *out++ = static_cast<typename string_t::value_type>(current);
  7753. }
  7754. }
  7755. /*!
  7756. @brief Parses a zero-terminated string of length @a len from the BSON
  7757. input.
  7758. @param[in] len The length (including the zero-byte at the end) of the
  7759. string to be read.
  7760. @param[in,out] result A reference to the string variable where the read
  7761. string is to be stored.
  7762. @tparam NumberType The type of the length @a len
  7763. @pre len >= 1
  7764. @return `true` if the string was successfully parsed
  7765. */
  7766. template<typename NumberType>
  7767. bool get_bson_string(const NumberType len, string_t& result)
  7768. {
  7769. if (JSON_HEDLEY_UNLIKELY(len < 1))
  7770. {
  7771. auto last_token = get_token_string();
  7772. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  7773. exception_message(input_format_t::bson, concat("string length must be at least 1, is ", std::to_string(len)), "string"), nullptr));
  7774. }
  7775. return get_string(input_format_t::bson, len - static_cast<NumberType>(1), result) && get() != std::char_traits<char_type>::eof();
  7776. }
  7777. /*!
  7778. @brief Parses a byte array input of length @a len from the BSON input.
  7779. @param[in] len The length of the byte array to be read.
  7780. @param[in,out] result A reference to the binary variable where the read
  7781. array is to be stored.
  7782. @tparam NumberType The type of the length @a len
  7783. @pre len >= 0
  7784. @return `true` if the byte array was successfully parsed
  7785. */
  7786. template<typename NumberType>
  7787. bool get_bson_binary(const NumberType len, binary_t& result)
  7788. {
  7789. if (JSON_HEDLEY_UNLIKELY(len < 0))
  7790. {
  7791. auto last_token = get_token_string();
  7792. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  7793. exception_message(input_format_t::bson, concat("byte array length cannot be negative, is ", std::to_string(len)), "binary"), nullptr));
  7794. }
  7795. // All BSON binary values have a subtype
  7796. std::uint8_t subtype{};
  7797. get_number<std::uint8_t>(input_format_t::bson, subtype);
  7798. result.set_subtype(subtype);
  7799. return get_binary(input_format_t::bson, len, result);
  7800. }
  7801. /*!
  7802. @brief Read a BSON document element of the given @a element_type.
  7803. @param[in] element_type The BSON element type, c.f. http://bsonspec.org/spec.html
  7804. @param[in] element_type_parse_position The position in the input stream,
  7805. where the `element_type` was read.
  7806. @warning Not all BSON element types are supported yet. An unsupported
  7807. @a element_type will give rise to a parse_error.114:
  7808. Unsupported BSON record type 0x...
  7809. @return whether a valid BSON-object/array was passed to the SAX parser
  7810. */
  7811. bool parse_bson_element_internal(const char_int_type element_type,
  7812. const std::size_t element_type_parse_position)
  7813. {
  7814. switch (element_type)
  7815. {
  7816. case 0x01: // double
  7817. {
  7818. double number{};
  7819. return get_number<double, true>(input_format_t::bson, number) && sax->number_float(static_cast<number_float_t>(number), "");
  7820. }
  7821. case 0x02: // string
  7822. {
  7823. std::int32_t len{};
  7824. string_t value;
  7825. return get_number<std::int32_t, true>(input_format_t::bson, len) && get_bson_string(len, value) && sax->string(value);
  7826. }
  7827. case 0x03: // object
  7828. {
  7829. return parse_bson_internal();
  7830. }
  7831. case 0x04: // array
  7832. {
  7833. return parse_bson_array();
  7834. }
  7835. case 0x05: // binary
  7836. {
  7837. std::int32_t len{};
  7838. binary_t value;
  7839. return get_number<std::int32_t, true>(input_format_t::bson, len) && get_bson_binary(len, value) && sax->binary(value);
  7840. }
  7841. case 0x08: // boolean
  7842. {
  7843. return sax->boolean(get() != 0);
  7844. }
  7845. case 0x0A: // null
  7846. {
  7847. return sax->null();
  7848. }
  7849. case 0x10: // int32
  7850. {
  7851. std::int32_t value{};
  7852. return get_number<std::int32_t, true>(input_format_t::bson, value) && sax->number_integer(value);
  7853. }
  7854. case 0x12: // int64
  7855. {
  7856. std::int64_t value{};
  7857. return get_number<std::int64_t, true>(input_format_t::bson, value) && sax->number_integer(value);
  7858. }
  7859. default: // anything else not supported (yet)
  7860. {
  7861. std::array<char, 3> cr{{}};
  7862. static_cast<void>((std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(element_type))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  7863. std::string cr_str{cr.data()};
  7864. return sax->parse_error(element_type_parse_position, cr_str,
  7865. parse_error::create(114, element_type_parse_position, concat("Unsupported BSON record type 0x", cr_str), nullptr));
  7866. }
  7867. }
  7868. }
  7869. /*!
  7870. @brief Read a BSON element list (as specified in the BSON-spec)
  7871. The same binary layout is used for objects and arrays, hence it must be
  7872. indicated with the argument @a is_array which one is expected
  7873. (true --> array, false --> object).
  7874. @param[in] is_array Determines if the element list being read is to be
  7875. treated as an object (@a is_array == false), or as an
  7876. array (@a is_array == true).
  7877. @return whether a valid BSON-object/array was passed to the SAX parser
  7878. */
  7879. bool parse_bson_element_list(const bool is_array)
  7880. {
  7881. string_t key;
  7882. while (auto element_type = get())
  7883. {
  7884. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "element list")))
  7885. {
  7886. return false;
  7887. }
  7888. const std::size_t element_type_parse_position = chars_read;
  7889. if (JSON_HEDLEY_UNLIKELY(!get_bson_cstr(key)))
  7890. {
  7891. return false;
  7892. }
  7893. if (!is_array && !sax->key(key))
  7894. {
  7895. return false;
  7896. }
  7897. if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_internal(element_type, element_type_parse_position)))
  7898. {
  7899. return false;
  7900. }
  7901. // get_bson_cstr only appends
  7902. key.clear();
  7903. }
  7904. return true;
  7905. }
  7906. /*!
  7907. @brief Reads an array from the BSON input and passes it to the SAX-parser.
  7908. @return whether a valid BSON-array was passed to the SAX parser
  7909. */
  7910. bool parse_bson_array()
  7911. {
  7912. std::int32_t document_size{};
  7913. get_number<std::int32_t, true>(input_format_t::bson, document_size);
  7914. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
  7915. {
  7916. return false;
  7917. }
  7918. if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/true)))
  7919. {
  7920. return false;
  7921. }
  7922. return sax->end_array();
  7923. }
  7924. //////////
  7925. // CBOR //
  7926. //////////
  7927. /*!
  7928. @param[in] get_char whether a new character should be retrieved from the
  7929. input (true) or whether the last read character should
  7930. be considered instead (false)
  7931. @param[in] tag_handler how CBOR tags should be treated
  7932. @return whether a valid CBOR value was passed to the SAX parser
  7933. */
  7934. bool parse_cbor_internal(const bool get_char,
  7935. const cbor_tag_handler_t tag_handler)
  7936. {
  7937. switch (get_char ? get() : current)
  7938. {
  7939. // EOF
  7940. case std::char_traits<char_type>::eof():
  7941. return unexpect_eof(input_format_t::cbor, "value");
  7942. // Integer 0x00..0x17 (0..23)
  7943. case 0x00:
  7944. case 0x01:
  7945. case 0x02:
  7946. case 0x03:
  7947. case 0x04:
  7948. case 0x05:
  7949. case 0x06:
  7950. case 0x07:
  7951. case 0x08:
  7952. case 0x09:
  7953. case 0x0A:
  7954. case 0x0B:
  7955. case 0x0C:
  7956. case 0x0D:
  7957. case 0x0E:
  7958. case 0x0F:
  7959. case 0x10:
  7960. case 0x11:
  7961. case 0x12:
  7962. case 0x13:
  7963. case 0x14:
  7964. case 0x15:
  7965. case 0x16:
  7966. case 0x17:
  7967. return sax->number_unsigned(static_cast<number_unsigned_t>(current));
  7968. case 0x18: // Unsigned integer (one-byte uint8_t follows)
  7969. {
  7970. std::uint8_t number{};
  7971. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  7972. }
  7973. case 0x19: // Unsigned integer (two-byte uint16_t follows)
  7974. {
  7975. std::uint16_t number{};
  7976. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  7977. }
  7978. case 0x1A: // Unsigned integer (four-byte uint32_t follows)
  7979. {
  7980. std::uint32_t number{};
  7981. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  7982. }
  7983. case 0x1B: // Unsigned integer (eight-byte uint64_t follows)
  7984. {
  7985. std::uint64_t number{};
  7986. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  7987. }
  7988. // Negative integer -1-0x00..-1-0x17 (-1..-24)
  7989. case 0x20:
  7990. case 0x21:
  7991. case 0x22:
  7992. case 0x23:
  7993. case 0x24:
  7994. case 0x25:
  7995. case 0x26:
  7996. case 0x27:
  7997. case 0x28:
  7998. case 0x29:
  7999. case 0x2A:
  8000. case 0x2B:
  8001. case 0x2C:
  8002. case 0x2D:
  8003. case 0x2E:
  8004. case 0x2F:
  8005. case 0x30:
  8006. case 0x31:
  8007. case 0x32:
  8008. case 0x33:
  8009. case 0x34:
  8010. case 0x35:
  8011. case 0x36:
  8012. case 0x37:
  8013. return sax->number_integer(static_cast<std::int8_t>(0x20 - 1 - current));
  8014. case 0x38: // Negative integer (one-byte uint8_t follows)
  8015. {
  8016. std::uint8_t number{};
  8017. return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
  8018. }
  8019. case 0x39: // Negative integer -1-n (two-byte uint16_t follows)
  8020. {
  8021. std::uint16_t number{};
  8022. return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
  8023. }
  8024. case 0x3A: // Negative integer -1-n (four-byte uint32_t follows)
  8025. {
  8026. std::uint32_t number{};
  8027. return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
  8028. }
  8029. case 0x3B: // Negative integer -1-n (eight-byte uint64_t follows)
  8030. {
  8031. std::uint64_t number{};
  8032. return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1)
  8033. - static_cast<number_integer_t>(number));
  8034. }
  8035. // Binary data (0x00..0x17 bytes follow)
  8036. case 0x40:
  8037. case 0x41:
  8038. case 0x42:
  8039. case 0x43:
  8040. case 0x44:
  8041. case 0x45:
  8042. case 0x46:
  8043. case 0x47:
  8044. case 0x48:
  8045. case 0x49:
  8046. case 0x4A:
  8047. case 0x4B:
  8048. case 0x4C:
  8049. case 0x4D:
  8050. case 0x4E:
  8051. case 0x4F:
  8052. case 0x50:
  8053. case 0x51:
  8054. case 0x52:
  8055. case 0x53:
  8056. case 0x54:
  8057. case 0x55:
  8058. case 0x56:
  8059. case 0x57:
  8060. case 0x58: // Binary data (one-byte uint8_t for n follows)
  8061. case 0x59: // Binary data (two-byte uint16_t for n follow)
  8062. case 0x5A: // Binary data (four-byte uint32_t for n follow)
  8063. case 0x5B: // Binary data (eight-byte uint64_t for n follow)
  8064. case 0x5F: // Binary data (indefinite length)
  8065. {
  8066. binary_t b;
  8067. return get_cbor_binary(b) && sax->binary(b);
  8068. }
  8069. // UTF-8 string (0x00..0x17 bytes follow)
  8070. case 0x60:
  8071. case 0x61:
  8072. case 0x62:
  8073. case 0x63:
  8074. case 0x64:
  8075. case 0x65:
  8076. case 0x66:
  8077. case 0x67:
  8078. case 0x68:
  8079. case 0x69:
  8080. case 0x6A:
  8081. case 0x6B:
  8082. case 0x6C:
  8083. case 0x6D:
  8084. case 0x6E:
  8085. case 0x6F:
  8086. case 0x70:
  8087. case 0x71:
  8088. case 0x72:
  8089. case 0x73:
  8090. case 0x74:
  8091. case 0x75:
  8092. case 0x76:
  8093. case 0x77:
  8094. case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
  8095. case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
  8096. case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
  8097. case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
  8098. case 0x7F: // UTF-8 string (indefinite length)
  8099. {
  8100. string_t s;
  8101. return get_cbor_string(s) && sax->string(s);
  8102. }
  8103. // array (0x00..0x17 data items follow)
  8104. case 0x80:
  8105. case 0x81:
  8106. case 0x82:
  8107. case 0x83:
  8108. case 0x84:
  8109. case 0x85:
  8110. case 0x86:
  8111. case 0x87:
  8112. case 0x88:
  8113. case 0x89:
  8114. case 0x8A:
  8115. case 0x8B:
  8116. case 0x8C:
  8117. case 0x8D:
  8118. case 0x8E:
  8119. case 0x8F:
  8120. case 0x90:
  8121. case 0x91:
  8122. case 0x92:
  8123. case 0x93:
  8124. case 0x94:
  8125. case 0x95:
  8126. case 0x96:
  8127. case 0x97:
  8128. return get_cbor_array(
  8129. conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
  8130. case 0x98: // array (one-byte uint8_t for n follows)
  8131. {
  8132. std::uint8_t len{};
  8133. return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
  8134. }
  8135. case 0x99: // array (two-byte uint16_t for n follow)
  8136. {
  8137. std::uint16_t len{};
  8138. return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
  8139. }
  8140. case 0x9A: // array (four-byte uint32_t for n follow)
  8141. {
  8142. std::uint32_t len{};
  8143. return get_number(input_format_t::cbor, len) && get_cbor_array(conditional_static_cast<std::size_t>(len), tag_handler);
  8144. }
  8145. case 0x9B: // array (eight-byte uint64_t for n follow)
  8146. {
  8147. std::uint64_t len{};
  8148. return get_number(input_format_t::cbor, len) && get_cbor_array(conditional_static_cast<std::size_t>(len), tag_handler);
  8149. }
  8150. case 0x9F: // array (indefinite length)
  8151. return get_cbor_array(static_cast<std::size_t>(-1), tag_handler);
  8152. // map (0x00..0x17 pairs of data items follow)
  8153. case 0xA0:
  8154. case 0xA1:
  8155. case 0xA2:
  8156. case 0xA3:
  8157. case 0xA4:
  8158. case 0xA5:
  8159. case 0xA6:
  8160. case 0xA7:
  8161. case 0xA8:
  8162. case 0xA9:
  8163. case 0xAA:
  8164. case 0xAB:
  8165. case 0xAC:
  8166. case 0xAD:
  8167. case 0xAE:
  8168. case 0xAF:
  8169. case 0xB0:
  8170. case 0xB1:
  8171. case 0xB2:
  8172. case 0xB3:
  8173. case 0xB4:
  8174. case 0xB5:
  8175. case 0xB6:
  8176. case 0xB7:
  8177. return get_cbor_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
  8178. case 0xB8: // map (one-byte uint8_t for n follows)
  8179. {
  8180. std::uint8_t len{};
  8181. return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
  8182. }
  8183. case 0xB9: // map (two-byte uint16_t for n follow)
  8184. {
  8185. std::uint16_t len{};
  8186. return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
  8187. }
  8188. case 0xBA: // map (four-byte uint32_t for n follow)
  8189. {
  8190. std::uint32_t len{};
  8191. return get_number(input_format_t::cbor, len) && get_cbor_object(conditional_static_cast<std::size_t>(len), tag_handler);
  8192. }
  8193. case 0xBB: // map (eight-byte uint64_t for n follow)
  8194. {
  8195. std::uint64_t len{};
  8196. return get_number(input_format_t::cbor, len) && get_cbor_object(conditional_static_cast<std::size_t>(len), tag_handler);
  8197. }
  8198. case 0xBF: // map (indefinite length)
  8199. return get_cbor_object(static_cast<std::size_t>(-1), tag_handler);
  8200. case 0xC6: // tagged item
  8201. case 0xC7:
  8202. case 0xC8:
  8203. case 0xC9:
  8204. case 0xCA:
  8205. case 0xCB:
  8206. case 0xCC:
  8207. case 0xCD:
  8208. case 0xCE:
  8209. case 0xCF:
  8210. case 0xD0:
  8211. case 0xD1:
  8212. case 0xD2:
  8213. case 0xD3:
  8214. case 0xD4:
  8215. case 0xD8: // tagged item (1 bytes follow)
  8216. case 0xD9: // tagged item (2 bytes follow)
  8217. case 0xDA: // tagged item (4 bytes follow)
  8218. case 0xDB: // tagged item (8 bytes follow)
  8219. {
  8220. switch (tag_handler)
  8221. {
  8222. case cbor_tag_handler_t::error:
  8223. {
  8224. auto last_token = get_token_string();
  8225. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  8226. exception_message(input_format_t::cbor, concat("invalid byte: 0x", last_token), "value"), nullptr));
  8227. }
  8228. case cbor_tag_handler_t::ignore:
  8229. {
  8230. // ignore binary subtype
  8231. switch (current)
  8232. {
  8233. case 0xD8:
  8234. {
  8235. std::uint8_t subtype_to_ignore{};
  8236. get_number(input_format_t::cbor, subtype_to_ignore);
  8237. break;
  8238. }
  8239. case 0xD9:
  8240. {
  8241. std::uint16_t subtype_to_ignore{};
  8242. get_number(input_format_t::cbor, subtype_to_ignore);
  8243. break;
  8244. }
  8245. case 0xDA:
  8246. {
  8247. std::uint32_t subtype_to_ignore{};
  8248. get_number(input_format_t::cbor, subtype_to_ignore);
  8249. break;
  8250. }
  8251. case 0xDB:
  8252. {
  8253. std::uint64_t subtype_to_ignore{};
  8254. get_number(input_format_t::cbor, subtype_to_ignore);
  8255. break;
  8256. }
  8257. default:
  8258. break;
  8259. }
  8260. return parse_cbor_internal(true, tag_handler);
  8261. }
  8262. case cbor_tag_handler_t::store:
  8263. {
  8264. binary_t b;
  8265. // use binary subtype and store in binary container
  8266. switch (current)
  8267. {
  8268. case 0xD8:
  8269. {
  8270. std::uint8_t subtype{};
  8271. get_number(input_format_t::cbor, subtype);
  8272. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  8273. break;
  8274. }
  8275. case 0xD9:
  8276. {
  8277. std::uint16_t subtype{};
  8278. get_number(input_format_t::cbor, subtype);
  8279. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  8280. break;
  8281. }
  8282. case 0xDA:
  8283. {
  8284. std::uint32_t subtype{};
  8285. get_number(input_format_t::cbor, subtype);
  8286. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  8287. break;
  8288. }
  8289. case 0xDB:
  8290. {
  8291. std::uint64_t subtype{};
  8292. get_number(input_format_t::cbor, subtype);
  8293. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  8294. break;
  8295. }
  8296. default:
  8297. return parse_cbor_internal(true, tag_handler);
  8298. }
  8299. get();
  8300. return get_cbor_binary(b) && sax->binary(b);
  8301. }
  8302. default: // LCOV_EXCL_LINE
  8303. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  8304. return false; // LCOV_EXCL_LINE
  8305. }
  8306. }
  8307. case 0xF4: // false
  8308. return sax->boolean(false);
  8309. case 0xF5: // true
  8310. return sax->boolean(true);
  8311. case 0xF6: // null
  8312. return sax->null();
  8313. case 0xF9: // Half-Precision Float (two-byte IEEE 754)
  8314. {
  8315. const auto byte1_raw = get();
  8316. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number")))
  8317. {
  8318. return false;
  8319. }
  8320. const auto byte2_raw = get();
  8321. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number")))
  8322. {
  8323. return false;
  8324. }
  8325. const auto byte1 = static_cast<unsigned char>(byte1_raw);
  8326. const auto byte2 = static_cast<unsigned char>(byte2_raw);
  8327. // code from RFC 7049, Appendix D, Figure 3:
  8328. // As half-precision floating-point numbers were only added
  8329. // to IEEE 754 in 2008, today's programming platforms often
  8330. // still only have limited support for them. It is very
  8331. // easy to include at least decoding support for them even
  8332. // without such support. An example of a small decoder for
  8333. // half-precision floating-point numbers in the C language
  8334. // is shown in Fig. 3.
  8335. const auto half = static_cast<unsigned int>((byte1 << 8u) + byte2);
  8336. const double val = [&half]
  8337. {
  8338. const int exp = (half >> 10u) & 0x1Fu;
  8339. const unsigned int mant = half & 0x3FFu;
  8340. JSON_ASSERT(0 <= exp&& exp <= 32);
  8341. JSON_ASSERT(mant <= 1024);
  8342. switch (exp)
  8343. {
  8344. case 0:
  8345. return std::ldexp(mant, -24);
  8346. case 31:
  8347. return (mant == 0)
  8348. ? std::numeric_limits<double>::infinity()
  8349. : std::numeric_limits<double>::quiet_NaN();
  8350. default:
  8351. return std::ldexp(mant + 1024, exp - 25);
  8352. }
  8353. }();
  8354. return sax->number_float((half & 0x8000u) != 0
  8355. ? static_cast<number_float_t>(-val)
  8356. : static_cast<number_float_t>(val), "");
  8357. }
  8358. case 0xFA: // Single-Precision Float (four-byte IEEE 754)
  8359. {
  8360. float number{};
  8361. return get_number(input_format_t::cbor, number) && sax->number_float(static_cast<number_float_t>(number), "");
  8362. }
  8363. case 0xFB: // Double-Precision Float (eight-byte IEEE 754)
  8364. {
  8365. double number{};
  8366. return get_number(input_format_t::cbor, number) && sax->number_float(static_cast<number_float_t>(number), "");
  8367. }
  8368. default: // anything else (0xFF is handled inside the other types)
  8369. {
  8370. auto last_token = get_token_string();
  8371. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  8372. exception_message(input_format_t::cbor, concat("invalid byte: 0x", last_token), "value"), nullptr));
  8373. }
  8374. }
  8375. }
  8376. /*!
  8377. @brief reads a CBOR string
  8378. This function first reads starting bytes to determine the expected
  8379. string length and then copies this number of bytes into a string.
  8380. Additionally, CBOR's strings with indefinite lengths are supported.
  8381. @param[out] result created string
  8382. @return whether string creation completed
  8383. */
  8384. bool get_cbor_string(string_t& result)
  8385. {
  8386. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "string")))
  8387. {
  8388. return false;
  8389. }
  8390. switch (current)
  8391. {
  8392. // UTF-8 string (0x00..0x17 bytes follow)
  8393. case 0x60:
  8394. case 0x61:
  8395. case 0x62:
  8396. case 0x63:
  8397. case 0x64:
  8398. case 0x65:
  8399. case 0x66:
  8400. case 0x67:
  8401. case 0x68:
  8402. case 0x69:
  8403. case 0x6A:
  8404. case 0x6B:
  8405. case 0x6C:
  8406. case 0x6D:
  8407. case 0x6E:
  8408. case 0x6F:
  8409. case 0x70:
  8410. case 0x71:
  8411. case 0x72:
  8412. case 0x73:
  8413. case 0x74:
  8414. case 0x75:
  8415. case 0x76:
  8416. case 0x77:
  8417. {
  8418. return get_string(input_format_t::cbor, static_cast<unsigned int>(current) & 0x1Fu, result);
  8419. }
  8420. case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
  8421. {
  8422. std::uint8_t len{};
  8423. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  8424. }
  8425. case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
  8426. {
  8427. std::uint16_t len{};
  8428. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  8429. }
  8430. case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
  8431. {
  8432. std::uint32_t len{};
  8433. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  8434. }
  8435. case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
  8436. {
  8437. std::uint64_t len{};
  8438. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  8439. }
  8440. case 0x7F: // UTF-8 string (indefinite length)
  8441. {
  8442. while (get() != 0xFF)
  8443. {
  8444. string_t chunk;
  8445. if (!get_cbor_string(chunk))
  8446. {
  8447. return false;
  8448. }
  8449. result.append(chunk);
  8450. }
  8451. return true;
  8452. }
  8453. default:
  8454. {
  8455. auto last_token = get_token_string();
  8456. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
  8457. exception_message(input_format_t::cbor, concat("expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x", last_token), "string"), nullptr));
  8458. }
  8459. }
  8460. }
  8461. /*!
  8462. @brief reads a CBOR byte array
  8463. This function first reads starting bytes to determine the expected
  8464. byte array length and then copies this number of bytes into the byte array.
  8465. Additionally, CBOR's byte arrays with indefinite lengths are supported.
  8466. @param[out] result created byte array
  8467. @return whether byte array creation completed
  8468. */
  8469. bool get_cbor_binary(binary_t& result)
  8470. {
  8471. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "binary")))
  8472. {
  8473. return false;
  8474. }
  8475. switch (current)
  8476. {
  8477. // Binary data (0x00..0x17 bytes follow)
  8478. case 0x40:
  8479. case 0x41:
  8480. case 0x42:
  8481. case 0x43:
  8482. case 0x44:
  8483. case 0x45:
  8484. case 0x46:
  8485. case 0x47:
  8486. case 0x48:
  8487. case 0x49:
  8488. case 0x4A:
  8489. case 0x4B:
  8490. case 0x4C:
  8491. case 0x4D:
  8492. case 0x4E:
  8493. case 0x4F:
  8494. case 0x50:
  8495. case 0x51:
  8496. case 0x52:
  8497. case 0x53:
  8498. case 0x54:
  8499. case 0x55:
  8500. case 0x56:
  8501. case 0x57:
  8502. {
  8503. return get_binary(input_format_t::cbor, static_cast<unsigned int>(current) & 0x1Fu, result);
  8504. }
  8505. case 0x58: // Binary data (one-byte uint8_t for n follows)
  8506. {
  8507. std::uint8_t len{};
  8508. return get_number(input_format_t::cbor, len) &&
  8509. get_binary(input_format_t::cbor, len, result);
  8510. }
  8511. case 0x59: // Binary data (two-byte uint16_t for n follow)
  8512. {
  8513. std::uint16_t len{};
  8514. return get_number(input_format_t::cbor, len) &&
  8515. get_binary(input_format_t::cbor, len, result);
  8516. }
  8517. case 0x5A: // Binary data (four-byte uint32_t for n follow)
  8518. {
  8519. std::uint32_t len{};
  8520. return get_number(input_format_t::cbor, len) &&
  8521. get_binary(input_format_t::cbor, len, result);
  8522. }
  8523. case 0x5B: // Binary data (eight-byte uint64_t for n follow)
  8524. {
  8525. std::uint64_t len{};
  8526. return get_number(input_format_t::cbor, len) &&
  8527. get_binary(input_format_t::cbor, len, result);
  8528. }
  8529. case 0x5F: // Binary data (indefinite length)
  8530. {
  8531. while (get() != 0xFF)
  8532. {
  8533. binary_t chunk;
  8534. if (!get_cbor_binary(chunk))
  8535. {
  8536. return false;
  8537. }
  8538. result.insert(result.end(), chunk.begin(), chunk.end());
  8539. }
  8540. return true;
  8541. }
  8542. default:
  8543. {
  8544. auto last_token = get_token_string();
  8545. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
  8546. 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));
  8547. }
  8548. }
  8549. }
  8550. /*!
  8551. @param[in] len the length of the array or static_cast<std::size_t>(-1) for an
  8552. array of indefinite size
  8553. @param[in] tag_handler how CBOR tags should be treated
  8554. @return whether array creation completed
  8555. */
  8556. bool get_cbor_array(const std::size_t len,
  8557. const cbor_tag_handler_t tag_handler)
  8558. {
  8559. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
  8560. {
  8561. return false;
  8562. }
  8563. if (len != static_cast<std::size_t>(-1))
  8564. {
  8565. for (std::size_t i = 0; i < len; ++i)
  8566. {
  8567. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
  8568. {
  8569. return false;
  8570. }
  8571. }
  8572. }
  8573. else
  8574. {
  8575. while (get() != 0xFF)
  8576. {
  8577. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(false, tag_handler)))
  8578. {
  8579. return false;
  8580. }
  8581. }
  8582. }
  8583. return sax->end_array();
  8584. }
  8585. /*!
  8586. @param[in] len the length of the object or static_cast<std::size_t>(-1) for an
  8587. object of indefinite size
  8588. @param[in] tag_handler how CBOR tags should be treated
  8589. @return whether object creation completed
  8590. */
  8591. bool get_cbor_object(const std::size_t len,
  8592. const cbor_tag_handler_t tag_handler)
  8593. {
  8594. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
  8595. {
  8596. return false;
  8597. }
  8598. if (len != 0)
  8599. {
  8600. string_t key;
  8601. if (len != static_cast<std::size_t>(-1))
  8602. {
  8603. for (std::size_t i = 0; i < len; ++i)
  8604. {
  8605. get();
  8606. if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
  8607. {
  8608. return false;
  8609. }
  8610. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
  8611. {
  8612. return false;
  8613. }
  8614. key.clear();
  8615. }
  8616. }
  8617. else
  8618. {
  8619. while (get() != 0xFF)
  8620. {
  8621. if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
  8622. {
  8623. return false;
  8624. }
  8625. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
  8626. {
  8627. return false;
  8628. }
  8629. key.clear();
  8630. }
  8631. }
  8632. }
  8633. return sax->end_object();
  8634. }
  8635. /////////////
  8636. // MsgPack //
  8637. /////////////
  8638. /*!
  8639. @return whether a valid MessagePack value was passed to the SAX parser
  8640. */
  8641. bool parse_msgpack_internal()
  8642. {
  8643. switch (get())
  8644. {
  8645. // EOF
  8646. case std::char_traits<char_type>::eof():
  8647. return unexpect_eof(input_format_t::msgpack, "value");
  8648. // positive fixint
  8649. case 0x00:
  8650. case 0x01:
  8651. case 0x02:
  8652. case 0x03:
  8653. case 0x04:
  8654. case 0x05:
  8655. case 0x06:
  8656. case 0x07:
  8657. case 0x08:
  8658. case 0x09:
  8659. case 0x0A:
  8660. case 0x0B:
  8661. case 0x0C:
  8662. case 0x0D:
  8663. case 0x0E:
  8664. case 0x0F:
  8665. case 0x10:
  8666. case 0x11:
  8667. case 0x12:
  8668. case 0x13:
  8669. case 0x14:
  8670. case 0x15:
  8671. case 0x16:
  8672. case 0x17:
  8673. case 0x18:
  8674. case 0x19:
  8675. case 0x1A:
  8676. case 0x1B:
  8677. case 0x1C:
  8678. case 0x1D:
  8679. case 0x1E:
  8680. case 0x1F:
  8681. case 0x20:
  8682. case 0x21:
  8683. case 0x22:
  8684. case 0x23:
  8685. case 0x24:
  8686. case 0x25:
  8687. case 0x26:
  8688. case 0x27:
  8689. case 0x28:
  8690. case 0x29:
  8691. case 0x2A:
  8692. case 0x2B:
  8693. case 0x2C:
  8694. case 0x2D:
  8695. case 0x2E:
  8696. case 0x2F:
  8697. case 0x30:
  8698. case 0x31:
  8699. case 0x32:
  8700. case 0x33:
  8701. case 0x34:
  8702. case 0x35:
  8703. case 0x36:
  8704. case 0x37:
  8705. case 0x38:
  8706. case 0x39:
  8707. case 0x3A:
  8708. case 0x3B:
  8709. case 0x3C:
  8710. case 0x3D:
  8711. case 0x3E:
  8712. case 0x3F:
  8713. case 0x40:
  8714. case 0x41:
  8715. case 0x42:
  8716. case 0x43:
  8717. case 0x44:
  8718. case 0x45:
  8719. case 0x46:
  8720. case 0x47:
  8721. case 0x48:
  8722. case 0x49:
  8723. case 0x4A:
  8724. case 0x4B:
  8725. case 0x4C:
  8726. case 0x4D:
  8727. case 0x4E:
  8728. case 0x4F:
  8729. case 0x50:
  8730. case 0x51:
  8731. case 0x52:
  8732. case 0x53:
  8733. case 0x54:
  8734. case 0x55:
  8735. case 0x56:
  8736. case 0x57:
  8737. case 0x58:
  8738. case 0x59:
  8739. case 0x5A:
  8740. case 0x5B:
  8741. case 0x5C:
  8742. case 0x5D:
  8743. case 0x5E:
  8744. case 0x5F:
  8745. case 0x60:
  8746. case 0x61:
  8747. case 0x62:
  8748. case 0x63:
  8749. case 0x64:
  8750. case 0x65:
  8751. case 0x66:
  8752. case 0x67:
  8753. case 0x68:
  8754. case 0x69:
  8755. case 0x6A:
  8756. case 0x6B:
  8757. case 0x6C:
  8758. case 0x6D:
  8759. case 0x6E:
  8760. case 0x6F:
  8761. case 0x70:
  8762. case 0x71:
  8763. case 0x72:
  8764. case 0x73:
  8765. case 0x74:
  8766. case 0x75:
  8767. case 0x76:
  8768. case 0x77:
  8769. case 0x78:
  8770. case 0x79:
  8771. case 0x7A:
  8772. case 0x7B:
  8773. case 0x7C:
  8774. case 0x7D:
  8775. case 0x7E:
  8776. case 0x7F:
  8777. return sax->number_unsigned(static_cast<number_unsigned_t>(current));
  8778. // fixmap
  8779. case 0x80:
  8780. case 0x81:
  8781. case 0x82:
  8782. case 0x83:
  8783. case 0x84:
  8784. case 0x85:
  8785. case 0x86:
  8786. case 0x87:
  8787. case 0x88:
  8788. case 0x89:
  8789. case 0x8A:
  8790. case 0x8B:
  8791. case 0x8C:
  8792. case 0x8D:
  8793. case 0x8E:
  8794. case 0x8F:
  8795. return get_msgpack_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
  8796. // fixarray
  8797. case 0x90:
  8798. case 0x91:
  8799. case 0x92:
  8800. case 0x93:
  8801. case 0x94:
  8802. case 0x95:
  8803. case 0x96:
  8804. case 0x97:
  8805. case 0x98:
  8806. case 0x99:
  8807. case 0x9A:
  8808. case 0x9B:
  8809. case 0x9C:
  8810. case 0x9D:
  8811. case 0x9E:
  8812. case 0x9F:
  8813. return get_msgpack_array(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
  8814. // fixstr
  8815. case 0xA0:
  8816. case 0xA1:
  8817. case 0xA2:
  8818. case 0xA3:
  8819. case 0xA4:
  8820. case 0xA5:
  8821. case 0xA6:
  8822. case 0xA7:
  8823. case 0xA8:
  8824. case 0xA9:
  8825. case 0xAA:
  8826. case 0xAB:
  8827. case 0xAC:
  8828. case 0xAD:
  8829. case 0xAE:
  8830. case 0xAF:
  8831. case 0xB0:
  8832. case 0xB1:
  8833. case 0xB2:
  8834. case 0xB3:
  8835. case 0xB4:
  8836. case 0xB5:
  8837. case 0xB6:
  8838. case 0xB7:
  8839. case 0xB8:
  8840. case 0xB9:
  8841. case 0xBA:
  8842. case 0xBB:
  8843. case 0xBC:
  8844. case 0xBD:
  8845. case 0xBE:
  8846. case 0xBF:
  8847. case 0xD9: // str 8
  8848. case 0xDA: // str 16
  8849. case 0xDB: // str 32
  8850. {
  8851. string_t s;
  8852. return get_msgpack_string(s) && sax->string(s);
  8853. }
  8854. case 0xC0: // nil
  8855. return sax->null();
  8856. case 0xC2: // false
  8857. return sax->boolean(false);
  8858. case 0xC3: // true
  8859. return sax->boolean(true);
  8860. case 0xC4: // bin 8
  8861. case 0xC5: // bin 16
  8862. case 0xC6: // bin 32
  8863. case 0xC7: // ext 8
  8864. case 0xC8: // ext 16
  8865. case 0xC9: // ext 32
  8866. case 0xD4: // fixext 1
  8867. case 0xD5: // fixext 2
  8868. case 0xD6: // fixext 4
  8869. case 0xD7: // fixext 8
  8870. case 0xD8: // fixext 16
  8871. {
  8872. binary_t b;
  8873. return get_msgpack_binary(b) && sax->binary(b);
  8874. }
  8875. case 0xCA: // float 32
  8876. {
  8877. float number{};
  8878. return get_number(input_format_t::msgpack, number) && sax->number_float(static_cast<number_float_t>(number), "");
  8879. }
  8880. case 0xCB: // float 64
  8881. {
  8882. double number{};
  8883. return get_number(input_format_t::msgpack, number) && sax->number_float(static_cast<number_float_t>(number), "");
  8884. }
  8885. case 0xCC: // uint 8
  8886. {
  8887. std::uint8_t number{};
  8888. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  8889. }
  8890. case 0xCD: // uint 16
  8891. {
  8892. std::uint16_t number{};
  8893. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  8894. }
  8895. case 0xCE: // uint 32
  8896. {
  8897. std::uint32_t number{};
  8898. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  8899. }
  8900. case 0xCF: // uint 64
  8901. {
  8902. std::uint64_t number{};
  8903. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  8904. }
  8905. case 0xD0: // int 8
  8906. {
  8907. std::int8_t number{};
  8908. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  8909. }
  8910. case 0xD1: // int 16
  8911. {
  8912. std::int16_t number{};
  8913. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  8914. }
  8915. case 0xD2: // int 32
  8916. {
  8917. std::int32_t number{};
  8918. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  8919. }
  8920. case 0xD3: // int 64
  8921. {
  8922. std::int64_t number{};
  8923. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  8924. }
  8925. case 0xDC: // array 16
  8926. {
  8927. std::uint16_t len{};
  8928. return get_number(input_format_t::msgpack, len) && get_msgpack_array(static_cast<std::size_t>(len));
  8929. }
  8930. case 0xDD: // array 32
  8931. {
  8932. std::uint32_t len{};
  8933. return get_number(input_format_t::msgpack, len) && get_msgpack_array(conditional_static_cast<std::size_t>(len));
  8934. }
  8935. case 0xDE: // map 16
  8936. {
  8937. std::uint16_t len{};
  8938. return get_number(input_format_t::msgpack, len) && get_msgpack_object(static_cast<std::size_t>(len));
  8939. }
  8940. case 0xDF: // map 32
  8941. {
  8942. std::uint32_t len{};
  8943. return get_number(input_format_t::msgpack, len) && get_msgpack_object(conditional_static_cast<std::size_t>(len));
  8944. }
  8945. // negative fixint
  8946. case 0xE0:
  8947. case 0xE1:
  8948. case 0xE2:
  8949. case 0xE3:
  8950. case 0xE4:
  8951. case 0xE5:
  8952. case 0xE6:
  8953. case 0xE7:
  8954. case 0xE8:
  8955. case 0xE9:
  8956. case 0xEA:
  8957. case 0xEB:
  8958. case 0xEC:
  8959. case 0xED:
  8960. case 0xEE:
  8961. case 0xEF:
  8962. case 0xF0:
  8963. case 0xF1:
  8964. case 0xF2:
  8965. case 0xF3:
  8966. case 0xF4:
  8967. case 0xF5:
  8968. case 0xF6:
  8969. case 0xF7:
  8970. case 0xF8:
  8971. case 0xF9:
  8972. case 0xFA:
  8973. case 0xFB:
  8974. case 0xFC:
  8975. case 0xFD:
  8976. case 0xFE:
  8977. case 0xFF:
  8978. return sax->number_integer(static_cast<std::int8_t>(current));
  8979. default: // anything else
  8980. {
  8981. auto last_token = get_token_string();
  8982. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  8983. exception_message(input_format_t::msgpack, concat("invalid byte: 0x", last_token), "value"), nullptr));
  8984. }
  8985. }
  8986. }
  8987. /*!
  8988. @brief reads a MessagePack string
  8989. This function first reads starting bytes to determine the expected
  8990. string length and then copies this number of bytes into a string.
  8991. @param[out] result created string
  8992. @return whether string creation completed
  8993. */
  8994. bool get_msgpack_string(string_t& result)
  8995. {
  8996. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::msgpack, "string")))
  8997. {
  8998. return false;
  8999. }
  9000. switch (current)
  9001. {
  9002. // fixstr
  9003. case 0xA0:
  9004. case 0xA1:
  9005. case 0xA2:
  9006. case 0xA3:
  9007. case 0xA4:
  9008. case 0xA5:
  9009. case 0xA6:
  9010. case 0xA7:
  9011. case 0xA8:
  9012. case 0xA9:
  9013. case 0xAA:
  9014. case 0xAB:
  9015. case 0xAC:
  9016. case 0xAD:
  9017. case 0xAE:
  9018. case 0xAF:
  9019. case 0xB0:
  9020. case 0xB1:
  9021. case 0xB2:
  9022. case 0xB3:
  9023. case 0xB4:
  9024. case 0xB5:
  9025. case 0xB6:
  9026. case 0xB7:
  9027. case 0xB8:
  9028. case 0xB9:
  9029. case 0xBA:
  9030. case 0xBB:
  9031. case 0xBC:
  9032. case 0xBD:
  9033. case 0xBE:
  9034. case 0xBF:
  9035. {
  9036. return get_string(input_format_t::msgpack, static_cast<unsigned int>(current) & 0x1Fu, result);
  9037. }
  9038. case 0xD9: // str 8
  9039. {
  9040. std::uint8_t len{};
  9041. return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
  9042. }
  9043. case 0xDA: // str 16
  9044. {
  9045. std::uint16_t len{};
  9046. return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
  9047. }
  9048. case 0xDB: // str 32
  9049. {
  9050. std::uint32_t len{};
  9051. return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
  9052. }
  9053. default:
  9054. {
  9055. auto last_token = get_token_string();
  9056. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
  9057. exception_message(input_format_t::msgpack, concat("expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x", last_token), "string"), nullptr));
  9058. }
  9059. }
  9060. }
  9061. /*!
  9062. @brief reads a MessagePack byte array
  9063. This function first reads starting bytes to determine the expected
  9064. byte array length and then copies this number of bytes into a byte array.
  9065. @param[out] result created byte array
  9066. @return whether byte array creation completed
  9067. */
  9068. bool get_msgpack_binary(binary_t& result)
  9069. {
  9070. // helper function to set the subtype
  9071. auto assign_and_return_true = [&result](std::int8_t subtype)
  9072. {
  9073. result.set_subtype(static_cast<std::uint8_t>(subtype));
  9074. return true;
  9075. };
  9076. switch (current)
  9077. {
  9078. case 0xC4: // bin 8
  9079. {
  9080. std::uint8_t len{};
  9081. return get_number(input_format_t::msgpack, len) &&
  9082. get_binary(input_format_t::msgpack, len, result);
  9083. }
  9084. case 0xC5: // bin 16
  9085. {
  9086. std::uint16_t len{};
  9087. return get_number(input_format_t::msgpack, len) &&
  9088. get_binary(input_format_t::msgpack, len, result);
  9089. }
  9090. case 0xC6: // bin 32
  9091. {
  9092. std::uint32_t len{};
  9093. return get_number(input_format_t::msgpack, len) &&
  9094. get_binary(input_format_t::msgpack, len, result);
  9095. }
  9096. case 0xC7: // ext 8
  9097. {
  9098. std::uint8_t len{};
  9099. std::int8_t subtype{};
  9100. return get_number(input_format_t::msgpack, len) &&
  9101. get_number(input_format_t::msgpack, subtype) &&
  9102. get_binary(input_format_t::msgpack, len, result) &&
  9103. assign_and_return_true(subtype);
  9104. }
  9105. case 0xC8: // ext 16
  9106. {
  9107. std::uint16_t len{};
  9108. std::int8_t subtype{};
  9109. return get_number(input_format_t::msgpack, len) &&
  9110. get_number(input_format_t::msgpack, subtype) &&
  9111. get_binary(input_format_t::msgpack, len, result) &&
  9112. assign_and_return_true(subtype);
  9113. }
  9114. case 0xC9: // ext 32
  9115. {
  9116. std::uint32_t len{};
  9117. std::int8_t subtype{};
  9118. return get_number(input_format_t::msgpack, len) &&
  9119. get_number(input_format_t::msgpack, subtype) &&
  9120. get_binary(input_format_t::msgpack, len, result) &&
  9121. assign_and_return_true(subtype);
  9122. }
  9123. case 0xD4: // fixext 1
  9124. {
  9125. std::int8_t subtype{};
  9126. return get_number(input_format_t::msgpack, subtype) &&
  9127. get_binary(input_format_t::msgpack, 1, result) &&
  9128. assign_and_return_true(subtype);
  9129. }
  9130. case 0xD5: // fixext 2
  9131. {
  9132. std::int8_t subtype{};
  9133. return get_number(input_format_t::msgpack, subtype) &&
  9134. get_binary(input_format_t::msgpack, 2, result) &&
  9135. assign_and_return_true(subtype);
  9136. }
  9137. case 0xD6: // fixext 4
  9138. {
  9139. std::int8_t subtype{};
  9140. return get_number(input_format_t::msgpack, subtype) &&
  9141. get_binary(input_format_t::msgpack, 4, result) &&
  9142. assign_and_return_true(subtype);
  9143. }
  9144. case 0xD7: // fixext 8
  9145. {
  9146. std::int8_t subtype{};
  9147. return get_number(input_format_t::msgpack, subtype) &&
  9148. get_binary(input_format_t::msgpack, 8, result) &&
  9149. assign_and_return_true(subtype);
  9150. }
  9151. case 0xD8: // fixext 16
  9152. {
  9153. std::int8_t subtype{};
  9154. return get_number(input_format_t::msgpack, subtype) &&
  9155. get_binary(input_format_t::msgpack, 16, result) &&
  9156. assign_and_return_true(subtype);
  9157. }
  9158. default: // LCOV_EXCL_LINE
  9159. return false; // LCOV_EXCL_LINE
  9160. }
  9161. }
  9162. /*!
  9163. @param[in] len the length of the array
  9164. @return whether array creation completed
  9165. */
  9166. bool get_msgpack_array(const std::size_t len)
  9167. {
  9168. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
  9169. {
  9170. return false;
  9171. }
  9172. for (std::size_t i = 0; i < len; ++i)
  9173. {
  9174. if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
  9175. {
  9176. return false;
  9177. }
  9178. }
  9179. return sax->end_array();
  9180. }
  9181. /*!
  9182. @param[in] len the length of the object
  9183. @return whether object creation completed
  9184. */
  9185. bool get_msgpack_object(const std::size_t len)
  9186. {
  9187. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
  9188. {
  9189. return false;
  9190. }
  9191. string_t key;
  9192. for (std::size_t i = 0; i < len; ++i)
  9193. {
  9194. get();
  9195. if (JSON_HEDLEY_UNLIKELY(!get_msgpack_string(key) || !sax->key(key)))
  9196. {
  9197. return false;
  9198. }
  9199. if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
  9200. {
  9201. return false;
  9202. }
  9203. key.clear();
  9204. }
  9205. return sax->end_object();
  9206. }
  9207. ////////////
  9208. // UBJSON //
  9209. ////////////
  9210. /*!
  9211. @param[in] get_char whether a new character should be retrieved from the
  9212. input (true, default) or whether the last read
  9213. character should be considered instead
  9214. @return whether a valid UBJSON value was passed to the SAX parser
  9215. */
  9216. bool parse_ubjson_internal(const bool get_char = true)
  9217. {
  9218. return get_ubjson_value(get_char ? get_ignore_noop() : current);
  9219. }
  9220. /*!
  9221. @brief reads a UBJSON string
  9222. This function is either called after reading the 'S' byte explicitly
  9223. indicating a string, or in case of an object key where the 'S' byte can be
  9224. left out.
  9225. @param[out] result created string
  9226. @param[in] get_char whether a new character should be retrieved from the
  9227. input (true, default) or whether the last read
  9228. character should be considered instead
  9229. @return whether string creation completed
  9230. */
  9231. bool get_ubjson_string(string_t& result, const bool get_char = true)
  9232. {
  9233. if (get_char)
  9234. {
  9235. get(); // TODO(niels): may we ignore N here?
  9236. }
  9237. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "value")))
  9238. {
  9239. return false;
  9240. }
  9241. switch (current)
  9242. {
  9243. case 'U':
  9244. {
  9245. std::uint8_t len{};
  9246. return get_number(input_format, len) && get_string(input_format, len, result);
  9247. }
  9248. case 'i':
  9249. {
  9250. std::int8_t len{};
  9251. return get_number(input_format, len) && get_string(input_format, len, result);
  9252. }
  9253. case 'I':
  9254. {
  9255. std::int16_t len{};
  9256. return get_number(input_format, len) && get_string(input_format, len, result);
  9257. }
  9258. case 'l':
  9259. {
  9260. std::int32_t len{};
  9261. return get_number(input_format, len) && get_string(input_format, len, result);
  9262. }
  9263. case 'L':
  9264. {
  9265. std::int64_t len{};
  9266. return get_number(input_format, len) && get_string(input_format, len, result);
  9267. }
  9268. case 'u':
  9269. {
  9270. if (input_format != input_format_t::bjdata)
  9271. {
  9272. break;
  9273. }
  9274. std::uint16_t len{};
  9275. return get_number(input_format, len) && get_string(input_format, len, result);
  9276. }
  9277. case 'm':
  9278. {
  9279. if (input_format != input_format_t::bjdata)
  9280. {
  9281. break;
  9282. }
  9283. std::uint32_t len{};
  9284. return get_number(input_format, len) && get_string(input_format, len, result);
  9285. }
  9286. case 'M':
  9287. {
  9288. if (input_format != input_format_t::bjdata)
  9289. {
  9290. break;
  9291. }
  9292. std::uint64_t len{};
  9293. return get_number(input_format, len) && get_string(input_format, len, result);
  9294. }
  9295. default:
  9296. break;
  9297. }
  9298. auto last_token = get_token_string();
  9299. std::string message;
  9300. if (input_format != input_format_t::bjdata)
  9301. {
  9302. message = "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token;
  9303. }
  9304. else
  9305. {
  9306. message = "expected length type specification (U, i, u, I, m, l, M, L); last byte: 0x" + last_token;
  9307. }
  9308. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "string"), nullptr));
  9309. }
  9310. /*!
  9311. @param[out] dim an integer vector storing the ND array dimensions
  9312. @return whether reading ND array size vector is successful
  9313. */
  9314. bool get_ubjson_ndarray_size(std::vector<size_t>& dim)
  9315. {
  9316. std::pair<std::size_t, char_int_type> size_and_type;
  9317. size_t dimlen = 0;
  9318. bool no_ndarray = true;
  9319. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type, no_ndarray)))
  9320. {
  9321. return false;
  9322. }
  9323. if (size_and_type.first != string_t::npos)
  9324. {
  9325. if (size_and_type.second != 0)
  9326. {
  9327. if (size_and_type.second != 'N')
  9328. {
  9329. for (std::size_t i = 0; i < size_and_type.first; ++i)
  9330. {
  9331. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray, size_and_type.second)))
  9332. {
  9333. return false;
  9334. }
  9335. dim.push_back(dimlen);
  9336. }
  9337. }
  9338. }
  9339. else
  9340. {
  9341. for (std::size_t i = 0; i < size_and_type.first; ++i)
  9342. {
  9343. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray)))
  9344. {
  9345. return false;
  9346. }
  9347. dim.push_back(dimlen);
  9348. }
  9349. }
  9350. }
  9351. else
  9352. {
  9353. while (current != ']')
  9354. {
  9355. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray, current)))
  9356. {
  9357. return false;
  9358. }
  9359. dim.push_back(dimlen);
  9360. get_ignore_noop();
  9361. }
  9362. }
  9363. return true;
  9364. }
  9365. /*!
  9366. @param[out] result determined size
  9367. @param[in,out] is_ndarray for input, `true` means already inside an ndarray vector
  9368. or ndarray dimension is not allowed; `false` means ndarray
  9369. is allowed; for output, `true` means an ndarray is found;
  9370. is_ndarray can only return `true` when its initial value
  9371. is `false`
  9372. @param[in] prefix type marker if already read, otherwise set to 0
  9373. @return whether size determination completed
  9374. */
  9375. bool get_ubjson_size_value(std::size_t& result, bool& is_ndarray, char_int_type prefix = 0)
  9376. {
  9377. if (prefix == 0)
  9378. {
  9379. prefix = get_ignore_noop();
  9380. }
  9381. switch (prefix)
  9382. {
  9383. case 'U':
  9384. {
  9385. std::uint8_t number{};
  9386. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9387. {
  9388. return false;
  9389. }
  9390. result = static_cast<std::size_t>(number);
  9391. return true;
  9392. }
  9393. case 'i':
  9394. {
  9395. std::int8_t number{};
  9396. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9397. {
  9398. return false;
  9399. }
  9400. if (number < 0)
  9401. {
  9402. return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
  9403. exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
  9404. }
  9405. result = static_cast<std::size_t>(number); // NOLINT(bugprone-signed-char-misuse,cert-str34-c): number is not a char
  9406. return true;
  9407. }
  9408. case 'I':
  9409. {
  9410. std::int16_t number{};
  9411. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9412. {
  9413. return false;
  9414. }
  9415. if (number < 0)
  9416. {
  9417. return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
  9418. exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
  9419. }
  9420. result = static_cast<std::size_t>(number);
  9421. return true;
  9422. }
  9423. case 'l':
  9424. {
  9425. std::int32_t number{};
  9426. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9427. {
  9428. return false;
  9429. }
  9430. if (number < 0)
  9431. {
  9432. return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
  9433. exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
  9434. }
  9435. result = static_cast<std::size_t>(number);
  9436. return true;
  9437. }
  9438. case 'L':
  9439. {
  9440. std::int64_t number{};
  9441. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9442. {
  9443. return false;
  9444. }
  9445. if (number < 0)
  9446. {
  9447. return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
  9448. exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
  9449. }
  9450. if (!value_in_range_of<std::size_t>(number))
  9451. {
  9452. return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408,
  9453. exception_message(input_format, "integer value overflow", "size"), nullptr));
  9454. }
  9455. result = static_cast<std::size_t>(number);
  9456. return true;
  9457. }
  9458. case 'u':
  9459. {
  9460. if (input_format != input_format_t::bjdata)
  9461. {
  9462. break;
  9463. }
  9464. std::uint16_t number{};
  9465. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9466. {
  9467. return false;
  9468. }
  9469. result = static_cast<std::size_t>(number);
  9470. return true;
  9471. }
  9472. case 'm':
  9473. {
  9474. if (input_format != input_format_t::bjdata)
  9475. {
  9476. break;
  9477. }
  9478. std::uint32_t number{};
  9479. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9480. {
  9481. return false;
  9482. }
  9483. result = conditional_static_cast<std::size_t>(number);
  9484. return true;
  9485. }
  9486. case 'M':
  9487. {
  9488. if (input_format != input_format_t::bjdata)
  9489. {
  9490. break;
  9491. }
  9492. std::uint64_t number{};
  9493. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
  9494. {
  9495. return false;
  9496. }
  9497. if (!value_in_range_of<std::size_t>(number))
  9498. {
  9499. return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408,
  9500. exception_message(input_format, "integer value overflow", "size"), nullptr));
  9501. }
  9502. result = detail::conditional_static_cast<std::size_t>(number);
  9503. return true;
  9504. }
  9505. case '[':
  9506. {
  9507. if (input_format != input_format_t::bjdata)
  9508. {
  9509. break;
  9510. }
  9511. if (is_ndarray) // ndarray dimensional vector can only contain integers, and can not embed another array
  9512. {
  9513. 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));
  9514. }
  9515. std::vector<size_t> dim;
  9516. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_ndarray_size(dim)))
  9517. {
  9518. return false;
  9519. }
  9520. if (dim.size() == 1 || (dim.size() == 2 && dim.at(0) == 1)) // return normal array size if 1D row vector
  9521. {
  9522. result = dim.at(dim.size() - 1);
  9523. return true;
  9524. }
  9525. if (!dim.empty()) // if ndarray, convert to an object in JData annotated array format
  9526. {
  9527. for (auto i : dim) // test if any dimension in an ndarray is 0, if so, return a 1D empty container
  9528. {
  9529. if ( i == 0 )
  9530. {
  9531. result = 0;
  9532. return true;
  9533. }
  9534. }
  9535. string_t key = "_ArraySize_";
  9536. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(3) || !sax->key(key) || !sax->start_array(dim.size())))
  9537. {
  9538. return false;
  9539. }
  9540. result = 1;
  9541. for (auto i : dim)
  9542. {
  9543. result *= i;
  9544. if (result == 0 || result == string_t::npos) // because dim elements shall not have zeros, result = 0 means overflow happened; it also can't be string_t::npos as it is used to initialize size in get_ubjson_size_type()
  9545. {
  9546. 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));
  9547. }
  9548. if (JSON_HEDLEY_UNLIKELY(!sax->number_unsigned(static_cast<number_unsigned_t>(i))))
  9549. {
  9550. return false;
  9551. }
  9552. }
  9553. is_ndarray = true;
  9554. return sax->end_array();
  9555. }
  9556. result = 0;
  9557. return true;
  9558. }
  9559. default:
  9560. break;
  9561. }
  9562. auto last_token = get_token_string();
  9563. std::string message;
  9564. if (input_format != input_format_t::bjdata)
  9565. {
  9566. message = "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token;
  9567. }
  9568. else
  9569. {
  9570. message = "expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x" + last_token;
  9571. }
  9572. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "size"), nullptr));
  9573. }
  9574. /*!
  9575. @brief determine the type and size for a container
  9576. In the optimized UBJSON format, a type and a size can be provided to allow
  9577. for a more compact representation.
  9578. @param[out] result pair of the size and the type
  9579. @param[in] inside_ndarray whether the parser is parsing an ND array dimensional vector
  9580. @return whether pair creation completed
  9581. */
  9582. bool get_ubjson_size_type(std::pair<std::size_t, char_int_type>& result, bool inside_ndarray = false)
  9583. {
  9584. result.first = string_t::npos; // size
  9585. result.second = 0; // type
  9586. bool is_ndarray = false;
  9587. get_ignore_noop();
  9588. if (current == '$')
  9589. {
  9590. std::vector<char_int_type> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type
  9591. result.second = get(); // must not ignore 'N', because 'N' maybe the type
  9592. if (JSON_HEDLEY_UNLIKELY( input_format == input_format_t::bjdata && std::find(bjdx.begin(), bjdx.end(), result.second) != bjdx.end() ))
  9593. {
  9594. auto last_token = get_token_string();
  9595. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  9596. exception_message(input_format, concat("marker 0x", last_token, " is not a permitted optimized array type"), "type"), nullptr));
  9597. }
  9598. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "type")))
  9599. {
  9600. return false;
  9601. }
  9602. get_ignore_noop();
  9603. if (JSON_HEDLEY_UNLIKELY(current != '#'))
  9604. {
  9605. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "value")))
  9606. {
  9607. return false;
  9608. }
  9609. auto last_token = get_token_string();
  9610. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  9611. exception_message(input_format, concat("expected '#' after type information; last byte: 0x", last_token), "size"), nullptr));
  9612. }
  9613. bool is_error = get_ubjson_size_value(result.first, is_ndarray);
  9614. if (input_format == input_format_t::bjdata && is_ndarray)
  9615. {
  9616. if (inside_ndarray)
  9617. {
  9618. return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
  9619. exception_message(input_format, "ndarray can not be recursive", "size"), nullptr));
  9620. }
  9621. result.second |= (1 << 8); // use bit 8 to indicate ndarray, all UBJSON and BJData markers should be ASCII letters
  9622. }
  9623. return is_error;
  9624. }
  9625. if (current == '#')
  9626. {
  9627. bool is_error = get_ubjson_size_value(result.first, is_ndarray);
  9628. if (input_format == input_format_t::bjdata && is_ndarray)
  9629. {
  9630. return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
  9631. exception_message(input_format, "ndarray requires both type and size", "size"), nullptr));
  9632. }
  9633. return is_error;
  9634. }
  9635. return true;
  9636. }
  9637. /*!
  9638. @param prefix the previously read or set type prefix
  9639. @return whether value creation completed
  9640. */
  9641. bool get_ubjson_value(const char_int_type prefix)
  9642. {
  9643. switch (prefix)
  9644. {
  9645. case std::char_traits<char_type>::eof(): // EOF
  9646. return unexpect_eof(input_format, "value");
  9647. case 'T': // true
  9648. return sax->boolean(true);
  9649. case 'F': // false
  9650. return sax->boolean(false);
  9651. case 'Z': // null
  9652. return sax->null();
  9653. case 'U':
  9654. {
  9655. std::uint8_t number{};
  9656. return get_number(input_format, number) && sax->number_unsigned(number);
  9657. }
  9658. case 'i':
  9659. {
  9660. std::int8_t number{};
  9661. return get_number(input_format, number) && sax->number_integer(number);
  9662. }
  9663. case 'I':
  9664. {
  9665. std::int16_t number{};
  9666. return get_number(input_format, number) && sax->number_integer(number);
  9667. }
  9668. case 'l':
  9669. {
  9670. std::int32_t number{};
  9671. return get_number(input_format, number) && sax->number_integer(number);
  9672. }
  9673. case 'L':
  9674. {
  9675. std::int64_t number{};
  9676. return get_number(input_format, number) && sax->number_integer(number);
  9677. }
  9678. case 'u':
  9679. {
  9680. if (input_format != input_format_t::bjdata)
  9681. {
  9682. break;
  9683. }
  9684. std::uint16_t number{};
  9685. return get_number(input_format, number) && sax->number_unsigned(number);
  9686. }
  9687. case 'm':
  9688. {
  9689. if (input_format != input_format_t::bjdata)
  9690. {
  9691. break;
  9692. }
  9693. std::uint32_t number{};
  9694. return get_number(input_format, number) && sax->number_unsigned(number);
  9695. }
  9696. case 'M':
  9697. {
  9698. if (input_format != input_format_t::bjdata)
  9699. {
  9700. break;
  9701. }
  9702. std::uint64_t number{};
  9703. return get_number(input_format, number) && sax->number_unsigned(number);
  9704. }
  9705. case 'h':
  9706. {
  9707. if (input_format != input_format_t::bjdata)
  9708. {
  9709. break;
  9710. }
  9711. const auto byte1_raw = get();
  9712. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
  9713. {
  9714. return false;
  9715. }
  9716. const auto byte2_raw = get();
  9717. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
  9718. {
  9719. return false;
  9720. }
  9721. const auto byte1 = static_cast<unsigned char>(byte1_raw);
  9722. const auto byte2 = static_cast<unsigned char>(byte2_raw);
  9723. // code from RFC 7049, Appendix D, Figure 3:
  9724. // As half-precision floating-point numbers were only added
  9725. // to IEEE 754 in 2008, today's programming platforms often
  9726. // still only have limited support for them. It is very
  9727. // easy to include at least decoding support for them even
  9728. // without such support. An example of a small decoder for
  9729. // half-precision floating-point numbers in the C language
  9730. // is shown in Fig. 3.
  9731. const auto half = static_cast<unsigned int>((byte2 << 8u) + byte1);
  9732. const double val = [&half]
  9733. {
  9734. const int exp = (half >> 10u) & 0x1Fu;
  9735. const unsigned int mant = half & 0x3FFu;
  9736. JSON_ASSERT(0 <= exp&& exp <= 32);
  9737. JSON_ASSERT(mant <= 1024);
  9738. switch (exp)
  9739. {
  9740. case 0:
  9741. return std::ldexp(mant, -24);
  9742. case 31:
  9743. return (mant == 0)
  9744. ? std::numeric_limits<double>::infinity()
  9745. : std::numeric_limits<double>::quiet_NaN();
  9746. default:
  9747. return std::ldexp(mant + 1024, exp - 25);
  9748. }
  9749. }();
  9750. return sax->number_float((half & 0x8000u) != 0
  9751. ? static_cast<number_float_t>(-val)
  9752. : static_cast<number_float_t>(val), "");
  9753. }
  9754. case 'd':
  9755. {
  9756. float number{};
  9757. return get_number(input_format, number) && sax->number_float(static_cast<number_float_t>(number), "");
  9758. }
  9759. case 'D':
  9760. {
  9761. double number{};
  9762. return get_number(input_format, number) && sax->number_float(static_cast<number_float_t>(number), "");
  9763. }
  9764. case 'H':
  9765. {
  9766. return get_ubjson_high_precision_number();
  9767. }
  9768. case 'C': // char
  9769. {
  9770. get();
  9771. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "char")))
  9772. {
  9773. return false;
  9774. }
  9775. if (JSON_HEDLEY_UNLIKELY(current > 127))
  9776. {
  9777. auto last_token = get_token_string();
  9778. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
  9779. exception_message(input_format, concat("byte after 'C' must be in range 0x00..0x7F; last byte: 0x", last_token), "char"), nullptr));
  9780. }
  9781. string_t s(1, static_cast<typename string_t::value_type>(current));
  9782. return sax->string(s);
  9783. }
  9784. case 'S': // string
  9785. {
  9786. string_t s;
  9787. return get_ubjson_string(s) && sax->string(s);
  9788. }
  9789. case '[': // array
  9790. return get_ubjson_array();
  9791. case '{': // object
  9792. return get_ubjson_object();
  9793. default: // anything else
  9794. break;
  9795. }
  9796. auto last_token = get_token_string();
  9797. 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));
  9798. }
  9799. /*!
  9800. @return whether array creation completed
  9801. */
  9802. bool get_ubjson_array()
  9803. {
  9804. std::pair<std::size_t, char_int_type> size_and_type;
  9805. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type)))
  9806. {
  9807. return false;
  9808. }
  9809. // 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):
  9810. // {"_ArrayType_" : "typeid", "_ArraySize_" : [n1, n2, ...], "_ArrayData_" : [v1, v2, ...]}
  9811. if (input_format == input_format_t::bjdata && size_and_type.first != string_t::npos && (size_and_type.second & (1 << 8)) != 0)
  9812. {
  9813. std::map<char_int_type, string_t> bjdtype = {{'U', "uint8"}, {'i', "int8"}, {'u', "uint16"}, {'I', "int16"},
  9814. {'m', "uint32"}, {'l', "int32"}, {'M', "uint64"}, {'L', "int64"}, {'d', "single"}, {'D', "double"}, {'C', "char"}
  9815. };
  9816. 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
  9817. string_t key = "_ArrayType_";
  9818. if (JSON_HEDLEY_UNLIKELY(bjdtype.count(size_and_type.second) == 0))
  9819. {
  9820. auto last_token = get_token_string();
  9821. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  9822. exception_message(input_format, "invalid byte: 0x" + last_token, "type"), nullptr));
  9823. }
  9824. if (JSON_HEDLEY_UNLIKELY(!sax->key(key) || !sax->string(bjdtype[size_and_type.second]) ))
  9825. {
  9826. return false;
  9827. }
  9828. if (size_and_type.second == 'C')
  9829. {
  9830. size_and_type.second = 'U';
  9831. }
  9832. key = "_ArrayData_";
  9833. if (JSON_HEDLEY_UNLIKELY(!sax->key(key) || !sax->start_array(size_and_type.first) ))
  9834. {
  9835. return false;
  9836. }
  9837. for (std::size_t i = 0; i < size_and_type.first; ++i)
  9838. {
  9839. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
  9840. {
  9841. return false;
  9842. }
  9843. }
  9844. return (sax->end_array() && sax->end_object());
  9845. }
  9846. if (size_and_type.first != string_t::npos)
  9847. {
  9848. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(size_and_type.first)))
  9849. {
  9850. return false;
  9851. }
  9852. if (size_and_type.second != 0)
  9853. {
  9854. if (size_and_type.second != 'N')
  9855. {
  9856. for (std::size_t i = 0; i < size_and_type.first; ++i)
  9857. {
  9858. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
  9859. {
  9860. return false;
  9861. }
  9862. }
  9863. }
  9864. }
  9865. else
  9866. {
  9867. for (std::size_t i = 0; i < size_and_type.first; ++i)
  9868. {
  9869. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
  9870. {
  9871. return false;
  9872. }
  9873. }
  9874. }
  9875. }
  9876. else
  9877. {
  9878. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
  9879. {
  9880. return false;
  9881. }
  9882. while (current != ']')
  9883. {
  9884. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal(false)))
  9885. {
  9886. return false;
  9887. }
  9888. get_ignore_noop();
  9889. }
  9890. }
  9891. return sax->end_array();
  9892. }
  9893. /*!
  9894. @return whether object creation completed
  9895. */
  9896. bool get_ubjson_object()
  9897. {
  9898. std::pair<std::size_t, char_int_type> size_and_type;
  9899. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type)))
  9900. {
  9901. return false;
  9902. }
  9903. // do not accept ND-array size in objects in BJData
  9904. if (input_format == input_format_t::bjdata && size_and_type.first != string_t::npos && (size_and_type.second & (1 << 8)) != 0)
  9905. {
  9906. auto last_token = get_token_string();
  9907. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
  9908. exception_message(input_format, "BJData object does not support ND-array size in optimized format", "object"), nullptr));
  9909. }
  9910. string_t key;
  9911. if (size_and_type.first != string_t::npos)
  9912. {
  9913. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(size_and_type.first)))
  9914. {
  9915. return false;
  9916. }
  9917. if (size_and_type.second != 0)
  9918. {
  9919. for (std::size_t i = 0; i < size_and_type.first; ++i)
  9920. {
  9921. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
  9922. {
  9923. return false;
  9924. }
  9925. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
  9926. {
  9927. return false;
  9928. }
  9929. key.clear();
  9930. }
  9931. }
  9932. else
  9933. {
  9934. for (std::size_t i = 0; i < size_and_type.first; ++i)
  9935. {
  9936. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
  9937. {
  9938. return false;
  9939. }
  9940. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
  9941. {
  9942. return false;
  9943. }
  9944. key.clear();
  9945. }
  9946. }
  9947. }
  9948. else
  9949. {
  9950. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast<std::size_t>(-1))))
  9951. {
  9952. return false;
  9953. }
  9954. while (current != '}')
  9955. {
  9956. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key, false) || !sax->key(key)))
  9957. {
  9958. return false;
  9959. }
  9960. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
  9961. {
  9962. return false;
  9963. }
  9964. get_ignore_noop();
  9965. key.clear();
  9966. }
  9967. }
  9968. return sax->end_object();
  9969. }
  9970. // Note, no reader for UBJSON binary types is implemented because they do
  9971. // not exist
  9972. bool get_ubjson_high_precision_number()
  9973. {
  9974. // get size of following number string
  9975. std::size_t size{};
  9976. bool no_ndarray = true;
  9977. auto res = get_ubjson_size_value(size, no_ndarray);
  9978. if (JSON_HEDLEY_UNLIKELY(!res))
  9979. {
  9980. return res;
  9981. }
  9982. // get number string
  9983. std::vector<char> number_vector;
  9984. for (std::size_t i = 0; i < size; ++i)
  9985. {
  9986. get();
  9987. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
  9988. {
  9989. return false;
  9990. }
  9991. number_vector.push_back(static_cast<char>(current));
  9992. }
  9993. // parse number string
  9994. using ia_type = decltype(detail::input_adapter(number_vector));
  9995. auto number_lexer = detail::lexer<BasicJsonType, ia_type>(detail::input_adapter(number_vector), false);
  9996. const auto result_number = number_lexer.scan();
  9997. const auto number_string = number_lexer.get_token_string();
  9998. const auto result_remainder = number_lexer.scan();
  9999. using token_type = typename detail::lexer_base<BasicJsonType>::token_type;
  10000. if (JSON_HEDLEY_UNLIKELY(result_remainder != token_type::end_of_input))
  10001. {
  10002. return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read,
  10003. exception_message(input_format, concat("invalid number text: ", number_lexer.get_token_string()), "high-precision number"), nullptr));
  10004. }
  10005. switch (result_number)
  10006. {
  10007. case token_type::value_integer:
  10008. return sax->number_integer(number_lexer.get_number_integer());
  10009. case token_type::value_unsigned:
  10010. return sax->number_unsigned(number_lexer.get_number_unsigned());
  10011. case token_type::value_float:
  10012. return sax->number_float(number_lexer.get_number_float(), std::move(number_string));
  10013. case token_type::uninitialized:
  10014. case token_type::literal_true:
  10015. case token_type::literal_false:
  10016. case token_type::literal_null:
  10017. case token_type::value_string:
  10018. case token_type::begin_array:
  10019. case token_type::begin_object:
  10020. case token_type::end_array:
  10021. case token_type::end_object:
  10022. case token_type::name_separator:
  10023. case token_type::value_separator:
  10024. case token_type::parse_error:
  10025. case token_type::end_of_input:
  10026. case token_type::literal_or_value:
  10027. default:
  10028. return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read,
  10029. exception_message(input_format, concat("invalid number text: ", number_lexer.get_token_string()), "high-precision number"), nullptr));
  10030. }
  10031. }
  10032. ///////////////////////
  10033. // Utility functions //
  10034. ///////////////////////
  10035. /*!
  10036. @brief get next character from the input
  10037. This function provides the interface to the used input adapter. It does
  10038. not throw in case the input reached EOF, but returns a -'ve valued
  10039. `std::char_traits<char_type>::eof()` in that case.
  10040. @return character read from the input
  10041. */
  10042. char_int_type get()
  10043. {
  10044. ++chars_read;
  10045. return current = ia.get_character();
  10046. }
  10047. /*!
  10048. @return character read from the input after ignoring all 'N' entries
  10049. */
  10050. char_int_type get_ignore_noop()
  10051. {
  10052. do
  10053. {
  10054. get();
  10055. }
  10056. while (current == 'N');
  10057. return current;
  10058. }
  10059. /*
  10060. @brief read a number from the input
  10061. @tparam NumberType the type of the number
  10062. @param[in] format the current format (for diagnostics)
  10063. @param[out] result number of type @a NumberType
  10064. @return whether conversion completed
  10065. @note This function needs to respect the system's endianness, because
  10066. bytes in CBOR, MessagePack, and UBJSON are stored in network order
  10067. (big endian) and therefore need reordering on little endian systems.
  10068. On the other hand, BSON and BJData use little endian and should reorder
  10069. on big endian systems.
  10070. */
  10071. template<typename NumberType, bool InputIsLittleEndian = false>
  10072. bool get_number(const input_format_t format, NumberType& result)
  10073. {
  10074. // step 1: read input into array with system's byte order
  10075. std::array<std::uint8_t, sizeof(NumberType)> vec{};
  10076. for (std::size_t i = 0; i < sizeof(NumberType); ++i)
  10077. {
  10078. get();
  10079. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "number")))
  10080. {
  10081. return false;
  10082. }
  10083. // reverse byte order prior to conversion if necessary
  10084. if (is_little_endian != (InputIsLittleEndian || format == input_format_t::bjdata))
  10085. {
  10086. vec[sizeof(NumberType) - i - 1] = static_cast<std::uint8_t>(current);
  10087. }
  10088. else
  10089. {
  10090. vec[i] = static_cast<std::uint8_t>(current); // LCOV_EXCL_LINE
  10091. }
  10092. }
  10093. // step 2: convert array into number of type T and return
  10094. std::memcpy(&result, vec.data(), sizeof(NumberType));
  10095. return true;
  10096. }
  10097. /*!
  10098. @brief create a string by reading characters from the input
  10099. @tparam NumberType the type of the number
  10100. @param[in] format the current format (for diagnostics)
  10101. @param[in] len number of characters to read
  10102. @param[out] result string created by reading @a len bytes
  10103. @return whether string creation completed
  10104. @note We can not reserve @a len bytes for the result, because @a len
  10105. may be too large. Usually, @ref unexpect_eof() detects the end of
  10106. the input before we run out of string memory.
  10107. */
  10108. template<typename NumberType>
  10109. bool get_string(const input_format_t format,
  10110. const NumberType len,
  10111. string_t& result)
  10112. {
  10113. bool success = true;
  10114. for (NumberType i = 0; i < len; i++)
  10115. {
  10116. get();
  10117. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "string")))
  10118. {
  10119. success = false;
  10120. break;
  10121. }
  10122. result.push_back(static_cast<typename string_t::value_type>(current));
  10123. }
  10124. return success;
  10125. }
  10126. /*!
  10127. @brief create a byte array by reading bytes from the input
  10128. @tparam NumberType the type of the number
  10129. @param[in] format the current format (for diagnostics)
  10130. @param[in] len number of bytes to read
  10131. @param[out] result byte array created by reading @a len bytes
  10132. @return whether byte array creation completed
  10133. @note We can not reserve @a len bytes for the result, because @a len
  10134. may be too large. Usually, @ref unexpect_eof() detects the end of
  10135. the input before we run out of memory.
  10136. */
  10137. template<typename NumberType>
  10138. bool get_binary(const input_format_t format,
  10139. const NumberType len,
  10140. binary_t& result)
  10141. {
  10142. bool success = true;
  10143. for (NumberType i = 0; i < len; i++)
  10144. {
  10145. get();
  10146. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "binary")))
  10147. {
  10148. success = false;
  10149. break;
  10150. }
  10151. result.push_back(static_cast<std::uint8_t>(current));
  10152. }
  10153. return success;
  10154. }
  10155. /*!
  10156. @param[in] format the current format (for diagnostics)
  10157. @param[in] context further context information (for diagnostics)
  10158. @return whether the last read character is not EOF
  10159. */
  10160. JSON_HEDLEY_NON_NULL(3)
  10161. bool unexpect_eof(const input_format_t format, const char* context) const
  10162. {
  10163. if (JSON_HEDLEY_UNLIKELY(current == std::char_traits<char_type>::eof()))
  10164. {
  10165. return sax->parse_error(chars_read, "<end of file>",
  10166. parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), nullptr));
  10167. }
  10168. return true;
  10169. }
  10170. /*!
  10171. @return a string representation of the last read byte
  10172. */
  10173. std::string get_token_string() const
  10174. {
  10175. std::array<char, 3> cr{{}};
  10176. static_cast<void>((std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(current))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  10177. return std::string{cr.data()};
  10178. }
  10179. /*!
  10180. @param[in] format the current format
  10181. @param[in] detail a detailed error message
  10182. @param[in] context further context information
  10183. @return a message string to use in the parse_error exceptions
  10184. */
  10185. std::string exception_message(const input_format_t format,
  10186. const std::string& detail,
  10187. const std::string& context) const
  10188. {
  10189. std::string error_msg = "syntax error while parsing ";
  10190. switch (format)
  10191. {
  10192. case input_format_t::cbor:
  10193. error_msg += "CBOR";
  10194. break;
  10195. case input_format_t::msgpack:
  10196. error_msg += "MessagePack";
  10197. break;
  10198. case input_format_t::ubjson:
  10199. error_msg += "UBJSON";
  10200. break;
  10201. case input_format_t::bson:
  10202. error_msg += "BSON";
  10203. break;
  10204. case input_format_t::bjdata:
  10205. error_msg += "BJData";
  10206. break;
  10207. case input_format_t::json: // LCOV_EXCL_LINE
  10208. default: // LCOV_EXCL_LINE
  10209. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  10210. }
  10211. return concat(error_msg, ' ', context, ": ", detail);
  10212. }
  10213. private:
  10214. /// input adapter
  10215. InputAdapterType ia;
  10216. /// the current character
  10217. char_int_type current = std::char_traits<char_type>::eof();
  10218. /// the number of characters read
  10219. std::size_t chars_read = 0;
  10220. /// whether we can assume little endianness
  10221. const bool is_little_endian = little_endianness();
  10222. /// input format
  10223. const input_format_t input_format = input_format_t::json;
  10224. /// the SAX parser
  10225. json_sax_t* sax = nullptr;
  10226. };
  10227. } // namespace detail
  10228. } // namespace nlohmann
  10229. // #include <nlohmann/detail/input/input_adapters.hpp>
  10230. // #include <nlohmann/detail/input/lexer.hpp>
  10231. // #include <nlohmann/detail/input/parser.hpp>
  10232. #include <cmath> // isfinite
  10233. #include <cstdint> // uint8_t
  10234. #include <functional> // function
  10235. #include <string> // string
  10236. #include <utility> // move
  10237. #include <vector> // vector
  10238. // #include <nlohmann/detail/exceptions.hpp>
  10239. // #include <nlohmann/detail/input/input_adapters.hpp>
  10240. // #include <nlohmann/detail/input/json_sax.hpp>
  10241. // #include <nlohmann/detail/input/lexer.hpp>
  10242. // #include <nlohmann/detail/macro_scope.hpp>
  10243. // #include <nlohmann/detail/meta/is_sax.hpp>
  10244. // #include <nlohmann/detail/string_concat.hpp>
  10245. // #include <nlohmann/detail/value_t.hpp>
  10246. namespace nlohmann
  10247. {
  10248. namespace detail
  10249. {
  10250. ////////////
  10251. // parser //
  10252. ////////////
  10253. enum class parse_event_t : std::uint8_t
  10254. {
  10255. /// the parser read `{` and started to process a JSON object
  10256. object_start,
  10257. /// the parser read `}` and finished processing a JSON object
  10258. object_end,
  10259. /// the parser read `[` and started to process a JSON array
  10260. array_start,
  10261. /// the parser read `]` and finished processing a JSON array
  10262. array_end,
  10263. /// the parser read a key of a value in an object
  10264. key,
  10265. /// the parser finished reading a JSON value
  10266. value
  10267. };
  10268. template<typename BasicJsonType>
  10269. using parser_callback_t =
  10270. std::function<bool(int /*depth*/, parse_event_t /*event*/, BasicJsonType& /*parsed*/)>;
  10271. /*!
  10272. @brief syntax analysis
  10273. This class implements a recursive descent parser.
  10274. */
  10275. template<typename BasicJsonType, typename InputAdapterType>
  10276. class parser
  10277. {
  10278. using number_integer_t = typename BasicJsonType::number_integer_t;
  10279. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  10280. using number_float_t = typename BasicJsonType::number_float_t;
  10281. using string_t = typename BasicJsonType::string_t;
  10282. using lexer_t = lexer<BasicJsonType, InputAdapterType>;
  10283. using token_type = typename lexer_t::token_type;
  10284. public:
  10285. /// a parser reading from an input adapter
  10286. explicit parser(InputAdapterType&& adapter,
  10287. const parser_callback_t<BasicJsonType> cb = nullptr,
  10288. const bool allow_exceptions_ = true,
  10289. const bool skip_comments = false)
  10290. : callback(cb)
  10291. , m_lexer(std::move(adapter), skip_comments)
  10292. , allow_exceptions(allow_exceptions_)
  10293. {
  10294. // read first token
  10295. get_token();
  10296. }
  10297. /*!
  10298. @brief public parser interface
  10299. @param[in] strict whether to expect the last token to be EOF
  10300. @param[in,out] result parsed JSON value
  10301. @throw parse_error.101 in case of an unexpected token
  10302. @throw parse_error.102 if to_unicode fails or surrogate error
  10303. @throw parse_error.103 if to_unicode fails
  10304. */
  10305. void parse(const bool strict, BasicJsonType& result)
  10306. {
  10307. if (callback)
  10308. {
  10309. json_sax_dom_callback_parser<BasicJsonType> sdp(result, callback, allow_exceptions);
  10310. sax_parse_internal(&sdp);
  10311. // in strict mode, input must be completely read
  10312. if (strict && (get_token() != token_type::end_of_input))
  10313. {
  10314. sdp.parse_error(m_lexer.get_position(),
  10315. m_lexer.get_token_string(),
  10316. parse_error::create(101, m_lexer.get_position(),
  10317. exception_message(token_type::end_of_input, "value"), nullptr));
  10318. }
  10319. // in case of an error, return discarded value
  10320. if (sdp.is_errored())
  10321. {
  10322. result = value_t::discarded;
  10323. return;
  10324. }
  10325. // set top-level value to null if it was discarded by the callback
  10326. // function
  10327. if (result.is_discarded())
  10328. {
  10329. result = nullptr;
  10330. }
  10331. }
  10332. else
  10333. {
  10334. json_sax_dom_parser<BasicJsonType> sdp(result, allow_exceptions);
  10335. sax_parse_internal(&sdp);
  10336. // in strict mode, input must be completely read
  10337. if (strict && (get_token() != token_type::end_of_input))
  10338. {
  10339. sdp.parse_error(m_lexer.get_position(),
  10340. m_lexer.get_token_string(),
  10341. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), nullptr));
  10342. }
  10343. // in case of an error, return discarded value
  10344. if (sdp.is_errored())
  10345. {
  10346. result = value_t::discarded;
  10347. return;
  10348. }
  10349. }
  10350. result.assert_invariant();
  10351. }
  10352. /*!
  10353. @brief public accept interface
  10354. @param[in] strict whether to expect the last token to be EOF
  10355. @return whether the input is a proper JSON text
  10356. */
  10357. bool accept(const bool strict = true)
  10358. {
  10359. json_sax_acceptor<BasicJsonType> sax_acceptor;
  10360. return sax_parse(&sax_acceptor, strict);
  10361. }
  10362. template<typename SAX>
  10363. JSON_HEDLEY_NON_NULL(2)
  10364. bool sax_parse(SAX* sax, const bool strict = true)
  10365. {
  10366. (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
  10367. const bool result = sax_parse_internal(sax);
  10368. // strict mode: next byte must be EOF
  10369. if (result && strict && (get_token() != token_type::end_of_input))
  10370. {
  10371. return sax->parse_error(m_lexer.get_position(),
  10372. m_lexer.get_token_string(),
  10373. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), nullptr));
  10374. }
  10375. return result;
  10376. }
  10377. private:
  10378. template<typename SAX>
  10379. JSON_HEDLEY_NON_NULL(2)
  10380. bool sax_parse_internal(SAX* sax)
  10381. {
  10382. // stack to remember the hierarchy of structured values we are parsing
  10383. // true = array; false = object
  10384. std::vector<bool> states;
  10385. // value to avoid a goto (see comment where set to true)
  10386. bool skip_to_state_evaluation = false;
  10387. while (true)
  10388. {
  10389. if (!skip_to_state_evaluation)
  10390. {
  10391. // invariant: get_token() was called before each iteration
  10392. switch (last_token)
  10393. {
  10394. case token_type::begin_object:
  10395. {
  10396. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast<std::size_t>(-1))))
  10397. {
  10398. return false;
  10399. }
  10400. // closing } -> we are done
  10401. if (get_token() == token_type::end_object)
  10402. {
  10403. if (JSON_HEDLEY_UNLIKELY(!sax->end_object()))
  10404. {
  10405. return false;
  10406. }
  10407. break;
  10408. }
  10409. // parse key
  10410. if (JSON_HEDLEY_UNLIKELY(last_token != token_type::value_string))
  10411. {
  10412. return sax->parse_error(m_lexer.get_position(),
  10413. m_lexer.get_token_string(),
  10414. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), nullptr));
  10415. }
  10416. if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string())))
  10417. {
  10418. return false;
  10419. }
  10420. // parse separator (:)
  10421. if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
  10422. {
  10423. return sax->parse_error(m_lexer.get_position(),
  10424. m_lexer.get_token_string(),
  10425. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), nullptr));
  10426. }
  10427. // remember we are now inside an object
  10428. states.push_back(false);
  10429. // parse values
  10430. get_token();
  10431. continue;
  10432. }
  10433. case token_type::begin_array:
  10434. {
  10435. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
  10436. {
  10437. return false;
  10438. }
  10439. // closing ] -> we are done
  10440. if (get_token() == token_type::end_array)
  10441. {
  10442. if (JSON_HEDLEY_UNLIKELY(!sax->end_array()))
  10443. {
  10444. return false;
  10445. }
  10446. break;
  10447. }
  10448. // remember we are now inside an array
  10449. states.push_back(true);
  10450. // parse values (no need to call get_token)
  10451. continue;
  10452. }
  10453. case token_type::value_float:
  10454. {
  10455. const auto res = m_lexer.get_number_float();
  10456. if (JSON_HEDLEY_UNLIKELY(!std::isfinite(res)))
  10457. {
  10458. return sax->parse_error(m_lexer.get_position(),
  10459. m_lexer.get_token_string(),
  10460. out_of_range::create(406, concat("number overflow parsing '", m_lexer.get_token_string(), '\''), nullptr));
  10461. }
  10462. if (JSON_HEDLEY_UNLIKELY(!sax->number_float(res, m_lexer.get_string())))
  10463. {
  10464. return false;
  10465. }
  10466. break;
  10467. }
  10468. case token_type::literal_false:
  10469. {
  10470. if (JSON_HEDLEY_UNLIKELY(!sax->boolean(false)))
  10471. {
  10472. return false;
  10473. }
  10474. break;
  10475. }
  10476. case token_type::literal_null:
  10477. {
  10478. if (JSON_HEDLEY_UNLIKELY(!sax->null()))
  10479. {
  10480. return false;
  10481. }
  10482. break;
  10483. }
  10484. case token_type::literal_true:
  10485. {
  10486. if (JSON_HEDLEY_UNLIKELY(!sax->boolean(true)))
  10487. {
  10488. return false;
  10489. }
  10490. break;
  10491. }
  10492. case token_type::value_integer:
  10493. {
  10494. if (JSON_HEDLEY_UNLIKELY(!sax->number_integer(m_lexer.get_number_integer())))
  10495. {
  10496. return false;
  10497. }
  10498. break;
  10499. }
  10500. case token_type::value_string:
  10501. {
  10502. if (JSON_HEDLEY_UNLIKELY(!sax->string(m_lexer.get_string())))
  10503. {
  10504. return false;
  10505. }
  10506. break;
  10507. }
  10508. case token_type::value_unsigned:
  10509. {
  10510. if (JSON_HEDLEY_UNLIKELY(!sax->number_unsigned(m_lexer.get_number_unsigned())))
  10511. {
  10512. return false;
  10513. }
  10514. break;
  10515. }
  10516. case token_type::parse_error:
  10517. {
  10518. // using "uninitialized" to avoid "expected" message
  10519. return sax->parse_error(m_lexer.get_position(),
  10520. m_lexer.get_token_string(),
  10521. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::uninitialized, "value"), nullptr));
  10522. }
  10523. case token_type::uninitialized:
  10524. case token_type::end_array:
  10525. case token_type::end_object:
  10526. case token_type::name_separator:
  10527. case token_type::value_separator:
  10528. case token_type::end_of_input:
  10529. case token_type::literal_or_value:
  10530. default: // the last token was unexpected
  10531. {
  10532. return sax->parse_error(m_lexer.get_position(),
  10533. m_lexer.get_token_string(),
  10534. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::literal_or_value, "value"), nullptr));
  10535. }
  10536. }
  10537. }
  10538. else
  10539. {
  10540. skip_to_state_evaluation = false;
  10541. }
  10542. // we reached this line after we successfully parsed a value
  10543. if (states.empty())
  10544. {
  10545. // empty stack: we reached the end of the hierarchy: done
  10546. return true;
  10547. }
  10548. if (states.back()) // array
  10549. {
  10550. // comma -> next value
  10551. if (get_token() == token_type::value_separator)
  10552. {
  10553. // parse a new value
  10554. get_token();
  10555. continue;
  10556. }
  10557. // closing ]
  10558. if (JSON_HEDLEY_LIKELY(last_token == token_type::end_array))
  10559. {
  10560. if (JSON_HEDLEY_UNLIKELY(!sax->end_array()))
  10561. {
  10562. return false;
  10563. }
  10564. // We are done with this array. Before we can parse a
  10565. // new value, we need to evaluate the new state first.
  10566. // By setting skip_to_state_evaluation to false, we
  10567. // are effectively jumping to the beginning of this if.
  10568. JSON_ASSERT(!states.empty());
  10569. states.pop_back();
  10570. skip_to_state_evaluation = true;
  10571. continue;
  10572. }
  10573. return sax->parse_error(m_lexer.get_position(),
  10574. m_lexer.get_token_string(),
  10575. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_array, "array"), nullptr));
  10576. }
  10577. // states.back() is false -> object
  10578. // comma -> next value
  10579. if (get_token() == token_type::value_separator)
  10580. {
  10581. // parse key
  10582. if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::value_string))
  10583. {
  10584. return sax->parse_error(m_lexer.get_position(),
  10585. m_lexer.get_token_string(),
  10586. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), nullptr));
  10587. }
  10588. if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string())))
  10589. {
  10590. return false;
  10591. }
  10592. // parse separator (:)
  10593. if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
  10594. {
  10595. return sax->parse_error(m_lexer.get_position(),
  10596. m_lexer.get_token_string(),
  10597. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), nullptr));
  10598. }
  10599. // parse values
  10600. get_token();
  10601. continue;
  10602. }
  10603. // closing }
  10604. if (JSON_HEDLEY_LIKELY(last_token == token_type::end_object))
  10605. {
  10606. if (JSON_HEDLEY_UNLIKELY(!sax->end_object()))
  10607. {
  10608. return false;
  10609. }
  10610. // We are done with this object. Before we can parse a
  10611. // new value, we need to evaluate the new state first.
  10612. // By setting skip_to_state_evaluation to false, we
  10613. // are effectively jumping to the beginning of this if.
  10614. JSON_ASSERT(!states.empty());
  10615. states.pop_back();
  10616. skip_to_state_evaluation = true;
  10617. continue;
  10618. }
  10619. return sax->parse_error(m_lexer.get_position(),
  10620. m_lexer.get_token_string(),
  10621. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_object, "object"), nullptr));
  10622. }
  10623. }
  10624. /// get next token from lexer
  10625. token_type get_token()
  10626. {
  10627. return last_token = m_lexer.scan();
  10628. }
  10629. std::string exception_message(const token_type expected, const std::string& context)
  10630. {
  10631. std::string error_msg = "syntax error ";
  10632. if (!context.empty())
  10633. {
  10634. error_msg += concat("while parsing ", context, ' ');
  10635. }
  10636. error_msg += "- ";
  10637. if (last_token == token_type::parse_error)
  10638. {
  10639. error_msg += concat(m_lexer.get_error_message(), "; last read: '",
  10640. m_lexer.get_token_string(), '\'');
  10641. }
  10642. else
  10643. {
  10644. error_msg += concat("unexpected ", lexer_t::token_type_name(last_token));
  10645. }
  10646. if (expected != token_type::uninitialized)
  10647. {
  10648. error_msg += concat("; expected ", lexer_t::token_type_name(expected));
  10649. }
  10650. return error_msg;
  10651. }
  10652. private:
  10653. /// callback function
  10654. const parser_callback_t<BasicJsonType> callback = nullptr;
  10655. /// the type of the last read token
  10656. token_type last_token = token_type::uninitialized;
  10657. /// the lexer
  10658. lexer_t m_lexer;
  10659. /// whether to throw exceptions in case of errors
  10660. const bool allow_exceptions = true;
  10661. };
  10662. } // namespace detail
  10663. } // namespace nlohmann
  10664. // #include <nlohmann/detail/iterators/internal_iterator.hpp>
  10665. // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
  10666. #include <cstddef> // ptrdiff_t
  10667. #include <limits> // numeric_limits
  10668. // #include <nlohmann/detail/macro_scope.hpp>
  10669. namespace nlohmann
  10670. {
  10671. namespace detail
  10672. {
  10673. /*
  10674. @brief an iterator for primitive JSON types
  10675. This class models an iterator for primitive JSON types (boolean, number,
  10676. string). It's only purpose is to allow the iterator/const_iterator classes
  10677. to "iterate" over primitive values. Internally, the iterator is modeled by
  10678. a `difference_type` variable. Value begin_value (`0`) models the begin,
  10679. end_value (`1`) models past the end.
  10680. */
  10681. class primitive_iterator_t
  10682. {
  10683. private:
  10684. using difference_type = std::ptrdiff_t;
  10685. static constexpr difference_type begin_value = 0;
  10686. static constexpr difference_type end_value = begin_value + 1;
  10687. JSON_PRIVATE_UNLESS_TESTED:
  10688. /// iterator as signed integer type
  10689. difference_type m_it = (std::numeric_limits<std::ptrdiff_t>::min)();
  10690. public:
  10691. constexpr difference_type get_value() const noexcept
  10692. {
  10693. return m_it;
  10694. }
  10695. /// set iterator to a defined beginning
  10696. void set_begin() noexcept
  10697. {
  10698. m_it = begin_value;
  10699. }
  10700. /// set iterator to a defined past the end
  10701. void set_end() noexcept
  10702. {
  10703. m_it = end_value;
  10704. }
  10705. /// return whether the iterator can be dereferenced
  10706. constexpr bool is_begin() const noexcept
  10707. {
  10708. return m_it == begin_value;
  10709. }
  10710. /// return whether the iterator is at end
  10711. constexpr bool is_end() const noexcept
  10712. {
  10713. return m_it == end_value;
  10714. }
  10715. friend constexpr bool operator==(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
  10716. {
  10717. return lhs.m_it == rhs.m_it;
  10718. }
  10719. friend constexpr bool operator<(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
  10720. {
  10721. return lhs.m_it < rhs.m_it;
  10722. }
  10723. primitive_iterator_t operator+(difference_type n) noexcept
  10724. {
  10725. auto result = *this;
  10726. result += n;
  10727. return result;
  10728. }
  10729. friend constexpr difference_type operator-(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
  10730. {
  10731. return lhs.m_it - rhs.m_it;
  10732. }
  10733. primitive_iterator_t& operator++() noexcept
  10734. {
  10735. ++m_it;
  10736. return *this;
  10737. }
  10738. primitive_iterator_t operator++(int)& noexcept // NOLINT(cert-dcl21-cpp)
  10739. {
  10740. auto result = *this;
  10741. ++m_it;
  10742. return result;
  10743. }
  10744. primitive_iterator_t& operator--() noexcept
  10745. {
  10746. --m_it;
  10747. return *this;
  10748. }
  10749. primitive_iterator_t operator--(int)& noexcept // NOLINT(cert-dcl21-cpp)
  10750. {
  10751. auto result = *this;
  10752. --m_it;
  10753. return result;
  10754. }
  10755. primitive_iterator_t& operator+=(difference_type n) noexcept
  10756. {
  10757. m_it += n;
  10758. return *this;
  10759. }
  10760. primitive_iterator_t& operator-=(difference_type n) noexcept
  10761. {
  10762. m_it -= n;
  10763. return *this;
  10764. }
  10765. };
  10766. } // namespace detail
  10767. } // namespace nlohmann
  10768. namespace nlohmann
  10769. {
  10770. namespace detail
  10771. {
  10772. /*!
  10773. @brief an iterator value
  10774. @note This structure could easily be a union, but MSVC currently does not allow
  10775. unions members with complex constructors, see https://github.com/nlohmann/json/pull/105.
  10776. */
  10777. template<typename BasicJsonType> struct internal_iterator
  10778. {
  10779. /// iterator for JSON objects
  10780. typename BasicJsonType::object_t::iterator object_iterator {};
  10781. /// iterator for JSON arrays
  10782. typename BasicJsonType::array_t::iterator array_iterator {};
  10783. /// generic iterator for all other types
  10784. primitive_iterator_t primitive_iterator {};
  10785. };
  10786. } // namespace detail
  10787. } // namespace nlohmann
  10788. // #include <nlohmann/detail/iterators/iter_impl.hpp>
  10789. #include <iterator> // iterator, random_access_iterator_tag, bidirectional_iterator_tag, advance, next
  10790. #include <type_traits> // conditional, is_const, remove_const
  10791. // #include <nlohmann/detail/exceptions.hpp>
  10792. // #include <nlohmann/detail/iterators/internal_iterator.hpp>
  10793. // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
  10794. // #include <nlohmann/detail/macro_scope.hpp>
  10795. // #include <nlohmann/detail/meta/cpp_future.hpp>
  10796. // #include <nlohmann/detail/meta/type_traits.hpp>
  10797. // #include <nlohmann/detail/value_t.hpp>
  10798. namespace nlohmann
  10799. {
  10800. namespace detail
  10801. {
  10802. // forward declare, to be able to friend it later on
  10803. template<typename IteratorType> class iteration_proxy;
  10804. template<typename IteratorType> class iteration_proxy_value;
  10805. /*!
  10806. @brief a template for a bidirectional iterator for the @ref basic_json class
  10807. This class implements a both iterators (iterator and const_iterator) for the
  10808. @ref basic_json class.
  10809. @note An iterator is called *initialized* when a pointer to a JSON value has
  10810. been set (e.g., by a constructor or a copy assignment). If the iterator is
  10811. default-constructed, it is *uninitialized* and most methods are undefined.
  10812. **The library uses assertions to detect calls on uninitialized iterators.**
  10813. @requirement The class satisfies the following concept requirements:
  10814. -
  10815. [BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
  10816. The iterator that can be moved can be moved in both directions (i.e.
  10817. incremented and decremented).
  10818. @since version 1.0.0, simplified in version 2.0.9, change to bidirectional
  10819. iterators in version 3.0.0 (see https://github.com/nlohmann/json/issues/593)
  10820. */
  10821. template<typename BasicJsonType>
  10822. class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions)
  10823. {
  10824. /// the iterator with BasicJsonType of different const-ness
  10825. using other_iter_impl = iter_impl<typename std::conditional<std::is_const<BasicJsonType>::value, typename std::remove_const<BasicJsonType>::type, const BasicJsonType>::type>;
  10826. /// allow basic_json to access private members
  10827. friend other_iter_impl;
  10828. friend BasicJsonType;
  10829. friend iteration_proxy<iter_impl>;
  10830. friend iteration_proxy_value<iter_impl>;
  10831. using object_t = typename BasicJsonType::object_t;
  10832. using array_t = typename BasicJsonType::array_t;
  10833. // make sure BasicJsonType is basic_json or const basic_json
  10834. static_assert(is_basic_json<typename std::remove_const<BasicJsonType>::type>::value,
  10835. "iter_impl only accepts (const) basic_json");
  10836. // superficial check for the LegacyBidirectionalIterator named requirement
  10837. static_assert(std::is_base_of<std::bidirectional_iterator_tag, std::bidirectional_iterator_tag>::value
  10838. && std::is_base_of<std::bidirectional_iterator_tag, typename std::iterator_traits<typename array_t::iterator>::iterator_category>::value,
  10839. "basic_json iterator assumes array and object type iterators satisfy the LegacyBidirectionalIterator named requirement.");
  10840. public:
  10841. /// The std::iterator class template (used as a base class to provide typedefs) is deprecated in C++17.
  10842. /// The C++ Standard has never required user-defined iterators to derive from std::iterator.
  10843. /// A user-defined iterator should provide publicly accessible typedefs named
  10844. /// iterator_category, value_type, difference_type, pointer, and reference.
  10845. /// Note that value_type is required to be non-const, even for constant iterators.
  10846. using iterator_category = std::bidirectional_iterator_tag;
  10847. /// the type of the values when the iterator is dereferenced
  10848. using value_type = typename BasicJsonType::value_type;
  10849. /// a type to represent differences between iterators
  10850. using difference_type = typename BasicJsonType::difference_type;
  10851. /// defines a pointer to the type iterated over (value_type)
  10852. using pointer = typename std::conditional<std::is_const<BasicJsonType>::value,
  10853. typename BasicJsonType::const_pointer,
  10854. typename BasicJsonType::pointer>::type;
  10855. /// defines a reference to the type iterated over (value_type)
  10856. using reference =
  10857. typename std::conditional<std::is_const<BasicJsonType>::value,
  10858. typename BasicJsonType::const_reference,
  10859. typename BasicJsonType::reference>::type;
  10860. iter_impl() = default;
  10861. ~iter_impl() = default;
  10862. iter_impl(iter_impl&&) noexcept = default;
  10863. iter_impl& operator=(iter_impl&&) noexcept = default;
  10864. /*!
  10865. @brief constructor for a given JSON instance
  10866. @param[in] object pointer to a JSON object for this iterator
  10867. @pre object != nullptr
  10868. @post The iterator is initialized; i.e. `m_object != nullptr`.
  10869. */
  10870. explicit iter_impl(pointer object) noexcept : m_object(object)
  10871. {
  10872. JSON_ASSERT(m_object != nullptr);
  10873. switch (m_object->m_type)
  10874. {
  10875. case value_t::object:
  10876. {
  10877. m_it.object_iterator = typename object_t::iterator();
  10878. break;
  10879. }
  10880. case value_t::array:
  10881. {
  10882. m_it.array_iterator = typename array_t::iterator();
  10883. break;
  10884. }
  10885. case value_t::null:
  10886. case value_t::string:
  10887. case value_t::boolean:
  10888. case value_t::number_integer:
  10889. case value_t::number_unsigned:
  10890. case value_t::number_float:
  10891. case value_t::binary:
  10892. case value_t::discarded:
  10893. default:
  10894. {
  10895. m_it.primitive_iterator = primitive_iterator_t();
  10896. break;
  10897. }
  10898. }
  10899. }
  10900. /*!
  10901. @note The conventional copy constructor and copy assignment are implicitly
  10902. defined. Combined with the following converting constructor and
  10903. assignment, they support: (1) copy from iterator to iterator, (2)
  10904. copy from const iterator to const iterator, and (3) conversion from
  10905. iterator to const iterator. However conversion from const iterator
  10906. to iterator is not defined.
  10907. */
  10908. /*!
  10909. @brief const copy constructor
  10910. @param[in] other const iterator to copy from
  10911. @note This copy constructor had to be defined explicitly to circumvent a bug
  10912. occurring on msvc v19.0 compiler (VS 2015) debug build. For more
  10913. information refer to: https://github.com/nlohmann/json/issues/1608
  10914. */
  10915. iter_impl(const iter_impl<const BasicJsonType>& other) noexcept
  10916. : m_object(other.m_object), m_it(other.m_it)
  10917. {}
  10918. /*!
  10919. @brief converting assignment
  10920. @param[in] other const iterator to copy from
  10921. @return const/non-const iterator
  10922. @note It is not checked whether @a other is initialized.
  10923. */
  10924. iter_impl& operator=(const iter_impl<const BasicJsonType>& other) noexcept
  10925. {
  10926. if (&other != this)
  10927. {
  10928. m_object = other.m_object;
  10929. m_it = other.m_it;
  10930. }
  10931. return *this;
  10932. }
  10933. /*!
  10934. @brief converting constructor
  10935. @param[in] other non-const iterator to copy from
  10936. @note It is not checked whether @a other is initialized.
  10937. */
  10938. iter_impl(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept
  10939. : m_object(other.m_object), m_it(other.m_it)
  10940. {}
  10941. /*!
  10942. @brief converting assignment
  10943. @param[in] other non-const iterator to copy from
  10944. @return const/non-const iterator
  10945. @note It is not checked whether @a other is initialized.
  10946. */
  10947. iter_impl& operator=(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept // NOLINT(cert-oop54-cpp)
  10948. {
  10949. m_object = other.m_object;
  10950. m_it = other.m_it;
  10951. return *this;
  10952. }
  10953. JSON_PRIVATE_UNLESS_TESTED:
  10954. /*!
  10955. @brief set the iterator to the first value
  10956. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  10957. */
  10958. void set_begin() noexcept
  10959. {
  10960. JSON_ASSERT(m_object != nullptr);
  10961. switch (m_object->m_type)
  10962. {
  10963. case value_t::object:
  10964. {
  10965. m_it.object_iterator = m_object->m_value.object->begin();
  10966. break;
  10967. }
  10968. case value_t::array:
  10969. {
  10970. m_it.array_iterator = m_object->m_value.array->begin();
  10971. break;
  10972. }
  10973. case value_t::null:
  10974. {
  10975. // set to end so begin()==end() is true: null is empty
  10976. m_it.primitive_iterator.set_end();
  10977. break;
  10978. }
  10979. case value_t::string:
  10980. case value_t::boolean:
  10981. case value_t::number_integer:
  10982. case value_t::number_unsigned:
  10983. case value_t::number_float:
  10984. case value_t::binary:
  10985. case value_t::discarded:
  10986. default:
  10987. {
  10988. m_it.primitive_iterator.set_begin();
  10989. break;
  10990. }
  10991. }
  10992. }
  10993. /*!
  10994. @brief set the iterator past the last value
  10995. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  10996. */
  10997. void set_end() noexcept
  10998. {
  10999. JSON_ASSERT(m_object != nullptr);
  11000. switch (m_object->m_type)
  11001. {
  11002. case value_t::object:
  11003. {
  11004. m_it.object_iterator = m_object->m_value.object->end();
  11005. break;
  11006. }
  11007. case value_t::array:
  11008. {
  11009. m_it.array_iterator = m_object->m_value.array->end();
  11010. break;
  11011. }
  11012. case value_t::null:
  11013. case value_t::string:
  11014. case value_t::boolean:
  11015. case value_t::number_integer:
  11016. case value_t::number_unsigned:
  11017. case value_t::number_float:
  11018. case value_t::binary:
  11019. case value_t::discarded:
  11020. default:
  11021. {
  11022. m_it.primitive_iterator.set_end();
  11023. break;
  11024. }
  11025. }
  11026. }
  11027. public:
  11028. /*!
  11029. @brief return a reference to the value pointed to by the iterator
  11030. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11031. */
  11032. reference operator*() const
  11033. {
  11034. JSON_ASSERT(m_object != nullptr);
  11035. switch (m_object->m_type)
  11036. {
  11037. case value_t::object:
  11038. {
  11039. JSON_ASSERT(m_it.object_iterator != m_object->m_value.object->end());
  11040. return m_it.object_iterator->second;
  11041. }
  11042. case value_t::array:
  11043. {
  11044. JSON_ASSERT(m_it.array_iterator != m_object->m_value.array->end());
  11045. return *m_it.array_iterator;
  11046. }
  11047. case value_t::null:
  11048. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  11049. case value_t::string:
  11050. case value_t::boolean:
  11051. case value_t::number_integer:
  11052. case value_t::number_unsigned:
  11053. case value_t::number_float:
  11054. case value_t::binary:
  11055. case value_t::discarded:
  11056. default:
  11057. {
  11058. if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin()))
  11059. {
  11060. return *m_object;
  11061. }
  11062. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  11063. }
  11064. }
  11065. }
  11066. /*!
  11067. @brief dereference the iterator
  11068. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11069. */
  11070. pointer operator->() const
  11071. {
  11072. JSON_ASSERT(m_object != nullptr);
  11073. switch (m_object->m_type)
  11074. {
  11075. case value_t::object:
  11076. {
  11077. JSON_ASSERT(m_it.object_iterator != m_object->m_value.object->end());
  11078. return &(m_it.object_iterator->second);
  11079. }
  11080. case value_t::array:
  11081. {
  11082. JSON_ASSERT(m_it.array_iterator != m_object->m_value.array->end());
  11083. return &*m_it.array_iterator;
  11084. }
  11085. case value_t::null:
  11086. case value_t::string:
  11087. case value_t::boolean:
  11088. case value_t::number_integer:
  11089. case value_t::number_unsigned:
  11090. case value_t::number_float:
  11091. case value_t::binary:
  11092. case value_t::discarded:
  11093. default:
  11094. {
  11095. if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin()))
  11096. {
  11097. return m_object;
  11098. }
  11099. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  11100. }
  11101. }
  11102. }
  11103. /*!
  11104. @brief post-increment (it++)
  11105. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11106. */
  11107. iter_impl operator++(int)& // NOLINT(cert-dcl21-cpp)
  11108. {
  11109. auto result = *this;
  11110. ++(*this);
  11111. return result;
  11112. }
  11113. /*!
  11114. @brief pre-increment (++it)
  11115. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11116. */
  11117. iter_impl& operator++()
  11118. {
  11119. JSON_ASSERT(m_object != nullptr);
  11120. switch (m_object->m_type)
  11121. {
  11122. case value_t::object:
  11123. {
  11124. std::advance(m_it.object_iterator, 1);
  11125. break;
  11126. }
  11127. case value_t::array:
  11128. {
  11129. std::advance(m_it.array_iterator, 1);
  11130. break;
  11131. }
  11132. case value_t::null:
  11133. case value_t::string:
  11134. case value_t::boolean:
  11135. case value_t::number_integer:
  11136. case value_t::number_unsigned:
  11137. case value_t::number_float:
  11138. case value_t::binary:
  11139. case value_t::discarded:
  11140. default:
  11141. {
  11142. ++m_it.primitive_iterator;
  11143. break;
  11144. }
  11145. }
  11146. return *this;
  11147. }
  11148. /*!
  11149. @brief post-decrement (it--)
  11150. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11151. */
  11152. iter_impl operator--(int)& // NOLINT(cert-dcl21-cpp)
  11153. {
  11154. auto result = *this;
  11155. --(*this);
  11156. return result;
  11157. }
  11158. /*!
  11159. @brief pre-decrement (--it)
  11160. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11161. */
  11162. iter_impl& operator--()
  11163. {
  11164. JSON_ASSERT(m_object != nullptr);
  11165. switch (m_object->m_type)
  11166. {
  11167. case value_t::object:
  11168. {
  11169. std::advance(m_it.object_iterator, -1);
  11170. break;
  11171. }
  11172. case value_t::array:
  11173. {
  11174. std::advance(m_it.array_iterator, -1);
  11175. break;
  11176. }
  11177. case value_t::null:
  11178. case value_t::string:
  11179. case value_t::boolean:
  11180. case value_t::number_integer:
  11181. case value_t::number_unsigned:
  11182. case value_t::number_float:
  11183. case value_t::binary:
  11184. case value_t::discarded:
  11185. default:
  11186. {
  11187. --m_it.primitive_iterator;
  11188. break;
  11189. }
  11190. }
  11191. return *this;
  11192. }
  11193. /*!
  11194. @brief comparison: equal
  11195. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11196. */
  11197. 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 >
  11198. bool operator==(const IterImpl& other) const
  11199. {
  11200. // if objects are not the same, the comparison is undefined
  11201. if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
  11202. {
  11203. JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", m_object));
  11204. }
  11205. JSON_ASSERT(m_object != nullptr);
  11206. switch (m_object->m_type)
  11207. {
  11208. case value_t::object:
  11209. return (m_it.object_iterator == other.m_it.object_iterator);
  11210. case value_t::array:
  11211. return (m_it.array_iterator == other.m_it.array_iterator);
  11212. case value_t::null:
  11213. case value_t::string:
  11214. case value_t::boolean:
  11215. case value_t::number_integer:
  11216. case value_t::number_unsigned:
  11217. case value_t::number_float:
  11218. case value_t::binary:
  11219. case value_t::discarded:
  11220. default:
  11221. return (m_it.primitive_iterator == other.m_it.primitive_iterator);
  11222. }
  11223. }
  11224. /*!
  11225. @brief comparison: not equal
  11226. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11227. */
  11228. 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 >
  11229. bool operator!=(const IterImpl& other) const
  11230. {
  11231. return !operator==(other);
  11232. }
  11233. /*!
  11234. @brief comparison: smaller
  11235. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11236. */
  11237. bool operator<(const iter_impl& other) const
  11238. {
  11239. // if objects are not the same, the comparison is undefined
  11240. if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
  11241. {
  11242. JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", m_object));
  11243. }
  11244. JSON_ASSERT(m_object != nullptr);
  11245. switch (m_object->m_type)
  11246. {
  11247. case value_t::object:
  11248. JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators", m_object));
  11249. case value_t::array:
  11250. return (m_it.array_iterator < other.m_it.array_iterator);
  11251. case value_t::null:
  11252. case value_t::string:
  11253. case value_t::boolean:
  11254. case value_t::number_integer:
  11255. case value_t::number_unsigned:
  11256. case value_t::number_float:
  11257. case value_t::binary:
  11258. case value_t::discarded:
  11259. default:
  11260. return (m_it.primitive_iterator < other.m_it.primitive_iterator);
  11261. }
  11262. }
  11263. /*!
  11264. @brief comparison: less than or equal
  11265. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11266. */
  11267. bool operator<=(const iter_impl& other) const
  11268. {
  11269. return !other.operator < (*this);
  11270. }
  11271. /*!
  11272. @brief comparison: greater than
  11273. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11274. */
  11275. bool operator>(const iter_impl& other) const
  11276. {
  11277. return !operator<=(other);
  11278. }
  11279. /*!
  11280. @brief comparison: greater than or equal
  11281. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11282. */
  11283. bool operator>=(const iter_impl& other) const
  11284. {
  11285. return !operator<(other);
  11286. }
  11287. /*!
  11288. @brief add to iterator
  11289. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11290. */
  11291. iter_impl& operator+=(difference_type i)
  11292. {
  11293. JSON_ASSERT(m_object != nullptr);
  11294. switch (m_object->m_type)
  11295. {
  11296. case value_t::object:
  11297. JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", m_object));
  11298. case value_t::array:
  11299. {
  11300. std::advance(m_it.array_iterator, i);
  11301. break;
  11302. }
  11303. case value_t::null:
  11304. case value_t::string:
  11305. case value_t::boolean:
  11306. case value_t::number_integer:
  11307. case value_t::number_unsigned:
  11308. case value_t::number_float:
  11309. case value_t::binary:
  11310. case value_t::discarded:
  11311. default:
  11312. {
  11313. m_it.primitive_iterator += i;
  11314. break;
  11315. }
  11316. }
  11317. return *this;
  11318. }
  11319. /*!
  11320. @brief subtract from iterator
  11321. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11322. */
  11323. iter_impl& operator-=(difference_type i)
  11324. {
  11325. return operator+=(-i);
  11326. }
  11327. /*!
  11328. @brief add to iterator
  11329. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11330. */
  11331. iter_impl operator+(difference_type i) const
  11332. {
  11333. auto result = *this;
  11334. result += i;
  11335. return result;
  11336. }
  11337. /*!
  11338. @brief addition of distance and iterator
  11339. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11340. */
  11341. friend iter_impl operator+(difference_type i, const iter_impl& it)
  11342. {
  11343. auto result = it;
  11344. result += i;
  11345. return result;
  11346. }
  11347. /*!
  11348. @brief subtract from iterator
  11349. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11350. */
  11351. iter_impl operator-(difference_type i) const
  11352. {
  11353. auto result = *this;
  11354. result -= i;
  11355. return result;
  11356. }
  11357. /*!
  11358. @brief return difference
  11359. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11360. */
  11361. difference_type operator-(const iter_impl& other) const
  11362. {
  11363. JSON_ASSERT(m_object != nullptr);
  11364. switch (m_object->m_type)
  11365. {
  11366. case value_t::object:
  11367. JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", m_object));
  11368. case value_t::array:
  11369. return m_it.array_iterator - other.m_it.array_iterator;
  11370. case value_t::null:
  11371. case value_t::string:
  11372. case value_t::boolean:
  11373. case value_t::number_integer:
  11374. case value_t::number_unsigned:
  11375. case value_t::number_float:
  11376. case value_t::binary:
  11377. case value_t::discarded:
  11378. default:
  11379. return m_it.primitive_iterator - other.m_it.primitive_iterator;
  11380. }
  11381. }
  11382. /*!
  11383. @brief access to successor
  11384. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11385. */
  11386. reference operator[](difference_type n) const
  11387. {
  11388. JSON_ASSERT(m_object != nullptr);
  11389. switch (m_object->m_type)
  11390. {
  11391. case value_t::object:
  11392. JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators", m_object));
  11393. case value_t::array:
  11394. return *std::next(m_it.array_iterator, n);
  11395. case value_t::null:
  11396. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  11397. case value_t::string:
  11398. case value_t::boolean:
  11399. case value_t::number_integer:
  11400. case value_t::number_unsigned:
  11401. case value_t::number_float:
  11402. case value_t::binary:
  11403. case value_t::discarded:
  11404. default:
  11405. {
  11406. if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.get_value() == -n))
  11407. {
  11408. return *m_object;
  11409. }
  11410. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  11411. }
  11412. }
  11413. }
  11414. /*!
  11415. @brief return the key of an object iterator
  11416. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11417. */
  11418. const typename object_t::key_type& key() const
  11419. {
  11420. JSON_ASSERT(m_object != nullptr);
  11421. if (JSON_HEDLEY_LIKELY(m_object->is_object()))
  11422. {
  11423. return m_it.object_iterator->first;
  11424. }
  11425. JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators", m_object));
  11426. }
  11427. /*!
  11428. @brief return the value of an iterator
  11429. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  11430. */
  11431. reference value() const
  11432. {
  11433. return operator*();
  11434. }
  11435. JSON_PRIVATE_UNLESS_TESTED:
  11436. /// associated JSON instance
  11437. pointer m_object = nullptr;
  11438. /// the actual iterator of the associated instance
  11439. internal_iterator<typename std::remove_const<BasicJsonType>::type> m_it {};
  11440. };
  11441. } // namespace detail
  11442. } // namespace nlohmann
  11443. // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
  11444. // #include <nlohmann/detail/iterators/json_reverse_iterator.hpp>
  11445. #include <cstddef> // ptrdiff_t
  11446. #include <iterator> // reverse_iterator
  11447. #include <utility> // declval
  11448. namespace nlohmann
  11449. {
  11450. namespace detail
  11451. {
  11452. //////////////////////
  11453. // reverse_iterator //
  11454. //////////////////////
  11455. /*!
  11456. @brief a template for a reverse iterator class
  11457. @tparam Base the base iterator type to reverse. Valid types are @ref
  11458. iterator (to create @ref reverse_iterator) and @ref const_iterator (to
  11459. create @ref const_reverse_iterator).
  11460. @requirement The class satisfies the following concept requirements:
  11461. -
  11462. [BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
  11463. The iterator that can be moved can be moved in both directions (i.e.
  11464. incremented and decremented).
  11465. - [OutputIterator](https://en.cppreference.com/w/cpp/named_req/OutputIterator):
  11466. It is possible to write to the pointed-to element (only if @a Base is
  11467. @ref iterator).
  11468. @since version 1.0.0
  11469. */
  11470. template<typename Base>
  11471. class json_reverse_iterator : public std::reverse_iterator<Base>
  11472. {
  11473. public:
  11474. using difference_type = std::ptrdiff_t;
  11475. /// shortcut to the reverse iterator adapter
  11476. using base_iterator = std::reverse_iterator<Base>;
  11477. /// the reference type for the pointed-to element
  11478. using reference = typename Base::reference;
  11479. /// create reverse iterator from iterator
  11480. explicit json_reverse_iterator(const typename base_iterator::iterator_type& it) noexcept
  11481. : base_iterator(it) {}
  11482. /// create reverse iterator from base class
  11483. explicit json_reverse_iterator(const base_iterator& it) noexcept : base_iterator(it) {}
  11484. /// post-increment (it++)
  11485. json_reverse_iterator operator++(int)& // NOLINT(cert-dcl21-cpp)
  11486. {
  11487. return static_cast<json_reverse_iterator>(base_iterator::operator++(1));
  11488. }
  11489. /// pre-increment (++it)
  11490. json_reverse_iterator& operator++()
  11491. {
  11492. return static_cast<json_reverse_iterator&>(base_iterator::operator++());
  11493. }
  11494. /// post-decrement (it--)
  11495. json_reverse_iterator operator--(int)& // NOLINT(cert-dcl21-cpp)
  11496. {
  11497. return static_cast<json_reverse_iterator>(base_iterator::operator--(1));
  11498. }
  11499. /// pre-decrement (--it)
  11500. json_reverse_iterator& operator--()
  11501. {
  11502. return static_cast<json_reverse_iterator&>(base_iterator::operator--());
  11503. }
  11504. /// add to iterator
  11505. json_reverse_iterator& operator+=(difference_type i)
  11506. {
  11507. return static_cast<json_reverse_iterator&>(base_iterator::operator+=(i));
  11508. }
  11509. /// add to iterator
  11510. json_reverse_iterator operator+(difference_type i) const
  11511. {
  11512. return static_cast<json_reverse_iterator>(base_iterator::operator+(i));
  11513. }
  11514. /// subtract from iterator
  11515. json_reverse_iterator operator-(difference_type i) const
  11516. {
  11517. return static_cast<json_reverse_iterator>(base_iterator::operator-(i));
  11518. }
  11519. /// return difference
  11520. difference_type operator-(const json_reverse_iterator& other) const
  11521. {
  11522. return base_iterator(*this) - base_iterator(other);
  11523. }
  11524. /// access to successor
  11525. reference operator[](difference_type n) const
  11526. {
  11527. return *(this->operator+(n));
  11528. }
  11529. /// return the key of an object iterator
  11530. auto key() const -> decltype(std::declval<Base>().key())
  11531. {
  11532. auto it = --this->base();
  11533. return it.key();
  11534. }
  11535. /// return the value of an iterator
  11536. reference value() const
  11537. {
  11538. auto it = --this->base();
  11539. return it.operator * ();
  11540. }
  11541. };
  11542. } // namespace detail
  11543. } // namespace nlohmann
  11544. // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
  11545. // #include <nlohmann/detail/json_pointer.hpp>
  11546. #include <algorithm> // all_of
  11547. #include <cctype> // isdigit
  11548. #include <cerrno> // errno, ERANGE
  11549. #include <cstdlib> // strtoull
  11550. #include <limits> // max
  11551. #include <numeric> // accumulate
  11552. #include <string> // string
  11553. #include <utility> // move
  11554. #include <vector> // vector
  11555. // #include <nlohmann/detail/exceptions.hpp>
  11556. // #include <nlohmann/detail/macro_scope.hpp>
  11557. // #include <nlohmann/detail/string_concat.hpp>
  11558. // #include <nlohmann/detail/string_escape.hpp>
  11559. // #include <nlohmann/detail/value_t.hpp>
  11560. namespace nlohmann
  11561. {
  11562. /// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document
  11563. /// @sa https://json.nlohmann.me/api/json_pointer/
  11564. template<typename RefStringType>
  11565. class json_pointer
  11566. {
  11567. // allow basic_json to access private members
  11568. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  11569. friend class basic_json;
  11570. template<typename>
  11571. friend class json_pointer;
  11572. template<typename T>
  11573. struct string_t_helper
  11574. {
  11575. using type = T;
  11576. };
  11577. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  11578. struct string_t_helper<NLOHMANN_BASIC_JSON_TPL>
  11579. {
  11580. using type = StringType;
  11581. };
  11582. public:
  11583. // for backwards compatibility accept BasicJsonType
  11584. using string_t = typename string_t_helper<RefStringType>::type;
  11585. /// @brief create JSON pointer
  11586. /// @sa https://json.nlohmann.me/api/json_pointer/json_pointer/
  11587. explicit json_pointer(const string_t& s = "")
  11588. : reference_tokens(split(s))
  11589. {}
  11590. /// @brief return a string representation of the JSON pointer
  11591. /// @sa https://json.nlohmann.me/api/json_pointer/to_string/
  11592. string_t to_string() const
  11593. {
  11594. return std::accumulate(reference_tokens.begin(), reference_tokens.end(),
  11595. string_t{},
  11596. [](const string_t& a, const string_t& b)
  11597. {
  11598. return detail::concat(a, '/', detail::escape(b));
  11599. });
  11600. }
  11601. /// @brief return a string representation of the JSON pointer
  11602. /// @sa https://json.nlohmann.me/api/json_pointer/operator_string/
  11603. operator string_t() const
  11604. {
  11605. return to_string();
  11606. }
  11607. /// @brief append another JSON pointer at the end of this JSON pointer
  11608. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
  11609. json_pointer& operator/=(const json_pointer& ptr)
  11610. {
  11611. reference_tokens.insert(reference_tokens.end(),
  11612. ptr.reference_tokens.begin(),
  11613. ptr.reference_tokens.end());
  11614. return *this;
  11615. }
  11616. /// @brief append an unescaped reference token at the end of this JSON pointer
  11617. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
  11618. json_pointer& operator/=(string_t token)
  11619. {
  11620. push_back(std::move(token));
  11621. return *this;
  11622. }
  11623. /// @brief append an array index at the end of this JSON pointer
  11624. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
  11625. json_pointer& operator/=(std::size_t array_idx)
  11626. {
  11627. return *this /= std::to_string(array_idx);
  11628. }
  11629. /// @brief create a new JSON pointer by appending the right JSON pointer at the end of the left JSON pointer
  11630. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
  11631. friend json_pointer operator/(const json_pointer& lhs,
  11632. const json_pointer& rhs)
  11633. {
  11634. return json_pointer(lhs) /= rhs;
  11635. }
  11636. /// @brief create a new JSON pointer by appending the unescaped token at the end of the JSON pointer
  11637. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
  11638. friend json_pointer operator/(const json_pointer& lhs, string_t token) // NOLINT(performance-unnecessary-value-param)
  11639. {
  11640. return json_pointer(lhs) /= std::move(token);
  11641. }
  11642. /// @brief create a new JSON pointer by appending the array-index-token at the end of the JSON pointer
  11643. /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
  11644. friend json_pointer operator/(const json_pointer& lhs, std::size_t array_idx)
  11645. {
  11646. return json_pointer(lhs) /= array_idx;
  11647. }
  11648. /// @brief returns the parent of this JSON pointer
  11649. /// @sa https://json.nlohmann.me/api/json_pointer/parent_pointer/
  11650. json_pointer parent_pointer() const
  11651. {
  11652. if (empty())
  11653. {
  11654. return *this;
  11655. }
  11656. json_pointer res = *this;
  11657. res.pop_back();
  11658. return res;
  11659. }
  11660. /// @brief remove last reference token
  11661. /// @sa https://json.nlohmann.me/api/json_pointer/pop_back/
  11662. void pop_back()
  11663. {
  11664. if (JSON_HEDLEY_UNLIKELY(empty()))
  11665. {
  11666. JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
  11667. }
  11668. reference_tokens.pop_back();
  11669. }
  11670. /// @brief return last reference token
  11671. /// @sa https://json.nlohmann.me/api/json_pointer/back/
  11672. const string_t& back() const
  11673. {
  11674. if (JSON_HEDLEY_UNLIKELY(empty()))
  11675. {
  11676. JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
  11677. }
  11678. return reference_tokens.back();
  11679. }
  11680. /// @brief append an unescaped token at the end of the reference pointer
  11681. /// @sa https://json.nlohmann.me/api/json_pointer/push_back/
  11682. void push_back(const string_t& token)
  11683. {
  11684. reference_tokens.push_back(token);
  11685. }
  11686. /// @brief append an unescaped token at the end of the reference pointer
  11687. /// @sa https://json.nlohmann.me/api/json_pointer/push_back/
  11688. void push_back(string_t&& token)
  11689. {
  11690. reference_tokens.push_back(std::move(token));
  11691. }
  11692. /// @brief return whether pointer points to the root document
  11693. /// @sa https://json.nlohmann.me/api/json_pointer/empty/
  11694. bool empty() const noexcept
  11695. {
  11696. return reference_tokens.empty();
  11697. }
  11698. private:
  11699. /*!
  11700. @param[in] s reference token to be converted into an array index
  11701. @return integer representation of @a s
  11702. @throw parse_error.106 if an array index begins with '0'
  11703. @throw parse_error.109 if an array index begins not with a digit
  11704. @throw out_of_range.404 if string @a s could not be converted to an integer
  11705. @throw out_of_range.410 if an array index exceeds size_type
  11706. */
  11707. template<typename BasicJsonType>
  11708. static typename BasicJsonType::size_type array_index(const string_t& s)
  11709. {
  11710. using size_type = typename BasicJsonType::size_type;
  11711. // error condition (cf. RFC 6901, Sect. 4)
  11712. if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && s[0] == '0'))
  11713. {
  11714. JSON_THROW(detail::parse_error::create(106, 0, detail::concat("array index '", s, "' must not begin with '0'"), nullptr));
  11715. }
  11716. // error condition (cf. RFC 6901, Sect. 4)
  11717. if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && !(s[0] >= '1' && s[0] <= '9')))
  11718. {
  11719. JSON_THROW(detail::parse_error::create(109, 0, detail::concat("array index '", s, "' is not a number"), nullptr));
  11720. }
  11721. const char* p = s.c_str();
  11722. char* p_end = nullptr;
  11723. errno = 0; // strtoull doesn't reset errno
  11724. unsigned long long res = std::strtoull(p, &p_end, 10); // NOLINT(runtime/int)
  11725. if (p == p_end // invalid input or empty string
  11726. || errno == ERANGE // out of range
  11727. || JSON_HEDLEY_UNLIKELY(static_cast<std::size_t>(p_end - p) != s.size())) // incomplete read
  11728. {
  11729. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", s, "'"), nullptr));
  11730. }
  11731. // only triggered on special platforms (like 32bit), see also
  11732. // https://github.com/nlohmann/json/pull/2203
  11733. if (res >= static_cast<unsigned long long>((std::numeric_limits<size_type>::max)())) // NOLINT(runtime/int)
  11734. {
  11735. JSON_THROW(detail::out_of_range::create(410, detail::concat("array index ", s, " exceeds size_type"), nullptr)); // LCOV_EXCL_LINE
  11736. }
  11737. return static_cast<size_type>(res);
  11738. }
  11739. JSON_PRIVATE_UNLESS_TESTED:
  11740. json_pointer top() const
  11741. {
  11742. if (JSON_HEDLEY_UNLIKELY(empty()))
  11743. {
  11744. JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
  11745. }
  11746. json_pointer result = *this;
  11747. result.reference_tokens = {reference_tokens[0]};
  11748. return result;
  11749. }
  11750. private:
  11751. /*!
  11752. @brief create and return a reference to the pointed to value
  11753. @complexity Linear in the number of reference tokens.
  11754. @throw parse_error.109 if array index is not a number
  11755. @throw type_error.313 if value cannot be unflattened
  11756. */
  11757. template<typename BasicJsonType>
  11758. BasicJsonType& get_and_create(BasicJsonType& j) const
  11759. {
  11760. auto* result = &j;
  11761. // in case no reference tokens exist, return a reference to the JSON value
  11762. // j which will be overwritten by a primitive value
  11763. for (const auto& reference_token : reference_tokens)
  11764. {
  11765. switch (result->type())
  11766. {
  11767. case detail::value_t::null:
  11768. {
  11769. if (reference_token == "0")
  11770. {
  11771. // start a new array if reference token is 0
  11772. result = &result->operator[](0);
  11773. }
  11774. else
  11775. {
  11776. // start a new object otherwise
  11777. result = &result->operator[](reference_token);
  11778. }
  11779. break;
  11780. }
  11781. case detail::value_t::object:
  11782. {
  11783. // create an entry in the object
  11784. result = &result->operator[](reference_token);
  11785. break;
  11786. }
  11787. case detail::value_t::array:
  11788. {
  11789. // create an entry in the array
  11790. result = &result->operator[](array_index<BasicJsonType>(reference_token));
  11791. break;
  11792. }
  11793. /*
  11794. The following code is only reached if there exists a reference
  11795. token _and_ the current value is primitive. In this case, we have
  11796. an error situation, because primitive values may only occur as
  11797. single value; that is, with an empty list of reference tokens.
  11798. */
  11799. case detail::value_t::string:
  11800. case detail::value_t::boolean:
  11801. case detail::value_t::number_integer:
  11802. case detail::value_t::number_unsigned:
  11803. case detail::value_t::number_float:
  11804. case detail::value_t::binary:
  11805. case detail::value_t::discarded:
  11806. default:
  11807. JSON_THROW(detail::type_error::create(313, "invalid value to unflatten", &j));
  11808. }
  11809. }
  11810. return *result;
  11811. }
  11812. /*!
  11813. @brief return a reference to the pointed to value
  11814. @note This version does not throw if a value is not present, but tries to
  11815. create nested values instead. For instance, calling this function
  11816. with pointer `"/this/that"` on a null value is equivalent to calling
  11817. `operator[]("this").operator[]("that")` on that value, effectively
  11818. changing the null value to an object.
  11819. @param[in] ptr a JSON value
  11820. @return reference to the JSON value pointed to by the JSON pointer
  11821. @complexity Linear in the length of the JSON pointer.
  11822. @throw parse_error.106 if an array index begins with '0'
  11823. @throw parse_error.109 if an array index was not a number
  11824. @throw out_of_range.404 if the JSON pointer can not be resolved
  11825. */
  11826. template<typename BasicJsonType>
  11827. BasicJsonType& get_unchecked(BasicJsonType* ptr) const
  11828. {
  11829. for (const auto& reference_token : reference_tokens)
  11830. {
  11831. // convert null values to arrays or objects before continuing
  11832. if (ptr->is_null())
  11833. {
  11834. // check if reference token is a number
  11835. const bool nums =
  11836. std::all_of(reference_token.begin(), reference_token.end(),
  11837. [](const unsigned char x)
  11838. {
  11839. return std::isdigit(x);
  11840. });
  11841. // change value to array for numbers or "-" or to object otherwise
  11842. *ptr = (nums || reference_token == "-")
  11843. ? detail::value_t::array
  11844. : detail::value_t::object;
  11845. }
  11846. switch (ptr->type())
  11847. {
  11848. case detail::value_t::object:
  11849. {
  11850. // use unchecked object access
  11851. ptr = &ptr->operator[](reference_token);
  11852. break;
  11853. }
  11854. case detail::value_t::array:
  11855. {
  11856. if (reference_token == "-")
  11857. {
  11858. // explicitly treat "-" as index beyond the end
  11859. ptr = &ptr->operator[](ptr->m_value.array->size());
  11860. }
  11861. else
  11862. {
  11863. // convert array index to number; unchecked access
  11864. ptr = &ptr->operator[](array_index<BasicJsonType>(reference_token));
  11865. }
  11866. break;
  11867. }
  11868. case detail::value_t::null:
  11869. case detail::value_t::string:
  11870. case detail::value_t::boolean:
  11871. case detail::value_t::number_integer:
  11872. case detail::value_t::number_unsigned:
  11873. case detail::value_t::number_float:
  11874. case detail::value_t::binary:
  11875. case detail::value_t::discarded:
  11876. default:
  11877. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
  11878. }
  11879. }
  11880. return *ptr;
  11881. }
  11882. /*!
  11883. @throw parse_error.106 if an array index begins with '0'
  11884. @throw parse_error.109 if an array index was not a number
  11885. @throw out_of_range.402 if the array index '-' is used
  11886. @throw out_of_range.404 if the JSON pointer can not be resolved
  11887. */
  11888. template<typename BasicJsonType>
  11889. BasicJsonType& get_checked(BasicJsonType* ptr) const
  11890. {
  11891. for (const auto& reference_token : reference_tokens)
  11892. {
  11893. switch (ptr->type())
  11894. {
  11895. case detail::value_t::object:
  11896. {
  11897. // note: at performs range check
  11898. ptr = &ptr->at(reference_token);
  11899. break;
  11900. }
  11901. case detail::value_t::array:
  11902. {
  11903. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  11904. {
  11905. // "-" always fails the range check
  11906. JSON_THROW(detail::out_of_range::create(402, detail::concat(
  11907. "array index '-' (", std::to_string(ptr->m_value.array->size()),
  11908. ") is out of range"), ptr));
  11909. }
  11910. // note: at performs range check
  11911. ptr = &ptr->at(array_index<BasicJsonType>(reference_token));
  11912. break;
  11913. }
  11914. case detail::value_t::null:
  11915. case detail::value_t::string:
  11916. case detail::value_t::boolean:
  11917. case detail::value_t::number_integer:
  11918. case detail::value_t::number_unsigned:
  11919. case detail::value_t::number_float:
  11920. case detail::value_t::binary:
  11921. case detail::value_t::discarded:
  11922. default:
  11923. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
  11924. }
  11925. }
  11926. return *ptr;
  11927. }
  11928. /*!
  11929. @brief return a const reference to the pointed to value
  11930. @param[in] ptr a JSON value
  11931. @return const reference to the JSON value pointed to by the JSON
  11932. pointer
  11933. @throw parse_error.106 if an array index begins with '0'
  11934. @throw parse_error.109 if an array index was not a number
  11935. @throw out_of_range.402 if the array index '-' is used
  11936. @throw out_of_range.404 if the JSON pointer can not be resolved
  11937. */
  11938. template<typename BasicJsonType>
  11939. const BasicJsonType& get_unchecked(const BasicJsonType* ptr) const
  11940. {
  11941. for (const auto& reference_token : reference_tokens)
  11942. {
  11943. switch (ptr->type())
  11944. {
  11945. case detail::value_t::object:
  11946. {
  11947. // use unchecked object access
  11948. ptr = &ptr->operator[](reference_token);
  11949. break;
  11950. }
  11951. case detail::value_t::array:
  11952. {
  11953. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  11954. {
  11955. // "-" cannot be used for const access
  11956. 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));
  11957. }
  11958. // use unchecked array access
  11959. ptr = &ptr->operator[](array_index<BasicJsonType>(reference_token));
  11960. break;
  11961. }
  11962. case detail::value_t::null:
  11963. case detail::value_t::string:
  11964. case detail::value_t::boolean:
  11965. case detail::value_t::number_integer:
  11966. case detail::value_t::number_unsigned:
  11967. case detail::value_t::number_float:
  11968. case detail::value_t::binary:
  11969. case detail::value_t::discarded:
  11970. default:
  11971. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
  11972. }
  11973. }
  11974. return *ptr;
  11975. }
  11976. /*!
  11977. @throw parse_error.106 if an array index begins with '0'
  11978. @throw parse_error.109 if an array index was not a number
  11979. @throw out_of_range.402 if the array index '-' is used
  11980. @throw out_of_range.404 if the JSON pointer can not be resolved
  11981. */
  11982. template<typename BasicJsonType>
  11983. const BasicJsonType& get_checked(const BasicJsonType* ptr) const
  11984. {
  11985. for (const auto& reference_token : reference_tokens)
  11986. {
  11987. switch (ptr->type())
  11988. {
  11989. case detail::value_t::object:
  11990. {
  11991. // note: at performs range check
  11992. ptr = &ptr->at(reference_token);
  11993. break;
  11994. }
  11995. case detail::value_t::array:
  11996. {
  11997. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  11998. {
  11999. // "-" always fails the range check
  12000. JSON_THROW(detail::out_of_range::create(402, detail::concat(
  12001. "array index '-' (", std::to_string(ptr->m_value.array->size()),
  12002. ") is out of range"), ptr));
  12003. }
  12004. // note: at performs range check
  12005. ptr = &ptr->at(array_index<BasicJsonType>(reference_token));
  12006. break;
  12007. }
  12008. case detail::value_t::null:
  12009. case detail::value_t::string:
  12010. case detail::value_t::boolean:
  12011. case detail::value_t::number_integer:
  12012. case detail::value_t::number_unsigned:
  12013. case detail::value_t::number_float:
  12014. case detail::value_t::binary:
  12015. case detail::value_t::discarded:
  12016. default:
  12017. JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
  12018. }
  12019. }
  12020. return *ptr;
  12021. }
  12022. /*!
  12023. @throw parse_error.106 if an array index begins with '0'
  12024. @throw parse_error.109 if an array index was not a number
  12025. */
  12026. template<typename BasicJsonType>
  12027. bool contains(const BasicJsonType* ptr) const
  12028. {
  12029. for (const auto& reference_token : reference_tokens)
  12030. {
  12031. switch (ptr->type())
  12032. {
  12033. case detail::value_t::object:
  12034. {
  12035. if (!ptr->contains(reference_token))
  12036. {
  12037. // we did not find the key in the object
  12038. return false;
  12039. }
  12040. ptr = &ptr->operator[](reference_token);
  12041. break;
  12042. }
  12043. case detail::value_t::array:
  12044. {
  12045. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  12046. {
  12047. // "-" always fails the range check
  12048. return false;
  12049. }
  12050. if (JSON_HEDLEY_UNLIKELY(reference_token.size() == 1 && !("0" <= reference_token && reference_token <= "9")))
  12051. {
  12052. // invalid char
  12053. return false;
  12054. }
  12055. if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1))
  12056. {
  12057. if (JSON_HEDLEY_UNLIKELY(!('1' <= reference_token[0] && reference_token[0] <= '9')))
  12058. {
  12059. // first char should be between '1' and '9'
  12060. return false;
  12061. }
  12062. for (std::size_t i = 1; i < reference_token.size(); i++)
  12063. {
  12064. if (JSON_HEDLEY_UNLIKELY(!('0' <= reference_token[i] && reference_token[i] <= '9')))
  12065. {
  12066. // other char should be between '0' and '9'
  12067. return false;
  12068. }
  12069. }
  12070. }
  12071. const auto idx = array_index<BasicJsonType>(reference_token);
  12072. if (idx >= ptr->size())
  12073. {
  12074. // index out of range
  12075. return false;
  12076. }
  12077. ptr = &ptr->operator[](idx);
  12078. break;
  12079. }
  12080. case detail::value_t::null:
  12081. case detail::value_t::string:
  12082. case detail::value_t::boolean:
  12083. case detail::value_t::number_integer:
  12084. case detail::value_t::number_unsigned:
  12085. case detail::value_t::number_float:
  12086. case detail::value_t::binary:
  12087. case detail::value_t::discarded:
  12088. default:
  12089. {
  12090. // we do not expect primitive values if there is still a
  12091. // reference token to process
  12092. return false;
  12093. }
  12094. }
  12095. }
  12096. // no reference token left means we found a primitive value
  12097. return true;
  12098. }
  12099. /*!
  12100. @brief split the string input to reference tokens
  12101. @note This function is only called by the json_pointer constructor.
  12102. All exceptions below are documented there.
  12103. @throw parse_error.107 if the pointer is not empty or begins with '/'
  12104. @throw parse_error.108 if character '~' is not followed by '0' or '1'
  12105. */
  12106. static std::vector<string_t> split(const string_t& reference_string)
  12107. {
  12108. std::vector<string_t> result;
  12109. // special case: empty reference string -> no reference tokens
  12110. if (reference_string.empty())
  12111. {
  12112. return result;
  12113. }
  12114. // check if nonempty reference string begins with slash
  12115. if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/'))
  12116. {
  12117. JSON_THROW(detail::parse_error::create(107, 1, detail::concat("JSON pointer must be empty or begin with '/' - was: '", reference_string, "'"), nullptr));
  12118. }
  12119. // extract the reference tokens:
  12120. // - slash: position of the last read slash (or end of string)
  12121. // - start: position after the previous slash
  12122. for (
  12123. // search for the first slash after the first character
  12124. std::size_t slash = reference_string.find_first_of('/', 1),
  12125. // set the beginning of the first reference token
  12126. start = 1;
  12127. // we can stop if start == 0 (if slash == string_t::npos)
  12128. start != 0;
  12129. // set the beginning of the next reference token
  12130. // (will eventually be 0 if slash == string_t::npos)
  12131. start = (slash == string_t::npos) ? 0 : slash + 1,
  12132. // find next slash
  12133. slash = reference_string.find_first_of('/', start))
  12134. {
  12135. // use the text between the beginning of the reference token
  12136. // (start) and the last slash (slash).
  12137. auto reference_token = reference_string.substr(start, slash - start);
  12138. // check reference tokens are properly escaped
  12139. for (std::size_t pos = reference_token.find_first_of('~');
  12140. pos != string_t::npos;
  12141. pos = reference_token.find_first_of('~', pos + 1))
  12142. {
  12143. JSON_ASSERT(reference_token[pos] == '~');
  12144. // ~ must be followed by 0 or 1
  12145. if (JSON_HEDLEY_UNLIKELY(pos == reference_token.size() - 1 ||
  12146. (reference_token[pos + 1] != '0' &&
  12147. reference_token[pos + 1] != '1')))
  12148. {
  12149. JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'", nullptr));
  12150. }
  12151. }
  12152. // finally, store the reference token
  12153. detail::unescape(reference_token);
  12154. result.push_back(reference_token);
  12155. }
  12156. return result;
  12157. }
  12158. private:
  12159. /*!
  12160. @param[in] reference_string the reference string to the current value
  12161. @param[in] value the value to consider
  12162. @param[in,out] result the result object to insert values to
  12163. @note Empty objects or arrays are flattened to `null`.
  12164. */
  12165. template<typename BasicJsonType>
  12166. static void flatten(const string_t& reference_string,
  12167. const BasicJsonType& value,
  12168. BasicJsonType& result)
  12169. {
  12170. switch (value.type())
  12171. {
  12172. case detail::value_t::array:
  12173. {
  12174. if (value.m_value.array->empty())
  12175. {
  12176. // flatten empty array as null
  12177. result[reference_string] = nullptr;
  12178. }
  12179. else
  12180. {
  12181. // iterate array and use index as reference string
  12182. for (std::size_t i = 0; i < value.m_value.array->size(); ++i)
  12183. {
  12184. flatten(detail::concat(reference_string, '/', std::to_string(i)),
  12185. value.m_value.array->operator[](i), result);
  12186. }
  12187. }
  12188. break;
  12189. }
  12190. case detail::value_t::object:
  12191. {
  12192. if (value.m_value.object->empty())
  12193. {
  12194. // flatten empty object as null
  12195. result[reference_string] = nullptr;
  12196. }
  12197. else
  12198. {
  12199. // iterate object and use keys as reference string
  12200. for (const auto& element : *value.m_value.object)
  12201. {
  12202. flatten(detail::concat(reference_string, '/', detail::escape(element.first)), element.second, result);
  12203. }
  12204. }
  12205. break;
  12206. }
  12207. case detail::value_t::null:
  12208. case detail::value_t::string:
  12209. case detail::value_t::boolean:
  12210. case detail::value_t::number_integer:
  12211. case detail::value_t::number_unsigned:
  12212. case detail::value_t::number_float:
  12213. case detail::value_t::binary:
  12214. case detail::value_t::discarded:
  12215. default:
  12216. {
  12217. // add primitive value with its reference string
  12218. result[reference_string] = value;
  12219. break;
  12220. }
  12221. }
  12222. }
  12223. /*!
  12224. @param[in] value flattened JSON
  12225. @return unflattened JSON
  12226. @throw parse_error.109 if array index is not a number
  12227. @throw type_error.314 if value is not an object
  12228. @throw type_error.315 if object values are not primitive
  12229. @throw type_error.313 if value cannot be unflattened
  12230. */
  12231. template<typename BasicJsonType>
  12232. static BasicJsonType
  12233. unflatten(const BasicJsonType& value)
  12234. {
  12235. if (JSON_HEDLEY_UNLIKELY(!value.is_object()))
  12236. {
  12237. JSON_THROW(detail::type_error::create(314, "only objects can be unflattened", &value));
  12238. }
  12239. BasicJsonType result;
  12240. // iterate the JSON object values
  12241. for (const auto& element : *value.m_value.object)
  12242. {
  12243. if (JSON_HEDLEY_UNLIKELY(!element.second.is_primitive()))
  12244. {
  12245. JSON_THROW(detail::type_error::create(315, "values in object must be primitive", &element.second));
  12246. }
  12247. // assign value to reference pointed to by JSON pointer; Note that if
  12248. // the JSON pointer is "" (i.e., points to the whole value), function
  12249. // get_and_create returns a reference to result itself. An assignment
  12250. // will then create a primitive value.
  12251. json_pointer(element.first).get_and_create(result) = element.second;
  12252. }
  12253. return result;
  12254. }
  12255. // can't use conversion operator because of ambiguity
  12256. json_pointer<string_t> convert() const&
  12257. {
  12258. json_pointer<string_t> result;
  12259. result.reference_tokens = reference_tokens;
  12260. return result;
  12261. }
  12262. json_pointer<string_t> convert()&&
  12263. {
  12264. json_pointer<string_t> result;
  12265. result.reference_tokens = std::move(reference_tokens);
  12266. return result;
  12267. }
  12268. /*!
  12269. @brief compares two JSON pointers for equality
  12270. @param[in] lhs JSON pointer to compare
  12271. @param[in] rhs JSON pointer to compare
  12272. @return whether @a lhs is equal to @a rhs
  12273. @complexity Linear in the length of the JSON pointer
  12274. @exceptionsafety No-throw guarantee: this function never throws exceptions.
  12275. */
  12276. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  12277. // NOLINTNEXTLINE(readability-redundant-declaration)
  12278. friend bool operator==(json_pointer<RefStringTypeLhs> const& lhs,
  12279. json_pointer<RefStringTypeRhs> const& rhs) noexcept;
  12280. /*!
  12281. @brief compares two JSON pointers for inequality
  12282. @param[in] lhs JSON pointer to compare
  12283. @param[in] rhs JSON pointer to compare
  12284. @return whether @a lhs is not equal @a rhs
  12285. @complexity Linear in the length of the JSON pointer
  12286. @exceptionsafety No-throw guarantee: this function never throws exceptions.
  12287. */
  12288. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  12289. // NOLINTNEXTLINE(readability-redundant-declaration)
  12290. friend bool operator!=(json_pointer<RefStringTypeLhs> const& lhs,
  12291. json_pointer<RefStringTypeRhs> const& rhs) noexcept;
  12292. /// the reference tokens
  12293. std::vector<string_t> reference_tokens;
  12294. };
  12295. // functions cannot be defined inside class due to ODR violations
  12296. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  12297. inline bool operator==(json_pointer<RefStringTypeLhs> const& lhs,
  12298. json_pointer<RefStringTypeRhs> const& rhs) noexcept
  12299. {
  12300. return lhs.reference_tokens == rhs.reference_tokens;
  12301. }
  12302. template<typename RefStringTypeLhs, typename RefStringTypeRhs>
  12303. inline bool operator!=(json_pointer<RefStringTypeLhs> const& lhs,
  12304. json_pointer<RefStringTypeRhs> const& rhs) noexcept
  12305. {
  12306. return !(lhs == rhs);
  12307. }
  12308. } // namespace nlohmann
  12309. // #include <nlohmann/detail/json_ref.hpp>
  12310. #include <initializer_list>
  12311. #include <utility>
  12312. // #include <nlohmann/detail/meta/type_traits.hpp>
  12313. namespace nlohmann
  12314. {
  12315. namespace detail
  12316. {
  12317. template<typename BasicJsonType>
  12318. class json_ref
  12319. {
  12320. public:
  12321. using value_type = BasicJsonType;
  12322. json_ref(value_type&& value)
  12323. : owned_value(std::move(value))
  12324. {}
  12325. json_ref(const value_type& value)
  12326. : value_ref(&value)
  12327. {}
  12328. json_ref(std::initializer_list<json_ref> init)
  12329. : owned_value(init)
  12330. {}
  12331. template <
  12332. class... Args,
  12333. enable_if_t<std::is_constructible<value_type, Args...>::value, int> = 0 >
  12334. json_ref(Args && ... args)
  12335. : owned_value(std::forward<Args>(args)...)
  12336. {}
  12337. // class should be movable only
  12338. json_ref(json_ref&&) noexcept = default;
  12339. json_ref(const json_ref&) = delete;
  12340. json_ref& operator=(const json_ref&) = delete;
  12341. json_ref& operator=(json_ref&&) = delete;
  12342. ~json_ref() = default;
  12343. value_type moved_or_copied() const
  12344. {
  12345. if (value_ref == nullptr)
  12346. {
  12347. return std::move(owned_value);
  12348. }
  12349. return *value_ref;
  12350. }
  12351. value_type const& operator*() const
  12352. {
  12353. return value_ref ? *value_ref : owned_value;
  12354. }
  12355. value_type const* operator->() const
  12356. {
  12357. return &** this;
  12358. }
  12359. private:
  12360. mutable value_type owned_value = nullptr;
  12361. value_type const* value_ref = nullptr;
  12362. };
  12363. } // namespace detail
  12364. } // namespace nlohmann
  12365. // #include <nlohmann/detail/macro_scope.hpp>
  12366. // #include <nlohmann/detail/string_concat.hpp>
  12367. // #include <nlohmann/detail/string_escape.hpp>
  12368. // #include <nlohmann/detail/meta/cpp_future.hpp>
  12369. // #include <nlohmann/detail/meta/type_traits.hpp>
  12370. // #include <nlohmann/detail/output/binary_writer.hpp>
  12371. #include <algorithm> // reverse
  12372. #include <array> // array
  12373. #include <map> // map
  12374. #include <cmath> // isnan, isinf
  12375. #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
  12376. #include <cstring> // memcpy
  12377. #include <limits> // numeric_limits
  12378. #include <string> // string
  12379. #include <utility> // move
  12380. #include <vector> // vector
  12381. // #include <nlohmann/detail/input/binary_reader.hpp>
  12382. // #include <nlohmann/detail/macro_scope.hpp>
  12383. // #include <nlohmann/detail/output/output_adapters.hpp>
  12384. #include <algorithm> // copy
  12385. #include <cstddef> // size_t
  12386. #include <iterator> // back_inserter
  12387. #include <memory> // shared_ptr, make_shared
  12388. #include <string> // basic_string
  12389. #include <vector> // vector
  12390. #ifndef JSON_NO_IO
  12391. #include <ios> // streamsize
  12392. #include <ostream> // basic_ostream
  12393. #endif // JSON_NO_IO
  12394. // #include <nlohmann/detail/macro_scope.hpp>
  12395. namespace nlohmann
  12396. {
  12397. namespace detail
  12398. {
  12399. /// abstract output adapter interface
  12400. template<typename CharType> struct output_adapter_protocol
  12401. {
  12402. virtual void write_character(CharType c) = 0;
  12403. virtual void write_characters(const CharType* s, std::size_t length) = 0;
  12404. virtual ~output_adapter_protocol() = default;
  12405. output_adapter_protocol() = default;
  12406. output_adapter_protocol(const output_adapter_protocol&) = default;
  12407. output_adapter_protocol(output_adapter_protocol&&) noexcept = default;
  12408. output_adapter_protocol& operator=(const output_adapter_protocol&) = default;
  12409. output_adapter_protocol& operator=(output_adapter_protocol&&) noexcept = default;
  12410. };
  12411. /// a type to simplify interfaces
  12412. template<typename CharType>
  12413. using output_adapter_t = std::shared_ptr<output_adapter_protocol<CharType>>;
  12414. /// output adapter for byte vectors
  12415. template<typename CharType, typename AllocatorType = std::allocator<CharType>>
  12416. class output_vector_adapter : public output_adapter_protocol<CharType>
  12417. {
  12418. public:
  12419. explicit output_vector_adapter(std::vector<CharType, AllocatorType>& vec) noexcept
  12420. : v(vec)
  12421. {}
  12422. void write_character(CharType c) override
  12423. {
  12424. v.push_back(c);
  12425. }
  12426. JSON_HEDLEY_NON_NULL(2)
  12427. void write_characters(const CharType* s, std::size_t length) override
  12428. {
  12429. std::copy(s, s + length, std::back_inserter(v));
  12430. }
  12431. private:
  12432. std::vector<CharType, AllocatorType>& v;
  12433. };
  12434. #ifndef JSON_NO_IO
  12435. /// output adapter for output streams
  12436. template<typename CharType>
  12437. class output_stream_adapter : public output_adapter_protocol<CharType>
  12438. {
  12439. public:
  12440. explicit output_stream_adapter(std::basic_ostream<CharType>& s) noexcept
  12441. : stream(s)
  12442. {}
  12443. void write_character(CharType c) override
  12444. {
  12445. stream.put(c);
  12446. }
  12447. JSON_HEDLEY_NON_NULL(2)
  12448. void write_characters(const CharType* s, std::size_t length) override
  12449. {
  12450. stream.write(s, static_cast<std::streamsize>(length));
  12451. }
  12452. private:
  12453. std::basic_ostream<CharType>& stream;
  12454. };
  12455. #endif // JSON_NO_IO
  12456. /// output adapter for basic_string
  12457. template<typename CharType, typename StringType = std::basic_string<CharType>>
  12458. class output_string_adapter : public output_adapter_protocol<CharType>
  12459. {
  12460. public:
  12461. explicit output_string_adapter(StringType& s) noexcept
  12462. : str(s)
  12463. {}
  12464. void write_character(CharType c) override
  12465. {
  12466. str.push_back(c);
  12467. }
  12468. JSON_HEDLEY_NON_NULL(2)
  12469. void write_characters(const CharType* s, std::size_t length) override
  12470. {
  12471. str.append(s, length);
  12472. }
  12473. private:
  12474. StringType& str;
  12475. };
  12476. template<typename CharType, typename StringType = std::basic_string<CharType>>
  12477. class output_adapter
  12478. {
  12479. public:
  12480. template<typename AllocatorType = std::allocator<CharType>>
  12481. output_adapter(std::vector<CharType, AllocatorType>& vec)
  12482. : oa(std::make_shared<output_vector_adapter<CharType, AllocatorType>>(vec)) {}
  12483. #ifndef JSON_NO_IO
  12484. output_adapter(std::basic_ostream<CharType>& s)
  12485. : oa(std::make_shared<output_stream_adapter<CharType>>(s)) {}
  12486. #endif // JSON_NO_IO
  12487. output_adapter(StringType& s)
  12488. : oa(std::make_shared<output_string_adapter<CharType, StringType>>(s)) {}
  12489. operator output_adapter_t<CharType>()
  12490. {
  12491. return oa;
  12492. }
  12493. private:
  12494. output_adapter_t<CharType> oa = nullptr;
  12495. };
  12496. } // namespace detail
  12497. } // namespace nlohmann
  12498. // #include <nlohmann/detail/string_concat.hpp>
  12499. namespace nlohmann
  12500. {
  12501. namespace detail
  12502. {
  12503. ///////////////////
  12504. // binary writer //
  12505. ///////////////////
  12506. /*!
  12507. @brief serialization to CBOR and MessagePack values
  12508. */
  12509. template<typename BasicJsonType, typename CharType>
  12510. class binary_writer
  12511. {
  12512. using string_t = typename BasicJsonType::string_t;
  12513. using binary_t = typename BasicJsonType::binary_t;
  12514. using number_float_t = typename BasicJsonType::number_float_t;
  12515. public:
  12516. /*!
  12517. @brief create a binary writer
  12518. @param[in] adapter output adapter to write to
  12519. */
  12520. explicit binary_writer(output_adapter_t<CharType> adapter) : oa(std::move(adapter))
  12521. {
  12522. JSON_ASSERT(oa);
  12523. }
  12524. /*!
  12525. @param[in] j JSON value to serialize
  12526. @pre j.type() == value_t::object
  12527. */
  12528. void write_bson(const BasicJsonType& j)
  12529. {
  12530. switch (j.type())
  12531. {
  12532. case value_t::object:
  12533. {
  12534. write_bson_object(*j.m_value.object);
  12535. break;
  12536. }
  12537. case value_t::null:
  12538. case value_t::array:
  12539. case value_t::string:
  12540. case value_t::boolean:
  12541. case value_t::number_integer:
  12542. case value_t::number_unsigned:
  12543. case value_t::number_float:
  12544. case value_t::binary:
  12545. case value_t::discarded:
  12546. default:
  12547. {
  12548. JSON_THROW(type_error::create(317, concat("to serialize to BSON, top-level type must be object, but is ", j.type_name()), &j));
  12549. }
  12550. }
  12551. }
  12552. /*!
  12553. @param[in] j JSON value to serialize
  12554. */
  12555. void write_cbor(const BasicJsonType& j)
  12556. {
  12557. switch (j.type())
  12558. {
  12559. case value_t::null:
  12560. {
  12561. oa->write_character(to_char_type(0xF6));
  12562. break;
  12563. }
  12564. case value_t::boolean:
  12565. {
  12566. oa->write_character(j.m_value.boolean
  12567. ? to_char_type(0xF5)
  12568. : to_char_type(0xF4));
  12569. break;
  12570. }
  12571. case value_t::number_integer:
  12572. {
  12573. if (j.m_value.number_integer >= 0)
  12574. {
  12575. // CBOR does not differentiate between positive signed
  12576. // integers and unsigned integers. Therefore, we used the
  12577. // code from the value_t::number_unsigned case here.
  12578. if (j.m_value.number_integer <= 0x17)
  12579. {
  12580. write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
  12581. }
  12582. else if (j.m_value.number_integer <= (std::numeric_limits<std::uint8_t>::max)())
  12583. {
  12584. oa->write_character(to_char_type(0x18));
  12585. write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
  12586. }
  12587. else if (j.m_value.number_integer <= (std::numeric_limits<std::uint16_t>::max)())
  12588. {
  12589. oa->write_character(to_char_type(0x19));
  12590. write_number(static_cast<std::uint16_t>(j.m_value.number_integer));
  12591. }
  12592. else if (j.m_value.number_integer <= (std::numeric_limits<std::uint32_t>::max)())
  12593. {
  12594. oa->write_character(to_char_type(0x1A));
  12595. write_number(static_cast<std::uint32_t>(j.m_value.number_integer));
  12596. }
  12597. else
  12598. {
  12599. oa->write_character(to_char_type(0x1B));
  12600. write_number(static_cast<std::uint64_t>(j.m_value.number_integer));
  12601. }
  12602. }
  12603. else
  12604. {
  12605. // The conversions below encode the sign in the first
  12606. // byte, and the value is converted to a positive number.
  12607. const auto positive_number = -1 - j.m_value.number_integer;
  12608. if (j.m_value.number_integer >= -24)
  12609. {
  12610. write_number(static_cast<std::uint8_t>(0x20 + positive_number));
  12611. }
  12612. else if (positive_number <= (std::numeric_limits<std::uint8_t>::max)())
  12613. {
  12614. oa->write_character(to_char_type(0x38));
  12615. write_number(static_cast<std::uint8_t>(positive_number));
  12616. }
  12617. else if (positive_number <= (std::numeric_limits<std::uint16_t>::max)())
  12618. {
  12619. oa->write_character(to_char_type(0x39));
  12620. write_number(static_cast<std::uint16_t>(positive_number));
  12621. }
  12622. else if (positive_number <= (std::numeric_limits<std::uint32_t>::max)())
  12623. {
  12624. oa->write_character(to_char_type(0x3A));
  12625. write_number(static_cast<std::uint32_t>(positive_number));
  12626. }
  12627. else
  12628. {
  12629. oa->write_character(to_char_type(0x3B));
  12630. write_number(static_cast<std::uint64_t>(positive_number));
  12631. }
  12632. }
  12633. break;
  12634. }
  12635. case value_t::number_unsigned:
  12636. {
  12637. if (j.m_value.number_unsigned <= 0x17)
  12638. {
  12639. write_number(static_cast<std::uint8_t>(j.m_value.number_unsigned));
  12640. }
  12641. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
  12642. {
  12643. oa->write_character(to_char_type(0x18));
  12644. write_number(static_cast<std::uint8_t>(j.m_value.number_unsigned));
  12645. }
  12646. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
  12647. {
  12648. oa->write_character(to_char_type(0x19));
  12649. write_number(static_cast<std::uint16_t>(j.m_value.number_unsigned));
  12650. }
  12651. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
  12652. {
  12653. oa->write_character(to_char_type(0x1A));
  12654. write_number(static_cast<std::uint32_t>(j.m_value.number_unsigned));
  12655. }
  12656. else
  12657. {
  12658. oa->write_character(to_char_type(0x1B));
  12659. write_number(static_cast<std::uint64_t>(j.m_value.number_unsigned));
  12660. }
  12661. break;
  12662. }
  12663. case value_t::number_float:
  12664. {
  12665. if (std::isnan(j.m_value.number_float))
  12666. {
  12667. // NaN is 0xf97e00 in CBOR
  12668. oa->write_character(to_char_type(0xF9));
  12669. oa->write_character(to_char_type(0x7E));
  12670. oa->write_character(to_char_type(0x00));
  12671. }
  12672. else if (std::isinf(j.m_value.number_float))
  12673. {
  12674. // Infinity is 0xf97c00, -Infinity is 0xf9fc00
  12675. oa->write_character(to_char_type(0xf9));
  12676. oa->write_character(j.m_value.number_float > 0 ? to_char_type(0x7C) : to_char_type(0xFC));
  12677. oa->write_character(to_char_type(0x00));
  12678. }
  12679. else
  12680. {
  12681. write_compact_float(j.m_value.number_float, detail::input_format_t::cbor);
  12682. }
  12683. break;
  12684. }
  12685. case value_t::string:
  12686. {
  12687. // step 1: write control byte and the string length
  12688. const auto N = j.m_value.string->size();
  12689. if (N <= 0x17)
  12690. {
  12691. write_number(static_cast<std::uint8_t>(0x60 + N));
  12692. }
  12693. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  12694. {
  12695. oa->write_character(to_char_type(0x78));
  12696. write_number(static_cast<std::uint8_t>(N));
  12697. }
  12698. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  12699. {
  12700. oa->write_character(to_char_type(0x79));
  12701. write_number(static_cast<std::uint16_t>(N));
  12702. }
  12703. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  12704. {
  12705. oa->write_character(to_char_type(0x7A));
  12706. write_number(static_cast<std::uint32_t>(N));
  12707. }
  12708. // LCOV_EXCL_START
  12709. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  12710. {
  12711. oa->write_character(to_char_type(0x7B));
  12712. write_number(static_cast<std::uint64_t>(N));
  12713. }
  12714. // LCOV_EXCL_STOP
  12715. // step 2: write the string
  12716. oa->write_characters(
  12717. reinterpret_cast<const CharType*>(j.m_value.string->c_str()),
  12718. j.m_value.string->size());
  12719. break;
  12720. }
  12721. case value_t::array:
  12722. {
  12723. // step 1: write control byte and the array size
  12724. const auto N = j.m_value.array->size();
  12725. if (N <= 0x17)
  12726. {
  12727. write_number(static_cast<std::uint8_t>(0x80 + N));
  12728. }
  12729. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  12730. {
  12731. oa->write_character(to_char_type(0x98));
  12732. write_number(static_cast<std::uint8_t>(N));
  12733. }
  12734. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  12735. {
  12736. oa->write_character(to_char_type(0x99));
  12737. write_number(static_cast<std::uint16_t>(N));
  12738. }
  12739. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  12740. {
  12741. oa->write_character(to_char_type(0x9A));
  12742. write_number(static_cast<std::uint32_t>(N));
  12743. }
  12744. // LCOV_EXCL_START
  12745. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  12746. {
  12747. oa->write_character(to_char_type(0x9B));
  12748. write_number(static_cast<std::uint64_t>(N));
  12749. }
  12750. // LCOV_EXCL_STOP
  12751. // step 2: write each element
  12752. for (const auto& el : *j.m_value.array)
  12753. {
  12754. write_cbor(el);
  12755. }
  12756. break;
  12757. }
  12758. case value_t::binary:
  12759. {
  12760. if (j.m_value.binary->has_subtype())
  12761. {
  12762. if (j.m_value.binary->subtype() <= (std::numeric_limits<std::uint8_t>::max)())
  12763. {
  12764. write_number(static_cast<std::uint8_t>(0xd8));
  12765. write_number(static_cast<std::uint8_t>(j.m_value.binary->subtype()));
  12766. }
  12767. else if (j.m_value.binary->subtype() <= (std::numeric_limits<std::uint16_t>::max)())
  12768. {
  12769. write_number(static_cast<std::uint8_t>(0xd9));
  12770. write_number(static_cast<std::uint16_t>(j.m_value.binary->subtype()));
  12771. }
  12772. else if (j.m_value.binary->subtype() <= (std::numeric_limits<std::uint32_t>::max)())
  12773. {
  12774. write_number(static_cast<std::uint8_t>(0xda));
  12775. write_number(static_cast<std::uint32_t>(j.m_value.binary->subtype()));
  12776. }
  12777. else if (j.m_value.binary->subtype() <= (std::numeric_limits<std::uint64_t>::max)())
  12778. {
  12779. write_number(static_cast<std::uint8_t>(0xdb));
  12780. write_number(static_cast<std::uint64_t>(j.m_value.binary->subtype()));
  12781. }
  12782. }
  12783. // step 1: write control byte and the binary array size
  12784. const auto N = j.m_value.binary->size();
  12785. if (N <= 0x17)
  12786. {
  12787. write_number(static_cast<std::uint8_t>(0x40 + N));
  12788. }
  12789. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  12790. {
  12791. oa->write_character(to_char_type(0x58));
  12792. write_number(static_cast<std::uint8_t>(N));
  12793. }
  12794. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  12795. {
  12796. oa->write_character(to_char_type(0x59));
  12797. write_number(static_cast<std::uint16_t>(N));
  12798. }
  12799. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  12800. {
  12801. oa->write_character(to_char_type(0x5A));
  12802. write_number(static_cast<std::uint32_t>(N));
  12803. }
  12804. // LCOV_EXCL_START
  12805. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  12806. {
  12807. oa->write_character(to_char_type(0x5B));
  12808. write_number(static_cast<std::uint64_t>(N));
  12809. }
  12810. // LCOV_EXCL_STOP
  12811. // step 2: write each element
  12812. oa->write_characters(
  12813. reinterpret_cast<const CharType*>(j.m_value.binary->data()),
  12814. N);
  12815. break;
  12816. }
  12817. case value_t::object:
  12818. {
  12819. // step 1: write control byte and the object size
  12820. const auto N = j.m_value.object->size();
  12821. if (N <= 0x17)
  12822. {
  12823. write_number(static_cast<std::uint8_t>(0xA0 + N));
  12824. }
  12825. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  12826. {
  12827. oa->write_character(to_char_type(0xB8));
  12828. write_number(static_cast<std::uint8_t>(N));
  12829. }
  12830. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  12831. {
  12832. oa->write_character(to_char_type(0xB9));
  12833. write_number(static_cast<std::uint16_t>(N));
  12834. }
  12835. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  12836. {
  12837. oa->write_character(to_char_type(0xBA));
  12838. write_number(static_cast<std::uint32_t>(N));
  12839. }
  12840. // LCOV_EXCL_START
  12841. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  12842. {
  12843. oa->write_character(to_char_type(0xBB));
  12844. write_number(static_cast<std::uint64_t>(N));
  12845. }
  12846. // LCOV_EXCL_STOP
  12847. // step 2: write each element
  12848. for (const auto& el : *j.m_value.object)
  12849. {
  12850. write_cbor(el.first);
  12851. write_cbor(el.second);
  12852. }
  12853. break;
  12854. }
  12855. case value_t::discarded:
  12856. default:
  12857. break;
  12858. }
  12859. }
  12860. /*!
  12861. @param[in] j JSON value to serialize
  12862. */
  12863. void write_msgpack(const BasicJsonType& j)
  12864. {
  12865. switch (j.type())
  12866. {
  12867. case value_t::null: // nil
  12868. {
  12869. oa->write_character(to_char_type(0xC0));
  12870. break;
  12871. }
  12872. case value_t::boolean: // true and false
  12873. {
  12874. oa->write_character(j.m_value.boolean
  12875. ? to_char_type(0xC3)
  12876. : to_char_type(0xC2));
  12877. break;
  12878. }
  12879. case value_t::number_integer:
  12880. {
  12881. if (j.m_value.number_integer >= 0)
  12882. {
  12883. // MessagePack does not differentiate between positive
  12884. // signed integers and unsigned integers. Therefore, we used
  12885. // the code from the value_t::number_unsigned case here.
  12886. if (j.m_value.number_unsigned < 128)
  12887. {
  12888. // positive fixnum
  12889. write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
  12890. }
  12891. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
  12892. {
  12893. // uint 8
  12894. oa->write_character(to_char_type(0xCC));
  12895. write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
  12896. }
  12897. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
  12898. {
  12899. // uint 16
  12900. oa->write_character(to_char_type(0xCD));
  12901. write_number(static_cast<std::uint16_t>(j.m_value.number_integer));
  12902. }
  12903. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
  12904. {
  12905. // uint 32
  12906. oa->write_character(to_char_type(0xCE));
  12907. write_number(static_cast<std::uint32_t>(j.m_value.number_integer));
  12908. }
  12909. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
  12910. {
  12911. // uint 64
  12912. oa->write_character(to_char_type(0xCF));
  12913. write_number(static_cast<std::uint64_t>(j.m_value.number_integer));
  12914. }
  12915. }
  12916. else
  12917. {
  12918. if (j.m_value.number_integer >= -32)
  12919. {
  12920. // negative fixnum
  12921. write_number(static_cast<std::int8_t>(j.m_value.number_integer));
  12922. }
  12923. else if (j.m_value.number_integer >= (std::numeric_limits<std::int8_t>::min)() &&
  12924. j.m_value.number_integer <= (std::numeric_limits<std::int8_t>::max)())
  12925. {
  12926. // int 8
  12927. oa->write_character(to_char_type(0xD0));
  12928. write_number(static_cast<std::int8_t>(j.m_value.number_integer));
  12929. }
  12930. else if (j.m_value.number_integer >= (std::numeric_limits<std::int16_t>::min)() &&
  12931. j.m_value.number_integer <= (std::numeric_limits<std::int16_t>::max)())
  12932. {
  12933. // int 16
  12934. oa->write_character(to_char_type(0xD1));
  12935. write_number(static_cast<std::int16_t>(j.m_value.number_integer));
  12936. }
  12937. else if (j.m_value.number_integer >= (std::numeric_limits<std::int32_t>::min)() &&
  12938. j.m_value.number_integer <= (std::numeric_limits<std::int32_t>::max)())
  12939. {
  12940. // int 32
  12941. oa->write_character(to_char_type(0xD2));
  12942. write_number(static_cast<std::int32_t>(j.m_value.number_integer));
  12943. }
  12944. else if (j.m_value.number_integer >= (std::numeric_limits<std::int64_t>::min)() &&
  12945. j.m_value.number_integer <= (std::numeric_limits<std::int64_t>::max)())
  12946. {
  12947. // int 64
  12948. oa->write_character(to_char_type(0xD3));
  12949. write_number(static_cast<std::int64_t>(j.m_value.number_integer));
  12950. }
  12951. }
  12952. break;
  12953. }
  12954. case value_t::number_unsigned:
  12955. {
  12956. if (j.m_value.number_unsigned < 128)
  12957. {
  12958. // positive fixnum
  12959. write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
  12960. }
  12961. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
  12962. {
  12963. // uint 8
  12964. oa->write_character(to_char_type(0xCC));
  12965. write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
  12966. }
  12967. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
  12968. {
  12969. // uint 16
  12970. oa->write_character(to_char_type(0xCD));
  12971. write_number(static_cast<std::uint16_t>(j.m_value.number_integer));
  12972. }
  12973. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
  12974. {
  12975. // uint 32
  12976. oa->write_character(to_char_type(0xCE));
  12977. write_number(static_cast<std::uint32_t>(j.m_value.number_integer));
  12978. }
  12979. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
  12980. {
  12981. // uint 64
  12982. oa->write_character(to_char_type(0xCF));
  12983. write_number(static_cast<std::uint64_t>(j.m_value.number_integer));
  12984. }
  12985. break;
  12986. }
  12987. case value_t::number_float:
  12988. {
  12989. write_compact_float(j.m_value.number_float, detail::input_format_t::msgpack);
  12990. break;
  12991. }
  12992. case value_t::string:
  12993. {
  12994. // step 1: write control byte and the string length
  12995. const auto N = j.m_value.string->size();
  12996. if (N <= 31)
  12997. {
  12998. // fixstr
  12999. write_number(static_cast<std::uint8_t>(0xA0 | N));
  13000. }
  13001. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13002. {
  13003. // str 8
  13004. oa->write_character(to_char_type(0xD9));
  13005. write_number(static_cast<std::uint8_t>(N));
  13006. }
  13007. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13008. {
  13009. // str 16
  13010. oa->write_character(to_char_type(0xDA));
  13011. write_number(static_cast<std::uint16_t>(N));
  13012. }
  13013. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13014. {
  13015. // str 32
  13016. oa->write_character(to_char_type(0xDB));
  13017. write_number(static_cast<std::uint32_t>(N));
  13018. }
  13019. // step 2: write the string
  13020. oa->write_characters(
  13021. reinterpret_cast<const CharType*>(j.m_value.string->c_str()),
  13022. j.m_value.string->size());
  13023. break;
  13024. }
  13025. case value_t::array:
  13026. {
  13027. // step 1: write control byte and the array size
  13028. const auto N = j.m_value.array->size();
  13029. if (N <= 15)
  13030. {
  13031. // fixarray
  13032. write_number(static_cast<std::uint8_t>(0x90 | N));
  13033. }
  13034. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13035. {
  13036. // array 16
  13037. oa->write_character(to_char_type(0xDC));
  13038. write_number(static_cast<std::uint16_t>(N));
  13039. }
  13040. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13041. {
  13042. // array 32
  13043. oa->write_character(to_char_type(0xDD));
  13044. write_number(static_cast<std::uint32_t>(N));
  13045. }
  13046. // step 2: write each element
  13047. for (const auto& el : *j.m_value.array)
  13048. {
  13049. write_msgpack(el);
  13050. }
  13051. break;
  13052. }
  13053. case value_t::binary:
  13054. {
  13055. // step 0: determine if the binary type has a set subtype to
  13056. // determine whether or not to use the ext or fixext types
  13057. const bool use_ext = j.m_value.binary->has_subtype();
  13058. // step 1: write control byte and the byte string length
  13059. const auto N = j.m_value.binary->size();
  13060. if (N <= (std::numeric_limits<std::uint8_t>::max)())
  13061. {
  13062. std::uint8_t output_type{};
  13063. bool fixed = true;
  13064. if (use_ext)
  13065. {
  13066. switch (N)
  13067. {
  13068. case 1:
  13069. output_type = 0xD4; // fixext 1
  13070. break;
  13071. case 2:
  13072. output_type = 0xD5; // fixext 2
  13073. break;
  13074. case 4:
  13075. output_type = 0xD6; // fixext 4
  13076. break;
  13077. case 8:
  13078. output_type = 0xD7; // fixext 8
  13079. break;
  13080. case 16:
  13081. output_type = 0xD8; // fixext 16
  13082. break;
  13083. default:
  13084. output_type = 0xC7; // ext 8
  13085. fixed = false;
  13086. break;
  13087. }
  13088. }
  13089. else
  13090. {
  13091. output_type = 0xC4; // bin 8
  13092. fixed = false;
  13093. }
  13094. oa->write_character(to_char_type(output_type));
  13095. if (!fixed)
  13096. {
  13097. write_number(static_cast<std::uint8_t>(N));
  13098. }
  13099. }
  13100. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13101. {
  13102. std::uint8_t output_type = use_ext
  13103. ? 0xC8 // ext 16
  13104. : 0xC5; // bin 16
  13105. oa->write_character(to_char_type(output_type));
  13106. write_number(static_cast<std::uint16_t>(N));
  13107. }
  13108. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13109. {
  13110. std::uint8_t output_type = use_ext
  13111. ? 0xC9 // ext 32
  13112. : 0xC6; // bin 32
  13113. oa->write_character(to_char_type(output_type));
  13114. write_number(static_cast<std::uint32_t>(N));
  13115. }
  13116. // step 1.5: if this is an ext type, write the subtype
  13117. if (use_ext)
  13118. {
  13119. write_number(static_cast<std::int8_t>(j.m_value.binary->subtype()));
  13120. }
  13121. // step 2: write the byte string
  13122. oa->write_characters(
  13123. reinterpret_cast<const CharType*>(j.m_value.binary->data()),
  13124. N);
  13125. break;
  13126. }
  13127. case value_t::object:
  13128. {
  13129. // step 1: write control byte and the object size
  13130. const auto N = j.m_value.object->size();
  13131. if (N <= 15)
  13132. {
  13133. // fixmap
  13134. write_number(static_cast<std::uint8_t>(0x80 | (N & 0xF)));
  13135. }
  13136. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  13137. {
  13138. // map 16
  13139. oa->write_character(to_char_type(0xDE));
  13140. write_number(static_cast<std::uint16_t>(N));
  13141. }
  13142. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  13143. {
  13144. // map 32
  13145. oa->write_character(to_char_type(0xDF));
  13146. write_number(static_cast<std::uint32_t>(N));
  13147. }
  13148. // step 2: write each element
  13149. for (const auto& el : *j.m_value.object)
  13150. {
  13151. write_msgpack(el.first);
  13152. write_msgpack(el.second);
  13153. }
  13154. break;
  13155. }
  13156. case value_t::discarded:
  13157. default:
  13158. break;
  13159. }
  13160. }
  13161. /*!
  13162. @param[in] j JSON value to serialize
  13163. @param[in] use_count whether to use '#' prefixes (optimized format)
  13164. @param[in] use_type whether to use '$' prefixes (optimized format)
  13165. @param[in] add_prefix whether prefixes need to be used for this value
  13166. @param[in] use_bjdata whether write in BJData format, default is false
  13167. */
  13168. void write_ubjson(const BasicJsonType& j, const bool use_count,
  13169. const bool use_type, const bool add_prefix = true,
  13170. const bool use_bjdata = false)
  13171. {
  13172. switch (j.type())
  13173. {
  13174. case value_t::null:
  13175. {
  13176. if (add_prefix)
  13177. {
  13178. oa->write_character(to_char_type('Z'));
  13179. }
  13180. break;
  13181. }
  13182. case value_t::boolean:
  13183. {
  13184. if (add_prefix)
  13185. {
  13186. oa->write_character(j.m_value.boolean
  13187. ? to_char_type('T')
  13188. : to_char_type('F'));
  13189. }
  13190. break;
  13191. }
  13192. case value_t::number_integer:
  13193. {
  13194. write_number_with_ubjson_prefix(j.m_value.number_integer, add_prefix, use_bjdata);
  13195. break;
  13196. }
  13197. case value_t::number_unsigned:
  13198. {
  13199. write_number_with_ubjson_prefix(j.m_value.number_unsigned, add_prefix, use_bjdata);
  13200. break;
  13201. }
  13202. case value_t::number_float:
  13203. {
  13204. write_number_with_ubjson_prefix(j.m_value.number_float, add_prefix, use_bjdata);
  13205. break;
  13206. }
  13207. case value_t::string:
  13208. {
  13209. if (add_prefix)
  13210. {
  13211. oa->write_character(to_char_type('S'));
  13212. }
  13213. write_number_with_ubjson_prefix(j.m_value.string->size(), true, use_bjdata);
  13214. oa->write_characters(
  13215. reinterpret_cast<const CharType*>(j.m_value.string->c_str()),
  13216. j.m_value.string->size());
  13217. break;
  13218. }
  13219. case value_t::array:
  13220. {
  13221. if (add_prefix)
  13222. {
  13223. oa->write_character(to_char_type('['));
  13224. }
  13225. bool prefix_required = true;
  13226. if (use_type && !j.m_value.array->empty())
  13227. {
  13228. JSON_ASSERT(use_count);
  13229. const CharType first_prefix = ubjson_prefix(j.front(), use_bjdata);
  13230. const bool same_prefix = std::all_of(j.begin() + 1, j.end(),
  13231. [this, first_prefix, use_bjdata](const BasicJsonType & v)
  13232. {
  13233. return ubjson_prefix(v, use_bjdata) == first_prefix;
  13234. });
  13235. std::vector<CharType> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type
  13236. if (same_prefix && !(use_bjdata && std::find(bjdx.begin(), bjdx.end(), first_prefix) != bjdx.end()))
  13237. {
  13238. prefix_required = false;
  13239. oa->write_character(to_char_type('$'));
  13240. oa->write_character(first_prefix);
  13241. }
  13242. }
  13243. if (use_count)
  13244. {
  13245. oa->write_character(to_char_type('#'));
  13246. write_number_with_ubjson_prefix(j.m_value.array->size(), true, use_bjdata);
  13247. }
  13248. for (const auto& el : *j.m_value.array)
  13249. {
  13250. write_ubjson(el, use_count, use_type, prefix_required, use_bjdata);
  13251. }
  13252. if (!use_count)
  13253. {
  13254. oa->write_character(to_char_type(']'));
  13255. }
  13256. break;
  13257. }
  13258. case value_t::binary:
  13259. {
  13260. if (add_prefix)
  13261. {
  13262. oa->write_character(to_char_type('['));
  13263. }
  13264. if (use_type && !j.m_value.binary->empty())
  13265. {
  13266. JSON_ASSERT(use_count);
  13267. oa->write_character(to_char_type('$'));
  13268. oa->write_character('U');
  13269. }
  13270. if (use_count)
  13271. {
  13272. oa->write_character(to_char_type('#'));
  13273. write_number_with_ubjson_prefix(j.m_value.binary->size(), true, use_bjdata);
  13274. }
  13275. if (use_type)
  13276. {
  13277. oa->write_characters(
  13278. reinterpret_cast<const CharType*>(j.m_value.binary->data()),
  13279. j.m_value.binary->size());
  13280. }
  13281. else
  13282. {
  13283. for (size_t i = 0; i < j.m_value.binary->size(); ++i)
  13284. {
  13285. oa->write_character(to_char_type('U'));
  13286. oa->write_character(j.m_value.binary->data()[i]);
  13287. }
  13288. }
  13289. if (!use_count)
  13290. {
  13291. oa->write_character(to_char_type(']'));
  13292. }
  13293. break;
  13294. }
  13295. case value_t::object:
  13296. {
  13297. 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())
  13298. {
  13299. if (!write_bjdata_ndarray(*j.m_value.object, use_count, use_type)) // decode bjdata ndarray in the JData format (https://github.com/NeuroJSON/jdata)
  13300. {
  13301. break;
  13302. }
  13303. }
  13304. if (add_prefix)
  13305. {
  13306. oa->write_character(to_char_type('{'));
  13307. }
  13308. bool prefix_required = true;
  13309. if (use_type && !j.m_value.object->empty())
  13310. {
  13311. JSON_ASSERT(use_count);
  13312. const CharType first_prefix = ubjson_prefix(j.front(), use_bjdata);
  13313. const bool same_prefix = std::all_of(j.begin(), j.end(),
  13314. [this, first_prefix, use_bjdata](const BasicJsonType & v)
  13315. {
  13316. return ubjson_prefix(v, use_bjdata) == first_prefix;
  13317. });
  13318. std::vector<CharType> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type
  13319. if (same_prefix && !(use_bjdata && std::find(bjdx.begin(), bjdx.end(), first_prefix) != bjdx.end()))
  13320. {
  13321. prefix_required = false;
  13322. oa->write_character(to_char_type('$'));
  13323. oa->write_character(first_prefix);
  13324. }
  13325. }
  13326. if (use_count)
  13327. {
  13328. oa->write_character(to_char_type('#'));
  13329. write_number_with_ubjson_prefix(j.m_value.object->size(), true, use_bjdata);
  13330. }
  13331. for (const auto& el : *j.m_value.object)
  13332. {
  13333. write_number_with_ubjson_prefix(el.first.size(), true, use_bjdata);
  13334. oa->write_characters(
  13335. reinterpret_cast<const CharType*>(el.first.c_str()),
  13336. el.first.size());
  13337. write_ubjson(el.second, use_count, use_type, prefix_required, use_bjdata);
  13338. }
  13339. if (!use_count)
  13340. {
  13341. oa->write_character(to_char_type('}'));
  13342. }
  13343. break;
  13344. }
  13345. case value_t::discarded:
  13346. default:
  13347. break;
  13348. }
  13349. }
  13350. private:
  13351. //////////
  13352. // BSON //
  13353. //////////
  13354. /*!
  13355. @return The size of a BSON document entry header, including the id marker
  13356. and the entry name size (and its null-terminator).
  13357. */
  13358. static std::size_t calc_bson_entry_header_size(const string_t& name, const BasicJsonType& j)
  13359. {
  13360. const auto it = name.find(static_cast<typename string_t::value_type>(0));
  13361. if (JSON_HEDLEY_UNLIKELY(it != BasicJsonType::string_t::npos))
  13362. {
  13363. JSON_THROW(out_of_range::create(409, concat("BSON key cannot contain code point U+0000 (at byte ", std::to_string(it), ")"), &j));
  13364. static_cast<void>(j);
  13365. }
  13366. return /*id*/ 1ul + name.size() + /*zero-terminator*/1u;
  13367. }
  13368. /*!
  13369. @brief Writes the given @a element_type and @a name to the output adapter
  13370. */
  13371. void write_bson_entry_header(const string_t& name,
  13372. const std::uint8_t element_type)
  13373. {
  13374. oa->write_character(to_char_type(element_type)); // boolean
  13375. oa->write_characters(
  13376. reinterpret_cast<const CharType*>(name.c_str()),
  13377. name.size() + 1u);
  13378. }
  13379. /*!
  13380. @brief Writes a BSON element with key @a name and boolean value @a value
  13381. */
  13382. void write_bson_boolean(const string_t& name,
  13383. const bool value)
  13384. {
  13385. write_bson_entry_header(name, 0x08);
  13386. oa->write_character(value ? to_char_type(0x01) : to_char_type(0x00));
  13387. }
  13388. /*!
  13389. @brief Writes a BSON element with key @a name and double value @a value
  13390. */
  13391. void write_bson_double(const string_t& name,
  13392. const double value)
  13393. {
  13394. write_bson_entry_header(name, 0x01);
  13395. write_number<double>(value, true);
  13396. }
  13397. /*!
  13398. @return The size of the BSON-encoded string in @a value
  13399. */
  13400. static std::size_t calc_bson_string_size(const string_t& value)
  13401. {
  13402. return sizeof(std::int32_t) + value.size() + 1ul;
  13403. }
  13404. /*!
  13405. @brief Writes a BSON element with key @a name and string value @a value
  13406. */
  13407. void write_bson_string(const string_t& name,
  13408. const string_t& value)
  13409. {
  13410. write_bson_entry_header(name, 0x02);
  13411. write_number<std::int32_t>(static_cast<std::int32_t>(value.size() + 1ul), true);
  13412. oa->write_characters(
  13413. reinterpret_cast<const CharType*>(value.c_str()),
  13414. value.size() + 1);
  13415. }
  13416. /*!
  13417. @brief Writes a BSON element with key @a name and null value
  13418. */
  13419. void write_bson_null(const string_t& name)
  13420. {
  13421. write_bson_entry_header(name, 0x0A);
  13422. }
  13423. /*!
  13424. @return The size of the BSON-encoded integer @a value
  13425. */
  13426. static std::size_t calc_bson_integer_size(const std::int64_t value)
  13427. {
  13428. return (std::numeric_limits<std::int32_t>::min)() <= value && value <= (std::numeric_limits<std::int32_t>::max)()
  13429. ? sizeof(std::int32_t)
  13430. : sizeof(std::int64_t);
  13431. }
  13432. /*!
  13433. @brief Writes a BSON element with key @a name and integer @a value
  13434. */
  13435. void write_bson_integer(const string_t& name,
  13436. const std::int64_t value)
  13437. {
  13438. if ((std::numeric_limits<std::int32_t>::min)() <= value && value <= (std::numeric_limits<std::int32_t>::max)())
  13439. {
  13440. write_bson_entry_header(name, 0x10); // int32
  13441. write_number<std::int32_t>(static_cast<std::int32_t>(value), true);
  13442. }
  13443. else
  13444. {
  13445. write_bson_entry_header(name, 0x12); // int64
  13446. write_number<std::int64_t>(static_cast<std::int64_t>(value), true);
  13447. }
  13448. }
  13449. /*!
  13450. @return The size of the BSON-encoded unsigned integer in @a j
  13451. */
  13452. static constexpr std::size_t calc_bson_unsigned_size(const std::uint64_t value) noexcept
  13453. {
  13454. return (value <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  13455. ? sizeof(std::int32_t)
  13456. : sizeof(std::int64_t);
  13457. }
  13458. /*!
  13459. @brief Writes a BSON element with key @a name and unsigned @a value
  13460. */
  13461. void write_bson_unsigned(const string_t& name,
  13462. const BasicJsonType& j)
  13463. {
  13464. if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  13465. {
  13466. write_bson_entry_header(name, 0x10 /* int32 */);
  13467. write_number<std::int32_t>(static_cast<std::int32_t>(j.m_value.number_unsigned), true);
  13468. }
  13469. else if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
  13470. {
  13471. write_bson_entry_header(name, 0x12 /* int64 */);
  13472. write_number<std::int64_t>(static_cast<std::int64_t>(j.m_value.number_unsigned), true);
  13473. }
  13474. else
  13475. {
  13476. 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));
  13477. }
  13478. }
  13479. /*!
  13480. @brief Writes a BSON element with key @a name and object @a value
  13481. */
  13482. void write_bson_object_entry(const string_t& name,
  13483. const typename BasicJsonType::object_t& value)
  13484. {
  13485. write_bson_entry_header(name, 0x03); // object
  13486. write_bson_object(value);
  13487. }
  13488. /*!
  13489. @return The size of the BSON-encoded array @a value
  13490. */
  13491. static std::size_t calc_bson_array_size(const typename BasicJsonType::array_t& value)
  13492. {
  13493. std::size_t array_index = 0ul;
  13494. 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)
  13495. {
  13496. return result + calc_bson_element_size(std::to_string(array_index++), el);
  13497. });
  13498. return sizeof(std::int32_t) + embedded_document_size + 1ul;
  13499. }
  13500. /*!
  13501. @return The size of the BSON-encoded binary array @a value
  13502. */
  13503. static std::size_t calc_bson_binary_size(const typename BasicJsonType::binary_t& value)
  13504. {
  13505. return sizeof(std::int32_t) + value.size() + 1ul;
  13506. }
  13507. /*!
  13508. @brief Writes a BSON element with key @a name and array @a value
  13509. */
  13510. void write_bson_array(const string_t& name,
  13511. const typename BasicJsonType::array_t& value)
  13512. {
  13513. write_bson_entry_header(name, 0x04); // array
  13514. write_number<std::int32_t>(static_cast<std::int32_t>(calc_bson_array_size(value)), true);
  13515. std::size_t array_index = 0ul;
  13516. for (const auto& el : value)
  13517. {
  13518. write_bson_element(std::to_string(array_index++), el);
  13519. }
  13520. oa->write_character(to_char_type(0x00));
  13521. }
  13522. /*!
  13523. @brief Writes a BSON element with key @a name and binary value @a value
  13524. */
  13525. void write_bson_binary(const string_t& name,
  13526. const binary_t& value)
  13527. {
  13528. write_bson_entry_header(name, 0x05);
  13529. write_number<std::int32_t>(static_cast<std::int32_t>(value.size()), true);
  13530. write_number(value.has_subtype() ? static_cast<std::uint8_t>(value.subtype()) : static_cast<std::uint8_t>(0x00));
  13531. oa->write_characters(reinterpret_cast<const CharType*>(value.data()), value.size());
  13532. }
  13533. /*!
  13534. @brief Calculates the size necessary to serialize the JSON value @a j with its @a name
  13535. @return The calculated size for the BSON document entry for @a j with the given @a name.
  13536. */
  13537. static std::size_t calc_bson_element_size(const string_t& name,
  13538. const BasicJsonType& j)
  13539. {
  13540. const auto header_size = calc_bson_entry_header_size(name, j);
  13541. switch (j.type())
  13542. {
  13543. case value_t::object:
  13544. return header_size + calc_bson_object_size(*j.m_value.object);
  13545. case value_t::array:
  13546. return header_size + calc_bson_array_size(*j.m_value.array);
  13547. case value_t::binary:
  13548. return header_size + calc_bson_binary_size(*j.m_value.binary);
  13549. case value_t::boolean:
  13550. return header_size + 1ul;
  13551. case value_t::number_float:
  13552. return header_size + 8ul;
  13553. case value_t::number_integer:
  13554. return header_size + calc_bson_integer_size(j.m_value.number_integer);
  13555. case value_t::number_unsigned:
  13556. return header_size + calc_bson_unsigned_size(j.m_value.number_unsigned);
  13557. case value_t::string:
  13558. return header_size + calc_bson_string_size(*j.m_value.string);
  13559. case value_t::null:
  13560. return header_size + 0ul;
  13561. // LCOV_EXCL_START
  13562. case value_t::discarded:
  13563. default:
  13564. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert)
  13565. return 0ul;
  13566. // LCOV_EXCL_STOP
  13567. }
  13568. }
  13569. /*!
  13570. @brief Serializes the JSON value @a j to BSON and associates it with the
  13571. key @a name.
  13572. @param name The name to associate with the JSON entity @a j within the
  13573. current BSON document
  13574. */
  13575. void write_bson_element(const string_t& name,
  13576. const BasicJsonType& j)
  13577. {
  13578. switch (j.type())
  13579. {
  13580. case value_t::object:
  13581. return write_bson_object_entry(name, *j.m_value.object);
  13582. case value_t::array:
  13583. return write_bson_array(name, *j.m_value.array);
  13584. case value_t::binary:
  13585. return write_bson_binary(name, *j.m_value.binary);
  13586. case value_t::boolean:
  13587. return write_bson_boolean(name, j.m_value.boolean);
  13588. case value_t::number_float:
  13589. return write_bson_double(name, j.m_value.number_float);
  13590. case value_t::number_integer:
  13591. return write_bson_integer(name, j.m_value.number_integer);
  13592. case value_t::number_unsigned:
  13593. return write_bson_unsigned(name, j);
  13594. case value_t::string:
  13595. return write_bson_string(name, *j.m_value.string);
  13596. case value_t::null:
  13597. return write_bson_null(name);
  13598. // LCOV_EXCL_START
  13599. case value_t::discarded:
  13600. default:
  13601. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert)
  13602. return;
  13603. // LCOV_EXCL_STOP
  13604. }
  13605. }
  13606. /*!
  13607. @brief Calculates the size of the BSON serialization of the given
  13608. JSON-object @a j.
  13609. @param[in] value JSON value to serialize
  13610. @pre value.type() == value_t::object
  13611. */
  13612. static std::size_t calc_bson_object_size(const typename BasicJsonType::object_t& value)
  13613. {
  13614. std::size_t document_size = std::accumulate(value.begin(), value.end(), static_cast<std::size_t>(0),
  13615. [](size_t result, const typename BasicJsonType::object_t::value_type & el)
  13616. {
  13617. return result += calc_bson_element_size(el.first, el.second);
  13618. });
  13619. return sizeof(std::int32_t) + document_size + 1ul;
  13620. }
  13621. /*!
  13622. @param[in] value JSON value to serialize
  13623. @pre value.type() == value_t::object
  13624. */
  13625. void write_bson_object(const typename BasicJsonType::object_t& value)
  13626. {
  13627. write_number<std::int32_t>(static_cast<std::int32_t>(calc_bson_object_size(value)), true);
  13628. for (const auto& el : value)
  13629. {
  13630. write_bson_element(el.first, el.second);
  13631. }
  13632. oa->write_character(to_char_type(0x00));
  13633. }
  13634. //////////
  13635. // CBOR //
  13636. //////////
  13637. static constexpr CharType get_cbor_float_prefix(float /*unused*/)
  13638. {
  13639. return to_char_type(0xFA); // Single-Precision Float
  13640. }
  13641. static constexpr CharType get_cbor_float_prefix(double /*unused*/)
  13642. {
  13643. return to_char_type(0xFB); // Double-Precision Float
  13644. }
  13645. /////////////
  13646. // MsgPack //
  13647. /////////////
  13648. static constexpr CharType get_msgpack_float_prefix(float /*unused*/)
  13649. {
  13650. return to_char_type(0xCA); // float 32
  13651. }
  13652. static constexpr CharType get_msgpack_float_prefix(double /*unused*/)
  13653. {
  13654. return to_char_type(0xCB); // float 64
  13655. }
  13656. ////////////
  13657. // UBJSON //
  13658. ////////////
  13659. // UBJSON: write number (floating point)
  13660. template<typename NumberType, typename std::enable_if<
  13661. std::is_floating_point<NumberType>::value, int>::type = 0>
  13662. void write_number_with_ubjson_prefix(const NumberType n,
  13663. const bool add_prefix,
  13664. const bool use_bjdata)
  13665. {
  13666. if (add_prefix)
  13667. {
  13668. oa->write_character(get_ubjson_float_prefix(n));
  13669. }
  13670. write_number(n, use_bjdata);
  13671. }
  13672. // UBJSON: write number (unsigned integer)
  13673. template<typename NumberType, typename std::enable_if<
  13674. std::is_unsigned<NumberType>::value, int>::type = 0>
  13675. void write_number_with_ubjson_prefix(const NumberType n,
  13676. const bool add_prefix,
  13677. const bool use_bjdata)
  13678. {
  13679. if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int8_t>::max)()))
  13680. {
  13681. if (add_prefix)
  13682. {
  13683. oa->write_character(to_char_type('i')); // int8
  13684. }
  13685. write_number(static_cast<std::uint8_t>(n), use_bjdata);
  13686. }
  13687. else if (n <= (std::numeric_limits<std::uint8_t>::max)())
  13688. {
  13689. if (add_prefix)
  13690. {
  13691. oa->write_character(to_char_type('U')); // uint8
  13692. }
  13693. write_number(static_cast<std::uint8_t>(n), use_bjdata);
  13694. }
  13695. else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int16_t>::max)()))
  13696. {
  13697. if (add_prefix)
  13698. {
  13699. oa->write_character(to_char_type('I')); // int16
  13700. }
  13701. write_number(static_cast<std::int16_t>(n), use_bjdata);
  13702. }
  13703. else if (use_bjdata && n <= static_cast<uint64_t>((std::numeric_limits<uint16_t>::max)()))
  13704. {
  13705. if (add_prefix)
  13706. {
  13707. oa->write_character(to_char_type('u')); // uint16 - bjdata only
  13708. }
  13709. write_number(static_cast<std::uint16_t>(n), use_bjdata);
  13710. }
  13711. else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  13712. {
  13713. if (add_prefix)
  13714. {
  13715. oa->write_character(to_char_type('l')); // int32
  13716. }
  13717. write_number(static_cast<std::int32_t>(n), use_bjdata);
  13718. }
  13719. else if (use_bjdata && n <= static_cast<uint64_t>((std::numeric_limits<uint32_t>::max)()))
  13720. {
  13721. if (add_prefix)
  13722. {
  13723. oa->write_character(to_char_type('m')); // uint32 - bjdata only
  13724. }
  13725. write_number(static_cast<std::uint32_t>(n), use_bjdata);
  13726. }
  13727. else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
  13728. {
  13729. if (add_prefix)
  13730. {
  13731. oa->write_character(to_char_type('L')); // int64
  13732. }
  13733. write_number(static_cast<std::int64_t>(n), use_bjdata);
  13734. }
  13735. else if (use_bjdata && n <= (std::numeric_limits<uint64_t>::max)())
  13736. {
  13737. if (add_prefix)
  13738. {
  13739. oa->write_character(to_char_type('M')); // uint64 - bjdata only
  13740. }
  13741. write_number(static_cast<std::uint64_t>(n), use_bjdata);
  13742. }
  13743. else
  13744. {
  13745. if (add_prefix)
  13746. {
  13747. oa->write_character(to_char_type('H')); // high-precision number
  13748. }
  13749. const auto number = BasicJsonType(n).dump();
  13750. write_number_with_ubjson_prefix(number.size(), true, use_bjdata);
  13751. for (std::size_t i = 0; i < number.size(); ++i)
  13752. {
  13753. oa->write_character(to_char_type(static_cast<std::uint8_t>(number[i])));
  13754. }
  13755. }
  13756. }
  13757. // UBJSON: write number (signed integer)
  13758. template < typename NumberType, typename std::enable_if <
  13759. std::is_signed<NumberType>::value&&
  13760. !std::is_floating_point<NumberType>::value, int >::type = 0 >
  13761. void write_number_with_ubjson_prefix(const NumberType n,
  13762. const bool add_prefix,
  13763. const bool use_bjdata)
  13764. {
  13765. if ((std::numeric_limits<std::int8_t>::min)() <= n && n <= (std::numeric_limits<std::int8_t>::max)())
  13766. {
  13767. if (add_prefix)
  13768. {
  13769. oa->write_character(to_char_type('i')); // int8
  13770. }
  13771. write_number(static_cast<std::int8_t>(n), use_bjdata);
  13772. }
  13773. 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)()))
  13774. {
  13775. if (add_prefix)
  13776. {
  13777. oa->write_character(to_char_type('U')); // uint8
  13778. }
  13779. write_number(static_cast<std::uint8_t>(n), use_bjdata);
  13780. }
  13781. else if ((std::numeric_limits<std::int16_t>::min)() <= n && n <= (std::numeric_limits<std::int16_t>::max)())
  13782. {
  13783. if (add_prefix)
  13784. {
  13785. oa->write_character(to_char_type('I')); // int16
  13786. }
  13787. write_number(static_cast<std::int16_t>(n), use_bjdata);
  13788. }
  13789. 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)())))
  13790. {
  13791. if (add_prefix)
  13792. {
  13793. oa->write_character(to_char_type('u')); // uint16 - bjdata only
  13794. }
  13795. write_number(static_cast<uint16_t>(n), use_bjdata);
  13796. }
  13797. else if ((std::numeric_limits<std::int32_t>::min)() <= n && n <= (std::numeric_limits<std::int32_t>::max)())
  13798. {
  13799. if (add_prefix)
  13800. {
  13801. oa->write_character(to_char_type('l')); // int32
  13802. }
  13803. write_number(static_cast<std::int32_t>(n), use_bjdata);
  13804. }
  13805. 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)())))
  13806. {
  13807. if (add_prefix)
  13808. {
  13809. oa->write_character(to_char_type('m')); // uint32 - bjdata only
  13810. }
  13811. write_number(static_cast<uint32_t>(n), use_bjdata);
  13812. }
  13813. else if ((std::numeric_limits<std::int64_t>::min)() <= n && n <= (std::numeric_limits<std::int64_t>::max)())
  13814. {
  13815. if (add_prefix)
  13816. {
  13817. oa->write_character(to_char_type('L')); // int64
  13818. }
  13819. write_number(static_cast<std::int64_t>(n), use_bjdata);
  13820. }
  13821. // LCOV_EXCL_START
  13822. else
  13823. {
  13824. if (add_prefix)
  13825. {
  13826. oa->write_character(to_char_type('H')); // high-precision number
  13827. }
  13828. const auto number = BasicJsonType(n).dump();
  13829. write_number_with_ubjson_prefix(number.size(), true, use_bjdata);
  13830. for (std::size_t i = 0; i < number.size(); ++i)
  13831. {
  13832. oa->write_character(to_char_type(static_cast<std::uint8_t>(number[i])));
  13833. }
  13834. }
  13835. // LCOV_EXCL_STOP
  13836. }
  13837. /*!
  13838. @brief determine the type prefix of container values
  13839. */
  13840. CharType ubjson_prefix(const BasicJsonType& j, const bool use_bjdata) const noexcept
  13841. {
  13842. switch (j.type())
  13843. {
  13844. case value_t::null:
  13845. return 'Z';
  13846. case value_t::boolean:
  13847. return j.m_value.boolean ? 'T' : 'F';
  13848. case value_t::number_integer:
  13849. {
  13850. 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)())
  13851. {
  13852. return 'i';
  13853. }
  13854. 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)())
  13855. {
  13856. return 'U';
  13857. }
  13858. 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)())
  13859. {
  13860. return 'I';
  13861. }
  13862. 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)()))
  13863. {
  13864. return 'u';
  13865. }
  13866. 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)())
  13867. {
  13868. return 'l';
  13869. }
  13870. 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)()))
  13871. {
  13872. return 'm';
  13873. }
  13874. 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)())
  13875. {
  13876. return 'L';
  13877. }
  13878. // anything else is treated as high-precision number
  13879. return 'H'; // LCOV_EXCL_LINE
  13880. }
  13881. case value_t::number_unsigned:
  13882. {
  13883. if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int8_t>::max)()))
  13884. {
  13885. return 'i';
  13886. }
  13887. if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint8_t>::max)()))
  13888. {
  13889. return 'U';
  13890. }
  13891. if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int16_t>::max)()))
  13892. {
  13893. return 'I';
  13894. }
  13895. if (use_bjdata && j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint16_t>::max)()))
  13896. {
  13897. return 'u';
  13898. }
  13899. if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  13900. {
  13901. return 'l';
  13902. }
  13903. if (use_bjdata && j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint32_t>::max)()))
  13904. {
  13905. return 'm';
  13906. }
  13907. if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
  13908. {
  13909. return 'L';
  13910. }
  13911. if (use_bjdata && j.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
  13912. {
  13913. return 'M';
  13914. }
  13915. // anything else is treated as high-precision number
  13916. return 'H'; // LCOV_EXCL_LINE
  13917. }
  13918. case value_t::number_float:
  13919. return get_ubjson_float_prefix(j.m_value.number_float);
  13920. case value_t::string:
  13921. return 'S';
  13922. case value_t::array: // fallthrough
  13923. case value_t::binary:
  13924. return '[';
  13925. case value_t::object:
  13926. return '{';
  13927. case value_t::discarded:
  13928. default: // discarded values
  13929. return 'N';
  13930. }
  13931. }
  13932. static constexpr CharType get_ubjson_float_prefix(float /*unused*/)
  13933. {
  13934. return 'd'; // float 32
  13935. }
  13936. static constexpr CharType get_ubjson_float_prefix(double /*unused*/)
  13937. {
  13938. return 'D'; // float 64
  13939. }
  13940. /*!
  13941. @return false if the object is successfully converted to a bjdata ndarray, true if the type or size is invalid
  13942. */
  13943. bool write_bjdata_ndarray(const typename BasicJsonType::object_t& value, const bool use_count, const bool use_type)
  13944. {
  13945. std::map<string_t, CharType> bjdtype = {{"uint8", 'U'}, {"int8", 'i'}, {"uint16", 'u'}, {"int16", 'I'},
  13946. {"uint32", 'm'}, {"int32", 'l'}, {"uint64", 'M'}, {"int64", 'L'}, {"single", 'd'}, {"double", 'D'}, {"char", 'C'}
  13947. };
  13948. string_t key = "_ArrayType_";
  13949. auto it = bjdtype.find(static_cast<string_t>(value.at(key)));
  13950. if (it == bjdtype.end())
  13951. {
  13952. return true;
  13953. }
  13954. CharType dtype = it->second;
  13955. key = "_ArraySize_";
  13956. std::size_t len = (value.at(key).empty() ? 0 : 1);
  13957. for (const auto& el : value.at(key))
  13958. {
  13959. len *= static_cast<std::size_t>(el.m_value.number_unsigned);
  13960. }
  13961. key = "_ArrayData_";
  13962. if (value.at(key).size() != len)
  13963. {
  13964. return true;
  13965. }
  13966. oa->write_character('[');
  13967. oa->write_character('$');
  13968. oa->write_character(dtype);
  13969. oa->write_character('#');
  13970. key = "_ArraySize_";
  13971. write_ubjson(value.at(key), use_count, use_type, true, true);
  13972. key = "_ArrayData_";
  13973. if (dtype == 'U' || dtype == 'C')
  13974. {
  13975. for (const auto& el : value.at(key))
  13976. {
  13977. write_number(static_cast<std::uint8_t>(el.m_value.number_unsigned), true);
  13978. }
  13979. }
  13980. else if (dtype == 'i')
  13981. {
  13982. for (const auto& el : value.at(key))
  13983. {
  13984. write_number(static_cast<std::int8_t>(el.m_value.number_integer), true);
  13985. }
  13986. }
  13987. else if (dtype == 'u')
  13988. {
  13989. for (const auto& el : value.at(key))
  13990. {
  13991. write_number(static_cast<std::uint16_t>(el.m_value.number_unsigned), true);
  13992. }
  13993. }
  13994. else if (dtype == 'I')
  13995. {
  13996. for (const auto& el : value.at(key))
  13997. {
  13998. write_number(static_cast<std::int16_t>(el.m_value.number_integer), true);
  13999. }
  14000. }
  14001. else if (dtype == 'm')
  14002. {
  14003. for (const auto& el : value.at(key))
  14004. {
  14005. write_number(static_cast<std::uint32_t>(el.m_value.number_unsigned), true);
  14006. }
  14007. }
  14008. else if (dtype == 'l')
  14009. {
  14010. for (const auto& el : value.at(key))
  14011. {
  14012. write_number(static_cast<std::int32_t>(el.m_value.number_integer), true);
  14013. }
  14014. }
  14015. else if (dtype == 'M')
  14016. {
  14017. for (const auto& el : value.at(key))
  14018. {
  14019. write_number(static_cast<std::uint64_t>(el.m_value.number_unsigned), true);
  14020. }
  14021. }
  14022. else if (dtype == 'L')
  14023. {
  14024. for (const auto& el : value.at(key))
  14025. {
  14026. write_number(static_cast<std::int64_t>(el.m_value.number_integer), true);
  14027. }
  14028. }
  14029. else if (dtype == 'd')
  14030. {
  14031. for (const auto& el : value.at(key))
  14032. {
  14033. write_number(static_cast<float>(el.m_value.number_float), true);
  14034. }
  14035. }
  14036. else if (dtype == 'D')
  14037. {
  14038. for (const auto& el : value.at(key))
  14039. {
  14040. write_number(static_cast<double>(el.m_value.number_float), true);
  14041. }
  14042. }
  14043. return false;
  14044. }
  14045. ///////////////////////
  14046. // Utility functions //
  14047. ///////////////////////
  14048. /*
  14049. @brief write a number to output input
  14050. @param[in] n number of type @a NumberType
  14051. @param[in] OutputIsLittleEndian Set to true if output data is
  14052. required to be little endian
  14053. @tparam NumberType the type of the number
  14054. @note This function needs to respect the system's endianness, because bytes
  14055. in CBOR, MessagePack, and UBJSON are stored in network order (big
  14056. endian) and therefore need reordering on little endian systems.
  14057. On the other hand, BSON and BJData use little endian and should reorder
  14058. on big endian systems.
  14059. */
  14060. template<typename NumberType>
  14061. void write_number(const NumberType n, const bool OutputIsLittleEndian = false)
  14062. {
  14063. // step 1: write number to array of length NumberType
  14064. std::array<CharType, sizeof(NumberType)> vec{};
  14065. std::memcpy(vec.data(), &n, sizeof(NumberType));
  14066. // step 2: write array to output (with possible reordering)
  14067. if (is_little_endian != OutputIsLittleEndian)
  14068. {
  14069. // reverse byte order prior to conversion if necessary
  14070. std::reverse(vec.begin(), vec.end());
  14071. }
  14072. oa->write_characters(vec.data(), sizeof(NumberType));
  14073. }
  14074. void write_compact_float(const number_float_t n, detail::input_format_t format)
  14075. {
  14076. #ifdef __GNUC__
  14077. #pragma GCC diagnostic push
  14078. #pragma GCC diagnostic ignored "-Wfloat-equal"
  14079. #endif
  14080. if (static_cast<double>(n) >= static_cast<double>(std::numeric_limits<float>::lowest()) &&
  14081. static_cast<double>(n) <= static_cast<double>((std::numeric_limits<float>::max)()) &&
  14082. static_cast<double>(static_cast<float>(n)) == static_cast<double>(n))
  14083. {
  14084. oa->write_character(format == detail::input_format_t::cbor
  14085. ? get_cbor_float_prefix(static_cast<float>(n))
  14086. : get_msgpack_float_prefix(static_cast<float>(n)));
  14087. write_number(static_cast<float>(n));
  14088. }
  14089. else
  14090. {
  14091. oa->write_character(format == detail::input_format_t::cbor
  14092. ? get_cbor_float_prefix(n)
  14093. : get_msgpack_float_prefix(n));
  14094. write_number(n);
  14095. }
  14096. #ifdef __GNUC__
  14097. #pragma GCC diagnostic pop
  14098. #endif
  14099. }
  14100. public:
  14101. // The following to_char_type functions are implement the conversion
  14102. // between uint8_t and CharType. In case CharType is not unsigned,
  14103. // such a conversion is required to allow values greater than 128.
  14104. // See <https://github.com/nlohmann/json/issues/1286> for a discussion.
  14105. template < typename C = CharType,
  14106. enable_if_t < std::is_signed<C>::value && std::is_signed<char>::value > * = nullptr >
  14107. static constexpr CharType to_char_type(std::uint8_t x) noexcept
  14108. {
  14109. return *reinterpret_cast<char*>(&x);
  14110. }
  14111. template < typename C = CharType,
  14112. enable_if_t < std::is_signed<C>::value && std::is_unsigned<char>::value > * = nullptr >
  14113. static CharType to_char_type(std::uint8_t x) noexcept
  14114. {
  14115. static_assert(sizeof(std::uint8_t) == sizeof(CharType), "size of CharType must be equal to std::uint8_t");
  14116. static_assert(std::is_trivial<CharType>::value, "CharType must be trivial");
  14117. CharType result;
  14118. std::memcpy(&result, &x, sizeof(x));
  14119. return result;
  14120. }
  14121. template<typename C = CharType,
  14122. enable_if_t<std::is_unsigned<C>::value>* = nullptr>
  14123. static constexpr CharType to_char_type(std::uint8_t x) noexcept
  14124. {
  14125. return x;
  14126. }
  14127. template < typename InputCharType, typename C = CharType,
  14128. enable_if_t <
  14129. std::is_signed<C>::value &&
  14130. std::is_signed<char>::value &&
  14131. std::is_same<char, typename std::remove_cv<InputCharType>::type>::value
  14132. > * = nullptr >
  14133. static constexpr CharType to_char_type(InputCharType x) noexcept
  14134. {
  14135. return x;
  14136. }
  14137. private:
  14138. /// whether we can assume little endianness
  14139. const bool is_little_endian = little_endianness();
  14140. /// the output
  14141. output_adapter_t<CharType> oa = nullptr;
  14142. };
  14143. } // namespace detail
  14144. } // namespace nlohmann
  14145. // #include <nlohmann/detail/output/output_adapters.hpp>
  14146. // #include <nlohmann/detail/output/serializer.hpp>
  14147. #include <algorithm> // reverse, remove, fill, find, none_of
  14148. #include <array> // array
  14149. #include <clocale> // localeconv, lconv
  14150. #include <cmath> // labs, isfinite, isnan, signbit
  14151. #include <cstddef> // size_t, ptrdiff_t
  14152. #include <cstdint> // uint8_t
  14153. #include <cstdio> // snprintf
  14154. #include <limits> // numeric_limits
  14155. #include <string> // string, char_traits
  14156. #include <iomanip> // setfill, setw
  14157. #include <type_traits> // is_same
  14158. #include <utility> // move
  14159. // #include <nlohmann/detail/conversions/to_chars.hpp>
  14160. #include <array> // array
  14161. #include <cmath> // signbit, isfinite
  14162. #include <cstdint> // intN_t, uintN_t
  14163. #include <cstring> // memcpy, memmove
  14164. #include <limits> // numeric_limits
  14165. #include <type_traits> // conditional
  14166. // #include <nlohmann/detail/macro_scope.hpp>
  14167. namespace nlohmann
  14168. {
  14169. namespace detail
  14170. {
  14171. /*!
  14172. @brief implements the Grisu2 algorithm for binary to decimal floating-point
  14173. conversion.
  14174. This implementation is a slightly modified version of the reference
  14175. implementation which may be obtained from
  14176. http://florian.loitsch.com/publications (bench.tar.gz).
  14177. The code is distributed under the MIT license, Copyright (c) 2009 Florian Loitsch.
  14178. For a detailed description of the algorithm see:
  14179. [1] Loitsch, "Printing Floating-Point Numbers Quickly and Accurately with
  14180. Integers", Proceedings of the ACM SIGPLAN 2010 Conference on Programming
  14181. Language Design and Implementation, PLDI 2010
  14182. [2] Burger, Dybvig, "Printing Floating-Point Numbers Quickly and Accurately",
  14183. Proceedings of the ACM SIGPLAN 1996 Conference on Programming Language
  14184. Design and Implementation, PLDI 1996
  14185. */
  14186. namespace dtoa_impl
  14187. {
  14188. template<typename Target, typename Source>
  14189. Target reinterpret_bits(const Source source)
  14190. {
  14191. static_assert(sizeof(Target) == sizeof(Source), "size mismatch");
  14192. Target target;
  14193. std::memcpy(&target, &source, sizeof(Source));
  14194. return target;
  14195. }
  14196. struct diyfp // f * 2^e
  14197. {
  14198. static constexpr int kPrecision = 64; // = q
  14199. std::uint64_t f = 0;
  14200. int e = 0;
  14201. constexpr diyfp(std::uint64_t f_, int e_) noexcept : f(f_), e(e_) {}
  14202. /*!
  14203. @brief returns x - y
  14204. @pre x.e == y.e and x.f >= y.f
  14205. */
  14206. static diyfp sub(const diyfp& x, const diyfp& y) noexcept
  14207. {
  14208. JSON_ASSERT(x.e == y.e);
  14209. JSON_ASSERT(x.f >= y.f);
  14210. return {x.f - y.f, x.e};
  14211. }
  14212. /*!
  14213. @brief returns x * y
  14214. @note The result is rounded. (Only the upper q bits are returned.)
  14215. */
  14216. static diyfp mul(const diyfp& x, const diyfp& y) noexcept
  14217. {
  14218. static_assert(kPrecision == 64, "internal error");
  14219. // Computes:
  14220. // f = round((x.f * y.f) / 2^q)
  14221. // e = x.e + y.e + q
  14222. // Emulate the 64-bit * 64-bit multiplication:
  14223. //
  14224. // p = u * v
  14225. // = (u_lo + 2^32 u_hi) (v_lo + 2^32 v_hi)
  14226. // = (u_lo v_lo ) + 2^32 ((u_lo v_hi ) + (u_hi v_lo )) + 2^64 (u_hi v_hi )
  14227. // = (p0 ) + 2^32 ((p1 ) + (p2 )) + 2^64 (p3 )
  14228. // = (p0_lo + 2^32 p0_hi) + 2^32 ((p1_lo + 2^32 p1_hi) + (p2_lo + 2^32 p2_hi)) + 2^64 (p3 )
  14229. // = (p0_lo ) + 2^32 (p0_hi + p1_lo + p2_lo ) + 2^64 (p1_hi + p2_hi + p3)
  14230. // = (p0_lo ) + 2^32 (Q ) + 2^64 (H )
  14231. // = (p0_lo ) + 2^32 (Q_lo + 2^32 Q_hi ) + 2^64 (H )
  14232. //
  14233. // (Since Q might be larger than 2^32 - 1)
  14234. //
  14235. // = (p0_lo + 2^32 Q_lo) + 2^64 (Q_hi + H)
  14236. //
  14237. // (Q_hi + H does not overflow a 64-bit int)
  14238. //
  14239. // = p_lo + 2^64 p_hi
  14240. const std::uint64_t u_lo = x.f & 0xFFFFFFFFu;
  14241. const std::uint64_t u_hi = x.f >> 32u;
  14242. const std::uint64_t v_lo = y.f & 0xFFFFFFFFu;
  14243. const std::uint64_t v_hi = y.f >> 32u;
  14244. const std::uint64_t p0 = u_lo * v_lo;
  14245. const std::uint64_t p1 = u_lo * v_hi;
  14246. const std::uint64_t p2 = u_hi * v_lo;
  14247. const std::uint64_t p3 = u_hi * v_hi;
  14248. const std::uint64_t p0_hi = p0 >> 32u;
  14249. const std::uint64_t p1_lo = p1 & 0xFFFFFFFFu;
  14250. const std::uint64_t p1_hi = p1 >> 32u;
  14251. const std::uint64_t p2_lo = p2 & 0xFFFFFFFFu;
  14252. const std::uint64_t p2_hi = p2 >> 32u;
  14253. std::uint64_t Q = p0_hi + p1_lo + p2_lo;
  14254. // The full product might now be computed as
  14255. //
  14256. // p_hi = p3 + p2_hi + p1_hi + (Q >> 32)
  14257. // p_lo = p0_lo + (Q << 32)
  14258. //
  14259. // But in this particular case here, the full p_lo is not required.
  14260. // Effectively we only need to add the highest bit in p_lo to p_hi (and
  14261. // Q_hi + 1 does not overflow).
  14262. Q += std::uint64_t{1} << (64u - 32u - 1u); // round, ties up
  14263. const std::uint64_t h = p3 + p2_hi + p1_hi + (Q >> 32u);
  14264. return {h, x.e + y.e + 64};
  14265. }
  14266. /*!
  14267. @brief normalize x such that the significand is >= 2^(q-1)
  14268. @pre x.f != 0
  14269. */
  14270. static diyfp normalize(diyfp x) noexcept
  14271. {
  14272. JSON_ASSERT(x.f != 0);
  14273. while ((x.f >> 63u) == 0)
  14274. {
  14275. x.f <<= 1u;
  14276. x.e--;
  14277. }
  14278. return x;
  14279. }
  14280. /*!
  14281. @brief normalize x such that the result has the exponent E
  14282. @pre e >= x.e and the upper e - x.e bits of x.f must be zero.
  14283. */
  14284. static diyfp normalize_to(const diyfp& x, const int target_exponent) noexcept
  14285. {
  14286. const int delta = x.e - target_exponent;
  14287. JSON_ASSERT(delta >= 0);
  14288. JSON_ASSERT(((x.f << delta) >> delta) == x.f);
  14289. return {x.f << delta, target_exponent};
  14290. }
  14291. };
  14292. struct boundaries
  14293. {
  14294. diyfp w;
  14295. diyfp minus;
  14296. diyfp plus;
  14297. };
  14298. /*!
  14299. Compute the (normalized) diyfp representing the input number 'value' and its
  14300. boundaries.
  14301. @pre value must be finite and positive
  14302. */
  14303. template<typename FloatType>
  14304. boundaries compute_boundaries(FloatType value)
  14305. {
  14306. JSON_ASSERT(std::isfinite(value));
  14307. JSON_ASSERT(value > 0);
  14308. // Convert the IEEE representation into a diyfp.
  14309. //
  14310. // If v is denormal:
  14311. // value = 0.F * 2^(1 - bias) = ( F) * 2^(1 - bias - (p-1))
  14312. // If v is normalized:
  14313. // value = 1.F * 2^(E - bias) = (2^(p-1) + F) * 2^(E - bias - (p-1))
  14314. static_assert(std::numeric_limits<FloatType>::is_iec559,
  14315. "internal error: dtoa_short requires an IEEE-754 floating-point implementation");
  14316. constexpr int kPrecision = std::numeric_limits<FloatType>::digits; // = p (includes the hidden bit)
  14317. constexpr int kBias = std::numeric_limits<FloatType>::max_exponent - 1 + (kPrecision - 1);
  14318. constexpr int kMinExp = 1 - kBias;
  14319. constexpr std::uint64_t kHiddenBit = std::uint64_t{1} << (kPrecision - 1); // = 2^(p-1)
  14320. using bits_type = typename std::conditional<kPrecision == 24, std::uint32_t, std::uint64_t >::type;
  14321. const auto bits = static_cast<std::uint64_t>(reinterpret_bits<bits_type>(value));
  14322. const std::uint64_t E = bits >> (kPrecision - 1);
  14323. const std::uint64_t F = bits & (kHiddenBit - 1);
  14324. const bool is_denormal = E == 0;
  14325. const diyfp v = is_denormal
  14326. ? diyfp(F, kMinExp)
  14327. : diyfp(F + kHiddenBit, static_cast<int>(E) - kBias);
  14328. // Compute the boundaries m- and m+ of the floating-point value
  14329. // v = f * 2^e.
  14330. //
  14331. // Determine v- and v+, the floating-point predecessor and successor if v,
  14332. // respectively.
  14333. //
  14334. // v- = v - 2^e if f != 2^(p-1) or e == e_min (A)
  14335. // = v - 2^(e-1) if f == 2^(p-1) and e > e_min (B)
  14336. //
  14337. // v+ = v + 2^e
  14338. //
  14339. // Let m- = (v- + v) / 2 and m+ = (v + v+) / 2. All real numbers _strictly_
  14340. // between m- and m+ round to v, regardless of how the input rounding
  14341. // algorithm breaks ties.
  14342. //
  14343. // ---+-------------+-------------+-------------+-------------+--- (A)
  14344. // v- m- v m+ v+
  14345. //
  14346. // -----------------+------+------+-------------+-------------+--- (B)
  14347. // v- m- v m+ v+
  14348. const bool lower_boundary_is_closer = F == 0 && E > 1;
  14349. const diyfp m_plus = diyfp(2 * v.f + 1, v.e - 1);
  14350. const diyfp m_minus = lower_boundary_is_closer
  14351. ? diyfp(4 * v.f - 1, v.e - 2) // (B)
  14352. : diyfp(2 * v.f - 1, v.e - 1); // (A)
  14353. // Determine the normalized w+ = m+.
  14354. const diyfp w_plus = diyfp::normalize(m_plus);
  14355. // Determine w- = m- such that e_(w-) = e_(w+).
  14356. const diyfp w_minus = diyfp::normalize_to(m_minus, w_plus.e);
  14357. return {diyfp::normalize(v), w_minus, w_plus};
  14358. }
  14359. // Given normalized diyfp w, Grisu needs to find a (normalized) cached
  14360. // power-of-ten c, such that the exponent of the product c * w = f * 2^e lies
  14361. // within a certain range [alpha, gamma] (Definition 3.2 from [1])
  14362. //
  14363. // alpha <= e = e_c + e_w + q <= gamma
  14364. //
  14365. // or
  14366. //
  14367. // f_c * f_w * 2^alpha <= f_c 2^(e_c) * f_w 2^(e_w) * 2^q
  14368. // <= f_c * f_w * 2^gamma
  14369. //
  14370. // Since c and w are normalized, i.e. 2^(q-1) <= f < 2^q, this implies
  14371. //
  14372. // 2^(q-1) * 2^(q-1) * 2^alpha <= c * w * 2^q < 2^q * 2^q * 2^gamma
  14373. //
  14374. // or
  14375. //
  14376. // 2^(q - 2 + alpha) <= c * w < 2^(q + gamma)
  14377. //
  14378. // The choice of (alpha,gamma) determines the size of the table and the form of
  14379. // the digit generation procedure. Using (alpha,gamma)=(-60,-32) works out well
  14380. // in practice:
  14381. //
  14382. // The idea is to cut the number c * w = f * 2^e into two parts, which can be
  14383. // processed independently: An integral part p1, and a fractional part p2:
  14384. //
  14385. // f * 2^e = ( (f div 2^-e) * 2^-e + (f mod 2^-e) ) * 2^e
  14386. // = (f div 2^-e) + (f mod 2^-e) * 2^e
  14387. // = p1 + p2 * 2^e
  14388. //
  14389. // The conversion of p1 into decimal form requires a series of divisions and
  14390. // modulos by (a power of) 10. These operations are faster for 32-bit than for
  14391. // 64-bit integers, so p1 should ideally fit into a 32-bit integer. This can be
  14392. // achieved by choosing
  14393. //
  14394. // -e >= 32 or e <= -32 := gamma
  14395. //
  14396. // In order to convert the fractional part
  14397. //
  14398. // p2 * 2^e = p2 / 2^-e = d[-1] / 10^1 + d[-2] / 10^2 + ...
  14399. //
  14400. // into decimal form, the fraction is repeatedly multiplied by 10 and the digits
  14401. // d[-i] are extracted in order:
  14402. //
  14403. // (10 * p2) div 2^-e = d[-1]
  14404. // (10 * p2) mod 2^-e = d[-2] / 10^1 + ...
  14405. //
  14406. // The multiplication by 10 must not overflow. It is sufficient to choose
  14407. //
  14408. // 10 * p2 < 16 * p2 = 2^4 * p2 <= 2^64.
  14409. //
  14410. // Since p2 = f mod 2^-e < 2^-e,
  14411. //
  14412. // -e <= 60 or e >= -60 := alpha
  14413. constexpr int kAlpha = -60;
  14414. constexpr int kGamma = -32;
  14415. struct cached_power // c = f * 2^e ~= 10^k
  14416. {
  14417. std::uint64_t f;
  14418. int e;
  14419. int k;
  14420. };
  14421. /*!
  14422. For a normalized diyfp w = f * 2^e, this function returns a (normalized) cached
  14423. power-of-ten c = f_c * 2^e_c, such that the exponent of the product w * c
  14424. satisfies (Definition 3.2 from [1])
  14425. alpha <= e_c + e + q <= gamma.
  14426. */
  14427. inline cached_power get_cached_power_for_binary_exponent(int e)
  14428. {
  14429. // Now
  14430. //
  14431. // alpha <= e_c + e + q <= gamma (1)
  14432. // ==> f_c * 2^alpha <= c * 2^e * 2^q
  14433. //
  14434. // and since the c's are normalized, 2^(q-1) <= f_c,
  14435. //
  14436. // ==> 2^(q - 1 + alpha) <= c * 2^(e + q)
  14437. // ==> 2^(alpha - e - 1) <= c
  14438. //
  14439. // If c were an exact power of ten, i.e. c = 10^k, one may determine k as
  14440. //
  14441. // k = ceil( log_10( 2^(alpha - e - 1) ) )
  14442. // = ceil( (alpha - e - 1) * log_10(2) )
  14443. //
  14444. // From the paper:
  14445. // "In theory the result of the procedure could be wrong since c is rounded,
  14446. // and the computation itself is approximated [...]. In practice, however,
  14447. // this simple function is sufficient."
  14448. //
  14449. // For IEEE double precision floating-point numbers converted into
  14450. // normalized diyfp's w = f * 2^e, with q = 64,
  14451. //
  14452. // e >= -1022 (min IEEE exponent)
  14453. // -52 (p - 1)
  14454. // -52 (p - 1, possibly normalize denormal IEEE numbers)
  14455. // -11 (normalize the diyfp)
  14456. // = -1137
  14457. //
  14458. // and
  14459. //
  14460. // e <= +1023 (max IEEE exponent)
  14461. // -52 (p - 1)
  14462. // -11 (normalize the diyfp)
  14463. // = 960
  14464. //
  14465. // This binary exponent range [-1137,960] results in a decimal exponent
  14466. // range [-307,324]. One does not need to store a cached power for each
  14467. // k in this range. For each such k it suffices to find a cached power
  14468. // such that the exponent of the product lies in [alpha,gamma].
  14469. // This implies that the difference of the decimal exponents of adjacent
  14470. // table entries must be less than or equal to
  14471. //
  14472. // floor( (gamma - alpha) * log_10(2) ) = 8.
  14473. //
  14474. // (A smaller distance gamma-alpha would require a larger table.)
  14475. // NB:
  14476. // Actually this function returns c, such that -60 <= e_c + e + 64 <= -34.
  14477. constexpr int kCachedPowersMinDecExp = -300;
  14478. constexpr int kCachedPowersDecStep = 8;
  14479. static constexpr std::array<cached_power, 79> kCachedPowers =
  14480. {
  14481. {
  14482. { 0xAB70FE17C79AC6CA, -1060, -300 },
  14483. { 0xFF77B1FCBEBCDC4F, -1034, -292 },
  14484. { 0xBE5691EF416BD60C, -1007, -284 },
  14485. { 0x8DD01FAD907FFC3C, -980, -276 },
  14486. { 0xD3515C2831559A83, -954, -268 },
  14487. { 0x9D71AC8FADA6C9B5, -927, -260 },
  14488. { 0xEA9C227723EE8BCB, -901, -252 },
  14489. { 0xAECC49914078536D, -874, -244 },
  14490. { 0x823C12795DB6CE57, -847, -236 },
  14491. { 0xC21094364DFB5637, -821, -228 },
  14492. { 0x9096EA6F3848984F, -794, -220 },
  14493. { 0xD77485CB25823AC7, -768, -212 },
  14494. { 0xA086CFCD97BF97F4, -741, -204 },
  14495. { 0xEF340A98172AACE5, -715, -196 },
  14496. { 0xB23867FB2A35B28E, -688, -188 },
  14497. { 0x84C8D4DFD2C63F3B, -661, -180 },
  14498. { 0xC5DD44271AD3CDBA, -635, -172 },
  14499. { 0x936B9FCEBB25C996, -608, -164 },
  14500. { 0xDBAC6C247D62A584, -582, -156 },
  14501. { 0xA3AB66580D5FDAF6, -555, -148 },
  14502. { 0xF3E2F893DEC3F126, -529, -140 },
  14503. { 0xB5B5ADA8AAFF80B8, -502, -132 },
  14504. { 0x87625F056C7C4A8B, -475, -124 },
  14505. { 0xC9BCFF6034C13053, -449, -116 },
  14506. { 0x964E858C91BA2655, -422, -108 },
  14507. { 0xDFF9772470297EBD, -396, -100 },
  14508. { 0xA6DFBD9FB8E5B88F, -369, -92 },
  14509. { 0xF8A95FCF88747D94, -343, -84 },
  14510. { 0xB94470938FA89BCF, -316, -76 },
  14511. { 0x8A08F0F8BF0F156B, -289, -68 },
  14512. { 0xCDB02555653131B6, -263, -60 },
  14513. { 0x993FE2C6D07B7FAC, -236, -52 },
  14514. { 0xE45C10C42A2B3B06, -210, -44 },
  14515. { 0xAA242499697392D3, -183, -36 },
  14516. { 0xFD87B5F28300CA0E, -157, -28 },
  14517. { 0xBCE5086492111AEB, -130, -20 },
  14518. { 0x8CBCCC096F5088CC, -103, -12 },
  14519. { 0xD1B71758E219652C, -77, -4 },
  14520. { 0x9C40000000000000, -50, 4 },
  14521. { 0xE8D4A51000000000, -24, 12 },
  14522. { 0xAD78EBC5AC620000, 3, 20 },
  14523. { 0x813F3978F8940984, 30, 28 },
  14524. { 0xC097CE7BC90715B3, 56, 36 },
  14525. { 0x8F7E32CE7BEA5C70, 83, 44 },
  14526. { 0xD5D238A4ABE98068, 109, 52 },
  14527. { 0x9F4F2726179A2245, 136, 60 },
  14528. { 0xED63A231D4C4FB27, 162, 68 },
  14529. { 0xB0DE65388CC8ADA8, 189, 76 },
  14530. { 0x83C7088E1AAB65DB, 216, 84 },
  14531. { 0xC45D1DF942711D9A, 242, 92 },
  14532. { 0x924D692CA61BE758, 269, 100 },
  14533. { 0xDA01EE641A708DEA, 295, 108 },
  14534. { 0xA26DA3999AEF774A, 322, 116 },
  14535. { 0xF209787BB47D6B85, 348, 124 },
  14536. { 0xB454E4A179DD1877, 375, 132 },
  14537. { 0x865B86925B9BC5C2, 402, 140 },
  14538. { 0xC83553C5C8965D3D, 428, 148 },
  14539. { 0x952AB45CFA97A0B3, 455, 156 },
  14540. { 0xDE469FBD99A05FE3, 481, 164 },
  14541. { 0xA59BC234DB398C25, 508, 172 },
  14542. { 0xF6C69A72A3989F5C, 534, 180 },
  14543. { 0xB7DCBF5354E9BECE, 561, 188 },
  14544. { 0x88FCF317F22241E2, 588, 196 },
  14545. { 0xCC20CE9BD35C78A5, 614, 204 },
  14546. { 0x98165AF37B2153DF, 641, 212 },
  14547. { 0xE2A0B5DC971F303A, 667, 220 },
  14548. { 0xA8D9D1535CE3B396, 694, 228 },
  14549. { 0xFB9B7CD9A4A7443C, 720, 236 },
  14550. { 0xBB764C4CA7A44410, 747, 244 },
  14551. { 0x8BAB8EEFB6409C1A, 774, 252 },
  14552. { 0xD01FEF10A657842C, 800, 260 },
  14553. { 0x9B10A4E5E9913129, 827, 268 },
  14554. { 0xE7109BFBA19C0C9D, 853, 276 },
  14555. { 0xAC2820D9623BF429, 880, 284 },
  14556. { 0x80444B5E7AA7CF85, 907, 292 },
  14557. { 0xBF21E44003ACDD2D, 933, 300 },
  14558. { 0x8E679C2F5E44FF8F, 960, 308 },
  14559. { 0xD433179D9C8CB841, 986, 316 },
  14560. { 0x9E19DB92B4E31BA9, 1013, 324 },
  14561. }
  14562. };
  14563. // This computation gives exactly the same results for k as
  14564. // k = ceil((kAlpha - e - 1) * 0.30102999566398114)
  14565. // for |e| <= 1500, but doesn't require floating-point operations.
  14566. // NB: log_10(2) ~= 78913 / 2^18
  14567. JSON_ASSERT(e >= -1500);
  14568. JSON_ASSERT(e <= 1500);
  14569. const int f = kAlpha - e - 1;
  14570. const int k = (f * 78913) / (1 << 18) + static_cast<int>(f > 0);
  14571. const int index = (-kCachedPowersMinDecExp + k + (kCachedPowersDecStep - 1)) / kCachedPowersDecStep;
  14572. JSON_ASSERT(index >= 0);
  14573. JSON_ASSERT(static_cast<std::size_t>(index) < kCachedPowers.size());
  14574. const cached_power cached = kCachedPowers[static_cast<std::size_t>(index)];
  14575. JSON_ASSERT(kAlpha <= cached.e + e + 64);
  14576. JSON_ASSERT(kGamma >= cached.e + e + 64);
  14577. return cached;
  14578. }
  14579. /*!
  14580. For n != 0, returns k, such that pow10 := 10^(k-1) <= n < 10^k.
  14581. For n == 0, returns 1 and sets pow10 := 1.
  14582. */
  14583. inline int find_largest_pow10(const std::uint32_t n, std::uint32_t& pow10)
  14584. {
  14585. // LCOV_EXCL_START
  14586. if (n >= 1000000000)
  14587. {
  14588. pow10 = 1000000000;
  14589. return 10;
  14590. }
  14591. // LCOV_EXCL_STOP
  14592. if (n >= 100000000)
  14593. {
  14594. pow10 = 100000000;
  14595. return 9;
  14596. }
  14597. if (n >= 10000000)
  14598. {
  14599. pow10 = 10000000;
  14600. return 8;
  14601. }
  14602. if (n >= 1000000)
  14603. {
  14604. pow10 = 1000000;
  14605. return 7;
  14606. }
  14607. if (n >= 100000)
  14608. {
  14609. pow10 = 100000;
  14610. return 6;
  14611. }
  14612. if (n >= 10000)
  14613. {
  14614. pow10 = 10000;
  14615. return 5;
  14616. }
  14617. if (n >= 1000)
  14618. {
  14619. pow10 = 1000;
  14620. return 4;
  14621. }
  14622. if (n >= 100)
  14623. {
  14624. pow10 = 100;
  14625. return 3;
  14626. }
  14627. if (n >= 10)
  14628. {
  14629. pow10 = 10;
  14630. return 2;
  14631. }
  14632. pow10 = 1;
  14633. return 1;
  14634. }
  14635. inline void grisu2_round(char* buf, int len, std::uint64_t dist, std::uint64_t delta,
  14636. std::uint64_t rest, std::uint64_t ten_k)
  14637. {
  14638. JSON_ASSERT(len >= 1);
  14639. JSON_ASSERT(dist <= delta);
  14640. JSON_ASSERT(rest <= delta);
  14641. JSON_ASSERT(ten_k > 0);
  14642. // <--------------------------- delta ---->
  14643. // <---- dist --------->
  14644. // --------------[------------------+-------------------]--------------
  14645. // M- w M+
  14646. //
  14647. // ten_k
  14648. // <------>
  14649. // <---- rest ---->
  14650. // --------------[------------------+----+--------------]--------------
  14651. // w V
  14652. // = buf * 10^k
  14653. //
  14654. // ten_k represents a unit-in-the-last-place in the decimal representation
  14655. // stored in buf.
  14656. // Decrement buf by ten_k while this takes buf closer to w.
  14657. // The tests are written in this order to avoid overflow in unsigned
  14658. // integer arithmetic.
  14659. while (rest < dist
  14660. && delta - rest >= ten_k
  14661. && (rest + ten_k < dist || dist - rest > rest + ten_k - dist))
  14662. {
  14663. JSON_ASSERT(buf[len - 1] != '0');
  14664. buf[len - 1]--;
  14665. rest += ten_k;
  14666. }
  14667. }
  14668. /*!
  14669. Generates V = buffer * 10^decimal_exponent, such that M- <= V <= M+.
  14670. M- and M+ must be normalized and share the same exponent -60 <= e <= -32.
  14671. */
  14672. inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent,
  14673. diyfp M_minus, diyfp w, diyfp M_plus)
  14674. {
  14675. static_assert(kAlpha >= -60, "internal error");
  14676. static_assert(kGamma <= -32, "internal error");
  14677. // Generates the digits (and the exponent) of a decimal floating-point
  14678. // number V = buffer * 10^decimal_exponent in the range [M-, M+]. The diyfp's
  14679. // w, M- and M+ share the same exponent e, which satisfies alpha <= e <= gamma.
  14680. //
  14681. // <--------------------------- delta ---->
  14682. // <---- dist --------->
  14683. // --------------[------------------+-------------------]--------------
  14684. // M- w M+
  14685. //
  14686. // Grisu2 generates the digits of M+ from left to right and stops as soon as
  14687. // V is in [M-,M+].
  14688. JSON_ASSERT(M_plus.e >= kAlpha);
  14689. JSON_ASSERT(M_plus.e <= kGamma);
  14690. std::uint64_t delta = diyfp::sub(M_plus, M_minus).f; // (significand of (M+ - M-), implicit exponent is e)
  14691. std::uint64_t dist = diyfp::sub(M_plus, w ).f; // (significand of (M+ - w ), implicit exponent is e)
  14692. // Split M+ = f * 2^e into two parts p1 and p2 (note: e < 0):
  14693. //
  14694. // M+ = f * 2^e
  14695. // = ((f div 2^-e) * 2^-e + (f mod 2^-e)) * 2^e
  14696. // = ((p1 ) * 2^-e + (p2 )) * 2^e
  14697. // = p1 + p2 * 2^e
  14698. const diyfp one(std::uint64_t{1} << -M_plus.e, M_plus.e);
  14699. 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.)
  14700. std::uint64_t p2 = M_plus.f & (one.f - 1); // p2 = f mod 2^-e
  14701. // 1)
  14702. //
  14703. // Generate the digits of the integral part p1 = d[n-1]...d[1]d[0]
  14704. JSON_ASSERT(p1 > 0);
  14705. std::uint32_t pow10{};
  14706. const int k = find_largest_pow10(p1, pow10);
  14707. // 10^(k-1) <= p1 < 10^k, pow10 = 10^(k-1)
  14708. //
  14709. // p1 = (p1 div 10^(k-1)) * 10^(k-1) + (p1 mod 10^(k-1))
  14710. // = (d[k-1] ) * 10^(k-1) + (p1 mod 10^(k-1))
  14711. //
  14712. // M+ = p1 + p2 * 2^e
  14713. // = d[k-1] * 10^(k-1) + (p1 mod 10^(k-1)) + p2 * 2^e
  14714. // = d[k-1] * 10^(k-1) + ((p1 mod 10^(k-1)) * 2^-e + p2) * 2^e
  14715. // = d[k-1] * 10^(k-1) + ( rest) * 2^e
  14716. //
  14717. // Now generate the digits d[n] of p1 from left to right (n = k-1,...,0)
  14718. //
  14719. // p1 = d[k-1]...d[n] * 10^n + d[n-1]...d[0]
  14720. //
  14721. // but stop as soon as
  14722. //
  14723. // rest * 2^e = (d[n-1]...d[0] * 2^-e + p2) * 2^e <= delta * 2^e
  14724. int n = k;
  14725. while (n > 0)
  14726. {
  14727. // Invariants:
  14728. // M+ = buffer * 10^n + (p1 + p2 * 2^e) (buffer = 0 for n = k)
  14729. // pow10 = 10^(n-1) <= p1 < 10^n
  14730. //
  14731. const std::uint32_t d = p1 / pow10; // d = p1 div 10^(n-1)
  14732. const std::uint32_t r = p1 % pow10; // r = p1 mod 10^(n-1)
  14733. //
  14734. // M+ = buffer * 10^n + (d * 10^(n-1) + r) + p2 * 2^e
  14735. // = (buffer * 10 + d) * 10^(n-1) + (r + p2 * 2^e)
  14736. //
  14737. JSON_ASSERT(d <= 9);
  14738. buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
  14739. //
  14740. // M+ = buffer * 10^(n-1) + (r + p2 * 2^e)
  14741. //
  14742. p1 = r;
  14743. n--;
  14744. //
  14745. // M+ = buffer * 10^n + (p1 + p2 * 2^e)
  14746. // pow10 = 10^n
  14747. //
  14748. // Now check if enough digits have been generated.
  14749. // Compute
  14750. //
  14751. // p1 + p2 * 2^e = (p1 * 2^-e + p2) * 2^e = rest * 2^e
  14752. //
  14753. // Note:
  14754. // Since rest and delta share the same exponent e, it suffices to
  14755. // compare the significands.
  14756. const std::uint64_t rest = (std::uint64_t{p1} << -one.e) + p2;
  14757. if (rest <= delta)
  14758. {
  14759. // V = buffer * 10^n, with M- <= V <= M+.
  14760. decimal_exponent += n;
  14761. // We may now just stop. But instead look if the buffer could be
  14762. // decremented to bring V closer to w.
  14763. //
  14764. // pow10 = 10^n is now 1 ulp in the decimal representation V.
  14765. // The rounding procedure works with diyfp's with an implicit
  14766. // exponent of e.
  14767. //
  14768. // 10^n = (10^n * 2^-e) * 2^e = ulp * 2^e
  14769. //
  14770. const std::uint64_t ten_n = std::uint64_t{pow10} << -one.e;
  14771. grisu2_round(buffer, length, dist, delta, rest, ten_n);
  14772. return;
  14773. }
  14774. pow10 /= 10;
  14775. //
  14776. // pow10 = 10^(n-1) <= p1 < 10^n
  14777. // Invariants restored.
  14778. }
  14779. // 2)
  14780. //
  14781. // The digits of the integral part have been generated:
  14782. //
  14783. // M+ = d[k-1]...d[1]d[0] + p2 * 2^e
  14784. // = buffer + p2 * 2^e
  14785. //
  14786. // Now generate the digits of the fractional part p2 * 2^e.
  14787. //
  14788. // Note:
  14789. // No decimal point is generated: the exponent is adjusted instead.
  14790. //
  14791. // p2 actually represents the fraction
  14792. //
  14793. // p2 * 2^e
  14794. // = p2 / 2^-e
  14795. // = d[-1] / 10^1 + d[-2] / 10^2 + ...
  14796. //
  14797. // Now generate the digits d[-m] of p1 from left to right (m = 1,2,...)
  14798. //
  14799. // p2 * 2^e = d[-1]d[-2]...d[-m] * 10^-m
  14800. // + 10^-m * (d[-m-1] / 10^1 + d[-m-2] / 10^2 + ...)
  14801. //
  14802. // using
  14803. //
  14804. // 10^m * p2 = ((10^m * p2) div 2^-e) * 2^-e + ((10^m * p2) mod 2^-e)
  14805. // = ( d) * 2^-e + ( r)
  14806. //
  14807. // or
  14808. // 10^m * p2 * 2^e = d + r * 2^e
  14809. //
  14810. // i.e.
  14811. //
  14812. // M+ = buffer + p2 * 2^e
  14813. // = buffer + 10^-m * (d + r * 2^e)
  14814. // = (buffer * 10^m + d) * 10^-m + 10^-m * r * 2^e
  14815. //
  14816. // and stop as soon as 10^-m * r * 2^e <= delta * 2^e
  14817. JSON_ASSERT(p2 > delta);
  14818. int m = 0;
  14819. for (;;)
  14820. {
  14821. // Invariant:
  14822. // M+ = buffer * 10^-m + 10^-m * (d[-m-1] / 10 + d[-m-2] / 10^2 + ...) * 2^e
  14823. // = buffer * 10^-m + 10^-m * (p2 ) * 2^e
  14824. // = buffer * 10^-m + 10^-m * (1/10 * (10 * p2) ) * 2^e
  14825. // = buffer * 10^-m + 10^-m * (1/10 * ((10*p2 div 2^-e) * 2^-e + (10*p2 mod 2^-e)) * 2^e
  14826. //
  14827. JSON_ASSERT(p2 <= (std::numeric_limits<std::uint64_t>::max)() / 10);
  14828. p2 *= 10;
  14829. const std::uint64_t d = p2 >> -one.e; // d = (10 * p2) div 2^-e
  14830. const std::uint64_t r = p2 & (one.f - 1); // r = (10 * p2) mod 2^-e
  14831. //
  14832. // M+ = buffer * 10^-m + 10^-m * (1/10 * (d * 2^-e + r) * 2^e
  14833. // = buffer * 10^-m + 10^-m * (1/10 * (d + r * 2^e))
  14834. // = (buffer * 10 + d) * 10^(-m-1) + 10^(-m-1) * r * 2^e
  14835. //
  14836. JSON_ASSERT(d <= 9);
  14837. buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
  14838. //
  14839. // M+ = buffer * 10^(-m-1) + 10^(-m-1) * r * 2^e
  14840. //
  14841. p2 = r;
  14842. m++;
  14843. //
  14844. // M+ = buffer * 10^-m + 10^-m * p2 * 2^e
  14845. // Invariant restored.
  14846. // Check if enough digits have been generated.
  14847. //
  14848. // 10^-m * p2 * 2^e <= delta * 2^e
  14849. // p2 * 2^e <= 10^m * delta * 2^e
  14850. // p2 <= 10^m * delta
  14851. delta *= 10;
  14852. dist *= 10;
  14853. if (p2 <= delta)
  14854. {
  14855. break;
  14856. }
  14857. }
  14858. // V = buffer * 10^-m, with M- <= V <= M+.
  14859. decimal_exponent -= m;
  14860. // 1 ulp in the decimal representation is now 10^-m.
  14861. // Since delta and dist are now scaled by 10^m, we need to do the
  14862. // same with ulp in order to keep the units in sync.
  14863. //
  14864. // 10^m * 10^-m = 1 = 2^-e * 2^e = ten_m * 2^e
  14865. //
  14866. const std::uint64_t ten_m = one.f;
  14867. grisu2_round(buffer, length, dist, delta, p2, ten_m);
  14868. // By construction this algorithm generates the shortest possible decimal
  14869. // number (Loitsch, Theorem 6.2) which rounds back to w.
  14870. // For an input number of precision p, at least
  14871. //
  14872. // N = 1 + ceil(p * log_10(2))
  14873. //
  14874. // decimal digits are sufficient to identify all binary floating-point
  14875. // numbers (Matula, "In-and-Out conversions").
  14876. // This implies that the algorithm does not produce more than N decimal
  14877. // digits.
  14878. //
  14879. // N = 17 for p = 53 (IEEE double precision)
  14880. // N = 9 for p = 24 (IEEE single precision)
  14881. }
  14882. /*!
  14883. v = buf * 10^decimal_exponent
  14884. len is the length of the buffer (number of decimal digits)
  14885. The buffer must be large enough, i.e. >= max_digits10.
  14886. */
  14887. JSON_HEDLEY_NON_NULL(1)
  14888. inline void grisu2(char* buf, int& len, int& decimal_exponent,
  14889. diyfp m_minus, diyfp v, diyfp m_plus)
  14890. {
  14891. JSON_ASSERT(m_plus.e == m_minus.e);
  14892. JSON_ASSERT(m_plus.e == v.e);
  14893. // --------(-----------------------+-----------------------)-------- (A)
  14894. // m- v m+
  14895. //
  14896. // --------------------(-----------+-----------------------)-------- (B)
  14897. // m- v m+
  14898. //
  14899. // First scale v (and m- and m+) such that the exponent is in the range
  14900. // [alpha, gamma].
  14901. const cached_power cached = get_cached_power_for_binary_exponent(m_plus.e);
  14902. const diyfp c_minus_k(cached.f, cached.e); // = c ~= 10^-k
  14903. // The exponent of the products is = v.e + c_minus_k.e + q and is in the range [alpha,gamma]
  14904. const diyfp w = diyfp::mul(v, c_minus_k);
  14905. const diyfp w_minus = diyfp::mul(m_minus, c_minus_k);
  14906. const diyfp w_plus = diyfp::mul(m_plus, c_minus_k);
  14907. // ----(---+---)---------------(---+---)---------------(---+---)----
  14908. // w- w w+
  14909. // = c*m- = c*v = c*m+
  14910. //
  14911. // diyfp::mul rounds its result and c_minus_k is approximated too. w, w- and
  14912. // w+ are now off by a small amount.
  14913. // In fact:
  14914. //
  14915. // w - v * 10^k < 1 ulp
  14916. //
  14917. // To account for this inaccuracy, add resp. subtract 1 ulp.
  14918. //
  14919. // --------+---[---------------(---+---)---------------]---+--------
  14920. // w- M- w M+ w+
  14921. //
  14922. // Now any number in [M-, M+] (bounds included) will round to w when input,
  14923. // regardless of how the input rounding algorithm breaks ties.
  14924. //
  14925. // And digit_gen generates the shortest possible such number in [M-, M+].
  14926. // Note that this does not mean that Grisu2 always generates the shortest
  14927. // possible number in the interval (m-, m+).
  14928. const diyfp M_minus(w_minus.f + 1, w_minus.e);
  14929. const diyfp M_plus (w_plus.f - 1, w_plus.e );
  14930. decimal_exponent = -cached.k; // = -(-k) = k
  14931. grisu2_digit_gen(buf, len, decimal_exponent, M_minus, w, M_plus);
  14932. }
  14933. /*!
  14934. v = buf * 10^decimal_exponent
  14935. len is the length of the buffer (number of decimal digits)
  14936. The buffer must be large enough, i.e. >= max_digits10.
  14937. */
  14938. template<typename FloatType>
  14939. JSON_HEDLEY_NON_NULL(1)
  14940. void grisu2(char* buf, int& len, int& decimal_exponent, FloatType value)
  14941. {
  14942. static_assert(diyfp::kPrecision >= std::numeric_limits<FloatType>::digits + 3,
  14943. "internal error: not enough precision");
  14944. JSON_ASSERT(std::isfinite(value));
  14945. JSON_ASSERT(value > 0);
  14946. // If the neighbors (and boundaries) of 'value' are always computed for double-precision
  14947. // numbers, all float's can be recovered using strtod (and strtof). However, the resulting
  14948. // decimal representations are not exactly "short".
  14949. //
  14950. // The documentation for 'std::to_chars' (https://en.cppreference.com/w/cpp/utility/to_chars)
  14951. // says "value is converted to a string as if by std::sprintf in the default ("C") locale"
  14952. // and since sprintf promotes floats to doubles, I think this is exactly what 'std::to_chars'
  14953. // does.
  14954. // On the other hand, the documentation for 'std::to_chars' requires that "parsing the
  14955. // representation using the corresponding std::from_chars function recovers value exactly". That
  14956. // indicates that single precision floating-point numbers should be recovered using
  14957. // 'std::strtof'.
  14958. //
  14959. // NB: If the neighbors are computed for single-precision numbers, there is a single float
  14960. // (7.0385307e-26f) which can't be recovered using strtod. The resulting double precision
  14961. // value is off by 1 ulp.
  14962. #if 0
  14963. const boundaries w = compute_boundaries(static_cast<double>(value));
  14964. #else
  14965. const boundaries w = compute_boundaries(value);
  14966. #endif
  14967. grisu2(buf, len, decimal_exponent, w.minus, w.w, w.plus);
  14968. }
  14969. /*!
  14970. @brief appends a decimal representation of e to buf
  14971. @return a pointer to the element following the exponent.
  14972. @pre -1000 < e < 1000
  14973. */
  14974. JSON_HEDLEY_NON_NULL(1)
  14975. JSON_HEDLEY_RETURNS_NON_NULL
  14976. inline char* append_exponent(char* buf, int e)
  14977. {
  14978. JSON_ASSERT(e > -1000);
  14979. JSON_ASSERT(e < 1000);
  14980. if (e < 0)
  14981. {
  14982. e = -e;
  14983. *buf++ = '-';
  14984. }
  14985. else
  14986. {
  14987. *buf++ = '+';
  14988. }
  14989. auto k = static_cast<std::uint32_t>(e);
  14990. if (k < 10)
  14991. {
  14992. // Always print at least two digits in the exponent.
  14993. // This is for compatibility with printf("%g").
  14994. *buf++ = '0';
  14995. *buf++ = static_cast<char>('0' + k);
  14996. }
  14997. else if (k < 100)
  14998. {
  14999. *buf++ = static_cast<char>('0' + k / 10);
  15000. k %= 10;
  15001. *buf++ = static_cast<char>('0' + k);
  15002. }
  15003. else
  15004. {
  15005. *buf++ = static_cast<char>('0' + k / 100);
  15006. k %= 100;
  15007. *buf++ = static_cast<char>('0' + k / 10);
  15008. k %= 10;
  15009. *buf++ = static_cast<char>('0' + k);
  15010. }
  15011. return buf;
  15012. }
  15013. /*!
  15014. @brief prettify v = buf * 10^decimal_exponent
  15015. If v is in the range [10^min_exp, 10^max_exp) it will be printed in fixed-point
  15016. notation. Otherwise it will be printed in exponential notation.
  15017. @pre min_exp < 0
  15018. @pre max_exp > 0
  15019. */
  15020. JSON_HEDLEY_NON_NULL(1)
  15021. JSON_HEDLEY_RETURNS_NON_NULL
  15022. inline char* format_buffer(char* buf, int len, int decimal_exponent,
  15023. int min_exp, int max_exp)
  15024. {
  15025. JSON_ASSERT(min_exp < 0);
  15026. JSON_ASSERT(max_exp > 0);
  15027. const int k = len;
  15028. const int n = len + decimal_exponent;
  15029. // v = buf * 10^(n-k)
  15030. // k is the length of the buffer (number of decimal digits)
  15031. // n is the position of the decimal point relative to the start of the buffer.
  15032. if (k <= n && n <= max_exp)
  15033. {
  15034. // digits[000]
  15035. // len <= max_exp + 2
  15036. std::memset(buf + k, '0', static_cast<size_t>(n) - static_cast<size_t>(k));
  15037. // Make it look like a floating-point number (#362, #378)
  15038. buf[n + 0] = '.';
  15039. buf[n + 1] = '0';
  15040. return buf + (static_cast<size_t>(n) + 2);
  15041. }
  15042. if (0 < n && n <= max_exp)
  15043. {
  15044. // dig.its
  15045. // len <= max_digits10 + 1
  15046. JSON_ASSERT(k > n);
  15047. std::memmove(buf + (static_cast<size_t>(n) + 1), buf + n, static_cast<size_t>(k) - static_cast<size_t>(n));
  15048. buf[n] = '.';
  15049. return buf + (static_cast<size_t>(k) + 1U);
  15050. }
  15051. if (min_exp < n && n <= 0)
  15052. {
  15053. // 0.[000]digits
  15054. // len <= 2 + (-min_exp - 1) + max_digits10
  15055. std::memmove(buf + (2 + static_cast<size_t>(-n)), buf, static_cast<size_t>(k));
  15056. buf[0] = '0';
  15057. buf[1] = '.';
  15058. std::memset(buf + 2, '0', static_cast<size_t>(-n));
  15059. return buf + (2U + static_cast<size_t>(-n) + static_cast<size_t>(k));
  15060. }
  15061. if (k == 1)
  15062. {
  15063. // dE+123
  15064. // len <= 1 + 5
  15065. buf += 1;
  15066. }
  15067. else
  15068. {
  15069. // d.igitsE+123
  15070. // len <= max_digits10 + 1 + 5
  15071. std::memmove(buf + 2, buf + 1, static_cast<size_t>(k) - 1);
  15072. buf[1] = '.';
  15073. buf += 1 + static_cast<size_t>(k);
  15074. }
  15075. *buf++ = 'e';
  15076. return append_exponent(buf, n - 1);
  15077. }
  15078. } // namespace dtoa_impl
  15079. /*!
  15080. @brief generates a decimal representation of the floating-point number value in [first, last).
  15081. The format of the resulting decimal representation is similar to printf's %g
  15082. format. Returns an iterator pointing past-the-end of the decimal representation.
  15083. @note The input number must be finite, i.e. NaN's and Inf's are not supported.
  15084. @note The buffer must be large enough.
  15085. @note The result is NOT null-terminated.
  15086. */
  15087. template<typename FloatType>
  15088. JSON_HEDLEY_NON_NULL(1, 2)
  15089. JSON_HEDLEY_RETURNS_NON_NULL
  15090. char* to_chars(char* first, const char* last, FloatType value)
  15091. {
  15092. static_cast<void>(last); // maybe unused - fix warning
  15093. JSON_ASSERT(std::isfinite(value));
  15094. // Use signbit(value) instead of (value < 0) since signbit works for -0.
  15095. if (std::signbit(value))
  15096. {
  15097. value = -value;
  15098. *first++ = '-';
  15099. }
  15100. #ifdef __GNUC__
  15101. #pragma GCC diagnostic push
  15102. #pragma GCC diagnostic ignored "-Wfloat-equal"
  15103. #endif
  15104. if (value == 0) // +-0
  15105. {
  15106. *first++ = '0';
  15107. // Make it look like a floating-point number (#362, #378)
  15108. *first++ = '.';
  15109. *first++ = '0';
  15110. return first;
  15111. }
  15112. #ifdef __GNUC__
  15113. #pragma GCC diagnostic pop
  15114. #endif
  15115. JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10);
  15116. // Compute v = buffer * 10^decimal_exponent.
  15117. // The decimal digits are stored in the buffer, which needs to be interpreted
  15118. // as an unsigned decimal integer.
  15119. // len is the length of the buffer, i.e. the number of decimal digits.
  15120. int len = 0;
  15121. int decimal_exponent = 0;
  15122. dtoa_impl::grisu2(first, len, decimal_exponent, value);
  15123. JSON_ASSERT(len <= std::numeric_limits<FloatType>::max_digits10);
  15124. // Format the buffer like printf("%.*g", prec, value)
  15125. constexpr int kMinExp = -4;
  15126. // Use digits10 here to increase compatibility with version 2.
  15127. constexpr int kMaxExp = std::numeric_limits<FloatType>::digits10;
  15128. JSON_ASSERT(last - first >= kMaxExp + 2);
  15129. JSON_ASSERT(last - first >= 2 + (-kMinExp - 1) + std::numeric_limits<FloatType>::max_digits10);
  15130. JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10 + 6);
  15131. return dtoa_impl::format_buffer(first, len, decimal_exponent, kMinExp, kMaxExp);
  15132. }
  15133. } // namespace detail
  15134. } // namespace nlohmann
  15135. // #include <nlohmann/detail/exceptions.hpp>
  15136. // #include <nlohmann/detail/macro_scope.hpp>
  15137. // #include <nlohmann/detail/meta/cpp_future.hpp>
  15138. // #include <nlohmann/detail/output/binary_writer.hpp>
  15139. // #include <nlohmann/detail/output/output_adapters.hpp>
  15140. // #include <nlohmann/detail/string_concat.hpp>
  15141. // #include <nlohmann/detail/value_t.hpp>
  15142. namespace nlohmann
  15143. {
  15144. namespace detail
  15145. {
  15146. ///////////////////
  15147. // serialization //
  15148. ///////////////////
  15149. /// how to treat decoding errors
  15150. enum class error_handler_t
  15151. {
  15152. strict, ///< throw a type_error exception in case of invalid UTF-8
  15153. replace, ///< replace invalid UTF-8 sequences with U+FFFD
  15154. ignore ///< ignore invalid UTF-8 sequences
  15155. };
  15156. template<typename BasicJsonType>
  15157. class serializer
  15158. {
  15159. using string_t = typename BasicJsonType::string_t;
  15160. using number_float_t = typename BasicJsonType::number_float_t;
  15161. using number_integer_t = typename BasicJsonType::number_integer_t;
  15162. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  15163. using binary_char_t = typename BasicJsonType::binary_t::value_type;
  15164. static constexpr std::uint8_t UTF8_ACCEPT = 0;
  15165. static constexpr std::uint8_t UTF8_REJECT = 1;
  15166. public:
  15167. /*!
  15168. @param[in] s output stream to serialize to
  15169. @param[in] ichar indentation character to use
  15170. @param[in] error_handler_ how to react on decoding errors
  15171. */
  15172. serializer(output_adapter_t<char> s, const char ichar,
  15173. error_handler_t error_handler_ = error_handler_t::strict)
  15174. : o(std::move(s))
  15175. , loc(std::localeconv())
  15176. , thousands_sep(loc->thousands_sep == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->thousands_sep)))
  15177. , decimal_point(loc->decimal_point == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->decimal_point)))
  15178. , indent_char(ichar)
  15179. , indent_string(512, indent_char)
  15180. , error_handler(error_handler_)
  15181. {}
  15182. // delete because of pointer members
  15183. serializer(const serializer&) = delete;
  15184. serializer& operator=(const serializer&) = delete;
  15185. serializer(serializer&&) = delete;
  15186. serializer& operator=(serializer&&) = delete;
  15187. ~serializer() = default;
  15188. /*!
  15189. @brief internal implementation of the serialization function
  15190. This function is called by the public member function dump and organizes
  15191. the serialization internally. The indentation level is propagated as
  15192. additional parameter. In case of arrays and objects, the function is
  15193. called recursively.
  15194. - strings and object keys are escaped using `escape_string()`
  15195. - integer numbers are converted implicitly via `operator<<`
  15196. - floating-point numbers are converted to a string using `"%g"` format
  15197. - binary values are serialized as objects containing the subtype and the
  15198. byte array
  15199. @param[in] val value to serialize
  15200. @param[in] pretty_print whether the output shall be pretty-printed
  15201. @param[in] ensure_ascii If @a ensure_ascii is true, all non-ASCII characters
  15202. in the output are escaped with `\uXXXX` sequences, and the result consists
  15203. of ASCII characters only.
  15204. @param[in] indent_step the indent level
  15205. @param[in] current_indent the current indent level (only used internally)
  15206. */
  15207. void dump(const BasicJsonType& val,
  15208. const bool pretty_print,
  15209. const bool ensure_ascii,
  15210. const unsigned int indent_step,
  15211. const unsigned int current_indent = 0)
  15212. {
  15213. switch (val.m_type)
  15214. {
  15215. case value_t::object:
  15216. {
  15217. if (val.m_value.object->empty())
  15218. {
  15219. o->write_characters("{}", 2);
  15220. return;
  15221. }
  15222. if (pretty_print)
  15223. {
  15224. o->write_characters("{\n", 2);
  15225. // variable to hold indentation for recursive calls
  15226. const auto new_indent = current_indent + indent_step;
  15227. if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
  15228. {
  15229. indent_string.resize(indent_string.size() * 2, ' ');
  15230. }
  15231. // first n-1 elements
  15232. auto i = val.m_value.object->cbegin();
  15233. for (std::size_t cnt = 0; cnt < val.m_value.object->size() - 1; ++cnt, ++i)
  15234. {
  15235. o->write_characters(indent_string.c_str(), new_indent);
  15236. o->write_character('\"');
  15237. dump_escaped(i->first, ensure_ascii);
  15238. o->write_characters("\": ", 3);
  15239. dump(i->second, true, ensure_ascii, indent_step, new_indent);
  15240. o->write_characters(",\n", 2);
  15241. }
  15242. // last element
  15243. JSON_ASSERT(i != val.m_value.object->cend());
  15244. JSON_ASSERT(std::next(i) == val.m_value.object->cend());
  15245. o->write_characters(indent_string.c_str(), new_indent);
  15246. o->write_character('\"');
  15247. dump_escaped(i->first, ensure_ascii);
  15248. o->write_characters("\": ", 3);
  15249. dump(i->second, true, ensure_ascii, indent_step, new_indent);
  15250. o->write_character('\n');
  15251. o->write_characters(indent_string.c_str(), current_indent);
  15252. o->write_character('}');
  15253. }
  15254. else
  15255. {
  15256. o->write_character('{');
  15257. // first n-1 elements
  15258. auto i = val.m_value.object->cbegin();
  15259. for (std::size_t cnt = 0; cnt < val.m_value.object->size() - 1; ++cnt, ++i)
  15260. {
  15261. o->write_character('\"');
  15262. dump_escaped(i->first, ensure_ascii);
  15263. o->write_characters("\":", 2);
  15264. dump(i->second, false, ensure_ascii, indent_step, current_indent);
  15265. o->write_character(',');
  15266. }
  15267. // last element
  15268. JSON_ASSERT(i != val.m_value.object->cend());
  15269. JSON_ASSERT(std::next(i) == val.m_value.object->cend());
  15270. o->write_character('\"');
  15271. dump_escaped(i->first, ensure_ascii);
  15272. o->write_characters("\":", 2);
  15273. dump(i->second, false, ensure_ascii, indent_step, current_indent);
  15274. o->write_character('}');
  15275. }
  15276. return;
  15277. }
  15278. case value_t::array:
  15279. {
  15280. if (val.m_value.array->empty())
  15281. {
  15282. o->write_characters("[]", 2);
  15283. return;
  15284. }
  15285. if (pretty_print)
  15286. {
  15287. o->write_characters("[\n", 2);
  15288. // variable to hold indentation for recursive calls
  15289. const auto new_indent = current_indent + indent_step;
  15290. if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
  15291. {
  15292. indent_string.resize(indent_string.size() * 2, ' ');
  15293. }
  15294. // first n-1 elements
  15295. for (auto i = val.m_value.array->cbegin();
  15296. i != val.m_value.array->cend() - 1; ++i)
  15297. {
  15298. o->write_characters(indent_string.c_str(), new_indent);
  15299. dump(*i, true, ensure_ascii, indent_step, new_indent);
  15300. o->write_characters(",\n", 2);
  15301. }
  15302. // last element
  15303. JSON_ASSERT(!val.m_value.array->empty());
  15304. o->write_characters(indent_string.c_str(), new_indent);
  15305. dump(val.m_value.array->back(), true, ensure_ascii, indent_step, new_indent);
  15306. o->write_character('\n');
  15307. o->write_characters(indent_string.c_str(), current_indent);
  15308. o->write_character(']');
  15309. }
  15310. else
  15311. {
  15312. o->write_character('[');
  15313. // first n-1 elements
  15314. for (auto i = val.m_value.array->cbegin();
  15315. i != val.m_value.array->cend() - 1; ++i)
  15316. {
  15317. dump(*i, false, ensure_ascii, indent_step, current_indent);
  15318. o->write_character(',');
  15319. }
  15320. // last element
  15321. JSON_ASSERT(!val.m_value.array->empty());
  15322. dump(val.m_value.array->back(), false, ensure_ascii, indent_step, current_indent);
  15323. o->write_character(']');
  15324. }
  15325. return;
  15326. }
  15327. case value_t::string:
  15328. {
  15329. o->write_character('\"');
  15330. dump_escaped(*val.m_value.string, ensure_ascii);
  15331. o->write_character('\"');
  15332. return;
  15333. }
  15334. case value_t::binary:
  15335. {
  15336. if (pretty_print)
  15337. {
  15338. o->write_characters("{\n", 2);
  15339. // variable to hold indentation for recursive calls
  15340. const auto new_indent = current_indent + indent_step;
  15341. if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
  15342. {
  15343. indent_string.resize(indent_string.size() * 2, ' ');
  15344. }
  15345. o->write_characters(indent_string.c_str(), new_indent);
  15346. o->write_characters("\"bytes\": [", 10);
  15347. if (!val.m_value.binary->empty())
  15348. {
  15349. for (auto i = val.m_value.binary->cbegin();
  15350. i != val.m_value.binary->cend() - 1; ++i)
  15351. {
  15352. dump_integer(*i);
  15353. o->write_characters(", ", 2);
  15354. }
  15355. dump_integer(val.m_value.binary->back());
  15356. }
  15357. o->write_characters("],\n", 3);
  15358. o->write_characters(indent_string.c_str(), new_indent);
  15359. o->write_characters("\"subtype\": ", 11);
  15360. if (val.m_value.binary->has_subtype())
  15361. {
  15362. dump_integer(val.m_value.binary->subtype());
  15363. }
  15364. else
  15365. {
  15366. o->write_characters("null", 4);
  15367. }
  15368. o->write_character('\n');
  15369. o->write_characters(indent_string.c_str(), current_indent);
  15370. o->write_character('}');
  15371. }
  15372. else
  15373. {
  15374. o->write_characters("{\"bytes\":[", 10);
  15375. if (!val.m_value.binary->empty())
  15376. {
  15377. for (auto i = val.m_value.binary->cbegin();
  15378. i != val.m_value.binary->cend() - 1; ++i)
  15379. {
  15380. dump_integer(*i);
  15381. o->write_character(',');
  15382. }
  15383. dump_integer(val.m_value.binary->back());
  15384. }
  15385. o->write_characters("],\"subtype\":", 12);
  15386. if (val.m_value.binary->has_subtype())
  15387. {
  15388. dump_integer(val.m_value.binary->subtype());
  15389. o->write_character('}');
  15390. }
  15391. else
  15392. {
  15393. o->write_characters("null}", 5);
  15394. }
  15395. }
  15396. return;
  15397. }
  15398. case value_t::boolean:
  15399. {
  15400. if (val.m_value.boolean)
  15401. {
  15402. o->write_characters("true", 4);
  15403. }
  15404. else
  15405. {
  15406. o->write_characters("false", 5);
  15407. }
  15408. return;
  15409. }
  15410. case value_t::number_integer:
  15411. {
  15412. dump_integer(val.m_value.number_integer);
  15413. return;
  15414. }
  15415. case value_t::number_unsigned:
  15416. {
  15417. dump_integer(val.m_value.number_unsigned);
  15418. return;
  15419. }
  15420. case value_t::number_float:
  15421. {
  15422. dump_float(val.m_value.number_float);
  15423. return;
  15424. }
  15425. case value_t::discarded:
  15426. {
  15427. o->write_characters("<discarded>", 11);
  15428. return;
  15429. }
  15430. case value_t::null:
  15431. {
  15432. o->write_characters("null", 4);
  15433. return;
  15434. }
  15435. default: // LCOV_EXCL_LINE
  15436. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  15437. }
  15438. }
  15439. JSON_PRIVATE_UNLESS_TESTED:
  15440. /*!
  15441. @brief dump escaped string
  15442. Escape a string by replacing certain special characters by a sequence of an
  15443. escape character (backslash) and another character and other control
  15444. characters by a sequence of "\u" followed by a four-digit hex
  15445. representation. The escaped string is written to output stream @a o.
  15446. @param[in] s the string to escape
  15447. @param[in] ensure_ascii whether to escape non-ASCII characters with
  15448. \uXXXX sequences
  15449. @complexity Linear in the length of string @a s.
  15450. */
  15451. void dump_escaped(const string_t& s, const bool ensure_ascii)
  15452. {
  15453. std::uint32_t codepoint{};
  15454. std::uint8_t state = UTF8_ACCEPT;
  15455. std::size_t bytes = 0; // number of bytes written to string_buffer
  15456. // number of bytes written at the point of the last valid byte
  15457. std::size_t bytes_after_last_accept = 0;
  15458. std::size_t undumped_chars = 0;
  15459. for (std::size_t i = 0; i < s.size(); ++i)
  15460. {
  15461. const auto byte = static_cast<std::uint8_t>(s[i]);
  15462. switch (decode(state, codepoint, byte))
  15463. {
  15464. case UTF8_ACCEPT: // decode found a new code point
  15465. {
  15466. switch (codepoint)
  15467. {
  15468. case 0x08: // backspace
  15469. {
  15470. string_buffer[bytes++] = '\\';
  15471. string_buffer[bytes++] = 'b';
  15472. break;
  15473. }
  15474. case 0x09: // horizontal tab
  15475. {
  15476. string_buffer[bytes++] = '\\';
  15477. string_buffer[bytes++] = 't';
  15478. break;
  15479. }
  15480. case 0x0A: // newline
  15481. {
  15482. string_buffer[bytes++] = '\\';
  15483. string_buffer[bytes++] = 'n';
  15484. break;
  15485. }
  15486. case 0x0C: // formfeed
  15487. {
  15488. string_buffer[bytes++] = '\\';
  15489. string_buffer[bytes++] = 'f';
  15490. break;
  15491. }
  15492. case 0x0D: // carriage return
  15493. {
  15494. string_buffer[bytes++] = '\\';
  15495. string_buffer[bytes++] = 'r';
  15496. break;
  15497. }
  15498. case 0x22: // quotation mark
  15499. {
  15500. string_buffer[bytes++] = '\\';
  15501. string_buffer[bytes++] = '\"';
  15502. break;
  15503. }
  15504. case 0x5C: // reverse solidus
  15505. {
  15506. string_buffer[bytes++] = '\\';
  15507. string_buffer[bytes++] = '\\';
  15508. break;
  15509. }
  15510. default:
  15511. {
  15512. // escape control characters (0x00..0x1F) or, if
  15513. // ensure_ascii parameter is used, non-ASCII characters
  15514. if ((codepoint <= 0x1F) || (ensure_ascii && (codepoint >= 0x7F)))
  15515. {
  15516. if (codepoint <= 0xFFFF)
  15517. {
  15518. // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  15519. static_cast<void>((std::snprintf)(string_buffer.data() + bytes, 7, "\\u%04x",
  15520. static_cast<std::uint16_t>(codepoint)));
  15521. bytes += 6;
  15522. }
  15523. else
  15524. {
  15525. // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  15526. static_cast<void>((std::snprintf)(string_buffer.data() + bytes, 13, "\\u%04x\\u%04x",
  15527. static_cast<std::uint16_t>(0xD7C0u + (codepoint >> 10u)),
  15528. static_cast<std::uint16_t>(0xDC00u + (codepoint & 0x3FFu))));
  15529. bytes += 12;
  15530. }
  15531. }
  15532. else
  15533. {
  15534. // copy byte to buffer (all previous bytes
  15535. // been copied have in default case above)
  15536. string_buffer[bytes++] = s[i];
  15537. }
  15538. break;
  15539. }
  15540. }
  15541. // write buffer and reset index; there must be 13 bytes
  15542. // left, as this is the maximal number of bytes to be
  15543. // written ("\uxxxx\uxxxx\0") for one code point
  15544. if (string_buffer.size() - bytes < 13)
  15545. {
  15546. o->write_characters(string_buffer.data(), bytes);
  15547. bytes = 0;
  15548. }
  15549. // remember the byte position of this accept
  15550. bytes_after_last_accept = bytes;
  15551. undumped_chars = 0;
  15552. break;
  15553. }
  15554. case UTF8_REJECT: // decode found invalid UTF-8 byte
  15555. {
  15556. switch (error_handler)
  15557. {
  15558. case error_handler_t::strict:
  15559. {
  15560. JSON_THROW(type_error::create(316, concat("invalid UTF-8 byte at index ", std::to_string(i), ": 0x", hex_bytes(byte | 0)), nullptr));
  15561. }
  15562. case error_handler_t::ignore:
  15563. case error_handler_t::replace:
  15564. {
  15565. // in case we saw this character the first time, we
  15566. // would like to read it again, because the byte
  15567. // may be OK for itself, but just not OK for the
  15568. // previous sequence
  15569. if (undumped_chars > 0)
  15570. {
  15571. --i;
  15572. }
  15573. // reset length buffer to the last accepted index;
  15574. // thus removing/ignoring the invalid characters
  15575. bytes = bytes_after_last_accept;
  15576. if (error_handler == error_handler_t::replace)
  15577. {
  15578. // add a replacement character
  15579. if (ensure_ascii)
  15580. {
  15581. string_buffer[bytes++] = '\\';
  15582. string_buffer[bytes++] = 'u';
  15583. string_buffer[bytes++] = 'f';
  15584. string_buffer[bytes++] = 'f';
  15585. string_buffer[bytes++] = 'f';
  15586. string_buffer[bytes++] = 'd';
  15587. }
  15588. else
  15589. {
  15590. string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xEF');
  15591. string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xBF');
  15592. string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xBD');
  15593. }
  15594. // write buffer and reset index; there must be 13 bytes
  15595. // left, as this is the maximal number of bytes to be
  15596. // written ("\uxxxx\uxxxx\0") for one code point
  15597. if (string_buffer.size() - bytes < 13)
  15598. {
  15599. o->write_characters(string_buffer.data(), bytes);
  15600. bytes = 0;
  15601. }
  15602. bytes_after_last_accept = bytes;
  15603. }
  15604. undumped_chars = 0;
  15605. // continue processing the string
  15606. state = UTF8_ACCEPT;
  15607. break;
  15608. }
  15609. default: // LCOV_EXCL_LINE
  15610. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  15611. }
  15612. break;
  15613. }
  15614. default: // decode found yet incomplete multi-byte code point
  15615. {
  15616. if (!ensure_ascii)
  15617. {
  15618. // code point will not be escaped - copy byte to buffer
  15619. string_buffer[bytes++] = s[i];
  15620. }
  15621. ++undumped_chars;
  15622. break;
  15623. }
  15624. }
  15625. }
  15626. // we finished processing the string
  15627. if (JSON_HEDLEY_LIKELY(state == UTF8_ACCEPT))
  15628. {
  15629. // write buffer
  15630. if (bytes > 0)
  15631. {
  15632. o->write_characters(string_buffer.data(), bytes);
  15633. }
  15634. }
  15635. else
  15636. {
  15637. // we finish reading, but do not accept: string was incomplete
  15638. switch (error_handler)
  15639. {
  15640. case error_handler_t::strict:
  15641. {
  15642. 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));
  15643. }
  15644. case error_handler_t::ignore:
  15645. {
  15646. // write all accepted bytes
  15647. o->write_characters(string_buffer.data(), bytes_after_last_accept);
  15648. break;
  15649. }
  15650. case error_handler_t::replace:
  15651. {
  15652. // write all accepted bytes
  15653. o->write_characters(string_buffer.data(), bytes_after_last_accept);
  15654. // add a replacement character
  15655. if (ensure_ascii)
  15656. {
  15657. o->write_characters("\\ufffd", 6);
  15658. }
  15659. else
  15660. {
  15661. o->write_characters("\xEF\xBF\xBD", 3);
  15662. }
  15663. break;
  15664. }
  15665. default: // LCOV_EXCL_LINE
  15666. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  15667. }
  15668. }
  15669. }
  15670. private:
  15671. /*!
  15672. @brief count digits
  15673. Count the number of decimal (base 10) digits for an input unsigned integer.
  15674. @param[in] x unsigned integer number to count its digits
  15675. @return number of decimal digits
  15676. */
  15677. inline unsigned int count_digits(number_unsigned_t x) noexcept
  15678. {
  15679. unsigned int n_digits = 1;
  15680. for (;;)
  15681. {
  15682. if (x < 10)
  15683. {
  15684. return n_digits;
  15685. }
  15686. if (x < 100)
  15687. {
  15688. return n_digits + 1;
  15689. }
  15690. if (x < 1000)
  15691. {
  15692. return n_digits + 2;
  15693. }
  15694. if (x < 10000)
  15695. {
  15696. return n_digits + 3;
  15697. }
  15698. x = x / 10000u;
  15699. n_digits += 4;
  15700. }
  15701. }
  15702. /*!
  15703. * @brief convert a byte to a uppercase hex representation
  15704. * @param[in] byte byte to represent
  15705. * @return representation ("00".."FF")
  15706. */
  15707. static std::string hex_bytes(std::uint8_t byte)
  15708. {
  15709. std::string result = "FF";
  15710. constexpr const char* nibble_to_hex = "0123456789ABCDEF";
  15711. result[0] = nibble_to_hex[byte / 16];
  15712. result[1] = nibble_to_hex[byte % 16];
  15713. return result;
  15714. }
  15715. // templates to avoid warnings about useless casts
  15716. template <typename NumberType, enable_if_t<std::is_signed<NumberType>::value, int> = 0>
  15717. bool is_negative_number(NumberType x)
  15718. {
  15719. return x < 0;
  15720. }
  15721. template < typename NumberType, enable_if_t <std::is_unsigned<NumberType>::value, int > = 0 >
  15722. bool is_negative_number(NumberType /*unused*/)
  15723. {
  15724. return false;
  15725. }
  15726. /*!
  15727. @brief dump an integer
  15728. Dump a given integer to output stream @a o. Works internally with
  15729. @a number_buffer.
  15730. @param[in] x integer number (signed or unsigned) to dump
  15731. @tparam NumberType either @a number_integer_t or @a number_unsigned_t
  15732. */
  15733. template < typename NumberType, detail::enable_if_t <
  15734. std::is_integral<NumberType>::value ||
  15735. std::is_same<NumberType, number_unsigned_t>::value ||
  15736. std::is_same<NumberType, number_integer_t>::value ||
  15737. std::is_same<NumberType, binary_char_t>::value,
  15738. int > = 0 >
  15739. void dump_integer(NumberType x)
  15740. {
  15741. static constexpr std::array<std::array<char, 2>, 100> digits_to_99
  15742. {
  15743. {
  15744. {{'0', '0'}}, {{'0', '1'}}, {{'0', '2'}}, {{'0', '3'}}, {{'0', '4'}}, {{'0', '5'}}, {{'0', '6'}}, {{'0', '7'}}, {{'0', '8'}}, {{'0', '9'}},
  15745. {{'1', '0'}}, {{'1', '1'}}, {{'1', '2'}}, {{'1', '3'}}, {{'1', '4'}}, {{'1', '5'}}, {{'1', '6'}}, {{'1', '7'}}, {{'1', '8'}}, {{'1', '9'}},
  15746. {{'2', '0'}}, {{'2', '1'}}, {{'2', '2'}}, {{'2', '3'}}, {{'2', '4'}}, {{'2', '5'}}, {{'2', '6'}}, {{'2', '7'}}, {{'2', '8'}}, {{'2', '9'}},
  15747. {{'3', '0'}}, {{'3', '1'}}, {{'3', '2'}}, {{'3', '3'}}, {{'3', '4'}}, {{'3', '5'}}, {{'3', '6'}}, {{'3', '7'}}, {{'3', '8'}}, {{'3', '9'}},
  15748. {{'4', '0'}}, {{'4', '1'}}, {{'4', '2'}}, {{'4', '3'}}, {{'4', '4'}}, {{'4', '5'}}, {{'4', '6'}}, {{'4', '7'}}, {{'4', '8'}}, {{'4', '9'}},
  15749. {{'5', '0'}}, {{'5', '1'}}, {{'5', '2'}}, {{'5', '3'}}, {{'5', '4'}}, {{'5', '5'}}, {{'5', '6'}}, {{'5', '7'}}, {{'5', '8'}}, {{'5', '9'}},
  15750. {{'6', '0'}}, {{'6', '1'}}, {{'6', '2'}}, {{'6', '3'}}, {{'6', '4'}}, {{'6', '5'}}, {{'6', '6'}}, {{'6', '7'}}, {{'6', '8'}}, {{'6', '9'}},
  15751. {{'7', '0'}}, {{'7', '1'}}, {{'7', '2'}}, {{'7', '3'}}, {{'7', '4'}}, {{'7', '5'}}, {{'7', '6'}}, {{'7', '7'}}, {{'7', '8'}}, {{'7', '9'}},
  15752. {{'8', '0'}}, {{'8', '1'}}, {{'8', '2'}}, {{'8', '3'}}, {{'8', '4'}}, {{'8', '5'}}, {{'8', '6'}}, {{'8', '7'}}, {{'8', '8'}}, {{'8', '9'}},
  15753. {{'9', '0'}}, {{'9', '1'}}, {{'9', '2'}}, {{'9', '3'}}, {{'9', '4'}}, {{'9', '5'}}, {{'9', '6'}}, {{'9', '7'}}, {{'9', '8'}}, {{'9', '9'}},
  15754. }
  15755. };
  15756. // special case for "0"
  15757. if (x == 0)
  15758. {
  15759. o->write_character('0');
  15760. return;
  15761. }
  15762. // use a pointer to fill the buffer
  15763. auto buffer_ptr = number_buffer.begin(); // NOLINT(llvm-qualified-auto,readability-qualified-auto,cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  15764. number_unsigned_t abs_value;
  15765. unsigned int n_chars{};
  15766. if (is_negative_number(x))
  15767. {
  15768. *buffer_ptr = '-';
  15769. abs_value = remove_sign(static_cast<number_integer_t>(x));
  15770. // account one more byte for the minus sign
  15771. n_chars = 1 + count_digits(abs_value);
  15772. }
  15773. else
  15774. {
  15775. abs_value = static_cast<number_unsigned_t>(x);
  15776. n_chars = count_digits(abs_value);
  15777. }
  15778. // spare 1 byte for '\0'
  15779. JSON_ASSERT(n_chars < number_buffer.size() - 1);
  15780. // jump to the end to generate the string from backward,
  15781. // so we later avoid reversing the result
  15782. buffer_ptr += n_chars;
  15783. // Fast int2ascii implementation inspired by "Fastware" talk by Andrei Alexandrescu
  15784. // See: https://www.youtube.com/watch?v=o4-CwDo2zpg
  15785. while (abs_value >= 100)
  15786. {
  15787. const auto digits_index = static_cast<unsigned>((abs_value % 100));
  15788. abs_value /= 100;
  15789. *(--buffer_ptr) = digits_to_99[digits_index][1];
  15790. *(--buffer_ptr) = digits_to_99[digits_index][0];
  15791. }
  15792. if (abs_value >= 10)
  15793. {
  15794. const auto digits_index = static_cast<unsigned>(abs_value);
  15795. *(--buffer_ptr) = digits_to_99[digits_index][1];
  15796. *(--buffer_ptr) = digits_to_99[digits_index][0];
  15797. }
  15798. else
  15799. {
  15800. *(--buffer_ptr) = static_cast<char>('0' + abs_value);
  15801. }
  15802. o->write_characters(number_buffer.data(), n_chars);
  15803. }
  15804. /*!
  15805. @brief dump a floating-point number
  15806. Dump a given floating-point number to output stream @a o. Works internally
  15807. with @a number_buffer.
  15808. @param[in] x floating-point number to dump
  15809. */
  15810. void dump_float(number_float_t x)
  15811. {
  15812. // NaN / inf
  15813. if (!std::isfinite(x))
  15814. {
  15815. o->write_characters("null", 4);
  15816. return;
  15817. }
  15818. // If number_float_t is an IEEE-754 single or double precision number,
  15819. // use the Grisu2 algorithm to produce short numbers which are
  15820. // guaranteed to round-trip, using strtof and strtod, resp.
  15821. //
  15822. // NB: The test below works if <long double> == <double>.
  15823. static constexpr bool is_ieee_single_or_double
  15824. = (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) ||
  15825. (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);
  15826. dump_float(x, std::integral_constant<bool, is_ieee_single_or_double>());
  15827. }
  15828. void dump_float(number_float_t x, std::true_type /*is_ieee_single_or_double*/)
  15829. {
  15830. auto* begin = number_buffer.data();
  15831. auto* end = ::nlohmann::detail::to_chars(begin, begin + number_buffer.size(), x);
  15832. o->write_characters(begin, static_cast<size_t>(end - begin));
  15833. }
  15834. void dump_float(number_float_t x, std::false_type /*is_ieee_single_or_double*/)
  15835. {
  15836. // get number of digits for a float -> text -> float round-trip
  15837. static constexpr auto d = std::numeric_limits<number_float_t>::max_digits10;
  15838. // the actual conversion
  15839. // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  15840. std::ptrdiff_t len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*g", d, x);
  15841. // negative value indicates an error
  15842. JSON_ASSERT(len > 0);
  15843. // check if buffer was large enough
  15844. JSON_ASSERT(static_cast<std::size_t>(len) < number_buffer.size());
  15845. // erase thousands separator
  15846. if (thousands_sep != '\0')
  15847. {
  15848. // NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::remove returns an iterator, see https://github.com/nlohmann/json/issues/3081
  15849. const auto end = std::remove(number_buffer.begin(), number_buffer.begin() + len, thousands_sep);
  15850. std::fill(end, number_buffer.end(), '\0');
  15851. JSON_ASSERT((end - number_buffer.begin()) <= len);
  15852. len = (end - number_buffer.begin());
  15853. }
  15854. // convert decimal point to '.'
  15855. if (decimal_point != '\0' && decimal_point != '.')
  15856. {
  15857. // NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::find returns an iterator, see https://github.com/nlohmann/json/issues/3081
  15858. const auto dec_pos = std::find(number_buffer.begin(), number_buffer.end(), decimal_point);
  15859. if (dec_pos != number_buffer.end())
  15860. {
  15861. *dec_pos = '.';
  15862. }
  15863. }
  15864. o->write_characters(number_buffer.data(), static_cast<std::size_t>(len));
  15865. // determine if we need to append ".0"
  15866. const bool value_is_int_like =
  15867. std::none_of(number_buffer.begin(), number_buffer.begin() + len + 1,
  15868. [](char c)
  15869. {
  15870. return c == '.' || c == 'e';
  15871. });
  15872. if (value_is_int_like)
  15873. {
  15874. o->write_characters(".0", 2);
  15875. }
  15876. }
  15877. /*!
  15878. @brief check whether a string is UTF-8 encoded
  15879. The function checks each byte of a string whether it is UTF-8 encoded. The
  15880. result of the check is stored in the @a state parameter. The function must
  15881. be called initially with state 0 (accept). State 1 means the string must
  15882. be rejected, because the current byte is not allowed. If the string is
  15883. completely processed, but the state is non-zero, the string ended
  15884. prematurely; that is, the last byte indicated more bytes should have
  15885. followed.
  15886. @param[in,out] state the state of the decoding
  15887. @param[in,out] codep codepoint (valid only if resulting state is UTF8_ACCEPT)
  15888. @param[in] byte next byte to decode
  15889. @return new state
  15890. @note The function has been edited: a std::array is used.
  15891. @copyright Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
  15892. @sa http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
  15893. */
  15894. static std::uint8_t decode(std::uint8_t& state, std::uint32_t& codep, const std::uint8_t byte) noexcept
  15895. {
  15896. static const std::array<std::uint8_t, 400> utf8d =
  15897. {
  15898. {
  15899. 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
  15900. 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
  15901. 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
  15902. 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
  15903. 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
  15904. 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
  15905. 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
  15906. 0xA, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x4, 0x3, 0x3, // E0..EF
  15907. 0xB, 0x6, 0x6, 0x6, 0x5, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, // F0..FF
  15908. 0x0, 0x1, 0x2, 0x3, 0x5, 0x8, 0x7, 0x1, 0x1, 0x1, 0x4, 0x6, 0x1, 0x1, 0x1, 0x1, // s0..s0
  15909. 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
  15910. 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
  15911. 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
  15912. 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
  15913. }
  15914. };
  15915. JSON_ASSERT(byte < utf8d.size());
  15916. const std::uint8_t type = utf8d[byte];
  15917. codep = (state != UTF8_ACCEPT)
  15918. ? (byte & 0x3fu) | (codep << 6u)
  15919. : (0xFFu >> type) & (byte);
  15920. std::size_t index = 256u + static_cast<size_t>(state) * 16u + static_cast<size_t>(type);
  15921. JSON_ASSERT(index < 400);
  15922. state = utf8d[index];
  15923. return state;
  15924. }
  15925. /*
  15926. * Overload to make the compiler happy while it is instantiating
  15927. * dump_integer for number_unsigned_t.
  15928. * Must never be called.
  15929. */
  15930. number_unsigned_t remove_sign(number_unsigned_t x)
  15931. {
  15932. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  15933. return x; // LCOV_EXCL_LINE
  15934. }
  15935. /*
  15936. * Helper function for dump_integer
  15937. *
  15938. * This function takes a negative signed integer and returns its absolute
  15939. * value as unsigned integer. The plus/minus shuffling is necessary as we can
  15940. * not directly remove the sign of an arbitrary signed integer as the
  15941. * absolute values of INT_MIN and INT_MAX are usually not the same. See
  15942. * #1708 for details.
  15943. */
  15944. inline number_unsigned_t remove_sign(number_integer_t x) noexcept
  15945. {
  15946. JSON_ASSERT(x < 0 && x < (std::numeric_limits<number_integer_t>::max)()); // NOLINT(misc-redundant-expression)
  15947. return static_cast<number_unsigned_t>(-(x + 1)) + 1;
  15948. }
  15949. private:
  15950. /// the output of the serializer
  15951. output_adapter_t<char> o = nullptr;
  15952. /// a (hopefully) large enough character buffer
  15953. std::array<char, 64> number_buffer{{}};
  15954. /// the locale
  15955. const std::lconv* loc = nullptr;
  15956. /// the locale's thousand separator character
  15957. const char thousands_sep = '\0';
  15958. /// the locale's decimal point character
  15959. const char decimal_point = '\0';
  15960. /// string buffer
  15961. std::array<char, 512> string_buffer{{}};
  15962. /// the indentation character
  15963. const char indent_char;
  15964. /// the indentation string
  15965. string_t indent_string;
  15966. /// error_handler how to react on decoding errors
  15967. const error_handler_t error_handler;
  15968. };
  15969. } // namespace detail
  15970. } // namespace nlohmann
  15971. // #include <nlohmann/detail/value_t.hpp>
  15972. // #include <nlohmann/json_fwd.hpp>
  15973. // #include <nlohmann/ordered_map.hpp>
  15974. #include <functional> // equal_to, less
  15975. #include <initializer_list> // initializer_list
  15976. #include <iterator> // input_iterator_tag, iterator_traits
  15977. #include <memory> // allocator
  15978. #include <stdexcept> // for out_of_range
  15979. #include <type_traits> // enable_if, is_convertible
  15980. #include <utility> // pair
  15981. #include <vector> // vector
  15982. // #include <nlohmann/detail/macro_scope.hpp>
  15983. namespace nlohmann
  15984. {
  15985. /// ordered_map: a minimal map-like container that preserves insertion order
  15986. /// for use within nlohmann::basic_json<ordered_map>
  15987. template <class Key, class T, class IgnoredLess = std::less<Key>,
  15988. class Allocator = std::allocator<std::pair<const Key, T>>>
  15989. struct ordered_map : std::vector<std::pair<const Key, T>, Allocator>
  15990. {
  15991. using key_type = Key;
  15992. using mapped_type = T;
  15993. using Container = std::vector<std::pair<const Key, T>, Allocator>;
  15994. using iterator = typename Container::iterator;
  15995. using const_iterator = typename Container::const_iterator;
  15996. using size_type = typename Container::size_type;
  15997. using value_type = typename Container::value_type;
  15998. #ifdef JSON_HAS_CPP_14
  15999. using key_compare = std::equal_to<>;
  16000. #else
  16001. using key_compare = std::equal_to<Key>;
  16002. #endif
  16003. // Explicit constructors instead of `using Container::Container`
  16004. // otherwise older compilers choke on it (GCC <= 5.5, xcode <= 9.4)
  16005. ordered_map() noexcept(noexcept(Container())) : Container{} {}
  16006. explicit ordered_map(const Allocator& alloc) noexcept(noexcept(Container(alloc))) : Container{alloc} {}
  16007. template <class It>
  16008. ordered_map(It first, It last, const Allocator& alloc = Allocator())
  16009. : Container{first, last, alloc} {}
  16010. ordered_map(std::initializer_list<value_type> init, const Allocator& alloc = Allocator() )
  16011. : Container{init, alloc} {}
  16012. std::pair<iterator, bool> emplace(const key_type& key, T&& t)
  16013. {
  16014. for (auto it = this->begin(); it != this->end(); ++it)
  16015. {
  16016. if (m_compare(it->first, key))
  16017. {
  16018. return {it, false};
  16019. }
  16020. }
  16021. Container::emplace_back(key, t);
  16022. return {--this->end(), true};
  16023. }
  16024. T& operator[](const Key& key)
  16025. {
  16026. return emplace(key, T{}).first->second;
  16027. }
  16028. const T& operator[](const Key& key) const
  16029. {
  16030. return at(key);
  16031. }
  16032. T& at(const Key& key)
  16033. {
  16034. for (auto it = this->begin(); it != this->end(); ++it)
  16035. {
  16036. if (m_compare(it->first, key))
  16037. {
  16038. return it->second;
  16039. }
  16040. }
  16041. JSON_THROW(std::out_of_range("key not found"));
  16042. }
  16043. const T& at(const Key& key) const
  16044. {
  16045. for (auto it = this->begin(); it != this->end(); ++it)
  16046. {
  16047. if (m_compare(it->first, key))
  16048. {
  16049. return it->second;
  16050. }
  16051. }
  16052. JSON_THROW(std::out_of_range("key not found"));
  16053. }
  16054. size_type erase(const Key& key)
  16055. {
  16056. for (auto it = this->begin(); it != this->end(); ++it)
  16057. {
  16058. if (m_compare(it->first, key))
  16059. {
  16060. // Since we cannot move const Keys, re-construct them in place
  16061. for (auto next = it; ++next != this->end(); ++it)
  16062. {
  16063. it->~value_type(); // Destroy but keep allocation
  16064. new (&*it) value_type{std::move(*next)};
  16065. }
  16066. Container::pop_back();
  16067. return 1;
  16068. }
  16069. }
  16070. return 0;
  16071. }
  16072. iterator erase(iterator pos)
  16073. {
  16074. return erase(pos, std::next(pos));
  16075. }
  16076. iterator erase(iterator first, iterator last)
  16077. {
  16078. const auto elements_affected = std::distance(first, last);
  16079. const auto offset = std::distance(Container::begin(), first);
  16080. // This is the start situation. We need to delete elements_affected
  16081. // elements (3 in this example: e, f, g), and need to return an
  16082. // iterator past the last deleted element (h in this example).
  16083. // Note that offset is the distance from the start of the vector
  16084. // to first. We will need this later.
  16085. // [ a, b, c, d, e, f, g, h, i, j ]
  16086. // ^ ^
  16087. // first last
  16088. // Since we cannot move const Keys, we re-construct them in place.
  16089. // We start at first and re-construct (viz. copy) the elements from
  16090. // the back of the vector. Example for first iteration:
  16091. // ,--------.
  16092. // v | destroy e and re-construct with h
  16093. // [ a, b, c, d, e, f, g, h, i, j ]
  16094. // ^ ^
  16095. // it it + elements_affected
  16096. for (auto it = first; std::next(it, elements_affected) != Container::end(); ++it)
  16097. {
  16098. it->~value_type(); // destroy but keep allocation
  16099. new (&*it) value_type{std::move(*std::next(it, elements_affected))}; // "move" next element to it
  16100. }
  16101. // [ a, b, c, d, h, i, j, h, i, j ]
  16102. // ^ ^
  16103. // first last
  16104. // remove the unneeded elements at the end of the vector
  16105. Container::resize(this->size() - static_cast<size_type>(elements_affected));
  16106. // [ a, b, c, d, h, i, j ]
  16107. // ^ ^
  16108. // first last
  16109. // first is now pointing past the last deleted element, but we cannot
  16110. // use this iterator, because it may have been invalidated by the
  16111. // resize call. Instead, we can return begin() + offset.
  16112. return Container::begin() + offset;
  16113. }
  16114. size_type count(const Key& key) const
  16115. {
  16116. for (auto it = this->begin(); it != this->end(); ++it)
  16117. {
  16118. if (m_compare(it->first, key))
  16119. {
  16120. return 1;
  16121. }
  16122. }
  16123. return 0;
  16124. }
  16125. iterator find(const Key& key)
  16126. {
  16127. for (auto it = this->begin(); it != this->end(); ++it)
  16128. {
  16129. if (m_compare(it->first, key))
  16130. {
  16131. return it;
  16132. }
  16133. }
  16134. return Container::end();
  16135. }
  16136. const_iterator find(const Key& key) const
  16137. {
  16138. for (auto it = this->begin(); it != this->end(); ++it)
  16139. {
  16140. if (m_compare(it->first, key))
  16141. {
  16142. return it;
  16143. }
  16144. }
  16145. return Container::end();
  16146. }
  16147. std::pair<iterator, bool> insert( value_type&& value )
  16148. {
  16149. return emplace(value.first, std::move(value.second));
  16150. }
  16151. std::pair<iterator, bool> insert( const value_type& value )
  16152. {
  16153. for (auto it = this->begin(); it != this->end(); ++it)
  16154. {
  16155. if (m_compare(it->first, value.first))
  16156. {
  16157. return {it, false};
  16158. }
  16159. }
  16160. Container::push_back(value);
  16161. return {--this->end(), true};
  16162. }
  16163. template<typename InputIt>
  16164. using require_input_iter = typename std::enable_if<std::is_convertible<typename std::iterator_traits<InputIt>::iterator_category,
  16165. std::input_iterator_tag>::value>::type;
  16166. template<typename InputIt, typename = require_input_iter<InputIt>>
  16167. void insert(InputIt first, InputIt last)
  16168. {
  16169. for (auto it = first; it != last; ++it)
  16170. {
  16171. insert(*it);
  16172. }
  16173. }
  16174. private:
  16175. JSON_NO_UNIQUE_ADDRESS key_compare m_compare = key_compare();
  16176. };
  16177. } // namespace nlohmann
  16178. #if defined(JSON_HAS_CPP_17)
  16179. #include <any>
  16180. #include <string_view>
  16181. #endif
  16182. /*!
  16183. @brief namespace for Niels Lohmann
  16184. @see https://github.com/nlohmann
  16185. @since version 1.0.0
  16186. */
  16187. namespace nlohmann
  16188. {
  16189. /*!
  16190. @brief a class to store JSON values
  16191. @internal
  16192. @invariant The member variables @a m_value and @a m_type have the following
  16193. relationship:
  16194. - If `m_type == value_t::object`, then `m_value.object != nullptr`.
  16195. - If `m_type == value_t::array`, then `m_value.array != nullptr`.
  16196. - If `m_type == value_t::string`, then `m_value.string != nullptr`.
  16197. The invariants are checked by member function assert_invariant().
  16198. @note ObjectType trick from https://stackoverflow.com/a/9860911
  16199. @endinternal
  16200. @since version 1.0.0
  16201. @nosubgrouping
  16202. */
  16203. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  16204. class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions)
  16205. {
  16206. private:
  16207. template<detail::value_t> friend struct detail::external_constructor;
  16208. template<typename>
  16209. friend class ::nlohmann::json_pointer;
  16210. // can be restored when json_pointer backwards compatibility is removed
  16211. // friend ::nlohmann::json_pointer<StringType>;
  16212. template<typename BasicJsonType, typename InputType>
  16213. friend class ::nlohmann::detail::parser;
  16214. friend ::nlohmann::detail::serializer<basic_json>;
  16215. template<typename BasicJsonType>
  16216. friend class ::nlohmann::detail::iter_impl;
  16217. template<typename BasicJsonType, typename CharType>
  16218. friend class ::nlohmann::detail::binary_writer;
  16219. template<typename BasicJsonType, typename InputType, typename SAX>
  16220. friend class ::nlohmann::detail::binary_reader;
  16221. template<typename BasicJsonType>
  16222. friend class ::nlohmann::detail::json_sax_dom_parser;
  16223. template<typename BasicJsonType>
  16224. friend class ::nlohmann::detail::json_sax_dom_callback_parser;
  16225. friend class ::nlohmann::detail::exception;
  16226. /// workaround type for MSVC
  16227. using basic_json_t = NLOHMANN_BASIC_JSON_TPL;
  16228. JSON_PRIVATE_UNLESS_TESTED:
  16229. // convenience aliases for types residing in namespace detail;
  16230. using lexer = ::nlohmann::detail::lexer_base<basic_json>;
  16231. template<typename InputAdapterType>
  16232. static ::nlohmann::detail::parser<basic_json, InputAdapterType> parser(
  16233. InputAdapterType adapter,
  16234. detail::parser_callback_t<basic_json>cb = nullptr,
  16235. const bool allow_exceptions = true,
  16236. const bool ignore_comments = false
  16237. )
  16238. {
  16239. return ::nlohmann::detail::parser<basic_json, InputAdapterType>(std::move(adapter),
  16240. std::move(cb), allow_exceptions, ignore_comments);
  16241. }
  16242. private:
  16243. using primitive_iterator_t = ::nlohmann::detail::primitive_iterator_t;
  16244. template<typename BasicJsonType>
  16245. using internal_iterator = ::nlohmann::detail::internal_iterator<BasicJsonType>;
  16246. template<typename BasicJsonType>
  16247. using iter_impl = ::nlohmann::detail::iter_impl<BasicJsonType>;
  16248. template<typename Iterator>
  16249. using iteration_proxy = ::nlohmann::detail::iteration_proxy<Iterator>;
  16250. template<typename Base> using json_reverse_iterator = ::nlohmann::detail::json_reverse_iterator<Base>;
  16251. template<typename CharType>
  16252. using output_adapter_t = ::nlohmann::detail::output_adapter_t<CharType>;
  16253. template<typename InputType>
  16254. using binary_reader = ::nlohmann::detail::binary_reader<basic_json, InputType>;
  16255. template<typename CharType> using binary_writer = ::nlohmann::detail::binary_writer<basic_json, CharType>;
  16256. JSON_PRIVATE_UNLESS_TESTED:
  16257. using serializer = ::nlohmann::detail::serializer<basic_json>;
  16258. public:
  16259. using value_t = detail::value_t;
  16260. /// JSON Pointer, see @ref nlohmann::json_pointer
  16261. using json_pointer = ::nlohmann::json_pointer<StringType>;
  16262. template<typename T, typename SFINAE>
  16263. using json_serializer = JSONSerializer<T, SFINAE>;
  16264. /// how to treat decoding errors
  16265. using error_handler_t = detail::error_handler_t;
  16266. /// how to treat CBOR tags
  16267. using cbor_tag_handler_t = detail::cbor_tag_handler_t;
  16268. /// helper type for initializer lists of basic_json values
  16269. using initializer_list_t = std::initializer_list<detail::json_ref<basic_json>>;
  16270. using input_format_t = detail::input_format_t;
  16271. /// SAX interface type, see @ref nlohmann::json_sax
  16272. using json_sax_t = json_sax<basic_json>;
  16273. ////////////////
  16274. // exceptions //
  16275. ////////////////
  16276. /// @name exceptions
  16277. /// Classes to implement user-defined exceptions.
  16278. /// @{
  16279. using exception = detail::exception;
  16280. using parse_error = detail::parse_error;
  16281. using invalid_iterator = detail::invalid_iterator;
  16282. using type_error = detail::type_error;
  16283. using out_of_range = detail::out_of_range;
  16284. using other_error = detail::other_error;
  16285. /// @}
  16286. /////////////////////
  16287. // container types //
  16288. /////////////////////
  16289. /// @name container types
  16290. /// The canonic container types to use @ref basic_json like any other STL
  16291. /// container.
  16292. /// @{
  16293. /// the type of elements in a basic_json container
  16294. using value_type = basic_json;
  16295. /// the type of an element reference
  16296. using reference = value_type&;
  16297. /// the type of an element const reference
  16298. using const_reference = const value_type&;
  16299. /// a type to represent differences between iterators
  16300. using difference_type = std::ptrdiff_t;
  16301. /// a type to represent container sizes
  16302. using size_type = std::size_t;
  16303. /// the allocator type
  16304. using allocator_type = AllocatorType<basic_json>;
  16305. /// the type of an element pointer
  16306. using pointer = typename std::allocator_traits<allocator_type>::pointer;
  16307. /// the type of an element const pointer
  16308. using const_pointer = typename std::allocator_traits<allocator_type>::const_pointer;
  16309. /// an iterator for a basic_json container
  16310. using iterator = iter_impl<basic_json>;
  16311. /// a const iterator for a basic_json container
  16312. using const_iterator = iter_impl<const basic_json>;
  16313. /// a reverse iterator for a basic_json container
  16314. using reverse_iterator = json_reverse_iterator<typename basic_json::iterator>;
  16315. /// a const reverse iterator for a basic_json container
  16316. using const_reverse_iterator = json_reverse_iterator<typename basic_json::const_iterator>;
  16317. /// @}
  16318. /// @brief returns the allocator associated with the container
  16319. /// @sa https://json.nlohmann.me/api/basic_json/get_allocator/
  16320. static allocator_type get_allocator()
  16321. {
  16322. return allocator_type();
  16323. }
  16324. /// @brief returns version information on the library
  16325. /// @sa https://json.nlohmann.me/api/basic_json/meta/
  16326. JSON_HEDLEY_WARN_UNUSED_RESULT
  16327. static basic_json meta()
  16328. {
  16329. basic_json result;
  16330. result["copyright"] = "(C) 2013-2022 Niels Lohmann";
  16331. result["name"] = "JSON for Modern C++";
  16332. result["url"] = "https://github.com/nlohmann/json";
  16333. result["version"]["string"] =
  16334. detail::concat(std::to_string(NLOHMANN_JSON_VERSION_MAJOR), '.',
  16335. std::to_string(NLOHMANN_JSON_VERSION_MINOR), '.',
  16336. std::to_string(NLOHMANN_JSON_VERSION_PATCH));
  16337. result["version"]["major"] = NLOHMANN_JSON_VERSION_MAJOR;
  16338. result["version"]["minor"] = NLOHMANN_JSON_VERSION_MINOR;
  16339. result["version"]["patch"] = NLOHMANN_JSON_VERSION_PATCH;
  16340. #ifdef _WIN32
  16341. result["platform"] = "win32";
  16342. #elif defined __linux__
  16343. result["platform"] = "linux";
  16344. #elif defined __APPLE__
  16345. result["platform"] = "apple";
  16346. #elif defined __unix__
  16347. result["platform"] = "unix";
  16348. #else
  16349. result["platform"] = "unknown";
  16350. #endif
  16351. #if defined(__ICC) || defined(__INTEL_COMPILER)
  16352. result["compiler"] = {{"family", "icc"}, {"version", __INTEL_COMPILER}};
  16353. #elif defined(__clang__)
  16354. result["compiler"] = {{"family", "clang"}, {"version", __clang_version__}};
  16355. #elif defined(__GNUC__) || defined(__GNUG__)
  16356. result["compiler"] = {{"family", "gcc"}, {"version", detail::concat(
  16357. std::to_string(__GNUC__), '.',
  16358. std::to_string(__GNUC_MINOR__), '.',
  16359. std::to_string(__GNUC_PATCHLEVEL__))
  16360. }
  16361. };
  16362. #elif defined(__HP_cc) || defined(__HP_aCC)
  16363. result["compiler"] = "hp"
  16364. #elif defined(__IBMCPP__)
  16365. result["compiler"] = {{"family", "ilecpp"}, {"version", __IBMCPP__}};
  16366. #elif defined(_MSC_VER)
  16367. result["compiler"] = {{"family", "msvc"}, {"version", _MSC_VER}};
  16368. #elif defined(__PGI)
  16369. result["compiler"] = {{"family", "pgcpp"}, {"version", __PGI}};
  16370. #elif defined(__SUNPRO_CC)
  16371. result["compiler"] = {{"family", "sunpro"}, {"version", __SUNPRO_CC}};
  16372. #else
  16373. result["compiler"] = {{"family", "unknown"}, {"version", "unknown"}};
  16374. #endif
  16375. #if defined(_MSVC_LANG)
  16376. result["compiler"]["c++"] = std::to_string(_MSVC_LANG);
  16377. #elif defined(__cplusplus)
  16378. result["compiler"]["c++"] = std::to_string(__cplusplus);
  16379. #else
  16380. result["compiler"]["c++"] = "unknown";
  16381. #endif
  16382. return result;
  16383. }
  16384. ///////////////////////////
  16385. // JSON value data types //
  16386. ///////////////////////////
  16387. /// @name JSON value data types
  16388. /// The data types to store a JSON value. These types are derived from
  16389. /// the template arguments passed to class @ref basic_json.
  16390. /// @{
  16391. /// @brief default object key comparator type
  16392. /// The actual object key comparator type (@ref object_comparator_t) may be
  16393. /// different.
  16394. /// @sa https://json.nlohmann.me/api/basic_json/default_object_comparator_t/
  16395. #if defined(JSON_HAS_CPP_14)
  16396. // use of transparent comparator avoids unnecessary repeated construction of temporaries
  16397. // in functions involving lookup by key with types other than object_t::key_type (aka. StringType)
  16398. using default_object_comparator_t = std::less<>;
  16399. #else
  16400. using default_object_comparator_t = std::less<StringType>;
  16401. #endif
  16402. /// @brief a type for an object
  16403. /// @sa https://json.nlohmann.me/api/basic_json/object_t/
  16404. using object_t = ObjectType<StringType,
  16405. basic_json,
  16406. default_object_comparator_t,
  16407. AllocatorType<std::pair<const StringType,
  16408. basic_json>>>;
  16409. /// @brief a type for an array
  16410. /// @sa https://json.nlohmann.me/api/basic_json/array_t/
  16411. using array_t = ArrayType<basic_json, AllocatorType<basic_json>>;
  16412. /// @brief a type for a string
  16413. /// @sa https://json.nlohmann.me/api/basic_json/string_t/
  16414. using string_t = StringType;
  16415. /// @brief a type for a boolean
  16416. /// @sa https://json.nlohmann.me/api/basic_json/boolean_t/
  16417. using boolean_t = BooleanType;
  16418. /// @brief a type for a number (integer)
  16419. /// @sa https://json.nlohmann.me/api/basic_json/number_integer_t/
  16420. using number_integer_t = NumberIntegerType;
  16421. /// @brief a type for a number (unsigned)
  16422. /// @sa https://json.nlohmann.me/api/basic_json/number_unsigned_t/
  16423. using number_unsigned_t = NumberUnsignedType;
  16424. /// @brief a type for a number (floating-point)
  16425. /// @sa https://json.nlohmann.me/api/basic_json/number_float_t/
  16426. using number_float_t = NumberFloatType;
  16427. /// @brief a type for a packed binary type
  16428. /// @sa https://json.nlohmann.me/api/basic_json/binary_t/
  16429. using binary_t = nlohmann::byte_container_with_subtype<BinaryType>;
  16430. /// @brief object key comparator type
  16431. /// @sa https://json.nlohmann.me/api/basic_json/object_comparator_t/
  16432. using object_comparator_t = detail::actual_object_comparator_t<basic_json>;
  16433. /// @}
  16434. private:
  16435. /// helper for exception-safe object creation
  16436. template<typename T, typename... Args>
  16437. JSON_HEDLEY_RETURNS_NON_NULL
  16438. static T* create(Args&& ... args)
  16439. {
  16440. AllocatorType<T> alloc;
  16441. using AllocatorTraits = std::allocator_traits<AllocatorType<T>>;
  16442. auto deleter = [&](T * obj)
  16443. {
  16444. AllocatorTraits::deallocate(alloc, obj, 1);
  16445. };
  16446. std::unique_ptr<T, decltype(deleter)> obj(AllocatorTraits::allocate(alloc, 1), deleter);
  16447. AllocatorTraits::construct(alloc, obj.get(), std::forward<Args>(args)...);
  16448. JSON_ASSERT(obj != nullptr);
  16449. return obj.release();
  16450. }
  16451. ////////////////////////
  16452. // JSON value storage //
  16453. ////////////////////////
  16454. JSON_PRIVATE_UNLESS_TESTED:
  16455. /*!
  16456. @brief a JSON value
  16457. The actual storage for a JSON value of the @ref basic_json class. This
  16458. union combines the different storage types for the JSON value types
  16459. defined in @ref value_t.
  16460. JSON type | value_t type | used type
  16461. --------- | --------------- | ------------------------
  16462. object | object | pointer to @ref object_t
  16463. array | array | pointer to @ref array_t
  16464. string | string | pointer to @ref string_t
  16465. boolean | boolean | @ref boolean_t
  16466. number | number_integer | @ref number_integer_t
  16467. number | number_unsigned | @ref number_unsigned_t
  16468. number | number_float | @ref number_float_t
  16469. binary | binary | pointer to @ref binary_t
  16470. null | null | *no value is stored*
  16471. @note Variable-length types (objects, arrays, and strings) are stored as
  16472. pointers. The size of the union should not exceed 64 bits if the default
  16473. value types are used.
  16474. @since version 1.0.0
  16475. */
  16476. union json_value
  16477. {
  16478. /// object (stored with pointer to save storage)
  16479. object_t* object;
  16480. /// array (stored with pointer to save storage)
  16481. array_t* array;
  16482. /// string (stored with pointer to save storage)
  16483. string_t* string;
  16484. /// binary (stored with pointer to save storage)
  16485. binary_t* binary;
  16486. /// boolean
  16487. boolean_t boolean;
  16488. /// number (integer)
  16489. number_integer_t number_integer;
  16490. /// number (unsigned integer)
  16491. number_unsigned_t number_unsigned;
  16492. /// number (floating-point)
  16493. number_float_t number_float;
  16494. /// default constructor (for null values)
  16495. json_value() = default;
  16496. /// constructor for booleans
  16497. json_value(boolean_t v) noexcept : boolean(v) {}
  16498. /// constructor for numbers (integer)
  16499. json_value(number_integer_t v) noexcept : number_integer(v) {}
  16500. /// constructor for numbers (unsigned)
  16501. json_value(number_unsigned_t v) noexcept : number_unsigned(v) {}
  16502. /// constructor for numbers (floating-point)
  16503. json_value(number_float_t v) noexcept : number_float(v) {}
  16504. /// constructor for empty values of a given type
  16505. json_value(value_t t)
  16506. {
  16507. switch (t)
  16508. {
  16509. case value_t::object:
  16510. {
  16511. object = create<object_t>();
  16512. break;
  16513. }
  16514. case value_t::array:
  16515. {
  16516. array = create<array_t>();
  16517. break;
  16518. }
  16519. case value_t::string:
  16520. {
  16521. string = create<string_t>("");
  16522. break;
  16523. }
  16524. case value_t::binary:
  16525. {
  16526. binary = create<binary_t>();
  16527. break;
  16528. }
  16529. case value_t::boolean:
  16530. {
  16531. boolean = static_cast<boolean_t>(false);
  16532. break;
  16533. }
  16534. case value_t::number_integer:
  16535. {
  16536. number_integer = static_cast<number_integer_t>(0);
  16537. break;
  16538. }
  16539. case value_t::number_unsigned:
  16540. {
  16541. number_unsigned = static_cast<number_unsigned_t>(0);
  16542. break;
  16543. }
  16544. case value_t::number_float:
  16545. {
  16546. number_float = static_cast<number_float_t>(0.0);
  16547. break;
  16548. }
  16549. case value_t::null:
  16550. {
  16551. object = nullptr; // silence warning, see #821
  16552. break;
  16553. }
  16554. case value_t::discarded:
  16555. default:
  16556. {
  16557. object = nullptr; // silence warning, see #821
  16558. if (JSON_HEDLEY_UNLIKELY(t == value_t::null))
  16559. {
  16560. JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.10.5", nullptr)); // LCOV_EXCL_LINE
  16561. }
  16562. break;
  16563. }
  16564. }
  16565. }
  16566. /// constructor for strings
  16567. json_value(const string_t& value) : string(create<string_t>(value)) {}
  16568. /// constructor for rvalue strings
  16569. json_value(string_t&& value) : string(create<string_t>(std::move(value))) {}
  16570. /// constructor for objects
  16571. json_value(const object_t& value) : object(create<object_t>(value)) {}
  16572. /// constructor for rvalue objects
  16573. json_value(object_t&& value) : object(create<object_t>(std::move(value))) {}
  16574. /// constructor for arrays
  16575. json_value(const array_t& value) : array(create<array_t>(value)) {}
  16576. /// constructor for rvalue arrays
  16577. json_value(array_t&& value) : array(create<array_t>(std::move(value))) {}
  16578. /// constructor for binary arrays
  16579. json_value(const typename binary_t::container_type& value) : binary(create<binary_t>(value)) {}
  16580. /// constructor for rvalue binary arrays
  16581. json_value(typename binary_t::container_type&& value) : binary(create<binary_t>(std::move(value))) {}
  16582. /// constructor for binary arrays (internal type)
  16583. json_value(const binary_t& value) : binary(create<binary_t>(value)) {}
  16584. /// constructor for rvalue binary arrays (internal type)
  16585. json_value(binary_t&& value) : binary(create<binary_t>(std::move(value))) {}
  16586. void destroy(value_t t)
  16587. {
  16588. if (t == value_t::array || t == value_t::object)
  16589. {
  16590. // flatten the current json_value to a heap-allocated stack
  16591. std::vector<basic_json> stack;
  16592. // move the top-level items to stack
  16593. if (t == value_t::array)
  16594. {
  16595. stack.reserve(array->size());
  16596. std::move(array->begin(), array->end(), std::back_inserter(stack));
  16597. }
  16598. else
  16599. {
  16600. stack.reserve(object->size());
  16601. for (auto&& it : *object)
  16602. {
  16603. stack.push_back(std::move(it.second));
  16604. }
  16605. }
  16606. while (!stack.empty())
  16607. {
  16608. // move the last item to local variable to be processed
  16609. basic_json current_item(std::move(stack.back()));
  16610. stack.pop_back();
  16611. // if current_item is array/object, move
  16612. // its children to the stack to be processed later
  16613. if (current_item.is_array())
  16614. {
  16615. std::move(current_item.m_value.array->begin(), current_item.m_value.array->end(), std::back_inserter(stack));
  16616. current_item.m_value.array->clear();
  16617. }
  16618. else if (current_item.is_object())
  16619. {
  16620. for (auto&& it : *current_item.m_value.object)
  16621. {
  16622. stack.push_back(std::move(it.second));
  16623. }
  16624. current_item.m_value.object->clear();
  16625. }
  16626. // it's now safe that current_item get destructed
  16627. // since it doesn't have any children
  16628. }
  16629. }
  16630. switch (t)
  16631. {
  16632. case value_t::object:
  16633. {
  16634. AllocatorType<object_t> alloc;
  16635. std::allocator_traits<decltype(alloc)>::destroy(alloc, object);
  16636. std::allocator_traits<decltype(alloc)>::deallocate(alloc, object, 1);
  16637. break;
  16638. }
  16639. case value_t::array:
  16640. {
  16641. AllocatorType<array_t> alloc;
  16642. std::allocator_traits<decltype(alloc)>::destroy(alloc, array);
  16643. std::allocator_traits<decltype(alloc)>::deallocate(alloc, array, 1);
  16644. break;
  16645. }
  16646. case value_t::string:
  16647. {
  16648. AllocatorType<string_t> alloc;
  16649. std::allocator_traits<decltype(alloc)>::destroy(alloc, string);
  16650. std::allocator_traits<decltype(alloc)>::deallocate(alloc, string, 1);
  16651. break;
  16652. }
  16653. case value_t::binary:
  16654. {
  16655. AllocatorType<binary_t> alloc;
  16656. std::allocator_traits<decltype(alloc)>::destroy(alloc, binary);
  16657. std::allocator_traits<decltype(alloc)>::deallocate(alloc, binary, 1);
  16658. break;
  16659. }
  16660. case value_t::null:
  16661. case value_t::boolean:
  16662. case value_t::number_integer:
  16663. case value_t::number_unsigned:
  16664. case value_t::number_float:
  16665. case value_t::discarded:
  16666. default:
  16667. {
  16668. break;
  16669. }
  16670. }
  16671. }
  16672. };
  16673. private:
  16674. /*!
  16675. @brief checks the class invariants
  16676. This function asserts the class invariants. It needs to be called at the
  16677. end of every constructor to make sure that created objects respect the
  16678. invariant. Furthermore, it has to be called each time the type of a JSON
  16679. value is changed, because the invariant expresses a relationship between
  16680. @a m_type and @a m_value.
  16681. Furthermore, the parent relation is checked for arrays and objects: If
  16682. @a check_parents true and the value is an array or object, then the
  16683. container's elements must have the current value as parent.
  16684. @param[in] check_parents whether the parent relation should be checked.
  16685. The value is true by default and should only be set to false
  16686. during destruction of objects when the invariant does not
  16687. need to hold.
  16688. */
  16689. void assert_invariant(bool check_parents = true) const noexcept
  16690. {
  16691. JSON_ASSERT(m_type != value_t::object || m_value.object != nullptr);
  16692. JSON_ASSERT(m_type != value_t::array || m_value.array != nullptr);
  16693. JSON_ASSERT(m_type != value_t::string || m_value.string != nullptr);
  16694. JSON_ASSERT(m_type != value_t::binary || m_value.binary != nullptr);
  16695. #if JSON_DIAGNOSTICS
  16696. JSON_TRY
  16697. {
  16698. // cppcheck-suppress assertWithSideEffect
  16699. JSON_ASSERT(!check_parents || !is_structured() || std::all_of(begin(), end(), [this](const basic_json & j)
  16700. {
  16701. return j.m_parent == this;
  16702. }));
  16703. }
  16704. JSON_CATCH(...) {} // LCOV_EXCL_LINE
  16705. #endif
  16706. static_cast<void>(check_parents);
  16707. }
  16708. void set_parents()
  16709. {
  16710. #if JSON_DIAGNOSTICS
  16711. switch (m_type)
  16712. {
  16713. case value_t::array:
  16714. {
  16715. for (auto& element : *m_value.array)
  16716. {
  16717. element.m_parent = this;
  16718. }
  16719. break;
  16720. }
  16721. case value_t::object:
  16722. {
  16723. for (auto& element : *m_value.object)
  16724. {
  16725. element.second.m_parent = this;
  16726. }
  16727. break;
  16728. }
  16729. case value_t::null:
  16730. case value_t::string:
  16731. case value_t::boolean:
  16732. case value_t::number_integer:
  16733. case value_t::number_unsigned:
  16734. case value_t::number_float:
  16735. case value_t::binary:
  16736. case value_t::discarded:
  16737. default:
  16738. break;
  16739. }
  16740. #endif
  16741. }
  16742. iterator set_parents(iterator it, typename iterator::difference_type count_set_parents)
  16743. {
  16744. #if JSON_DIAGNOSTICS
  16745. for (typename iterator::difference_type i = 0; i < count_set_parents; ++i)
  16746. {
  16747. (it + i)->m_parent = this;
  16748. }
  16749. #else
  16750. static_cast<void>(count_set_parents);
  16751. #endif
  16752. return it;
  16753. }
  16754. reference set_parent(reference j, std::size_t old_capacity = static_cast<std::size_t>(-1))
  16755. {
  16756. #if JSON_DIAGNOSTICS
  16757. if (old_capacity != static_cast<std::size_t>(-1))
  16758. {
  16759. // see https://github.com/nlohmann/json/issues/2838
  16760. JSON_ASSERT(type() == value_t::array);
  16761. if (JSON_HEDLEY_UNLIKELY(m_value.array->capacity() != old_capacity))
  16762. {
  16763. // capacity has changed: update all parents
  16764. set_parents();
  16765. return j;
  16766. }
  16767. }
  16768. // ordered_json uses a vector internally, so pointers could have
  16769. // been invalidated; see https://github.com/nlohmann/json/issues/2962
  16770. #ifdef JSON_HEDLEY_MSVC_VERSION
  16771. #pragma warning(push )
  16772. #pragma warning(disable : 4127) // ignore warning to replace if with if constexpr
  16773. #endif
  16774. if (detail::is_ordered_map<object_t>::value)
  16775. {
  16776. set_parents();
  16777. return j;
  16778. }
  16779. #ifdef JSON_HEDLEY_MSVC_VERSION
  16780. #pragma warning( pop )
  16781. #endif
  16782. j.m_parent = this;
  16783. #else
  16784. static_cast<void>(j);
  16785. static_cast<void>(old_capacity);
  16786. #endif
  16787. return j;
  16788. }
  16789. public:
  16790. //////////////////////////
  16791. // JSON parser callback //
  16792. //////////////////////////
  16793. /// @brief parser event types
  16794. /// @sa https://json.nlohmann.me/api/basic_json/parse_event_t/
  16795. using parse_event_t = detail::parse_event_t;
  16796. /// @brief per-element parser callback type
  16797. /// @sa https://json.nlohmann.me/api/basic_json/parser_callback_t/
  16798. using parser_callback_t = detail::parser_callback_t<basic_json>;
  16799. //////////////////
  16800. // constructors //
  16801. //////////////////
  16802. /// @name constructors and destructors
  16803. /// Constructors of class @ref basic_json, copy/move constructor, copy
  16804. /// assignment, static functions creating objects, and the destructor.
  16805. /// @{
  16806. /// @brief create an empty value with a given type
  16807. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  16808. basic_json(const value_t v)
  16809. : m_type(v), m_value(v)
  16810. {
  16811. assert_invariant();
  16812. }
  16813. /// @brief create a null object
  16814. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  16815. basic_json(std::nullptr_t = nullptr) noexcept // NOLINT(bugprone-exception-escape)
  16816. : basic_json(value_t::null)
  16817. {
  16818. assert_invariant();
  16819. }
  16820. /// @brief create a JSON value from compatible types
  16821. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  16822. template < typename CompatibleType,
  16823. typename U = detail::uncvref_t<CompatibleType>,
  16824. detail::enable_if_t <
  16825. !detail::is_basic_json<U>::value && detail::is_compatible_type<basic_json_t, U>::value, int > = 0 >
  16826. basic_json(CompatibleType && val) noexcept(noexcept( // NOLINT(bugprone-forwarding-reference-overload,bugprone-exception-escape)
  16827. JSONSerializer<U>::to_json(std::declval<basic_json_t&>(),
  16828. std::forward<CompatibleType>(val))))
  16829. {
  16830. JSONSerializer<U>::to_json(*this, std::forward<CompatibleType>(val));
  16831. set_parents();
  16832. assert_invariant();
  16833. }
  16834. /// @brief create a JSON value from an existing one
  16835. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  16836. template < typename BasicJsonType,
  16837. detail::enable_if_t <
  16838. detail::is_basic_json<BasicJsonType>::value&& !std::is_same<basic_json, BasicJsonType>::value, int > = 0 >
  16839. basic_json(const BasicJsonType& val)
  16840. {
  16841. using other_boolean_t = typename BasicJsonType::boolean_t;
  16842. using other_number_float_t = typename BasicJsonType::number_float_t;
  16843. using other_number_integer_t = typename BasicJsonType::number_integer_t;
  16844. using other_number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  16845. using other_string_t = typename BasicJsonType::string_t;
  16846. using other_object_t = typename BasicJsonType::object_t;
  16847. using other_array_t = typename BasicJsonType::array_t;
  16848. using other_binary_t = typename BasicJsonType::binary_t;
  16849. switch (val.type())
  16850. {
  16851. case value_t::boolean:
  16852. JSONSerializer<other_boolean_t>::to_json(*this, val.template get<other_boolean_t>());
  16853. break;
  16854. case value_t::number_float:
  16855. JSONSerializer<other_number_float_t>::to_json(*this, val.template get<other_number_float_t>());
  16856. break;
  16857. case value_t::number_integer:
  16858. JSONSerializer<other_number_integer_t>::to_json(*this, val.template get<other_number_integer_t>());
  16859. break;
  16860. case value_t::number_unsigned:
  16861. JSONSerializer<other_number_unsigned_t>::to_json(*this, val.template get<other_number_unsigned_t>());
  16862. break;
  16863. case value_t::string:
  16864. JSONSerializer<other_string_t>::to_json(*this, val.template get_ref<const other_string_t&>());
  16865. break;
  16866. case value_t::object:
  16867. JSONSerializer<other_object_t>::to_json(*this, val.template get_ref<const other_object_t&>());
  16868. break;
  16869. case value_t::array:
  16870. JSONSerializer<other_array_t>::to_json(*this, val.template get_ref<const other_array_t&>());
  16871. break;
  16872. case value_t::binary:
  16873. JSONSerializer<other_binary_t>::to_json(*this, val.template get_ref<const other_binary_t&>());
  16874. break;
  16875. case value_t::null:
  16876. *this = nullptr;
  16877. break;
  16878. case value_t::discarded:
  16879. m_type = value_t::discarded;
  16880. break;
  16881. default: // LCOV_EXCL_LINE
  16882. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  16883. }
  16884. JSON_ASSERT(m_type == val.type());
  16885. set_parents();
  16886. assert_invariant();
  16887. }
  16888. /// @brief create a container (array or object) from an initializer list
  16889. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  16890. basic_json(initializer_list_t init,
  16891. bool type_deduction = true,
  16892. value_t manual_type = value_t::array)
  16893. {
  16894. // check if each element is an array with two elements whose first
  16895. // element is a string
  16896. bool is_an_object = std::all_of(init.begin(), init.end(),
  16897. [](const detail::json_ref<basic_json>& element_ref)
  16898. {
  16899. return element_ref->is_array() && element_ref->size() == 2 && (*element_ref)[0].is_string();
  16900. });
  16901. // adjust type if type deduction is not wanted
  16902. if (!type_deduction)
  16903. {
  16904. // if array is wanted, do not create an object though possible
  16905. if (manual_type == value_t::array)
  16906. {
  16907. is_an_object = false;
  16908. }
  16909. // if object is wanted but impossible, throw an exception
  16910. if (JSON_HEDLEY_UNLIKELY(manual_type == value_t::object && !is_an_object))
  16911. {
  16912. JSON_THROW(type_error::create(301, "cannot create object from initializer list", nullptr));
  16913. }
  16914. }
  16915. if (is_an_object)
  16916. {
  16917. // the initializer list is a list of pairs -> create object
  16918. m_type = value_t::object;
  16919. m_value = value_t::object;
  16920. for (auto& element_ref : init)
  16921. {
  16922. auto element = element_ref.moved_or_copied();
  16923. m_value.object->emplace(
  16924. std::move(*((*element.m_value.array)[0].m_value.string)),
  16925. std::move((*element.m_value.array)[1]));
  16926. }
  16927. }
  16928. else
  16929. {
  16930. // the initializer list describes an array -> create array
  16931. m_type = value_t::array;
  16932. m_value.array = create<array_t>(init.begin(), init.end());
  16933. }
  16934. set_parents();
  16935. assert_invariant();
  16936. }
  16937. /// @brief explicitly create a binary array (without subtype)
  16938. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  16939. JSON_HEDLEY_WARN_UNUSED_RESULT
  16940. static basic_json binary(const typename binary_t::container_type& init)
  16941. {
  16942. auto res = basic_json();
  16943. res.m_type = value_t::binary;
  16944. res.m_value = init;
  16945. return res;
  16946. }
  16947. /// @brief explicitly create a binary array (with subtype)
  16948. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  16949. JSON_HEDLEY_WARN_UNUSED_RESULT
  16950. static basic_json binary(const typename binary_t::container_type& init, typename binary_t::subtype_type subtype)
  16951. {
  16952. auto res = basic_json();
  16953. res.m_type = value_t::binary;
  16954. res.m_value = binary_t(init, subtype);
  16955. return res;
  16956. }
  16957. /// @brief explicitly create a binary array
  16958. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  16959. JSON_HEDLEY_WARN_UNUSED_RESULT
  16960. static basic_json binary(typename binary_t::container_type&& init)
  16961. {
  16962. auto res = basic_json();
  16963. res.m_type = value_t::binary;
  16964. res.m_value = std::move(init);
  16965. return res;
  16966. }
  16967. /// @brief explicitly create a binary array (with subtype)
  16968. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  16969. JSON_HEDLEY_WARN_UNUSED_RESULT
  16970. static basic_json binary(typename binary_t::container_type&& init, typename binary_t::subtype_type subtype)
  16971. {
  16972. auto res = basic_json();
  16973. res.m_type = value_t::binary;
  16974. res.m_value = binary_t(std::move(init), subtype);
  16975. return res;
  16976. }
  16977. /// @brief explicitly create an array from an initializer list
  16978. /// @sa https://json.nlohmann.me/api/basic_json/array/
  16979. JSON_HEDLEY_WARN_UNUSED_RESULT
  16980. static basic_json array(initializer_list_t init = {})
  16981. {
  16982. return basic_json(init, false, value_t::array);
  16983. }
  16984. /// @brief explicitly create an object from an initializer list
  16985. /// @sa https://json.nlohmann.me/api/basic_json/object/
  16986. JSON_HEDLEY_WARN_UNUSED_RESULT
  16987. static basic_json object(initializer_list_t init = {})
  16988. {
  16989. return basic_json(init, false, value_t::object);
  16990. }
  16991. /// @brief construct an array with count copies of given value
  16992. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  16993. basic_json(size_type cnt, const basic_json& val)
  16994. : m_type(value_t::array)
  16995. {
  16996. m_value.array = create<array_t>(cnt, val);
  16997. set_parents();
  16998. assert_invariant();
  16999. }
  17000. /// @brief construct a JSON container given an iterator range
  17001. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17002. template < class InputIT, typename std::enable_if <
  17003. std::is_same<InputIT, typename basic_json_t::iterator>::value ||
  17004. std::is_same<InputIT, typename basic_json_t::const_iterator>::value, int >::type = 0 >
  17005. basic_json(InputIT first, InputIT last)
  17006. {
  17007. JSON_ASSERT(first.m_object != nullptr);
  17008. JSON_ASSERT(last.m_object != nullptr);
  17009. // make sure iterator fits the current value
  17010. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  17011. {
  17012. JSON_THROW(invalid_iterator::create(201, "iterators are not compatible", nullptr));
  17013. }
  17014. // copy type from first iterator
  17015. m_type = first.m_object->m_type;
  17016. // check if iterator range is complete for primitive values
  17017. switch (m_type)
  17018. {
  17019. case value_t::boolean:
  17020. case value_t::number_float:
  17021. case value_t::number_integer:
  17022. case value_t::number_unsigned:
  17023. case value_t::string:
  17024. {
  17025. if (JSON_HEDLEY_UNLIKELY(!first.m_it.primitive_iterator.is_begin()
  17026. || !last.m_it.primitive_iterator.is_end()))
  17027. {
  17028. JSON_THROW(invalid_iterator::create(204, "iterators out of range", first.m_object));
  17029. }
  17030. break;
  17031. }
  17032. case value_t::null:
  17033. case value_t::object:
  17034. case value_t::array:
  17035. case value_t::binary:
  17036. case value_t::discarded:
  17037. default:
  17038. break;
  17039. }
  17040. switch (m_type)
  17041. {
  17042. case value_t::number_integer:
  17043. {
  17044. m_value.number_integer = first.m_object->m_value.number_integer;
  17045. break;
  17046. }
  17047. case value_t::number_unsigned:
  17048. {
  17049. m_value.number_unsigned = first.m_object->m_value.number_unsigned;
  17050. break;
  17051. }
  17052. case value_t::number_float:
  17053. {
  17054. m_value.number_float = first.m_object->m_value.number_float;
  17055. break;
  17056. }
  17057. case value_t::boolean:
  17058. {
  17059. m_value.boolean = first.m_object->m_value.boolean;
  17060. break;
  17061. }
  17062. case value_t::string:
  17063. {
  17064. m_value = *first.m_object->m_value.string;
  17065. break;
  17066. }
  17067. case value_t::object:
  17068. {
  17069. m_value.object = create<object_t>(first.m_it.object_iterator,
  17070. last.m_it.object_iterator);
  17071. break;
  17072. }
  17073. case value_t::array:
  17074. {
  17075. m_value.array = create<array_t>(first.m_it.array_iterator,
  17076. last.m_it.array_iterator);
  17077. break;
  17078. }
  17079. case value_t::binary:
  17080. {
  17081. m_value = *first.m_object->m_value.binary;
  17082. break;
  17083. }
  17084. case value_t::null:
  17085. case value_t::discarded:
  17086. default:
  17087. JSON_THROW(invalid_iterator::create(206, detail::concat("cannot construct with iterators from ", first.m_object->type_name()), first.m_object));
  17088. }
  17089. set_parents();
  17090. assert_invariant();
  17091. }
  17092. ///////////////////////////////////////
  17093. // other constructors and destructor //
  17094. ///////////////////////////////////////
  17095. template<typename JsonRef,
  17096. detail::enable_if_t<detail::conjunction<detail::is_json_ref<JsonRef>,
  17097. std::is_same<typename JsonRef::value_type, basic_json>>::value, int> = 0 >
  17098. basic_json(const JsonRef& ref) : basic_json(ref.moved_or_copied()) {}
  17099. /// @brief copy constructor
  17100. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17101. basic_json(const basic_json& other)
  17102. : m_type(other.m_type)
  17103. {
  17104. // check of passed value is valid
  17105. other.assert_invariant();
  17106. switch (m_type)
  17107. {
  17108. case value_t::object:
  17109. {
  17110. m_value = *other.m_value.object;
  17111. break;
  17112. }
  17113. case value_t::array:
  17114. {
  17115. m_value = *other.m_value.array;
  17116. break;
  17117. }
  17118. case value_t::string:
  17119. {
  17120. m_value = *other.m_value.string;
  17121. break;
  17122. }
  17123. case value_t::boolean:
  17124. {
  17125. m_value = other.m_value.boolean;
  17126. break;
  17127. }
  17128. case value_t::number_integer:
  17129. {
  17130. m_value = other.m_value.number_integer;
  17131. break;
  17132. }
  17133. case value_t::number_unsigned:
  17134. {
  17135. m_value = other.m_value.number_unsigned;
  17136. break;
  17137. }
  17138. case value_t::number_float:
  17139. {
  17140. m_value = other.m_value.number_float;
  17141. break;
  17142. }
  17143. case value_t::binary:
  17144. {
  17145. m_value = *other.m_value.binary;
  17146. break;
  17147. }
  17148. case value_t::null:
  17149. case value_t::discarded:
  17150. default:
  17151. break;
  17152. }
  17153. set_parents();
  17154. assert_invariant();
  17155. }
  17156. /// @brief move constructor
  17157. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  17158. basic_json(basic_json&& other) noexcept
  17159. : m_type(std::move(other.m_type)),
  17160. m_value(std::move(other.m_value))
  17161. {
  17162. // check that passed value is valid
  17163. other.assert_invariant(false);
  17164. // invalidate payload
  17165. other.m_type = value_t::null;
  17166. other.m_value = {};
  17167. set_parents();
  17168. assert_invariant();
  17169. }
  17170. /// @brief copy assignment
  17171. /// @sa https://json.nlohmann.me/api/basic_json/operator=/
  17172. basic_json& operator=(basic_json other) noexcept (
  17173. std::is_nothrow_move_constructible<value_t>::value&&
  17174. std::is_nothrow_move_assignable<value_t>::value&&
  17175. std::is_nothrow_move_constructible<json_value>::value&&
  17176. std::is_nothrow_move_assignable<json_value>::value
  17177. )
  17178. {
  17179. // check that passed value is valid
  17180. other.assert_invariant();
  17181. using std::swap;
  17182. swap(m_type, other.m_type);
  17183. swap(m_value, other.m_value);
  17184. set_parents();
  17185. assert_invariant();
  17186. return *this;
  17187. }
  17188. /// @brief destructor
  17189. /// @sa https://json.nlohmann.me/api/basic_json/~basic_json/
  17190. ~basic_json() noexcept
  17191. {
  17192. assert_invariant(false);
  17193. m_value.destroy(m_type);
  17194. }
  17195. /// @}
  17196. public:
  17197. ///////////////////////
  17198. // object inspection //
  17199. ///////////////////////
  17200. /// @name object inspection
  17201. /// Functions to inspect the type of a JSON value.
  17202. /// @{
  17203. /// @brief serialization
  17204. /// @sa https://json.nlohmann.me/api/basic_json/dump/
  17205. string_t dump(const int indent = -1,
  17206. const char indent_char = ' ',
  17207. const bool ensure_ascii = false,
  17208. const error_handler_t error_handler = error_handler_t::strict) const
  17209. {
  17210. string_t result;
  17211. serializer s(detail::output_adapter<char, string_t>(result), indent_char, error_handler);
  17212. if (indent >= 0)
  17213. {
  17214. s.dump(*this, true, ensure_ascii, static_cast<unsigned int>(indent));
  17215. }
  17216. else
  17217. {
  17218. s.dump(*this, false, ensure_ascii, 0);
  17219. }
  17220. return result;
  17221. }
  17222. /// @brief return the type of the JSON value (explicit)
  17223. /// @sa https://json.nlohmann.me/api/basic_json/type/
  17224. constexpr value_t type() const noexcept
  17225. {
  17226. return m_type;
  17227. }
  17228. /// @brief return whether type is primitive
  17229. /// @sa https://json.nlohmann.me/api/basic_json/is_primitive/
  17230. constexpr bool is_primitive() const noexcept
  17231. {
  17232. return is_null() || is_string() || is_boolean() || is_number() || is_binary();
  17233. }
  17234. /// @brief return whether type is structured
  17235. /// @sa https://json.nlohmann.me/api/basic_json/is_structured/
  17236. constexpr bool is_structured() const noexcept
  17237. {
  17238. return is_array() || is_object();
  17239. }
  17240. /// @brief return whether value is null
  17241. /// @sa https://json.nlohmann.me/api/basic_json/is_null/
  17242. constexpr bool is_null() const noexcept
  17243. {
  17244. return m_type == value_t::null;
  17245. }
  17246. /// @brief return whether value is a boolean
  17247. /// @sa https://json.nlohmann.me/api/basic_json/is_boolean/
  17248. constexpr bool is_boolean() const noexcept
  17249. {
  17250. return m_type == value_t::boolean;
  17251. }
  17252. /// @brief return whether value is a number
  17253. /// @sa https://json.nlohmann.me/api/basic_json/is_number/
  17254. constexpr bool is_number() const noexcept
  17255. {
  17256. return is_number_integer() || is_number_float();
  17257. }
  17258. /// @brief return whether value is an integer number
  17259. /// @sa https://json.nlohmann.me/api/basic_json/is_number_integer/
  17260. constexpr bool is_number_integer() const noexcept
  17261. {
  17262. return m_type == value_t::number_integer || m_type == value_t::number_unsigned;
  17263. }
  17264. /// @brief return whether value is an unsigned integer number
  17265. /// @sa https://json.nlohmann.me/api/basic_json/is_number_unsigned/
  17266. constexpr bool is_number_unsigned() const noexcept
  17267. {
  17268. return m_type == value_t::number_unsigned;
  17269. }
  17270. /// @brief return whether value is a floating-point number
  17271. /// @sa https://json.nlohmann.me/api/basic_json/is_number_float/
  17272. constexpr bool is_number_float() const noexcept
  17273. {
  17274. return m_type == value_t::number_float;
  17275. }
  17276. /// @brief return whether value is an object
  17277. /// @sa https://json.nlohmann.me/api/basic_json/is_object/
  17278. constexpr bool is_object() const noexcept
  17279. {
  17280. return m_type == value_t::object;
  17281. }
  17282. /// @brief return whether value is an array
  17283. /// @sa https://json.nlohmann.me/api/basic_json/is_array/
  17284. constexpr bool is_array() const noexcept
  17285. {
  17286. return m_type == value_t::array;
  17287. }
  17288. /// @brief return whether value is a string
  17289. /// @sa https://json.nlohmann.me/api/basic_json/is_string/
  17290. constexpr bool is_string() const noexcept
  17291. {
  17292. return m_type == value_t::string;
  17293. }
  17294. /// @brief return whether value is a binary array
  17295. /// @sa https://json.nlohmann.me/api/basic_json/is_binary/
  17296. constexpr bool is_binary() const noexcept
  17297. {
  17298. return m_type == value_t::binary;
  17299. }
  17300. /// @brief return whether value is discarded
  17301. /// @sa https://json.nlohmann.me/api/basic_json/is_discarded/
  17302. constexpr bool is_discarded() const noexcept
  17303. {
  17304. return m_type == value_t::discarded;
  17305. }
  17306. /// @brief return the type of the JSON value (implicit)
  17307. /// @sa https://json.nlohmann.me/api/basic_json/operator_value_t/
  17308. constexpr operator value_t() const noexcept
  17309. {
  17310. return m_type;
  17311. }
  17312. /// @}
  17313. private:
  17314. //////////////////
  17315. // value access //
  17316. //////////////////
  17317. /// get a boolean (explicit)
  17318. boolean_t get_impl(boolean_t* /*unused*/) const
  17319. {
  17320. if (JSON_HEDLEY_LIKELY(is_boolean()))
  17321. {
  17322. return m_value.boolean;
  17323. }
  17324. JSON_THROW(type_error::create(302, detail::concat("type must be boolean, but is ", type_name()), this));
  17325. }
  17326. /// get a pointer to the value (object)
  17327. object_t* get_impl_ptr(object_t* /*unused*/) noexcept
  17328. {
  17329. return is_object() ? m_value.object : nullptr;
  17330. }
  17331. /// get a pointer to the value (object)
  17332. constexpr const object_t* get_impl_ptr(const object_t* /*unused*/) const noexcept
  17333. {
  17334. return is_object() ? m_value.object : nullptr;
  17335. }
  17336. /// get a pointer to the value (array)
  17337. array_t* get_impl_ptr(array_t* /*unused*/) noexcept
  17338. {
  17339. return is_array() ? m_value.array : nullptr;
  17340. }
  17341. /// get a pointer to the value (array)
  17342. constexpr const array_t* get_impl_ptr(const array_t* /*unused*/) const noexcept
  17343. {
  17344. return is_array() ? m_value.array : nullptr;
  17345. }
  17346. /// get a pointer to the value (string)
  17347. string_t* get_impl_ptr(string_t* /*unused*/) noexcept
  17348. {
  17349. return is_string() ? m_value.string : nullptr;
  17350. }
  17351. /// get a pointer to the value (string)
  17352. constexpr const string_t* get_impl_ptr(const string_t* /*unused*/) const noexcept
  17353. {
  17354. return is_string() ? m_value.string : nullptr;
  17355. }
  17356. /// get a pointer to the value (boolean)
  17357. boolean_t* get_impl_ptr(boolean_t* /*unused*/) noexcept
  17358. {
  17359. return is_boolean() ? &m_value.boolean : nullptr;
  17360. }
  17361. /// get a pointer to the value (boolean)
  17362. constexpr const boolean_t* get_impl_ptr(const boolean_t* /*unused*/) const noexcept
  17363. {
  17364. return is_boolean() ? &m_value.boolean : nullptr;
  17365. }
  17366. /// get a pointer to the value (integer number)
  17367. number_integer_t* get_impl_ptr(number_integer_t* /*unused*/) noexcept
  17368. {
  17369. return is_number_integer() ? &m_value.number_integer : nullptr;
  17370. }
  17371. /// get a pointer to the value (integer number)
  17372. constexpr const number_integer_t* get_impl_ptr(const number_integer_t* /*unused*/) const noexcept
  17373. {
  17374. return is_number_integer() ? &m_value.number_integer : nullptr;
  17375. }
  17376. /// get a pointer to the value (unsigned number)
  17377. number_unsigned_t* get_impl_ptr(number_unsigned_t* /*unused*/) noexcept
  17378. {
  17379. return is_number_unsigned() ? &m_value.number_unsigned : nullptr;
  17380. }
  17381. /// get a pointer to the value (unsigned number)
  17382. constexpr const number_unsigned_t* get_impl_ptr(const number_unsigned_t* /*unused*/) const noexcept
  17383. {
  17384. return is_number_unsigned() ? &m_value.number_unsigned : nullptr;
  17385. }
  17386. /// get a pointer to the value (floating-point number)
  17387. number_float_t* get_impl_ptr(number_float_t* /*unused*/) noexcept
  17388. {
  17389. return is_number_float() ? &m_value.number_float : nullptr;
  17390. }
  17391. /// get a pointer to the value (floating-point number)
  17392. constexpr const number_float_t* get_impl_ptr(const number_float_t* /*unused*/) const noexcept
  17393. {
  17394. return is_number_float() ? &m_value.number_float : nullptr;
  17395. }
  17396. /// get a pointer to the value (binary)
  17397. binary_t* get_impl_ptr(binary_t* /*unused*/) noexcept
  17398. {
  17399. return is_binary() ? m_value.binary : nullptr;
  17400. }
  17401. /// get a pointer to the value (binary)
  17402. constexpr const binary_t* get_impl_ptr(const binary_t* /*unused*/) const noexcept
  17403. {
  17404. return is_binary() ? m_value.binary : nullptr;
  17405. }
  17406. /*!
  17407. @brief helper function to implement get_ref()
  17408. This function helps to implement get_ref() without code duplication for
  17409. const and non-const overloads
  17410. @tparam ThisType will be deduced as `basic_json` or `const basic_json`
  17411. @throw type_error.303 if ReferenceType does not match underlying value
  17412. type of the current JSON
  17413. */
  17414. template<typename ReferenceType, typename ThisType>
  17415. static ReferenceType get_ref_impl(ThisType& obj)
  17416. {
  17417. // delegate the call to get_ptr<>()
  17418. auto* ptr = obj.template get_ptr<typename std::add_pointer<ReferenceType>::type>();
  17419. if (JSON_HEDLEY_LIKELY(ptr != nullptr))
  17420. {
  17421. return *ptr;
  17422. }
  17423. JSON_THROW(type_error::create(303, detail::concat("incompatible ReferenceType for get_ref, actual type is ", obj.type_name()), &obj));
  17424. }
  17425. public:
  17426. /// @name value access
  17427. /// Direct access to the stored value of a JSON value.
  17428. /// @{
  17429. /// @brief get a pointer value (implicit)
  17430. /// @sa https://json.nlohmann.me/api/basic_json/get_ptr/
  17431. template<typename PointerType, typename std::enable_if<
  17432. std::is_pointer<PointerType>::value, int>::type = 0>
  17433. auto get_ptr() noexcept -> decltype(std::declval<basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
  17434. {
  17435. // delegate the call to get_impl_ptr<>()
  17436. return get_impl_ptr(static_cast<PointerType>(nullptr));
  17437. }
  17438. /// @brief get a pointer value (implicit)
  17439. /// @sa https://json.nlohmann.me/api/basic_json/get_ptr/
  17440. template < typename PointerType, typename std::enable_if <
  17441. std::is_pointer<PointerType>::value&&
  17442. std::is_const<typename std::remove_pointer<PointerType>::type>::value, int >::type = 0 >
  17443. constexpr auto get_ptr() const noexcept -> decltype(std::declval<const basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
  17444. {
  17445. // delegate the call to get_impl_ptr<>() const
  17446. return get_impl_ptr(static_cast<PointerType>(nullptr));
  17447. }
  17448. private:
  17449. /*!
  17450. @brief get a value (explicit)
  17451. Explicit type conversion between the JSON value and a compatible value
  17452. which is [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)
  17453. and [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible).
  17454. The value is converted by calling the @ref json_serializer<ValueType>
  17455. `from_json()` method.
  17456. The function is equivalent to executing
  17457. @code {.cpp}
  17458. ValueType ret;
  17459. JSONSerializer<ValueType>::from_json(*this, ret);
  17460. return ret;
  17461. @endcode
  17462. This overloads is chosen if:
  17463. - @a ValueType is not @ref basic_json,
  17464. - @ref json_serializer<ValueType> has a `from_json()` method of the form
  17465. `void from_json(const basic_json&, ValueType&)`, and
  17466. - @ref json_serializer<ValueType> does not have a `from_json()` method of
  17467. the form `ValueType from_json(const basic_json&)`
  17468. @tparam ValueType the returned value type
  17469. @return copy of the JSON value, converted to @a ValueType
  17470. @throw what @ref json_serializer<ValueType> `from_json()` method throws
  17471. @liveexample{The example below shows several conversions from JSON values
  17472. to other types. There a few things to note: (1) Floating-point numbers can
  17473. be converted to integers\, (2) A JSON array can be converted to a standard
  17474. `std::vector<short>`\, (3) A JSON object can be converted to C++
  17475. associative containers such as `std::unordered_map<std::string\,
  17476. json>`.,get__ValueType_const}
  17477. @since version 2.1.0
  17478. */
  17479. template < typename ValueType,
  17480. detail::enable_if_t <
  17481. detail::is_default_constructible<ValueType>::value&&
  17482. detail::has_from_json<basic_json_t, ValueType>::value,
  17483. int > = 0 >
  17484. ValueType get_impl(detail::priority_tag<0> /*unused*/) const noexcept(noexcept(
  17485. JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), std::declval<ValueType&>())))
  17486. {
  17487. auto ret = ValueType();
  17488. JSONSerializer<ValueType>::from_json(*this, ret);
  17489. return ret;
  17490. }
  17491. /*!
  17492. @brief get a value (explicit); special case
  17493. Explicit type conversion between the JSON value and a compatible value
  17494. which is **not** [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)
  17495. and **not** [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible).
  17496. The value is converted by calling the @ref json_serializer<ValueType>
  17497. `from_json()` method.
  17498. The function is equivalent to executing
  17499. @code {.cpp}
  17500. return JSONSerializer<ValueType>::from_json(*this);
  17501. @endcode
  17502. This overloads is chosen if:
  17503. - @a ValueType is not @ref basic_json and
  17504. - @ref json_serializer<ValueType> has a `from_json()` method of the form
  17505. `ValueType from_json(const basic_json&)`
  17506. @note If @ref json_serializer<ValueType> has both overloads of
  17507. `from_json()`, this one is chosen.
  17508. @tparam ValueType the returned value type
  17509. @return copy of the JSON value, converted to @a ValueType
  17510. @throw what @ref json_serializer<ValueType> `from_json()` method throws
  17511. @since version 2.1.0
  17512. */
  17513. template < typename ValueType,
  17514. detail::enable_if_t <
  17515. detail::has_non_default_from_json<basic_json_t, ValueType>::value,
  17516. int > = 0 >
  17517. ValueType get_impl(detail::priority_tag<1> /*unused*/) const noexcept(noexcept(
  17518. JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>())))
  17519. {
  17520. return JSONSerializer<ValueType>::from_json(*this);
  17521. }
  17522. /*!
  17523. @brief get special-case overload
  17524. This overloads converts the current @ref basic_json in a different
  17525. @ref basic_json type
  17526. @tparam BasicJsonType == @ref basic_json
  17527. @return a copy of *this, converted into @a BasicJsonType
  17528. @complexity Depending on the implementation of the called `from_json()`
  17529. method.
  17530. @since version 3.2.0
  17531. */
  17532. template < typename BasicJsonType,
  17533. detail::enable_if_t <
  17534. detail::is_basic_json<BasicJsonType>::value,
  17535. int > = 0 >
  17536. BasicJsonType get_impl(detail::priority_tag<2> /*unused*/) const
  17537. {
  17538. return *this;
  17539. }
  17540. /*!
  17541. @brief get special-case overload
  17542. This overloads avoids a lot of template boilerplate, it can be seen as the
  17543. identity method
  17544. @tparam BasicJsonType == @ref basic_json
  17545. @return a copy of *this
  17546. @complexity Constant.
  17547. @since version 2.1.0
  17548. */
  17549. template<typename BasicJsonType,
  17550. detail::enable_if_t<
  17551. std::is_same<BasicJsonType, basic_json_t>::value,
  17552. int> = 0>
  17553. basic_json get_impl(detail::priority_tag<3> /*unused*/) const
  17554. {
  17555. return *this;
  17556. }
  17557. /*!
  17558. @brief get a pointer value (explicit)
  17559. @copydoc get()
  17560. */
  17561. template<typename PointerType,
  17562. detail::enable_if_t<
  17563. std::is_pointer<PointerType>::value,
  17564. int> = 0>
  17565. constexpr auto get_impl(detail::priority_tag<4> /*unused*/) const noexcept
  17566. -> decltype(std::declval<const basic_json_t&>().template get_ptr<PointerType>())
  17567. {
  17568. // delegate the call to get_ptr
  17569. return get_ptr<PointerType>();
  17570. }
  17571. public:
  17572. /*!
  17573. @brief get a (pointer) value (explicit)
  17574. Performs explicit type conversion between the JSON value and a compatible value if required.
  17575. - If the requested type is a pointer to the internally stored JSON value that pointer is returned.
  17576. No copies are made.
  17577. - If the requested type is the current @ref basic_json, or a different @ref basic_json convertible
  17578. from the current @ref basic_json.
  17579. - Otherwise the value is converted by calling the @ref json_serializer<ValueType> `from_json()`
  17580. method.
  17581. @tparam ValueTypeCV the provided value type
  17582. @tparam ValueType the returned value type
  17583. @return copy of the JSON value, converted to @tparam ValueType if necessary
  17584. @throw what @ref json_serializer<ValueType> `from_json()` method throws if conversion is required
  17585. @since version 2.1.0
  17586. */
  17587. template < typename ValueTypeCV, typename ValueType = detail::uncvref_t<ValueTypeCV>>
  17588. #if defined(JSON_HAS_CPP_14)
  17589. constexpr
  17590. #endif
  17591. auto get() const noexcept(
  17592. noexcept(std::declval<const basic_json_t&>().template get_impl<ValueType>(detail::priority_tag<4> {})))
  17593. -> decltype(std::declval<const basic_json_t&>().template get_impl<ValueType>(detail::priority_tag<4> {}))
  17594. {
  17595. // we cannot static_assert on ValueTypeCV being non-const, because
  17596. // there is support for get<const basic_json_t>(), which is why we
  17597. // still need the uncvref
  17598. static_assert(!std::is_reference<ValueTypeCV>::value,
  17599. "get() cannot be used with reference types, you might want to use get_ref()");
  17600. return get_impl<ValueType>(detail::priority_tag<4> {});
  17601. }
  17602. /*!
  17603. @brief get a pointer value (explicit)
  17604. Explicit pointer access to the internally stored JSON value. No copies are
  17605. made.
  17606. @warning The pointer becomes invalid if the underlying JSON object
  17607. changes.
  17608. @tparam PointerType pointer type; must be a pointer to @ref array_t, @ref
  17609. object_t, @ref string_t, @ref boolean_t, @ref number_integer_t,
  17610. @ref number_unsigned_t, or @ref number_float_t.
  17611. @return pointer to the internally stored JSON value if the requested
  17612. pointer type @a PointerType fits to the JSON value; `nullptr` otherwise
  17613. @complexity Constant.
  17614. @liveexample{The example below shows how pointers to internal values of a
  17615. JSON value can be requested. Note that no type conversions are made and a
  17616. `nullptr` is returned if the value and the requested pointer type does not
  17617. match.,get__PointerType}
  17618. @sa see @ref get_ptr() for explicit pointer-member access
  17619. @since version 1.0.0
  17620. */
  17621. template<typename PointerType, typename std::enable_if<
  17622. std::is_pointer<PointerType>::value, int>::type = 0>
  17623. auto get() noexcept -> decltype(std::declval<basic_json_t&>().template get_ptr<PointerType>())
  17624. {
  17625. // delegate the call to get_ptr
  17626. return get_ptr<PointerType>();
  17627. }
  17628. /// @brief get a value (explicit)
  17629. /// @sa https://json.nlohmann.me/api/basic_json/get_to/
  17630. template < typename ValueType,
  17631. detail::enable_if_t <
  17632. !detail::is_basic_json<ValueType>::value&&
  17633. detail::has_from_json<basic_json_t, ValueType>::value,
  17634. int > = 0 >
  17635. ValueType & get_to(ValueType& v) const noexcept(noexcept(
  17636. JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), v)))
  17637. {
  17638. JSONSerializer<ValueType>::from_json(*this, v);
  17639. return v;
  17640. }
  17641. // specialization to allow calling get_to with a basic_json value
  17642. // see https://github.com/nlohmann/json/issues/2175
  17643. template<typename ValueType,
  17644. detail::enable_if_t <
  17645. detail::is_basic_json<ValueType>::value,
  17646. int> = 0>
  17647. ValueType & get_to(ValueType& v) const
  17648. {
  17649. v = *this;
  17650. return v;
  17651. }
  17652. template <
  17653. typename T, std::size_t N,
  17654. typename Array = T (&)[N], // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  17655. detail::enable_if_t <
  17656. detail::has_from_json<basic_json_t, Array>::value, int > = 0 >
  17657. Array get_to(T (&v)[N]) const // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  17658. noexcept(noexcept(JSONSerializer<Array>::from_json(
  17659. std::declval<const basic_json_t&>(), v)))
  17660. {
  17661. JSONSerializer<Array>::from_json(*this, v);
  17662. return v;
  17663. }
  17664. /// @brief get a reference value (implicit)
  17665. /// @sa https://json.nlohmann.me/api/basic_json/get_ref/
  17666. template<typename ReferenceType, typename std::enable_if<
  17667. std::is_reference<ReferenceType>::value, int>::type = 0>
  17668. ReferenceType get_ref()
  17669. {
  17670. // delegate call to get_ref_impl
  17671. return get_ref_impl<ReferenceType>(*this);
  17672. }
  17673. /// @brief get a reference value (implicit)
  17674. /// @sa https://json.nlohmann.me/api/basic_json/get_ref/
  17675. template < typename ReferenceType, typename std::enable_if <
  17676. std::is_reference<ReferenceType>::value&&
  17677. std::is_const<typename std::remove_reference<ReferenceType>::type>::value, int >::type = 0 >
  17678. ReferenceType get_ref() const
  17679. {
  17680. // delegate call to get_ref_impl
  17681. return get_ref_impl<ReferenceType>(*this);
  17682. }
  17683. /*!
  17684. @brief get a value (implicit)
  17685. Implicit type conversion between the JSON value and a compatible value.
  17686. The call is realized by calling @ref get() const.
  17687. @tparam ValueType non-pointer type compatible to the JSON value, for
  17688. instance `int` for JSON integer numbers, `bool` for JSON booleans, or
  17689. `std::vector` types for JSON arrays. The character type of @ref string_t
  17690. as well as an initializer list of this type is excluded to avoid
  17691. ambiguities as these types implicitly convert to `std::string`.
  17692. @return copy of the JSON value, converted to type @a ValueType
  17693. @throw type_error.302 in case passed type @a ValueType is incompatible
  17694. to the JSON value type (e.g., the JSON value is of type boolean, but a
  17695. string is requested); see example below
  17696. @complexity Linear in the size of the JSON value.
  17697. @liveexample{The example below shows several conversions from JSON values
  17698. to other types. There a few things to note: (1) Floating-point numbers can
  17699. be converted to integers\, (2) A JSON array can be converted to a standard
  17700. `std::vector<short>`\, (3) A JSON object can be converted to C++
  17701. associative containers such as `std::unordered_map<std::string\,
  17702. json>`.,operator__ValueType}
  17703. @since version 1.0.0
  17704. */
  17705. template < typename ValueType, typename std::enable_if <
  17706. detail::conjunction <
  17707. detail::negation<std::is_pointer<ValueType>>,
  17708. detail::negation<std::is_same<ValueType, std::nullptr_t>>,
  17709. detail::negation<std::is_same<ValueType, detail::json_ref<basic_json>>>,
  17710. detail::negation<std::is_same<ValueType, typename string_t::value_type>>,
  17711. detail::negation<detail::is_basic_json<ValueType>>,
  17712. detail::negation<std::is_same<ValueType, std::initializer_list<typename string_t::value_type>>>,
  17713. #if defined(JSON_HAS_CPP_17) && (defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER <= 1914))
  17714. detail::negation<std::is_same<ValueType, std::string_view>>,
  17715. #endif
  17716. #if defined(JSON_HAS_CPP_17)
  17717. detail::negation<std::is_same<ValueType, std::any>>,
  17718. #endif
  17719. detail::is_detected_lazy<detail::get_template_function, const basic_json_t&, ValueType>
  17720. >::value, int >::type = 0 >
  17721. JSON_EXPLICIT operator ValueType() const
  17722. {
  17723. // delegate the call to get<>() const
  17724. return get<ValueType>();
  17725. }
  17726. /// @brief get a binary value
  17727. /// @sa https://json.nlohmann.me/api/basic_json/get_binary/
  17728. binary_t& get_binary()
  17729. {
  17730. if (!is_binary())
  17731. {
  17732. JSON_THROW(type_error::create(302, detail::concat("type must be binary, but is ", type_name()), this));
  17733. }
  17734. return *get_ptr<binary_t*>();
  17735. }
  17736. /// @brief get a binary value
  17737. /// @sa https://json.nlohmann.me/api/basic_json/get_binary/
  17738. const binary_t& get_binary() const
  17739. {
  17740. if (!is_binary())
  17741. {
  17742. JSON_THROW(type_error::create(302, detail::concat("type must be binary, but is ", type_name()), this));
  17743. }
  17744. return *get_ptr<const binary_t*>();
  17745. }
  17746. /// @}
  17747. ////////////////////
  17748. // element access //
  17749. ////////////////////
  17750. /// @name element access
  17751. /// Access to the JSON value.
  17752. /// @{
  17753. /// @brief access specified array element with bounds checking
  17754. /// @sa https://json.nlohmann.me/api/basic_json/at/
  17755. reference at(size_type idx)
  17756. {
  17757. // at only works for arrays
  17758. if (JSON_HEDLEY_LIKELY(is_array()))
  17759. {
  17760. JSON_TRY
  17761. {
  17762. return set_parent(m_value.array->at(idx));
  17763. }
  17764. JSON_CATCH (std::out_of_range&)
  17765. {
  17766. // create better exception explanation
  17767. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
  17768. }
  17769. }
  17770. else
  17771. {
  17772. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  17773. }
  17774. }
  17775. /// @brief access specified array element with bounds checking
  17776. /// @sa https://json.nlohmann.me/api/basic_json/at/
  17777. const_reference at(size_type idx) const
  17778. {
  17779. // at only works for arrays
  17780. if (JSON_HEDLEY_LIKELY(is_array()))
  17781. {
  17782. JSON_TRY
  17783. {
  17784. return m_value.array->at(idx);
  17785. }
  17786. JSON_CATCH (std::out_of_range&)
  17787. {
  17788. // create better exception explanation
  17789. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
  17790. }
  17791. }
  17792. else
  17793. {
  17794. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  17795. }
  17796. }
  17797. /// @brief access specified object element with bounds checking
  17798. /// @sa https://json.nlohmann.me/api/basic_json/at/
  17799. reference at(const typename object_t::key_type& key)
  17800. {
  17801. // at only works for objects
  17802. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  17803. {
  17804. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  17805. }
  17806. auto it = m_value.object->find(key);
  17807. if (it == m_value.object->end())
  17808. {
  17809. JSON_THROW(out_of_range::create(403, detail::concat("key '", key, "' not found"), this));
  17810. }
  17811. return set_parent(it->second);
  17812. }
  17813. /// @brief access specified object element with bounds checking
  17814. /// @sa https://json.nlohmann.me/api/basic_json/at/
  17815. template<class KeyType, detail::enable_if_t<
  17816. detail::is_usable_as_key_type<basic_json_t, KeyType>::value, int> = 0>
  17817. reference at(KeyType && key)
  17818. {
  17819. // at only works for objects
  17820. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  17821. {
  17822. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  17823. }
  17824. auto it = m_value.object->find(std::forward<KeyType>(key));
  17825. if (it == m_value.object->end())
  17826. {
  17827. JSON_THROW(out_of_range::create(403, detail::concat("key '", string_t(std::forward<KeyType>(key)), "' not found"), this));
  17828. }
  17829. return set_parent(it->second);
  17830. }
  17831. /// @brief access specified object element with bounds checking
  17832. /// @sa https://json.nlohmann.me/api/basic_json/at/
  17833. const_reference at(const typename object_t::key_type& key) const
  17834. {
  17835. // at only works for objects
  17836. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  17837. {
  17838. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  17839. }
  17840. auto it = m_value.object->find(key);
  17841. if (it == m_value.object->end())
  17842. {
  17843. JSON_THROW(out_of_range::create(403, detail::concat("key '", key, "' not found"), this));
  17844. }
  17845. return it->second;
  17846. }
  17847. /// @brief access specified object element with bounds checking
  17848. /// @sa https://json.nlohmann.me/api/basic_json/at/
  17849. template<class KeyType, detail::enable_if_t<
  17850. detail::is_usable_as_key_type<basic_json_t, KeyType>::value, int> = 0>
  17851. const_reference at(KeyType && key) const
  17852. {
  17853. // at only works for objects
  17854. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  17855. {
  17856. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  17857. }
  17858. auto it = m_value.object->find(std::forward<KeyType>(key));
  17859. if (it == m_value.object->end())
  17860. {
  17861. JSON_THROW(out_of_range::create(403, detail::concat("key '", string_t(std::forward<KeyType>(key)), "' not found"), this));
  17862. }
  17863. return it->second;
  17864. }
  17865. /// @brief access specified array element
  17866. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  17867. reference operator[](size_type idx)
  17868. {
  17869. // implicitly convert null value to an empty array
  17870. if (is_null())
  17871. {
  17872. m_type = value_t::array;
  17873. m_value.array = create<array_t>();
  17874. assert_invariant();
  17875. }
  17876. // operator[] only works for arrays
  17877. if (JSON_HEDLEY_LIKELY(is_array()))
  17878. {
  17879. // fill up array with null values if given idx is outside range
  17880. if (idx >= m_value.array->size())
  17881. {
  17882. #if JSON_DIAGNOSTICS
  17883. // remember array size & capacity before resizing
  17884. const auto old_size = m_value.array->size();
  17885. const auto old_capacity = m_value.array->capacity();
  17886. #endif
  17887. m_value.array->resize(idx + 1);
  17888. #if JSON_DIAGNOSTICS
  17889. if (JSON_HEDLEY_UNLIKELY(m_value.array->capacity() != old_capacity))
  17890. {
  17891. // capacity has changed: update all parents
  17892. set_parents();
  17893. }
  17894. else
  17895. {
  17896. // set parent for values added above
  17897. set_parents(begin() + static_cast<typename iterator::difference_type>(old_size), static_cast<typename iterator::difference_type>(idx + 1 - old_size));
  17898. }
  17899. #endif
  17900. assert_invariant();
  17901. }
  17902. return m_value.array->operator[](idx);
  17903. }
  17904. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a numeric argument with ", type_name()), this));
  17905. }
  17906. /// @brief access specified array element
  17907. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  17908. const_reference operator[](size_type idx) const
  17909. {
  17910. // const operator[] only works for arrays
  17911. if (JSON_HEDLEY_LIKELY(is_array()))
  17912. {
  17913. return m_value.array->operator[](idx);
  17914. }
  17915. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a numeric argument with ", type_name()), this));
  17916. }
  17917. /// @brief access specified object element
  17918. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  17919. reference operator[](typename object_t::key_type key)
  17920. {
  17921. // implicitly convert null value to an empty object
  17922. if (is_null())
  17923. {
  17924. m_type = value_t::object;
  17925. m_value.object = create<object_t>();
  17926. assert_invariant();
  17927. }
  17928. // operator[] only works for objects
  17929. if (JSON_HEDLEY_LIKELY(is_object()))
  17930. {
  17931. auto result = m_value.object->emplace(std::move(key), nullptr);
  17932. return set_parent(result.first->second);
  17933. }
  17934. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  17935. }
  17936. /// @brief access specified object element
  17937. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  17938. const_reference operator[](const typename object_t::key_type& key) const
  17939. {
  17940. // const operator[] only works for objects
  17941. if (JSON_HEDLEY_LIKELY(is_object()))
  17942. {
  17943. auto it = m_value.object->find(key);
  17944. JSON_ASSERT(it != m_value.object->end());
  17945. return it->second;
  17946. }
  17947. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  17948. }
  17949. // these two functions resolve a (const) char * ambiguity affecting Clang and MSVC
  17950. // (they seemingly cannot be constrained to resolve the ambiguity)
  17951. template<typename T>
  17952. reference operator[](T* key)
  17953. {
  17954. return operator[](typename object_t::key_type(key));
  17955. }
  17956. template<typename T>
  17957. const_reference operator[](T* key) const
  17958. {
  17959. return operator[](typename object_t::key_type(key));
  17960. }
  17961. /// @brief access specified object element
  17962. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  17963. template<class KeyType, detail::enable_if_t<
  17964. detail::is_usable_as_key_type<basic_json_t, KeyType>::value, int > = 0 >
  17965. reference operator[](KeyType && key)
  17966. {
  17967. // implicitly convert null value to an empty object
  17968. if (is_null())
  17969. {
  17970. m_type = value_t::object;
  17971. m_value.object = create<object_t>();
  17972. assert_invariant();
  17973. }
  17974. // operator[] only works for objects
  17975. if (JSON_HEDLEY_LIKELY(is_object()))
  17976. {
  17977. auto result = m_value.object->emplace(std::forward<KeyType>(key), nullptr);
  17978. return set_parent(result.first->second);
  17979. }
  17980. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  17981. }
  17982. /// @brief access specified object element
  17983. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  17984. template<class KeyType, detail::enable_if_t<
  17985. detail::is_usable_as_key_type<basic_json_t, KeyType>::value, int > = 0 >
  17986. const_reference operator[](KeyType && key) const
  17987. {
  17988. // const operator[] only works for objects
  17989. if (JSON_HEDLEY_LIKELY(is_object()))
  17990. {
  17991. auto it = m_value.object->find(std::forward<KeyType>(key));
  17992. JSON_ASSERT(it != m_value.object->end());
  17993. return it->second;
  17994. }
  17995. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  17996. }
  17997. /// @brief access specified object element with default value
  17998. /// @sa https://json.nlohmann.me/api/basic_json/value/
  17999. // this is the value(const typename object_t::key_type&) overload
  18000. template < class KeyType, class ValueType, detail::enable_if_t <
  18001. std::is_same<KeyType, typename object_t::key_type>::value
  18002. && detail::is_getable<basic_json_t, ValueType>::value
  18003. && !std::is_same<value_t, ValueType>::value, int > = 0 >
  18004. typename std::decay<ValueType>::type value(const KeyType& key, ValueType && default_value) const
  18005. {
  18006. // value only works for objects
  18007. if (JSON_HEDLEY_LIKELY(is_object()))
  18008. {
  18009. // if key is found, return value and given default value otherwise
  18010. const auto it = find(key);
  18011. if (it != end())
  18012. {
  18013. return it->template get<typename std::decay<ValueType>::type>();
  18014. }
  18015. return std::forward<ValueType>(default_value);
  18016. }
  18017. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  18018. }
  18019. /// @brief access specified object element with default value
  18020. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18021. /// overload for a default value of type const char*
  18022. string_t value(const typename object_t::key_type& key, const char* default_value) const
  18023. {
  18024. return value(key, string_t(default_value));
  18025. }
  18026. // these two functions, in conjunction with value(const KeyType &, ValueType &&),
  18027. // resolve an ambiguity that would otherwise occur between the json_pointer and
  18028. // typename object_t::key_type & overloads
  18029. template < class ValueType, detail::enable_if_t <
  18030. detail::is_getable<basic_json_t, ValueType>::value
  18031. && !std::is_same<value_t, ValueType>::value, int > = 0 >
  18032. typename std::decay<ValueType>::type value(const char* key, ValueType && default_value) const
  18033. {
  18034. return value(typename object_t::key_type(key), std::forward<ValueType>(default_value));
  18035. }
  18036. string_t value(const char* key, const char* default_value) const
  18037. {
  18038. return value(typename object_t::key_type(key), string_t(default_value));
  18039. }
  18040. /// @brief access specified object element with default value
  18041. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18042. /// using std::is_convertible in a std::enable_if will fail when using explicit conversions
  18043. template < class KeyType, class ValueType, detail::enable_if_t <
  18044. detail::is_getable<basic_json_t, ValueType>::value
  18045. && !std::is_same<value_t, ValueType>::value
  18046. && detail::is_usable_as_key_type<basic_json_t, KeyType>::value, int > = 0 >
  18047. typename std::decay<ValueType>::type value(KeyType && key, ValueType && default_value) const
  18048. {
  18049. // value only works for objects
  18050. if (JSON_HEDLEY_LIKELY(is_object()))
  18051. {
  18052. // if key is found, return value and given default value otherwise
  18053. const auto it = find(std::forward<KeyType>(key));
  18054. if (it != end())
  18055. {
  18056. return it->template get<typename std::decay<ValueType>::type>();
  18057. }
  18058. return std::forward<ValueType>(default_value);
  18059. }
  18060. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  18061. }
  18062. /// @brief access specified object element with default value
  18063. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18064. /// overload for a default value of type const char*
  18065. template < class KeyType, detail::enable_if_t <
  18066. !detail::is_json_pointer<KeyType>::value, int > = 0 >
  18067. string_t value(KeyType && key, const char* default_value) const
  18068. {
  18069. return value(std::forward<KeyType>(key), string_t(default_value));
  18070. }
  18071. /// @brief access specified object element via JSON Pointer with default value
  18072. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18073. template < class ValueType, detail::enable_if_t <
  18074. detail::is_getable<basic_json_t, ValueType>::value, int> = 0 >
  18075. ValueType value(const json_pointer& ptr, const ValueType& default_value) const
  18076. {
  18077. // value only works for objects
  18078. if (JSON_HEDLEY_LIKELY(is_object()))
  18079. {
  18080. // if pointer resolves a value, return it or use default value
  18081. JSON_TRY
  18082. {
  18083. return ptr.get_checked(this).template get<ValueType>();
  18084. }
  18085. JSON_INTERNAL_CATCH (out_of_range&)
  18086. {
  18087. return default_value;
  18088. }
  18089. }
  18090. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  18091. }
  18092. template < class ValueType, class BasicJsonType, detail::enable_if_t <
  18093. detail::is_getable<basic_json_t, ValueType>::value, int> = 0 >
  18094. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  18095. ValueType value(const ::nlohmann::json_pointer<BasicJsonType>& ptr, const ValueType& default_value) const
  18096. {
  18097. return value(ptr.convert(), default_value);
  18098. }
  18099. /// @brief access specified object element via JSON Pointer with default value
  18100. /// @sa https://json.nlohmann.me/api/basic_json/value/
  18101. /// overload for a default value of type const char*
  18102. JSON_HEDLEY_NON_NULL(3)
  18103. string_t value(const json_pointer& ptr, const char* default_value) const
  18104. {
  18105. return value(ptr, string_t(default_value));
  18106. }
  18107. template<typename BasicJsonType>
  18108. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  18109. JSON_HEDLEY_NON_NULL(3)
  18110. string_t value(const typename ::nlohmann::json_pointer<BasicJsonType>& ptr, const char* default_value) const
  18111. {
  18112. return value(ptr.convert(), default_value);
  18113. }
  18114. /// @brief access the first element
  18115. /// @sa https://json.nlohmann.me/api/basic_json/front/
  18116. reference front()
  18117. {
  18118. return *begin();
  18119. }
  18120. /// @brief access the first element
  18121. /// @sa https://json.nlohmann.me/api/basic_json/front/
  18122. const_reference front() const
  18123. {
  18124. return *cbegin();
  18125. }
  18126. /// @brief access the last element
  18127. /// @sa https://json.nlohmann.me/api/basic_json/back/
  18128. reference back()
  18129. {
  18130. auto tmp = end();
  18131. --tmp;
  18132. return *tmp;
  18133. }
  18134. /// @brief access the last element
  18135. /// @sa https://json.nlohmann.me/api/basic_json/back/
  18136. const_reference back() const
  18137. {
  18138. auto tmp = cend();
  18139. --tmp;
  18140. return *tmp;
  18141. }
  18142. /// @brief remove element given an iterator
  18143. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  18144. template < class IteratorType, detail::enable_if_t <
  18145. std::is_same<IteratorType, typename basic_json_t::iterator>::value ||
  18146. std::is_same<IteratorType, typename basic_json_t::const_iterator>::value, int > = 0 >
  18147. IteratorType erase(IteratorType pos)
  18148. {
  18149. // make sure iterator fits the current value
  18150. if (JSON_HEDLEY_UNLIKELY(this != pos.m_object))
  18151. {
  18152. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  18153. }
  18154. IteratorType result = end();
  18155. switch (m_type)
  18156. {
  18157. case value_t::boolean:
  18158. case value_t::number_float:
  18159. case value_t::number_integer:
  18160. case value_t::number_unsigned:
  18161. case value_t::string:
  18162. case value_t::binary:
  18163. {
  18164. if (JSON_HEDLEY_UNLIKELY(!pos.m_it.primitive_iterator.is_begin()))
  18165. {
  18166. JSON_THROW(invalid_iterator::create(205, "iterator out of range", this));
  18167. }
  18168. if (is_string())
  18169. {
  18170. AllocatorType<string_t> alloc;
  18171. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.string);
  18172. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.string, 1);
  18173. m_value.string = nullptr;
  18174. }
  18175. else if (is_binary())
  18176. {
  18177. AllocatorType<binary_t> alloc;
  18178. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.binary);
  18179. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.binary, 1);
  18180. m_value.binary = nullptr;
  18181. }
  18182. m_type = value_t::null;
  18183. assert_invariant();
  18184. break;
  18185. }
  18186. case value_t::object:
  18187. {
  18188. result.m_it.object_iterator = m_value.object->erase(pos.m_it.object_iterator);
  18189. break;
  18190. }
  18191. case value_t::array:
  18192. {
  18193. result.m_it.array_iterator = m_value.array->erase(pos.m_it.array_iterator);
  18194. break;
  18195. }
  18196. case value_t::null:
  18197. case value_t::discarded:
  18198. default:
  18199. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  18200. }
  18201. return result;
  18202. }
  18203. /// @brief remove elements given an iterator range
  18204. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  18205. template < class IteratorType, detail::enable_if_t <
  18206. std::is_same<IteratorType, typename basic_json_t::iterator>::value ||
  18207. std::is_same<IteratorType, typename basic_json_t::const_iterator>::value, int > = 0 >
  18208. IteratorType erase(IteratorType first, IteratorType last)
  18209. {
  18210. // make sure iterator fits the current value
  18211. if (JSON_HEDLEY_UNLIKELY(this != first.m_object || this != last.m_object))
  18212. {
  18213. JSON_THROW(invalid_iterator::create(203, "iterators do not fit current value", this));
  18214. }
  18215. IteratorType result = end();
  18216. switch (m_type)
  18217. {
  18218. case value_t::boolean:
  18219. case value_t::number_float:
  18220. case value_t::number_integer:
  18221. case value_t::number_unsigned:
  18222. case value_t::string:
  18223. case value_t::binary:
  18224. {
  18225. if (JSON_HEDLEY_LIKELY(!first.m_it.primitive_iterator.is_begin()
  18226. || !last.m_it.primitive_iterator.is_end()))
  18227. {
  18228. JSON_THROW(invalid_iterator::create(204, "iterators out of range", this));
  18229. }
  18230. if (is_string())
  18231. {
  18232. AllocatorType<string_t> alloc;
  18233. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.string);
  18234. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.string, 1);
  18235. m_value.string = nullptr;
  18236. }
  18237. else if (is_binary())
  18238. {
  18239. AllocatorType<binary_t> alloc;
  18240. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.binary);
  18241. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.binary, 1);
  18242. m_value.binary = nullptr;
  18243. }
  18244. m_type = value_t::null;
  18245. assert_invariant();
  18246. break;
  18247. }
  18248. case value_t::object:
  18249. {
  18250. result.m_it.object_iterator = m_value.object->erase(first.m_it.object_iterator,
  18251. last.m_it.object_iterator);
  18252. break;
  18253. }
  18254. case value_t::array:
  18255. {
  18256. result.m_it.array_iterator = m_value.array->erase(first.m_it.array_iterator,
  18257. last.m_it.array_iterator);
  18258. break;
  18259. }
  18260. case value_t::null:
  18261. case value_t::discarded:
  18262. default:
  18263. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  18264. }
  18265. return result;
  18266. }
  18267. private:
  18268. template < typename KeyType, detail::enable_if_t <
  18269. detail::has_erase_with_key_type<basic_json_t, KeyType>::value, int > = 0 >
  18270. size_type erase_internal(KeyType && key)
  18271. {
  18272. // this erase only works for objects
  18273. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  18274. {
  18275. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  18276. }
  18277. return m_value.object->erase(std::forward<KeyType>(key));
  18278. }
  18279. template < typename KeyType, detail::enable_if_t <
  18280. !detail::has_erase_with_key_type<basic_json_t, KeyType>::value, int > = 0 >
  18281. size_type erase_internal(KeyType && key)
  18282. {
  18283. // this erase only works for objects
  18284. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  18285. {
  18286. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  18287. }
  18288. const auto it = m_value.object->find(std::forward<KeyType>(key));
  18289. if (it != m_value.object->end())
  18290. {
  18291. m_value.object->erase(it);
  18292. return 1;
  18293. }
  18294. return 0;
  18295. }
  18296. public:
  18297. /// @brief remove element from a JSON object given a key
  18298. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  18299. size_type erase(const typename object_t::key_type& key)
  18300. {
  18301. // the indirection via erase_internal() is added to avoid making this
  18302. // function a template and thus de-rank it during overload resolution
  18303. return erase_internal(key);
  18304. }
  18305. /// @brief remove element from a JSON object given a key
  18306. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  18307. template<class KeyType, detail::enable_if_t<
  18308. detail::is_usable_as_key_type<basic_json_t, KeyType>::value, int> = 0>
  18309. size_type erase(KeyType && key)
  18310. {
  18311. return erase_internal(std::forward<KeyType>(key));
  18312. }
  18313. /// @brief remove element from a JSON array given an index
  18314. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  18315. void erase(const size_type idx)
  18316. {
  18317. // this erase only works for arrays
  18318. if (JSON_HEDLEY_LIKELY(is_array()))
  18319. {
  18320. if (JSON_HEDLEY_UNLIKELY(idx >= size()))
  18321. {
  18322. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
  18323. }
  18324. m_value.array->erase(m_value.array->begin() + static_cast<difference_type>(idx));
  18325. }
  18326. else
  18327. {
  18328. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  18329. }
  18330. }
  18331. /// @}
  18332. ////////////
  18333. // lookup //
  18334. ////////////
  18335. /// @name lookup
  18336. /// @{
  18337. /// @brief find an element in a JSON object
  18338. /// @sa https://json.nlohmann.me/api/basic_json/find/
  18339. iterator find(const typename object_t::key_type& key)
  18340. {
  18341. auto result = end();
  18342. if (is_object())
  18343. {
  18344. result.m_it.object_iterator = m_value.object->find(key);
  18345. }
  18346. return result;
  18347. }
  18348. /// @brief find an element in a JSON object
  18349. /// @sa https://json.nlohmann.me/api/basic_json/find/
  18350. const_iterator find(const typename object_t::key_type& key) const
  18351. {
  18352. auto result = cend();
  18353. if (is_object())
  18354. {
  18355. result.m_it.object_iterator = m_value.object->find(key);
  18356. }
  18357. return result;
  18358. }
  18359. /// @brief find an element in a JSON object
  18360. /// @sa https://json.nlohmann.me/api/basic_json/find/
  18361. template<class KeyType, detail::enable_if_t<
  18362. detail::is_usable_as_key_type<basic_json_t, KeyType>::value, int> = 0>
  18363. iterator find(KeyType && key)
  18364. {
  18365. auto result = end();
  18366. if (is_object())
  18367. {
  18368. result.m_it.object_iterator = m_value.object->find(std::forward<KeyType>(key));
  18369. }
  18370. return result;
  18371. }
  18372. /// @brief find an element in a JSON object
  18373. /// @sa https://json.nlohmann.me/api/basic_json/find/
  18374. template<class KeyType, detail::enable_if_t<
  18375. detail::is_usable_as_key_type<basic_json_t, KeyType>::value, int> = 0>
  18376. const_iterator find(KeyType && key) const
  18377. {
  18378. auto result = cend();
  18379. if (is_object())
  18380. {
  18381. result.m_it.object_iterator = m_value.object->find(std::forward<KeyType>(key));
  18382. }
  18383. return result;
  18384. }
  18385. /// @brief returns the number of occurrences of a key in a JSON object
  18386. /// @sa https://json.nlohmann.me/api/basic_json/count/
  18387. size_type count(const typename object_t::key_type& key) const
  18388. {
  18389. // return 0 for all nonobject types
  18390. return is_object() ? m_value.object->count(key) : 0;
  18391. }
  18392. /// @brief returns the number of occurrences of a key in a JSON object
  18393. /// @sa https://json.nlohmann.me/api/basic_json/count/
  18394. template<class KeyType, detail::enable_if_t<
  18395. detail::is_usable_as_key_type<basic_json_t, KeyType>::value, int> = 0>
  18396. size_type count(KeyType && key) const
  18397. {
  18398. // return 0 for all nonobject types
  18399. return is_object() ? m_value.object->count(std::forward<KeyType>(key)) : 0;
  18400. }
  18401. /// @brief check the existence of an element in a JSON object
  18402. /// @sa https://json.nlohmann.me/api/basic_json/contains/
  18403. bool contains(const typename object_t::key_type& key) const
  18404. {
  18405. return is_object() && m_value.object->find(key) != m_value.object->end();
  18406. }
  18407. /// @brief check the existence of an element in a JSON object
  18408. /// @sa https://json.nlohmann.me/api/basic_json/contains/
  18409. template<class KeyType, detail::enable_if_t<
  18410. detail::is_usable_as_key_type<basic_json_t, KeyType>::value, int> = 0>
  18411. bool contains(KeyType && key) const
  18412. {
  18413. return is_object() && m_value.object->find(std::forward<KeyType>(key)) != m_value.object->end();
  18414. }
  18415. /// @brief check the existence of an element in a JSON object given a JSON pointer
  18416. /// @sa https://json.nlohmann.me/api/basic_json/contains/
  18417. bool contains(const json_pointer& ptr) const
  18418. {
  18419. return ptr.contains(this);
  18420. }
  18421. template<typename BasicJsonType>
  18422. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  18423. bool contains(const typename ::nlohmann::json_pointer<BasicJsonType> ptr) const
  18424. {
  18425. return ptr.contains(this);
  18426. }
  18427. /// @}
  18428. ///////////////
  18429. // iterators //
  18430. ///////////////
  18431. /// @name iterators
  18432. /// @{
  18433. /// @brief returns an iterator to the first element
  18434. /// @sa https://json.nlohmann.me/api/basic_json/begin/
  18435. iterator begin() noexcept
  18436. {
  18437. iterator result(this);
  18438. result.set_begin();
  18439. return result;
  18440. }
  18441. /// @brief returns an iterator to the first element
  18442. /// @sa https://json.nlohmann.me/api/basic_json/begin/
  18443. const_iterator begin() const noexcept
  18444. {
  18445. return cbegin();
  18446. }
  18447. /// @brief returns a const iterator to the first element
  18448. /// @sa https://json.nlohmann.me/api/basic_json/cbegin/
  18449. const_iterator cbegin() const noexcept
  18450. {
  18451. const_iterator result(this);
  18452. result.set_begin();
  18453. return result;
  18454. }
  18455. /// @brief returns an iterator to one past the last element
  18456. /// @sa https://json.nlohmann.me/api/basic_json/end/
  18457. iterator end() noexcept
  18458. {
  18459. iterator result(this);
  18460. result.set_end();
  18461. return result;
  18462. }
  18463. /// @brief returns an iterator to one past the last element
  18464. /// @sa https://json.nlohmann.me/api/basic_json/end/
  18465. const_iterator end() const noexcept
  18466. {
  18467. return cend();
  18468. }
  18469. /// @brief returns an iterator to one past the last element
  18470. /// @sa https://json.nlohmann.me/api/basic_json/cend/
  18471. const_iterator cend() const noexcept
  18472. {
  18473. const_iterator result(this);
  18474. result.set_end();
  18475. return result;
  18476. }
  18477. /// @brief returns an iterator to the reverse-beginning
  18478. /// @sa https://json.nlohmann.me/api/basic_json/rbegin/
  18479. reverse_iterator rbegin() noexcept
  18480. {
  18481. return reverse_iterator(end());
  18482. }
  18483. /// @brief returns an iterator to the reverse-beginning
  18484. /// @sa https://json.nlohmann.me/api/basic_json/rbegin/
  18485. const_reverse_iterator rbegin() const noexcept
  18486. {
  18487. return crbegin();
  18488. }
  18489. /// @brief returns an iterator to the reverse-end
  18490. /// @sa https://json.nlohmann.me/api/basic_json/rend/
  18491. reverse_iterator rend() noexcept
  18492. {
  18493. return reverse_iterator(begin());
  18494. }
  18495. /// @brief returns an iterator to the reverse-end
  18496. /// @sa https://json.nlohmann.me/api/basic_json/rend/
  18497. const_reverse_iterator rend() const noexcept
  18498. {
  18499. return crend();
  18500. }
  18501. /// @brief returns a const reverse iterator to the last element
  18502. /// @sa https://json.nlohmann.me/api/basic_json/crbegin/
  18503. const_reverse_iterator crbegin() const noexcept
  18504. {
  18505. return const_reverse_iterator(cend());
  18506. }
  18507. /// @brief returns a const reverse iterator to one before the first
  18508. /// @sa https://json.nlohmann.me/api/basic_json/crend/
  18509. const_reverse_iterator crend() const noexcept
  18510. {
  18511. return const_reverse_iterator(cbegin());
  18512. }
  18513. public:
  18514. /// @brief wrapper to access iterator member functions in range-based for
  18515. /// @sa https://json.nlohmann.me/api/basic_json/items/
  18516. /// @deprecated This function is deprecated since 3.1.0 and will be removed in
  18517. /// version 4.0.0 of the library. Please use @ref items() instead;
  18518. /// that is, replace `json::iterator_wrapper(j)` with `j.items()`.
  18519. JSON_HEDLEY_DEPRECATED_FOR(3.1.0, items())
  18520. static iteration_proxy<iterator> iterator_wrapper(reference ref) noexcept
  18521. {
  18522. return ref.items();
  18523. }
  18524. /// @brief wrapper to access iterator member functions in range-based for
  18525. /// @sa https://json.nlohmann.me/api/basic_json/items/
  18526. /// @deprecated This function is deprecated since 3.1.0 and will be removed in
  18527. /// version 4.0.0 of the library. Please use @ref items() instead;
  18528. /// that is, replace `json::iterator_wrapper(j)` with `j.items()`.
  18529. JSON_HEDLEY_DEPRECATED_FOR(3.1.0, items())
  18530. static iteration_proxy<const_iterator> iterator_wrapper(const_reference ref) noexcept
  18531. {
  18532. return ref.items();
  18533. }
  18534. /// @brief helper to access iterator member functions in range-based for
  18535. /// @sa https://json.nlohmann.me/api/basic_json/items/
  18536. iteration_proxy<iterator> items() noexcept
  18537. {
  18538. return iteration_proxy<iterator>(*this);
  18539. }
  18540. /// @brief helper to access iterator member functions in range-based for
  18541. /// @sa https://json.nlohmann.me/api/basic_json/items/
  18542. iteration_proxy<const_iterator> items() const noexcept
  18543. {
  18544. return iteration_proxy<const_iterator>(*this);
  18545. }
  18546. /// @}
  18547. //////////////
  18548. // capacity //
  18549. //////////////
  18550. /// @name capacity
  18551. /// @{
  18552. /// @brief checks whether the container is empty.
  18553. /// @sa https://json.nlohmann.me/api/basic_json/empty/
  18554. bool empty() const noexcept
  18555. {
  18556. switch (m_type)
  18557. {
  18558. case value_t::null:
  18559. {
  18560. // null values are empty
  18561. return true;
  18562. }
  18563. case value_t::array:
  18564. {
  18565. // delegate call to array_t::empty()
  18566. return m_value.array->empty();
  18567. }
  18568. case value_t::object:
  18569. {
  18570. // delegate call to object_t::empty()
  18571. return m_value.object->empty();
  18572. }
  18573. case value_t::string:
  18574. case value_t::boolean:
  18575. case value_t::number_integer:
  18576. case value_t::number_unsigned:
  18577. case value_t::number_float:
  18578. case value_t::binary:
  18579. case value_t::discarded:
  18580. default:
  18581. {
  18582. // all other types are nonempty
  18583. return false;
  18584. }
  18585. }
  18586. }
  18587. /// @brief returns the number of elements
  18588. /// @sa https://json.nlohmann.me/api/basic_json/size/
  18589. size_type size() const noexcept
  18590. {
  18591. switch (m_type)
  18592. {
  18593. case value_t::null:
  18594. {
  18595. // null values are empty
  18596. return 0;
  18597. }
  18598. case value_t::array:
  18599. {
  18600. // delegate call to array_t::size()
  18601. return m_value.array->size();
  18602. }
  18603. case value_t::object:
  18604. {
  18605. // delegate call to object_t::size()
  18606. return m_value.object->size();
  18607. }
  18608. case value_t::string:
  18609. case value_t::boolean:
  18610. case value_t::number_integer:
  18611. case value_t::number_unsigned:
  18612. case value_t::number_float:
  18613. case value_t::binary:
  18614. case value_t::discarded:
  18615. default:
  18616. {
  18617. // all other types have size 1
  18618. return 1;
  18619. }
  18620. }
  18621. }
  18622. /// @brief returns the maximum possible number of elements
  18623. /// @sa https://json.nlohmann.me/api/basic_json/max_size/
  18624. size_type max_size() const noexcept
  18625. {
  18626. switch (m_type)
  18627. {
  18628. case value_t::array:
  18629. {
  18630. // delegate call to array_t::max_size()
  18631. return m_value.array->max_size();
  18632. }
  18633. case value_t::object:
  18634. {
  18635. // delegate call to object_t::max_size()
  18636. return m_value.object->max_size();
  18637. }
  18638. case value_t::null:
  18639. case value_t::string:
  18640. case value_t::boolean:
  18641. case value_t::number_integer:
  18642. case value_t::number_unsigned:
  18643. case value_t::number_float:
  18644. case value_t::binary:
  18645. case value_t::discarded:
  18646. default:
  18647. {
  18648. // all other types have max_size() == size()
  18649. return size();
  18650. }
  18651. }
  18652. }
  18653. /// @}
  18654. ///////////////
  18655. // modifiers //
  18656. ///////////////
  18657. /// @name modifiers
  18658. /// @{
  18659. /// @brief clears the contents
  18660. /// @sa https://json.nlohmann.me/api/basic_json/clear/
  18661. void clear() noexcept
  18662. {
  18663. switch (m_type)
  18664. {
  18665. case value_t::number_integer:
  18666. {
  18667. m_value.number_integer = 0;
  18668. break;
  18669. }
  18670. case value_t::number_unsigned:
  18671. {
  18672. m_value.number_unsigned = 0;
  18673. break;
  18674. }
  18675. case value_t::number_float:
  18676. {
  18677. m_value.number_float = 0.0;
  18678. break;
  18679. }
  18680. case value_t::boolean:
  18681. {
  18682. m_value.boolean = false;
  18683. break;
  18684. }
  18685. case value_t::string:
  18686. {
  18687. m_value.string->clear();
  18688. break;
  18689. }
  18690. case value_t::binary:
  18691. {
  18692. m_value.binary->clear();
  18693. break;
  18694. }
  18695. case value_t::array:
  18696. {
  18697. m_value.array->clear();
  18698. break;
  18699. }
  18700. case value_t::object:
  18701. {
  18702. m_value.object->clear();
  18703. break;
  18704. }
  18705. case value_t::null:
  18706. case value_t::discarded:
  18707. default:
  18708. break;
  18709. }
  18710. }
  18711. /// @brief add an object to an array
  18712. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  18713. void push_back(basic_json&& val)
  18714. {
  18715. // push_back only works for null objects or arrays
  18716. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
  18717. {
  18718. JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
  18719. }
  18720. // transform null object into an array
  18721. if (is_null())
  18722. {
  18723. m_type = value_t::array;
  18724. m_value = value_t::array;
  18725. assert_invariant();
  18726. }
  18727. // add element to array (move semantics)
  18728. const auto old_capacity = m_value.array->capacity();
  18729. m_value.array->push_back(std::move(val));
  18730. set_parent(m_value.array->back(), old_capacity);
  18731. // if val is moved from, basic_json move constructor marks it null, so we do not call the destructor
  18732. }
  18733. /// @brief add an object to an array
  18734. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  18735. reference operator+=(basic_json&& val)
  18736. {
  18737. push_back(std::move(val));
  18738. return *this;
  18739. }
  18740. /// @brief add an object to an array
  18741. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  18742. void push_back(const basic_json& val)
  18743. {
  18744. // push_back only works for null objects or arrays
  18745. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
  18746. {
  18747. JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
  18748. }
  18749. // transform null object into an array
  18750. if (is_null())
  18751. {
  18752. m_type = value_t::array;
  18753. m_value = value_t::array;
  18754. assert_invariant();
  18755. }
  18756. // add element to array
  18757. const auto old_capacity = m_value.array->capacity();
  18758. m_value.array->push_back(val);
  18759. set_parent(m_value.array->back(), old_capacity);
  18760. }
  18761. /// @brief add an object to an array
  18762. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  18763. reference operator+=(const basic_json& val)
  18764. {
  18765. push_back(val);
  18766. return *this;
  18767. }
  18768. /// @brief add an object to an object
  18769. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  18770. void push_back(const typename object_t::value_type& val)
  18771. {
  18772. // push_back only works for null objects or objects
  18773. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object())))
  18774. {
  18775. JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
  18776. }
  18777. // transform null object into an object
  18778. if (is_null())
  18779. {
  18780. m_type = value_t::object;
  18781. m_value = value_t::object;
  18782. assert_invariant();
  18783. }
  18784. // add element to object
  18785. auto res = m_value.object->insert(val);
  18786. set_parent(res.first->second);
  18787. }
  18788. /// @brief add an object to an object
  18789. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  18790. reference operator+=(const typename object_t::value_type& val)
  18791. {
  18792. push_back(val);
  18793. return *this;
  18794. }
  18795. /// @brief add an object to an object
  18796. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  18797. void push_back(initializer_list_t init)
  18798. {
  18799. if (is_object() && init.size() == 2 && (*init.begin())->is_string())
  18800. {
  18801. basic_json&& key = init.begin()->moved_or_copied();
  18802. push_back(typename object_t::value_type(
  18803. std::move(key.get_ref<string_t&>()), (init.begin() + 1)->moved_or_copied()));
  18804. }
  18805. else
  18806. {
  18807. push_back(basic_json(init));
  18808. }
  18809. }
  18810. /// @brief add an object to an object
  18811. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  18812. reference operator+=(initializer_list_t init)
  18813. {
  18814. push_back(init);
  18815. return *this;
  18816. }
  18817. /// @brief add an object to an array
  18818. /// @sa https://json.nlohmann.me/api/basic_json/emplace_back/
  18819. template<class... Args>
  18820. reference emplace_back(Args&& ... args)
  18821. {
  18822. // emplace_back only works for null objects or arrays
  18823. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
  18824. {
  18825. JSON_THROW(type_error::create(311, detail::concat("cannot use emplace_back() with ", type_name()), this));
  18826. }
  18827. // transform null object into an array
  18828. if (is_null())
  18829. {
  18830. m_type = value_t::array;
  18831. m_value = value_t::array;
  18832. assert_invariant();
  18833. }
  18834. // add element to array (perfect forwarding)
  18835. const auto old_capacity = m_value.array->capacity();
  18836. m_value.array->emplace_back(std::forward<Args>(args)...);
  18837. return set_parent(m_value.array->back(), old_capacity);
  18838. }
  18839. /// @brief add an object to an object if key does not exist
  18840. /// @sa https://json.nlohmann.me/api/basic_json/emplace/
  18841. template<class... Args>
  18842. std::pair<iterator, bool> emplace(Args&& ... args)
  18843. {
  18844. // emplace only works for null objects or arrays
  18845. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object())))
  18846. {
  18847. JSON_THROW(type_error::create(311, detail::concat("cannot use emplace() with ", type_name()), this));
  18848. }
  18849. // transform null object into an object
  18850. if (is_null())
  18851. {
  18852. m_type = value_t::object;
  18853. m_value = value_t::object;
  18854. assert_invariant();
  18855. }
  18856. // add element to array (perfect forwarding)
  18857. auto res = m_value.object->emplace(std::forward<Args>(args)...);
  18858. set_parent(res.first->second);
  18859. // create result iterator and set iterator to the result of emplace
  18860. auto it = begin();
  18861. it.m_it.object_iterator = res.first;
  18862. // return pair of iterator and boolean
  18863. return {it, res.second};
  18864. }
  18865. /// Helper for insertion of an iterator
  18866. /// @note: This uses std::distance to support GCC 4.8,
  18867. /// see https://github.com/nlohmann/json/pull/1257
  18868. template<typename... Args>
  18869. iterator insert_iterator(const_iterator pos, Args&& ... args)
  18870. {
  18871. iterator result(this);
  18872. JSON_ASSERT(m_value.array != nullptr);
  18873. auto insert_pos = std::distance(m_value.array->begin(), pos.m_it.array_iterator);
  18874. m_value.array->insert(pos.m_it.array_iterator, std::forward<Args>(args)...);
  18875. result.m_it.array_iterator = m_value.array->begin() + insert_pos;
  18876. // This could have been written as:
  18877. // result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, cnt, val);
  18878. // but the return value of insert is missing in GCC 4.8, so it is written this way instead.
  18879. set_parents();
  18880. return result;
  18881. }
  18882. /// @brief inserts element into array
  18883. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  18884. iterator insert(const_iterator pos, const basic_json& val)
  18885. {
  18886. // insert only works for arrays
  18887. if (JSON_HEDLEY_LIKELY(is_array()))
  18888. {
  18889. // check if iterator pos fits to this JSON value
  18890. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  18891. {
  18892. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  18893. }
  18894. // insert to array and return iterator
  18895. return insert_iterator(pos, val);
  18896. }
  18897. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  18898. }
  18899. /// @brief inserts element into array
  18900. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  18901. iterator insert(const_iterator pos, basic_json&& val)
  18902. {
  18903. return insert(pos, val);
  18904. }
  18905. /// @brief inserts copies of element into array
  18906. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  18907. iterator insert(const_iterator pos, size_type cnt, const basic_json& val)
  18908. {
  18909. // insert only works for arrays
  18910. if (JSON_HEDLEY_LIKELY(is_array()))
  18911. {
  18912. // check if iterator pos fits to this JSON value
  18913. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  18914. {
  18915. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  18916. }
  18917. // insert to array and return iterator
  18918. return insert_iterator(pos, cnt, val);
  18919. }
  18920. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  18921. }
  18922. /// @brief inserts range of elements into array
  18923. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  18924. iterator insert(const_iterator pos, const_iterator first, const_iterator last)
  18925. {
  18926. // insert only works for arrays
  18927. if (JSON_HEDLEY_UNLIKELY(!is_array()))
  18928. {
  18929. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  18930. }
  18931. // check if iterator pos fits to this JSON value
  18932. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  18933. {
  18934. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  18935. }
  18936. // check if range iterators belong to the same JSON object
  18937. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  18938. {
  18939. JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
  18940. }
  18941. if (JSON_HEDLEY_UNLIKELY(first.m_object == this))
  18942. {
  18943. JSON_THROW(invalid_iterator::create(211, "passed iterators may not belong to container", this));
  18944. }
  18945. // insert to array and return iterator
  18946. return insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator);
  18947. }
  18948. /// @brief inserts elements from initializer list into array
  18949. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  18950. iterator insert(const_iterator pos, initializer_list_t ilist)
  18951. {
  18952. // insert only works for arrays
  18953. if (JSON_HEDLEY_UNLIKELY(!is_array()))
  18954. {
  18955. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  18956. }
  18957. // check if iterator pos fits to this JSON value
  18958. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  18959. {
  18960. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  18961. }
  18962. // insert to array and return iterator
  18963. return insert_iterator(pos, ilist.begin(), ilist.end());
  18964. }
  18965. /// @brief inserts range of elements into object
  18966. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  18967. void insert(const_iterator first, const_iterator last)
  18968. {
  18969. // insert only works for objects
  18970. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  18971. {
  18972. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  18973. }
  18974. // check if range iterators belong to the same JSON object
  18975. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  18976. {
  18977. JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
  18978. }
  18979. // passed iterators must belong to objects
  18980. if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()))
  18981. {
  18982. JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", this));
  18983. }
  18984. m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator);
  18985. }
  18986. /// @brief updates a JSON object from another object, overwriting existing keys
  18987. /// @sa https://json.nlohmann.me/api/basic_json/update/
  18988. void update(const_reference j, bool merge_objects = false)
  18989. {
  18990. update(j.begin(), j.end(), merge_objects);
  18991. }
  18992. /// @brief updates a JSON object from another object, overwriting existing keys
  18993. /// @sa https://json.nlohmann.me/api/basic_json/update/
  18994. void update(const_iterator first, const_iterator last, bool merge_objects = false)
  18995. {
  18996. // implicitly convert null value to an empty object
  18997. if (is_null())
  18998. {
  18999. m_type = value_t::object;
  19000. m_value.object = create<object_t>();
  19001. assert_invariant();
  19002. }
  19003. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  19004. {
  19005. JSON_THROW(type_error::create(312, detail::concat("cannot use update() with ", type_name()), this));
  19006. }
  19007. // check if range iterators belong to the same JSON object
  19008. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  19009. {
  19010. JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
  19011. }
  19012. // passed iterators must belong to objects
  19013. if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()))
  19014. {
  19015. JSON_THROW(type_error::create(312, detail::concat("cannot use update() with ", first.m_object->type_name()), first.m_object));
  19016. }
  19017. for (auto it = first; it != last; ++it)
  19018. {
  19019. if (merge_objects && it.value().is_object())
  19020. {
  19021. auto it2 = m_value.object->find(it.key());
  19022. if (it2 != m_value.object->end())
  19023. {
  19024. it2->second.update(it.value(), true);
  19025. continue;
  19026. }
  19027. }
  19028. m_value.object->operator[](it.key()) = it.value();
  19029. #if JSON_DIAGNOSTICS
  19030. m_value.object->operator[](it.key()).m_parent = this;
  19031. #endif
  19032. }
  19033. }
  19034. /// @brief exchanges the values
  19035. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19036. void swap(reference other) noexcept (
  19037. std::is_nothrow_move_constructible<value_t>::value&&
  19038. std::is_nothrow_move_assignable<value_t>::value&&
  19039. std::is_nothrow_move_constructible<json_value>::value&&
  19040. std::is_nothrow_move_assignable<json_value>::value
  19041. )
  19042. {
  19043. std::swap(m_type, other.m_type);
  19044. std::swap(m_value, other.m_value);
  19045. set_parents();
  19046. other.set_parents();
  19047. assert_invariant();
  19048. }
  19049. /// @brief exchanges the values
  19050. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19051. friend void swap(reference left, reference right) noexcept (
  19052. std::is_nothrow_move_constructible<value_t>::value&&
  19053. std::is_nothrow_move_assignable<value_t>::value&&
  19054. std::is_nothrow_move_constructible<json_value>::value&&
  19055. std::is_nothrow_move_assignable<json_value>::value
  19056. )
  19057. {
  19058. left.swap(right);
  19059. }
  19060. /// @brief exchanges the values
  19061. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19062. void swap(array_t& other) // NOLINT(bugprone-exception-escape)
  19063. {
  19064. // swap only works for arrays
  19065. if (JSON_HEDLEY_LIKELY(is_array()))
  19066. {
  19067. std::swap(*(m_value.array), other);
  19068. }
  19069. else
  19070. {
  19071. JSON_THROW(type_error::create(310, detail::concat("cannot use swap() with ", type_name()), this));
  19072. }
  19073. }
  19074. /// @brief exchanges the values
  19075. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19076. void swap(object_t& other) // NOLINT(bugprone-exception-escape)
  19077. {
  19078. // swap only works for objects
  19079. if (JSON_HEDLEY_LIKELY(is_object()))
  19080. {
  19081. std::swap(*(m_value.object), other);
  19082. }
  19083. else
  19084. {
  19085. JSON_THROW(type_error::create(310, detail::concat("cannot use swap() with ", type_name()), this));
  19086. }
  19087. }
  19088. /// @brief exchanges the values
  19089. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19090. void swap(string_t& other) // NOLINT(bugprone-exception-escape)
  19091. {
  19092. // swap only works for strings
  19093. if (JSON_HEDLEY_LIKELY(is_string()))
  19094. {
  19095. std::swap(*(m_value.string), other);
  19096. }
  19097. else
  19098. {
  19099. JSON_THROW(type_error::create(310, detail::concat("cannot use swap() with ", type_name()), this));
  19100. }
  19101. }
  19102. /// @brief exchanges the values
  19103. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19104. void swap(binary_t& other) // NOLINT(bugprone-exception-escape)
  19105. {
  19106. // swap only works for strings
  19107. if (JSON_HEDLEY_LIKELY(is_binary()))
  19108. {
  19109. std::swap(*(m_value.binary), other);
  19110. }
  19111. else
  19112. {
  19113. JSON_THROW(type_error::create(310, detail::concat("cannot use swap() with ", type_name()), this));
  19114. }
  19115. }
  19116. /// @brief exchanges the values
  19117. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  19118. void swap(typename binary_t::container_type& other) // NOLINT(bugprone-exception-escape)
  19119. {
  19120. // swap only works for strings
  19121. if (JSON_HEDLEY_LIKELY(is_binary()))
  19122. {
  19123. std::swap(*(m_value.binary), other);
  19124. }
  19125. else
  19126. {
  19127. JSON_THROW(type_error::create(310, detail::concat("cannot use swap() with ", type_name()), this));
  19128. }
  19129. }
  19130. /// @}
  19131. //////////////////////////////////////////
  19132. // lexicographical comparison operators //
  19133. //////////////////////////////////////////
  19134. /// @name lexicographical comparison operators
  19135. /// @{
  19136. // note parentheses around operands are necessary; see
  19137. // https://github.com/nlohmann/json/issues/1530
  19138. #define JSON_IMPLEMENT_OPERATOR(op, null_result, unordered_result, default_result) \
  19139. const auto lhs_type = lhs.type(); \
  19140. const auto rhs_type = rhs.type(); \
  19141. \
  19142. if (lhs_type == rhs_type) /* NOLINT(readability/braces) */ \
  19143. { \
  19144. switch (lhs_type) \
  19145. { \
  19146. case value_t::array: \
  19147. return (*lhs.m_value.array) op (*rhs.m_value.array); \
  19148. \
  19149. case value_t::object: \
  19150. return (*lhs.m_value.object) op (*rhs.m_value.object); \
  19151. \
  19152. case value_t::null: \
  19153. return (null_result); \
  19154. \
  19155. case value_t::string: \
  19156. return (*lhs.m_value.string) op (*rhs.m_value.string); \
  19157. \
  19158. case value_t::boolean: \
  19159. return (lhs.m_value.boolean) op (rhs.m_value.boolean); \
  19160. \
  19161. case value_t::number_integer: \
  19162. return (lhs.m_value.number_integer) op (rhs.m_value.number_integer); \
  19163. \
  19164. case value_t::number_unsigned: \
  19165. return (lhs.m_value.number_unsigned) op (rhs.m_value.number_unsigned); \
  19166. \
  19167. case value_t::number_float: \
  19168. return (lhs.m_value.number_float) op (rhs.m_value.number_float); \
  19169. \
  19170. case value_t::binary: \
  19171. return (*lhs.m_value.binary) op (*rhs.m_value.binary); \
  19172. \
  19173. case value_t::discarded: \
  19174. default: \
  19175. return (unordered_result); \
  19176. } \
  19177. } \
  19178. else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_float) \
  19179. { \
  19180. return static_cast<number_float_t>(lhs.m_value.number_integer) op rhs.m_value.number_float; \
  19181. } \
  19182. else if (lhs_type == value_t::number_float && rhs_type == value_t::number_integer) \
  19183. { \
  19184. return lhs.m_value.number_float op static_cast<number_float_t>(rhs.m_value.number_integer); \
  19185. } \
  19186. else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_float) \
  19187. { \
  19188. return static_cast<number_float_t>(lhs.m_value.number_unsigned) op rhs.m_value.number_float; \
  19189. } \
  19190. else if (lhs_type == value_t::number_float && rhs_type == value_t::number_unsigned) \
  19191. { \
  19192. return lhs.m_value.number_float op static_cast<number_float_t>(rhs.m_value.number_unsigned); \
  19193. } \
  19194. else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_integer) \
  19195. { \
  19196. return static_cast<number_integer_t>(lhs.m_value.number_unsigned) op rhs.m_value.number_integer; \
  19197. } \
  19198. else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_unsigned) \
  19199. { \
  19200. return lhs.m_value.number_integer op static_cast<number_integer_t>(rhs.m_value.number_unsigned); \
  19201. } \
  19202. else if(compares_unordered(lhs, rhs))\
  19203. {\
  19204. return (unordered_result);\
  19205. }\
  19206. \
  19207. return (default_result);
  19208. JSON_PRIVATE_UNLESS_TESTED:
  19209. // returns true if:
  19210. // - any operand is NaN and the other operand is of number type
  19211. // - any operand is discarded
  19212. // in legacy mode, discarded values are considered ordered if
  19213. // an operation is computed as an odd number of inverses of others
  19214. static bool compares_unordered(const_reference lhs, const_reference rhs, bool inverse = false) noexcept
  19215. {
  19216. if ((lhs.is_number_float() && std::isnan(lhs.m_value.number_float) && rhs.is_number())
  19217. || (rhs.is_number_float() && std::isnan(rhs.m_value.number_float) && lhs.is_number()))
  19218. {
  19219. return true;
  19220. }
  19221. #if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  19222. return (lhs.is_discarded() || rhs.is_discarded()) && !inverse;
  19223. #else
  19224. static_cast<void>(inverse);
  19225. return lhs.is_discarded() || rhs.is_discarded();
  19226. #endif
  19227. }
  19228. private:
  19229. bool compares_unordered(const_reference rhs, bool inverse = false) const noexcept
  19230. {
  19231. return compares_unordered(*this, rhs, inverse);
  19232. }
  19233. public:
  19234. #if JSON_HAS_THREE_WAY_COMPARISON
  19235. /// @brief comparison: equal
  19236. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  19237. bool operator==(const_reference rhs) const noexcept
  19238. {
  19239. #ifdef __GNUC__
  19240. #pragma GCC diagnostic push
  19241. #pragma GCC diagnostic ignored "-Wfloat-equal"
  19242. #endif
  19243. const_reference lhs = *this;
  19244. JSON_IMPLEMENT_OPERATOR( ==, true, false, false)
  19245. #ifdef __GNUC__
  19246. #pragma GCC diagnostic pop
  19247. #endif
  19248. }
  19249. /// @brief comparison: equal
  19250. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  19251. template<typename ScalarType>
  19252. requires std::is_scalar_v<ScalarType>
  19253. bool operator==(ScalarType rhs) const noexcept
  19254. {
  19255. return *this == basic_json(rhs);
  19256. }
  19257. /// @brief comparison: not equal
  19258. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  19259. bool operator!=(const_reference rhs) const noexcept
  19260. {
  19261. if (compares_unordered(rhs, true))
  19262. {
  19263. return false;
  19264. }
  19265. return !operator==(rhs);
  19266. }
  19267. /// @brief comparison: 3-way
  19268. /// @sa https://json.nlohmann.me/api/basic_json/operator_spaceship/
  19269. std::partial_ordering operator<=>(const_reference rhs) const noexcept // *NOPAD*
  19270. {
  19271. const_reference lhs = *this;
  19272. // default_result is used if we cannot compare values. In that case,
  19273. // we compare types.
  19274. JSON_IMPLEMENT_OPERATOR(<=>, // *NOPAD*
  19275. std::partial_ordering::equivalent,
  19276. std::partial_ordering::unordered,
  19277. lhs_type <=> rhs_type) // *NOPAD*
  19278. }
  19279. /// @brief comparison: 3-way
  19280. /// @sa https://json.nlohmann.me/api/basic_json/operator_spaceship/
  19281. template<typename ScalarType>
  19282. requires std::is_scalar_v<ScalarType>
  19283. std::partial_ordering operator<=>(ScalarType rhs) const noexcept // *NOPAD*
  19284. {
  19285. return *this <=> basic_json(rhs); // *NOPAD*
  19286. }
  19287. #if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  19288. // all operators that are computed as an odd number of inverses of others
  19289. // need to be overloaded to emulate the legacy comparison behavior
  19290. /// @brief comparison: less than or equal
  19291. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  19292. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON)
  19293. bool operator<=(const_reference rhs) const noexcept
  19294. {
  19295. if (compares_unordered(rhs, true))
  19296. {
  19297. return false;
  19298. }
  19299. return !(rhs < *this);
  19300. }
  19301. /// @brief comparison: less than or equal
  19302. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  19303. template<typename ScalarType>
  19304. requires std::is_scalar_v<ScalarType>
  19305. bool operator<=(ScalarType rhs) const noexcept
  19306. {
  19307. return *this <= basic_json(rhs);
  19308. }
  19309. /// @brief comparison: greater than or equal
  19310. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  19311. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON)
  19312. bool operator>=(const_reference rhs) const noexcept
  19313. {
  19314. if (compares_unordered(rhs, true))
  19315. {
  19316. return false;
  19317. }
  19318. return !(*this < rhs);
  19319. }
  19320. /// @brief comparison: greater than or equal
  19321. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  19322. template<typename ScalarType>
  19323. requires std::is_scalar_v<ScalarType>
  19324. bool operator>=(ScalarType rhs) const noexcept
  19325. {
  19326. return *this >= basic_json(rhs);
  19327. }
  19328. #endif
  19329. #else
  19330. /// @brief comparison: equal
  19331. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  19332. friend bool operator==(const_reference lhs, const_reference rhs) noexcept
  19333. {
  19334. #ifdef __GNUC__
  19335. #pragma GCC diagnostic push
  19336. #pragma GCC diagnostic ignored "-Wfloat-equal"
  19337. #endif
  19338. JSON_IMPLEMENT_OPERATOR( ==, true, false, false)
  19339. #ifdef __GNUC__
  19340. #pragma GCC diagnostic pop
  19341. #endif
  19342. }
  19343. /// @brief comparison: equal
  19344. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  19345. template<typename ScalarType, typename std::enable_if<
  19346. std::is_scalar<ScalarType>::value, int>::type = 0>
  19347. friend bool operator==(const_reference lhs, ScalarType rhs) noexcept
  19348. {
  19349. return lhs == basic_json(rhs);
  19350. }
  19351. /// @brief comparison: equal
  19352. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  19353. template<typename ScalarType, typename std::enable_if<
  19354. std::is_scalar<ScalarType>::value, int>::type = 0>
  19355. friend bool operator==(ScalarType lhs, const_reference rhs) noexcept
  19356. {
  19357. return basic_json(lhs) == rhs;
  19358. }
  19359. /// @brief comparison: not equal
  19360. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  19361. friend bool operator!=(const_reference lhs, const_reference rhs) noexcept
  19362. {
  19363. if (compares_unordered(lhs, rhs, true))
  19364. {
  19365. return false;
  19366. }
  19367. return !(lhs == rhs);
  19368. }
  19369. /// @brief comparison: not equal
  19370. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  19371. template<typename ScalarType, typename std::enable_if<
  19372. std::is_scalar<ScalarType>::value, int>::type = 0>
  19373. friend bool operator!=(const_reference lhs, ScalarType rhs) noexcept
  19374. {
  19375. return lhs != basic_json(rhs);
  19376. }
  19377. /// @brief comparison: not equal
  19378. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  19379. template<typename ScalarType, typename std::enable_if<
  19380. std::is_scalar<ScalarType>::value, int>::type = 0>
  19381. friend bool operator!=(ScalarType lhs, const_reference rhs) noexcept
  19382. {
  19383. return basic_json(lhs) != rhs;
  19384. }
  19385. /// @brief comparison: less than
  19386. /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
  19387. friend bool operator<(const_reference lhs, const_reference rhs) noexcept
  19388. {
  19389. // default_result is used if we cannot compare values. In that case,
  19390. // we compare types. Note we have to call the operator explicitly,
  19391. // because MSVC has problems otherwise.
  19392. JSON_IMPLEMENT_OPERATOR( <, false, false, operator<(lhs_type, rhs_type))
  19393. }
  19394. /// @brief comparison: less than
  19395. /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
  19396. template<typename ScalarType, typename std::enable_if<
  19397. std::is_scalar<ScalarType>::value, int>::type = 0>
  19398. friend bool operator<(const_reference lhs, ScalarType rhs) noexcept
  19399. {
  19400. return lhs < basic_json(rhs);
  19401. }
  19402. /// @brief comparison: less than
  19403. /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
  19404. template<typename ScalarType, typename std::enable_if<
  19405. std::is_scalar<ScalarType>::value, int>::type = 0>
  19406. friend bool operator<(ScalarType lhs, const_reference rhs) noexcept
  19407. {
  19408. return basic_json(lhs) < rhs;
  19409. }
  19410. /// @brief comparison: less than or equal
  19411. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  19412. friend bool operator<=(const_reference lhs, const_reference rhs) noexcept
  19413. {
  19414. if (compares_unordered(lhs, rhs, true))
  19415. {
  19416. return false;
  19417. }
  19418. return !(rhs < lhs);
  19419. }
  19420. /// @brief comparison: less than or equal
  19421. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  19422. template<typename ScalarType, typename std::enable_if<
  19423. std::is_scalar<ScalarType>::value, int>::type = 0>
  19424. friend bool operator<=(const_reference lhs, ScalarType rhs) noexcept
  19425. {
  19426. return lhs <= basic_json(rhs);
  19427. }
  19428. /// @brief comparison: less than or equal
  19429. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  19430. template<typename ScalarType, typename std::enable_if<
  19431. std::is_scalar<ScalarType>::value, int>::type = 0>
  19432. friend bool operator<=(ScalarType lhs, const_reference rhs) noexcept
  19433. {
  19434. return basic_json(lhs) <= rhs;
  19435. }
  19436. /// @brief comparison: greater than
  19437. /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
  19438. friend bool operator>(const_reference lhs, const_reference rhs) noexcept
  19439. {
  19440. // double inverse
  19441. if (compares_unordered(lhs, rhs))
  19442. {
  19443. return false;
  19444. }
  19445. return !(lhs <= rhs);
  19446. }
  19447. /// @brief comparison: greater than
  19448. /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
  19449. template<typename ScalarType, typename std::enable_if<
  19450. std::is_scalar<ScalarType>::value, int>::type = 0>
  19451. friend bool operator>(const_reference lhs, ScalarType rhs) noexcept
  19452. {
  19453. return lhs > basic_json(rhs);
  19454. }
  19455. /// @brief comparison: greater than
  19456. /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
  19457. template<typename ScalarType, typename std::enable_if<
  19458. std::is_scalar<ScalarType>::value, int>::type = 0>
  19459. friend bool operator>(ScalarType lhs, const_reference rhs) noexcept
  19460. {
  19461. return basic_json(lhs) > rhs;
  19462. }
  19463. /// @brief comparison: greater than or equal
  19464. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  19465. friend bool operator>=(const_reference lhs, const_reference rhs) noexcept
  19466. {
  19467. if (compares_unordered(lhs, rhs, true))
  19468. {
  19469. return false;
  19470. }
  19471. return !(lhs < rhs);
  19472. }
  19473. /// @brief comparison: greater than or equal
  19474. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  19475. template<typename ScalarType, typename std::enable_if<
  19476. std::is_scalar<ScalarType>::value, int>::type = 0>
  19477. friend bool operator>=(const_reference lhs, ScalarType rhs) noexcept
  19478. {
  19479. return lhs >= basic_json(rhs);
  19480. }
  19481. /// @brief comparison: greater than or equal
  19482. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  19483. template<typename ScalarType, typename std::enable_if<
  19484. std::is_scalar<ScalarType>::value, int>::type = 0>
  19485. friend bool operator>=(ScalarType lhs, const_reference rhs) noexcept
  19486. {
  19487. return basic_json(lhs) >= rhs;
  19488. }
  19489. #endif
  19490. #undef JSON_IMPLEMENT_OPERATOR
  19491. /// @}
  19492. ///////////////////
  19493. // serialization //
  19494. ///////////////////
  19495. /// @name serialization
  19496. /// @{
  19497. #ifndef JSON_NO_IO
  19498. /// @brief serialize to stream
  19499. /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
  19500. friend std::ostream& operator<<(std::ostream& o, const basic_json& j)
  19501. {
  19502. // read width member and use it as indentation parameter if nonzero
  19503. const bool pretty_print = o.width() > 0;
  19504. const auto indentation = pretty_print ? o.width() : 0;
  19505. // reset width to 0 for subsequent calls to this stream
  19506. o.width(0);
  19507. // do the actual serialization
  19508. serializer s(detail::output_adapter<char>(o), o.fill());
  19509. s.dump(j, pretty_print, false, static_cast<unsigned int>(indentation));
  19510. return o;
  19511. }
  19512. /// @brief serialize to stream
  19513. /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
  19514. /// @deprecated This function is deprecated since 3.0.0 and will be removed in
  19515. /// version 4.0.0 of the library. Please use
  19516. /// operator<<(std::ostream&, const basic_json&) instead; that is,
  19517. /// replace calls like `j >> o;` with `o << j;`.
  19518. JSON_HEDLEY_DEPRECATED_FOR(3.0.0, operator<<(std::ostream&, const basic_json&))
  19519. friend std::ostream& operator>>(const basic_json& j, std::ostream& o)
  19520. {
  19521. return o << j;
  19522. }
  19523. #endif // JSON_NO_IO
  19524. /// @}
  19525. /////////////////////
  19526. // deserialization //
  19527. /////////////////////
  19528. /// @name deserialization
  19529. /// @{
  19530. /// @brief deserialize from a compatible input
  19531. /// @sa https://json.nlohmann.me/api/basic_json/parse/
  19532. template<typename InputType>
  19533. JSON_HEDLEY_WARN_UNUSED_RESULT
  19534. static basic_json parse(InputType&& i,
  19535. const parser_callback_t cb = nullptr,
  19536. const bool allow_exceptions = true,
  19537. const bool ignore_comments = false)
  19538. {
  19539. basic_json result;
  19540. parser(detail::input_adapter(std::forward<InputType>(i)), cb, allow_exceptions, ignore_comments).parse(true, result);
  19541. return result;
  19542. }
  19543. /// @brief deserialize from a pair of character iterators
  19544. /// @sa https://json.nlohmann.me/api/basic_json/parse/
  19545. template<typename IteratorType>
  19546. JSON_HEDLEY_WARN_UNUSED_RESULT
  19547. static basic_json parse(IteratorType first,
  19548. IteratorType last,
  19549. const parser_callback_t cb = nullptr,
  19550. const bool allow_exceptions = true,
  19551. const bool ignore_comments = false)
  19552. {
  19553. basic_json result;
  19554. parser(detail::input_adapter(std::move(first), std::move(last)), cb, allow_exceptions, ignore_comments).parse(true, result);
  19555. return result;
  19556. }
  19557. JSON_HEDLEY_WARN_UNUSED_RESULT
  19558. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, parse(ptr, ptr + len))
  19559. static basic_json parse(detail::span_input_adapter&& i,
  19560. const parser_callback_t cb = nullptr,
  19561. const bool allow_exceptions = true,
  19562. const bool ignore_comments = false)
  19563. {
  19564. basic_json result;
  19565. parser(i.get(), cb, allow_exceptions, ignore_comments).parse(true, result);
  19566. return result;
  19567. }
  19568. /// @brief check if the input is valid JSON
  19569. /// @sa https://json.nlohmann.me/api/basic_json/accept/
  19570. template<typename InputType>
  19571. static bool accept(InputType&& i,
  19572. const bool ignore_comments = false)
  19573. {
  19574. return parser(detail::input_adapter(std::forward<InputType>(i)), nullptr, false, ignore_comments).accept(true);
  19575. }
  19576. /// @brief check if the input is valid JSON
  19577. /// @sa https://json.nlohmann.me/api/basic_json/accept/
  19578. template<typename IteratorType>
  19579. static bool accept(IteratorType first, IteratorType last,
  19580. const bool ignore_comments = false)
  19581. {
  19582. return parser(detail::input_adapter(std::move(first), std::move(last)), nullptr, false, ignore_comments).accept(true);
  19583. }
  19584. JSON_HEDLEY_WARN_UNUSED_RESULT
  19585. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, accept(ptr, ptr + len))
  19586. static bool accept(detail::span_input_adapter&& i,
  19587. const bool ignore_comments = false)
  19588. {
  19589. return parser(i.get(), nullptr, false, ignore_comments).accept(true);
  19590. }
  19591. /// @brief generate SAX events
  19592. /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
  19593. template <typename InputType, typename SAX>
  19594. JSON_HEDLEY_NON_NULL(2)
  19595. static bool sax_parse(InputType&& i, SAX* sax,
  19596. input_format_t format = input_format_t::json,
  19597. const bool strict = true,
  19598. const bool ignore_comments = false)
  19599. {
  19600. auto ia = detail::input_adapter(std::forward<InputType>(i));
  19601. return format == input_format_t::json
  19602. ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
  19603. : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
  19604. }
  19605. /// @brief generate SAX events
  19606. /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
  19607. template<class IteratorType, class SAX>
  19608. JSON_HEDLEY_NON_NULL(3)
  19609. static bool sax_parse(IteratorType first, IteratorType last, SAX* sax,
  19610. input_format_t format = input_format_t::json,
  19611. const bool strict = true,
  19612. const bool ignore_comments = false)
  19613. {
  19614. auto ia = detail::input_adapter(std::move(first), std::move(last));
  19615. return format == input_format_t::json
  19616. ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
  19617. : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
  19618. }
  19619. /// @brief generate SAX events
  19620. /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
  19621. /// @deprecated This function is deprecated since 3.8.0 and will be removed in
  19622. /// version 4.0.0 of the library. Please use
  19623. /// sax_parse(ptr, ptr + len) instead.
  19624. template <typename SAX>
  19625. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, sax_parse(ptr, ptr + len, ...))
  19626. JSON_HEDLEY_NON_NULL(2)
  19627. static bool sax_parse(detail::span_input_adapter&& i, SAX* sax,
  19628. input_format_t format = input_format_t::json,
  19629. const bool strict = true,
  19630. const bool ignore_comments = false)
  19631. {
  19632. auto ia = i.get();
  19633. return format == input_format_t::json
  19634. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  19635. ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
  19636. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  19637. : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
  19638. }
  19639. #ifndef JSON_NO_IO
  19640. /// @brief deserialize from stream
  19641. /// @sa https://json.nlohmann.me/api/basic_json/operator_gtgt/
  19642. /// @deprecated This stream operator is deprecated since 3.0.0 and will be removed in
  19643. /// version 4.0.0 of the library. Please use
  19644. /// operator>>(std::istream&, basic_json&) instead; that is,
  19645. /// replace calls like `j << i;` with `i >> j;`.
  19646. JSON_HEDLEY_DEPRECATED_FOR(3.0.0, operator>>(std::istream&, basic_json&))
  19647. friend std::istream& operator<<(basic_json& j, std::istream& i)
  19648. {
  19649. return operator>>(i, j);
  19650. }
  19651. /// @brief deserialize from stream
  19652. /// @sa https://json.nlohmann.me/api/basic_json/operator_gtgt/
  19653. friend std::istream& operator>>(std::istream& i, basic_json& j)
  19654. {
  19655. parser(detail::input_adapter(i)).parse(false, j);
  19656. return i;
  19657. }
  19658. #endif // JSON_NO_IO
  19659. /// @}
  19660. ///////////////////////////
  19661. // convenience functions //
  19662. ///////////////////////////
  19663. /// @brief return the type as string
  19664. /// @sa https://json.nlohmann.me/api/basic_json/type_name/
  19665. JSON_HEDLEY_RETURNS_NON_NULL
  19666. const char* type_name() const noexcept
  19667. {
  19668. switch (m_type)
  19669. {
  19670. case value_t::null:
  19671. return "null";
  19672. case value_t::object:
  19673. return "object";
  19674. case value_t::array:
  19675. return "array";
  19676. case value_t::string:
  19677. return "string";
  19678. case value_t::boolean:
  19679. return "boolean";
  19680. case value_t::binary:
  19681. return "binary";
  19682. case value_t::discarded:
  19683. return "discarded";
  19684. case value_t::number_integer:
  19685. case value_t::number_unsigned:
  19686. case value_t::number_float:
  19687. default:
  19688. return "number";
  19689. }
  19690. }
  19691. JSON_PRIVATE_UNLESS_TESTED:
  19692. //////////////////////
  19693. // member variables //
  19694. //////////////////////
  19695. /// the type of the current element
  19696. value_t m_type = value_t::null;
  19697. /// the value of the current element
  19698. json_value m_value = {};
  19699. #if JSON_DIAGNOSTICS
  19700. /// a pointer to a parent value (for debugging purposes)
  19701. basic_json* m_parent = nullptr;
  19702. #endif
  19703. //////////////////////////////////////////
  19704. // binary serialization/deserialization //
  19705. //////////////////////////////////////////
  19706. /// @name binary serialization/deserialization support
  19707. /// @{
  19708. public:
  19709. /// @brief create a CBOR serialization of a given JSON value
  19710. /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
  19711. static std::vector<std::uint8_t> to_cbor(const basic_json& j)
  19712. {
  19713. std::vector<std::uint8_t> result;
  19714. to_cbor(j, result);
  19715. return result;
  19716. }
  19717. /// @brief create a CBOR serialization of a given JSON value
  19718. /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
  19719. static void to_cbor(const basic_json& j, detail::output_adapter<std::uint8_t> o)
  19720. {
  19721. binary_writer<std::uint8_t>(o).write_cbor(j);
  19722. }
  19723. /// @brief create a CBOR serialization of a given JSON value
  19724. /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
  19725. static void to_cbor(const basic_json& j, detail::output_adapter<char> o)
  19726. {
  19727. binary_writer<char>(o).write_cbor(j);
  19728. }
  19729. /// @brief create a MessagePack serialization of a given JSON value
  19730. /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
  19731. static std::vector<std::uint8_t> to_msgpack(const basic_json& j)
  19732. {
  19733. std::vector<std::uint8_t> result;
  19734. to_msgpack(j, result);
  19735. return result;
  19736. }
  19737. /// @brief create a MessagePack serialization of a given JSON value
  19738. /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
  19739. static void to_msgpack(const basic_json& j, detail::output_adapter<std::uint8_t> o)
  19740. {
  19741. binary_writer<std::uint8_t>(o).write_msgpack(j);
  19742. }
  19743. /// @brief create a MessagePack serialization of a given JSON value
  19744. /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
  19745. static void to_msgpack(const basic_json& j, detail::output_adapter<char> o)
  19746. {
  19747. binary_writer<char>(o).write_msgpack(j);
  19748. }
  19749. /// @brief create a UBJSON serialization of a given JSON value
  19750. /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
  19751. static std::vector<std::uint8_t> to_ubjson(const basic_json& j,
  19752. const bool use_size = false,
  19753. const bool use_type = false)
  19754. {
  19755. std::vector<std::uint8_t> result;
  19756. to_ubjson(j, result, use_size, use_type);
  19757. return result;
  19758. }
  19759. /// @brief create a UBJSON serialization of a given JSON value
  19760. /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
  19761. static void to_ubjson(const basic_json& j, detail::output_adapter<std::uint8_t> o,
  19762. const bool use_size = false, const bool use_type = false)
  19763. {
  19764. binary_writer<std::uint8_t>(o).write_ubjson(j, use_size, use_type);
  19765. }
  19766. /// @brief create a UBJSON serialization of a given JSON value
  19767. /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
  19768. static void to_ubjson(const basic_json& j, detail::output_adapter<char> o,
  19769. const bool use_size = false, const bool use_type = false)
  19770. {
  19771. binary_writer<char>(o).write_ubjson(j, use_size, use_type);
  19772. }
  19773. /// @brief create a BJData serialization of a given JSON value
  19774. /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
  19775. static std::vector<std::uint8_t> to_bjdata(const basic_json& j,
  19776. const bool use_size = false,
  19777. const bool use_type = false)
  19778. {
  19779. std::vector<std::uint8_t> result;
  19780. to_bjdata(j, result, use_size, use_type);
  19781. return result;
  19782. }
  19783. /// @brief create a BJData serialization of a given JSON value
  19784. /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
  19785. static void to_bjdata(const basic_json& j, detail::output_adapter<std::uint8_t> o,
  19786. const bool use_size = false, const bool use_type = false)
  19787. {
  19788. binary_writer<std::uint8_t>(o).write_ubjson(j, use_size, use_type, true, true);
  19789. }
  19790. /// @brief create a BJData serialization of a given JSON value
  19791. /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
  19792. static void to_bjdata(const basic_json& j, detail::output_adapter<char> o,
  19793. const bool use_size = false, const bool use_type = false)
  19794. {
  19795. binary_writer<char>(o).write_ubjson(j, use_size, use_type, true, true);
  19796. }
  19797. /// @brief create a BSON serialization of a given JSON value
  19798. /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
  19799. static std::vector<std::uint8_t> to_bson(const basic_json& j)
  19800. {
  19801. std::vector<std::uint8_t> result;
  19802. to_bson(j, result);
  19803. return result;
  19804. }
  19805. /// @brief create a BSON serialization of a given JSON value
  19806. /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
  19807. static void to_bson(const basic_json& j, detail::output_adapter<std::uint8_t> o)
  19808. {
  19809. binary_writer<std::uint8_t>(o).write_bson(j);
  19810. }
  19811. /// @brief create a BSON serialization of a given JSON value
  19812. /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
  19813. static void to_bson(const basic_json& j, detail::output_adapter<char> o)
  19814. {
  19815. binary_writer<char>(o).write_bson(j);
  19816. }
  19817. /// @brief create a JSON value from an input in CBOR format
  19818. /// @sa https://json.nlohmann.me/api/basic_json/from_cbor/
  19819. template<typename InputType>
  19820. JSON_HEDLEY_WARN_UNUSED_RESULT
  19821. static basic_json from_cbor(InputType&& i,
  19822. const bool strict = true,
  19823. const bool allow_exceptions = true,
  19824. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  19825. {
  19826. basic_json result;
  19827. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  19828. auto ia = detail::input_adapter(std::forward<InputType>(i));
  19829. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler);
  19830. return res ? result : basic_json(value_t::discarded);
  19831. }
  19832. /// @brief create a JSON value from an input in CBOR format
  19833. /// @sa https://json.nlohmann.me/api/basic_json/from_cbor/
  19834. template<typename IteratorType>
  19835. JSON_HEDLEY_WARN_UNUSED_RESULT
  19836. static basic_json from_cbor(IteratorType first, IteratorType last,
  19837. const bool strict = true,
  19838. const bool allow_exceptions = true,
  19839. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  19840. {
  19841. basic_json result;
  19842. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  19843. auto ia = detail::input_adapter(std::move(first), std::move(last));
  19844. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler);
  19845. return res ? result : basic_json(value_t::discarded);
  19846. }
  19847. template<typename T>
  19848. JSON_HEDLEY_WARN_UNUSED_RESULT
  19849. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_cbor(ptr, ptr + len))
  19850. static basic_json from_cbor(const T* ptr, std::size_t len,
  19851. const bool strict = true,
  19852. const bool allow_exceptions = true,
  19853. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  19854. {
  19855. return from_cbor(ptr, ptr + len, strict, allow_exceptions, tag_handler);
  19856. }
  19857. JSON_HEDLEY_WARN_UNUSED_RESULT
  19858. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_cbor(ptr, ptr + len))
  19859. static basic_json from_cbor(detail::span_input_adapter&& i,
  19860. const bool strict = true,
  19861. const bool allow_exceptions = true,
  19862. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  19863. {
  19864. basic_json result;
  19865. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  19866. auto ia = i.get();
  19867. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  19868. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler);
  19869. return res ? result : basic_json(value_t::discarded);
  19870. }
  19871. /// @brief create a JSON value from an input in MessagePack format
  19872. /// @sa https://json.nlohmann.me/api/basic_json/from_msgpack/
  19873. template<typename InputType>
  19874. JSON_HEDLEY_WARN_UNUSED_RESULT
  19875. static basic_json from_msgpack(InputType&& i,
  19876. const bool strict = true,
  19877. const bool allow_exceptions = true)
  19878. {
  19879. basic_json result;
  19880. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  19881. auto ia = detail::input_adapter(std::forward<InputType>(i));
  19882. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict);
  19883. return res ? result : basic_json(value_t::discarded);
  19884. }
  19885. /// @brief create a JSON value from an input in MessagePack format
  19886. /// @sa https://json.nlohmann.me/api/basic_json/from_msgpack/
  19887. template<typename IteratorType>
  19888. JSON_HEDLEY_WARN_UNUSED_RESULT
  19889. static basic_json from_msgpack(IteratorType first, IteratorType last,
  19890. const bool strict = true,
  19891. const bool allow_exceptions = true)
  19892. {
  19893. basic_json result;
  19894. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  19895. auto ia = detail::input_adapter(std::move(first), std::move(last));
  19896. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict);
  19897. return res ? result : basic_json(value_t::discarded);
  19898. }
  19899. template<typename T>
  19900. JSON_HEDLEY_WARN_UNUSED_RESULT
  19901. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_msgpack(ptr, ptr + len))
  19902. static basic_json from_msgpack(const T* ptr, std::size_t len,
  19903. const bool strict = true,
  19904. const bool allow_exceptions = true)
  19905. {
  19906. return from_msgpack(ptr, ptr + len, strict, allow_exceptions);
  19907. }
  19908. JSON_HEDLEY_WARN_UNUSED_RESULT
  19909. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_msgpack(ptr, ptr + len))
  19910. static basic_json from_msgpack(detail::span_input_adapter&& i,
  19911. const bool strict = true,
  19912. const bool allow_exceptions = true)
  19913. {
  19914. basic_json result;
  19915. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  19916. auto ia = i.get();
  19917. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  19918. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict);
  19919. return res ? result : basic_json(value_t::discarded);
  19920. }
  19921. /// @brief create a JSON value from an input in UBJSON format
  19922. /// @sa https://json.nlohmann.me/api/basic_json/from_ubjson/
  19923. template<typename InputType>
  19924. JSON_HEDLEY_WARN_UNUSED_RESULT
  19925. static basic_json from_ubjson(InputType&& i,
  19926. const bool strict = true,
  19927. const bool allow_exceptions = true)
  19928. {
  19929. basic_json result;
  19930. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  19931. auto ia = detail::input_adapter(std::forward<InputType>(i));
  19932. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict);
  19933. return res ? result : basic_json(value_t::discarded);
  19934. }
  19935. /// @brief create a JSON value from an input in UBJSON format
  19936. /// @sa https://json.nlohmann.me/api/basic_json/from_ubjson/
  19937. template<typename IteratorType>
  19938. JSON_HEDLEY_WARN_UNUSED_RESULT
  19939. static basic_json from_ubjson(IteratorType first, IteratorType last,
  19940. const bool strict = true,
  19941. const bool allow_exceptions = true)
  19942. {
  19943. basic_json result;
  19944. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  19945. auto ia = detail::input_adapter(std::move(first), std::move(last));
  19946. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict);
  19947. return res ? result : basic_json(value_t::discarded);
  19948. }
  19949. template<typename T>
  19950. JSON_HEDLEY_WARN_UNUSED_RESULT
  19951. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_ubjson(ptr, ptr + len))
  19952. static basic_json from_ubjson(const T* ptr, std::size_t len,
  19953. const bool strict = true,
  19954. const bool allow_exceptions = true)
  19955. {
  19956. return from_ubjson(ptr, ptr + len, strict, allow_exceptions);
  19957. }
  19958. JSON_HEDLEY_WARN_UNUSED_RESULT
  19959. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_ubjson(ptr, ptr + len))
  19960. static basic_json from_ubjson(detail::span_input_adapter&& i,
  19961. const bool strict = true,
  19962. const bool allow_exceptions = true)
  19963. {
  19964. basic_json result;
  19965. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  19966. auto ia = i.get();
  19967. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  19968. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict);
  19969. return res ? result : basic_json(value_t::discarded);
  19970. }
  19971. /// @brief create a JSON value from an input in BJData format
  19972. /// @sa https://json.nlohmann.me/api/basic_json/from_bjdata/
  19973. template<typename InputType>
  19974. JSON_HEDLEY_WARN_UNUSED_RESULT
  19975. static basic_json from_bjdata(InputType&& i,
  19976. const bool strict = true,
  19977. const bool allow_exceptions = true)
  19978. {
  19979. basic_json result;
  19980. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  19981. auto ia = detail::input_adapter(std::forward<InputType>(i));
  19982. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict);
  19983. return res ? result : basic_json(value_t::discarded);
  19984. }
  19985. /// @brief create a JSON value from an input in BJData format
  19986. /// @sa https://json.nlohmann.me/api/basic_json/from_bjdata/
  19987. template<typename IteratorType>
  19988. JSON_HEDLEY_WARN_UNUSED_RESULT
  19989. static basic_json from_bjdata(IteratorType first, IteratorType last,
  19990. const bool strict = true,
  19991. const bool allow_exceptions = true)
  19992. {
  19993. basic_json result;
  19994. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  19995. auto ia = detail::input_adapter(std::move(first), std::move(last));
  19996. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict);
  19997. return res ? result : basic_json(value_t::discarded);
  19998. }
  19999. /// @brief create a JSON value from an input in BSON format
  20000. /// @sa https://json.nlohmann.me/api/basic_json/from_bson/
  20001. template<typename InputType>
  20002. JSON_HEDLEY_WARN_UNUSED_RESULT
  20003. static basic_json from_bson(InputType&& i,
  20004. const bool strict = true,
  20005. const bool allow_exceptions = true)
  20006. {
  20007. basic_json result;
  20008. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20009. auto ia = detail::input_adapter(std::forward<InputType>(i));
  20010. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict);
  20011. return res ? result : basic_json(value_t::discarded);
  20012. }
  20013. /// @brief create a JSON value from an input in BSON format
  20014. /// @sa https://json.nlohmann.me/api/basic_json/from_bson/
  20015. template<typename IteratorType>
  20016. JSON_HEDLEY_WARN_UNUSED_RESULT
  20017. static basic_json from_bson(IteratorType first, IteratorType last,
  20018. const bool strict = true,
  20019. const bool allow_exceptions = true)
  20020. {
  20021. basic_json result;
  20022. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20023. auto ia = detail::input_adapter(std::move(first), std::move(last));
  20024. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict);
  20025. return res ? result : basic_json(value_t::discarded);
  20026. }
  20027. template<typename T>
  20028. JSON_HEDLEY_WARN_UNUSED_RESULT
  20029. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_bson(ptr, ptr + len))
  20030. static basic_json from_bson(const T* ptr, std::size_t len,
  20031. const bool strict = true,
  20032. const bool allow_exceptions = true)
  20033. {
  20034. return from_bson(ptr, ptr + len, strict, allow_exceptions);
  20035. }
  20036. JSON_HEDLEY_WARN_UNUSED_RESULT
  20037. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_bson(ptr, ptr + len))
  20038. static basic_json from_bson(detail::span_input_adapter&& i,
  20039. const bool strict = true,
  20040. const bool allow_exceptions = true)
  20041. {
  20042. basic_json result;
  20043. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  20044. auto ia = i.get();
  20045. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  20046. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict);
  20047. return res ? result : basic_json(value_t::discarded);
  20048. }
  20049. /// @}
  20050. //////////////////////////
  20051. // JSON Pointer support //
  20052. //////////////////////////
  20053. /// @name JSON Pointer functions
  20054. /// @{
  20055. /// @brief access specified element via JSON Pointer
  20056. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  20057. reference operator[](const json_pointer& ptr)
  20058. {
  20059. return ptr.get_unchecked(this);
  20060. }
  20061. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  20062. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  20063. reference operator[](const ::nlohmann::json_pointer<BasicJsonType>& ptr)
  20064. {
  20065. return ptr.get_unchecked(this);
  20066. }
  20067. /// @brief access specified element via JSON Pointer
  20068. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  20069. const_reference operator[](const json_pointer& ptr) const
  20070. {
  20071. return ptr.get_unchecked(this);
  20072. }
  20073. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  20074. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  20075. const_reference operator[](const ::nlohmann::json_pointer<BasicJsonType>& ptr) const
  20076. {
  20077. return ptr.get_unchecked(this);
  20078. }
  20079. /// @brief access specified element via JSON Pointer
  20080. /// @sa https://json.nlohmann.me/api/basic_json/at/
  20081. reference at(const json_pointer& ptr)
  20082. {
  20083. return ptr.get_checked(this);
  20084. }
  20085. template<typename BasicJsonType>
  20086. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  20087. reference at(const ::nlohmann::json_pointer<BasicJsonType>& ptr)
  20088. {
  20089. return ptr.get_checked(this);
  20090. }
  20091. /// @brief access specified element via JSON Pointer
  20092. /// @sa https://json.nlohmann.me/api/basic_json/at/
  20093. const_reference at(const json_pointer& ptr) const
  20094. {
  20095. return ptr.get_checked(this);
  20096. }
  20097. template<typename BasicJsonType>
  20098. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  20099. const_reference at(const ::nlohmann::json_pointer<BasicJsonType>& ptr) const
  20100. {
  20101. return ptr.get_checked(this);
  20102. }
  20103. /// @brief return flattened JSON value
  20104. /// @sa https://json.nlohmann.me/api/basic_json/flatten/
  20105. basic_json flatten() const
  20106. {
  20107. basic_json result(value_t::object);
  20108. json_pointer::flatten("", *this, result);
  20109. return result;
  20110. }
  20111. /// @brief unflatten a previously flattened JSON value
  20112. /// @sa https://json.nlohmann.me/api/basic_json/unflatten/
  20113. basic_json unflatten() const
  20114. {
  20115. return json_pointer::unflatten(*this);
  20116. }
  20117. /// @}
  20118. //////////////////////////
  20119. // JSON Patch functions //
  20120. //////////////////////////
  20121. /// @name JSON Patch functions
  20122. /// @{
  20123. /// @brief applies a JSON patch
  20124. /// @sa https://json.nlohmann.me/api/basic_json/patch/
  20125. basic_json patch(const basic_json& json_patch) const
  20126. {
  20127. // make a working copy to apply the patch to
  20128. basic_json result = *this;
  20129. // the valid JSON Patch operations
  20130. enum class patch_operations {add, remove, replace, move, copy, test, invalid};
  20131. const auto get_op = [](const std::string & op)
  20132. {
  20133. if (op == "add")
  20134. {
  20135. return patch_operations::add;
  20136. }
  20137. if (op == "remove")
  20138. {
  20139. return patch_operations::remove;
  20140. }
  20141. if (op == "replace")
  20142. {
  20143. return patch_operations::replace;
  20144. }
  20145. if (op == "move")
  20146. {
  20147. return patch_operations::move;
  20148. }
  20149. if (op == "copy")
  20150. {
  20151. return patch_operations::copy;
  20152. }
  20153. if (op == "test")
  20154. {
  20155. return patch_operations::test;
  20156. }
  20157. return patch_operations::invalid;
  20158. };
  20159. // wrapper for "add" operation; add value at ptr
  20160. const auto operation_add = [&result](json_pointer & ptr, basic_json val)
  20161. {
  20162. // adding to the root of the target document means replacing it
  20163. if (ptr.empty())
  20164. {
  20165. result = val;
  20166. return;
  20167. }
  20168. // make sure the top element of the pointer exists
  20169. json_pointer top_pointer = ptr.top();
  20170. if (top_pointer != ptr)
  20171. {
  20172. result.at(top_pointer);
  20173. }
  20174. // get reference to parent of JSON pointer ptr
  20175. const auto last_path = ptr.back();
  20176. ptr.pop_back();
  20177. basic_json& parent = result[ptr];
  20178. switch (parent.m_type)
  20179. {
  20180. case value_t::null:
  20181. case value_t::object:
  20182. {
  20183. // use operator[] to add value
  20184. parent[last_path] = val;
  20185. break;
  20186. }
  20187. case value_t::array:
  20188. {
  20189. if (last_path == "-")
  20190. {
  20191. // special case: append to back
  20192. parent.push_back(val);
  20193. }
  20194. else
  20195. {
  20196. const auto idx = json_pointer::template array_index<basic_json_t>(last_path);
  20197. if (JSON_HEDLEY_UNLIKELY(idx > parent.size()))
  20198. {
  20199. // avoid undefined behavior
  20200. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), &parent));
  20201. }
  20202. // default case: insert add offset
  20203. parent.insert(parent.begin() + static_cast<difference_type>(idx), val);
  20204. }
  20205. break;
  20206. }
  20207. // if there exists a parent it cannot be primitive
  20208. case value_t::string: // LCOV_EXCL_LINE
  20209. case value_t::boolean: // LCOV_EXCL_LINE
  20210. case value_t::number_integer: // LCOV_EXCL_LINE
  20211. case value_t::number_unsigned: // LCOV_EXCL_LINE
  20212. case value_t::number_float: // LCOV_EXCL_LINE
  20213. case value_t::binary: // LCOV_EXCL_LINE
  20214. case value_t::discarded: // LCOV_EXCL_LINE
  20215. default: // LCOV_EXCL_LINE
  20216. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  20217. }
  20218. };
  20219. // wrapper for "remove" operation; remove value at ptr
  20220. const auto operation_remove = [this, &result](json_pointer & ptr)
  20221. {
  20222. // get reference to parent of JSON pointer ptr
  20223. const auto last_path = ptr.back();
  20224. ptr.pop_back();
  20225. basic_json& parent = result.at(ptr);
  20226. // remove child
  20227. if (parent.is_object())
  20228. {
  20229. // perform range check
  20230. auto it = parent.find(last_path);
  20231. if (JSON_HEDLEY_LIKELY(it != parent.end()))
  20232. {
  20233. parent.erase(it);
  20234. }
  20235. else
  20236. {
  20237. JSON_THROW(out_of_range::create(403, detail::concat("key '", last_path, "' not found"), this));
  20238. }
  20239. }
  20240. else if (parent.is_array())
  20241. {
  20242. // note erase performs range check
  20243. parent.erase(json_pointer::template array_index<basic_json_t>(last_path));
  20244. }
  20245. };
  20246. // type check: top level value must be an array
  20247. if (JSON_HEDLEY_UNLIKELY(!json_patch.is_array()))
  20248. {
  20249. JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", &json_patch));
  20250. }
  20251. // iterate and apply the operations
  20252. for (const auto& val : json_patch)
  20253. {
  20254. // wrapper to get a value for an operation
  20255. const auto get_value = [&val](const std::string & op,
  20256. const std::string & member,
  20257. bool string_type) -> basic_json &
  20258. {
  20259. // find value
  20260. auto it = val.m_value.object->find(member);
  20261. // context-sensitive error message
  20262. const auto error_msg = (op == "op") ? "operation" : detail::concat("operation '", op, '\'');
  20263. // check if desired value is present
  20264. if (JSON_HEDLEY_UNLIKELY(it == val.m_value.object->end()))
  20265. {
  20266. // NOLINTNEXTLINE(performance-inefficient-string-concatenation)
  20267. JSON_THROW(parse_error::create(105, 0, detail::concat(error_msg, " must have member '", member, "'"), &val));
  20268. }
  20269. // check if result is of type string
  20270. if (JSON_HEDLEY_UNLIKELY(string_type && !it->second.is_string()))
  20271. {
  20272. // NOLINTNEXTLINE(performance-inefficient-string-concatenation)
  20273. JSON_THROW(parse_error::create(105, 0, detail::concat(error_msg, " must have string member '", member, "'"), &val));
  20274. }
  20275. // no error: return value
  20276. return it->second;
  20277. };
  20278. // type check: every element of the array must be an object
  20279. if (JSON_HEDLEY_UNLIKELY(!val.is_object()))
  20280. {
  20281. JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", &val));
  20282. }
  20283. // collect mandatory members
  20284. const auto op = get_value("op", "op", true).template get<std::string>();
  20285. const auto path = get_value(op, "path", true).template get<std::string>();
  20286. json_pointer ptr(path);
  20287. switch (get_op(op))
  20288. {
  20289. case patch_operations::add:
  20290. {
  20291. operation_add(ptr, get_value("add", "value", false));
  20292. break;
  20293. }
  20294. case patch_operations::remove:
  20295. {
  20296. operation_remove(ptr);
  20297. break;
  20298. }
  20299. case patch_operations::replace:
  20300. {
  20301. // the "path" location must exist - use at()
  20302. result.at(ptr) = get_value("replace", "value", false);
  20303. break;
  20304. }
  20305. case patch_operations::move:
  20306. {
  20307. const auto from_path = get_value("move", "from", true).template get<std::string>();
  20308. json_pointer from_ptr(from_path);
  20309. // the "from" location must exist - use at()
  20310. basic_json v = result.at(from_ptr);
  20311. // The move operation is functionally identical to a
  20312. // "remove" operation on the "from" location, followed
  20313. // immediately by an "add" operation at the target
  20314. // location with the value that was just removed.
  20315. operation_remove(from_ptr);
  20316. operation_add(ptr, v);
  20317. break;
  20318. }
  20319. case patch_operations::copy:
  20320. {
  20321. const auto from_path = get_value("copy", "from", true).template get<std::string>();
  20322. const json_pointer from_ptr(from_path);
  20323. // the "from" location must exist - use at()
  20324. basic_json v = result.at(from_ptr);
  20325. // The copy is functionally identical to an "add"
  20326. // operation at the target location using the value
  20327. // specified in the "from" member.
  20328. operation_add(ptr, v);
  20329. break;
  20330. }
  20331. case patch_operations::test:
  20332. {
  20333. bool success = false;
  20334. JSON_TRY
  20335. {
  20336. // check if "value" matches the one at "path"
  20337. // the "path" location must exist - use at()
  20338. success = (result.at(ptr) == get_value("test", "value", false));
  20339. }
  20340. JSON_INTERNAL_CATCH (out_of_range&)
  20341. {
  20342. // ignore out of range errors: success remains false
  20343. }
  20344. // throw an exception if test fails
  20345. if (JSON_HEDLEY_UNLIKELY(!success))
  20346. {
  20347. JSON_THROW(other_error::create(501, detail::concat("unsuccessful: ", val.dump()), &val));
  20348. }
  20349. break;
  20350. }
  20351. case patch_operations::invalid:
  20352. default:
  20353. {
  20354. // op must be "add", "remove", "replace", "move", "copy", or
  20355. // "test"
  20356. JSON_THROW(parse_error::create(105, 0, detail::concat("operation value '", op, "' is invalid"), &val));
  20357. }
  20358. }
  20359. }
  20360. return result;
  20361. }
  20362. /// @brief creates a diff as a JSON patch
  20363. /// @sa https://json.nlohmann.me/api/basic_json/diff/
  20364. JSON_HEDLEY_WARN_UNUSED_RESULT
  20365. static basic_json diff(const basic_json& source, const basic_json& target,
  20366. const std::string& path = "")
  20367. {
  20368. // the patch
  20369. basic_json result(value_t::array);
  20370. // if the values are the same, return empty patch
  20371. if (source == target)
  20372. {
  20373. return result;
  20374. }
  20375. if (source.type() != target.type())
  20376. {
  20377. // different types: replace value
  20378. result.push_back(
  20379. {
  20380. {"op", "replace"}, {"path", path}, {"value", target}
  20381. });
  20382. return result;
  20383. }
  20384. switch (source.type())
  20385. {
  20386. case value_t::array:
  20387. {
  20388. // first pass: traverse common elements
  20389. std::size_t i = 0;
  20390. while (i < source.size() && i < target.size())
  20391. {
  20392. // recursive call to compare array values at index i
  20393. auto temp_diff = diff(source[i], target[i], detail::concat(path, '/', std::to_string(i)));
  20394. result.insert(result.end(), temp_diff.begin(), temp_diff.end());
  20395. ++i;
  20396. }
  20397. // We now reached the end of at least one array
  20398. // in a second pass, traverse the remaining elements
  20399. // remove my remaining elements
  20400. const auto end_index = static_cast<difference_type>(result.size());
  20401. while (i < source.size())
  20402. {
  20403. // add operations in reverse order to avoid invalid
  20404. // indices
  20405. result.insert(result.begin() + end_index, object(
  20406. {
  20407. {"op", "remove"},
  20408. {"path", detail::concat(path, '/', std::to_string(i))}
  20409. }));
  20410. ++i;
  20411. }
  20412. // add other remaining elements
  20413. while (i < target.size())
  20414. {
  20415. result.push_back(
  20416. {
  20417. {"op", "add"},
  20418. {"path", detail::concat(path, "/-")},
  20419. {"value", target[i]}
  20420. });
  20421. ++i;
  20422. }
  20423. break;
  20424. }
  20425. case value_t::object:
  20426. {
  20427. // first pass: traverse this object's elements
  20428. for (auto it = source.cbegin(); it != source.cend(); ++it)
  20429. {
  20430. // escape the key name to be used in a JSON patch
  20431. const auto path_key = detail::concat(path, '/', detail::escape(it.key()));
  20432. if (target.find(it.key()) != target.end())
  20433. {
  20434. // recursive call to compare object values at key it
  20435. auto temp_diff = diff(it.value(), target[it.key()], path_key);
  20436. result.insert(result.end(), temp_diff.begin(), temp_diff.end());
  20437. }
  20438. else
  20439. {
  20440. // found a key that is not in o -> remove it
  20441. result.push_back(object(
  20442. {
  20443. {"op", "remove"}, {"path", path_key}
  20444. }));
  20445. }
  20446. }
  20447. // second pass: traverse other object's elements
  20448. for (auto it = target.cbegin(); it != target.cend(); ++it)
  20449. {
  20450. if (source.find(it.key()) == source.end())
  20451. {
  20452. // found a key that is not in this -> add it
  20453. const auto path_key = detail::concat(path, '/', detail::escape(it.key()));
  20454. result.push_back(
  20455. {
  20456. {"op", "add"}, {"path", path_key},
  20457. {"value", it.value()}
  20458. });
  20459. }
  20460. }
  20461. break;
  20462. }
  20463. case value_t::null:
  20464. case value_t::string:
  20465. case value_t::boolean:
  20466. case value_t::number_integer:
  20467. case value_t::number_unsigned:
  20468. case value_t::number_float:
  20469. case value_t::binary:
  20470. case value_t::discarded:
  20471. default:
  20472. {
  20473. // both primitive type: replace value
  20474. result.push_back(
  20475. {
  20476. {"op", "replace"}, {"path", path}, {"value", target}
  20477. });
  20478. break;
  20479. }
  20480. }
  20481. return result;
  20482. }
  20483. /// @}
  20484. ////////////////////////////////
  20485. // JSON Merge Patch functions //
  20486. ////////////////////////////////
  20487. /// @name JSON Merge Patch functions
  20488. /// @{
  20489. /// @brief applies a JSON Merge Patch
  20490. /// @sa https://json.nlohmann.me/api/basic_json/merge_patch/
  20491. void merge_patch(const basic_json& apply_patch)
  20492. {
  20493. if (apply_patch.is_object())
  20494. {
  20495. if (!is_object())
  20496. {
  20497. *this = object();
  20498. }
  20499. for (auto it = apply_patch.begin(); it != apply_patch.end(); ++it)
  20500. {
  20501. if (it.value().is_null())
  20502. {
  20503. erase(it.key());
  20504. }
  20505. else
  20506. {
  20507. operator[](it.key()).merge_patch(it.value());
  20508. }
  20509. }
  20510. }
  20511. else
  20512. {
  20513. *this = apply_patch;
  20514. }
  20515. }
  20516. /// @}
  20517. };
  20518. /// @brief user-defined to_string function for JSON values
  20519. /// @sa https://json.nlohmann.me/api/basic_json/to_string/
  20520. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  20521. std::string to_string(const NLOHMANN_BASIC_JSON_TPL& j)
  20522. {
  20523. return j.dump();
  20524. }
  20525. } // namespace nlohmann
  20526. ///////////////////////
  20527. // nonmember support //
  20528. ///////////////////////
  20529. namespace std // NOLINT(cert-dcl58-cpp)
  20530. {
  20531. /// @brief hash value for JSON objects
  20532. /// @sa https://json.nlohmann.me/api/basic_json/std_hash/
  20533. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  20534. struct hash<nlohmann::NLOHMANN_BASIC_JSON_TPL>
  20535. {
  20536. std::size_t operator()(const nlohmann::NLOHMANN_BASIC_JSON_TPL& j) const
  20537. {
  20538. return nlohmann::detail::hash(j);
  20539. }
  20540. };
  20541. // specialization for std::less<value_t>
  20542. template<>
  20543. struct less< ::nlohmann::detail::value_t> // do not remove the space after '<', see https://github.com/nlohmann/json/pull/679
  20544. {
  20545. /*!
  20546. @brief compare two value_t enum values
  20547. @since version 3.0.0
  20548. */
  20549. bool operator()(::nlohmann::detail::value_t lhs,
  20550. ::nlohmann::detail::value_t rhs) const noexcept
  20551. {
  20552. #if JSON_HAS_THREE_WAY_COMPARISON
  20553. return std::is_lt(lhs <=> rhs); // *NOPAD*
  20554. #else
  20555. return ::nlohmann::detail::operator<(lhs, rhs);
  20556. #endif
  20557. }
  20558. };
  20559. // C++20 prohibit function specialization in the std namespace.
  20560. #ifndef JSON_HAS_CPP_20
  20561. /// @brief exchanges the values of two JSON objects
  20562. /// @sa https://json.nlohmann.me/api/basic_json/std_swap/
  20563. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  20564. inline void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL& j1, nlohmann::NLOHMANN_BASIC_JSON_TPL& j2) noexcept( // NOLINT(readability-inconsistent-declaration-parameter-name)
  20565. is_nothrow_move_constructible<nlohmann::NLOHMANN_BASIC_JSON_TPL>::value&& // NOLINT(misc-redundant-expression)
  20566. is_nothrow_move_assignable<nlohmann::NLOHMANN_BASIC_JSON_TPL>::value)
  20567. {
  20568. j1.swap(j2);
  20569. }
  20570. #endif
  20571. } // namespace std
  20572. /// @brief user-defined string literal for JSON values
  20573. /// @sa https://json.nlohmann.me/api/basic_json/operator_literal_json/
  20574. JSON_HEDLEY_NON_NULL(1)
  20575. inline nlohmann::json operator "" _json(const char* s, std::size_t n)
  20576. {
  20577. return nlohmann::json::parse(s, s + n);
  20578. }
  20579. /// @brief user-defined string literal for JSON pointer
  20580. /// @sa https://json.nlohmann.me/api/basic_json/operator_literal_json_pointer/
  20581. JSON_HEDLEY_NON_NULL(1)
  20582. inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std::size_t n)
  20583. {
  20584. return nlohmann::json::json_pointer(std::string(s, n));
  20585. }
  20586. // #include <nlohmann/detail/macro_unscope.hpp>
  20587. // restore clang diagnostic settings
  20588. #if defined(__clang__)
  20589. #pragma clang diagnostic pop
  20590. #endif
  20591. // clean up
  20592. #undef JSON_ASSERT
  20593. #undef JSON_INTERNAL_CATCH
  20594. #undef JSON_THROW
  20595. #undef JSON_PRIVATE_UNLESS_TESTED
  20596. #undef NLOHMANN_BASIC_JSON_TPL_DECLARATION
  20597. #undef NLOHMANN_BASIC_JSON_TPL
  20598. #undef JSON_EXPLICIT
  20599. #undef NLOHMANN_CAN_CALL_STD_FUNC_IMPL
  20600. #undef JSON_INLINE_VARIABLE
  20601. #undef JSON_NO_UNIQUE_ADDRESS
  20602. #undef JSON_DISABLE_ENUM_SERIALIZATION
  20603. #ifndef JSON_TEST_KEEP_MACROS
  20604. #undef JSON_CATCH
  20605. #undef JSON_TRY
  20606. #undef JSON_HAS_CPP_11
  20607. #undef JSON_HAS_CPP_14
  20608. #undef JSON_HAS_CPP_17
  20609. #undef JSON_HAS_CPP_20
  20610. #undef JSON_HAS_FILESYSTEM
  20611. #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
  20612. #undef JSON_HAS_THREE_WAY_COMPARISON
  20613. #undef JSON_HAS_RANGES
  20614. #undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  20615. #endif
  20616. // #include <nlohmann/thirdparty/hedley/hedley_undef.hpp>
  20617. #undef JSON_HEDLEY_ALWAYS_INLINE
  20618. #undef JSON_HEDLEY_ARM_VERSION
  20619. #undef JSON_HEDLEY_ARM_VERSION_CHECK
  20620. #undef JSON_HEDLEY_ARRAY_PARAM
  20621. #undef JSON_HEDLEY_ASSUME
  20622. #undef JSON_HEDLEY_BEGIN_C_DECLS
  20623. #undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE
  20624. #undef JSON_HEDLEY_CLANG_HAS_BUILTIN
  20625. #undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE
  20626. #undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE
  20627. #undef JSON_HEDLEY_CLANG_HAS_EXTENSION
  20628. #undef JSON_HEDLEY_CLANG_HAS_FEATURE
  20629. #undef JSON_HEDLEY_CLANG_HAS_WARNING
  20630. #undef JSON_HEDLEY_COMPCERT_VERSION
  20631. #undef JSON_HEDLEY_COMPCERT_VERSION_CHECK
  20632. #undef JSON_HEDLEY_CONCAT
  20633. #undef JSON_HEDLEY_CONCAT3
  20634. #undef JSON_HEDLEY_CONCAT3_EX
  20635. #undef JSON_HEDLEY_CONCAT_EX
  20636. #undef JSON_HEDLEY_CONST
  20637. #undef JSON_HEDLEY_CONSTEXPR
  20638. #undef JSON_HEDLEY_CONST_CAST
  20639. #undef JSON_HEDLEY_CPP_CAST
  20640. #undef JSON_HEDLEY_CRAY_VERSION
  20641. #undef JSON_HEDLEY_CRAY_VERSION_CHECK
  20642. #undef JSON_HEDLEY_C_DECL
  20643. #undef JSON_HEDLEY_DEPRECATED
  20644. #undef JSON_HEDLEY_DEPRECATED_FOR
  20645. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
  20646. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_
  20647. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
  20648. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
  20649. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
  20650. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
  20651. #undef JSON_HEDLEY_DIAGNOSTIC_POP
  20652. #undef JSON_HEDLEY_DIAGNOSTIC_PUSH
  20653. #undef JSON_HEDLEY_DMC_VERSION
  20654. #undef JSON_HEDLEY_DMC_VERSION_CHECK
  20655. #undef JSON_HEDLEY_EMPTY_BASES
  20656. #undef JSON_HEDLEY_EMSCRIPTEN_VERSION
  20657. #undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK
  20658. #undef JSON_HEDLEY_END_C_DECLS
  20659. #undef JSON_HEDLEY_FLAGS
  20660. #undef JSON_HEDLEY_FLAGS_CAST
  20661. #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE
  20662. #undef JSON_HEDLEY_GCC_HAS_BUILTIN
  20663. #undef JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE
  20664. #undef JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE
  20665. #undef JSON_HEDLEY_GCC_HAS_EXTENSION
  20666. #undef JSON_HEDLEY_GCC_HAS_FEATURE
  20667. #undef JSON_HEDLEY_GCC_HAS_WARNING
  20668. #undef JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK
  20669. #undef JSON_HEDLEY_GCC_VERSION
  20670. #undef JSON_HEDLEY_GCC_VERSION_CHECK
  20671. #undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE
  20672. #undef JSON_HEDLEY_GNUC_HAS_BUILTIN
  20673. #undef JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE
  20674. #undef JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE
  20675. #undef JSON_HEDLEY_GNUC_HAS_EXTENSION
  20676. #undef JSON_HEDLEY_GNUC_HAS_FEATURE
  20677. #undef JSON_HEDLEY_GNUC_HAS_WARNING
  20678. #undef JSON_HEDLEY_GNUC_VERSION
  20679. #undef JSON_HEDLEY_GNUC_VERSION_CHECK
  20680. #undef JSON_HEDLEY_HAS_ATTRIBUTE
  20681. #undef JSON_HEDLEY_HAS_BUILTIN
  20682. #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE
  20683. #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS
  20684. #undef JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE
  20685. #undef JSON_HEDLEY_HAS_EXTENSION
  20686. #undef JSON_HEDLEY_HAS_FEATURE
  20687. #undef JSON_HEDLEY_HAS_WARNING
  20688. #undef JSON_HEDLEY_IAR_VERSION
  20689. #undef JSON_HEDLEY_IAR_VERSION_CHECK
  20690. #undef JSON_HEDLEY_IBM_VERSION
  20691. #undef JSON_HEDLEY_IBM_VERSION_CHECK
  20692. #undef JSON_HEDLEY_IMPORT
  20693. #undef JSON_HEDLEY_INLINE
  20694. #undef JSON_HEDLEY_INTEL_CL_VERSION
  20695. #undef JSON_HEDLEY_INTEL_CL_VERSION_CHECK
  20696. #undef JSON_HEDLEY_INTEL_VERSION
  20697. #undef JSON_HEDLEY_INTEL_VERSION_CHECK
  20698. #undef JSON_HEDLEY_IS_CONSTANT
  20699. #undef JSON_HEDLEY_IS_CONSTEXPR_
  20700. #undef JSON_HEDLEY_LIKELY
  20701. #undef JSON_HEDLEY_MALLOC
  20702. #undef JSON_HEDLEY_MCST_LCC_VERSION
  20703. #undef JSON_HEDLEY_MCST_LCC_VERSION_CHECK
  20704. #undef JSON_HEDLEY_MESSAGE
  20705. #undef JSON_HEDLEY_MSVC_VERSION
  20706. #undef JSON_HEDLEY_MSVC_VERSION_CHECK
  20707. #undef JSON_HEDLEY_NEVER_INLINE
  20708. #undef JSON_HEDLEY_NON_NULL
  20709. #undef JSON_HEDLEY_NO_ESCAPE
  20710. #undef JSON_HEDLEY_NO_RETURN
  20711. #undef JSON_HEDLEY_NO_THROW
  20712. #undef JSON_HEDLEY_NULL
  20713. #undef JSON_HEDLEY_PELLES_VERSION
  20714. #undef JSON_HEDLEY_PELLES_VERSION_CHECK
  20715. #undef JSON_HEDLEY_PGI_VERSION
  20716. #undef JSON_HEDLEY_PGI_VERSION_CHECK
  20717. #undef JSON_HEDLEY_PREDICT
  20718. #undef JSON_HEDLEY_PRINTF_FORMAT
  20719. #undef JSON_HEDLEY_PRIVATE
  20720. #undef JSON_HEDLEY_PUBLIC
  20721. #undef JSON_HEDLEY_PURE
  20722. #undef JSON_HEDLEY_REINTERPRET_CAST
  20723. #undef JSON_HEDLEY_REQUIRE
  20724. #undef JSON_HEDLEY_REQUIRE_CONSTEXPR
  20725. #undef JSON_HEDLEY_REQUIRE_MSG
  20726. #undef JSON_HEDLEY_RESTRICT
  20727. #undef JSON_HEDLEY_RETURNS_NON_NULL
  20728. #undef JSON_HEDLEY_SENTINEL
  20729. #undef JSON_HEDLEY_STATIC_ASSERT
  20730. #undef JSON_HEDLEY_STATIC_CAST
  20731. #undef JSON_HEDLEY_STRINGIFY
  20732. #undef JSON_HEDLEY_STRINGIFY_EX
  20733. #undef JSON_HEDLEY_SUNPRO_VERSION
  20734. #undef JSON_HEDLEY_SUNPRO_VERSION_CHECK
  20735. #undef JSON_HEDLEY_TINYC_VERSION
  20736. #undef JSON_HEDLEY_TINYC_VERSION_CHECK
  20737. #undef JSON_HEDLEY_TI_ARMCL_VERSION
  20738. #undef JSON_HEDLEY_TI_ARMCL_VERSION_CHECK
  20739. #undef JSON_HEDLEY_TI_CL2000_VERSION
  20740. #undef JSON_HEDLEY_TI_CL2000_VERSION_CHECK
  20741. #undef JSON_HEDLEY_TI_CL430_VERSION
  20742. #undef JSON_HEDLEY_TI_CL430_VERSION_CHECK
  20743. #undef JSON_HEDLEY_TI_CL6X_VERSION
  20744. #undef JSON_HEDLEY_TI_CL6X_VERSION_CHECK
  20745. #undef JSON_HEDLEY_TI_CL7X_VERSION
  20746. #undef JSON_HEDLEY_TI_CL7X_VERSION_CHECK
  20747. #undef JSON_HEDLEY_TI_CLPRU_VERSION
  20748. #undef JSON_HEDLEY_TI_CLPRU_VERSION_CHECK
  20749. #undef JSON_HEDLEY_TI_VERSION
  20750. #undef JSON_HEDLEY_TI_VERSION_CHECK
  20751. #undef JSON_HEDLEY_UNAVAILABLE
  20752. #undef JSON_HEDLEY_UNLIKELY
  20753. #undef JSON_HEDLEY_UNPREDICTABLE
  20754. #undef JSON_HEDLEY_UNREACHABLE
  20755. #undef JSON_HEDLEY_UNREACHABLE_RETURN
  20756. #undef JSON_HEDLEY_VERSION
  20757. #undef JSON_HEDLEY_VERSION_DECODE_MAJOR
  20758. #undef JSON_HEDLEY_VERSION_DECODE_MINOR
  20759. #undef JSON_HEDLEY_VERSION_DECODE_REVISION
  20760. #undef JSON_HEDLEY_VERSION_ENCODE
  20761. #undef JSON_HEDLEY_WARNING
  20762. #undef JSON_HEDLEY_WARN_UNUSED_RESULT
  20763. #undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG
  20764. #undef JSON_HEDLEY_FALL_THROUGH
  20765. #endif // INCLUDE_NLOHMANN_JSON_HPP_