json.hpp 956 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640
  1. /*
  2. __ _____ _____ _____
  3. __| | __| | | | JSON for Modern C++
  4. | | |__ | | | | | | version 3.10.2
  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-2019 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. #ifndef INCLUDE_NLOHMANN_JSON_HPP_
  26. #define INCLUDE_NLOHMANN_JSON_HPP_
  27. #define NLOHMANN_JSON_VERSION_MAJOR 3
  28. #define NLOHMANN_JSON_VERSION_MINOR 10
  29. #define NLOHMANN_JSON_VERSION_PATCH 2
  30. #include <algorithm> // all_of, find, for_each
  31. #include <cstddef> // nullptr_t, ptrdiff_t, size_t
  32. #include <functional> // hash, less
  33. #include <initializer_list> // initializer_list
  34. #ifndef JSON_NO_IO
  35. #include <iosfwd> // istream, ostream
  36. #endif // JSON_NO_IO
  37. #include <iterator> // random_access_iterator_tag
  38. #include <memory> // unique_ptr
  39. #include <numeric> // accumulate
  40. #include <string> // string, stoi, to_string
  41. #include <utility> // declval, forward, move, pair, swap
  42. #include <vector> // vector
  43. // #include <nlohmann/adl_serializer.hpp>
  44. #include <type_traits>
  45. #include <utility>
  46. // #include <nlohmann/detail/conversions/from_json.hpp>
  47. #include <algorithm> // transform
  48. #include <array> // array
  49. #include <forward_list> // forward_list
  50. #include <iterator> // inserter, front_inserter, end
  51. #include <map> // map
  52. #include <string> // string
  53. #include <tuple> // tuple, make_tuple
  54. #include <type_traits> // is_arithmetic, is_same, is_enum, underlying_type, is_convertible
  55. #include <unordered_map> // unordered_map
  56. #include <utility> // pair, declval
  57. #include <valarray> // valarray
  58. // #include <nlohmann/detail/exceptions.hpp>
  59. #include <exception> // exception
  60. #include <stdexcept> // runtime_error
  61. #include <string> // to_string
  62. #include <vector> // vector
  63. // #include <nlohmann/detail/value_t.hpp>
  64. #include <array> // array
  65. #include <cstddef> // size_t
  66. #include <cstdint> // uint8_t
  67. #include <string> // string
  68. namespace nlohmann
  69. {
  70. namespace detail
  71. {
  72. ///////////////////////////
  73. // JSON type enumeration //
  74. ///////////////////////////
  75. /*!
  76. @brief the JSON type enumeration
  77. This enumeration collects the different JSON types. It is internally used to
  78. distinguish the stored values, and the functions @ref basic_json::is_null(),
  79. @ref basic_json::is_object(), @ref basic_json::is_array(),
  80. @ref basic_json::is_string(), @ref basic_json::is_boolean(),
  81. @ref basic_json::is_number() (with @ref basic_json::is_number_integer(),
  82. @ref basic_json::is_number_unsigned(), and @ref basic_json::is_number_float()),
  83. @ref basic_json::is_discarded(), @ref basic_json::is_primitive(), and
  84. @ref basic_json::is_structured() rely on it.
  85. @note There are three enumeration entries (number_integer, number_unsigned, and
  86. number_float), because the library distinguishes these three types for numbers:
  87. @ref basic_json::number_unsigned_t is used for unsigned integers,
  88. @ref basic_json::number_integer_t is used for signed integers, and
  89. @ref basic_json::number_float_t is used for floating-point numbers or to
  90. approximate integers which do not fit in the limits of their respective type.
  91. @sa see @ref basic_json::basic_json(const value_t value_type) -- create a JSON
  92. value with the default value for a given type
  93. @since version 1.0.0
  94. */
  95. enum class value_t : std::uint8_t
  96. {
  97. null, ///< null value
  98. object, ///< object (unordered set of name/value pairs)
  99. array, ///< array (ordered collection of values)
  100. string, ///< string value
  101. boolean, ///< boolean value
  102. number_integer, ///< number value (signed integer)
  103. number_unsigned, ///< number value (unsigned integer)
  104. number_float, ///< number value (floating-point)
  105. binary, ///< binary array (ordered collection of bytes)
  106. discarded ///< discarded by the parser callback function
  107. };
  108. /*!
  109. @brief comparison operator for JSON types
  110. Returns an ordering that is similar to Python:
  111. - order: null < boolean < number < object < array < string < binary
  112. - furthermore, each type is not smaller than itself
  113. - discarded values are not comparable
  114. - binary is represented as a b"" string in python and directly comparable to a
  115. string; however, making a binary array directly comparable with a string would
  116. be surprising behavior in a JSON file.
  117. @since version 1.0.0
  118. */
  119. inline bool operator<(const value_t lhs, const value_t rhs) noexcept
  120. {
  121. static constexpr std::array<std::uint8_t, 9> order = {{
  122. 0 /* null */, 3 /* object */, 4 /* array */, 5 /* string */,
  123. 1 /* boolean */, 2 /* integer */, 2 /* unsigned */, 2 /* float */,
  124. 6 /* binary */
  125. }
  126. };
  127. const auto l_index = static_cast<std::size_t>(lhs);
  128. const auto r_index = static_cast<std::size_t>(rhs);
  129. return l_index < order.size() && r_index < order.size() && order[l_index] < order[r_index];
  130. }
  131. } // namespace detail
  132. } // namespace nlohmann
  133. // #include <nlohmann/detail/string_escape.hpp>
  134. #include <string>
  135. // #include <nlohmann/detail/macro_scope.hpp>
  136. #include <utility> // pair
  137. // #include <nlohmann/thirdparty/hedley/hedley.hpp>
  138. /* Hedley - https://nemequ.github.io/hedley
  139. * Created by Evan Nemerson <[email protected]>
  140. *
  141. * To the extent possible under law, the author(s) have dedicated all
  142. * copyright and related and neighboring rights to this software to
  143. * the public domain worldwide. This software is distributed without
  144. * any warranty.
  145. *
  146. * For details, see <http://creativecommons.org/publicdomain/zero/1.0/>.
  147. * SPDX-License-Identifier: CC0-1.0
  148. */
  149. #if !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < 15)
  150. #if defined(JSON_HEDLEY_VERSION)
  151. #undef JSON_HEDLEY_VERSION
  152. #endif
  153. #define JSON_HEDLEY_VERSION 15
  154. #if defined(JSON_HEDLEY_STRINGIFY_EX)
  155. #undef JSON_HEDLEY_STRINGIFY_EX
  156. #endif
  157. #define JSON_HEDLEY_STRINGIFY_EX(x) #x
  158. #if defined(JSON_HEDLEY_STRINGIFY)
  159. #undef JSON_HEDLEY_STRINGIFY
  160. #endif
  161. #define JSON_HEDLEY_STRINGIFY(x) JSON_HEDLEY_STRINGIFY_EX(x)
  162. #if defined(JSON_HEDLEY_CONCAT_EX)
  163. #undef JSON_HEDLEY_CONCAT_EX
  164. #endif
  165. #define JSON_HEDLEY_CONCAT_EX(a,b) a##b
  166. #if defined(JSON_HEDLEY_CONCAT)
  167. #undef JSON_HEDLEY_CONCAT
  168. #endif
  169. #define JSON_HEDLEY_CONCAT(a,b) JSON_HEDLEY_CONCAT_EX(a,b)
  170. #if defined(JSON_HEDLEY_CONCAT3_EX)
  171. #undef JSON_HEDLEY_CONCAT3_EX
  172. #endif
  173. #define JSON_HEDLEY_CONCAT3_EX(a,b,c) a##b##c
  174. #if defined(JSON_HEDLEY_CONCAT3)
  175. #undef JSON_HEDLEY_CONCAT3
  176. #endif
  177. #define JSON_HEDLEY_CONCAT3(a,b,c) JSON_HEDLEY_CONCAT3_EX(a,b,c)
  178. #if defined(JSON_HEDLEY_VERSION_ENCODE)
  179. #undef JSON_HEDLEY_VERSION_ENCODE
  180. #endif
  181. #define JSON_HEDLEY_VERSION_ENCODE(major,minor,revision) (((major) * 1000000) + ((minor) * 1000) + (revision))
  182. #if defined(JSON_HEDLEY_VERSION_DECODE_MAJOR)
  183. #undef JSON_HEDLEY_VERSION_DECODE_MAJOR
  184. #endif
  185. #define JSON_HEDLEY_VERSION_DECODE_MAJOR(version) ((version) / 1000000)
  186. #if defined(JSON_HEDLEY_VERSION_DECODE_MINOR)
  187. #undef JSON_HEDLEY_VERSION_DECODE_MINOR
  188. #endif
  189. #define JSON_HEDLEY_VERSION_DECODE_MINOR(version) (((version) % 1000000) / 1000)
  190. #if defined(JSON_HEDLEY_VERSION_DECODE_REVISION)
  191. #undef JSON_HEDLEY_VERSION_DECODE_REVISION
  192. #endif
  193. #define JSON_HEDLEY_VERSION_DECODE_REVISION(version) ((version) % 1000)
  194. #if defined(JSON_HEDLEY_GNUC_VERSION)
  195. #undef JSON_HEDLEY_GNUC_VERSION
  196. #endif
  197. #if defined(__GNUC__) && defined(__GNUC_PATCHLEVEL__)
  198. #define JSON_HEDLEY_GNUC_VERSION JSON_HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
  199. #elif defined(__GNUC__)
  200. #define JSON_HEDLEY_GNUC_VERSION JSON_HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, 0)
  201. #endif
  202. #if defined(JSON_HEDLEY_GNUC_VERSION_CHECK)
  203. #undef JSON_HEDLEY_GNUC_VERSION_CHECK
  204. #endif
  205. #if defined(JSON_HEDLEY_GNUC_VERSION)
  206. #define JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_GNUC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  207. #else
  208. #define JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) (0)
  209. #endif
  210. #if defined(JSON_HEDLEY_MSVC_VERSION)
  211. #undef JSON_HEDLEY_MSVC_VERSION
  212. #endif
  213. #if defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 140000000) && !defined(__ICL)
  214. #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 10000000, (_MSC_FULL_VER % 10000000) / 100000, (_MSC_FULL_VER % 100000) / 100)
  215. #elif defined(_MSC_FULL_VER) && !defined(__ICL)
  216. #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 1000000, (_MSC_FULL_VER % 1000000) / 10000, (_MSC_FULL_VER % 10000) / 10)
  217. #elif defined(_MSC_VER) && !defined(__ICL)
  218. #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_VER / 100, _MSC_VER % 100, 0)
  219. #endif
  220. #if defined(JSON_HEDLEY_MSVC_VERSION_CHECK)
  221. #undef JSON_HEDLEY_MSVC_VERSION_CHECK
  222. #endif
  223. #if !defined(JSON_HEDLEY_MSVC_VERSION)
  224. #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (0)
  225. #elif defined(_MSC_VER) && (_MSC_VER >= 1400)
  226. #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 10000000) + (minor * 100000) + (patch)))
  227. #elif defined(_MSC_VER) && (_MSC_VER >= 1200)
  228. #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 1000000) + (minor * 10000) + (patch)))
  229. #else
  230. #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_VER >= ((major * 100) + (minor)))
  231. #endif
  232. #if defined(JSON_HEDLEY_INTEL_VERSION)
  233. #undef JSON_HEDLEY_INTEL_VERSION
  234. #endif
  235. #if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) && !defined(__ICL)
  236. #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, __INTEL_COMPILER_UPDATE)
  237. #elif defined(__INTEL_COMPILER) && !defined(__ICL)
  238. #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, 0)
  239. #endif
  240. #if defined(JSON_HEDLEY_INTEL_VERSION_CHECK)
  241. #undef JSON_HEDLEY_INTEL_VERSION_CHECK
  242. #endif
  243. #if defined(JSON_HEDLEY_INTEL_VERSION)
  244. #define JSON_HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_INTEL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  245. #else
  246. #define JSON_HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (0)
  247. #endif
  248. #if defined(JSON_HEDLEY_INTEL_CL_VERSION)
  249. #undef JSON_HEDLEY_INTEL_CL_VERSION
  250. #endif
  251. #if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) && defined(__ICL)
  252. #define JSON_HEDLEY_INTEL_CL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER, __INTEL_COMPILER_UPDATE, 0)
  253. #endif
  254. #if defined(JSON_HEDLEY_INTEL_CL_VERSION_CHECK)
  255. #undef JSON_HEDLEY_INTEL_CL_VERSION_CHECK
  256. #endif
  257. #if defined(JSON_HEDLEY_INTEL_CL_VERSION)
  258. #define JSON_HEDLEY_INTEL_CL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_INTEL_CL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  259. #else
  260. #define JSON_HEDLEY_INTEL_CL_VERSION_CHECK(major,minor,patch) (0)
  261. #endif
  262. #if defined(JSON_HEDLEY_PGI_VERSION)
  263. #undef JSON_HEDLEY_PGI_VERSION
  264. #endif
  265. #if defined(__PGI) && defined(__PGIC__) && defined(__PGIC_MINOR__) && defined(__PGIC_PATCHLEVEL__)
  266. #define JSON_HEDLEY_PGI_VERSION JSON_HEDLEY_VERSION_ENCODE(__PGIC__, __PGIC_MINOR__, __PGIC_PATCHLEVEL__)
  267. #endif
  268. #if defined(JSON_HEDLEY_PGI_VERSION_CHECK)
  269. #undef JSON_HEDLEY_PGI_VERSION_CHECK
  270. #endif
  271. #if defined(JSON_HEDLEY_PGI_VERSION)
  272. #define JSON_HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_PGI_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  273. #else
  274. #define JSON_HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (0)
  275. #endif
  276. #if defined(JSON_HEDLEY_SUNPRO_VERSION)
  277. #undef JSON_HEDLEY_SUNPRO_VERSION
  278. #endif
  279. #if defined(__SUNPRO_C) && (__SUNPRO_C > 0x1000)
  280. #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)
  281. #elif defined(__SUNPRO_C)
  282. #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((__SUNPRO_C >> 8) & 0xf, (__SUNPRO_C >> 4) & 0xf, (__SUNPRO_C) & 0xf)
  283. #elif defined(__SUNPRO_CC) && (__SUNPRO_CC > 0x1000)
  284. #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)
  285. #elif defined(__SUNPRO_CC)
  286. #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((__SUNPRO_CC >> 8) & 0xf, (__SUNPRO_CC >> 4) & 0xf, (__SUNPRO_CC) & 0xf)
  287. #endif
  288. #if defined(JSON_HEDLEY_SUNPRO_VERSION_CHECK)
  289. #undef JSON_HEDLEY_SUNPRO_VERSION_CHECK
  290. #endif
  291. #if defined(JSON_HEDLEY_SUNPRO_VERSION)
  292. #define JSON_HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_SUNPRO_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  293. #else
  294. #define JSON_HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) (0)
  295. #endif
  296. #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION)
  297. #undef JSON_HEDLEY_EMSCRIPTEN_VERSION
  298. #endif
  299. #if defined(__EMSCRIPTEN__)
  300. #define JSON_HEDLEY_EMSCRIPTEN_VERSION JSON_HEDLEY_VERSION_ENCODE(__EMSCRIPTEN_major__, __EMSCRIPTEN_minor__, __EMSCRIPTEN_tiny__)
  301. #endif
  302. #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK)
  303. #undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK
  304. #endif
  305. #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION)
  306. #define JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_EMSCRIPTEN_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  307. #else
  308. #define JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) (0)
  309. #endif
  310. #if defined(JSON_HEDLEY_ARM_VERSION)
  311. #undef JSON_HEDLEY_ARM_VERSION
  312. #endif
  313. #if defined(__CC_ARM) && defined(__ARMCOMPILER_VERSION)
  314. #define JSON_HEDLEY_ARM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ARMCOMPILER_VERSION / 1000000, (__ARMCOMPILER_VERSION % 1000000) / 10000, (__ARMCOMPILER_VERSION % 10000) / 100)
  315. #elif defined(__CC_ARM) && defined(__ARMCC_VERSION)
  316. #define JSON_HEDLEY_ARM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ARMCC_VERSION / 1000000, (__ARMCC_VERSION % 1000000) / 10000, (__ARMCC_VERSION % 10000) / 100)
  317. #endif
  318. #if defined(JSON_HEDLEY_ARM_VERSION_CHECK)
  319. #undef JSON_HEDLEY_ARM_VERSION_CHECK
  320. #endif
  321. #if defined(JSON_HEDLEY_ARM_VERSION)
  322. #define JSON_HEDLEY_ARM_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_ARM_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  323. #else
  324. #define JSON_HEDLEY_ARM_VERSION_CHECK(major,minor,patch) (0)
  325. #endif
  326. #if defined(JSON_HEDLEY_IBM_VERSION)
  327. #undef JSON_HEDLEY_IBM_VERSION
  328. #endif
  329. #if defined(__ibmxl__)
  330. #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ibmxl_version__, __ibmxl_release__, __ibmxl_modification__)
  331. #elif defined(__xlC__) && defined(__xlC_ver__)
  332. #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__xlC__ >> 8, __xlC__ & 0xff, (__xlC_ver__ >> 8) & 0xff)
  333. #elif defined(__xlC__)
  334. #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__xlC__ >> 8, __xlC__ & 0xff, 0)
  335. #endif
  336. #if defined(JSON_HEDLEY_IBM_VERSION_CHECK)
  337. #undef JSON_HEDLEY_IBM_VERSION_CHECK
  338. #endif
  339. #if defined(JSON_HEDLEY_IBM_VERSION)
  340. #define JSON_HEDLEY_IBM_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_IBM_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  341. #else
  342. #define JSON_HEDLEY_IBM_VERSION_CHECK(major,minor,patch) (0)
  343. #endif
  344. #if defined(JSON_HEDLEY_TI_VERSION)
  345. #undef JSON_HEDLEY_TI_VERSION
  346. #endif
  347. #if \
  348. defined(__TI_COMPILER_VERSION__) && \
  349. ( \
  350. defined(__TMS470__) || defined(__TI_ARM__) || \
  351. defined(__MSP430__) || \
  352. defined(__TMS320C2000__) \
  353. )
  354. #if (__TI_COMPILER_VERSION__ >= 16000000)
  355. #define JSON_HEDLEY_TI_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  356. #endif
  357. #endif
  358. #if defined(JSON_HEDLEY_TI_VERSION_CHECK)
  359. #undef JSON_HEDLEY_TI_VERSION_CHECK
  360. #endif
  361. #if defined(JSON_HEDLEY_TI_VERSION)
  362. #define JSON_HEDLEY_TI_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  363. #else
  364. #define JSON_HEDLEY_TI_VERSION_CHECK(major,minor,patch) (0)
  365. #endif
  366. #if defined(JSON_HEDLEY_TI_CL2000_VERSION)
  367. #undef JSON_HEDLEY_TI_CL2000_VERSION
  368. #endif
  369. #if defined(__TI_COMPILER_VERSION__) && defined(__TMS320C2000__)
  370. #define JSON_HEDLEY_TI_CL2000_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  371. #endif
  372. #if defined(JSON_HEDLEY_TI_CL2000_VERSION_CHECK)
  373. #undef JSON_HEDLEY_TI_CL2000_VERSION_CHECK
  374. #endif
  375. #if defined(JSON_HEDLEY_TI_CL2000_VERSION)
  376. #define JSON_HEDLEY_TI_CL2000_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL2000_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  377. #else
  378. #define JSON_HEDLEY_TI_CL2000_VERSION_CHECK(major,minor,patch) (0)
  379. #endif
  380. #if defined(JSON_HEDLEY_TI_CL430_VERSION)
  381. #undef JSON_HEDLEY_TI_CL430_VERSION
  382. #endif
  383. #if defined(__TI_COMPILER_VERSION__) && defined(__MSP430__)
  384. #define JSON_HEDLEY_TI_CL430_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  385. #endif
  386. #if defined(JSON_HEDLEY_TI_CL430_VERSION_CHECK)
  387. #undef JSON_HEDLEY_TI_CL430_VERSION_CHECK
  388. #endif
  389. #if defined(JSON_HEDLEY_TI_CL430_VERSION)
  390. #define JSON_HEDLEY_TI_CL430_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL430_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  391. #else
  392. #define JSON_HEDLEY_TI_CL430_VERSION_CHECK(major,minor,patch) (0)
  393. #endif
  394. #if defined(JSON_HEDLEY_TI_ARMCL_VERSION)
  395. #undef JSON_HEDLEY_TI_ARMCL_VERSION
  396. #endif
  397. #if defined(__TI_COMPILER_VERSION__) && (defined(__TMS470__) || defined(__TI_ARM__))
  398. #define JSON_HEDLEY_TI_ARMCL_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  399. #endif
  400. #if defined(JSON_HEDLEY_TI_ARMCL_VERSION_CHECK)
  401. #undef JSON_HEDLEY_TI_ARMCL_VERSION_CHECK
  402. #endif
  403. #if defined(JSON_HEDLEY_TI_ARMCL_VERSION)
  404. #define JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_ARMCL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  405. #else
  406. #define JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(major,minor,patch) (0)
  407. #endif
  408. #if defined(JSON_HEDLEY_TI_CL6X_VERSION)
  409. #undef JSON_HEDLEY_TI_CL6X_VERSION
  410. #endif
  411. #if defined(__TI_COMPILER_VERSION__) && defined(__TMS320C6X__)
  412. #define JSON_HEDLEY_TI_CL6X_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  413. #endif
  414. #if defined(JSON_HEDLEY_TI_CL6X_VERSION_CHECK)
  415. #undef JSON_HEDLEY_TI_CL6X_VERSION_CHECK
  416. #endif
  417. #if defined(JSON_HEDLEY_TI_CL6X_VERSION)
  418. #define JSON_HEDLEY_TI_CL6X_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL6X_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  419. #else
  420. #define JSON_HEDLEY_TI_CL6X_VERSION_CHECK(major,minor,patch) (0)
  421. #endif
  422. #if defined(JSON_HEDLEY_TI_CL7X_VERSION)
  423. #undef JSON_HEDLEY_TI_CL7X_VERSION
  424. #endif
  425. #if defined(__TI_COMPILER_VERSION__) && defined(__C7000__)
  426. #define JSON_HEDLEY_TI_CL7X_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  427. #endif
  428. #if defined(JSON_HEDLEY_TI_CL7X_VERSION_CHECK)
  429. #undef JSON_HEDLEY_TI_CL7X_VERSION_CHECK
  430. #endif
  431. #if defined(JSON_HEDLEY_TI_CL7X_VERSION)
  432. #define JSON_HEDLEY_TI_CL7X_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL7X_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  433. #else
  434. #define JSON_HEDLEY_TI_CL7X_VERSION_CHECK(major,minor,patch) (0)
  435. #endif
  436. #if defined(JSON_HEDLEY_TI_CLPRU_VERSION)
  437. #undef JSON_HEDLEY_TI_CLPRU_VERSION
  438. #endif
  439. #if defined(__TI_COMPILER_VERSION__) && defined(__PRU__)
  440. #define JSON_HEDLEY_TI_CLPRU_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
  441. #endif
  442. #if defined(JSON_HEDLEY_TI_CLPRU_VERSION_CHECK)
  443. #undef JSON_HEDLEY_TI_CLPRU_VERSION_CHECK
  444. #endif
  445. #if defined(JSON_HEDLEY_TI_CLPRU_VERSION)
  446. #define JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CLPRU_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  447. #else
  448. #define JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(major,minor,patch) (0)
  449. #endif
  450. #if defined(JSON_HEDLEY_CRAY_VERSION)
  451. #undef JSON_HEDLEY_CRAY_VERSION
  452. #endif
  453. #if defined(_CRAYC)
  454. #if defined(_RELEASE_PATCHLEVEL)
  455. #define JSON_HEDLEY_CRAY_VERSION JSON_HEDLEY_VERSION_ENCODE(_RELEASE_MAJOR, _RELEASE_MINOR, _RELEASE_PATCHLEVEL)
  456. #else
  457. #define JSON_HEDLEY_CRAY_VERSION JSON_HEDLEY_VERSION_ENCODE(_RELEASE_MAJOR, _RELEASE_MINOR, 0)
  458. #endif
  459. #endif
  460. #if defined(JSON_HEDLEY_CRAY_VERSION_CHECK)
  461. #undef JSON_HEDLEY_CRAY_VERSION_CHECK
  462. #endif
  463. #if defined(JSON_HEDLEY_CRAY_VERSION)
  464. #define JSON_HEDLEY_CRAY_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_CRAY_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  465. #else
  466. #define JSON_HEDLEY_CRAY_VERSION_CHECK(major,minor,patch) (0)
  467. #endif
  468. #if defined(JSON_HEDLEY_IAR_VERSION)
  469. #undef JSON_HEDLEY_IAR_VERSION
  470. #endif
  471. #if defined(__IAR_SYSTEMS_ICC__)
  472. #if __VER__ > 1000
  473. #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE((__VER__ / 1000000), ((__VER__ / 1000) % 1000), (__VER__ % 1000))
  474. #else
  475. #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE(__VER__ / 100, __VER__ % 100, 0)
  476. #endif
  477. #endif
  478. #if defined(JSON_HEDLEY_IAR_VERSION_CHECK)
  479. #undef JSON_HEDLEY_IAR_VERSION_CHECK
  480. #endif
  481. #if defined(JSON_HEDLEY_IAR_VERSION)
  482. #define JSON_HEDLEY_IAR_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_IAR_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  483. #else
  484. #define JSON_HEDLEY_IAR_VERSION_CHECK(major,minor,patch) (0)
  485. #endif
  486. #if defined(JSON_HEDLEY_TINYC_VERSION)
  487. #undef JSON_HEDLEY_TINYC_VERSION
  488. #endif
  489. #if defined(__TINYC__)
  490. #define JSON_HEDLEY_TINYC_VERSION JSON_HEDLEY_VERSION_ENCODE(__TINYC__ / 1000, (__TINYC__ / 100) % 10, __TINYC__ % 100)
  491. #endif
  492. #if defined(JSON_HEDLEY_TINYC_VERSION_CHECK)
  493. #undef JSON_HEDLEY_TINYC_VERSION_CHECK
  494. #endif
  495. #if defined(JSON_HEDLEY_TINYC_VERSION)
  496. #define JSON_HEDLEY_TINYC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TINYC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  497. #else
  498. #define JSON_HEDLEY_TINYC_VERSION_CHECK(major,minor,patch) (0)
  499. #endif
  500. #if defined(JSON_HEDLEY_DMC_VERSION)
  501. #undef JSON_HEDLEY_DMC_VERSION
  502. #endif
  503. #if defined(__DMC__)
  504. #define JSON_HEDLEY_DMC_VERSION JSON_HEDLEY_VERSION_ENCODE(__DMC__ >> 8, (__DMC__ >> 4) & 0xf, __DMC__ & 0xf)
  505. #endif
  506. #if defined(JSON_HEDLEY_DMC_VERSION_CHECK)
  507. #undef JSON_HEDLEY_DMC_VERSION_CHECK
  508. #endif
  509. #if defined(JSON_HEDLEY_DMC_VERSION)
  510. #define JSON_HEDLEY_DMC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_DMC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  511. #else
  512. #define JSON_HEDLEY_DMC_VERSION_CHECK(major,minor,patch) (0)
  513. #endif
  514. #if defined(JSON_HEDLEY_COMPCERT_VERSION)
  515. #undef JSON_HEDLEY_COMPCERT_VERSION
  516. #endif
  517. #if defined(__COMPCERT_VERSION__)
  518. #define JSON_HEDLEY_COMPCERT_VERSION JSON_HEDLEY_VERSION_ENCODE(__COMPCERT_VERSION__ / 10000, (__COMPCERT_VERSION__ / 100) % 100, __COMPCERT_VERSION__ % 100)
  519. #endif
  520. #if defined(JSON_HEDLEY_COMPCERT_VERSION_CHECK)
  521. #undef JSON_HEDLEY_COMPCERT_VERSION_CHECK
  522. #endif
  523. #if defined(JSON_HEDLEY_COMPCERT_VERSION)
  524. #define JSON_HEDLEY_COMPCERT_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_COMPCERT_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  525. #else
  526. #define JSON_HEDLEY_COMPCERT_VERSION_CHECK(major,minor,patch) (0)
  527. #endif
  528. #if defined(JSON_HEDLEY_PELLES_VERSION)
  529. #undef JSON_HEDLEY_PELLES_VERSION
  530. #endif
  531. #if defined(__POCC__)
  532. #define JSON_HEDLEY_PELLES_VERSION JSON_HEDLEY_VERSION_ENCODE(__POCC__ / 100, __POCC__ % 100, 0)
  533. #endif
  534. #if defined(JSON_HEDLEY_PELLES_VERSION_CHECK)
  535. #undef JSON_HEDLEY_PELLES_VERSION_CHECK
  536. #endif
  537. #if defined(JSON_HEDLEY_PELLES_VERSION)
  538. #define JSON_HEDLEY_PELLES_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_PELLES_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  539. #else
  540. #define JSON_HEDLEY_PELLES_VERSION_CHECK(major,minor,patch) (0)
  541. #endif
  542. #if defined(JSON_HEDLEY_MCST_LCC_VERSION)
  543. #undef JSON_HEDLEY_MCST_LCC_VERSION
  544. #endif
  545. #if defined(__LCC__) && defined(__LCC_MINOR__)
  546. #define JSON_HEDLEY_MCST_LCC_VERSION JSON_HEDLEY_VERSION_ENCODE(__LCC__ / 100, __LCC__ % 100, __LCC_MINOR__)
  547. #endif
  548. #if defined(JSON_HEDLEY_MCST_LCC_VERSION_CHECK)
  549. #undef JSON_HEDLEY_MCST_LCC_VERSION_CHECK
  550. #endif
  551. #if defined(JSON_HEDLEY_MCST_LCC_VERSION)
  552. #define JSON_HEDLEY_MCST_LCC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_MCST_LCC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  553. #else
  554. #define JSON_HEDLEY_MCST_LCC_VERSION_CHECK(major,minor,patch) (0)
  555. #endif
  556. #if defined(JSON_HEDLEY_GCC_VERSION)
  557. #undef JSON_HEDLEY_GCC_VERSION
  558. #endif
  559. #if \
  560. defined(JSON_HEDLEY_GNUC_VERSION) && \
  561. !defined(__clang__) && \
  562. !defined(JSON_HEDLEY_INTEL_VERSION) && \
  563. !defined(JSON_HEDLEY_PGI_VERSION) && \
  564. !defined(JSON_HEDLEY_ARM_VERSION) && \
  565. !defined(JSON_HEDLEY_CRAY_VERSION) && \
  566. !defined(JSON_HEDLEY_TI_VERSION) && \
  567. !defined(JSON_HEDLEY_TI_ARMCL_VERSION) && \
  568. !defined(JSON_HEDLEY_TI_CL430_VERSION) && \
  569. !defined(JSON_HEDLEY_TI_CL2000_VERSION) && \
  570. !defined(JSON_HEDLEY_TI_CL6X_VERSION) && \
  571. !defined(JSON_HEDLEY_TI_CL7X_VERSION) && \
  572. !defined(JSON_HEDLEY_TI_CLPRU_VERSION) && \
  573. !defined(__COMPCERT__) && \
  574. !defined(JSON_HEDLEY_MCST_LCC_VERSION)
  575. #define JSON_HEDLEY_GCC_VERSION JSON_HEDLEY_GNUC_VERSION
  576. #endif
  577. #if defined(JSON_HEDLEY_GCC_VERSION_CHECK)
  578. #undef JSON_HEDLEY_GCC_VERSION_CHECK
  579. #endif
  580. #if defined(JSON_HEDLEY_GCC_VERSION)
  581. #define JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_GCC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
  582. #else
  583. #define JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (0)
  584. #endif
  585. #if defined(JSON_HEDLEY_HAS_ATTRIBUTE)
  586. #undef JSON_HEDLEY_HAS_ATTRIBUTE
  587. #endif
  588. #if \
  589. defined(__has_attribute) && \
  590. ( \
  591. (!defined(JSON_HEDLEY_IAR_VERSION) || JSON_HEDLEY_IAR_VERSION_CHECK(8,5,9)) \
  592. )
  593. # define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) __has_attribute(attribute)
  594. #else
  595. # define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) (0)
  596. #endif
  597. #if defined(JSON_HEDLEY_GNUC_HAS_ATTRIBUTE)
  598. #undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE
  599. #endif
  600. #if defined(__has_attribute)
  601. #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
  602. #else
  603. #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  604. #endif
  605. #if defined(JSON_HEDLEY_GCC_HAS_ATTRIBUTE)
  606. #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE
  607. #endif
  608. #if defined(__has_attribute)
  609. #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
  610. #else
  611. #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  612. #endif
  613. #if defined(JSON_HEDLEY_HAS_CPP_ATTRIBUTE)
  614. #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE
  615. #endif
  616. #if \
  617. defined(__has_cpp_attribute) && \
  618. defined(__cplusplus) && \
  619. (!defined(JSON_HEDLEY_SUNPRO_VERSION) || JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0))
  620. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) __has_cpp_attribute(attribute)
  621. #else
  622. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) (0)
  623. #endif
  624. #if defined(JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS)
  625. #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS
  626. #endif
  627. #if !defined(__cplusplus) || !defined(__has_cpp_attribute)
  628. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) (0)
  629. #elif \
  630. !defined(JSON_HEDLEY_PGI_VERSION) && \
  631. !defined(JSON_HEDLEY_IAR_VERSION) && \
  632. (!defined(JSON_HEDLEY_SUNPRO_VERSION) || JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0)) && \
  633. (!defined(JSON_HEDLEY_MSVC_VERSION) || JSON_HEDLEY_MSVC_VERSION_CHECK(19,20,0))
  634. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) JSON_HEDLEY_HAS_CPP_ATTRIBUTE(ns::attribute)
  635. #else
  636. #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) (0)
  637. #endif
  638. #if defined(JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE)
  639. #undef JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE
  640. #endif
  641. #if defined(__has_cpp_attribute) && defined(__cplusplus)
  642. #define JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) __has_cpp_attribute(attribute)
  643. #else
  644. #define JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  645. #endif
  646. #if defined(JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE)
  647. #undef JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE
  648. #endif
  649. #if defined(__has_cpp_attribute) && defined(__cplusplus)
  650. #define JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) __has_cpp_attribute(attribute)
  651. #else
  652. #define JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  653. #endif
  654. #if defined(JSON_HEDLEY_HAS_BUILTIN)
  655. #undef JSON_HEDLEY_HAS_BUILTIN
  656. #endif
  657. #if defined(__has_builtin)
  658. #define JSON_HEDLEY_HAS_BUILTIN(builtin) __has_builtin(builtin)
  659. #else
  660. #define JSON_HEDLEY_HAS_BUILTIN(builtin) (0)
  661. #endif
  662. #if defined(JSON_HEDLEY_GNUC_HAS_BUILTIN)
  663. #undef JSON_HEDLEY_GNUC_HAS_BUILTIN
  664. #endif
  665. #if defined(__has_builtin)
  666. #define JSON_HEDLEY_GNUC_HAS_BUILTIN(builtin,major,minor,patch) __has_builtin(builtin)
  667. #else
  668. #define JSON_HEDLEY_GNUC_HAS_BUILTIN(builtin,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  669. #endif
  670. #if defined(JSON_HEDLEY_GCC_HAS_BUILTIN)
  671. #undef JSON_HEDLEY_GCC_HAS_BUILTIN
  672. #endif
  673. #if defined(__has_builtin)
  674. #define JSON_HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) __has_builtin(builtin)
  675. #else
  676. #define JSON_HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  677. #endif
  678. #if defined(JSON_HEDLEY_HAS_FEATURE)
  679. #undef JSON_HEDLEY_HAS_FEATURE
  680. #endif
  681. #if defined(__has_feature)
  682. #define JSON_HEDLEY_HAS_FEATURE(feature) __has_feature(feature)
  683. #else
  684. #define JSON_HEDLEY_HAS_FEATURE(feature) (0)
  685. #endif
  686. #if defined(JSON_HEDLEY_GNUC_HAS_FEATURE)
  687. #undef JSON_HEDLEY_GNUC_HAS_FEATURE
  688. #endif
  689. #if defined(__has_feature)
  690. #define JSON_HEDLEY_GNUC_HAS_FEATURE(feature,major,minor,patch) __has_feature(feature)
  691. #else
  692. #define JSON_HEDLEY_GNUC_HAS_FEATURE(feature,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  693. #endif
  694. #if defined(JSON_HEDLEY_GCC_HAS_FEATURE)
  695. #undef JSON_HEDLEY_GCC_HAS_FEATURE
  696. #endif
  697. #if defined(__has_feature)
  698. #define JSON_HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) __has_feature(feature)
  699. #else
  700. #define JSON_HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  701. #endif
  702. #if defined(JSON_HEDLEY_HAS_EXTENSION)
  703. #undef JSON_HEDLEY_HAS_EXTENSION
  704. #endif
  705. #if defined(__has_extension)
  706. #define JSON_HEDLEY_HAS_EXTENSION(extension) __has_extension(extension)
  707. #else
  708. #define JSON_HEDLEY_HAS_EXTENSION(extension) (0)
  709. #endif
  710. #if defined(JSON_HEDLEY_GNUC_HAS_EXTENSION)
  711. #undef JSON_HEDLEY_GNUC_HAS_EXTENSION
  712. #endif
  713. #if defined(__has_extension)
  714. #define JSON_HEDLEY_GNUC_HAS_EXTENSION(extension,major,minor,patch) __has_extension(extension)
  715. #else
  716. #define JSON_HEDLEY_GNUC_HAS_EXTENSION(extension,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  717. #endif
  718. #if defined(JSON_HEDLEY_GCC_HAS_EXTENSION)
  719. #undef JSON_HEDLEY_GCC_HAS_EXTENSION
  720. #endif
  721. #if defined(__has_extension)
  722. #define JSON_HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) __has_extension(extension)
  723. #else
  724. #define JSON_HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  725. #endif
  726. #if defined(JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE)
  727. #undef JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE
  728. #endif
  729. #if defined(__has_declspec_attribute)
  730. #define JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) __has_declspec_attribute(attribute)
  731. #else
  732. #define JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) (0)
  733. #endif
  734. #if defined(JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE)
  735. #undef JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE
  736. #endif
  737. #if defined(__has_declspec_attribute)
  738. #define JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) __has_declspec_attribute(attribute)
  739. #else
  740. #define JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  741. #endif
  742. #if defined(JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE)
  743. #undef JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE
  744. #endif
  745. #if defined(__has_declspec_attribute)
  746. #define JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) __has_declspec_attribute(attribute)
  747. #else
  748. #define JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  749. #endif
  750. #if defined(JSON_HEDLEY_HAS_WARNING)
  751. #undef JSON_HEDLEY_HAS_WARNING
  752. #endif
  753. #if defined(__has_warning)
  754. #define JSON_HEDLEY_HAS_WARNING(warning) __has_warning(warning)
  755. #else
  756. #define JSON_HEDLEY_HAS_WARNING(warning) (0)
  757. #endif
  758. #if defined(JSON_HEDLEY_GNUC_HAS_WARNING)
  759. #undef JSON_HEDLEY_GNUC_HAS_WARNING
  760. #endif
  761. #if defined(__has_warning)
  762. #define JSON_HEDLEY_GNUC_HAS_WARNING(warning,major,minor,patch) __has_warning(warning)
  763. #else
  764. #define JSON_HEDLEY_GNUC_HAS_WARNING(warning,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
  765. #endif
  766. #if defined(JSON_HEDLEY_GCC_HAS_WARNING)
  767. #undef JSON_HEDLEY_GCC_HAS_WARNING
  768. #endif
  769. #if defined(__has_warning)
  770. #define JSON_HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) __has_warning(warning)
  771. #else
  772. #define JSON_HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  773. #endif
  774. #if \
  775. (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
  776. defined(__clang__) || \
  777. JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \
  778. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  779. JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \
  780. JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \
  781. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  782. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  783. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,7,0) || \
  784. JSON_HEDLEY_TI_CL430_VERSION_CHECK(2,0,1) || \
  785. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,1,0) || \
  786. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,0,0) || \
  787. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  788. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  789. JSON_HEDLEY_CRAY_VERSION_CHECK(5,0,0) || \
  790. JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,17) || \
  791. JSON_HEDLEY_SUNPRO_VERSION_CHECK(8,0,0) || \
  792. (JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) && defined(__C99_PRAGMA_OPERATOR))
  793. #define JSON_HEDLEY_PRAGMA(value) _Pragma(#value)
  794. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
  795. #define JSON_HEDLEY_PRAGMA(value) __pragma(value)
  796. #else
  797. #define JSON_HEDLEY_PRAGMA(value)
  798. #endif
  799. #if defined(JSON_HEDLEY_DIAGNOSTIC_PUSH)
  800. #undef JSON_HEDLEY_DIAGNOSTIC_PUSH
  801. #endif
  802. #if defined(JSON_HEDLEY_DIAGNOSTIC_POP)
  803. #undef JSON_HEDLEY_DIAGNOSTIC_POP
  804. #endif
  805. #if defined(__clang__)
  806. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("clang diagnostic push")
  807. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("clang diagnostic pop")
  808. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  809. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)")
  810. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)")
  811. #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0)
  812. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push")
  813. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("GCC diagnostic pop")
  814. #elif \
  815. JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) || \
  816. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  817. #define JSON_HEDLEY_DIAGNOSTIC_PUSH __pragma(warning(push))
  818. #define JSON_HEDLEY_DIAGNOSTIC_POP __pragma(warning(pop))
  819. #elif JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0)
  820. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("push")
  821. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("pop")
  822. #elif \
  823. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  824. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  825. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,4,0) || \
  826. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,1,0) || \
  827. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  828. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0)
  829. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("diag_push")
  830. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("diag_pop")
  831. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,90,0)
  832. #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)")
  833. #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)")
  834. #else
  835. #define JSON_HEDLEY_DIAGNOSTIC_PUSH
  836. #define JSON_HEDLEY_DIAGNOSTIC_POP
  837. #endif
  838. /* JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_ is for
  839. HEDLEY INTERNAL USE ONLY. API subject to change without notice. */
  840. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_)
  841. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_
  842. #endif
  843. #if defined(__cplusplus)
  844. # if JSON_HEDLEY_HAS_WARNING("-Wc++98-compat")
  845. # if JSON_HEDLEY_HAS_WARNING("-Wc++17-extensions")
  846. # if JSON_HEDLEY_HAS_WARNING("-Wc++1z-extensions")
  847. # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
  848. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  849. _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
  850. _Pragma("clang diagnostic ignored \"-Wc++17-extensions\"") \
  851. _Pragma("clang diagnostic ignored \"-Wc++1z-extensions\"") \
  852. xpr \
  853. JSON_HEDLEY_DIAGNOSTIC_POP
  854. # else
  855. # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
  856. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  857. _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
  858. _Pragma("clang diagnostic ignored \"-Wc++17-extensions\"") \
  859. xpr \
  860. JSON_HEDLEY_DIAGNOSTIC_POP
  861. # endif
  862. # else
  863. # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
  864. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  865. _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
  866. xpr \
  867. JSON_HEDLEY_DIAGNOSTIC_POP
  868. # endif
  869. # endif
  870. #endif
  871. #if !defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_)
  872. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(x) x
  873. #endif
  874. #if defined(JSON_HEDLEY_CONST_CAST)
  875. #undef JSON_HEDLEY_CONST_CAST
  876. #endif
  877. #if defined(__cplusplus)
  878. # define JSON_HEDLEY_CONST_CAST(T, expr) (const_cast<T>(expr))
  879. #elif \
  880. JSON_HEDLEY_HAS_WARNING("-Wcast-qual") || \
  881. JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0) || \
  882. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  883. # define JSON_HEDLEY_CONST_CAST(T, expr) (__extension__ ({ \
  884. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  885. JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL \
  886. ((T) (expr)); \
  887. JSON_HEDLEY_DIAGNOSTIC_POP \
  888. }))
  889. #else
  890. # define JSON_HEDLEY_CONST_CAST(T, expr) ((T) (expr))
  891. #endif
  892. #if defined(JSON_HEDLEY_REINTERPRET_CAST)
  893. #undef JSON_HEDLEY_REINTERPRET_CAST
  894. #endif
  895. #if defined(__cplusplus)
  896. #define JSON_HEDLEY_REINTERPRET_CAST(T, expr) (reinterpret_cast<T>(expr))
  897. #else
  898. #define JSON_HEDLEY_REINTERPRET_CAST(T, expr) ((T) (expr))
  899. #endif
  900. #if defined(JSON_HEDLEY_STATIC_CAST)
  901. #undef JSON_HEDLEY_STATIC_CAST
  902. #endif
  903. #if defined(__cplusplus)
  904. #define JSON_HEDLEY_STATIC_CAST(T, expr) (static_cast<T>(expr))
  905. #else
  906. #define JSON_HEDLEY_STATIC_CAST(T, expr) ((T) (expr))
  907. #endif
  908. #if defined(JSON_HEDLEY_CPP_CAST)
  909. #undef JSON_HEDLEY_CPP_CAST
  910. #endif
  911. #if defined(__cplusplus)
  912. # if JSON_HEDLEY_HAS_WARNING("-Wold-style-cast")
  913. # define JSON_HEDLEY_CPP_CAST(T, expr) \
  914. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  915. _Pragma("clang diagnostic ignored \"-Wold-style-cast\"") \
  916. ((T) (expr)) \
  917. JSON_HEDLEY_DIAGNOSTIC_POP
  918. # elif JSON_HEDLEY_IAR_VERSION_CHECK(8,3,0)
  919. # define JSON_HEDLEY_CPP_CAST(T, expr) \
  920. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  921. _Pragma("diag_suppress=Pe137") \
  922. JSON_HEDLEY_DIAGNOSTIC_POP
  923. # else
  924. # define JSON_HEDLEY_CPP_CAST(T, expr) ((T) (expr))
  925. # endif
  926. #else
  927. # define JSON_HEDLEY_CPP_CAST(T, expr) (expr)
  928. #endif
  929. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED)
  930. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
  931. #endif
  932. #if JSON_HEDLEY_HAS_WARNING("-Wdeprecated-declarations")
  933. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"")
  934. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  935. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warning(disable:1478 1786)")
  936. #elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  937. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:1478 1786))
  938. #elif JSON_HEDLEY_PGI_VERSION_CHECK(20,7,0)
  939. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1216,1444,1445")
  940. #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
  941. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1444")
  942. #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0)
  943. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
  944. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
  945. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:4996))
  946. #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  947. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1444")
  948. #elif \
  949. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  950. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  951. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  952. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  953. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  954. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  955. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  956. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  957. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  958. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  959. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0)
  960. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1291,1718")
  961. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) && !defined(__cplusplus)
  962. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("error_messages(off,E_DEPRECATED_ATT,E_DEPRECATED_ATT_MESS)")
  963. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) && defined(__cplusplus)
  964. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("error_messages(off,symdeprecated,symdeprecated2)")
  965. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  966. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress=Pe1444,Pe1215")
  967. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,90,0)
  968. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warn(disable:2241)")
  969. #else
  970. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
  971. #endif
  972. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS)
  973. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
  974. #endif
  975. #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
  976. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("clang diagnostic ignored \"-Wunknown-pragmas\"")
  977. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  978. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("warning(disable:161)")
  979. #elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  980. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS __pragma(warning(disable:161))
  981. #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
  982. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 1675")
  983. #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0)
  984. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("GCC diagnostic ignored \"-Wunknown-pragmas\"")
  985. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
  986. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS __pragma(warning(disable:4068))
  987. #elif \
  988. JSON_HEDLEY_TI_VERSION_CHECK(16,9,0) || \
  989. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) || \
  990. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  991. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0)
  992. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163")
  993. #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0)
  994. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163")
  995. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  996. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress=Pe161")
  997. #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  998. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 161")
  999. #else
  1000. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
  1001. #endif
  1002. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES)
  1003. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
  1004. #endif
  1005. #if JSON_HEDLEY_HAS_WARNING("-Wunknown-attributes")
  1006. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("clang diagnostic ignored \"-Wunknown-attributes\"")
  1007. #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0)
  1008. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
  1009. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(17,0,0)
  1010. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("warning(disable:1292)")
  1011. #elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1012. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES __pragma(warning(disable:1292))
  1013. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,0)
  1014. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES __pragma(warning(disable:5030))
  1015. #elif JSON_HEDLEY_PGI_VERSION_CHECK(20,7,0)
  1016. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097,1098")
  1017. #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
  1018. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097")
  1019. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,14,0) && defined(__cplusplus)
  1020. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("error_messages(off,attrskipunsup)")
  1021. #elif \
  1022. JSON_HEDLEY_TI_VERSION_CHECK(18,1,0) || \
  1023. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,3,0) || \
  1024. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0)
  1025. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1173")
  1026. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1027. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress=Pe1097")
  1028. #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1029. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097")
  1030. #else
  1031. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
  1032. #endif
  1033. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL)
  1034. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
  1035. #endif
  1036. #if JSON_HEDLEY_HAS_WARNING("-Wcast-qual")
  1037. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("clang diagnostic ignored \"-Wcast-qual\"")
  1038. #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  1039. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("warning(disable:2203 2331)")
  1040. #elif JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0)
  1041. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("GCC diagnostic ignored \"-Wcast-qual\"")
  1042. #else
  1043. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
  1044. #endif
  1045. #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION)
  1046. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
  1047. #endif
  1048. #if JSON_HEDLEY_HAS_WARNING("-Wunused-function")
  1049. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("clang diagnostic ignored \"-Wunused-function\"")
  1050. #elif JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0)
  1051. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("GCC diagnostic ignored \"-Wunused-function\"")
  1052. #elif JSON_HEDLEY_MSVC_VERSION_CHECK(1,0,0)
  1053. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION __pragma(warning(disable:4505))
  1054. #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1055. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("diag_suppress 3142")
  1056. #else
  1057. #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
  1058. #endif
  1059. #if defined(JSON_HEDLEY_DEPRECATED)
  1060. #undef JSON_HEDLEY_DEPRECATED
  1061. #endif
  1062. #if defined(JSON_HEDLEY_DEPRECATED_FOR)
  1063. #undef JSON_HEDLEY_DEPRECATED_FOR
  1064. #endif
  1065. #if \
  1066. JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
  1067. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1068. #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated("Since " # since))
  1069. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated("Since " #since "; use " #replacement))
  1070. #elif \
  1071. (JSON_HEDLEY_HAS_EXTENSION(attribute_deprecated_with_message) && !defined(JSON_HEDLEY_IAR_VERSION)) || \
  1072. JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \
  1073. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1074. JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) || \
  1075. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) || \
  1076. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1077. JSON_HEDLEY_TI_VERSION_CHECK(18,1,0) || \
  1078. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(18,1,0) || \
  1079. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,3,0) || \
  1080. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1081. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0) || \
  1082. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1083. #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__("Since " #since)))
  1084. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__("Since " #since "; use " #replacement)))
  1085. #elif defined(__cplusplus) && (__cplusplus >= 201402L)
  1086. #define JSON_HEDLEY_DEPRECATED(since) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since)]])
  1087. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since "; use " #replacement)]])
  1088. #elif \
  1089. JSON_HEDLEY_HAS_ATTRIBUTE(deprecated) || \
  1090. JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
  1091. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1092. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1093. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1094. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1095. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1096. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1097. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1098. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1099. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1100. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1101. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1102. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1103. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \
  1104. JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
  1105. #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__))
  1106. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__))
  1107. #elif \
  1108. JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
  1109. JSON_HEDLEY_PELLES_VERSION_CHECK(6,50,0) || \
  1110. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1111. #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated)
  1112. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated)
  1113. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1114. #define JSON_HEDLEY_DEPRECATED(since) _Pragma("deprecated")
  1115. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) _Pragma("deprecated")
  1116. #else
  1117. #define JSON_HEDLEY_DEPRECATED(since)
  1118. #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement)
  1119. #endif
  1120. #if defined(JSON_HEDLEY_UNAVAILABLE)
  1121. #undef JSON_HEDLEY_UNAVAILABLE
  1122. #endif
  1123. #if \
  1124. JSON_HEDLEY_HAS_ATTRIBUTE(warning) || \
  1125. JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0) || \
  1126. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1127. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1128. #define JSON_HEDLEY_UNAVAILABLE(available_since) __attribute__((__warning__("Not available until " #available_since)))
  1129. #else
  1130. #define JSON_HEDLEY_UNAVAILABLE(available_since)
  1131. #endif
  1132. #if defined(JSON_HEDLEY_WARN_UNUSED_RESULT)
  1133. #undef JSON_HEDLEY_WARN_UNUSED_RESULT
  1134. #endif
  1135. #if defined(JSON_HEDLEY_WARN_UNUSED_RESULT_MSG)
  1136. #undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG
  1137. #endif
  1138. #if \
  1139. JSON_HEDLEY_HAS_ATTRIBUTE(warn_unused_result) || \
  1140. JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
  1141. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1142. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1143. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1144. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1145. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1146. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1147. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1148. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1149. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1150. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1151. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1152. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1153. (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)) || \
  1154. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1155. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1156. #define JSON_HEDLEY_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
  1157. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) __attribute__((__warn_unused_result__))
  1158. #elif (JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard) >= 201907L)
  1159. #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
  1160. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard(msg)]])
  1161. #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard)
  1162. #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
  1163. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
  1164. #elif defined(_Check_return_) /* SAL */
  1165. #define JSON_HEDLEY_WARN_UNUSED_RESULT _Check_return_
  1166. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) _Check_return_
  1167. #else
  1168. #define JSON_HEDLEY_WARN_UNUSED_RESULT
  1169. #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg)
  1170. #endif
  1171. #if defined(JSON_HEDLEY_SENTINEL)
  1172. #undef JSON_HEDLEY_SENTINEL
  1173. #endif
  1174. #if \
  1175. JSON_HEDLEY_HAS_ATTRIBUTE(sentinel) || \
  1176. JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
  1177. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1178. JSON_HEDLEY_ARM_VERSION_CHECK(5,4,0) || \
  1179. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1180. #define JSON_HEDLEY_SENTINEL(position) __attribute__((__sentinel__(position)))
  1181. #else
  1182. #define JSON_HEDLEY_SENTINEL(position)
  1183. #endif
  1184. #if defined(JSON_HEDLEY_NO_RETURN)
  1185. #undef JSON_HEDLEY_NO_RETURN
  1186. #endif
  1187. #if JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1188. #define JSON_HEDLEY_NO_RETURN __noreturn
  1189. #elif \
  1190. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1191. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1192. #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__))
  1193. #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
  1194. #define JSON_HEDLEY_NO_RETURN _Noreturn
  1195. #elif defined(__cplusplus) && (__cplusplus >= 201103L)
  1196. #define JSON_HEDLEY_NO_RETURN JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[noreturn]])
  1197. #elif \
  1198. JSON_HEDLEY_HAS_ATTRIBUTE(noreturn) || \
  1199. JSON_HEDLEY_GCC_VERSION_CHECK(3,2,0) || \
  1200. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1201. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1202. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1203. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1204. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1205. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1206. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1207. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1208. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1209. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1210. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1211. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1212. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1213. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1214. JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
  1215. #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__))
  1216. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
  1217. #define JSON_HEDLEY_NO_RETURN _Pragma("does_not_return")
  1218. #elif \
  1219. JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
  1220. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1221. #define JSON_HEDLEY_NO_RETURN __declspec(noreturn)
  1222. #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,0,0) && defined(__cplusplus)
  1223. #define JSON_HEDLEY_NO_RETURN _Pragma("FUNC_NEVER_RETURNS;")
  1224. #elif JSON_HEDLEY_COMPCERT_VERSION_CHECK(3,2,0)
  1225. #define JSON_HEDLEY_NO_RETURN __attribute((noreturn))
  1226. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(9,0,0)
  1227. #define JSON_HEDLEY_NO_RETURN __declspec(noreturn)
  1228. #else
  1229. #define JSON_HEDLEY_NO_RETURN
  1230. #endif
  1231. #if defined(JSON_HEDLEY_NO_ESCAPE)
  1232. #undef JSON_HEDLEY_NO_ESCAPE
  1233. #endif
  1234. #if JSON_HEDLEY_HAS_ATTRIBUTE(noescape)
  1235. #define JSON_HEDLEY_NO_ESCAPE __attribute__((__noescape__))
  1236. #else
  1237. #define JSON_HEDLEY_NO_ESCAPE
  1238. #endif
  1239. #if defined(JSON_HEDLEY_UNREACHABLE)
  1240. #undef JSON_HEDLEY_UNREACHABLE
  1241. #endif
  1242. #if defined(JSON_HEDLEY_UNREACHABLE_RETURN)
  1243. #undef JSON_HEDLEY_UNREACHABLE_RETURN
  1244. #endif
  1245. #if defined(JSON_HEDLEY_ASSUME)
  1246. #undef JSON_HEDLEY_ASSUME
  1247. #endif
  1248. #if \
  1249. JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
  1250. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1251. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1252. #define JSON_HEDLEY_ASSUME(expr) __assume(expr)
  1253. #elif JSON_HEDLEY_HAS_BUILTIN(__builtin_assume)
  1254. #define JSON_HEDLEY_ASSUME(expr) __builtin_assume(expr)
  1255. #elif \
  1256. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
  1257. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0)
  1258. #if defined(__cplusplus)
  1259. #define JSON_HEDLEY_ASSUME(expr) std::_nassert(expr)
  1260. #else
  1261. #define JSON_HEDLEY_ASSUME(expr) _nassert(expr)
  1262. #endif
  1263. #endif
  1264. #if \
  1265. (JSON_HEDLEY_HAS_BUILTIN(__builtin_unreachable) && (!defined(JSON_HEDLEY_ARM_VERSION))) || \
  1266. JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \
  1267. JSON_HEDLEY_PGI_VERSION_CHECK(18,10,0) || \
  1268. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1269. JSON_HEDLEY_IBM_VERSION_CHECK(13,1,5) || \
  1270. JSON_HEDLEY_CRAY_VERSION_CHECK(10,0,0) || \
  1271. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1272. #define JSON_HEDLEY_UNREACHABLE() __builtin_unreachable()
  1273. #elif defined(JSON_HEDLEY_ASSUME)
  1274. #define JSON_HEDLEY_UNREACHABLE() JSON_HEDLEY_ASSUME(0)
  1275. #endif
  1276. #if !defined(JSON_HEDLEY_ASSUME)
  1277. #if defined(JSON_HEDLEY_UNREACHABLE)
  1278. #define JSON_HEDLEY_ASSUME(expr) JSON_HEDLEY_STATIC_CAST(void, ((expr) ? 1 : (JSON_HEDLEY_UNREACHABLE(), 1)))
  1279. #else
  1280. #define JSON_HEDLEY_ASSUME(expr) JSON_HEDLEY_STATIC_CAST(void, expr)
  1281. #endif
  1282. #endif
  1283. #if defined(JSON_HEDLEY_UNREACHABLE)
  1284. #if \
  1285. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
  1286. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0)
  1287. #define JSON_HEDLEY_UNREACHABLE_RETURN(value) return (JSON_HEDLEY_STATIC_CAST(void, JSON_HEDLEY_ASSUME(0)), (value))
  1288. #else
  1289. #define JSON_HEDLEY_UNREACHABLE_RETURN(value) JSON_HEDLEY_UNREACHABLE()
  1290. #endif
  1291. #else
  1292. #define JSON_HEDLEY_UNREACHABLE_RETURN(value) return (value)
  1293. #endif
  1294. #if !defined(JSON_HEDLEY_UNREACHABLE)
  1295. #define JSON_HEDLEY_UNREACHABLE() JSON_HEDLEY_ASSUME(0)
  1296. #endif
  1297. JSON_HEDLEY_DIAGNOSTIC_PUSH
  1298. #if JSON_HEDLEY_HAS_WARNING("-Wpedantic")
  1299. #pragma clang diagnostic ignored "-Wpedantic"
  1300. #endif
  1301. #if JSON_HEDLEY_HAS_WARNING("-Wc++98-compat-pedantic") && defined(__cplusplus)
  1302. #pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
  1303. #endif
  1304. #if JSON_HEDLEY_GCC_HAS_WARNING("-Wvariadic-macros",4,0,0)
  1305. #if defined(__clang__)
  1306. #pragma clang diagnostic ignored "-Wvariadic-macros"
  1307. #elif defined(JSON_HEDLEY_GCC_VERSION)
  1308. #pragma GCC diagnostic ignored "-Wvariadic-macros"
  1309. #endif
  1310. #endif
  1311. #if defined(JSON_HEDLEY_NON_NULL)
  1312. #undef JSON_HEDLEY_NON_NULL
  1313. #endif
  1314. #if \
  1315. JSON_HEDLEY_HAS_ATTRIBUTE(nonnull) || \
  1316. JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
  1317. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1318. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0)
  1319. #define JSON_HEDLEY_NON_NULL(...) __attribute__((__nonnull__(__VA_ARGS__)))
  1320. #else
  1321. #define JSON_HEDLEY_NON_NULL(...)
  1322. #endif
  1323. JSON_HEDLEY_DIAGNOSTIC_POP
  1324. #if defined(JSON_HEDLEY_PRINTF_FORMAT)
  1325. #undef JSON_HEDLEY_PRINTF_FORMAT
  1326. #endif
  1327. #if defined(__MINGW32__) && JSON_HEDLEY_GCC_HAS_ATTRIBUTE(format,4,4,0) && !defined(__USE_MINGW_ANSI_STDIO)
  1328. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(ms_printf, string_idx, first_to_check)))
  1329. #elif defined(__MINGW32__) && JSON_HEDLEY_GCC_HAS_ATTRIBUTE(format,4,4,0) && defined(__USE_MINGW_ANSI_STDIO)
  1330. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(gnu_printf, string_idx, first_to_check)))
  1331. #elif \
  1332. JSON_HEDLEY_HAS_ATTRIBUTE(format) || \
  1333. JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
  1334. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1335. JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) || \
  1336. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1337. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1338. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1339. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1340. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1341. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1342. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1343. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1344. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1345. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1346. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1347. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1348. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1349. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(__printf__, string_idx, first_to_check)))
  1350. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(6,0,0)
  1351. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __declspec(vaformat(printf,string_idx,first_to_check))
  1352. #else
  1353. #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check)
  1354. #endif
  1355. #if defined(JSON_HEDLEY_CONSTEXPR)
  1356. #undef JSON_HEDLEY_CONSTEXPR
  1357. #endif
  1358. #if defined(__cplusplus)
  1359. #if __cplusplus >= 201103L
  1360. #define JSON_HEDLEY_CONSTEXPR JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(constexpr)
  1361. #endif
  1362. #endif
  1363. #if !defined(JSON_HEDLEY_CONSTEXPR)
  1364. #define JSON_HEDLEY_CONSTEXPR
  1365. #endif
  1366. #if defined(JSON_HEDLEY_PREDICT)
  1367. #undef JSON_HEDLEY_PREDICT
  1368. #endif
  1369. #if defined(JSON_HEDLEY_LIKELY)
  1370. #undef JSON_HEDLEY_LIKELY
  1371. #endif
  1372. #if defined(JSON_HEDLEY_UNLIKELY)
  1373. #undef JSON_HEDLEY_UNLIKELY
  1374. #endif
  1375. #if defined(JSON_HEDLEY_UNPREDICTABLE)
  1376. #undef JSON_HEDLEY_UNPREDICTABLE
  1377. #endif
  1378. #if JSON_HEDLEY_HAS_BUILTIN(__builtin_unpredictable)
  1379. #define JSON_HEDLEY_UNPREDICTABLE(expr) __builtin_unpredictable((expr))
  1380. #endif
  1381. #if \
  1382. (JSON_HEDLEY_HAS_BUILTIN(__builtin_expect_with_probability) && !defined(JSON_HEDLEY_PGI_VERSION)) || \
  1383. JSON_HEDLEY_GCC_VERSION_CHECK(9,0,0) || \
  1384. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1385. # define JSON_HEDLEY_PREDICT(expr, value, probability) __builtin_expect_with_probability( (expr), (value), (probability))
  1386. # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) __builtin_expect_with_probability(!!(expr), 1 , (probability))
  1387. # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) __builtin_expect_with_probability(!!(expr), 0 , (probability))
  1388. # define JSON_HEDLEY_LIKELY(expr) __builtin_expect (!!(expr), 1 )
  1389. # define JSON_HEDLEY_UNLIKELY(expr) __builtin_expect (!!(expr), 0 )
  1390. #elif \
  1391. (JSON_HEDLEY_HAS_BUILTIN(__builtin_expect) && !defined(JSON_HEDLEY_INTEL_CL_VERSION)) || \
  1392. JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \
  1393. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1394. (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)) || \
  1395. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1396. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1397. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1398. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,7,0) || \
  1399. JSON_HEDLEY_TI_CL430_VERSION_CHECK(3,1,0) || \
  1400. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,1,0) || \
  1401. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
  1402. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1403. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1404. JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,27) || \
  1405. JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \
  1406. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1407. # define JSON_HEDLEY_PREDICT(expr, expected, probability) \
  1408. (((probability) >= 0.9) ? __builtin_expect((expr), (expected)) : (JSON_HEDLEY_STATIC_CAST(void, expected), (expr)))
  1409. # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) \
  1410. (__extension__ ({ \
  1411. double hedley_probability_ = (probability); \
  1412. ((hedley_probability_ >= 0.9) ? __builtin_expect(!!(expr), 1) : ((hedley_probability_ <= 0.1) ? __builtin_expect(!!(expr), 0) : !!(expr))); \
  1413. }))
  1414. # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) \
  1415. (__extension__ ({ \
  1416. double hedley_probability_ = (probability); \
  1417. ((hedley_probability_ >= 0.9) ? __builtin_expect(!!(expr), 0) : ((hedley_probability_ <= 0.1) ? __builtin_expect(!!(expr), 1) : !!(expr))); \
  1418. }))
  1419. # define JSON_HEDLEY_LIKELY(expr) __builtin_expect(!!(expr), 1)
  1420. # define JSON_HEDLEY_UNLIKELY(expr) __builtin_expect(!!(expr), 0)
  1421. #else
  1422. # define JSON_HEDLEY_PREDICT(expr, expected, probability) (JSON_HEDLEY_STATIC_CAST(void, expected), (expr))
  1423. # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) (!!(expr))
  1424. # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) (!!(expr))
  1425. # define JSON_HEDLEY_LIKELY(expr) (!!(expr))
  1426. # define JSON_HEDLEY_UNLIKELY(expr) (!!(expr))
  1427. #endif
  1428. #if !defined(JSON_HEDLEY_UNPREDICTABLE)
  1429. #define JSON_HEDLEY_UNPREDICTABLE(expr) JSON_HEDLEY_PREDICT(expr, 1, 0.5)
  1430. #endif
  1431. #if defined(JSON_HEDLEY_MALLOC)
  1432. #undef JSON_HEDLEY_MALLOC
  1433. #endif
  1434. #if \
  1435. JSON_HEDLEY_HAS_ATTRIBUTE(malloc) || \
  1436. JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
  1437. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1438. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1439. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1440. JSON_HEDLEY_IBM_VERSION_CHECK(12,1,0) || \
  1441. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1442. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1443. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1444. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1445. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1446. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1447. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1448. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1449. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1450. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1451. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1452. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1453. #define JSON_HEDLEY_MALLOC __attribute__((__malloc__))
  1454. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
  1455. #define JSON_HEDLEY_MALLOC _Pragma("returns_new_memory")
  1456. #elif \
  1457. JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
  1458. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1459. #define JSON_HEDLEY_MALLOC __declspec(restrict)
  1460. #else
  1461. #define JSON_HEDLEY_MALLOC
  1462. #endif
  1463. #if defined(JSON_HEDLEY_PURE)
  1464. #undef JSON_HEDLEY_PURE
  1465. #endif
  1466. #if \
  1467. JSON_HEDLEY_HAS_ATTRIBUTE(pure) || \
  1468. JSON_HEDLEY_GCC_VERSION_CHECK(2,96,0) || \
  1469. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1470. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1471. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1472. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1473. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1474. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1475. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1476. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1477. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1478. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1479. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1480. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1481. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1482. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1483. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1484. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1485. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1486. # define JSON_HEDLEY_PURE __attribute__((__pure__))
  1487. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
  1488. # define JSON_HEDLEY_PURE _Pragma("does_not_write_global_data")
  1489. #elif defined(__cplusplus) && \
  1490. ( \
  1491. JSON_HEDLEY_TI_CL430_VERSION_CHECK(2,0,1) || \
  1492. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0) || \
  1493. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) \
  1494. )
  1495. # define JSON_HEDLEY_PURE _Pragma("FUNC_IS_PURE;")
  1496. #else
  1497. # define JSON_HEDLEY_PURE
  1498. #endif
  1499. #if defined(JSON_HEDLEY_CONST)
  1500. #undef JSON_HEDLEY_CONST
  1501. #endif
  1502. #if \
  1503. JSON_HEDLEY_HAS_ATTRIBUTE(const) || \
  1504. JSON_HEDLEY_GCC_VERSION_CHECK(2,5,0) || \
  1505. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1506. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1507. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1508. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1509. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1510. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1511. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1512. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1513. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1514. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1515. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1516. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1517. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1518. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1519. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1520. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1521. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1522. #define JSON_HEDLEY_CONST __attribute__((__const__))
  1523. #elif \
  1524. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
  1525. #define JSON_HEDLEY_CONST _Pragma("no_side_effect")
  1526. #else
  1527. #define JSON_HEDLEY_CONST JSON_HEDLEY_PURE
  1528. #endif
  1529. #if defined(JSON_HEDLEY_RESTRICT)
  1530. #undef JSON_HEDLEY_RESTRICT
  1531. #endif
  1532. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && !defined(__cplusplus)
  1533. #define JSON_HEDLEY_RESTRICT restrict
  1534. #elif \
  1535. JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
  1536. JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
  1537. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1538. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \
  1539. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1540. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1541. JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
  1542. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1543. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,4) || \
  1544. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,1,0) || \
  1545. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1546. (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,14,0) && defined(__cplusplus)) || \
  1547. JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \
  1548. defined(__clang__) || \
  1549. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1550. #define JSON_HEDLEY_RESTRICT __restrict
  1551. #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,3,0) && !defined(__cplusplus)
  1552. #define JSON_HEDLEY_RESTRICT _Restrict
  1553. #else
  1554. #define JSON_HEDLEY_RESTRICT
  1555. #endif
  1556. #if defined(JSON_HEDLEY_INLINE)
  1557. #undef JSON_HEDLEY_INLINE
  1558. #endif
  1559. #if \
  1560. (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
  1561. (defined(__cplusplus) && (__cplusplus >= 199711L))
  1562. #define JSON_HEDLEY_INLINE inline
  1563. #elif \
  1564. defined(JSON_HEDLEY_GCC_VERSION) || \
  1565. JSON_HEDLEY_ARM_VERSION_CHECK(6,2,0)
  1566. #define JSON_HEDLEY_INLINE __inline__
  1567. #elif \
  1568. JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \
  1569. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \
  1570. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1571. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,1,0) || \
  1572. JSON_HEDLEY_TI_CL430_VERSION_CHECK(3,1,0) || \
  1573. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
  1574. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) || \
  1575. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1576. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1577. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1578. #define JSON_HEDLEY_INLINE __inline
  1579. #else
  1580. #define JSON_HEDLEY_INLINE
  1581. #endif
  1582. #if defined(JSON_HEDLEY_ALWAYS_INLINE)
  1583. #undef JSON_HEDLEY_ALWAYS_INLINE
  1584. #endif
  1585. #if \
  1586. JSON_HEDLEY_HAS_ATTRIBUTE(always_inline) || \
  1587. JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
  1588. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1589. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1590. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1591. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1592. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1593. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1594. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1595. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1596. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1597. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1598. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1599. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1600. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1601. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1602. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1603. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \
  1604. JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
  1605. # define JSON_HEDLEY_ALWAYS_INLINE __attribute__((__always_inline__)) JSON_HEDLEY_INLINE
  1606. #elif \
  1607. JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \
  1608. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1609. # define JSON_HEDLEY_ALWAYS_INLINE __forceinline
  1610. #elif defined(__cplusplus) && \
  1611. ( \
  1612. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1613. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1614. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1615. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
  1616. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1617. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) \
  1618. )
  1619. # define JSON_HEDLEY_ALWAYS_INLINE _Pragma("FUNC_ALWAYS_INLINE;")
  1620. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1621. # define JSON_HEDLEY_ALWAYS_INLINE _Pragma("inline=forced")
  1622. #else
  1623. # define JSON_HEDLEY_ALWAYS_INLINE JSON_HEDLEY_INLINE
  1624. #endif
  1625. #if defined(JSON_HEDLEY_NEVER_INLINE)
  1626. #undef JSON_HEDLEY_NEVER_INLINE
  1627. #endif
  1628. #if \
  1629. JSON_HEDLEY_HAS_ATTRIBUTE(noinline) || \
  1630. JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
  1631. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1632. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1633. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1634. JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  1635. JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
  1636. (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1637. JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
  1638. (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1639. JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
  1640. (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1641. JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
  1642. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1643. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
  1644. JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  1645. JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
  1646. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \
  1647. JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
  1648. #define JSON_HEDLEY_NEVER_INLINE __attribute__((__noinline__))
  1649. #elif \
  1650. JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
  1651. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1652. #define JSON_HEDLEY_NEVER_INLINE __declspec(noinline)
  1653. #elif JSON_HEDLEY_PGI_VERSION_CHECK(10,2,0)
  1654. #define JSON_HEDLEY_NEVER_INLINE _Pragma("noinline")
  1655. #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,0,0) && defined(__cplusplus)
  1656. #define JSON_HEDLEY_NEVER_INLINE _Pragma("FUNC_CANNOT_INLINE;")
  1657. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1658. #define JSON_HEDLEY_NEVER_INLINE _Pragma("inline=never")
  1659. #elif JSON_HEDLEY_COMPCERT_VERSION_CHECK(3,2,0)
  1660. #define JSON_HEDLEY_NEVER_INLINE __attribute((noinline))
  1661. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(9,0,0)
  1662. #define JSON_HEDLEY_NEVER_INLINE __declspec(noinline)
  1663. #else
  1664. #define JSON_HEDLEY_NEVER_INLINE
  1665. #endif
  1666. #if defined(JSON_HEDLEY_PRIVATE)
  1667. #undef JSON_HEDLEY_PRIVATE
  1668. #endif
  1669. #if defined(JSON_HEDLEY_PUBLIC)
  1670. #undef JSON_HEDLEY_PUBLIC
  1671. #endif
  1672. #if defined(JSON_HEDLEY_IMPORT)
  1673. #undef JSON_HEDLEY_IMPORT
  1674. #endif
  1675. #if defined(_WIN32) || defined(__CYGWIN__)
  1676. # define JSON_HEDLEY_PRIVATE
  1677. # define JSON_HEDLEY_PUBLIC __declspec(dllexport)
  1678. # define JSON_HEDLEY_IMPORT __declspec(dllimport)
  1679. #else
  1680. # if \
  1681. JSON_HEDLEY_HAS_ATTRIBUTE(visibility) || \
  1682. JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
  1683. JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  1684. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1685. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1686. JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
  1687. ( \
  1688. defined(__TI_EABI__) && \
  1689. ( \
  1690. (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
  1691. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) \
  1692. ) \
  1693. ) || \
  1694. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1695. # define JSON_HEDLEY_PRIVATE __attribute__((__visibility__("hidden")))
  1696. # define JSON_HEDLEY_PUBLIC __attribute__((__visibility__("default")))
  1697. # else
  1698. # define JSON_HEDLEY_PRIVATE
  1699. # define JSON_HEDLEY_PUBLIC
  1700. # endif
  1701. # define JSON_HEDLEY_IMPORT extern
  1702. #endif
  1703. #if defined(JSON_HEDLEY_NO_THROW)
  1704. #undef JSON_HEDLEY_NO_THROW
  1705. #endif
  1706. #if \
  1707. JSON_HEDLEY_HAS_ATTRIBUTE(nothrow) || \
  1708. JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
  1709. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1710. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1711. #define JSON_HEDLEY_NO_THROW __attribute__((__nothrow__))
  1712. #elif \
  1713. JSON_HEDLEY_MSVC_VERSION_CHECK(13,1,0) || \
  1714. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \
  1715. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0)
  1716. #define JSON_HEDLEY_NO_THROW __declspec(nothrow)
  1717. #else
  1718. #define JSON_HEDLEY_NO_THROW
  1719. #endif
  1720. #if defined(JSON_HEDLEY_FALL_THROUGH)
  1721. #undef JSON_HEDLEY_FALL_THROUGH
  1722. #endif
  1723. #if \
  1724. JSON_HEDLEY_HAS_ATTRIBUTE(fallthrough) || \
  1725. JSON_HEDLEY_GCC_VERSION_CHECK(7,0,0) || \
  1726. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1727. #define JSON_HEDLEY_FALL_THROUGH __attribute__((__fallthrough__))
  1728. #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(clang,fallthrough)
  1729. #define JSON_HEDLEY_FALL_THROUGH JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[clang::fallthrough]])
  1730. #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE(fallthrough)
  1731. #define JSON_HEDLEY_FALL_THROUGH JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[fallthrough]])
  1732. #elif defined(__fallthrough) /* SAL */
  1733. #define JSON_HEDLEY_FALL_THROUGH __fallthrough
  1734. #else
  1735. #define JSON_HEDLEY_FALL_THROUGH
  1736. #endif
  1737. #if defined(JSON_HEDLEY_RETURNS_NON_NULL)
  1738. #undef JSON_HEDLEY_RETURNS_NON_NULL
  1739. #endif
  1740. #if \
  1741. JSON_HEDLEY_HAS_ATTRIBUTE(returns_nonnull) || \
  1742. JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) || \
  1743. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1744. #define JSON_HEDLEY_RETURNS_NON_NULL __attribute__((__returns_nonnull__))
  1745. #elif defined(_Ret_notnull_) /* SAL */
  1746. #define JSON_HEDLEY_RETURNS_NON_NULL _Ret_notnull_
  1747. #else
  1748. #define JSON_HEDLEY_RETURNS_NON_NULL
  1749. #endif
  1750. #if defined(JSON_HEDLEY_ARRAY_PARAM)
  1751. #undef JSON_HEDLEY_ARRAY_PARAM
  1752. #endif
  1753. #if \
  1754. defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
  1755. !defined(__STDC_NO_VLA__) && \
  1756. !defined(__cplusplus) && \
  1757. !defined(JSON_HEDLEY_PGI_VERSION) && \
  1758. !defined(JSON_HEDLEY_TINYC_VERSION)
  1759. #define JSON_HEDLEY_ARRAY_PARAM(name) (name)
  1760. #else
  1761. #define JSON_HEDLEY_ARRAY_PARAM(name)
  1762. #endif
  1763. #if defined(JSON_HEDLEY_IS_CONSTANT)
  1764. #undef JSON_HEDLEY_IS_CONSTANT
  1765. #endif
  1766. #if defined(JSON_HEDLEY_REQUIRE_CONSTEXPR)
  1767. #undef JSON_HEDLEY_REQUIRE_CONSTEXPR
  1768. #endif
  1769. /* JSON_HEDLEY_IS_CONSTEXPR_ is for
  1770. HEDLEY INTERNAL USE ONLY. API subject to change without notice. */
  1771. #if defined(JSON_HEDLEY_IS_CONSTEXPR_)
  1772. #undef JSON_HEDLEY_IS_CONSTEXPR_
  1773. #endif
  1774. #if \
  1775. JSON_HEDLEY_HAS_BUILTIN(__builtin_constant_p) || \
  1776. JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
  1777. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1778. JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,19) || \
  1779. JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  1780. JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
  1781. JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
  1782. (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) && !defined(__cplusplus)) || \
  1783. JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \
  1784. JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
  1785. #define JSON_HEDLEY_IS_CONSTANT(expr) __builtin_constant_p(expr)
  1786. #endif
  1787. #if !defined(__cplusplus)
  1788. # if \
  1789. JSON_HEDLEY_HAS_BUILTIN(__builtin_types_compatible_p) || \
  1790. JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
  1791. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1792. JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
  1793. JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \
  1794. JSON_HEDLEY_ARM_VERSION_CHECK(5,4,0) || \
  1795. JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,24)
  1796. #if defined(__INTPTR_TYPE__)
  1797. #define JSON_HEDLEY_IS_CONSTEXPR_(expr) __builtin_types_compatible_p(__typeof__((1 ? (void*) ((__INTPTR_TYPE__) ((expr) * 0)) : (int*) 0)), int*)
  1798. #else
  1799. #include <stdint.h>
  1800. #define JSON_HEDLEY_IS_CONSTEXPR_(expr) __builtin_types_compatible_p(__typeof__((1 ? (void*) ((intptr_t) ((expr) * 0)) : (int*) 0)), int*)
  1801. #endif
  1802. # elif \
  1803. ( \
  1804. defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) && \
  1805. !defined(JSON_HEDLEY_SUNPRO_VERSION) && \
  1806. !defined(JSON_HEDLEY_PGI_VERSION) && \
  1807. !defined(JSON_HEDLEY_IAR_VERSION)) || \
  1808. (JSON_HEDLEY_HAS_EXTENSION(c_generic_selections) && !defined(JSON_HEDLEY_IAR_VERSION)) || \
  1809. JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) || \
  1810. JSON_HEDLEY_INTEL_VERSION_CHECK(17,0,0) || \
  1811. JSON_HEDLEY_IBM_VERSION_CHECK(12,1,0) || \
  1812. JSON_HEDLEY_ARM_VERSION_CHECK(5,3,0)
  1813. #if defined(__INTPTR_TYPE__)
  1814. #define JSON_HEDLEY_IS_CONSTEXPR_(expr) _Generic((1 ? (void*) ((__INTPTR_TYPE__) ((expr) * 0)) : (int*) 0), int*: 1, void*: 0)
  1815. #else
  1816. #include <stdint.h>
  1817. #define JSON_HEDLEY_IS_CONSTEXPR_(expr) _Generic((1 ? (void*) ((intptr_t) * 0) : (int*) 0), int*: 1, void*: 0)
  1818. #endif
  1819. # elif \
  1820. defined(JSON_HEDLEY_GCC_VERSION) || \
  1821. defined(JSON_HEDLEY_INTEL_VERSION) || \
  1822. defined(JSON_HEDLEY_TINYC_VERSION) || \
  1823. defined(JSON_HEDLEY_TI_ARMCL_VERSION) || \
  1824. JSON_HEDLEY_TI_CL430_VERSION_CHECK(18,12,0) || \
  1825. defined(JSON_HEDLEY_TI_CL2000_VERSION) || \
  1826. defined(JSON_HEDLEY_TI_CL6X_VERSION) || \
  1827. defined(JSON_HEDLEY_TI_CL7X_VERSION) || \
  1828. defined(JSON_HEDLEY_TI_CLPRU_VERSION) || \
  1829. defined(__clang__)
  1830. # define JSON_HEDLEY_IS_CONSTEXPR_(expr) ( \
  1831. sizeof(void) != \
  1832. sizeof(*( \
  1833. 1 ? \
  1834. ((void*) ((expr) * 0L) ) : \
  1835. ((struct { char v[sizeof(void) * 2]; } *) 1) \
  1836. ) \
  1837. ) \
  1838. )
  1839. # endif
  1840. #endif
  1841. #if defined(JSON_HEDLEY_IS_CONSTEXPR_)
  1842. #if !defined(JSON_HEDLEY_IS_CONSTANT)
  1843. #define JSON_HEDLEY_IS_CONSTANT(expr) JSON_HEDLEY_IS_CONSTEXPR_(expr)
  1844. #endif
  1845. #define JSON_HEDLEY_REQUIRE_CONSTEXPR(expr) (JSON_HEDLEY_IS_CONSTEXPR_(expr) ? (expr) : (-1))
  1846. #else
  1847. #if !defined(JSON_HEDLEY_IS_CONSTANT)
  1848. #define JSON_HEDLEY_IS_CONSTANT(expr) (0)
  1849. #endif
  1850. #define JSON_HEDLEY_REQUIRE_CONSTEXPR(expr) (expr)
  1851. #endif
  1852. #if defined(JSON_HEDLEY_BEGIN_C_DECLS)
  1853. #undef JSON_HEDLEY_BEGIN_C_DECLS
  1854. #endif
  1855. #if defined(JSON_HEDLEY_END_C_DECLS)
  1856. #undef JSON_HEDLEY_END_C_DECLS
  1857. #endif
  1858. #if defined(JSON_HEDLEY_C_DECL)
  1859. #undef JSON_HEDLEY_C_DECL
  1860. #endif
  1861. #if defined(__cplusplus)
  1862. #define JSON_HEDLEY_BEGIN_C_DECLS extern "C" {
  1863. #define JSON_HEDLEY_END_C_DECLS }
  1864. #define JSON_HEDLEY_C_DECL extern "C"
  1865. #else
  1866. #define JSON_HEDLEY_BEGIN_C_DECLS
  1867. #define JSON_HEDLEY_END_C_DECLS
  1868. #define JSON_HEDLEY_C_DECL
  1869. #endif
  1870. #if defined(JSON_HEDLEY_STATIC_ASSERT)
  1871. #undef JSON_HEDLEY_STATIC_ASSERT
  1872. #endif
  1873. #if \
  1874. !defined(__cplusplus) && ( \
  1875. (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)) || \
  1876. (JSON_HEDLEY_HAS_FEATURE(c_static_assert) && !defined(JSON_HEDLEY_INTEL_CL_VERSION)) || \
  1877. JSON_HEDLEY_GCC_VERSION_CHECK(6,0,0) || \
  1878. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  1879. defined(_Static_assert) \
  1880. )
  1881. # define JSON_HEDLEY_STATIC_ASSERT(expr, message) _Static_assert(expr, message)
  1882. #elif \
  1883. (defined(__cplusplus) && (__cplusplus >= 201103L)) || \
  1884. JSON_HEDLEY_MSVC_VERSION_CHECK(16,0,0) || \
  1885. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1886. # define JSON_HEDLEY_STATIC_ASSERT(expr, message) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(static_assert(expr, message))
  1887. #else
  1888. # define JSON_HEDLEY_STATIC_ASSERT(expr, message)
  1889. #endif
  1890. #if defined(JSON_HEDLEY_NULL)
  1891. #undef JSON_HEDLEY_NULL
  1892. #endif
  1893. #if defined(__cplusplus)
  1894. #if __cplusplus >= 201103L
  1895. #define JSON_HEDLEY_NULL JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(nullptr)
  1896. #elif defined(NULL)
  1897. #define JSON_HEDLEY_NULL NULL
  1898. #else
  1899. #define JSON_HEDLEY_NULL JSON_HEDLEY_STATIC_CAST(void*, 0)
  1900. #endif
  1901. #elif defined(NULL)
  1902. #define JSON_HEDLEY_NULL NULL
  1903. #else
  1904. #define JSON_HEDLEY_NULL ((void*) 0)
  1905. #endif
  1906. #if defined(JSON_HEDLEY_MESSAGE)
  1907. #undef JSON_HEDLEY_MESSAGE
  1908. #endif
  1909. #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
  1910. # define JSON_HEDLEY_MESSAGE(msg) \
  1911. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  1912. JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS \
  1913. JSON_HEDLEY_PRAGMA(message msg) \
  1914. JSON_HEDLEY_DIAGNOSTIC_POP
  1915. #elif \
  1916. JSON_HEDLEY_GCC_VERSION_CHECK(4,4,0) || \
  1917. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  1918. # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message msg)
  1919. #elif JSON_HEDLEY_CRAY_VERSION_CHECK(5,0,0)
  1920. # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(_CRI message msg)
  1921. #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
  1922. # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message(msg))
  1923. #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,0,0)
  1924. # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message(msg))
  1925. #else
  1926. # define JSON_HEDLEY_MESSAGE(msg)
  1927. #endif
  1928. #if defined(JSON_HEDLEY_WARNING)
  1929. #undef JSON_HEDLEY_WARNING
  1930. #endif
  1931. #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
  1932. # define JSON_HEDLEY_WARNING(msg) \
  1933. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  1934. JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS \
  1935. JSON_HEDLEY_PRAGMA(clang warning msg) \
  1936. JSON_HEDLEY_DIAGNOSTIC_POP
  1937. #elif \
  1938. JSON_HEDLEY_GCC_VERSION_CHECK(4,8,0) || \
  1939. JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \
  1940. JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  1941. # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(GCC warning msg)
  1942. #elif \
  1943. JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) || \
  1944. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  1945. # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(message(msg))
  1946. #else
  1947. # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_MESSAGE(msg)
  1948. #endif
  1949. #if defined(JSON_HEDLEY_REQUIRE)
  1950. #undef JSON_HEDLEY_REQUIRE
  1951. #endif
  1952. #if defined(JSON_HEDLEY_REQUIRE_MSG)
  1953. #undef JSON_HEDLEY_REQUIRE_MSG
  1954. #endif
  1955. #if JSON_HEDLEY_HAS_ATTRIBUTE(diagnose_if)
  1956. # if JSON_HEDLEY_HAS_WARNING("-Wgcc-compat")
  1957. # define JSON_HEDLEY_REQUIRE(expr) \
  1958. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  1959. _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \
  1960. __attribute__((diagnose_if(!(expr), #expr, "error"))) \
  1961. JSON_HEDLEY_DIAGNOSTIC_POP
  1962. # define JSON_HEDLEY_REQUIRE_MSG(expr,msg) \
  1963. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  1964. _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \
  1965. __attribute__((diagnose_if(!(expr), msg, "error"))) \
  1966. JSON_HEDLEY_DIAGNOSTIC_POP
  1967. # else
  1968. # define JSON_HEDLEY_REQUIRE(expr) __attribute__((diagnose_if(!(expr), #expr, "error")))
  1969. # define JSON_HEDLEY_REQUIRE_MSG(expr,msg) __attribute__((diagnose_if(!(expr), msg, "error")))
  1970. # endif
  1971. #else
  1972. # define JSON_HEDLEY_REQUIRE(expr)
  1973. # define JSON_HEDLEY_REQUIRE_MSG(expr,msg)
  1974. #endif
  1975. #if defined(JSON_HEDLEY_FLAGS)
  1976. #undef JSON_HEDLEY_FLAGS
  1977. #endif
  1978. #if JSON_HEDLEY_HAS_ATTRIBUTE(flag_enum) && (!defined(__cplusplus) || JSON_HEDLEY_HAS_WARNING("-Wbitfield-enum-conversion"))
  1979. #define JSON_HEDLEY_FLAGS __attribute__((__flag_enum__))
  1980. #else
  1981. #define JSON_HEDLEY_FLAGS
  1982. #endif
  1983. #if defined(JSON_HEDLEY_FLAGS_CAST)
  1984. #undef JSON_HEDLEY_FLAGS_CAST
  1985. #endif
  1986. #if JSON_HEDLEY_INTEL_VERSION_CHECK(19,0,0)
  1987. # define JSON_HEDLEY_FLAGS_CAST(T, expr) (__extension__ ({ \
  1988. JSON_HEDLEY_DIAGNOSTIC_PUSH \
  1989. _Pragma("warning(disable:188)") \
  1990. ((T) (expr)); \
  1991. JSON_HEDLEY_DIAGNOSTIC_POP \
  1992. }))
  1993. #else
  1994. # define JSON_HEDLEY_FLAGS_CAST(T, expr) JSON_HEDLEY_STATIC_CAST(T, expr)
  1995. #endif
  1996. #if defined(JSON_HEDLEY_EMPTY_BASES)
  1997. #undef JSON_HEDLEY_EMPTY_BASES
  1998. #endif
  1999. #if \
  2000. (JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,23918) && !JSON_HEDLEY_MSVC_VERSION_CHECK(20,0,0)) || \
  2001. JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
  2002. #define JSON_HEDLEY_EMPTY_BASES __declspec(empty_bases)
  2003. #else
  2004. #define JSON_HEDLEY_EMPTY_BASES
  2005. #endif
  2006. /* Remaining macros are deprecated. */
  2007. #if defined(JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK)
  2008. #undef JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK
  2009. #endif
  2010. #if defined(__clang__)
  2011. #define JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) (0)
  2012. #else
  2013. #define JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
  2014. #endif
  2015. #if defined(JSON_HEDLEY_CLANG_HAS_ATTRIBUTE)
  2016. #undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE
  2017. #endif
  2018. #define JSON_HEDLEY_CLANG_HAS_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
  2019. #if defined(JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE)
  2020. #undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE
  2021. #endif
  2022. #define JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute)
  2023. #if defined(JSON_HEDLEY_CLANG_HAS_BUILTIN)
  2024. #undef JSON_HEDLEY_CLANG_HAS_BUILTIN
  2025. #endif
  2026. #define JSON_HEDLEY_CLANG_HAS_BUILTIN(builtin) JSON_HEDLEY_HAS_BUILTIN(builtin)
  2027. #if defined(JSON_HEDLEY_CLANG_HAS_FEATURE)
  2028. #undef JSON_HEDLEY_CLANG_HAS_FEATURE
  2029. #endif
  2030. #define JSON_HEDLEY_CLANG_HAS_FEATURE(feature) JSON_HEDLEY_HAS_FEATURE(feature)
  2031. #if defined(JSON_HEDLEY_CLANG_HAS_EXTENSION)
  2032. #undef JSON_HEDLEY_CLANG_HAS_EXTENSION
  2033. #endif
  2034. #define JSON_HEDLEY_CLANG_HAS_EXTENSION(extension) JSON_HEDLEY_HAS_EXTENSION(extension)
  2035. #if defined(JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE)
  2036. #undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE
  2037. #endif
  2038. #define JSON_HEDLEY_CLANG_HAS_DECLSPEC_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute)
  2039. #if defined(JSON_HEDLEY_CLANG_HAS_WARNING)
  2040. #undef JSON_HEDLEY_CLANG_HAS_WARNING
  2041. #endif
  2042. #define JSON_HEDLEY_CLANG_HAS_WARNING(warning) JSON_HEDLEY_HAS_WARNING(warning)
  2043. #endif /* !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < X) */
  2044. // This file contains all internal macro definitions
  2045. // You MUST include macro_unscope.hpp at the end of json.hpp to undef all of them
  2046. // exclude unsupported compilers
  2047. #if !defined(JSON_SKIP_UNSUPPORTED_COMPILER_CHECK)
  2048. #if defined(__clang__)
  2049. #if (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) < 30400
  2050. #error "unsupported Clang version - see https://github.com/nlohmann/json#supported-compilers"
  2051. #endif
  2052. #elif defined(__GNUC__) && !(defined(__ICC) || defined(__INTEL_COMPILER))
  2053. #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40800
  2054. #error "unsupported GCC version - see https://github.com/nlohmann/json#supported-compilers"
  2055. #endif
  2056. #endif
  2057. #endif
  2058. // C++ language standard detection
  2059. // if the user manually specified the used c++ version this is skipped
  2060. #if !defined(JSON_HAS_CPP_20) && !defined(JSON_HAS_CPP_17) && !defined(JSON_HAS_CPP_14) && !defined(JSON_HAS_CPP_11)
  2061. #if (defined(__cplusplus) && __cplusplus >= 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L)
  2062. #define JSON_HAS_CPP_20
  2063. #define JSON_HAS_CPP_17
  2064. #define JSON_HAS_CPP_14
  2065. #elif (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464
  2066. #define JSON_HAS_CPP_17
  2067. #define JSON_HAS_CPP_14
  2068. #elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1)
  2069. #define JSON_HAS_CPP_14
  2070. #endif
  2071. // the cpp 11 flag is always specified because it is the minimal required version
  2072. #define JSON_HAS_CPP_11
  2073. #endif
  2074. // disable documentation warnings on clang
  2075. #if defined(__clang__)
  2076. #pragma clang diagnostic push
  2077. #pragma clang diagnostic ignored "-Wdocumentation"
  2078. #pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
  2079. #endif
  2080. // allow to disable exceptions
  2081. #if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION)
  2082. #define JSON_THROW(exception) throw exception
  2083. #define JSON_TRY try
  2084. #define JSON_CATCH(exception) catch(exception)
  2085. #define JSON_INTERNAL_CATCH(exception) catch(exception)
  2086. #else
  2087. #include <cstdlib>
  2088. #define JSON_THROW(exception) std::abort()
  2089. #define JSON_TRY if(true)
  2090. #define JSON_CATCH(exception) if(false)
  2091. #define JSON_INTERNAL_CATCH(exception) if(false)
  2092. #endif
  2093. // override exception macros
  2094. #if defined(JSON_THROW_USER)
  2095. #undef JSON_THROW
  2096. #define JSON_THROW JSON_THROW_USER
  2097. #endif
  2098. #if defined(JSON_TRY_USER)
  2099. #undef JSON_TRY
  2100. #define JSON_TRY JSON_TRY_USER
  2101. #endif
  2102. #if defined(JSON_CATCH_USER)
  2103. #undef JSON_CATCH
  2104. #define JSON_CATCH JSON_CATCH_USER
  2105. #undef JSON_INTERNAL_CATCH
  2106. #define JSON_INTERNAL_CATCH JSON_CATCH_USER
  2107. #endif
  2108. #if defined(JSON_INTERNAL_CATCH_USER)
  2109. #undef JSON_INTERNAL_CATCH
  2110. #define JSON_INTERNAL_CATCH JSON_INTERNAL_CATCH_USER
  2111. #endif
  2112. // allow to override assert
  2113. #if !defined(JSON_ASSERT)
  2114. #include <cassert> // assert
  2115. #define JSON_ASSERT(x) assert(x)
  2116. #endif
  2117. // allow to access some private functions (needed by the test suite)
  2118. #if defined(JSON_TESTS_PRIVATE)
  2119. #define JSON_PRIVATE_UNLESS_TESTED public
  2120. #else
  2121. #define JSON_PRIVATE_UNLESS_TESTED private
  2122. #endif
  2123. /*!
  2124. @brief macro to briefly define a mapping between an enum and JSON
  2125. @def NLOHMANN_JSON_SERIALIZE_ENUM
  2126. @since version 3.4.0
  2127. */
  2128. #define NLOHMANN_JSON_SERIALIZE_ENUM(ENUM_TYPE, ...) \
  2129. template<typename BasicJsonType> \
  2130. inline void to_json(BasicJsonType& j, const ENUM_TYPE& e) \
  2131. { \
  2132. static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
  2133. static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
  2134. auto it = std::find_if(std::begin(m), std::end(m), \
  2135. [e](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
  2136. { \
  2137. return ej_pair.first == e; \
  2138. }); \
  2139. j = ((it != std::end(m)) ? it : std::begin(m))->second; \
  2140. } \
  2141. template<typename BasicJsonType> \
  2142. inline void from_json(const BasicJsonType& j, ENUM_TYPE& e) \
  2143. { \
  2144. static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
  2145. static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
  2146. auto it = std::find_if(std::begin(m), std::end(m), \
  2147. [&j](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
  2148. { \
  2149. return ej_pair.second == j; \
  2150. }); \
  2151. e = ((it != std::end(m)) ? it : std::begin(m))->first; \
  2152. }
  2153. // Ugly macros to avoid uglier copy-paste when specializing basic_json. They
  2154. // may be removed in the future once the class is split.
  2155. #define NLOHMANN_BASIC_JSON_TPL_DECLARATION \
  2156. template<template<typename, typename, typename...> class ObjectType, \
  2157. template<typename, typename...> class ArrayType, \
  2158. class StringType, class BooleanType, class NumberIntegerType, \
  2159. class NumberUnsignedType, class NumberFloatType, \
  2160. template<typename> class AllocatorType, \
  2161. template<typename, typename = void> class JSONSerializer, \
  2162. class BinaryType>
  2163. #define NLOHMANN_BASIC_JSON_TPL \
  2164. basic_json<ObjectType, ArrayType, StringType, BooleanType, \
  2165. NumberIntegerType, NumberUnsignedType, NumberFloatType, \
  2166. AllocatorType, JSONSerializer, BinaryType>
  2167. // Macros to simplify conversion from/to types
  2168. #define NLOHMANN_JSON_EXPAND( x ) x
  2169. #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
  2170. #define NLOHMANN_JSON_PASTE(...) NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_GET_MACRO(__VA_ARGS__, \
  2171. NLOHMANN_JSON_PASTE64, \
  2172. NLOHMANN_JSON_PASTE63, \
  2173. NLOHMANN_JSON_PASTE62, \
  2174. NLOHMANN_JSON_PASTE61, \
  2175. NLOHMANN_JSON_PASTE60, \
  2176. NLOHMANN_JSON_PASTE59, \
  2177. NLOHMANN_JSON_PASTE58, \
  2178. NLOHMANN_JSON_PASTE57, \
  2179. NLOHMANN_JSON_PASTE56, \
  2180. NLOHMANN_JSON_PASTE55, \
  2181. NLOHMANN_JSON_PASTE54, \
  2182. NLOHMANN_JSON_PASTE53, \
  2183. NLOHMANN_JSON_PASTE52, \
  2184. NLOHMANN_JSON_PASTE51, \
  2185. NLOHMANN_JSON_PASTE50, \
  2186. NLOHMANN_JSON_PASTE49, \
  2187. NLOHMANN_JSON_PASTE48, \
  2188. NLOHMANN_JSON_PASTE47, \
  2189. NLOHMANN_JSON_PASTE46, \
  2190. NLOHMANN_JSON_PASTE45, \
  2191. NLOHMANN_JSON_PASTE44, \
  2192. NLOHMANN_JSON_PASTE43, \
  2193. NLOHMANN_JSON_PASTE42, \
  2194. NLOHMANN_JSON_PASTE41, \
  2195. NLOHMANN_JSON_PASTE40, \
  2196. NLOHMANN_JSON_PASTE39, \
  2197. NLOHMANN_JSON_PASTE38, \
  2198. NLOHMANN_JSON_PASTE37, \
  2199. NLOHMANN_JSON_PASTE36, \
  2200. NLOHMANN_JSON_PASTE35, \
  2201. NLOHMANN_JSON_PASTE34, \
  2202. NLOHMANN_JSON_PASTE33, \
  2203. NLOHMANN_JSON_PASTE32, \
  2204. NLOHMANN_JSON_PASTE31, \
  2205. NLOHMANN_JSON_PASTE30, \
  2206. NLOHMANN_JSON_PASTE29, \
  2207. NLOHMANN_JSON_PASTE28, \
  2208. NLOHMANN_JSON_PASTE27, \
  2209. NLOHMANN_JSON_PASTE26, \
  2210. NLOHMANN_JSON_PASTE25, \
  2211. NLOHMANN_JSON_PASTE24, \
  2212. NLOHMANN_JSON_PASTE23, \
  2213. NLOHMANN_JSON_PASTE22, \
  2214. NLOHMANN_JSON_PASTE21, \
  2215. NLOHMANN_JSON_PASTE20, \
  2216. NLOHMANN_JSON_PASTE19, \
  2217. NLOHMANN_JSON_PASTE18, \
  2218. NLOHMANN_JSON_PASTE17, \
  2219. NLOHMANN_JSON_PASTE16, \
  2220. NLOHMANN_JSON_PASTE15, \
  2221. NLOHMANN_JSON_PASTE14, \
  2222. NLOHMANN_JSON_PASTE13, \
  2223. NLOHMANN_JSON_PASTE12, \
  2224. NLOHMANN_JSON_PASTE11, \
  2225. NLOHMANN_JSON_PASTE10, \
  2226. NLOHMANN_JSON_PASTE9, \
  2227. NLOHMANN_JSON_PASTE8, \
  2228. NLOHMANN_JSON_PASTE7, \
  2229. NLOHMANN_JSON_PASTE6, \
  2230. NLOHMANN_JSON_PASTE5, \
  2231. NLOHMANN_JSON_PASTE4, \
  2232. NLOHMANN_JSON_PASTE3, \
  2233. NLOHMANN_JSON_PASTE2, \
  2234. NLOHMANN_JSON_PASTE1)(__VA_ARGS__))
  2235. #define NLOHMANN_JSON_PASTE2(func, v1) func(v1)
  2236. #define NLOHMANN_JSON_PASTE3(func, v1, v2) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE2(func, v2)
  2237. #define NLOHMANN_JSON_PASTE4(func, v1, v2, v3) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE3(func, v2, v3)
  2238. #define NLOHMANN_JSON_PASTE5(func, v1, v2, v3, v4) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE4(func, v2, v3, v4)
  2239. #define NLOHMANN_JSON_PASTE6(func, v1, v2, v3, v4, v5) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE5(func, v2, v3, v4, v5)
  2240. #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)
  2241. #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)
  2242. #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)
  2243. #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)
  2244. #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)
  2245. #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)
  2246. #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)
  2247. #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)
  2248. #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)
  2249. #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)
  2250. #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)
  2251. #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)
  2252. #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)
  2253. #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)
  2254. #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)
  2255. #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)
  2256. #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)
  2257. #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)
  2258. #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)
  2259. #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)
  2260. #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)
  2261. #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)
  2262. #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)
  2263. #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)
  2264. #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)
  2265. #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)
  2266. #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)
  2267. #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)
  2268. #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)
  2269. #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)
  2270. #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)
  2271. #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)
  2272. #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)
  2273. #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)
  2274. #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)
  2275. #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)
  2276. #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)
  2277. #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)
  2278. #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)
  2279. #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)
  2280. #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)
  2281. #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)
  2282. #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)
  2283. #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)
  2284. #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)
  2285. #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)
  2286. #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)
  2287. #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)
  2288. #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)
  2289. #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)
  2290. #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)
  2291. #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)
  2292. #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)
  2293. #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)
  2294. #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)
  2295. #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)
  2296. #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)
  2297. #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)
  2298. #define NLOHMANN_JSON_TO(v1) nlohmann_json_j[#v1] = nlohmann_json_t.v1;
  2299. #define NLOHMANN_JSON_FROM(v1) nlohmann_json_j.at(#v1).get_to(nlohmann_json_t.v1);
  2300. /*!
  2301. @brief macro
  2302. @def NLOHMANN_DEFINE_TYPE_INTRUSIVE
  2303. @since version 3.9.0
  2304. */
  2305. #define NLOHMANN_DEFINE_TYPE_INTRUSIVE(Type, ...) \
  2306. 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__)) } \
  2307. 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__)) }
  2308. /*!
  2309. @brief macro
  2310. @def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE
  2311. @since version 3.9.0
  2312. */
  2313. #define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Type, ...) \
  2314. 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__)) } \
  2315. 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__)) }
  2316. #ifndef JSON_USE_IMPLICIT_CONVERSIONS
  2317. #define JSON_USE_IMPLICIT_CONVERSIONS 1
  2318. #endif
  2319. #if JSON_USE_IMPLICIT_CONVERSIONS
  2320. #define JSON_EXPLICIT
  2321. #else
  2322. #define JSON_EXPLICIT explicit
  2323. #endif
  2324. #ifndef JSON_DIAGNOSTICS
  2325. #define JSON_DIAGNOSTICS 0
  2326. #endif
  2327. namespace nlohmann
  2328. {
  2329. namespace detail
  2330. {
  2331. /*!
  2332. @brief replace all occurrences of a substring by another string
  2333. @param[in,out] s the string to manipulate; changed so that all
  2334. occurrences of @a f are replaced with @a t
  2335. @param[in] f the substring to replace with @a t
  2336. @param[in] t the string to replace @a f
  2337. @pre The search string @a f must not be empty. **This precondition is
  2338. enforced with an assertion.**
  2339. @since version 2.0.0
  2340. */
  2341. inline void replace_substring(std::string& s, const std::string& f,
  2342. const std::string& t)
  2343. {
  2344. JSON_ASSERT(!f.empty());
  2345. for (auto pos = s.find(f); // find first occurrence of f
  2346. pos != std::string::npos; // make sure f was found
  2347. s.replace(pos, f.size(), t), // replace with t, and
  2348. pos = s.find(f, pos + t.size())) // find next occurrence of f
  2349. {}
  2350. }
  2351. /*!
  2352. * @brief string escaping as described in RFC 6901 (Sect. 4)
  2353. * @param[in] s string to escape
  2354. * @return escaped string
  2355. *
  2356. * Note the order of escaping "~" to "~0" and "/" to "~1" is important.
  2357. */
  2358. inline std::string escape(std::string s)
  2359. {
  2360. replace_substring(s, "~", "~0");
  2361. replace_substring(s, "/", "~1");
  2362. return s;
  2363. }
  2364. /*!
  2365. * @brief string unescaping as described in RFC 6901 (Sect. 4)
  2366. * @param[in] s string to unescape
  2367. * @return unescaped string
  2368. *
  2369. * Note the order of escaping "~1" to "/" and "~0" to "~" is important.
  2370. */
  2371. static void unescape(std::string& s)
  2372. {
  2373. replace_substring(s, "~1", "/");
  2374. replace_substring(s, "~0", "~");
  2375. }
  2376. } // namespace detail
  2377. } // namespace nlohmann
  2378. // #include <nlohmann/detail/input/position_t.hpp>
  2379. #include <cstddef> // size_t
  2380. namespace nlohmann
  2381. {
  2382. namespace detail
  2383. {
  2384. /// struct to capture the start position of the current token
  2385. struct position_t
  2386. {
  2387. /// the total number of characters read
  2388. std::size_t chars_read_total = 0;
  2389. /// the number of characters read in the current line
  2390. std::size_t chars_read_current_line = 0;
  2391. /// the number of lines read
  2392. std::size_t lines_read = 0;
  2393. /// conversion to size_t to preserve SAX interface
  2394. constexpr operator size_t() const
  2395. {
  2396. return chars_read_total;
  2397. }
  2398. };
  2399. } // namespace detail
  2400. } // namespace nlohmann
  2401. // #include <nlohmann/detail/macro_scope.hpp>
  2402. namespace nlohmann
  2403. {
  2404. namespace detail
  2405. {
  2406. ////////////////
  2407. // exceptions //
  2408. ////////////////
  2409. /*!
  2410. @brief general exception of the @ref basic_json class
  2411. This class is an extension of `std::exception` objects with a member @a id for
  2412. exception ids. It is used as the base class for all exceptions thrown by the
  2413. @ref basic_json class. This class can hence be used as "wildcard" to catch
  2414. exceptions.
  2415. Subclasses:
  2416. - @ref parse_error for exceptions indicating a parse error
  2417. - @ref invalid_iterator for exceptions indicating errors with iterators
  2418. - @ref type_error for exceptions indicating executing a member function with
  2419. a wrong type
  2420. - @ref out_of_range for exceptions indicating access out of the defined range
  2421. - @ref other_error for exceptions indicating other library errors
  2422. @internal
  2423. @note To have nothrow-copy-constructible exceptions, we internally use
  2424. `std::runtime_error` which can cope with arbitrary-length error messages.
  2425. Intermediate strings are built with static functions and then passed to
  2426. the actual constructor.
  2427. @endinternal
  2428. @liveexample{The following code shows how arbitrary library exceptions can be
  2429. caught.,exception}
  2430. @since version 3.0.0
  2431. */
  2432. class exception : public std::exception
  2433. {
  2434. public:
  2435. /// returns the explanatory string
  2436. const char* what() const noexcept override
  2437. {
  2438. return m.what();
  2439. }
  2440. /// the id of the exception
  2441. const int id; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes)
  2442. protected:
  2443. JSON_HEDLEY_NON_NULL(3)
  2444. exception(int id_, const char* what_arg) : id(id_), m(what_arg) {}
  2445. static std::string name(const std::string& ename, int id_)
  2446. {
  2447. return "[json.exception." + ename + "." + std::to_string(id_) + "] ";
  2448. }
  2449. template<typename BasicJsonType>
  2450. static std::string diagnostics(const BasicJsonType& leaf_element)
  2451. {
  2452. #if JSON_DIAGNOSTICS
  2453. std::vector<std::string> tokens;
  2454. for (const auto* current = &leaf_element; current->m_parent != nullptr; current = current->m_parent)
  2455. {
  2456. switch (current->m_parent->type())
  2457. {
  2458. case value_t::array:
  2459. {
  2460. for (std::size_t i = 0; i < current->m_parent->m_value.array->size(); ++i)
  2461. {
  2462. if (&current->m_parent->m_value.array->operator[](i) == current)
  2463. {
  2464. tokens.emplace_back(std::to_string(i));
  2465. break;
  2466. }
  2467. }
  2468. break;
  2469. }
  2470. case value_t::object:
  2471. {
  2472. for (const auto& element : *current->m_parent->m_value.object)
  2473. {
  2474. if (&element.second == current)
  2475. {
  2476. tokens.emplace_back(element.first.c_str());
  2477. break;
  2478. }
  2479. }
  2480. break;
  2481. }
  2482. case value_t::null: // LCOV_EXCL_LINE
  2483. case value_t::string: // LCOV_EXCL_LINE
  2484. case value_t::boolean: // LCOV_EXCL_LINE
  2485. case value_t::number_integer: // LCOV_EXCL_LINE
  2486. case value_t::number_unsigned: // LCOV_EXCL_LINE
  2487. case value_t::number_float: // LCOV_EXCL_LINE
  2488. case value_t::binary: // LCOV_EXCL_LINE
  2489. case value_t::discarded: // LCOV_EXCL_LINE
  2490. default: // LCOV_EXCL_LINE
  2491. break; // LCOV_EXCL_LINE
  2492. }
  2493. }
  2494. if (tokens.empty())
  2495. {
  2496. return "";
  2497. }
  2498. return "(" + std::accumulate(tokens.rbegin(), tokens.rend(), std::string{},
  2499. [](const std::string & a, const std::string & b)
  2500. {
  2501. return a + "/" + detail::escape(b);
  2502. }) + ") ";
  2503. #else
  2504. static_cast<void>(leaf_element);
  2505. return "";
  2506. #endif
  2507. }
  2508. private:
  2509. /// an exception object as storage for error messages
  2510. std::runtime_error m;
  2511. };
  2512. /*!
  2513. @brief exception indicating a parse error
  2514. This exception is thrown by the library when a parse error occurs. Parse errors
  2515. can occur during the deserialization of JSON text, CBOR, MessagePack, as well
  2516. as when using JSON Patch.
  2517. Member @a byte holds the byte index of the last read character in the input
  2518. file.
  2519. Exceptions have ids 1xx.
  2520. name / id | example message | description
  2521. ------------------------------ | --------------- | -------------------------
  2522. json.exception.parse_error.101 | parse error at 2: unexpected end of input; expected string literal | This error indicates a syntax error while deserializing a JSON text. The error message describes that an unexpected token (character) was encountered, and the member @a byte indicates the error position.
  2523. json.exception.parse_error.102 | parse error at 14: missing or wrong low surrogate | JSON uses the `\uxxxx` format to describe Unicode characters. Code points above above 0xFFFF are split into two `\uxxxx` entries ("surrogate pairs"). This error indicates that the surrogate pair is incomplete or contains an invalid code point.
  2524. json.exception.parse_error.103 | parse error: code points above 0x10FFFF are invalid | Unicode supports code points up to 0x10FFFF. Code points above 0x10FFFF are invalid.
  2525. json.exception.parse_error.104 | parse error: JSON patch must be an array of objects | [RFC 6902](https://tools.ietf.org/html/rfc6902) requires a JSON Patch document to be a JSON document that represents an array of objects.
  2526. json.exception.parse_error.105 | parse error: operation must have string member 'op' | An operation of a JSON Patch document must contain exactly one "op" member, whose value indicates the operation to perform. Its value must be one of "add", "remove", "replace", "move", "copy", or "test"; other values are errors.
  2527. json.exception.parse_error.106 | parse error: array index '01' must not begin with '0' | An array index in a JSON Pointer ([RFC 6901](https://tools.ietf.org/html/rfc6901)) may be `0` or any number without a leading `0`.
  2528. json.exception.parse_error.107 | parse error: JSON pointer must be empty or begin with '/' - was: 'foo' | A JSON Pointer must be a Unicode string containing a sequence of zero or more reference tokens, each prefixed by a `/` character.
  2529. json.exception.parse_error.108 | parse error: escape character '~' must be followed with '0' or '1' | In a JSON Pointer, only `~0` and `~1` are valid escape sequences.
  2530. json.exception.parse_error.109 | parse error: array index 'one' is not a number | A JSON Pointer array index must be a number.
  2531. json.exception.parse_error.110 | parse error at 1: cannot read 2 bytes from vector | When parsing CBOR or MessagePack, the byte vector ends before the complete value has been read.
  2532. json.exception.parse_error.112 | parse error at 1: error reading CBOR; last byte: 0xF8 | Not all types of CBOR or MessagePack are supported. This exception occurs if an unsupported byte was read.
  2533. json.exception.parse_error.113 | parse error at 2: expected a CBOR string; last byte: 0x98 | While parsing a map key, a value that is not a string has been read.
  2534. json.exception.parse_error.114 | parse error: Unsupported BSON record type 0x0F | The parsing of the corresponding BSON record type is not implemented (yet).
  2535. json.exception.parse_error.115 | parse error at byte 5: syntax error while parsing UBJSON high-precision number: invalid number text: 1A | A UBJSON high-precision number could not be parsed.
  2536. @note For an input with n bytes, 1 is the index of the first character and n+1
  2537. is the index of the terminating null byte or the end of file. This also
  2538. holds true when reading a byte vector (CBOR or MessagePack).
  2539. @liveexample{The following code shows how a `parse_error` exception can be
  2540. caught.,parse_error}
  2541. @sa - @ref exception for the base class of the library exceptions
  2542. @sa - @ref invalid_iterator for exceptions indicating errors with iterators
  2543. @sa - @ref type_error for exceptions indicating executing a member function with
  2544. a wrong type
  2545. @sa - @ref out_of_range for exceptions indicating access out of the defined range
  2546. @sa - @ref other_error for exceptions indicating other library errors
  2547. @since version 3.0.0
  2548. */
  2549. class parse_error : public exception
  2550. {
  2551. public:
  2552. /*!
  2553. @brief create a parse error exception
  2554. @param[in] id_ the id of the exception
  2555. @param[in] pos the position where the error occurred (or with
  2556. chars_read_total=0 if the position cannot be
  2557. determined)
  2558. @param[in] what_arg the explanatory string
  2559. @return parse_error object
  2560. */
  2561. template<typename BasicJsonType>
  2562. static parse_error create(int id_, const position_t& pos, const std::string& what_arg, const BasicJsonType& context)
  2563. {
  2564. std::string w = exception::name("parse_error", id_) + "parse error" +
  2565. position_string(pos) + ": " + exception::diagnostics(context) + what_arg;
  2566. return parse_error(id_, pos.chars_read_total, w.c_str());
  2567. }
  2568. template<typename BasicJsonType>
  2569. static parse_error create(int id_, std::size_t byte_, const std::string& what_arg, const BasicJsonType& context)
  2570. {
  2571. std::string w = exception::name("parse_error", id_) + "parse error" +
  2572. (byte_ != 0 ? (" at byte " + std::to_string(byte_)) : "") +
  2573. ": " + exception::diagnostics(context) + what_arg;
  2574. return parse_error(id_, byte_, w.c_str());
  2575. }
  2576. /*!
  2577. @brief byte index of the parse error
  2578. The byte index of the last read character in the input file.
  2579. @note For an input with n bytes, 1 is the index of the first character and
  2580. n+1 is the index of the terminating null byte or the end of file.
  2581. This also holds true when reading a byte vector (CBOR or MessagePack).
  2582. */
  2583. const std::size_t byte;
  2584. private:
  2585. parse_error(int id_, std::size_t byte_, const char* what_arg)
  2586. : exception(id_, what_arg), byte(byte_) {}
  2587. static std::string position_string(const position_t& pos)
  2588. {
  2589. return " at line " + std::to_string(pos.lines_read + 1) +
  2590. ", column " + std::to_string(pos.chars_read_current_line);
  2591. }
  2592. };
  2593. /*!
  2594. @brief exception indicating errors with iterators
  2595. This exception is thrown if iterators passed to a library function do not match
  2596. the expected semantics.
  2597. Exceptions have ids 2xx.
  2598. name / id | example message | description
  2599. ----------------------------------- | --------------- | -------------------------
  2600. json.exception.invalid_iterator.201 | iterators are not compatible | The iterators passed to constructor @ref basic_json(InputIT first, InputIT last) are not compatible, meaning they do not belong to the same container. Therefore, the range (@a first, @a last) is invalid.
  2601. json.exception.invalid_iterator.202 | iterator does not fit current value | In an erase or insert function, the passed iterator @a pos does not belong to the JSON value for which the function was called. It hence does not define a valid position for the deletion/insertion.
  2602. json.exception.invalid_iterator.203 | iterators do not fit current value | Either iterator passed to function @ref erase(IteratorType first, IteratorType last) does not belong to the JSON value from which values shall be erased. It hence does not define a valid range to delete values from.
  2603. json.exception.invalid_iterator.204 | iterators out of range | When an iterator range for a primitive type (number, boolean, or string) is passed to a constructor or an erase function, this range has to be exactly (@ref begin(), @ref end()), because this is the only way the single stored value is expressed. All other ranges are invalid.
  2604. json.exception.invalid_iterator.205 | iterator out of range | When an iterator for a primitive type (number, boolean, or string) is passed to an erase function, the iterator has to be the @ref begin() iterator, because it is the only way to address the stored value. All other iterators are invalid.
  2605. json.exception.invalid_iterator.206 | cannot construct with iterators from null | The iterators passed to constructor @ref basic_json(InputIT first, InputIT last) belong to a JSON null value and hence to not define a valid range.
  2606. json.exception.invalid_iterator.207 | cannot use key() for non-object iterators | The key() member function can only be used on iterators belonging to a JSON object, because other types do not have a concept of a key.
  2607. json.exception.invalid_iterator.208 | cannot use operator[] for object iterators | The operator[] to specify a concrete offset cannot be used on iterators belonging to a JSON object, because JSON objects are unordered.
  2608. json.exception.invalid_iterator.209 | cannot use offsets with object iterators | The offset operators (+, -, +=, -=) cannot be used on iterators belonging to a JSON object, because JSON objects are unordered.
  2609. json.exception.invalid_iterator.210 | iterators do not fit | The iterator range passed to the insert function are not compatible, meaning they do not belong to the same container. Therefore, the range (@a first, @a last) is invalid.
  2610. json.exception.invalid_iterator.211 | passed iterators may not belong to container | The iterator range passed to the insert function must not be a subrange of the container to insert to.
  2611. json.exception.invalid_iterator.212 | cannot compare iterators of different containers | When two iterators are compared, they must belong to the same container.
  2612. json.exception.invalid_iterator.213 | cannot compare order of object iterators | The order of object iterators cannot be compared, because JSON objects are unordered.
  2613. json.exception.invalid_iterator.214 | cannot get value | Cannot get value for iterator: Either the iterator belongs to a null value or it is an iterator to a primitive type (number, boolean, or string), but the iterator is different to @ref begin().
  2614. @liveexample{The following code shows how an `invalid_iterator` exception can be
  2615. caught.,invalid_iterator}
  2616. @sa - @ref exception for the base class of the library exceptions
  2617. @sa - @ref parse_error for exceptions indicating a parse error
  2618. @sa - @ref type_error for exceptions indicating executing a member function with
  2619. a wrong type
  2620. @sa - @ref out_of_range for exceptions indicating access out of the defined range
  2621. @sa - @ref other_error for exceptions indicating other library errors
  2622. @since version 3.0.0
  2623. */
  2624. class invalid_iterator : public exception
  2625. {
  2626. public:
  2627. template<typename BasicJsonType>
  2628. static invalid_iterator create(int id_, const std::string& what_arg, const BasicJsonType& context)
  2629. {
  2630. std::string w = exception::name("invalid_iterator", id_) + exception::diagnostics(context) + what_arg;
  2631. return invalid_iterator(id_, w.c_str());
  2632. }
  2633. private:
  2634. JSON_HEDLEY_NON_NULL(3)
  2635. invalid_iterator(int id_, const char* what_arg)
  2636. : exception(id_, what_arg) {}
  2637. };
  2638. /*!
  2639. @brief exception indicating executing a member function with a wrong type
  2640. This exception is thrown in case of a type error; that is, a library function is
  2641. executed on a JSON value whose type does not match the expected semantics.
  2642. Exceptions have ids 3xx.
  2643. name / id | example message | description
  2644. ----------------------------- | --------------- | -------------------------
  2645. json.exception.type_error.301 | cannot create object from initializer list | To create an object from an initializer list, the initializer list must consist only of a list of pairs whose first element is a string. When this constraint is violated, an array is created instead.
  2646. json.exception.type_error.302 | type must be object, but is array | During implicit or explicit value conversion, the JSON type must be compatible to the target type. For instance, a JSON string can only be converted into string types, but not into numbers or boolean types.
  2647. json.exception.type_error.303 | incompatible ReferenceType for get_ref, actual type is object | To retrieve a reference to a value stored in a @ref basic_json object with @ref get_ref, the type of the reference must match the value type. For instance, for a JSON array, the @a ReferenceType must be @ref array_t &.
  2648. json.exception.type_error.304 | cannot use at() with string | The @ref at() member functions can only be executed for certain JSON types.
  2649. json.exception.type_error.305 | cannot use operator[] with string | The @ref operator[] member functions can only be executed for certain JSON types.
  2650. json.exception.type_error.306 | cannot use value() with string | The @ref value() member functions can only be executed for certain JSON types.
  2651. json.exception.type_error.307 | cannot use erase() with string | The @ref erase() member functions can only be executed for certain JSON types.
  2652. json.exception.type_error.308 | cannot use push_back() with string | The @ref push_back() and @ref operator+= member functions can only be executed for certain JSON types.
  2653. json.exception.type_error.309 | cannot use insert() with | The @ref insert() member functions can only be executed for certain JSON types.
  2654. json.exception.type_error.310 | cannot use swap() with number | The @ref swap() member functions can only be executed for certain JSON types.
  2655. json.exception.type_error.311 | cannot use emplace_back() with string | The @ref emplace_back() member function can only be executed for certain JSON types.
  2656. json.exception.type_error.312 | cannot use update() with string | The @ref update() member functions can only be executed for certain JSON types.
  2657. json.exception.type_error.313 | invalid value to unflatten | The @ref unflatten function converts an object whose keys are JSON Pointers back into an arbitrary nested JSON value. The JSON Pointers must not overlap, because then the resulting value would not be well defined.
  2658. json.exception.type_error.314 | only objects can be unflattened | The @ref unflatten function only works for an object whose keys are JSON Pointers.
  2659. json.exception.type_error.315 | values in object must be primitive | The @ref unflatten function only works for an object whose keys are JSON Pointers and whose values are primitive.
  2660. json.exception.type_error.316 | invalid UTF-8 byte at index 10: 0x7E | The @ref dump function only works with UTF-8 encoded strings; that is, if you assign a `std::string` to a JSON value, make sure it is UTF-8 encoded. |
  2661. json.exception.type_error.317 | JSON value cannot be serialized to requested format | The dynamic type of the object cannot be represented in the requested serialization format (e.g. a raw `true` or `null` JSON object cannot be serialized to BSON) |
  2662. @liveexample{The following code shows how a `type_error` exception can be
  2663. caught.,type_error}
  2664. @sa - @ref exception for the base class of the library exceptions
  2665. @sa - @ref parse_error for exceptions indicating a parse error
  2666. @sa - @ref invalid_iterator for exceptions indicating errors with iterators
  2667. @sa - @ref out_of_range for exceptions indicating access out of the defined range
  2668. @sa - @ref other_error for exceptions indicating other library errors
  2669. @since version 3.0.0
  2670. */
  2671. class type_error : public exception
  2672. {
  2673. public:
  2674. template<typename BasicJsonType>
  2675. static type_error create(int id_, const std::string& what_arg, const BasicJsonType& context)
  2676. {
  2677. std::string w = exception::name("type_error", id_) + exception::diagnostics(context) + what_arg;
  2678. return type_error(id_, w.c_str());
  2679. }
  2680. private:
  2681. JSON_HEDLEY_NON_NULL(3)
  2682. type_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
  2683. };
  2684. /*!
  2685. @brief exception indicating access out of the defined range
  2686. This exception is thrown in case a library function is called on an input
  2687. parameter that exceeds the expected range, for instance in case of array
  2688. indices or nonexisting object keys.
  2689. Exceptions have ids 4xx.
  2690. name / id | example message | description
  2691. ------------------------------- | --------------- | -------------------------
  2692. json.exception.out_of_range.401 | array index 3 is out of range | The provided array index @a i is larger than @a size-1.
  2693. json.exception.out_of_range.402 | array index '-' (3) is out of range | The special array index `-` in a JSON Pointer never describes a valid element of the array, but the index past the end. That is, it can only be used to add elements at this position, but not to read it.
  2694. json.exception.out_of_range.403 | key 'foo' not found | The provided key was not found in the JSON object.
  2695. json.exception.out_of_range.404 | unresolved reference token 'foo' | A reference token in a JSON Pointer could not be resolved.
  2696. json.exception.out_of_range.405 | JSON pointer has no parent | The JSON Patch operations 'remove' and 'add' can not be applied to the root element of the JSON value.
  2697. json.exception.out_of_range.406 | number overflow parsing '10E1000' | A parsed number could not be stored as without changing it to NaN or INF.
  2698. json.exception.out_of_range.407 | number overflow serializing '9223372036854775808' | UBJSON and BSON only support integer numbers up to 9223372036854775807. (until version 3.8.0) |
  2699. json.exception.out_of_range.408 | excessive array size: 8658170730974374167 | The size (following `#`) of an UBJSON array or object exceeds the maximal capacity. |
  2700. json.exception.out_of_range.409 | BSON key cannot contain code point U+0000 (at byte 2) | Key identifiers to be serialized to BSON cannot contain code point U+0000, since the key is stored as zero-terminated c-string |
  2701. @liveexample{The following code shows how an `out_of_range` exception can be
  2702. caught.,out_of_range}
  2703. @sa - @ref exception for the base class of the library exceptions
  2704. @sa - @ref parse_error for exceptions indicating a parse error
  2705. @sa - @ref invalid_iterator for exceptions indicating errors with iterators
  2706. @sa - @ref type_error for exceptions indicating executing a member function with
  2707. a wrong type
  2708. @sa - @ref other_error for exceptions indicating other library errors
  2709. @since version 3.0.0
  2710. */
  2711. class out_of_range : public exception
  2712. {
  2713. public:
  2714. template<typename BasicJsonType>
  2715. static out_of_range create(int id_, const std::string& what_arg, const BasicJsonType& context)
  2716. {
  2717. std::string w = exception::name("out_of_range", id_) + exception::diagnostics(context) + what_arg;
  2718. return out_of_range(id_, w.c_str());
  2719. }
  2720. private:
  2721. JSON_HEDLEY_NON_NULL(3)
  2722. out_of_range(int id_, const char* what_arg) : exception(id_, what_arg) {}
  2723. };
  2724. /*!
  2725. @brief exception indicating other library errors
  2726. This exception is thrown in case of errors that cannot be classified with the
  2727. other exception types.
  2728. Exceptions have ids 5xx.
  2729. name / id | example message | description
  2730. ------------------------------ | --------------- | -------------------------
  2731. json.exception.other_error.501 | unsuccessful: {"op":"test","path":"/baz", "value":"bar"} | A JSON Patch operation 'test' failed. The unsuccessful operation is also printed.
  2732. @sa - @ref exception for the base class of the library exceptions
  2733. @sa - @ref parse_error for exceptions indicating a parse error
  2734. @sa - @ref invalid_iterator for exceptions indicating errors with iterators
  2735. @sa - @ref type_error for exceptions indicating executing a member function with
  2736. a wrong type
  2737. @sa - @ref out_of_range for exceptions indicating access out of the defined range
  2738. @liveexample{The following code shows how an `other_error` exception can be
  2739. caught.,other_error}
  2740. @since version 3.0.0
  2741. */
  2742. class other_error : public exception
  2743. {
  2744. public:
  2745. template<typename BasicJsonType>
  2746. static other_error create(int id_, const std::string& what_arg, const BasicJsonType& context)
  2747. {
  2748. std::string w = exception::name("other_error", id_) + exception::diagnostics(context) + what_arg;
  2749. return other_error(id_, w.c_str());
  2750. }
  2751. private:
  2752. JSON_HEDLEY_NON_NULL(3)
  2753. other_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
  2754. };
  2755. } // namespace detail
  2756. } // namespace nlohmann
  2757. // #include <nlohmann/detail/macro_scope.hpp>
  2758. // #include <nlohmann/detail/meta/cpp_future.hpp>
  2759. #include <cstddef> // size_t
  2760. #include <type_traits> // conditional, enable_if, false_type, integral_constant, is_constructible, is_integral, is_same, remove_cv, remove_reference, true_type
  2761. #include <utility> // index_sequence, make_index_sequence, index_sequence_for
  2762. // #include <nlohmann/detail/macro_scope.hpp>
  2763. namespace nlohmann
  2764. {
  2765. namespace detail
  2766. {
  2767. template<typename T>
  2768. using uncvref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
  2769. #ifdef JSON_HAS_CPP_14
  2770. // the following utilities are natively available in C++14
  2771. using std::enable_if_t;
  2772. using std::index_sequence;
  2773. using std::make_index_sequence;
  2774. using std::index_sequence_for;
  2775. #else
  2776. // alias templates to reduce boilerplate
  2777. template<bool B, typename T = void>
  2778. using enable_if_t = typename std::enable_if<B, T>::type;
  2779. // The following code is taken from https://github.com/abseil/abseil-cpp/blob/10cb35e459f5ecca5b2ff107635da0bfa41011b4/absl/utility/utility.h
  2780. // which is part of Google Abseil (https://github.com/abseil/abseil-cpp), licensed under the Apache License 2.0.
  2781. //// START OF CODE FROM GOOGLE ABSEIL
  2782. // integer_sequence
  2783. //
  2784. // Class template representing a compile-time integer sequence. An instantiation
  2785. // of `integer_sequence<T, Ints...>` has a sequence of integers encoded in its
  2786. // type through its template arguments (which is a common need when
  2787. // working with C++11 variadic templates). `absl::integer_sequence` is designed
  2788. // to be a drop-in replacement for C++14's `std::integer_sequence`.
  2789. //
  2790. // Example:
  2791. //
  2792. // template< class T, T... Ints >
  2793. // void user_function(integer_sequence<T, Ints...>);
  2794. //
  2795. // int main()
  2796. // {
  2797. // // user_function's `T` will be deduced to `int` and `Ints...`
  2798. // // will be deduced to `0, 1, 2, 3, 4`.
  2799. // user_function(make_integer_sequence<int, 5>());
  2800. // }
  2801. template <typename T, T... Ints>
  2802. struct integer_sequence
  2803. {
  2804. using value_type = T;
  2805. static constexpr std::size_t size() noexcept
  2806. {
  2807. return sizeof...(Ints);
  2808. }
  2809. };
  2810. // index_sequence
  2811. //
  2812. // A helper template for an `integer_sequence` of `size_t`,
  2813. // `absl::index_sequence` is designed to be a drop-in replacement for C++14's
  2814. // `std::index_sequence`.
  2815. template <size_t... Ints>
  2816. using index_sequence = integer_sequence<size_t, Ints...>;
  2817. namespace utility_internal
  2818. {
  2819. template <typename Seq, size_t SeqSize, size_t Rem>
  2820. struct Extend;
  2821. // Note that SeqSize == sizeof...(Ints). It's passed explicitly for efficiency.
  2822. template <typename T, T... Ints, size_t SeqSize>
  2823. struct Extend<integer_sequence<T, Ints...>, SeqSize, 0>
  2824. {
  2825. using type = integer_sequence < T, Ints..., (Ints + SeqSize)... >;
  2826. };
  2827. template <typename T, T... Ints, size_t SeqSize>
  2828. struct Extend<integer_sequence<T, Ints...>, SeqSize, 1>
  2829. {
  2830. using type = integer_sequence < T, Ints..., (Ints + SeqSize)..., 2 * SeqSize >;
  2831. };
  2832. // Recursion helper for 'make_integer_sequence<T, N>'.
  2833. // 'Gen<T, N>::type' is an alias for 'integer_sequence<T, 0, 1, ... N-1>'.
  2834. template <typename T, size_t N>
  2835. struct Gen
  2836. {
  2837. using type =
  2838. typename Extend < typename Gen < T, N / 2 >::type, N / 2, N % 2 >::type;
  2839. };
  2840. template <typename T>
  2841. struct Gen<T, 0>
  2842. {
  2843. using type = integer_sequence<T>;
  2844. };
  2845. } // namespace utility_internal
  2846. // Compile-time sequences of integers
  2847. // make_integer_sequence
  2848. //
  2849. // This template alias is equivalent to
  2850. // `integer_sequence<int, 0, 1, ..., N-1>`, and is designed to be a drop-in
  2851. // replacement for C++14's `std::make_integer_sequence`.
  2852. template <typename T, T N>
  2853. using make_integer_sequence = typename utility_internal::Gen<T, N>::type;
  2854. // make_index_sequence
  2855. //
  2856. // This template alias is equivalent to `index_sequence<0, 1, ..., N-1>`,
  2857. // and is designed to be a drop-in replacement for C++14's
  2858. // `std::make_index_sequence`.
  2859. template <size_t N>
  2860. using make_index_sequence = make_integer_sequence<size_t, N>;
  2861. // index_sequence_for
  2862. //
  2863. // Converts a typename pack into an index sequence of the same length, and
  2864. // is designed to be a drop-in replacement for C++14's
  2865. // `std::index_sequence_for()`
  2866. template <typename... Ts>
  2867. using index_sequence_for = make_index_sequence<sizeof...(Ts)>;
  2868. //// END OF CODE FROM GOOGLE ABSEIL
  2869. #endif
  2870. // dispatch utility (taken from ranges-v3)
  2871. template<unsigned N> struct priority_tag : priority_tag < N - 1 > {};
  2872. template<> struct priority_tag<0> {};
  2873. // taken from ranges-v3
  2874. template<typename T>
  2875. struct static_const
  2876. {
  2877. static constexpr T value{};
  2878. };
  2879. template<typename T>
  2880. constexpr T static_const<T>::value;
  2881. } // namespace detail
  2882. } // namespace nlohmann
  2883. // #include <nlohmann/detail/meta/identity_tag.hpp>
  2884. namespace nlohmann
  2885. {
  2886. namespace detail
  2887. {
  2888. // dispatching helper struct
  2889. template <class T> struct identity_tag {};
  2890. } // namespace detail
  2891. } // namespace nlohmann
  2892. // #include <nlohmann/detail/meta/type_traits.hpp>
  2893. #include <limits> // numeric_limits
  2894. #include <type_traits> // false_type, is_constructible, is_integral, is_same, true_type
  2895. #include <utility> // declval
  2896. #include <tuple> // tuple
  2897. // #include <nlohmann/detail/iterators/iterator_traits.hpp>
  2898. #include <iterator> // random_access_iterator_tag
  2899. // #include <nlohmann/detail/meta/void_t.hpp>
  2900. namespace nlohmann
  2901. {
  2902. namespace detail
  2903. {
  2904. template<typename ...Ts> struct make_void
  2905. {
  2906. using type = void;
  2907. };
  2908. template<typename ...Ts> using void_t = typename make_void<Ts...>::type;
  2909. } // namespace detail
  2910. } // namespace nlohmann
  2911. // #include <nlohmann/detail/meta/cpp_future.hpp>
  2912. namespace nlohmann
  2913. {
  2914. namespace detail
  2915. {
  2916. template<typename It, typename = void>
  2917. struct iterator_types {};
  2918. template<typename It>
  2919. struct iterator_types <
  2920. It,
  2921. void_t<typename It::difference_type, typename It::value_type, typename It::pointer,
  2922. typename It::reference, typename It::iterator_category >>
  2923. {
  2924. using difference_type = typename It::difference_type;
  2925. using value_type = typename It::value_type;
  2926. using pointer = typename It::pointer;
  2927. using reference = typename It::reference;
  2928. using iterator_category = typename It::iterator_category;
  2929. };
  2930. // This is required as some compilers implement std::iterator_traits in a way that
  2931. // doesn't work with SFINAE. See https://github.com/nlohmann/json/issues/1341.
  2932. template<typename T, typename = void>
  2933. struct iterator_traits
  2934. {
  2935. };
  2936. template<typename T>
  2937. struct iterator_traits < T, enable_if_t < !std::is_pointer<T>::value >>
  2938. : iterator_types<T>
  2939. {
  2940. };
  2941. template<typename T>
  2942. struct iterator_traits<T*, enable_if_t<std::is_object<T>::value>>
  2943. {
  2944. using iterator_category = std::random_access_iterator_tag;
  2945. using value_type = T;
  2946. using difference_type = ptrdiff_t;
  2947. using pointer = T*;
  2948. using reference = T&;
  2949. };
  2950. } // namespace detail
  2951. } // namespace nlohmann
  2952. // #include <nlohmann/detail/macro_scope.hpp>
  2953. // #include <nlohmann/detail/meta/cpp_future.hpp>
  2954. // #include <nlohmann/detail/meta/detected.hpp>
  2955. #include <type_traits>
  2956. // #include <nlohmann/detail/meta/void_t.hpp>
  2957. // https://en.cppreference.com/w/cpp/experimental/is_detected
  2958. namespace nlohmann
  2959. {
  2960. namespace detail
  2961. {
  2962. struct nonesuch
  2963. {
  2964. nonesuch() = delete;
  2965. ~nonesuch() = delete;
  2966. nonesuch(nonesuch const&) = delete;
  2967. nonesuch(nonesuch const&&) = delete;
  2968. void operator=(nonesuch const&) = delete;
  2969. void operator=(nonesuch&&) = delete;
  2970. };
  2971. template<class Default,
  2972. class AlwaysVoid,
  2973. template<class...> class Op,
  2974. class... Args>
  2975. struct detector
  2976. {
  2977. using value_t = std::false_type;
  2978. using type = Default;
  2979. };
  2980. template<class Default, template<class...> class Op, class... Args>
  2981. struct detector<Default, void_t<Op<Args...>>, Op, Args...>
  2982. {
  2983. using value_t = std::true_type;
  2984. using type = Op<Args...>;
  2985. };
  2986. template<template<class...> class Op, class... Args>
  2987. using is_detected = typename detector<nonesuch, void, Op, Args...>::value_t;
  2988. template<template<class...> class Op, class... Args>
  2989. struct is_detected_lazy : is_detected<Op, Args...> { };
  2990. template<template<class...> class Op, class... Args>
  2991. using detected_t = typename detector<nonesuch, void, Op, Args...>::type;
  2992. template<class Default, template<class...> class Op, class... Args>
  2993. using detected_or = detector<Default, void, Op, Args...>;
  2994. template<class Default, template<class...> class Op, class... Args>
  2995. using detected_or_t = typename detected_or<Default, Op, Args...>::type;
  2996. template<class Expected, template<class...> class Op, class... Args>
  2997. using is_detected_exact = std::is_same<Expected, detected_t<Op, Args...>>;
  2998. template<class To, template<class...> class Op, class... Args>
  2999. using is_detected_convertible =
  3000. std::is_convertible<detected_t<Op, Args...>, To>;
  3001. } // namespace detail
  3002. } // namespace nlohmann
  3003. // #include <nlohmann/json_fwd.hpp>
  3004. #ifndef INCLUDE_NLOHMANN_JSON_FWD_HPP_
  3005. #define INCLUDE_NLOHMANN_JSON_FWD_HPP_
  3006. #include <cstdint> // int64_t, uint64_t
  3007. #include <map> // map
  3008. #include <memory> // allocator
  3009. #include <string> // string
  3010. #include <vector> // vector
  3011. /*!
  3012. @brief namespace for Niels Lohmann
  3013. @see https://github.com/nlohmann
  3014. @since version 1.0.0
  3015. */
  3016. namespace nlohmann
  3017. {
  3018. /*!
  3019. @brief default JSONSerializer template argument
  3020. This serializer ignores the template arguments and uses ADL
  3021. ([argument-dependent lookup](https://en.cppreference.com/w/cpp/language/adl))
  3022. for serialization.
  3023. */
  3024. template<typename T = void, typename SFINAE = void>
  3025. struct adl_serializer;
  3026. template<template<typename U, typename V, typename... Args> class ObjectType =
  3027. std::map,
  3028. template<typename U, typename... Args> class ArrayType = std::vector,
  3029. class StringType = std::string, class BooleanType = bool,
  3030. class NumberIntegerType = std::int64_t,
  3031. class NumberUnsignedType = std::uint64_t,
  3032. class NumberFloatType = double,
  3033. template<typename U> class AllocatorType = std::allocator,
  3034. template<typename T, typename SFINAE = void> class JSONSerializer =
  3035. adl_serializer,
  3036. class BinaryType = std::vector<std::uint8_t>>
  3037. class basic_json;
  3038. /*!
  3039. @brief JSON Pointer
  3040. A JSON pointer defines a string syntax for identifying a specific value
  3041. within a JSON document. It can be used with functions `at` and
  3042. `operator[]`. Furthermore, JSON pointers are the base for JSON patches.
  3043. @sa [RFC 6901](https://tools.ietf.org/html/rfc6901)
  3044. @since version 2.0.0
  3045. */
  3046. template<typename BasicJsonType>
  3047. class json_pointer;
  3048. /*!
  3049. @brief default JSON class
  3050. This type is the default specialization of the @ref basic_json class which
  3051. uses the standard template types.
  3052. @since version 1.0.0
  3053. */
  3054. using json = basic_json<>;
  3055. template<class Key, class T, class IgnoredLess, class Allocator>
  3056. struct ordered_map;
  3057. /*!
  3058. @brief ordered JSON class
  3059. This type preserves the insertion order of object keys.
  3060. @since version 3.9.0
  3061. */
  3062. using ordered_json = basic_json<nlohmann::ordered_map>;
  3063. } // namespace nlohmann
  3064. #endif // INCLUDE_NLOHMANN_JSON_FWD_HPP_
  3065. namespace nlohmann
  3066. {
  3067. /*!
  3068. @brief detail namespace with internal helper functions
  3069. This namespace collects functions that should not be exposed,
  3070. implementations of some @ref basic_json methods, and meta-programming helpers.
  3071. @since version 2.1.0
  3072. */
  3073. namespace detail
  3074. {
  3075. /////////////
  3076. // helpers //
  3077. /////////////
  3078. // Note to maintainers:
  3079. //
  3080. // Every trait in this file expects a non CV-qualified type.
  3081. // The only exceptions are in the 'aliases for detected' section
  3082. // (i.e. those of the form: decltype(T::member_function(std::declval<T>())))
  3083. //
  3084. // In this case, T has to be properly CV-qualified to constraint the function arguments
  3085. // (e.g. to_json(BasicJsonType&, const T&))
  3086. template<typename> struct is_basic_json : std::false_type {};
  3087. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  3088. struct is_basic_json<NLOHMANN_BASIC_JSON_TPL> : std::true_type {};
  3089. //////////////////////
  3090. // json_ref helpers //
  3091. //////////////////////
  3092. template<typename>
  3093. class json_ref;
  3094. template<typename>
  3095. struct is_json_ref : std::false_type {};
  3096. template<typename T>
  3097. struct is_json_ref<json_ref<T>> : std::true_type {};
  3098. //////////////////////////
  3099. // aliases for detected //
  3100. //////////////////////////
  3101. template<typename T>
  3102. using mapped_type_t = typename T::mapped_type;
  3103. template<typename T>
  3104. using key_type_t = typename T::key_type;
  3105. template<typename T>
  3106. using value_type_t = typename T::value_type;
  3107. template<typename T>
  3108. using difference_type_t = typename T::difference_type;
  3109. template<typename T>
  3110. using pointer_t = typename T::pointer;
  3111. template<typename T>
  3112. using reference_t = typename T::reference;
  3113. template<typename T>
  3114. using iterator_category_t = typename T::iterator_category;
  3115. template<typename T>
  3116. using iterator_t = typename T::iterator;
  3117. template<typename T, typename... Args>
  3118. using to_json_function = decltype(T::to_json(std::declval<Args>()...));
  3119. template<typename T, typename... Args>
  3120. using from_json_function = decltype(T::from_json(std::declval<Args>()...));
  3121. template<typename T, typename U>
  3122. using get_template_function = decltype(std::declval<T>().template get<U>());
  3123. // trait checking if JSONSerializer<T>::from_json(json const&, udt&) exists
  3124. template<typename BasicJsonType, typename T, typename = void>
  3125. struct has_from_json : std::false_type {};
  3126. // trait checking if j.get<T> is valid
  3127. // use this trait instead of std::is_constructible or std::is_convertible,
  3128. // both rely on, or make use of implicit conversions, and thus fail when T
  3129. // has several constructors/operator= (see https://github.com/nlohmann/json/issues/958)
  3130. template <typename BasicJsonType, typename T>
  3131. struct is_getable
  3132. {
  3133. static constexpr bool value = is_detected<get_template_function, const BasicJsonType&, T>::value;
  3134. };
  3135. template<typename BasicJsonType, typename T>
  3136. struct has_from_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
  3137. {
  3138. using serializer = typename BasicJsonType::template json_serializer<T, void>;
  3139. static constexpr bool value =
  3140. is_detected_exact<void, from_json_function, serializer,
  3141. const BasicJsonType&, T&>::value;
  3142. };
  3143. // This trait checks if JSONSerializer<T>::from_json(json const&) exists
  3144. // this overload is used for non-default-constructible user-defined-types
  3145. template<typename BasicJsonType, typename T, typename = void>
  3146. struct has_non_default_from_json : std::false_type {};
  3147. template<typename BasicJsonType, typename T>
  3148. struct has_non_default_from_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
  3149. {
  3150. using serializer = typename BasicJsonType::template json_serializer<T, void>;
  3151. static constexpr bool value =
  3152. is_detected_exact<T, from_json_function, serializer,
  3153. const BasicJsonType&>::value;
  3154. };
  3155. // This trait checks if BasicJsonType::json_serializer<T>::to_json exists
  3156. // Do not evaluate the trait when T is a basic_json type, to avoid template instantiation infinite recursion.
  3157. template<typename BasicJsonType, typename T, typename = void>
  3158. struct has_to_json : std::false_type {};
  3159. template<typename BasicJsonType, typename T>
  3160. struct has_to_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
  3161. {
  3162. using serializer = typename BasicJsonType::template json_serializer<T, void>;
  3163. static constexpr bool value =
  3164. is_detected_exact<void, to_json_function, serializer, BasicJsonType&,
  3165. T>::value;
  3166. };
  3167. ///////////////////
  3168. // is_ functions //
  3169. ///////////////////
  3170. // https://en.cppreference.com/w/cpp/types/conjunction
  3171. template<class...> struct conjunction : std::true_type { };
  3172. template<class B1> struct conjunction<B1> : B1 { };
  3173. template<class B1, class... Bn>
  3174. struct conjunction<B1, Bn...>
  3175. : std::conditional<bool(B1::value), conjunction<Bn...>, B1>::type {};
  3176. // https://en.cppreference.com/w/cpp/types/negation
  3177. template<class B> struct negation : std::integral_constant < bool, !B::value > { };
  3178. // Reimplementation of is_constructible and is_default_constructible, due to them being broken for
  3179. // std::pair and std::tuple until LWG 2367 fix (see https://cplusplus.github.io/LWG/lwg-defects.html#2367).
  3180. // This causes compile errors in e.g. clang 3.5 or gcc 4.9.
  3181. template <typename T>
  3182. struct is_default_constructible : std::is_default_constructible<T> {};
  3183. template <typename T1, typename T2>
  3184. struct is_default_constructible<std::pair<T1, T2>>
  3185. : conjunction<is_default_constructible<T1>, is_default_constructible<T2>> {};
  3186. template <typename T1, typename T2>
  3187. struct is_default_constructible<const std::pair<T1, T2>>
  3188. : conjunction<is_default_constructible<T1>, is_default_constructible<T2>> {};
  3189. template <typename... Ts>
  3190. struct is_default_constructible<std::tuple<Ts...>>
  3191. : conjunction<is_default_constructible<Ts>...> {};
  3192. template <typename... Ts>
  3193. struct is_default_constructible<const std::tuple<Ts...>>
  3194. : conjunction<is_default_constructible<Ts>...> {};
  3195. template <typename T, typename... Args>
  3196. struct is_constructible : std::is_constructible<T, Args...> {};
  3197. template <typename T1, typename T2>
  3198. struct is_constructible<std::pair<T1, T2>> : is_default_constructible<std::pair<T1, T2>> {};
  3199. template <typename T1, typename T2>
  3200. struct is_constructible<const std::pair<T1, T2>> : is_default_constructible<const std::pair<T1, T2>> {};
  3201. template <typename... Ts>
  3202. struct is_constructible<std::tuple<Ts...>> : is_default_constructible<std::tuple<Ts...>> {};
  3203. template <typename... Ts>
  3204. struct is_constructible<const std::tuple<Ts...>> : is_default_constructible<const std::tuple<Ts...>> {};
  3205. template<typename T, typename = void>
  3206. struct is_iterator_traits : std::false_type {};
  3207. template<typename T>
  3208. struct is_iterator_traits<iterator_traits<T>>
  3209. {
  3210. private:
  3211. using traits = iterator_traits<T>;
  3212. public:
  3213. static constexpr auto value =
  3214. is_detected<value_type_t, traits>::value &&
  3215. is_detected<difference_type_t, traits>::value &&
  3216. is_detected<pointer_t, traits>::value &&
  3217. is_detected<iterator_category_t, traits>::value &&
  3218. is_detected<reference_t, traits>::value;
  3219. };
  3220. // The following implementation of is_complete_type is taken from
  3221. // https://blogs.msdn.microsoft.com/vcblog/2015/12/02/partial-support-for-expression-sfinae-in-vs-2015-update-1/
  3222. // and is written by Xiang Fan who agreed to using it in this library.
  3223. template<typename T, typename = void>
  3224. struct is_complete_type : std::false_type {};
  3225. template<typename T>
  3226. struct is_complete_type<T, decltype(void(sizeof(T)))> : std::true_type {};
  3227. template<typename BasicJsonType, typename CompatibleObjectType,
  3228. typename = void>
  3229. struct is_compatible_object_type_impl : std::false_type {};
  3230. template<typename BasicJsonType, typename CompatibleObjectType>
  3231. struct is_compatible_object_type_impl <
  3232. BasicJsonType, CompatibleObjectType,
  3233. enable_if_t < is_detected<mapped_type_t, CompatibleObjectType>::value&&
  3234. is_detected<key_type_t, CompatibleObjectType>::value >>
  3235. {
  3236. using object_t = typename BasicJsonType::object_t;
  3237. // macOS's is_constructible does not play well with nonesuch...
  3238. static constexpr bool value =
  3239. is_constructible<typename object_t::key_type,
  3240. typename CompatibleObjectType::key_type>::value &&
  3241. is_constructible<typename object_t::mapped_type,
  3242. typename CompatibleObjectType::mapped_type>::value;
  3243. };
  3244. template<typename BasicJsonType, typename CompatibleObjectType>
  3245. struct is_compatible_object_type
  3246. : is_compatible_object_type_impl<BasicJsonType, CompatibleObjectType> {};
  3247. template<typename BasicJsonType, typename ConstructibleObjectType,
  3248. typename = void>
  3249. struct is_constructible_object_type_impl : std::false_type {};
  3250. template<typename BasicJsonType, typename ConstructibleObjectType>
  3251. struct is_constructible_object_type_impl <
  3252. BasicJsonType, ConstructibleObjectType,
  3253. enable_if_t < is_detected<mapped_type_t, ConstructibleObjectType>::value&&
  3254. is_detected<key_type_t, ConstructibleObjectType>::value >>
  3255. {
  3256. using object_t = typename BasicJsonType::object_t;
  3257. static constexpr bool value =
  3258. (is_default_constructible<ConstructibleObjectType>::value &&
  3259. (std::is_move_assignable<ConstructibleObjectType>::value ||
  3260. std::is_copy_assignable<ConstructibleObjectType>::value) &&
  3261. (is_constructible<typename ConstructibleObjectType::key_type,
  3262. typename object_t::key_type>::value &&
  3263. std::is_same <
  3264. typename object_t::mapped_type,
  3265. typename ConstructibleObjectType::mapped_type >::value)) ||
  3266. (has_from_json<BasicJsonType,
  3267. typename ConstructibleObjectType::mapped_type>::value ||
  3268. has_non_default_from_json <
  3269. BasicJsonType,
  3270. typename ConstructibleObjectType::mapped_type >::value);
  3271. };
  3272. template<typename BasicJsonType, typename ConstructibleObjectType>
  3273. struct is_constructible_object_type
  3274. : is_constructible_object_type_impl<BasicJsonType,
  3275. ConstructibleObjectType> {};
  3276. template<typename BasicJsonType, typename CompatibleStringType,
  3277. typename = void>
  3278. struct is_compatible_string_type_impl : std::false_type {};
  3279. template<typename BasicJsonType, typename CompatibleStringType>
  3280. struct is_compatible_string_type_impl <
  3281. BasicJsonType, CompatibleStringType,
  3282. enable_if_t<is_detected_exact<typename BasicJsonType::string_t::value_type,
  3283. value_type_t, CompatibleStringType>::value >>
  3284. {
  3285. static constexpr auto value =
  3286. is_constructible<typename BasicJsonType::string_t, CompatibleStringType>::value;
  3287. };
  3288. template<typename BasicJsonType, typename ConstructibleStringType>
  3289. struct is_compatible_string_type
  3290. : is_compatible_string_type_impl<BasicJsonType, ConstructibleStringType> {};
  3291. template<typename BasicJsonType, typename ConstructibleStringType,
  3292. typename = void>
  3293. struct is_constructible_string_type_impl : std::false_type {};
  3294. template<typename BasicJsonType, typename ConstructibleStringType>
  3295. struct is_constructible_string_type_impl <
  3296. BasicJsonType, ConstructibleStringType,
  3297. enable_if_t<is_detected_exact<typename BasicJsonType::string_t::value_type,
  3298. value_type_t, ConstructibleStringType>::value >>
  3299. {
  3300. static constexpr auto value =
  3301. is_constructible<ConstructibleStringType,
  3302. typename BasicJsonType::string_t>::value;
  3303. };
  3304. template<typename BasicJsonType, typename ConstructibleStringType>
  3305. struct is_constructible_string_type
  3306. : is_constructible_string_type_impl<BasicJsonType, ConstructibleStringType> {};
  3307. template<typename BasicJsonType, typename CompatibleArrayType, typename = void>
  3308. struct is_compatible_array_type_impl : std::false_type {};
  3309. template<typename BasicJsonType, typename CompatibleArrayType>
  3310. struct is_compatible_array_type_impl <
  3311. BasicJsonType, CompatibleArrayType,
  3312. enable_if_t < is_detected<value_type_t, CompatibleArrayType>::value&&
  3313. is_detected<iterator_t, CompatibleArrayType>::value&&
  3314. // This is needed because json_reverse_iterator has a ::iterator type...
  3315. // Therefore it is detected as a CompatibleArrayType.
  3316. // The real fix would be to have an Iterable concept.
  3317. !is_iterator_traits <
  3318. iterator_traits<CompatibleArrayType >>::value >>
  3319. {
  3320. static constexpr bool value =
  3321. is_constructible<BasicJsonType,
  3322. typename CompatibleArrayType::value_type>::value;
  3323. };
  3324. template<typename BasicJsonType, typename CompatibleArrayType>
  3325. struct is_compatible_array_type
  3326. : is_compatible_array_type_impl<BasicJsonType, CompatibleArrayType> {};
  3327. template<typename BasicJsonType, typename ConstructibleArrayType, typename = void>
  3328. struct is_constructible_array_type_impl : std::false_type {};
  3329. template<typename BasicJsonType, typename ConstructibleArrayType>
  3330. struct is_constructible_array_type_impl <
  3331. BasicJsonType, ConstructibleArrayType,
  3332. enable_if_t<std::is_same<ConstructibleArrayType,
  3333. typename BasicJsonType::value_type>::value >>
  3334. : std::true_type {};
  3335. template<typename BasicJsonType, typename ConstructibleArrayType>
  3336. struct is_constructible_array_type_impl <
  3337. BasicJsonType, ConstructibleArrayType,
  3338. enable_if_t < !std::is_same<ConstructibleArrayType,
  3339. typename BasicJsonType::value_type>::value&&
  3340. is_default_constructible<ConstructibleArrayType>::value&&
  3341. (std::is_move_assignable<ConstructibleArrayType>::value ||
  3342. std::is_copy_assignable<ConstructibleArrayType>::value)&&
  3343. is_detected<value_type_t, ConstructibleArrayType>::value&&
  3344. is_detected<iterator_t, ConstructibleArrayType>::value&&
  3345. is_complete_type <
  3346. detected_t<value_type_t, ConstructibleArrayType >>::value >>
  3347. {
  3348. static constexpr bool value =
  3349. // This is needed because json_reverse_iterator has a ::iterator type,
  3350. // furthermore, std::back_insert_iterator (and other iterators) have a
  3351. // base class `iterator`... Therefore it is detected as a
  3352. // ConstructibleArrayType. The real fix would be to have an Iterable
  3353. // concept.
  3354. !is_iterator_traits<iterator_traits<ConstructibleArrayType>>::value &&
  3355. (std::is_same<typename ConstructibleArrayType::value_type,
  3356. typename BasicJsonType::array_t::value_type>::value ||
  3357. has_from_json<BasicJsonType,
  3358. typename ConstructibleArrayType::value_type>::value ||
  3359. has_non_default_from_json <
  3360. BasicJsonType, typename ConstructibleArrayType::value_type >::value);
  3361. };
  3362. template<typename BasicJsonType, typename ConstructibleArrayType>
  3363. struct is_constructible_array_type
  3364. : is_constructible_array_type_impl<BasicJsonType, ConstructibleArrayType> {};
  3365. template<typename RealIntegerType, typename CompatibleNumberIntegerType,
  3366. typename = void>
  3367. struct is_compatible_integer_type_impl : std::false_type {};
  3368. template<typename RealIntegerType, typename CompatibleNumberIntegerType>
  3369. struct is_compatible_integer_type_impl <
  3370. RealIntegerType, CompatibleNumberIntegerType,
  3371. enable_if_t < std::is_integral<RealIntegerType>::value&&
  3372. std::is_integral<CompatibleNumberIntegerType>::value&&
  3373. !std::is_same<bool, CompatibleNumberIntegerType>::value >>
  3374. {
  3375. // is there an assert somewhere on overflows?
  3376. using RealLimits = std::numeric_limits<RealIntegerType>;
  3377. using CompatibleLimits = std::numeric_limits<CompatibleNumberIntegerType>;
  3378. static constexpr auto value =
  3379. is_constructible<RealIntegerType,
  3380. CompatibleNumberIntegerType>::value &&
  3381. CompatibleLimits::is_integer &&
  3382. RealLimits::is_signed == CompatibleLimits::is_signed;
  3383. };
  3384. template<typename RealIntegerType, typename CompatibleNumberIntegerType>
  3385. struct is_compatible_integer_type
  3386. : is_compatible_integer_type_impl<RealIntegerType,
  3387. CompatibleNumberIntegerType> {};
  3388. template<typename BasicJsonType, typename CompatibleType, typename = void>
  3389. struct is_compatible_type_impl: std::false_type {};
  3390. template<typename BasicJsonType, typename CompatibleType>
  3391. struct is_compatible_type_impl <
  3392. BasicJsonType, CompatibleType,
  3393. enable_if_t<is_complete_type<CompatibleType>::value >>
  3394. {
  3395. static constexpr bool value =
  3396. has_to_json<BasicJsonType, CompatibleType>::value;
  3397. };
  3398. template<typename BasicJsonType, typename CompatibleType>
  3399. struct is_compatible_type
  3400. : is_compatible_type_impl<BasicJsonType, CompatibleType> {};
  3401. template<typename T1, typename T2>
  3402. struct is_constructible_tuple : std::false_type {};
  3403. template<typename T1, typename... Args>
  3404. struct is_constructible_tuple<T1, std::tuple<Args...>> : conjunction<is_constructible<T1, Args>...> {};
  3405. // a naive helper to check if a type is an ordered_map (exploits the fact that
  3406. // ordered_map inherits capacity() from std::vector)
  3407. template <typename T>
  3408. struct is_ordered_map
  3409. {
  3410. using one = char;
  3411. struct two
  3412. {
  3413. char x[2]; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  3414. };
  3415. template <typename C> static one test( decltype(&C::capacity) ) ;
  3416. template <typename C> static two test(...);
  3417. enum { value = sizeof(test<T>(nullptr)) == sizeof(char) }; // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  3418. };
  3419. // to avoid useless casts (see https://github.com/nlohmann/json/issues/2893#issuecomment-889152324)
  3420. template < typename T, typename U, enable_if_t < !std::is_same<T, U>::value, int > = 0 >
  3421. T conditional_static_cast(U value)
  3422. {
  3423. return static_cast<T>(value);
  3424. }
  3425. template<typename T, typename U, enable_if_t<std::is_same<T, U>::value, int> = 0>
  3426. T conditional_static_cast(U value)
  3427. {
  3428. return value;
  3429. }
  3430. } // namespace detail
  3431. } // namespace nlohmann
  3432. // #include <nlohmann/detail/value_t.hpp>
  3433. namespace nlohmann
  3434. {
  3435. namespace detail
  3436. {
  3437. template<typename BasicJsonType>
  3438. void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
  3439. {
  3440. if (JSON_HEDLEY_UNLIKELY(!j.is_null()))
  3441. {
  3442. JSON_THROW(type_error::create(302, "type must be null, but is " + std::string(j.type_name()), j));
  3443. }
  3444. n = nullptr;
  3445. }
  3446. // overloads for basic_json template parameters
  3447. template < typename BasicJsonType, typename ArithmeticType,
  3448. enable_if_t < std::is_arithmetic<ArithmeticType>::value&&
  3449. !std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
  3450. int > = 0 >
  3451. void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val)
  3452. {
  3453. switch (static_cast<value_t>(j))
  3454. {
  3455. case value_t::number_unsigned:
  3456. {
  3457. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
  3458. break;
  3459. }
  3460. case value_t::number_integer:
  3461. {
  3462. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
  3463. break;
  3464. }
  3465. case value_t::number_float:
  3466. {
  3467. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
  3468. break;
  3469. }
  3470. case value_t::null:
  3471. case value_t::object:
  3472. case value_t::array:
  3473. case value_t::string:
  3474. case value_t::boolean:
  3475. case value_t::binary:
  3476. case value_t::discarded:
  3477. default:
  3478. JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()), j));
  3479. }
  3480. }
  3481. template<typename BasicJsonType>
  3482. void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
  3483. {
  3484. if (JSON_HEDLEY_UNLIKELY(!j.is_boolean()))
  3485. {
  3486. JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(j.type_name()), j));
  3487. }
  3488. b = *j.template get_ptr<const typename BasicJsonType::boolean_t*>();
  3489. }
  3490. template<typename BasicJsonType>
  3491. void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s)
  3492. {
  3493. if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
  3494. {
  3495. JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), j));
  3496. }
  3497. s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
  3498. }
  3499. template <
  3500. typename BasicJsonType, typename ConstructibleStringType,
  3501. enable_if_t <
  3502. is_constructible_string_type<BasicJsonType, ConstructibleStringType>::value&&
  3503. !std::is_same<typename BasicJsonType::string_t,
  3504. ConstructibleStringType>::value,
  3505. int > = 0 >
  3506. void from_json(const BasicJsonType& j, ConstructibleStringType& s)
  3507. {
  3508. if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
  3509. {
  3510. JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), j));
  3511. }
  3512. s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
  3513. }
  3514. template<typename BasicJsonType>
  3515. void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val)
  3516. {
  3517. get_arithmetic_value(j, val);
  3518. }
  3519. template<typename BasicJsonType>
  3520. void from_json(const BasicJsonType& j, typename BasicJsonType::number_unsigned_t& val)
  3521. {
  3522. get_arithmetic_value(j, val);
  3523. }
  3524. template<typename BasicJsonType>
  3525. void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& val)
  3526. {
  3527. get_arithmetic_value(j, val);
  3528. }
  3529. template<typename BasicJsonType, typename EnumType,
  3530. enable_if_t<std::is_enum<EnumType>::value, int> = 0>
  3531. void from_json(const BasicJsonType& j, EnumType& e)
  3532. {
  3533. typename std::underlying_type<EnumType>::type val;
  3534. get_arithmetic_value(j, val);
  3535. e = static_cast<EnumType>(val);
  3536. }
  3537. // forward_list doesn't have an insert method
  3538. template<typename BasicJsonType, typename T, typename Allocator,
  3539. enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
  3540. void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
  3541. {
  3542. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  3543. {
  3544. JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
  3545. }
  3546. l.clear();
  3547. std::transform(j.rbegin(), j.rend(),
  3548. std::front_inserter(l), [](const BasicJsonType & i)
  3549. {
  3550. return i.template get<T>();
  3551. });
  3552. }
  3553. // valarray doesn't have an insert method
  3554. template<typename BasicJsonType, typename T,
  3555. enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
  3556. void from_json(const BasicJsonType& j, std::valarray<T>& l)
  3557. {
  3558. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  3559. {
  3560. JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
  3561. }
  3562. l.resize(j.size());
  3563. std::transform(j.begin(), j.end(), std::begin(l),
  3564. [](const BasicJsonType & elem)
  3565. {
  3566. return elem.template get<T>();
  3567. });
  3568. }
  3569. template<typename BasicJsonType, typename T, std::size_t N>
  3570. auto from_json(const BasicJsonType& j, T (&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  3571. -> decltype(j.template get<T>(), void())
  3572. {
  3573. for (std::size_t i = 0; i < N; ++i)
  3574. {
  3575. arr[i] = j.at(i).template get<T>();
  3576. }
  3577. }
  3578. template<typename BasicJsonType>
  3579. void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType::array_t& arr, priority_tag<3> /*unused*/)
  3580. {
  3581. arr = *j.template get_ptr<const typename BasicJsonType::array_t*>();
  3582. }
  3583. template<typename BasicJsonType, typename T, std::size_t N>
  3584. auto from_json_array_impl(const BasicJsonType& j, std::array<T, N>& arr,
  3585. priority_tag<2> /*unused*/)
  3586. -> decltype(j.template get<T>(), void())
  3587. {
  3588. for (std::size_t i = 0; i < N; ++i)
  3589. {
  3590. arr[i] = j.at(i).template get<T>();
  3591. }
  3592. }
  3593. template<typename BasicJsonType, typename ConstructibleArrayType,
  3594. enable_if_t<
  3595. std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
  3596. int> = 0>
  3597. auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, priority_tag<1> /*unused*/)
  3598. -> decltype(
  3599. arr.reserve(std::declval<typename ConstructibleArrayType::size_type>()),
  3600. j.template get<typename ConstructibleArrayType::value_type>(),
  3601. void())
  3602. {
  3603. using std::end;
  3604. ConstructibleArrayType ret;
  3605. ret.reserve(j.size());
  3606. std::transform(j.begin(), j.end(),
  3607. std::inserter(ret, end(ret)), [](const BasicJsonType & i)
  3608. {
  3609. // get<BasicJsonType>() returns *this, this won't call a from_json
  3610. // method when value_type is BasicJsonType
  3611. return i.template get<typename ConstructibleArrayType::value_type>();
  3612. });
  3613. arr = std::move(ret);
  3614. }
  3615. template<typename BasicJsonType, typename ConstructibleArrayType,
  3616. enable_if_t<
  3617. std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
  3618. int> = 0>
  3619. void from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr,
  3620. priority_tag<0> /*unused*/)
  3621. {
  3622. using std::end;
  3623. ConstructibleArrayType ret;
  3624. std::transform(
  3625. j.begin(), j.end(), std::inserter(ret, end(ret)),
  3626. [](const BasicJsonType & i)
  3627. {
  3628. // get<BasicJsonType>() returns *this, this won't call a from_json
  3629. // method when value_type is BasicJsonType
  3630. return i.template get<typename ConstructibleArrayType::value_type>();
  3631. });
  3632. arr = std::move(ret);
  3633. }
  3634. template < typename BasicJsonType, typename ConstructibleArrayType,
  3635. enable_if_t <
  3636. is_constructible_array_type<BasicJsonType, ConstructibleArrayType>::value&&
  3637. !is_constructible_object_type<BasicJsonType, ConstructibleArrayType>::value&&
  3638. !is_constructible_string_type<BasicJsonType, ConstructibleArrayType>::value&&
  3639. !std::is_same<ConstructibleArrayType, typename BasicJsonType::binary_t>::value&&
  3640. !is_basic_json<ConstructibleArrayType>::value,
  3641. int > = 0 >
  3642. auto from_json(const BasicJsonType& j, ConstructibleArrayType& arr)
  3643. -> decltype(from_json_array_impl(j, arr, priority_tag<3> {}),
  3644. j.template get<typename ConstructibleArrayType::value_type>(),
  3645. void())
  3646. {
  3647. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  3648. {
  3649. JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
  3650. }
  3651. from_json_array_impl(j, arr, priority_tag<3> {});
  3652. }
  3653. template < typename BasicJsonType, typename T, std::size_t... Idx >
  3654. std::array<T, sizeof...(Idx)> from_json_inplace_array_impl(BasicJsonType&& j,
  3655. identity_tag<std::array<T, sizeof...(Idx)>> /*unused*/, index_sequence<Idx...> /*unused*/)
  3656. {
  3657. return { { std::forward<BasicJsonType>(j).at(Idx).template get<T>()... } };
  3658. }
  3659. template < typename BasicJsonType, typename T, std::size_t N >
  3660. auto from_json(BasicJsonType&& j, identity_tag<std::array<T, N>> tag)
  3661. -> decltype(from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {}))
  3662. {
  3663. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  3664. {
  3665. JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
  3666. }
  3667. return from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {});
  3668. }
  3669. template<typename BasicJsonType>
  3670. void from_json(const BasicJsonType& j, typename BasicJsonType::binary_t& bin)
  3671. {
  3672. if (JSON_HEDLEY_UNLIKELY(!j.is_binary()))
  3673. {
  3674. JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(j.type_name()), j));
  3675. }
  3676. bin = *j.template get_ptr<const typename BasicJsonType::binary_t*>();
  3677. }
  3678. template<typename BasicJsonType, typename ConstructibleObjectType,
  3679. enable_if_t<is_constructible_object_type<BasicJsonType, ConstructibleObjectType>::value, int> = 0>
  3680. void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
  3681. {
  3682. if (JSON_HEDLEY_UNLIKELY(!j.is_object()))
  3683. {
  3684. JSON_THROW(type_error::create(302, "type must be object, but is " + std::string(j.type_name()), j));
  3685. }
  3686. ConstructibleObjectType ret;
  3687. const auto* inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
  3688. using value_type = typename ConstructibleObjectType::value_type;
  3689. std::transform(
  3690. inner_object->begin(), inner_object->end(),
  3691. std::inserter(ret, ret.begin()),
  3692. [](typename BasicJsonType::object_t::value_type const & p)
  3693. {
  3694. return value_type(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
  3695. });
  3696. obj = std::move(ret);
  3697. }
  3698. // overload for arithmetic types, not chosen for basic_json template arguments
  3699. // (BooleanType, etc..); note: Is it really necessary to provide explicit
  3700. // overloads for boolean_t etc. in case of a custom BooleanType which is not
  3701. // an arithmetic type?
  3702. template < typename BasicJsonType, typename ArithmeticType,
  3703. enable_if_t <
  3704. std::is_arithmetic<ArithmeticType>::value&&
  3705. !std::is_same<ArithmeticType, typename BasicJsonType::number_unsigned_t>::value&&
  3706. !std::is_same<ArithmeticType, typename BasicJsonType::number_integer_t>::value&&
  3707. !std::is_same<ArithmeticType, typename BasicJsonType::number_float_t>::value&&
  3708. !std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
  3709. int > = 0 >
  3710. void from_json(const BasicJsonType& j, ArithmeticType& val)
  3711. {
  3712. switch (static_cast<value_t>(j))
  3713. {
  3714. case value_t::number_unsigned:
  3715. {
  3716. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
  3717. break;
  3718. }
  3719. case value_t::number_integer:
  3720. {
  3721. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
  3722. break;
  3723. }
  3724. case value_t::number_float:
  3725. {
  3726. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
  3727. break;
  3728. }
  3729. case value_t::boolean:
  3730. {
  3731. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::boolean_t*>());
  3732. break;
  3733. }
  3734. case value_t::null:
  3735. case value_t::object:
  3736. case value_t::array:
  3737. case value_t::string:
  3738. case value_t::binary:
  3739. case value_t::discarded:
  3740. default:
  3741. JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()), j));
  3742. }
  3743. }
  3744. template<typename BasicJsonType, typename... Args, std::size_t... Idx>
  3745. std::tuple<Args...> from_json_tuple_impl_base(BasicJsonType&& j, index_sequence<Idx...> /*unused*/)
  3746. {
  3747. return std::make_tuple(std::forward<BasicJsonType>(j).at(Idx).template get<Args>()...);
  3748. }
  3749. template < typename BasicJsonType, class A1, class A2 >
  3750. std::pair<A1, A2> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::pair<A1, A2>> /*unused*/, priority_tag<0> /*unused*/)
  3751. {
  3752. return {std::forward<BasicJsonType>(j).at(0).template get<A1>(),
  3753. std::forward<BasicJsonType>(j).at(1).template get<A2>()};
  3754. }
  3755. template<typename BasicJsonType, typename A1, typename A2>
  3756. void from_json_tuple_impl(BasicJsonType&& j, std::pair<A1, A2>& p, priority_tag<1> /*unused*/)
  3757. {
  3758. p = from_json_tuple_impl(std::forward<BasicJsonType>(j), identity_tag<std::pair<A1, A2>> {}, priority_tag<0> {});
  3759. }
  3760. template<typename BasicJsonType, typename... Args>
  3761. std::tuple<Args...> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::tuple<Args...>> /*unused*/, priority_tag<2> /*unused*/)
  3762. {
  3763. return from_json_tuple_impl_base<BasicJsonType, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
  3764. }
  3765. template<typename BasicJsonType, typename... Args>
  3766. void from_json_tuple_impl(BasicJsonType&& j, std::tuple<Args...>& t, priority_tag<3> /*unused*/)
  3767. {
  3768. t = from_json_tuple_impl_base<BasicJsonType, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
  3769. }
  3770. template<typename BasicJsonType, typename TupleRelated>
  3771. auto from_json(BasicJsonType&& j, TupleRelated&& t)
  3772. -> decltype(from_json_tuple_impl(std::forward<BasicJsonType>(j), std::forward<TupleRelated>(t), priority_tag<3> {}))
  3773. {
  3774. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  3775. {
  3776. JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
  3777. }
  3778. return from_json_tuple_impl(std::forward<BasicJsonType>(j), std::forward<TupleRelated>(t), priority_tag<3> {});
  3779. }
  3780. template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator,
  3781. typename = enable_if_t < !std::is_constructible <
  3782. typename BasicJsonType::string_t, Key >::value >>
  3783. void from_json(const BasicJsonType& j, std::map<Key, Value, Compare, Allocator>& m)
  3784. {
  3785. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  3786. {
  3787. JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
  3788. }
  3789. m.clear();
  3790. for (const auto& p : j)
  3791. {
  3792. if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
  3793. {
  3794. JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()), j));
  3795. }
  3796. m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
  3797. }
  3798. }
  3799. template < typename BasicJsonType, typename Key, typename Value, typename Hash, typename KeyEqual, typename Allocator,
  3800. typename = enable_if_t < !std::is_constructible <
  3801. typename BasicJsonType::string_t, Key >::value >>
  3802. void from_json(const BasicJsonType& j, std::unordered_map<Key, Value, Hash, KeyEqual, Allocator>& m)
  3803. {
  3804. if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
  3805. {
  3806. JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
  3807. }
  3808. m.clear();
  3809. for (const auto& p : j)
  3810. {
  3811. if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
  3812. {
  3813. JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()), j));
  3814. }
  3815. m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
  3816. }
  3817. }
  3818. struct from_json_fn
  3819. {
  3820. template<typename BasicJsonType, typename T>
  3821. auto operator()(const BasicJsonType& j, T&& val) const
  3822. noexcept(noexcept(from_json(j, std::forward<T>(val))))
  3823. -> decltype(from_json(j, std::forward<T>(val)))
  3824. {
  3825. return from_json(j, std::forward<T>(val));
  3826. }
  3827. };
  3828. } // namespace detail
  3829. /// namespace to hold default `from_json` function
  3830. /// to see why this is required:
  3831. /// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
  3832. namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces)
  3833. {
  3834. constexpr const auto& from_json = detail::static_const<detail::from_json_fn>::value; // NOLINT(misc-definitions-in-headers)
  3835. } // namespace
  3836. } // namespace nlohmann
  3837. // #include <nlohmann/detail/conversions/to_json.hpp>
  3838. #include <algorithm> // copy
  3839. #include <iterator> // begin, end
  3840. #include <string> // string
  3841. #include <tuple> // tuple, get
  3842. #include <type_traits> // is_same, is_constructible, is_floating_point, is_enum, underlying_type
  3843. #include <utility> // move, forward, declval, pair
  3844. #include <valarray> // valarray
  3845. #include <vector> // vector
  3846. // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
  3847. #include <cstddef> // size_t
  3848. #include <iterator> // input_iterator_tag
  3849. #include <string> // string, to_string
  3850. #include <tuple> // tuple_size, get, tuple_element
  3851. #include <utility> // move
  3852. // #include <nlohmann/detail/meta/type_traits.hpp>
  3853. // #include <nlohmann/detail/value_t.hpp>
  3854. namespace nlohmann
  3855. {
  3856. namespace detail
  3857. {
  3858. template<typename string_type>
  3859. void int_to_string( string_type& target, std::size_t value )
  3860. {
  3861. // For ADL
  3862. using std::to_string;
  3863. target = to_string(value);
  3864. }
  3865. template<typename IteratorType> class iteration_proxy_value
  3866. {
  3867. public:
  3868. using difference_type = std::ptrdiff_t;
  3869. using value_type = iteration_proxy_value;
  3870. using pointer = value_type * ;
  3871. using reference = value_type & ;
  3872. using iterator_category = std::input_iterator_tag;
  3873. using string_type = typename std::remove_cv< typename std::remove_reference<decltype( std::declval<IteratorType>().key() ) >::type >::type;
  3874. private:
  3875. /// the iterator
  3876. IteratorType anchor;
  3877. /// an index for arrays (used to create key names)
  3878. std::size_t array_index = 0;
  3879. /// last stringified array index
  3880. mutable std::size_t array_index_last = 0;
  3881. /// a string representation of the array index
  3882. mutable string_type array_index_str = "0";
  3883. /// an empty string (to return a reference for primitive values)
  3884. const string_type empty_str{};
  3885. public:
  3886. explicit iteration_proxy_value(IteratorType it) noexcept
  3887. : anchor(std::move(it))
  3888. {}
  3889. /// dereference operator (needed for range-based for)
  3890. iteration_proxy_value& operator*()
  3891. {
  3892. return *this;
  3893. }
  3894. /// increment operator (needed for range-based for)
  3895. iteration_proxy_value& operator++()
  3896. {
  3897. ++anchor;
  3898. ++array_index;
  3899. return *this;
  3900. }
  3901. /// equality operator (needed for InputIterator)
  3902. bool operator==(const iteration_proxy_value& o) const
  3903. {
  3904. return anchor == o.anchor;
  3905. }
  3906. /// inequality operator (needed for range-based for)
  3907. bool operator!=(const iteration_proxy_value& o) const
  3908. {
  3909. return anchor != o.anchor;
  3910. }
  3911. /// return key of the iterator
  3912. const string_type& key() const
  3913. {
  3914. JSON_ASSERT(anchor.m_object != nullptr);
  3915. switch (anchor.m_object->type())
  3916. {
  3917. // use integer array index as key
  3918. case value_t::array:
  3919. {
  3920. if (array_index != array_index_last)
  3921. {
  3922. int_to_string( array_index_str, array_index );
  3923. array_index_last = array_index;
  3924. }
  3925. return array_index_str;
  3926. }
  3927. // use key from the object
  3928. case value_t::object:
  3929. return anchor.key();
  3930. // use an empty key for all primitive types
  3931. case value_t::null:
  3932. case value_t::string:
  3933. case value_t::boolean:
  3934. case value_t::number_integer:
  3935. case value_t::number_unsigned:
  3936. case value_t::number_float:
  3937. case value_t::binary:
  3938. case value_t::discarded:
  3939. default:
  3940. return empty_str;
  3941. }
  3942. }
  3943. /// return value of the iterator
  3944. typename IteratorType::reference value() const
  3945. {
  3946. return anchor.value();
  3947. }
  3948. };
  3949. /// proxy class for the items() function
  3950. template<typename IteratorType> class iteration_proxy
  3951. {
  3952. private:
  3953. /// the container to iterate
  3954. typename IteratorType::reference container;
  3955. public:
  3956. /// construct iteration proxy from a container
  3957. explicit iteration_proxy(typename IteratorType::reference cont) noexcept
  3958. : container(cont) {}
  3959. /// return iterator begin (needed for range-based for)
  3960. iteration_proxy_value<IteratorType> begin() noexcept
  3961. {
  3962. return iteration_proxy_value<IteratorType>(container.begin());
  3963. }
  3964. /// return iterator end (needed for range-based for)
  3965. iteration_proxy_value<IteratorType> end() noexcept
  3966. {
  3967. return iteration_proxy_value<IteratorType>(container.end());
  3968. }
  3969. };
  3970. // Structured Bindings Support
  3971. // For further reference see https://blog.tartanllama.xyz/structured-bindings/
  3972. // And see https://github.com/nlohmann/json/pull/1391
  3973. template<std::size_t N, typename IteratorType, enable_if_t<N == 0, int> = 0>
  3974. auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.key())
  3975. {
  3976. return i.key();
  3977. }
  3978. // Structured Bindings Support
  3979. // For further reference see https://blog.tartanllama.xyz/structured-bindings/
  3980. // And see https://github.com/nlohmann/json/pull/1391
  3981. template<std::size_t N, typename IteratorType, enable_if_t<N == 1, int> = 0>
  3982. auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.value())
  3983. {
  3984. return i.value();
  3985. }
  3986. } // namespace detail
  3987. } // namespace nlohmann
  3988. // The Addition to the STD Namespace is required to add
  3989. // Structured Bindings Support to the iteration_proxy_value class
  3990. // For further reference see https://blog.tartanllama.xyz/structured-bindings/
  3991. // And see https://github.com/nlohmann/json/pull/1391
  3992. namespace std
  3993. {
  3994. #if defined(__clang__)
  3995. // Fix: https://github.com/nlohmann/json/issues/1401
  3996. #pragma clang diagnostic push
  3997. #pragma clang diagnostic ignored "-Wmismatched-tags"
  3998. #endif
  3999. template<typename IteratorType>
  4000. class tuple_size<::nlohmann::detail::iteration_proxy_value<IteratorType>>
  4001. : public std::integral_constant<std::size_t, 2> {};
  4002. template<std::size_t N, typename IteratorType>
  4003. class tuple_element<N, ::nlohmann::detail::iteration_proxy_value<IteratorType >>
  4004. {
  4005. public:
  4006. using type = decltype(
  4007. get<N>(std::declval <
  4008. ::nlohmann::detail::iteration_proxy_value<IteratorType >> ()));
  4009. };
  4010. #if defined(__clang__)
  4011. #pragma clang diagnostic pop
  4012. #endif
  4013. } // namespace std
  4014. // #include <nlohmann/detail/meta/cpp_future.hpp>
  4015. // #include <nlohmann/detail/meta/type_traits.hpp>
  4016. // #include <nlohmann/detail/value_t.hpp>
  4017. namespace nlohmann
  4018. {
  4019. namespace detail
  4020. {
  4021. //////////////////
  4022. // constructors //
  4023. //////////////////
  4024. /*
  4025. * Note all external_constructor<>::construct functions need to call
  4026. * j.m_value.destroy(j.m_type) to avoid a memory leak in case j contains an
  4027. * allocated value (e.g., a string). See bug issue
  4028. * https://github.com/nlohmann/json/issues/2865 for more information.
  4029. */
  4030. template<value_t> struct external_constructor;
  4031. template<>
  4032. struct external_constructor<value_t::boolean>
  4033. {
  4034. template<typename BasicJsonType>
  4035. static void construct(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept
  4036. {
  4037. j.m_value.destroy(j.m_type);
  4038. j.m_type = value_t::boolean;
  4039. j.m_value = b;
  4040. j.assert_invariant();
  4041. }
  4042. };
  4043. template<>
  4044. struct external_constructor<value_t::string>
  4045. {
  4046. template<typename BasicJsonType>
  4047. static void construct(BasicJsonType& j, const typename BasicJsonType::string_t& s)
  4048. {
  4049. j.m_value.destroy(j.m_type);
  4050. j.m_type = value_t::string;
  4051. j.m_value = s;
  4052. j.assert_invariant();
  4053. }
  4054. template<typename BasicJsonType>
  4055. static void construct(BasicJsonType& j, typename BasicJsonType::string_t&& s)
  4056. {
  4057. j.m_value.destroy(j.m_type);
  4058. j.m_type = value_t::string;
  4059. j.m_value = std::move(s);
  4060. j.assert_invariant();
  4061. }
  4062. template < typename BasicJsonType, typename CompatibleStringType,
  4063. enable_if_t < !std::is_same<CompatibleStringType, typename BasicJsonType::string_t>::value,
  4064. int > = 0 >
  4065. static void construct(BasicJsonType& j, const CompatibleStringType& str)
  4066. {
  4067. j.m_value.destroy(j.m_type);
  4068. j.m_type = value_t::string;
  4069. j.m_value.string = j.template create<typename BasicJsonType::string_t>(str);
  4070. j.assert_invariant();
  4071. }
  4072. };
  4073. template<>
  4074. struct external_constructor<value_t::binary>
  4075. {
  4076. template<typename BasicJsonType>
  4077. static void construct(BasicJsonType& j, const typename BasicJsonType::binary_t& b)
  4078. {
  4079. j.m_value.destroy(j.m_type);
  4080. j.m_type = value_t::binary;
  4081. j.m_value = typename BasicJsonType::binary_t(b);
  4082. j.assert_invariant();
  4083. }
  4084. template<typename BasicJsonType>
  4085. static void construct(BasicJsonType& j, typename BasicJsonType::binary_t&& b)
  4086. {
  4087. j.m_value.destroy(j.m_type);
  4088. j.m_type = value_t::binary;
  4089. j.m_value = typename BasicJsonType::binary_t(std::move(b));
  4090. j.assert_invariant();
  4091. }
  4092. };
  4093. template<>
  4094. struct external_constructor<value_t::number_float>
  4095. {
  4096. template<typename BasicJsonType>
  4097. static void construct(BasicJsonType& j, typename BasicJsonType::number_float_t val) noexcept
  4098. {
  4099. j.m_value.destroy(j.m_type);
  4100. j.m_type = value_t::number_float;
  4101. j.m_value = val;
  4102. j.assert_invariant();
  4103. }
  4104. };
  4105. template<>
  4106. struct external_constructor<value_t::number_unsigned>
  4107. {
  4108. template<typename BasicJsonType>
  4109. static void construct(BasicJsonType& j, typename BasicJsonType::number_unsigned_t val) noexcept
  4110. {
  4111. j.m_value.destroy(j.m_type);
  4112. j.m_type = value_t::number_unsigned;
  4113. j.m_value = val;
  4114. j.assert_invariant();
  4115. }
  4116. };
  4117. template<>
  4118. struct external_constructor<value_t::number_integer>
  4119. {
  4120. template<typename BasicJsonType>
  4121. static void construct(BasicJsonType& j, typename BasicJsonType::number_integer_t val) noexcept
  4122. {
  4123. j.m_value.destroy(j.m_type);
  4124. j.m_type = value_t::number_integer;
  4125. j.m_value = val;
  4126. j.assert_invariant();
  4127. }
  4128. };
  4129. template<>
  4130. struct external_constructor<value_t::array>
  4131. {
  4132. template<typename BasicJsonType>
  4133. static void construct(BasicJsonType& j, const typename BasicJsonType::array_t& arr)
  4134. {
  4135. j.m_value.destroy(j.m_type);
  4136. j.m_type = value_t::array;
  4137. j.m_value = arr;
  4138. j.set_parents();
  4139. j.assert_invariant();
  4140. }
  4141. template<typename BasicJsonType>
  4142. static void construct(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
  4143. {
  4144. j.m_value.destroy(j.m_type);
  4145. j.m_type = value_t::array;
  4146. j.m_value = std::move(arr);
  4147. j.set_parents();
  4148. j.assert_invariant();
  4149. }
  4150. template < typename BasicJsonType, typename CompatibleArrayType,
  4151. enable_if_t < !std::is_same<CompatibleArrayType, typename BasicJsonType::array_t>::value,
  4152. int > = 0 >
  4153. static void construct(BasicJsonType& j, const CompatibleArrayType& arr)
  4154. {
  4155. using std::begin;
  4156. using std::end;
  4157. j.m_value.destroy(j.m_type);
  4158. j.m_type = value_t::array;
  4159. j.m_value.array = j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
  4160. j.set_parents();
  4161. j.assert_invariant();
  4162. }
  4163. template<typename BasicJsonType>
  4164. static void construct(BasicJsonType& j, const std::vector<bool>& arr)
  4165. {
  4166. j.m_value.destroy(j.m_type);
  4167. j.m_type = value_t::array;
  4168. j.m_value = value_t::array;
  4169. j.m_value.array->reserve(arr.size());
  4170. for (const bool x : arr)
  4171. {
  4172. j.m_value.array->push_back(x);
  4173. j.set_parent(j.m_value.array->back());
  4174. }
  4175. j.assert_invariant();
  4176. }
  4177. template<typename BasicJsonType, typename T,
  4178. enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
  4179. static void construct(BasicJsonType& j, const std::valarray<T>& arr)
  4180. {
  4181. j.m_value.destroy(j.m_type);
  4182. j.m_type = value_t::array;
  4183. j.m_value = value_t::array;
  4184. j.m_value.array->resize(arr.size());
  4185. if (arr.size() > 0)
  4186. {
  4187. std::copy(std::begin(arr), std::end(arr), j.m_value.array->begin());
  4188. }
  4189. j.set_parents();
  4190. j.assert_invariant();
  4191. }
  4192. };
  4193. template<>
  4194. struct external_constructor<value_t::object>
  4195. {
  4196. template<typename BasicJsonType>
  4197. static void construct(BasicJsonType& j, const typename BasicJsonType::object_t& obj)
  4198. {
  4199. j.m_value.destroy(j.m_type);
  4200. j.m_type = value_t::object;
  4201. j.m_value = obj;
  4202. j.set_parents();
  4203. j.assert_invariant();
  4204. }
  4205. template<typename BasicJsonType>
  4206. static void construct(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
  4207. {
  4208. j.m_value.destroy(j.m_type);
  4209. j.m_type = value_t::object;
  4210. j.m_value = std::move(obj);
  4211. j.set_parents();
  4212. j.assert_invariant();
  4213. }
  4214. template < typename BasicJsonType, typename CompatibleObjectType,
  4215. enable_if_t < !std::is_same<CompatibleObjectType, typename BasicJsonType::object_t>::value, int > = 0 >
  4216. static void construct(BasicJsonType& j, const CompatibleObjectType& obj)
  4217. {
  4218. using std::begin;
  4219. using std::end;
  4220. j.m_value.destroy(j.m_type);
  4221. j.m_type = value_t::object;
  4222. j.m_value.object = j.template create<typename BasicJsonType::object_t>(begin(obj), end(obj));
  4223. j.set_parents();
  4224. j.assert_invariant();
  4225. }
  4226. };
  4227. /////////////
  4228. // to_json //
  4229. /////////////
  4230. template<typename BasicJsonType, typename T,
  4231. enable_if_t<std::is_same<T, typename BasicJsonType::boolean_t>::value, int> = 0>
  4232. void to_json(BasicJsonType& j, T b) noexcept
  4233. {
  4234. external_constructor<value_t::boolean>::construct(j, b);
  4235. }
  4236. template<typename BasicJsonType, typename CompatibleString,
  4237. enable_if_t<std::is_constructible<typename BasicJsonType::string_t, CompatibleString>::value, int> = 0>
  4238. void to_json(BasicJsonType& j, const CompatibleString& s)
  4239. {
  4240. external_constructor<value_t::string>::construct(j, s);
  4241. }
  4242. template<typename BasicJsonType>
  4243. void to_json(BasicJsonType& j, typename BasicJsonType::string_t&& s)
  4244. {
  4245. external_constructor<value_t::string>::construct(j, std::move(s));
  4246. }
  4247. template<typename BasicJsonType, typename FloatType,
  4248. enable_if_t<std::is_floating_point<FloatType>::value, int> = 0>
  4249. void to_json(BasicJsonType& j, FloatType val) noexcept
  4250. {
  4251. external_constructor<value_t::number_float>::construct(j, static_cast<typename BasicJsonType::number_float_t>(val));
  4252. }
  4253. template<typename BasicJsonType, typename CompatibleNumberUnsignedType,
  4254. enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_unsigned_t, CompatibleNumberUnsignedType>::value, int> = 0>
  4255. void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val) noexcept
  4256. {
  4257. external_constructor<value_t::number_unsigned>::construct(j, static_cast<typename BasicJsonType::number_unsigned_t>(val));
  4258. }
  4259. template<typename BasicJsonType, typename CompatibleNumberIntegerType,
  4260. enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_integer_t, CompatibleNumberIntegerType>::value, int> = 0>
  4261. void to_json(BasicJsonType& j, CompatibleNumberIntegerType val) noexcept
  4262. {
  4263. external_constructor<value_t::number_integer>::construct(j, static_cast<typename BasicJsonType::number_integer_t>(val));
  4264. }
  4265. template<typename BasicJsonType, typename EnumType,
  4266. enable_if_t<std::is_enum<EnumType>::value, int> = 0>
  4267. void to_json(BasicJsonType& j, EnumType e) noexcept
  4268. {
  4269. using underlying_type = typename std::underlying_type<EnumType>::type;
  4270. external_constructor<value_t::number_integer>::construct(j, static_cast<underlying_type>(e));
  4271. }
  4272. template<typename BasicJsonType>
  4273. void to_json(BasicJsonType& j, const std::vector<bool>& e)
  4274. {
  4275. external_constructor<value_t::array>::construct(j, e);
  4276. }
  4277. template < typename BasicJsonType, typename CompatibleArrayType,
  4278. enable_if_t < is_compatible_array_type<BasicJsonType,
  4279. CompatibleArrayType>::value&&
  4280. !is_compatible_object_type<BasicJsonType, CompatibleArrayType>::value&&
  4281. !is_compatible_string_type<BasicJsonType, CompatibleArrayType>::value&&
  4282. !std::is_same<typename BasicJsonType::binary_t, CompatibleArrayType>::value&&
  4283. !is_basic_json<CompatibleArrayType>::value,
  4284. int > = 0 >
  4285. void to_json(BasicJsonType& j, const CompatibleArrayType& arr)
  4286. {
  4287. external_constructor<value_t::array>::construct(j, arr);
  4288. }
  4289. template<typename BasicJsonType>
  4290. void to_json(BasicJsonType& j, const typename BasicJsonType::binary_t& bin)
  4291. {
  4292. external_constructor<value_t::binary>::construct(j, bin);
  4293. }
  4294. template<typename BasicJsonType, typename T,
  4295. enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
  4296. void to_json(BasicJsonType& j, const std::valarray<T>& arr)
  4297. {
  4298. external_constructor<value_t::array>::construct(j, std::move(arr));
  4299. }
  4300. template<typename BasicJsonType>
  4301. void to_json(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
  4302. {
  4303. external_constructor<value_t::array>::construct(j, std::move(arr));
  4304. }
  4305. template < typename BasicJsonType, typename CompatibleObjectType,
  4306. enable_if_t < is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value&& !is_basic_json<CompatibleObjectType>::value, int > = 0 >
  4307. void to_json(BasicJsonType& j, const CompatibleObjectType& obj)
  4308. {
  4309. external_constructor<value_t::object>::construct(j, obj);
  4310. }
  4311. template<typename BasicJsonType>
  4312. void to_json(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
  4313. {
  4314. external_constructor<value_t::object>::construct(j, std::move(obj));
  4315. }
  4316. template <
  4317. typename BasicJsonType, typename T, std::size_t N,
  4318. enable_if_t < !std::is_constructible<typename BasicJsonType::string_t,
  4319. const T(&)[N]>::value, // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  4320. int > = 0 >
  4321. void to_json(BasicJsonType& j, const T(&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  4322. {
  4323. external_constructor<value_t::array>::construct(j, arr);
  4324. }
  4325. template < typename BasicJsonType, typename T1, typename T2, enable_if_t < std::is_constructible<BasicJsonType, T1>::value&& std::is_constructible<BasicJsonType, T2>::value, int > = 0 >
  4326. void to_json(BasicJsonType& j, const std::pair<T1, T2>& p)
  4327. {
  4328. j = { p.first, p.second };
  4329. }
  4330. // for https://github.com/nlohmann/json/pull/1134
  4331. template<typename BasicJsonType, typename T,
  4332. enable_if_t<std::is_same<T, iteration_proxy_value<typename BasicJsonType::iterator>>::value, int> = 0>
  4333. void to_json(BasicJsonType& j, const T& b)
  4334. {
  4335. j = { {b.key(), b.value()} };
  4336. }
  4337. template<typename BasicJsonType, typename Tuple, std::size_t... Idx>
  4338. void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence<Idx...> /*unused*/)
  4339. {
  4340. j = { std::get<Idx>(t)... };
  4341. }
  4342. template<typename BasicJsonType, typename T, enable_if_t<is_constructible_tuple<BasicJsonType, T>::value, int > = 0>
  4343. void to_json(BasicJsonType& j, const T& t)
  4344. {
  4345. to_json_tuple_impl(j, t, make_index_sequence<std::tuple_size<T>::value> {});
  4346. }
  4347. struct to_json_fn
  4348. {
  4349. template<typename BasicJsonType, typename T>
  4350. auto operator()(BasicJsonType& j, T&& val) const noexcept(noexcept(to_json(j, std::forward<T>(val))))
  4351. -> decltype(to_json(j, std::forward<T>(val)), void())
  4352. {
  4353. return to_json(j, std::forward<T>(val));
  4354. }
  4355. };
  4356. } // namespace detail
  4357. /// namespace to hold default `to_json` function
  4358. /// to see why this is required:
  4359. /// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
  4360. namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces)
  4361. {
  4362. constexpr const auto& to_json = detail::static_const<detail::to_json_fn>::value; // NOLINT(misc-definitions-in-headers)
  4363. } // namespace
  4364. } // namespace nlohmann
  4365. // #include <nlohmann/detail/meta/identity_tag.hpp>
  4366. // #include <nlohmann/detail/meta/type_traits.hpp>
  4367. namespace nlohmann
  4368. {
  4369. template<typename ValueType, typename>
  4370. struct adl_serializer
  4371. {
  4372. /*!
  4373. @brief convert a JSON value to any value type
  4374. This function is usually called by the `get()` function of the
  4375. @ref basic_json class (either explicit or via conversion operators).
  4376. @note This function is chosen for default-constructible value types.
  4377. @param[in] j JSON value to read from
  4378. @param[in,out] val value to write to
  4379. */
  4380. template<typename BasicJsonType, typename TargetType = ValueType>
  4381. static auto from_json(BasicJsonType && j, TargetType& val) noexcept(
  4382. noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), val)))
  4383. -> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), val), void())
  4384. {
  4385. ::nlohmann::from_json(std::forward<BasicJsonType>(j), val);
  4386. }
  4387. /*!
  4388. @brief convert a JSON value to any value type
  4389. This function is usually called by the `get()` function of the
  4390. @ref basic_json class (either explicit or via conversion operators).
  4391. @note This function is chosen for value types which are not default-constructible.
  4392. @param[in] j JSON value to read from
  4393. @return copy of the JSON value, converted to @a ValueType
  4394. */
  4395. template<typename BasicJsonType, typename TargetType = ValueType>
  4396. static auto from_json(BasicJsonType && j) noexcept(
  4397. noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {})))
  4398. -> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {}))
  4399. {
  4400. return ::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {});
  4401. }
  4402. /*!
  4403. @brief convert any value type to a JSON value
  4404. This function is usually called by the constructors of the @ref basic_json
  4405. class.
  4406. @param[in,out] j JSON value to write to
  4407. @param[in] val value to read from
  4408. */
  4409. template<typename BasicJsonType, typename TargetType = ValueType>
  4410. static auto to_json(BasicJsonType& j, TargetType && val) noexcept(
  4411. noexcept(::nlohmann::to_json(j, std::forward<TargetType>(val))))
  4412. -> decltype(::nlohmann::to_json(j, std::forward<TargetType>(val)), void())
  4413. {
  4414. ::nlohmann::to_json(j, std::forward<TargetType>(val));
  4415. }
  4416. };
  4417. } // namespace nlohmann
  4418. // #include <nlohmann/byte_container_with_subtype.hpp>
  4419. #include <cstdint> // uint8_t, uint64_t
  4420. #include <tuple> // tie
  4421. #include <utility> // move
  4422. namespace nlohmann
  4423. {
  4424. /*!
  4425. @brief an internal type for a backed binary type
  4426. This type extends the template parameter @a BinaryType provided to `basic_json`
  4427. with a subtype used by BSON and MessagePack. This type exists so that the user
  4428. does not have to specify a type themselves with a specific naming scheme in
  4429. order to override the binary type.
  4430. @tparam BinaryType container to store bytes (`std::vector<std::uint8_t>` by
  4431. default)
  4432. @since version 3.8.0; changed type of subtypes to std::uint64_t in 3.10.0.
  4433. */
  4434. template<typename BinaryType>
  4435. class byte_container_with_subtype : public BinaryType
  4436. {
  4437. public:
  4438. /// the type of the underlying container
  4439. using container_type = BinaryType;
  4440. /// the type of the subtype
  4441. using subtype_type = std::uint64_t;
  4442. byte_container_with_subtype() noexcept(noexcept(container_type()))
  4443. : container_type()
  4444. {}
  4445. byte_container_with_subtype(const container_type& b) noexcept(noexcept(container_type(b)))
  4446. : container_type(b)
  4447. {}
  4448. byte_container_with_subtype(container_type&& b) noexcept(noexcept(container_type(std::move(b))))
  4449. : container_type(std::move(b))
  4450. {}
  4451. byte_container_with_subtype(const container_type& b, subtype_type subtype_) noexcept(noexcept(container_type(b)))
  4452. : container_type(b)
  4453. , m_subtype(subtype_)
  4454. , m_has_subtype(true)
  4455. {}
  4456. byte_container_with_subtype(container_type&& b, subtype_type subtype_) noexcept(noexcept(container_type(std::move(b))))
  4457. : container_type(std::move(b))
  4458. , m_subtype(subtype_)
  4459. , m_has_subtype(true)
  4460. {}
  4461. bool operator==(const byte_container_with_subtype& rhs) const
  4462. {
  4463. return std::tie(static_cast<const BinaryType&>(*this), m_subtype, m_has_subtype) ==
  4464. std::tie(static_cast<const BinaryType&>(rhs), rhs.m_subtype, rhs.m_has_subtype);
  4465. }
  4466. bool operator!=(const byte_container_with_subtype& rhs) const
  4467. {
  4468. return !(rhs == *this);
  4469. }
  4470. /*!
  4471. @brief sets the binary subtype
  4472. Sets the binary subtype of the value, also flags a binary JSON value as
  4473. having a subtype, which has implications for serialization.
  4474. @complexity Constant.
  4475. @exceptionsafety No-throw guarantee: this member function never throws
  4476. exceptions.
  4477. @sa see @ref subtype() -- return the binary subtype
  4478. @sa see @ref clear_subtype() -- clears the binary subtype
  4479. @sa see @ref has_subtype() -- returns whether or not the binary value has a
  4480. subtype
  4481. @since version 3.8.0
  4482. */
  4483. void set_subtype(subtype_type subtype_) noexcept
  4484. {
  4485. m_subtype = subtype_;
  4486. m_has_subtype = true;
  4487. }
  4488. /*!
  4489. @brief return the binary subtype
  4490. Returns the numerical subtype of the value if it has a subtype. If it does
  4491. not have a subtype, this function will return subtype_type(-1) as a sentinel
  4492. value.
  4493. @return the numerical subtype of the binary value
  4494. @complexity Constant.
  4495. @exceptionsafety No-throw guarantee: this member function never throws
  4496. exceptions.
  4497. @sa see @ref set_subtype() -- sets the binary subtype
  4498. @sa see @ref clear_subtype() -- clears the binary subtype
  4499. @sa see @ref has_subtype() -- returns whether or not the binary value has a
  4500. subtype
  4501. @since version 3.8.0; fixed return value to properly return
  4502. subtype_type(-1) as documented in version 3.10.0
  4503. */
  4504. constexpr subtype_type subtype() const noexcept
  4505. {
  4506. return m_has_subtype ? m_subtype : subtype_type(-1);
  4507. }
  4508. /*!
  4509. @brief return whether the value has a subtype
  4510. @return whether the value has a subtype
  4511. @complexity Constant.
  4512. @exceptionsafety No-throw guarantee: this member function never throws
  4513. exceptions.
  4514. @sa see @ref subtype() -- return the binary subtype
  4515. @sa see @ref set_subtype() -- sets the binary subtype
  4516. @sa see @ref clear_subtype() -- clears the binary subtype
  4517. @since version 3.8.0
  4518. */
  4519. constexpr bool has_subtype() const noexcept
  4520. {
  4521. return m_has_subtype;
  4522. }
  4523. /*!
  4524. @brief clears the binary subtype
  4525. Clears the binary subtype and flags the value as not having a subtype, which
  4526. has implications for serialization; for instance MessagePack will prefer the
  4527. bin family over the ext family.
  4528. @complexity Constant.
  4529. @exceptionsafety No-throw guarantee: this member function never throws
  4530. exceptions.
  4531. @sa see @ref subtype() -- return the binary subtype
  4532. @sa see @ref set_subtype() -- sets the binary subtype
  4533. @sa see @ref has_subtype() -- returns whether or not the binary value has a
  4534. subtype
  4535. @since version 3.8.0
  4536. */
  4537. void clear_subtype() noexcept
  4538. {
  4539. m_subtype = 0;
  4540. m_has_subtype = false;
  4541. }
  4542. private:
  4543. subtype_type m_subtype = 0;
  4544. bool m_has_subtype = false;
  4545. };
  4546. } // namespace nlohmann
  4547. // #include <nlohmann/detail/conversions/from_json.hpp>
  4548. // #include <nlohmann/detail/conversions/to_json.hpp>
  4549. // #include <nlohmann/detail/exceptions.hpp>
  4550. // #include <nlohmann/detail/hash.hpp>
  4551. #include <cstdint> // uint8_t
  4552. #include <cstddef> // size_t
  4553. #include <functional> // hash
  4554. // #include <nlohmann/detail/macro_scope.hpp>
  4555. // #include <nlohmann/detail/value_t.hpp>
  4556. namespace nlohmann
  4557. {
  4558. namespace detail
  4559. {
  4560. // boost::hash_combine
  4561. inline std::size_t combine(std::size_t seed, std::size_t h) noexcept
  4562. {
  4563. seed ^= h + 0x9e3779b9 + (seed << 6U) + (seed >> 2U);
  4564. return seed;
  4565. }
  4566. /*!
  4567. @brief hash a JSON value
  4568. The hash function tries to rely on std::hash where possible. Furthermore, the
  4569. type of the JSON value is taken into account to have different hash values for
  4570. null, 0, 0U, and false, etc.
  4571. @tparam BasicJsonType basic_json specialization
  4572. @param j JSON value to hash
  4573. @return hash value of j
  4574. */
  4575. template<typename BasicJsonType>
  4576. std::size_t hash(const BasicJsonType& j)
  4577. {
  4578. using string_t = typename BasicJsonType::string_t;
  4579. using number_integer_t = typename BasicJsonType::number_integer_t;
  4580. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  4581. using number_float_t = typename BasicJsonType::number_float_t;
  4582. const auto type = static_cast<std::size_t>(j.type());
  4583. switch (j.type())
  4584. {
  4585. case BasicJsonType::value_t::null:
  4586. case BasicJsonType::value_t::discarded:
  4587. {
  4588. return combine(type, 0);
  4589. }
  4590. case BasicJsonType::value_t::object:
  4591. {
  4592. auto seed = combine(type, j.size());
  4593. for (const auto& element : j.items())
  4594. {
  4595. const auto h = std::hash<string_t> {}(element.key());
  4596. seed = combine(seed, h);
  4597. seed = combine(seed, hash(element.value()));
  4598. }
  4599. return seed;
  4600. }
  4601. case BasicJsonType::value_t::array:
  4602. {
  4603. auto seed = combine(type, j.size());
  4604. for (const auto& element : j)
  4605. {
  4606. seed = combine(seed, hash(element));
  4607. }
  4608. return seed;
  4609. }
  4610. case BasicJsonType::value_t::string:
  4611. {
  4612. const auto h = std::hash<string_t> {}(j.template get_ref<const string_t&>());
  4613. return combine(type, h);
  4614. }
  4615. case BasicJsonType::value_t::boolean:
  4616. {
  4617. const auto h = std::hash<bool> {}(j.template get<bool>());
  4618. return combine(type, h);
  4619. }
  4620. case BasicJsonType::value_t::number_integer:
  4621. {
  4622. const auto h = std::hash<number_integer_t> {}(j.template get<number_integer_t>());
  4623. return combine(type, h);
  4624. }
  4625. case BasicJsonType::value_t::number_unsigned:
  4626. {
  4627. const auto h = std::hash<number_unsigned_t> {}(j.template get<number_unsigned_t>());
  4628. return combine(type, h);
  4629. }
  4630. case BasicJsonType::value_t::number_float:
  4631. {
  4632. const auto h = std::hash<number_float_t> {}(j.template get<number_float_t>());
  4633. return combine(type, h);
  4634. }
  4635. case BasicJsonType::value_t::binary:
  4636. {
  4637. auto seed = combine(type, j.get_binary().size());
  4638. const auto h = std::hash<bool> {}(j.get_binary().has_subtype());
  4639. seed = combine(seed, h);
  4640. seed = combine(seed, static_cast<std::size_t>(j.get_binary().subtype()));
  4641. for (const auto byte : j.get_binary())
  4642. {
  4643. seed = combine(seed, std::hash<std::uint8_t> {}(byte));
  4644. }
  4645. return seed;
  4646. }
  4647. default: // LCOV_EXCL_LINE
  4648. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  4649. return 0; // LCOV_EXCL_LINE
  4650. }
  4651. }
  4652. } // namespace detail
  4653. } // namespace nlohmann
  4654. // #include <nlohmann/detail/input/binary_reader.hpp>
  4655. #include <algorithm> // generate_n
  4656. #include <array> // array
  4657. #include <cmath> // ldexp
  4658. #include <cstddef> // size_t
  4659. #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
  4660. #include <cstdio> // snprintf
  4661. #include <cstring> // memcpy
  4662. #include <iterator> // back_inserter
  4663. #include <limits> // numeric_limits
  4664. #include <string> // char_traits, string
  4665. #include <utility> // make_pair, move
  4666. #include <vector> // vector
  4667. // #include <nlohmann/detail/exceptions.hpp>
  4668. // #include <nlohmann/detail/input/input_adapters.hpp>
  4669. #include <array> // array
  4670. #include <cstddef> // size_t
  4671. #include <cstring> // strlen
  4672. #include <iterator> // begin, end, iterator_traits, random_access_iterator_tag, distance, next
  4673. #include <memory> // shared_ptr, make_shared, addressof
  4674. #include <numeric> // accumulate
  4675. #include <string> // string, char_traits
  4676. #include <type_traits> // enable_if, is_base_of, is_pointer, is_integral, remove_pointer
  4677. #include <utility> // pair, declval
  4678. #ifndef JSON_NO_IO
  4679. #include <cstdio> // FILE *
  4680. #include <istream> // istream
  4681. #endif // JSON_NO_IO
  4682. // #include <nlohmann/detail/iterators/iterator_traits.hpp>
  4683. // #include <nlohmann/detail/macro_scope.hpp>
  4684. namespace nlohmann
  4685. {
  4686. namespace detail
  4687. {
  4688. /// the supported input formats
  4689. enum class input_format_t { json, cbor, msgpack, ubjson, bson };
  4690. ////////////////////
  4691. // input adapters //
  4692. ////////////////////
  4693. #ifndef JSON_NO_IO
  4694. /*!
  4695. Input adapter for stdio file access. This adapter read only 1 byte and do not use any
  4696. buffer. This adapter is a very low level adapter.
  4697. */
  4698. class file_input_adapter
  4699. {
  4700. public:
  4701. using char_type = char;
  4702. JSON_HEDLEY_NON_NULL(2)
  4703. explicit file_input_adapter(std::FILE* f) noexcept
  4704. : m_file(f)
  4705. {}
  4706. // make class move-only
  4707. file_input_adapter(const file_input_adapter&) = delete;
  4708. file_input_adapter(file_input_adapter&&) noexcept = default;
  4709. file_input_adapter& operator=(const file_input_adapter&) = delete;
  4710. file_input_adapter& operator=(file_input_adapter&&) = delete;
  4711. ~file_input_adapter() = default;
  4712. std::char_traits<char>::int_type get_character() noexcept
  4713. {
  4714. return std::fgetc(m_file);
  4715. }
  4716. private:
  4717. /// the file pointer to read from
  4718. std::FILE* m_file;
  4719. };
  4720. /*!
  4721. Input adapter for a (caching) istream. Ignores a UFT Byte Order Mark at
  4722. beginning of input. Does not support changing the underlying std::streambuf
  4723. in mid-input. Maintains underlying std::istream and std::streambuf to support
  4724. subsequent use of standard std::istream operations to process any input
  4725. characters following those used in parsing the JSON input. Clears the
  4726. std::istream flags; any input errors (e.g., EOF) will be detected by the first
  4727. subsequent call for input from the std::istream.
  4728. */
  4729. class input_stream_adapter
  4730. {
  4731. public:
  4732. using char_type = char;
  4733. ~input_stream_adapter()
  4734. {
  4735. // clear stream flags; we use underlying streambuf I/O, do not
  4736. // maintain ifstream flags, except eof
  4737. if (is != nullptr)
  4738. {
  4739. is->clear(is->rdstate() & std::ios::eofbit);
  4740. }
  4741. }
  4742. explicit input_stream_adapter(std::istream& i)
  4743. : is(&i), sb(i.rdbuf())
  4744. {}
  4745. // delete because of pointer members
  4746. input_stream_adapter(const input_stream_adapter&) = delete;
  4747. input_stream_adapter& operator=(input_stream_adapter&) = delete;
  4748. input_stream_adapter& operator=(input_stream_adapter&&) = delete;
  4749. input_stream_adapter(input_stream_adapter&& rhs) noexcept
  4750. : is(rhs.is), sb(rhs.sb)
  4751. {
  4752. rhs.is = nullptr;
  4753. rhs.sb = nullptr;
  4754. }
  4755. // std::istream/std::streambuf use std::char_traits<char>::to_int_type, to
  4756. // ensure that std::char_traits<char>::eof() and the character 0xFF do not
  4757. // end up as the same value, eg. 0xFFFFFFFF.
  4758. std::char_traits<char>::int_type get_character()
  4759. {
  4760. auto res = sb->sbumpc();
  4761. // set eof manually, as we don't use the istream interface.
  4762. if (JSON_HEDLEY_UNLIKELY(res == std::char_traits<char>::eof()))
  4763. {
  4764. is->clear(is->rdstate() | std::ios::eofbit);
  4765. }
  4766. return res;
  4767. }
  4768. private:
  4769. /// the associated input stream
  4770. std::istream* is = nullptr;
  4771. std::streambuf* sb = nullptr;
  4772. };
  4773. #endif // JSON_NO_IO
  4774. // General-purpose iterator-based adapter. It might not be as fast as
  4775. // theoretically possible for some containers, but it is extremely versatile.
  4776. template<typename IteratorType>
  4777. class iterator_input_adapter
  4778. {
  4779. public:
  4780. using char_type = typename std::iterator_traits<IteratorType>::value_type;
  4781. iterator_input_adapter(IteratorType first, IteratorType last)
  4782. : current(std::move(first)), end(std::move(last))
  4783. {}
  4784. typename std::char_traits<char_type>::int_type get_character()
  4785. {
  4786. if (JSON_HEDLEY_LIKELY(current != end))
  4787. {
  4788. auto result = std::char_traits<char_type>::to_int_type(*current);
  4789. std::advance(current, 1);
  4790. return result;
  4791. }
  4792. return std::char_traits<char_type>::eof();
  4793. }
  4794. private:
  4795. IteratorType current;
  4796. IteratorType end;
  4797. template<typename BaseInputAdapter, size_t T>
  4798. friend struct wide_string_input_helper;
  4799. bool empty() const
  4800. {
  4801. return current == end;
  4802. }
  4803. };
  4804. template<typename BaseInputAdapter, size_t T>
  4805. struct wide_string_input_helper;
  4806. template<typename BaseInputAdapter>
  4807. struct wide_string_input_helper<BaseInputAdapter, 4>
  4808. {
  4809. // UTF-32
  4810. static void fill_buffer(BaseInputAdapter& input,
  4811. std::array<std::char_traits<char>::int_type, 4>& utf8_bytes,
  4812. size_t& utf8_bytes_index,
  4813. size_t& utf8_bytes_filled)
  4814. {
  4815. utf8_bytes_index = 0;
  4816. if (JSON_HEDLEY_UNLIKELY(input.empty()))
  4817. {
  4818. utf8_bytes[0] = std::char_traits<char>::eof();
  4819. utf8_bytes_filled = 1;
  4820. }
  4821. else
  4822. {
  4823. // get the current character
  4824. const auto wc = input.get_character();
  4825. // UTF-32 to UTF-8 encoding
  4826. if (wc < 0x80)
  4827. {
  4828. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  4829. utf8_bytes_filled = 1;
  4830. }
  4831. else if (wc <= 0x7FF)
  4832. {
  4833. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xC0u | ((static_cast<unsigned int>(wc) >> 6u) & 0x1Fu));
  4834. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  4835. utf8_bytes_filled = 2;
  4836. }
  4837. else if (wc <= 0xFFFF)
  4838. {
  4839. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xE0u | ((static_cast<unsigned int>(wc) >> 12u) & 0x0Fu));
  4840. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
  4841. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  4842. utf8_bytes_filled = 3;
  4843. }
  4844. else if (wc <= 0x10FFFF)
  4845. {
  4846. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xF0u | ((static_cast<unsigned int>(wc) >> 18u) & 0x07u));
  4847. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 12u) & 0x3Fu));
  4848. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
  4849. utf8_bytes[3] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  4850. utf8_bytes_filled = 4;
  4851. }
  4852. else
  4853. {
  4854. // unknown character
  4855. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  4856. utf8_bytes_filled = 1;
  4857. }
  4858. }
  4859. }
  4860. };
  4861. template<typename BaseInputAdapter>
  4862. struct wide_string_input_helper<BaseInputAdapter, 2>
  4863. {
  4864. // UTF-16
  4865. static void fill_buffer(BaseInputAdapter& input,
  4866. std::array<std::char_traits<char>::int_type, 4>& utf8_bytes,
  4867. size_t& utf8_bytes_index,
  4868. size_t& utf8_bytes_filled)
  4869. {
  4870. utf8_bytes_index = 0;
  4871. if (JSON_HEDLEY_UNLIKELY(input.empty()))
  4872. {
  4873. utf8_bytes[0] = std::char_traits<char>::eof();
  4874. utf8_bytes_filled = 1;
  4875. }
  4876. else
  4877. {
  4878. // get the current character
  4879. const auto wc = input.get_character();
  4880. // UTF-16 to UTF-8 encoding
  4881. if (wc < 0x80)
  4882. {
  4883. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  4884. utf8_bytes_filled = 1;
  4885. }
  4886. else if (wc <= 0x7FF)
  4887. {
  4888. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xC0u | ((static_cast<unsigned int>(wc) >> 6u)));
  4889. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  4890. utf8_bytes_filled = 2;
  4891. }
  4892. else if (0xD800 > wc || wc >= 0xE000)
  4893. {
  4894. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xE0u | ((static_cast<unsigned int>(wc) >> 12u)));
  4895. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
  4896. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  4897. utf8_bytes_filled = 3;
  4898. }
  4899. else
  4900. {
  4901. if (JSON_HEDLEY_UNLIKELY(!input.empty()))
  4902. {
  4903. const auto wc2 = static_cast<unsigned int>(input.get_character());
  4904. const auto charcode = 0x10000u + (((static_cast<unsigned int>(wc) & 0x3FFu) << 10u) | (wc2 & 0x3FFu));
  4905. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xF0u | (charcode >> 18u));
  4906. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((charcode >> 12u) & 0x3Fu));
  4907. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | ((charcode >> 6u) & 0x3Fu));
  4908. utf8_bytes[3] = static_cast<std::char_traits<char>::int_type>(0x80u | (charcode & 0x3Fu));
  4909. utf8_bytes_filled = 4;
  4910. }
  4911. else
  4912. {
  4913. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  4914. utf8_bytes_filled = 1;
  4915. }
  4916. }
  4917. }
  4918. }
  4919. };
  4920. // Wraps another input apdater to convert wide character types into individual bytes.
  4921. template<typename BaseInputAdapter, typename WideCharType>
  4922. class wide_string_input_adapter
  4923. {
  4924. public:
  4925. using char_type = char;
  4926. wide_string_input_adapter(BaseInputAdapter base)
  4927. : base_adapter(base) {}
  4928. typename std::char_traits<char>::int_type get_character() noexcept
  4929. {
  4930. // check if buffer needs to be filled
  4931. if (utf8_bytes_index == utf8_bytes_filled)
  4932. {
  4933. fill_buffer<sizeof(WideCharType)>();
  4934. JSON_ASSERT(utf8_bytes_filled > 0);
  4935. JSON_ASSERT(utf8_bytes_index == 0);
  4936. }
  4937. // use buffer
  4938. JSON_ASSERT(utf8_bytes_filled > 0);
  4939. JSON_ASSERT(utf8_bytes_index < utf8_bytes_filled);
  4940. return utf8_bytes[utf8_bytes_index++];
  4941. }
  4942. private:
  4943. BaseInputAdapter base_adapter;
  4944. template<size_t T>
  4945. void fill_buffer()
  4946. {
  4947. wide_string_input_helper<BaseInputAdapter, T>::fill_buffer(base_adapter, utf8_bytes, utf8_bytes_index, utf8_bytes_filled);
  4948. }
  4949. /// a buffer for UTF-8 bytes
  4950. std::array<std::char_traits<char>::int_type, 4> utf8_bytes = {{0, 0, 0, 0}};
  4951. /// index to the utf8_codes array for the next valid byte
  4952. std::size_t utf8_bytes_index = 0;
  4953. /// number of valid bytes in the utf8_codes array
  4954. std::size_t utf8_bytes_filled = 0;
  4955. };
  4956. template<typename IteratorType, typename Enable = void>
  4957. struct iterator_input_adapter_factory
  4958. {
  4959. using iterator_type = IteratorType;
  4960. using char_type = typename std::iterator_traits<iterator_type>::value_type;
  4961. using adapter_type = iterator_input_adapter<iterator_type>;
  4962. static adapter_type create(IteratorType first, IteratorType last)
  4963. {
  4964. return adapter_type(std::move(first), std::move(last));
  4965. }
  4966. };
  4967. template<typename T>
  4968. struct is_iterator_of_multibyte
  4969. {
  4970. using value_type = typename std::iterator_traits<T>::value_type;
  4971. enum
  4972. {
  4973. value = sizeof(value_type) > 1
  4974. };
  4975. };
  4976. template<typename IteratorType>
  4977. struct iterator_input_adapter_factory<IteratorType, enable_if_t<is_iterator_of_multibyte<IteratorType>::value>>
  4978. {
  4979. using iterator_type = IteratorType;
  4980. using char_type = typename std::iterator_traits<iterator_type>::value_type;
  4981. using base_adapter_type = iterator_input_adapter<iterator_type>;
  4982. using adapter_type = wide_string_input_adapter<base_adapter_type, char_type>;
  4983. static adapter_type create(IteratorType first, IteratorType last)
  4984. {
  4985. return adapter_type(base_adapter_type(std::move(first), std::move(last)));
  4986. }
  4987. };
  4988. // General purpose iterator-based input
  4989. template<typename IteratorType>
  4990. typename iterator_input_adapter_factory<IteratorType>::adapter_type input_adapter(IteratorType first, IteratorType last)
  4991. {
  4992. using factory_type = iterator_input_adapter_factory<IteratorType>;
  4993. return factory_type::create(first, last);
  4994. }
  4995. // Convenience shorthand from container to iterator
  4996. // Enables ADL on begin(container) and end(container)
  4997. // Encloses the using declarations in namespace for not to leak them to outside scope
  4998. namespace container_input_adapter_factory_impl
  4999. {
  5000. using std::begin;
  5001. using std::end;
  5002. template<typename ContainerType, typename Enable = void>
  5003. struct container_input_adapter_factory {};
  5004. template<typename ContainerType>
  5005. struct container_input_adapter_factory< ContainerType,
  5006. void_t<decltype(begin(std::declval<ContainerType>()), end(std::declval<ContainerType>()))>>
  5007. {
  5008. using adapter_type = decltype(input_adapter(begin(std::declval<ContainerType>()), end(std::declval<ContainerType>())));
  5009. static adapter_type create(const ContainerType& container)
  5010. {
  5011. return input_adapter(begin(container), end(container));
  5012. }
  5013. };
  5014. } // namespace container_input_adapter_factory_impl
  5015. template<typename ContainerType>
  5016. typename container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::adapter_type input_adapter(const ContainerType& container)
  5017. {
  5018. return container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::create(container);
  5019. }
  5020. #ifndef JSON_NO_IO
  5021. // Special cases with fast paths
  5022. inline file_input_adapter input_adapter(std::FILE* file)
  5023. {
  5024. return file_input_adapter(file);
  5025. }
  5026. inline input_stream_adapter input_adapter(std::istream& stream)
  5027. {
  5028. return input_stream_adapter(stream);
  5029. }
  5030. inline input_stream_adapter input_adapter(std::istream&& stream)
  5031. {
  5032. return input_stream_adapter(stream);
  5033. }
  5034. #endif // JSON_NO_IO
  5035. using contiguous_bytes_input_adapter = decltype(input_adapter(std::declval<const char*>(), std::declval<const char*>()));
  5036. // Null-delimited strings, and the like.
  5037. template < typename CharT,
  5038. typename std::enable_if <
  5039. std::is_pointer<CharT>::value&&
  5040. !std::is_array<CharT>::value&&
  5041. std::is_integral<typename std::remove_pointer<CharT>::type>::value&&
  5042. sizeof(typename std::remove_pointer<CharT>::type) == 1,
  5043. int >::type = 0 >
  5044. contiguous_bytes_input_adapter input_adapter(CharT b)
  5045. {
  5046. auto length = std::strlen(reinterpret_cast<const char*>(b));
  5047. const auto* ptr = reinterpret_cast<const char*>(b);
  5048. return input_adapter(ptr, ptr + length);
  5049. }
  5050. template<typename T, std::size_t N>
  5051. 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)
  5052. {
  5053. return input_adapter(array, array + N);
  5054. }
  5055. // This class only handles inputs of input_buffer_adapter type.
  5056. // It's required so that expressions like {ptr, len} can be implicitely casted
  5057. // to the correct adapter.
  5058. class span_input_adapter
  5059. {
  5060. public:
  5061. template < typename CharT,
  5062. typename std::enable_if <
  5063. std::is_pointer<CharT>::value&&
  5064. std::is_integral<typename std::remove_pointer<CharT>::type>::value&&
  5065. sizeof(typename std::remove_pointer<CharT>::type) == 1,
  5066. int >::type = 0 >
  5067. span_input_adapter(CharT b, std::size_t l)
  5068. : ia(reinterpret_cast<const char*>(b), reinterpret_cast<const char*>(b) + l) {}
  5069. template<class IteratorType,
  5070. typename std::enable_if<
  5071. std::is_same<typename iterator_traits<IteratorType>::iterator_category, std::random_access_iterator_tag>::value,
  5072. int>::type = 0>
  5073. span_input_adapter(IteratorType first, IteratorType last)
  5074. : ia(input_adapter(first, last)) {}
  5075. contiguous_bytes_input_adapter&& get()
  5076. {
  5077. return std::move(ia); // NOLINT(hicpp-move-const-arg,performance-move-const-arg)
  5078. }
  5079. private:
  5080. contiguous_bytes_input_adapter ia;
  5081. };
  5082. } // namespace detail
  5083. } // namespace nlohmann
  5084. // #include <nlohmann/detail/input/json_sax.hpp>
  5085. #include <cstddef>
  5086. #include <string> // string
  5087. #include <utility> // move
  5088. #include <vector> // vector
  5089. // #include <nlohmann/detail/exceptions.hpp>
  5090. // #include <nlohmann/detail/macro_scope.hpp>
  5091. namespace nlohmann
  5092. {
  5093. /*!
  5094. @brief SAX interface
  5095. This class describes the SAX interface used by @ref nlohmann::json::sax_parse.
  5096. Each function is called in different situations while the input is parsed. The
  5097. boolean return value informs the parser whether to continue processing the
  5098. input.
  5099. */
  5100. template<typename BasicJsonType>
  5101. struct json_sax
  5102. {
  5103. using number_integer_t = typename BasicJsonType::number_integer_t;
  5104. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  5105. using number_float_t = typename BasicJsonType::number_float_t;
  5106. using string_t = typename BasicJsonType::string_t;
  5107. using binary_t = typename BasicJsonType::binary_t;
  5108. /*!
  5109. @brief a null value was read
  5110. @return whether parsing should proceed
  5111. */
  5112. virtual bool null() = 0;
  5113. /*!
  5114. @brief a boolean value was read
  5115. @param[in] val boolean value
  5116. @return whether parsing should proceed
  5117. */
  5118. virtual bool boolean(bool val) = 0;
  5119. /*!
  5120. @brief an integer number was read
  5121. @param[in] val integer value
  5122. @return whether parsing should proceed
  5123. */
  5124. virtual bool number_integer(number_integer_t val) = 0;
  5125. /*!
  5126. @brief an unsigned integer number was read
  5127. @param[in] val unsigned integer value
  5128. @return whether parsing should proceed
  5129. */
  5130. virtual bool number_unsigned(number_unsigned_t val) = 0;
  5131. /*!
  5132. @brief an floating-point number was read
  5133. @param[in] val floating-point value
  5134. @param[in] s raw token value
  5135. @return whether parsing should proceed
  5136. */
  5137. virtual bool number_float(number_float_t val, const string_t& s) = 0;
  5138. /*!
  5139. @brief a string was read
  5140. @param[in] val string value
  5141. @return whether parsing should proceed
  5142. @note It is safe to move the passed string.
  5143. */
  5144. virtual bool string(string_t& val) = 0;
  5145. /*!
  5146. @brief a binary string was read
  5147. @param[in] val binary value
  5148. @return whether parsing should proceed
  5149. @note It is safe to move the passed binary.
  5150. */
  5151. virtual bool binary(binary_t& val) = 0;
  5152. /*!
  5153. @brief the beginning of an object was read
  5154. @param[in] elements number of object elements or -1 if unknown
  5155. @return whether parsing should proceed
  5156. @note binary formats may report the number of elements
  5157. */
  5158. virtual bool start_object(std::size_t elements) = 0;
  5159. /*!
  5160. @brief an object key was read
  5161. @param[in] val object key
  5162. @return whether parsing should proceed
  5163. @note It is safe to move the passed string.
  5164. */
  5165. virtual bool key(string_t& val) = 0;
  5166. /*!
  5167. @brief the end of an object was read
  5168. @return whether parsing should proceed
  5169. */
  5170. virtual bool end_object() = 0;
  5171. /*!
  5172. @brief the beginning of an array was read
  5173. @param[in] elements number of array elements or -1 if unknown
  5174. @return whether parsing should proceed
  5175. @note binary formats may report the number of elements
  5176. */
  5177. virtual bool start_array(std::size_t elements) = 0;
  5178. /*!
  5179. @brief the end of an array was read
  5180. @return whether parsing should proceed
  5181. */
  5182. virtual bool end_array() = 0;
  5183. /*!
  5184. @brief a parse error occurred
  5185. @param[in] position the position in the input where the error occurs
  5186. @param[in] last_token the last read token
  5187. @param[in] ex an exception object describing the error
  5188. @return whether parsing should proceed (must return false)
  5189. */
  5190. virtual bool parse_error(std::size_t position,
  5191. const std::string& last_token,
  5192. const detail::exception& ex) = 0;
  5193. json_sax() = default;
  5194. json_sax(const json_sax&) = default;
  5195. json_sax(json_sax&&) noexcept = default;
  5196. json_sax& operator=(const json_sax&) = default;
  5197. json_sax& operator=(json_sax&&) noexcept = default;
  5198. virtual ~json_sax() = default;
  5199. };
  5200. namespace detail
  5201. {
  5202. /*!
  5203. @brief SAX implementation to create a JSON value from SAX events
  5204. This class implements the @ref json_sax interface and processes the SAX events
  5205. to create a JSON value which makes it basically a DOM parser. The structure or
  5206. hierarchy of the JSON value is managed by the stack `ref_stack` which contains
  5207. a pointer to the respective array or object for each recursion depth.
  5208. After successful parsing, the value that is passed by reference to the
  5209. constructor contains the parsed value.
  5210. @tparam BasicJsonType the JSON type
  5211. */
  5212. template<typename BasicJsonType>
  5213. class json_sax_dom_parser
  5214. {
  5215. public:
  5216. using number_integer_t = typename BasicJsonType::number_integer_t;
  5217. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  5218. using number_float_t = typename BasicJsonType::number_float_t;
  5219. using string_t = typename BasicJsonType::string_t;
  5220. using binary_t = typename BasicJsonType::binary_t;
  5221. /*!
  5222. @param[in,out] r reference to a JSON value that is manipulated while
  5223. parsing
  5224. @param[in] allow_exceptions_ whether parse errors yield exceptions
  5225. */
  5226. explicit json_sax_dom_parser(BasicJsonType& r, const bool allow_exceptions_ = true)
  5227. : root(r), allow_exceptions(allow_exceptions_)
  5228. {}
  5229. // make class move-only
  5230. json_sax_dom_parser(const json_sax_dom_parser&) = delete;
  5231. json_sax_dom_parser(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  5232. json_sax_dom_parser& operator=(const json_sax_dom_parser&) = delete;
  5233. json_sax_dom_parser& operator=(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  5234. ~json_sax_dom_parser() = default;
  5235. bool null()
  5236. {
  5237. handle_value(nullptr);
  5238. return true;
  5239. }
  5240. bool boolean(bool val)
  5241. {
  5242. handle_value(val);
  5243. return true;
  5244. }
  5245. bool number_integer(number_integer_t val)
  5246. {
  5247. handle_value(val);
  5248. return true;
  5249. }
  5250. bool number_unsigned(number_unsigned_t val)
  5251. {
  5252. handle_value(val);
  5253. return true;
  5254. }
  5255. bool number_float(number_float_t val, const string_t& /*unused*/)
  5256. {
  5257. handle_value(val);
  5258. return true;
  5259. }
  5260. bool string(string_t& val)
  5261. {
  5262. handle_value(val);
  5263. return true;
  5264. }
  5265. bool binary(binary_t& val)
  5266. {
  5267. handle_value(std::move(val));
  5268. return true;
  5269. }
  5270. bool start_object(std::size_t len)
  5271. {
  5272. ref_stack.push_back(handle_value(BasicJsonType::value_t::object));
  5273. if (JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size()))
  5274. {
  5275. JSON_THROW(out_of_range::create(408, "excessive object size: " + std::to_string(len), *ref_stack.back()));
  5276. }
  5277. return true;
  5278. }
  5279. bool key(string_t& val)
  5280. {
  5281. // add null at given key and store the reference for later
  5282. object_element = &(ref_stack.back()->m_value.object->operator[](val));
  5283. return true;
  5284. }
  5285. bool end_object()
  5286. {
  5287. ref_stack.back()->set_parents();
  5288. ref_stack.pop_back();
  5289. return true;
  5290. }
  5291. bool start_array(std::size_t len)
  5292. {
  5293. ref_stack.push_back(handle_value(BasicJsonType::value_t::array));
  5294. if (JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size()))
  5295. {
  5296. JSON_THROW(out_of_range::create(408, "excessive array size: " + std::to_string(len), *ref_stack.back()));
  5297. }
  5298. return true;
  5299. }
  5300. bool end_array()
  5301. {
  5302. ref_stack.back()->set_parents();
  5303. ref_stack.pop_back();
  5304. return true;
  5305. }
  5306. template<class Exception>
  5307. bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
  5308. const Exception& ex)
  5309. {
  5310. errored = true;
  5311. static_cast<void>(ex);
  5312. if (allow_exceptions)
  5313. {
  5314. JSON_THROW(ex);
  5315. }
  5316. return false;
  5317. }
  5318. constexpr bool is_errored() const
  5319. {
  5320. return errored;
  5321. }
  5322. private:
  5323. /*!
  5324. @invariant If the ref stack is empty, then the passed value will be the new
  5325. root.
  5326. @invariant If the ref stack contains a value, then it is an array or an
  5327. object to which we can add elements
  5328. */
  5329. template<typename Value>
  5330. JSON_HEDLEY_RETURNS_NON_NULL
  5331. BasicJsonType* handle_value(Value&& v)
  5332. {
  5333. if (ref_stack.empty())
  5334. {
  5335. root = BasicJsonType(std::forward<Value>(v));
  5336. return &root;
  5337. }
  5338. JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
  5339. if (ref_stack.back()->is_array())
  5340. {
  5341. ref_stack.back()->m_value.array->emplace_back(std::forward<Value>(v));
  5342. return &(ref_stack.back()->m_value.array->back());
  5343. }
  5344. JSON_ASSERT(ref_stack.back()->is_object());
  5345. JSON_ASSERT(object_element);
  5346. *object_element = BasicJsonType(std::forward<Value>(v));
  5347. return object_element;
  5348. }
  5349. /// the parsed JSON value
  5350. BasicJsonType& root;
  5351. /// stack to model hierarchy of values
  5352. std::vector<BasicJsonType*> ref_stack {};
  5353. /// helper to hold the reference for the next object element
  5354. BasicJsonType* object_element = nullptr;
  5355. /// whether a syntax error occurred
  5356. bool errored = false;
  5357. /// whether to throw exceptions in case of errors
  5358. const bool allow_exceptions = true;
  5359. };
  5360. template<typename BasicJsonType>
  5361. class json_sax_dom_callback_parser
  5362. {
  5363. public:
  5364. using number_integer_t = typename BasicJsonType::number_integer_t;
  5365. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  5366. using number_float_t = typename BasicJsonType::number_float_t;
  5367. using string_t = typename BasicJsonType::string_t;
  5368. using binary_t = typename BasicJsonType::binary_t;
  5369. using parser_callback_t = typename BasicJsonType::parser_callback_t;
  5370. using parse_event_t = typename BasicJsonType::parse_event_t;
  5371. json_sax_dom_callback_parser(BasicJsonType& r,
  5372. const parser_callback_t cb,
  5373. const bool allow_exceptions_ = true)
  5374. : root(r), callback(cb), allow_exceptions(allow_exceptions_)
  5375. {
  5376. keep_stack.push_back(true);
  5377. }
  5378. // make class move-only
  5379. json_sax_dom_callback_parser(const json_sax_dom_callback_parser&) = delete;
  5380. json_sax_dom_callback_parser(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  5381. json_sax_dom_callback_parser& operator=(const json_sax_dom_callback_parser&) = delete;
  5382. json_sax_dom_callback_parser& operator=(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  5383. ~json_sax_dom_callback_parser() = default;
  5384. bool null()
  5385. {
  5386. handle_value(nullptr);
  5387. return true;
  5388. }
  5389. bool boolean(bool val)
  5390. {
  5391. handle_value(val);
  5392. return true;
  5393. }
  5394. bool number_integer(number_integer_t val)
  5395. {
  5396. handle_value(val);
  5397. return true;
  5398. }
  5399. bool number_unsigned(number_unsigned_t val)
  5400. {
  5401. handle_value(val);
  5402. return true;
  5403. }
  5404. bool number_float(number_float_t val, const string_t& /*unused*/)
  5405. {
  5406. handle_value(val);
  5407. return true;
  5408. }
  5409. bool string(string_t& val)
  5410. {
  5411. handle_value(val);
  5412. return true;
  5413. }
  5414. bool binary(binary_t& val)
  5415. {
  5416. handle_value(std::move(val));
  5417. return true;
  5418. }
  5419. bool start_object(std::size_t len)
  5420. {
  5421. // check callback for object start
  5422. const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::object_start, discarded);
  5423. keep_stack.push_back(keep);
  5424. auto val = handle_value(BasicJsonType::value_t::object, true);
  5425. ref_stack.push_back(val.second);
  5426. // check object limit
  5427. if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size()))
  5428. {
  5429. JSON_THROW(out_of_range::create(408, "excessive object size: " + std::to_string(len), *ref_stack.back()));
  5430. }
  5431. return true;
  5432. }
  5433. bool key(string_t& val)
  5434. {
  5435. BasicJsonType k = BasicJsonType(val);
  5436. // check callback for key
  5437. const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::key, k);
  5438. key_keep_stack.push_back(keep);
  5439. // add discarded value at given key and store the reference for later
  5440. if (keep && ref_stack.back())
  5441. {
  5442. object_element = &(ref_stack.back()->m_value.object->operator[](val) = discarded);
  5443. }
  5444. return true;
  5445. }
  5446. bool end_object()
  5447. {
  5448. if (ref_stack.back())
  5449. {
  5450. if (!callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back()))
  5451. {
  5452. // discard object
  5453. *ref_stack.back() = discarded;
  5454. }
  5455. else
  5456. {
  5457. ref_stack.back()->set_parents();
  5458. }
  5459. }
  5460. JSON_ASSERT(!ref_stack.empty());
  5461. JSON_ASSERT(!keep_stack.empty());
  5462. ref_stack.pop_back();
  5463. keep_stack.pop_back();
  5464. if (!ref_stack.empty() && ref_stack.back() && ref_stack.back()->is_structured())
  5465. {
  5466. // remove discarded value
  5467. for (auto it = ref_stack.back()->begin(); it != ref_stack.back()->end(); ++it)
  5468. {
  5469. if (it->is_discarded())
  5470. {
  5471. ref_stack.back()->erase(it);
  5472. break;
  5473. }
  5474. }
  5475. }
  5476. return true;
  5477. }
  5478. bool start_array(std::size_t len)
  5479. {
  5480. const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::array_start, discarded);
  5481. keep_stack.push_back(keep);
  5482. auto val = handle_value(BasicJsonType::value_t::array, true);
  5483. ref_stack.push_back(val.second);
  5484. // check array limit
  5485. if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size()))
  5486. {
  5487. JSON_THROW(out_of_range::create(408, "excessive array size: " + std::to_string(len), *ref_stack.back()));
  5488. }
  5489. return true;
  5490. }
  5491. bool end_array()
  5492. {
  5493. bool keep = true;
  5494. if (ref_stack.back())
  5495. {
  5496. keep = callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::array_end, *ref_stack.back());
  5497. if (keep)
  5498. {
  5499. ref_stack.back()->set_parents();
  5500. }
  5501. else
  5502. {
  5503. // discard array
  5504. *ref_stack.back() = discarded;
  5505. }
  5506. }
  5507. JSON_ASSERT(!ref_stack.empty());
  5508. JSON_ASSERT(!keep_stack.empty());
  5509. ref_stack.pop_back();
  5510. keep_stack.pop_back();
  5511. // remove discarded value
  5512. if (!keep && !ref_stack.empty() && ref_stack.back()->is_array())
  5513. {
  5514. ref_stack.back()->m_value.array->pop_back();
  5515. }
  5516. return true;
  5517. }
  5518. template<class Exception>
  5519. bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
  5520. const Exception& ex)
  5521. {
  5522. errored = true;
  5523. static_cast<void>(ex);
  5524. if (allow_exceptions)
  5525. {
  5526. JSON_THROW(ex);
  5527. }
  5528. return false;
  5529. }
  5530. constexpr bool is_errored() const
  5531. {
  5532. return errored;
  5533. }
  5534. private:
  5535. /*!
  5536. @param[in] v value to add to the JSON value we build during parsing
  5537. @param[in] skip_callback whether we should skip calling the callback
  5538. function; this is required after start_array() and
  5539. start_object() SAX events, because otherwise we would call the
  5540. callback function with an empty array or object, respectively.
  5541. @invariant If the ref stack is empty, then the passed value will be the new
  5542. root.
  5543. @invariant If the ref stack contains a value, then it is an array or an
  5544. object to which we can add elements
  5545. @return pair of boolean (whether value should be kept) and pointer (to the
  5546. passed value in the ref_stack hierarchy; nullptr if not kept)
  5547. */
  5548. template<typename Value>
  5549. std::pair<bool, BasicJsonType*> handle_value(Value&& v, const bool skip_callback = false)
  5550. {
  5551. JSON_ASSERT(!keep_stack.empty());
  5552. // do not handle this value if we know it would be added to a discarded
  5553. // container
  5554. if (!keep_stack.back())
  5555. {
  5556. return {false, nullptr};
  5557. }
  5558. // create value
  5559. auto value = BasicJsonType(std::forward<Value>(v));
  5560. // check callback
  5561. const bool keep = skip_callback || callback(static_cast<int>(ref_stack.size()), parse_event_t::value, value);
  5562. // do not handle this value if we just learnt it shall be discarded
  5563. if (!keep)
  5564. {
  5565. return {false, nullptr};
  5566. }
  5567. if (ref_stack.empty())
  5568. {
  5569. root = std::move(value);
  5570. return {true, &root};
  5571. }
  5572. // skip this value if we already decided to skip the parent
  5573. // (https://github.com/nlohmann/json/issues/971#issuecomment-413678360)
  5574. if (!ref_stack.back())
  5575. {
  5576. return {false, nullptr};
  5577. }
  5578. // we now only expect arrays and objects
  5579. JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
  5580. // array
  5581. if (ref_stack.back()->is_array())
  5582. {
  5583. ref_stack.back()->m_value.array->emplace_back(std::move(value));
  5584. return {true, &(ref_stack.back()->m_value.array->back())};
  5585. }
  5586. // object
  5587. JSON_ASSERT(ref_stack.back()->is_object());
  5588. // check if we should store an element for the current key
  5589. JSON_ASSERT(!key_keep_stack.empty());
  5590. const bool store_element = key_keep_stack.back();
  5591. key_keep_stack.pop_back();
  5592. if (!store_element)
  5593. {
  5594. return {false, nullptr};
  5595. }
  5596. JSON_ASSERT(object_element);
  5597. *object_element = std::move(value);
  5598. return {true, object_element};
  5599. }
  5600. /// the parsed JSON value
  5601. BasicJsonType& root;
  5602. /// stack to model hierarchy of values
  5603. std::vector<BasicJsonType*> ref_stack {};
  5604. /// stack to manage which values to keep
  5605. std::vector<bool> keep_stack {};
  5606. /// stack to manage which object keys to keep
  5607. std::vector<bool> key_keep_stack {};
  5608. /// helper to hold the reference for the next object element
  5609. BasicJsonType* object_element = nullptr;
  5610. /// whether a syntax error occurred
  5611. bool errored = false;
  5612. /// callback function
  5613. const parser_callback_t callback = nullptr;
  5614. /// whether to throw exceptions in case of errors
  5615. const bool allow_exceptions = true;
  5616. /// a discarded value for the callback
  5617. BasicJsonType discarded = BasicJsonType::value_t::discarded;
  5618. };
  5619. template<typename BasicJsonType>
  5620. class json_sax_acceptor
  5621. {
  5622. public:
  5623. using number_integer_t = typename BasicJsonType::number_integer_t;
  5624. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  5625. using number_float_t = typename BasicJsonType::number_float_t;
  5626. using string_t = typename BasicJsonType::string_t;
  5627. using binary_t = typename BasicJsonType::binary_t;
  5628. bool null()
  5629. {
  5630. return true;
  5631. }
  5632. bool boolean(bool /*unused*/)
  5633. {
  5634. return true;
  5635. }
  5636. bool number_integer(number_integer_t /*unused*/)
  5637. {
  5638. return true;
  5639. }
  5640. bool number_unsigned(number_unsigned_t /*unused*/)
  5641. {
  5642. return true;
  5643. }
  5644. bool number_float(number_float_t /*unused*/, const string_t& /*unused*/)
  5645. {
  5646. return true;
  5647. }
  5648. bool string(string_t& /*unused*/)
  5649. {
  5650. return true;
  5651. }
  5652. bool binary(binary_t& /*unused*/)
  5653. {
  5654. return true;
  5655. }
  5656. bool start_object(std::size_t /*unused*/ = std::size_t(-1))
  5657. {
  5658. return true;
  5659. }
  5660. bool key(string_t& /*unused*/)
  5661. {
  5662. return true;
  5663. }
  5664. bool end_object()
  5665. {
  5666. return true;
  5667. }
  5668. bool start_array(std::size_t /*unused*/ = std::size_t(-1))
  5669. {
  5670. return true;
  5671. }
  5672. bool end_array()
  5673. {
  5674. return true;
  5675. }
  5676. bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const detail::exception& /*unused*/)
  5677. {
  5678. return false;
  5679. }
  5680. };
  5681. } // namespace detail
  5682. } // namespace nlohmann
  5683. // #include <nlohmann/detail/input/lexer.hpp>
  5684. #include <array> // array
  5685. #include <clocale> // localeconv
  5686. #include <cstddef> // size_t
  5687. #include <cstdio> // snprintf
  5688. #include <cstdlib> // strtof, strtod, strtold, strtoll, strtoull
  5689. #include <initializer_list> // initializer_list
  5690. #include <string> // char_traits, string
  5691. #include <utility> // move
  5692. #include <vector> // vector
  5693. // #include <nlohmann/detail/input/input_adapters.hpp>
  5694. // #include <nlohmann/detail/input/position_t.hpp>
  5695. // #include <nlohmann/detail/macro_scope.hpp>
  5696. namespace nlohmann
  5697. {
  5698. namespace detail
  5699. {
  5700. ///////////
  5701. // lexer //
  5702. ///////////
  5703. template<typename BasicJsonType>
  5704. class lexer_base
  5705. {
  5706. public:
  5707. /// token types for the parser
  5708. enum class token_type
  5709. {
  5710. uninitialized, ///< indicating the scanner is uninitialized
  5711. literal_true, ///< the `true` literal
  5712. literal_false, ///< the `false` literal
  5713. literal_null, ///< the `null` literal
  5714. value_string, ///< a string -- use get_string() for actual value
  5715. value_unsigned, ///< an unsigned integer -- use get_number_unsigned() for actual value
  5716. value_integer, ///< a signed integer -- use get_number_integer() for actual value
  5717. value_float, ///< an floating point number -- use get_number_float() for actual value
  5718. begin_array, ///< the character for array begin `[`
  5719. begin_object, ///< the character for object begin `{`
  5720. end_array, ///< the character for array end `]`
  5721. end_object, ///< the character for object end `}`
  5722. name_separator, ///< the name separator `:`
  5723. value_separator, ///< the value separator `,`
  5724. parse_error, ///< indicating a parse error
  5725. end_of_input, ///< indicating the end of the input buffer
  5726. literal_or_value ///< a literal or the begin of a value (only for diagnostics)
  5727. };
  5728. /// return name of values of type token_type (only used for errors)
  5729. JSON_HEDLEY_RETURNS_NON_NULL
  5730. JSON_HEDLEY_CONST
  5731. static const char* token_type_name(const token_type t) noexcept
  5732. {
  5733. switch (t)
  5734. {
  5735. case token_type::uninitialized:
  5736. return "<uninitialized>";
  5737. case token_type::literal_true:
  5738. return "true literal";
  5739. case token_type::literal_false:
  5740. return "false literal";
  5741. case token_type::literal_null:
  5742. return "null literal";
  5743. case token_type::value_string:
  5744. return "string literal";
  5745. case token_type::value_unsigned:
  5746. case token_type::value_integer:
  5747. case token_type::value_float:
  5748. return "number literal";
  5749. case token_type::begin_array:
  5750. return "'['";
  5751. case token_type::begin_object:
  5752. return "'{'";
  5753. case token_type::end_array:
  5754. return "']'";
  5755. case token_type::end_object:
  5756. return "'}'";
  5757. case token_type::name_separator:
  5758. return "':'";
  5759. case token_type::value_separator:
  5760. return "','";
  5761. case token_type::parse_error:
  5762. return "<parse error>";
  5763. case token_type::end_of_input:
  5764. return "end of input";
  5765. case token_type::literal_or_value:
  5766. return "'[', '{', or a literal";
  5767. // LCOV_EXCL_START
  5768. default: // catch non-enum values
  5769. return "unknown token";
  5770. // LCOV_EXCL_STOP
  5771. }
  5772. }
  5773. };
  5774. /*!
  5775. @brief lexical analysis
  5776. This class organizes the lexical analysis during JSON deserialization.
  5777. */
  5778. template<typename BasicJsonType, typename InputAdapterType>
  5779. class lexer : public lexer_base<BasicJsonType>
  5780. {
  5781. using number_integer_t = typename BasicJsonType::number_integer_t;
  5782. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  5783. using number_float_t = typename BasicJsonType::number_float_t;
  5784. using string_t = typename BasicJsonType::string_t;
  5785. using char_type = typename InputAdapterType::char_type;
  5786. using char_int_type = typename std::char_traits<char_type>::int_type;
  5787. public:
  5788. using token_type = typename lexer_base<BasicJsonType>::token_type;
  5789. explicit lexer(InputAdapterType&& adapter, bool ignore_comments_ = false) noexcept
  5790. : ia(std::move(adapter))
  5791. , ignore_comments(ignore_comments_)
  5792. , decimal_point_char(static_cast<char_int_type>(get_decimal_point()))
  5793. {}
  5794. // delete because of pointer members
  5795. lexer(const lexer&) = delete;
  5796. lexer(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  5797. lexer& operator=(lexer&) = delete;
  5798. lexer& operator=(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  5799. ~lexer() = default;
  5800. private:
  5801. /////////////////////
  5802. // locales
  5803. /////////////////////
  5804. /// return the locale-dependent decimal point
  5805. JSON_HEDLEY_PURE
  5806. static char get_decimal_point() noexcept
  5807. {
  5808. const auto* loc = localeconv();
  5809. JSON_ASSERT(loc != nullptr);
  5810. return (loc->decimal_point == nullptr) ? '.' : *(loc->decimal_point);
  5811. }
  5812. /////////////////////
  5813. // scan functions
  5814. /////////////////////
  5815. /*!
  5816. @brief get codepoint from 4 hex characters following `\u`
  5817. For input "\u c1 c2 c3 c4" the codepoint is:
  5818. (c1 * 0x1000) + (c2 * 0x0100) + (c3 * 0x0010) + c4
  5819. = (c1 << 12) + (c2 << 8) + (c3 << 4) + (c4 << 0)
  5820. Furthermore, the possible characters '0'..'9', 'A'..'F', and 'a'..'f'
  5821. must be converted to the integers 0x0..0x9, 0xA..0xF, 0xA..0xF, resp. The
  5822. conversion is done by subtracting the offset (0x30, 0x37, and 0x57)
  5823. between the ASCII value of the character and the desired integer value.
  5824. @return codepoint (0x0000..0xFFFF) or -1 in case of an error (e.g. EOF or
  5825. non-hex character)
  5826. */
  5827. int get_codepoint()
  5828. {
  5829. // this function only makes sense after reading `\u`
  5830. JSON_ASSERT(current == 'u');
  5831. int codepoint = 0;
  5832. const auto factors = { 12u, 8u, 4u, 0u };
  5833. for (const auto factor : factors)
  5834. {
  5835. get();
  5836. if (current >= '0' && current <= '9')
  5837. {
  5838. codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x30u) << factor);
  5839. }
  5840. else if (current >= 'A' && current <= 'F')
  5841. {
  5842. codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x37u) << factor);
  5843. }
  5844. else if (current >= 'a' && current <= 'f')
  5845. {
  5846. codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x57u) << factor);
  5847. }
  5848. else
  5849. {
  5850. return -1;
  5851. }
  5852. }
  5853. JSON_ASSERT(0x0000 <= codepoint && codepoint <= 0xFFFF);
  5854. return codepoint;
  5855. }
  5856. /*!
  5857. @brief check if the next byte(s) are inside a given range
  5858. Adds the current byte and, for each passed range, reads a new byte and
  5859. checks if it is inside the range. If a violation was detected, set up an
  5860. error message and return false. Otherwise, return true.
  5861. @param[in] ranges list of integers; interpreted as list of pairs of
  5862. inclusive lower and upper bound, respectively
  5863. @pre The passed list @a ranges must have 2, 4, or 6 elements; that is,
  5864. 1, 2, or 3 pairs. This precondition is enforced by an assertion.
  5865. @return true if and only if no range violation was detected
  5866. */
  5867. bool next_byte_in_range(std::initializer_list<char_int_type> ranges)
  5868. {
  5869. JSON_ASSERT(ranges.size() == 2 || ranges.size() == 4 || ranges.size() == 6);
  5870. add(current);
  5871. for (auto range = ranges.begin(); range != ranges.end(); ++range)
  5872. {
  5873. get();
  5874. if (JSON_HEDLEY_LIKELY(*range <= current && current <= *(++range)))
  5875. {
  5876. add(current);
  5877. }
  5878. else
  5879. {
  5880. error_message = "invalid string: ill-formed UTF-8 byte";
  5881. return false;
  5882. }
  5883. }
  5884. return true;
  5885. }
  5886. /*!
  5887. @brief scan a string literal
  5888. This function scans a string according to Sect. 7 of RFC 8259. While
  5889. scanning, bytes are escaped and copied into buffer token_buffer. Then the
  5890. function returns successfully, token_buffer is *not* null-terminated (as it
  5891. may contain \0 bytes), and token_buffer.size() is the number of bytes in the
  5892. string.
  5893. @return token_type::value_string if string could be successfully scanned,
  5894. token_type::parse_error otherwise
  5895. @note In case of errors, variable error_message contains a textual
  5896. description.
  5897. */
  5898. token_type scan_string()
  5899. {
  5900. // reset token_buffer (ignore opening quote)
  5901. reset();
  5902. // we entered the function by reading an open quote
  5903. JSON_ASSERT(current == '\"');
  5904. while (true)
  5905. {
  5906. // get next character
  5907. switch (get())
  5908. {
  5909. // end of file while parsing string
  5910. case std::char_traits<char_type>::eof():
  5911. {
  5912. error_message = "invalid string: missing closing quote";
  5913. return token_type::parse_error;
  5914. }
  5915. // closing quote
  5916. case '\"':
  5917. {
  5918. return token_type::value_string;
  5919. }
  5920. // escapes
  5921. case '\\':
  5922. {
  5923. switch (get())
  5924. {
  5925. // quotation mark
  5926. case '\"':
  5927. add('\"');
  5928. break;
  5929. // reverse solidus
  5930. case '\\':
  5931. add('\\');
  5932. break;
  5933. // solidus
  5934. case '/':
  5935. add('/');
  5936. break;
  5937. // backspace
  5938. case 'b':
  5939. add('\b');
  5940. break;
  5941. // form feed
  5942. case 'f':
  5943. add('\f');
  5944. break;
  5945. // line feed
  5946. case 'n':
  5947. add('\n');
  5948. break;
  5949. // carriage return
  5950. case 'r':
  5951. add('\r');
  5952. break;
  5953. // tab
  5954. case 't':
  5955. add('\t');
  5956. break;
  5957. // unicode escapes
  5958. case 'u':
  5959. {
  5960. const int codepoint1 = get_codepoint();
  5961. int codepoint = codepoint1; // start with codepoint1
  5962. if (JSON_HEDLEY_UNLIKELY(codepoint1 == -1))
  5963. {
  5964. error_message = "invalid string: '\\u' must be followed by 4 hex digits";
  5965. return token_type::parse_error;
  5966. }
  5967. // check if code point is a high surrogate
  5968. if (0xD800 <= codepoint1 && codepoint1 <= 0xDBFF)
  5969. {
  5970. // expect next \uxxxx entry
  5971. if (JSON_HEDLEY_LIKELY(get() == '\\' && get() == 'u'))
  5972. {
  5973. const int codepoint2 = get_codepoint();
  5974. if (JSON_HEDLEY_UNLIKELY(codepoint2 == -1))
  5975. {
  5976. error_message = "invalid string: '\\u' must be followed by 4 hex digits";
  5977. return token_type::parse_error;
  5978. }
  5979. // check if codepoint2 is a low surrogate
  5980. if (JSON_HEDLEY_LIKELY(0xDC00 <= codepoint2 && codepoint2 <= 0xDFFF))
  5981. {
  5982. // overwrite codepoint
  5983. codepoint = static_cast<int>(
  5984. // high surrogate occupies the most significant 22 bits
  5985. (static_cast<unsigned int>(codepoint1) << 10u)
  5986. // low surrogate occupies the least significant 15 bits
  5987. + static_cast<unsigned int>(codepoint2)
  5988. // there is still the 0xD800, 0xDC00 and 0x10000 noise
  5989. // in the result so we have to subtract with:
  5990. // (0xD800 << 10) + DC00 - 0x10000 = 0x35FDC00
  5991. - 0x35FDC00u);
  5992. }
  5993. else
  5994. {
  5995. error_message = "invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF";
  5996. return token_type::parse_error;
  5997. }
  5998. }
  5999. else
  6000. {
  6001. error_message = "invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF";
  6002. return token_type::parse_error;
  6003. }
  6004. }
  6005. else
  6006. {
  6007. if (JSON_HEDLEY_UNLIKELY(0xDC00 <= codepoint1 && codepoint1 <= 0xDFFF))
  6008. {
  6009. error_message = "invalid string: surrogate U+DC00..U+DFFF must follow U+D800..U+DBFF";
  6010. return token_type::parse_error;
  6011. }
  6012. }
  6013. // result of the above calculation yields a proper codepoint
  6014. JSON_ASSERT(0x00 <= codepoint && codepoint <= 0x10FFFF);
  6015. // translate codepoint into bytes
  6016. if (codepoint < 0x80)
  6017. {
  6018. // 1-byte characters: 0xxxxxxx (ASCII)
  6019. add(static_cast<char_int_type>(codepoint));
  6020. }
  6021. else if (codepoint <= 0x7FF)
  6022. {
  6023. // 2-byte characters: 110xxxxx 10xxxxxx
  6024. add(static_cast<char_int_type>(0xC0u | (static_cast<unsigned int>(codepoint) >> 6u)));
  6025. add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
  6026. }
  6027. else if (codepoint <= 0xFFFF)
  6028. {
  6029. // 3-byte characters: 1110xxxx 10xxxxxx 10xxxxxx
  6030. add(static_cast<char_int_type>(0xE0u | (static_cast<unsigned int>(codepoint) >> 12u)));
  6031. add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 6u) & 0x3Fu)));
  6032. add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
  6033. }
  6034. else
  6035. {
  6036. // 4-byte characters: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  6037. add(static_cast<char_int_type>(0xF0u | (static_cast<unsigned int>(codepoint) >> 18u)));
  6038. add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 12u) & 0x3Fu)));
  6039. add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 6u) & 0x3Fu)));
  6040. add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
  6041. }
  6042. break;
  6043. }
  6044. // other characters after escape
  6045. default:
  6046. error_message = "invalid string: forbidden character after backslash";
  6047. return token_type::parse_error;
  6048. }
  6049. break;
  6050. }
  6051. // invalid control characters
  6052. case 0x00:
  6053. {
  6054. error_message = "invalid string: control character U+0000 (NUL) must be escaped to \\u0000";
  6055. return token_type::parse_error;
  6056. }
  6057. case 0x01:
  6058. {
  6059. error_message = "invalid string: control character U+0001 (SOH) must be escaped to \\u0001";
  6060. return token_type::parse_error;
  6061. }
  6062. case 0x02:
  6063. {
  6064. error_message = "invalid string: control character U+0002 (STX) must be escaped to \\u0002";
  6065. return token_type::parse_error;
  6066. }
  6067. case 0x03:
  6068. {
  6069. error_message = "invalid string: control character U+0003 (ETX) must be escaped to \\u0003";
  6070. return token_type::parse_error;
  6071. }
  6072. case 0x04:
  6073. {
  6074. error_message = "invalid string: control character U+0004 (EOT) must be escaped to \\u0004";
  6075. return token_type::parse_error;
  6076. }
  6077. case 0x05:
  6078. {
  6079. error_message = "invalid string: control character U+0005 (ENQ) must be escaped to \\u0005";
  6080. return token_type::parse_error;
  6081. }
  6082. case 0x06:
  6083. {
  6084. error_message = "invalid string: control character U+0006 (ACK) must be escaped to \\u0006";
  6085. return token_type::parse_error;
  6086. }
  6087. case 0x07:
  6088. {
  6089. error_message = "invalid string: control character U+0007 (BEL) must be escaped to \\u0007";
  6090. return token_type::parse_error;
  6091. }
  6092. case 0x08:
  6093. {
  6094. error_message = "invalid string: control character U+0008 (BS) must be escaped to \\u0008 or \\b";
  6095. return token_type::parse_error;
  6096. }
  6097. case 0x09:
  6098. {
  6099. error_message = "invalid string: control character U+0009 (HT) must be escaped to \\u0009 or \\t";
  6100. return token_type::parse_error;
  6101. }
  6102. case 0x0A:
  6103. {
  6104. error_message = "invalid string: control character U+000A (LF) must be escaped to \\u000A or \\n";
  6105. return token_type::parse_error;
  6106. }
  6107. case 0x0B:
  6108. {
  6109. error_message = "invalid string: control character U+000B (VT) must be escaped to \\u000B";
  6110. return token_type::parse_error;
  6111. }
  6112. case 0x0C:
  6113. {
  6114. error_message = "invalid string: control character U+000C (FF) must be escaped to \\u000C or \\f";
  6115. return token_type::parse_error;
  6116. }
  6117. case 0x0D:
  6118. {
  6119. error_message = "invalid string: control character U+000D (CR) must be escaped to \\u000D or \\r";
  6120. return token_type::parse_error;
  6121. }
  6122. case 0x0E:
  6123. {
  6124. error_message = "invalid string: control character U+000E (SO) must be escaped to \\u000E";
  6125. return token_type::parse_error;
  6126. }
  6127. case 0x0F:
  6128. {
  6129. error_message = "invalid string: control character U+000F (SI) must be escaped to \\u000F";
  6130. return token_type::parse_error;
  6131. }
  6132. case 0x10:
  6133. {
  6134. error_message = "invalid string: control character U+0010 (DLE) must be escaped to \\u0010";
  6135. return token_type::parse_error;
  6136. }
  6137. case 0x11:
  6138. {
  6139. error_message = "invalid string: control character U+0011 (DC1) must be escaped to \\u0011";
  6140. return token_type::parse_error;
  6141. }
  6142. case 0x12:
  6143. {
  6144. error_message = "invalid string: control character U+0012 (DC2) must be escaped to \\u0012";
  6145. return token_type::parse_error;
  6146. }
  6147. case 0x13:
  6148. {
  6149. error_message = "invalid string: control character U+0013 (DC3) must be escaped to \\u0013";
  6150. return token_type::parse_error;
  6151. }
  6152. case 0x14:
  6153. {
  6154. error_message = "invalid string: control character U+0014 (DC4) must be escaped to \\u0014";
  6155. return token_type::parse_error;
  6156. }
  6157. case 0x15:
  6158. {
  6159. error_message = "invalid string: control character U+0015 (NAK) must be escaped to \\u0015";
  6160. return token_type::parse_error;
  6161. }
  6162. case 0x16:
  6163. {
  6164. error_message = "invalid string: control character U+0016 (SYN) must be escaped to \\u0016";
  6165. return token_type::parse_error;
  6166. }
  6167. case 0x17:
  6168. {
  6169. error_message = "invalid string: control character U+0017 (ETB) must be escaped to \\u0017";
  6170. return token_type::parse_error;
  6171. }
  6172. case 0x18:
  6173. {
  6174. error_message = "invalid string: control character U+0018 (CAN) must be escaped to \\u0018";
  6175. return token_type::parse_error;
  6176. }
  6177. case 0x19:
  6178. {
  6179. error_message = "invalid string: control character U+0019 (EM) must be escaped to \\u0019";
  6180. return token_type::parse_error;
  6181. }
  6182. case 0x1A:
  6183. {
  6184. error_message = "invalid string: control character U+001A (SUB) must be escaped to \\u001A";
  6185. return token_type::parse_error;
  6186. }
  6187. case 0x1B:
  6188. {
  6189. error_message = "invalid string: control character U+001B (ESC) must be escaped to \\u001B";
  6190. return token_type::parse_error;
  6191. }
  6192. case 0x1C:
  6193. {
  6194. error_message = "invalid string: control character U+001C (FS) must be escaped to \\u001C";
  6195. return token_type::parse_error;
  6196. }
  6197. case 0x1D:
  6198. {
  6199. error_message = "invalid string: control character U+001D (GS) must be escaped to \\u001D";
  6200. return token_type::parse_error;
  6201. }
  6202. case 0x1E:
  6203. {
  6204. error_message = "invalid string: control character U+001E (RS) must be escaped to \\u001E";
  6205. return token_type::parse_error;
  6206. }
  6207. case 0x1F:
  6208. {
  6209. error_message = "invalid string: control character U+001F (US) must be escaped to \\u001F";
  6210. return token_type::parse_error;
  6211. }
  6212. // U+0020..U+007F (except U+0022 (quote) and U+005C (backspace))
  6213. case 0x20:
  6214. case 0x21:
  6215. case 0x23:
  6216. case 0x24:
  6217. case 0x25:
  6218. case 0x26:
  6219. case 0x27:
  6220. case 0x28:
  6221. case 0x29:
  6222. case 0x2A:
  6223. case 0x2B:
  6224. case 0x2C:
  6225. case 0x2D:
  6226. case 0x2E:
  6227. case 0x2F:
  6228. case 0x30:
  6229. case 0x31:
  6230. case 0x32:
  6231. case 0x33:
  6232. case 0x34:
  6233. case 0x35:
  6234. case 0x36:
  6235. case 0x37:
  6236. case 0x38:
  6237. case 0x39:
  6238. case 0x3A:
  6239. case 0x3B:
  6240. case 0x3C:
  6241. case 0x3D:
  6242. case 0x3E:
  6243. case 0x3F:
  6244. case 0x40:
  6245. case 0x41:
  6246. case 0x42:
  6247. case 0x43:
  6248. case 0x44:
  6249. case 0x45:
  6250. case 0x46:
  6251. case 0x47:
  6252. case 0x48:
  6253. case 0x49:
  6254. case 0x4A:
  6255. case 0x4B:
  6256. case 0x4C:
  6257. case 0x4D:
  6258. case 0x4E:
  6259. case 0x4F:
  6260. case 0x50:
  6261. case 0x51:
  6262. case 0x52:
  6263. case 0x53:
  6264. case 0x54:
  6265. case 0x55:
  6266. case 0x56:
  6267. case 0x57:
  6268. case 0x58:
  6269. case 0x59:
  6270. case 0x5A:
  6271. case 0x5B:
  6272. case 0x5D:
  6273. case 0x5E:
  6274. case 0x5F:
  6275. case 0x60:
  6276. case 0x61:
  6277. case 0x62:
  6278. case 0x63:
  6279. case 0x64:
  6280. case 0x65:
  6281. case 0x66:
  6282. case 0x67:
  6283. case 0x68:
  6284. case 0x69:
  6285. case 0x6A:
  6286. case 0x6B:
  6287. case 0x6C:
  6288. case 0x6D:
  6289. case 0x6E:
  6290. case 0x6F:
  6291. case 0x70:
  6292. case 0x71:
  6293. case 0x72:
  6294. case 0x73:
  6295. case 0x74:
  6296. case 0x75:
  6297. case 0x76:
  6298. case 0x77:
  6299. case 0x78:
  6300. case 0x79:
  6301. case 0x7A:
  6302. case 0x7B:
  6303. case 0x7C:
  6304. case 0x7D:
  6305. case 0x7E:
  6306. case 0x7F:
  6307. {
  6308. add(current);
  6309. break;
  6310. }
  6311. // U+0080..U+07FF: bytes C2..DF 80..BF
  6312. case 0xC2:
  6313. case 0xC3:
  6314. case 0xC4:
  6315. case 0xC5:
  6316. case 0xC6:
  6317. case 0xC7:
  6318. case 0xC8:
  6319. case 0xC9:
  6320. case 0xCA:
  6321. case 0xCB:
  6322. case 0xCC:
  6323. case 0xCD:
  6324. case 0xCE:
  6325. case 0xCF:
  6326. case 0xD0:
  6327. case 0xD1:
  6328. case 0xD2:
  6329. case 0xD3:
  6330. case 0xD4:
  6331. case 0xD5:
  6332. case 0xD6:
  6333. case 0xD7:
  6334. case 0xD8:
  6335. case 0xD9:
  6336. case 0xDA:
  6337. case 0xDB:
  6338. case 0xDC:
  6339. case 0xDD:
  6340. case 0xDE:
  6341. case 0xDF:
  6342. {
  6343. if (JSON_HEDLEY_UNLIKELY(!next_byte_in_range({0x80, 0xBF})))
  6344. {
  6345. return token_type::parse_error;
  6346. }
  6347. break;
  6348. }
  6349. // U+0800..U+0FFF: bytes E0 A0..BF 80..BF
  6350. case 0xE0:
  6351. {
  6352. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0xA0, 0xBF, 0x80, 0xBF}))))
  6353. {
  6354. return token_type::parse_error;
  6355. }
  6356. break;
  6357. }
  6358. // U+1000..U+CFFF: bytes E1..EC 80..BF 80..BF
  6359. // U+E000..U+FFFF: bytes EE..EF 80..BF 80..BF
  6360. case 0xE1:
  6361. case 0xE2:
  6362. case 0xE3:
  6363. case 0xE4:
  6364. case 0xE5:
  6365. case 0xE6:
  6366. case 0xE7:
  6367. case 0xE8:
  6368. case 0xE9:
  6369. case 0xEA:
  6370. case 0xEB:
  6371. case 0xEC:
  6372. case 0xEE:
  6373. case 0xEF:
  6374. {
  6375. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0xBF, 0x80, 0xBF}))))
  6376. {
  6377. return token_type::parse_error;
  6378. }
  6379. break;
  6380. }
  6381. // U+D000..U+D7FF: bytes ED 80..9F 80..BF
  6382. case 0xED:
  6383. {
  6384. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0x9F, 0x80, 0xBF}))))
  6385. {
  6386. return token_type::parse_error;
  6387. }
  6388. break;
  6389. }
  6390. // U+10000..U+3FFFF F0 90..BF 80..BF 80..BF
  6391. case 0xF0:
  6392. {
  6393. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x90, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
  6394. {
  6395. return token_type::parse_error;
  6396. }
  6397. break;
  6398. }
  6399. // U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF
  6400. case 0xF1:
  6401. case 0xF2:
  6402. case 0xF3:
  6403. {
  6404. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
  6405. {
  6406. return token_type::parse_error;
  6407. }
  6408. break;
  6409. }
  6410. // U+100000..U+10FFFF F4 80..8F 80..BF 80..BF
  6411. case 0xF4:
  6412. {
  6413. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0x8F, 0x80, 0xBF, 0x80, 0xBF}))))
  6414. {
  6415. return token_type::parse_error;
  6416. }
  6417. break;
  6418. }
  6419. // remaining bytes (80..C1 and F5..FF) are ill-formed
  6420. default:
  6421. {
  6422. error_message = "invalid string: ill-formed UTF-8 byte";
  6423. return token_type::parse_error;
  6424. }
  6425. }
  6426. }
  6427. }
  6428. /*!
  6429. * @brief scan a comment
  6430. * @return whether comment could be scanned successfully
  6431. */
  6432. bool scan_comment()
  6433. {
  6434. switch (get())
  6435. {
  6436. // single-line comments skip input until a newline or EOF is read
  6437. case '/':
  6438. {
  6439. while (true)
  6440. {
  6441. switch (get())
  6442. {
  6443. case '\n':
  6444. case '\r':
  6445. case std::char_traits<char_type>::eof():
  6446. case '\0':
  6447. return true;
  6448. default:
  6449. break;
  6450. }
  6451. }
  6452. }
  6453. // multi-line comments skip input until */ is read
  6454. case '*':
  6455. {
  6456. while (true)
  6457. {
  6458. switch (get())
  6459. {
  6460. case std::char_traits<char_type>::eof():
  6461. case '\0':
  6462. {
  6463. error_message = "invalid comment; missing closing '*/'";
  6464. return false;
  6465. }
  6466. case '*':
  6467. {
  6468. switch (get())
  6469. {
  6470. case '/':
  6471. return true;
  6472. default:
  6473. {
  6474. unget();
  6475. continue;
  6476. }
  6477. }
  6478. }
  6479. default:
  6480. continue;
  6481. }
  6482. }
  6483. }
  6484. // unexpected character after reading '/'
  6485. default:
  6486. {
  6487. error_message = "invalid comment; expecting '/' or '*' after '/'";
  6488. return false;
  6489. }
  6490. }
  6491. }
  6492. JSON_HEDLEY_NON_NULL(2)
  6493. static void strtof(float& f, const char* str, char** endptr) noexcept
  6494. {
  6495. f = std::strtof(str, endptr);
  6496. }
  6497. JSON_HEDLEY_NON_NULL(2)
  6498. static void strtof(double& f, const char* str, char** endptr) noexcept
  6499. {
  6500. f = std::strtod(str, endptr);
  6501. }
  6502. JSON_HEDLEY_NON_NULL(2)
  6503. static void strtof(long double& f, const char* str, char** endptr) noexcept
  6504. {
  6505. f = std::strtold(str, endptr);
  6506. }
  6507. /*!
  6508. @brief scan a number literal
  6509. This function scans a string according to Sect. 6 of RFC 8259.
  6510. The function is realized with a deterministic finite state machine derived
  6511. from the grammar described in RFC 8259. Starting in state "init", the
  6512. input is read and used to determined the next state. Only state "done"
  6513. accepts the number. State "error" is a trap state to model errors. In the
  6514. table below, "anything" means any character but the ones listed before.
  6515. state | 0 | 1-9 | e E | + | - | . | anything
  6516. ---------|----------|----------|----------|---------|---------|----------|-----------
  6517. init | zero | any1 | [error] | [error] | minus | [error] | [error]
  6518. minus | zero | any1 | [error] | [error] | [error] | [error] | [error]
  6519. zero | done | done | exponent | done | done | decimal1 | done
  6520. any1 | any1 | any1 | exponent | done | done | decimal1 | done
  6521. decimal1 | decimal2 | decimal2 | [error] | [error] | [error] | [error] | [error]
  6522. decimal2 | decimal2 | decimal2 | exponent | done | done | done | done
  6523. exponent | any2 | any2 | [error] | sign | sign | [error] | [error]
  6524. sign | any2 | any2 | [error] | [error] | [error] | [error] | [error]
  6525. any2 | any2 | any2 | done | done | done | done | done
  6526. The state machine is realized with one label per state (prefixed with
  6527. "scan_number_") and `goto` statements between them. The state machine
  6528. contains cycles, but any cycle can be left when EOF is read. Therefore,
  6529. the function is guaranteed to terminate.
  6530. During scanning, the read bytes are stored in token_buffer. This string is
  6531. then converted to a signed integer, an unsigned integer, or a
  6532. floating-point number.
  6533. @return token_type::value_unsigned, token_type::value_integer, or
  6534. token_type::value_float if number could be successfully scanned,
  6535. token_type::parse_error otherwise
  6536. @note The scanner is independent of the current locale. Internally, the
  6537. locale's decimal point is used instead of `.` to work with the
  6538. locale-dependent converters.
  6539. */
  6540. token_type scan_number() // lgtm [cpp/use-of-goto]
  6541. {
  6542. // reset token_buffer to store the number's bytes
  6543. reset();
  6544. // the type of the parsed number; initially set to unsigned; will be
  6545. // changed if minus sign, decimal point or exponent is read
  6546. token_type number_type = token_type::value_unsigned;
  6547. // state (init): we just found out we need to scan a number
  6548. switch (current)
  6549. {
  6550. case '-':
  6551. {
  6552. add(current);
  6553. goto scan_number_minus;
  6554. }
  6555. case '0':
  6556. {
  6557. add(current);
  6558. goto scan_number_zero;
  6559. }
  6560. case '1':
  6561. case '2':
  6562. case '3':
  6563. case '4':
  6564. case '5':
  6565. case '6':
  6566. case '7':
  6567. case '8':
  6568. case '9':
  6569. {
  6570. add(current);
  6571. goto scan_number_any1;
  6572. }
  6573. // all other characters are rejected outside scan_number()
  6574. default: // LCOV_EXCL_LINE
  6575. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  6576. }
  6577. scan_number_minus:
  6578. // state: we just parsed a leading minus sign
  6579. number_type = token_type::value_integer;
  6580. switch (get())
  6581. {
  6582. case '0':
  6583. {
  6584. add(current);
  6585. goto scan_number_zero;
  6586. }
  6587. case '1':
  6588. case '2':
  6589. case '3':
  6590. case '4':
  6591. case '5':
  6592. case '6':
  6593. case '7':
  6594. case '8':
  6595. case '9':
  6596. {
  6597. add(current);
  6598. goto scan_number_any1;
  6599. }
  6600. default:
  6601. {
  6602. error_message = "invalid number; expected digit after '-'";
  6603. return token_type::parse_error;
  6604. }
  6605. }
  6606. scan_number_zero:
  6607. // state: we just parse a zero (maybe with a leading minus sign)
  6608. switch (get())
  6609. {
  6610. case '.':
  6611. {
  6612. add(decimal_point_char);
  6613. goto scan_number_decimal1;
  6614. }
  6615. case 'e':
  6616. case 'E':
  6617. {
  6618. add(current);
  6619. goto scan_number_exponent;
  6620. }
  6621. default:
  6622. goto scan_number_done;
  6623. }
  6624. scan_number_any1:
  6625. // state: we just parsed a number 0-9 (maybe with a leading minus sign)
  6626. switch (get())
  6627. {
  6628. case '0':
  6629. case '1':
  6630. case '2':
  6631. case '3':
  6632. case '4':
  6633. case '5':
  6634. case '6':
  6635. case '7':
  6636. case '8':
  6637. case '9':
  6638. {
  6639. add(current);
  6640. goto scan_number_any1;
  6641. }
  6642. case '.':
  6643. {
  6644. add(decimal_point_char);
  6645. goto scan_number_decimal1;
  6646. }
  6647. case 'e':
  6648. case 'E':
  6649. {
  6650. add(current);
  6651. goto scan_number_exponent;
  6652. }
  6653. default:
  6654. goto scan_number_done;
  6655. }
  6656. scan_number_decimal1:
  6657. // state: we just parsed a decimal point
  6658. number_type = token_type::value_float;
  6659. switch (get())
  6660. {
  6661. case '0':
  6662. case '1':
  6663. case '2':
  6664. case '3':
  6665. case '4':
  6666. case '5':
  6667. case '6':
  6668. case '7':
  6669. case '8':
  6670. case '9':
  6671. {
  6672. add(current);
  6673. goto scan_number_decimal2;
  6674. }
  6675. default:
  6676. {
  6677. error_message = "invalid number; expected digit after '.'";
  6678. return token_type::parse_error;
  6679. }
  6680. }
  6681. scan_number_decimal2:
  6682. // we just parsed at least one number after a decimal point
  6683. switch (get())
  6684. {
  6685. case '0':
  6686. case '1':
  6687. case '2':
  6688. case '3':
  6689. case '4':
  6690. case '5':
  6691. case '6':
  6692. case '7':
  6693. case '8':
  6694. case '9':
  6695. {
  6696. add(current);
  6697. goto scan_number_decimal2;
  6698. }
  6699. case 'e':
  6700. case 'E':
  6701. {
  6702. add(current);
  6703. goto scan_number_exponent;
  6704. }
  6705. default:
  6706. goto scan_number_done;
  6707. }
  6708. scan_number_exponent:
  6709. // we just parsed an exponent
  6710. number_type = token_type::value_float;
  6711. switch (get())
  6712. {
  6713. case '+':
  6714. case '-':
  6715. {
  6716. add(current);
  6717. goto scan_number_sign;
  6718. }
  6719. case '0':
  6720. case '1':
  6721. case '2':
  6722. case '3':
  6723. case '4':
  6724. case '5':
  6725. case '6':
  6726. case '7':
  6727. case '8':
  6728. case '9':
  6729. {
  6730. add(current);
  6731. goto scan_number_any2;
  6732. }
  6733. default:
  6734. {
  6735. error_message =
  6736. "invalid number; expected '+', '-', or digit after exponent";
  6737. return token_type::parse_error;
  6738. }
  6739. }
  6740. scan_number_sign:
  6741. // we just parsed an exponent sign
  6742. switch (get())
  6743. {
  6744. case '0':
  6745. case '1':
  6746. case '2':
  6747. case '3':
  6748. case '4':
  6749. case '5':
  6750. case '6':
  6751. case '7':
  6752. case '8':
  6753. case '9':
  6754. {
  6755. add(current);
  6756. goto scan_number_any2;
  6757. }
  6758. default:
  6759. {
  6760. error_message = "invalid number; expected digit after exponent sign";
  6761. return token_type::parse_error;
  6762. }
  6763. }
  6764. scan_number_any2:
  6765. // we just parsed a number after the exponent or exponent sign
  6766. switch (get())
  6767. {
  6768. case '0':
  6769. case '1':
  6770. case '2':
  6771. case '3':
  6772. case '4':
  6773. case '5':
  6774. case '6':
  6775. case '7':
  6776. case '8':
  6777. case '9':
  6778. {
  6779. add(current);
  6780. goto scan_number_any2;
  6781. }
  6782. default:
  6783. goto scan_number_done;
  6784. }
  6785. scan_number_done:
  6786. // unget the character after the number (we only read it to know that
  6787. // we are done scanning a number)
  6788. unget();
  6789. char* endptr = nullptr; // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  6790. errno = 0;
  6791. // try to parse integers first and fall back to floats
  6792. if (number_type == token_type::value_unsigned)
  6793. {
  6794. const auto x = std::strtoull(token_buffer.data(), &endptr, 10);
  6795. // we checked the number format before
  6796. JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
  6797. if (errno == 0)
  6798. {
  6799. value_unsigned = static_cast<number_unsigned_t>(x);
  6800. if (value_unsigned == x)
  6801. {
  6802. return token_type::value_unsigned;
  6803. }
  6804. }
  6805. }
  6806. else if (number_type == token_type::value_integer)
  6807. {
  6808. const auto x = std::strtoll(token_buffer.data(), &endptr, 10);
  6809. // we checked the number format before
  6810. JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
  6811. if (errno == 0)
  6812. {
  6813. value_integer = static_cast<number_integer_t>(x);
  6814. if (value_integer == x)
  6815. {
  6816. return token_type::value_integer;
  6817. }
  6818. }
  6819. }
  6820. // this code is reached if we parse a floating-point number or if an
  6821. // integer conversion above failed
  6822. strtof(value_float, token_buffer.data(), &endptr);
  6823. // we checked the number format before
  6824. JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
  6825. return token_type::value_float;
  6826. }
  6827. /*!
  6828. @param[in] literal_text the literal text to expect
  6829. @param[in] length the length of the passed literal text
  6830. @param[in] return_type the token type to return on success
  6831. */
  6832. JSON_HEDLEY_NON_NULL(2)
  6833. token_type scan_literal(const char_type* literal_text, const std::size_t length,
  6834. token_type return_type)
  6835. {
  6836. JSON_ASSERT(std::char_traits<char_type>::to_char_type(current) == literal_text[0]);
  6837. for (std::size_t i = 1; i < length; ++i)
  6838. {
  6839. if (JSON_HEDLEY_UNLIKELY(std::char_traits<char_type>::to_char_type(get()) != literal_text[i]))
  6840. {
  6841. error_message = "invalid literal";
  6842. return token_type::parse_error;
  6843. }
  6844. }
  6845. return return_type;
  6846. }
  6847. /////////////////////
  6848. // input management
  6849. /////////////////////
  6850. /// reset token_buffer; current character is beginning of token
  6851. void reset() noexcept
  6852. {
  6853. token_buffer.clear();
  6854. token_string.clear();
  6855. token_string.push_back(std::char_traits<char_type>::to_char_type(current));
  6856. }
  6857. /*
  6858. @brief get next character from the input
  6859. This function provides the interface to the used input adapter. It does
  6860. not throw in case the input reached EOF, but returns a
  6861. `std::char_traits<char>::eof()` in that case. Stores the scanned characters
  6862. for use in error messages.
  6863. @return character read from the input
  6864. */
  6865. char_int_type get()
  6866. {
  6867. ++position.chars_read_total;
  6868. ++position.chars_read_current_line;
  6869. if (next_unget)
  6870. {
  6871. // just reset the next_unget variable and work with current
  6872. next_unget = false;
  6873. }
  6874. else
  6875. {
  6876. current = ia.get_character();
  6877. }
  6878. if (JSON_HEDLEY_LIKELY(current != std::char_traits<char_type>::eof()))
  6879. {
  6880. token_string.push_back(std::char_traits<char_type>::to_char_type(current));
  6881. }
  6882. if (current == '\n')
  6883. {
  6884. ++position.lines_read;
  6885. position.chars_read_current_line = 0;
  6886. }
  6887. return current;
  6888. }
  6889. /*!
  6890. @brief unget current character (read it again on next get)
  6891. We implement unget by setting variable next_unget to true. The input is not
  6892. changed - we just simulate ungetting by modifying chars_read_total,
  6893. chars_read_current_line, and token_string. The next call to get() will
  6894. behave as if the unget character is read again.
  6895. */
  6896. void unget()
  6897. {
  6898. next_unget = true;
  6899. --position.chars_read_total;
  6900. // in case we "unget" a newline, we have to also decrement the lines_read
  6901. if (position.chars_read_current_line == 0)
  6902. {
  6903. if (position.lines_read > 0)
  6904. {
  6905. --position.lines_read;
  6906. }
  6907. }
  6908. else
  6909. {
  6910. --position.chars_read_current_line;
  6911. }
  6912. if (JSON_HEDLEY_LIKELY(current != std::char_traits<char_type>::eof()))
  6913. {
  6914. JSON_ASSERT(!token_string.empty());
  6915. token_string.pop_back();
  6916. }
  6917. }
  6918. /// add a character to token_buffer
  6919. void add(char_int_type c)
  6920. {
  6921. token_buffer.push_back(static_cast<typename string_t::value_type>(c));
  6922. }
  6923. public:
  6924. /////////////////////
  6925. // value getters
  6926. /////////////////////
  6927. /// return integer value
  6928. constexpr number_integer_t get_number_integer() const noexcept
  6929. {
  6930. return value_integer;
  6931. }
  6932. /// return unsigned integer value
  6933. constexpr number_unsigned_t get_number_unsigned() const noexcept
  6934. {
  6935. return value_unsigned;
  6936. }
  6937. /// return floating-point value
  6938. constexpr number_float_t get_number_float() const noexcept
  6939. {
  6940. return value_float;
  6941. }
  6942. /// return current string value (implicitly resets the token; useful only once)
  6943. string_t& get_string()
  6944. {
  6945. return token_buffer;
  6946. }
  6947. /////////////////////
  6948. // diagnostics
  6949. /////////////////////
  6950. /// return position of last read token
  6951. constexpr position_t get_position() const noexcept
  6952. {
  6953. return position;
  6954. }
  6955. /// return the last read token (for errors only). Will never contain EOF
  6956. /// (an arbitrary value that is not a valid char value, often -1), because
  6957. /// 255 may legitimately occur. May contain NUL, which should be escaped.
  6958. std::string get_token_string() const
  6959. {
  6960. // escape control characters
  6961. std::string result;
  6962. for (const auto c : token_string)
  6963. {
  6964. if (static_cast<unsigned char>(c) <= '\x1F')
  6965. {
  6966. // escape control characters
  6967. std::array<char, 9> cs{{}};
  6968. (std::snprintf)(cs.data(), cs.size(), "<U+%.4X>", static_cast<unsigned char>(c)); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  6969. result += cs.data();
  6970. }
  6971. else
  6972. {
  6973. // add character as is
  6974. result.push_back(static_cast<std::string::value_type>(c));
  6975. }
  6976. }
  6977. return result;
  6978. }
  6979. /// return syntax error message
  6980. JSON_HEDLEY_RETURNS_NON_NULL
  6981. constexpr const char* get_error_message() const noexcept
  6982. {
  6983. return error_message;
  6984. }
  6985. /////////////////////
  6986. // actual scanner
  6987. /////////////////////
  6988. /*!
  6989. @brief skip the UTF-8 byte order mark
  6990. @return true iff there is no BOM or the correct BOM has been skipped
  6991. */
  6992. bool skip_bom()
  6993. {
  6994. if (get() == 0xEF)
  6995. {
  6996. // check if we completely parse the BOM
  6997. return get() == 0xBB && get() == 0xBF;
  6998. }
  6999. // the first character is not the beginning of the BOM; unget it to
  7000. // process is later
  7001. unget();
  7002. return true;
  7003. }
  7004. void skip_whitespace()
  7005. {
  7006. do
  7007. {
  7008. get();
  7009. }
  7010. while (current == ' ' || current == '\t' || current == '\n' || current == '\r');
  7011. }
  7012. token_type scan()
  7013. {
  7014. // initially, skip the BOM
  7015. if (position.chars_read_total == 0 && !skip_bom())
  7016. {
  7017. error_message = "invalid BOM; must be 0xEF 0xBB 0xBF if given";
  7018. return token_type::parse_error;
  7019. }
  7020. // read next character and ignore whitespace
  7021. skip_whitespace();
  7022. // ignore comments
  7023. while (ignore_comments && current == '/')
  7024. {
  7025. if (!scan_comment())
  7026. {
  7027. return token_type::parse_error;
  7028. }
  7029. // skip following whitespace
  7030. skip_whitespace();
  7031. }
  7032. switch (current)
  7033. {
  7034. // structural characters
  7035. case '[':
  7036. return token_type::begin_array;
  7037. case ']':
  7038. return token_type::end_array;
  7039. case '{':
  7040. return token_type::begin_object;
  7041. case '}':
  7042. return token_type::end_object;
  7043. case ':':
  7044. return token_type::name_separator;
  7045. case ',':
  7046. return token_type::value_separator;
  7047. // literals
  7048. case 't':
  7049. {
  7050. std::array<char_type, 4> true_literal = {{char_type('t'), char_type('r'), char_type('u'), char_type('e')}};
  7051. return scan_literal(true_literal.data(), true_literal.size(), token_type::literal_true);
  7052. }
  7053. case 'f':
  7054. {
  7055. std::array<char_type, 5> false_literal = {{char_type('f'), char_type('a'), char_type('l'), char_type('s'), char_type('e')}};
  7056. return scan_literal(false_literal.data(), false_literal.size(), token_type::literal_false);
  7057. }
  7058. case 'n':
  7059. {
  7060. std::array<char_type, 4> null_literal = {{char_type('n'), char_type('u'), char_type('l'), char_type('l')}};
  7061. return scan_literal(null_literal.data(), null_literal.size(), token_type::literal_null);
  7062. }
  7063. // string
  7064. case '\"':
  7065. return scan_string();
  7066. // number
  7067. case '-':
  7068. case '0':
  7069. case '1':
  7070. case '2':
  7071. case '3':
  7072. case '4':
  7073. case '5':
  7074. case '6':
  7075. case '7':
  7076. case '8':
  7077. case '9':
  7078. return scan_number();
  7079. // end of input (the null byte is needed when parsing from
  7080. // string literals)
  7081. case '\0':
  7082. case std::char_traits<char_type>::eof():
  7083. return token_type::end_of_input;
  7084. // error
  7085. default:
  7086. error_message = "invalid literal";
  7087. return token_type::parse_error;
  7088. }
  7089. }
  7090. private:
  7091. /// input adapter
  7092. InputAdapterType ia;
  7093. /// whether comments should be ignored (true) or signaled as errors (false)
  7094. const bool ignore_comments = false;
  7095. /// the current character
  7096. char_int_type current = std::char_traits<char_type>::eof();
  7097. /// whether the next get() call should just return current
  7098. bool next_unget = false;
  7099. /// the start position of the current token
  7100. position_t position {};
  7101. /// raw input token string (for error messages)
  7102. std::vector<char_type> token_string {};
  7103. /// buffer for variable-length tokens (numbers, strings)
  7104. string_t token_buffer {};
  7105. /// a description of occurred lexer errors
  7106. const char* error_message = "";
  7107. // number values
  7108. number_integer_t value_integer = 0;
  7109. number_unsigned_t value_unsigned = 0;
  7110. number_float_t value_float = 0;
  7111. /// the decimal point
  7112. const char_int_type decimal_point_char = '.';
  7113. };
  7114. } // namespace detail
  7115. } // namespace nlohmann
  7116. // #include <nlohmann/detail/macro_scope.hpp>
  7117. // #include <nlohmann/detail/meta/is_sax.hpp>
  7118. #include <cstdint> // size_t
  7119. #include <utility> // declval
  7120. #include <string> // string
  7121. // #include <nlohmann/detail/meta/detected.hpp>
  7122. // #include <nlohmann/detail/meta/type_traits.hpp>
  7123. namespace nlohmann
  7124. {
  7125. namespace detail
  7126. {
  7127. template<typename T>
  7128. using null_function_t = decltype(std::declval<T&>().null());
  7129. template<typename T>
  7130. using boolean_function_t =
  7131. decltype(std::declval<T&>().boolean(std::declval<bool>()));
  7132. template<typename T, typename Integer>
  7133. using number_integer_function_t =
  7134. decltype(std::declval<T&>().number_integer(std::declval<Integer>()));
  7135. template<typename T, typename Unsigned>
  7136. using number_unsigned_function_t =
  7137. decltype(std::declval<T&>().number_unsigned(std::declval<Unsigned>()));
  7138. template<typename T, typename Float, typename String>
  7139. using number_float_function_t = decltype(std::declval<T&>().number_float(
  7140. std::declval<Float>(), std::declval<const String&>()));
  7141. template<typename T, typename String>
  7142. using string_function_t =
  7143. decltype(std::declval<T&>().string(std::declval<String&>()));
  7144. template<typename T, typename Binary>
  7145. using binary_function_t =
  7146. decltype(std::declval<T&>().binary(std::declval<Binary&>()));
  7147. template<typename T>
  7148. using start_object_function_t =
  7149. decltype(std::declval<T&>().start_object(std::declval<std::size_t>()));
  7150. template<typename T, typename String>
  7151. using key_function_t =
  7152. decltype(std::declval<T&>().key(std::declval<String&>()));
  7153. template<typename T>
  7154. using end_object_function_t = decltype(std::declval<T&>().end_object());
  7155. template<typename T>
  7156. using start_array_function_t =
  7157. decltype(std::declval<T&>().start_array(std::declval<std::size_t>()));
  7158. template<typename T>
  7159. using end_array_function_t = decltype(std::declval<T&>().end_array());
  7160. template<typename T, typename Exception>
  7161. using parse_error_function_t = decltype(std::declval<T&>().parse_error(
  7162. std::declval<std::size_t>(), std::declval<const std::string&>(),
  7163. std::declval<const Exception&>()));
  7164. template<typename SAX, typename BasicJsonType>
  7165. struct is_sax
  7166. {
  7167. private:
  7168. static_assert(is_basic_json<BasicJsonType>::value,
  7169. "BasicJsonType must be of type basic_json<...>");
  7170. using number_integer_t = typename BasicJsonType::number_integer_t;
  7171. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  7172. using number_float_t = typename BasicJsonType::number_float_t;
  7173. using string_t = typename BasicJsonType::string_t;
  7174. using binary_t = typename BasicJsonType::binary_t;
  7175. using exception_t = typename BasicJsonType::exception;
  7176. public:
  7177. static constexpr bool value =
  7178. is_detected_exact<bool, null_function_t, SAX>::value &&
  7179. is_detected_exact<bool, boolean_function_t, SAX>::value &&
  7180. is_detected_exact<bool, number_integer_function_t, SAX, number_integer_t>::value &&
  7181. is_detected_exact<bool, number_unsigned_function_t, SAX, number_unsigned_t>::value &&
  7182. is_detected_exact<bool, number_float_function_t, SAX, number_float_t, string_t>::value &&
  7183. is_detected_exact<bool, string_function_t, SAX, string_t>::value &&
  7184. is_detected_exact<bool, binary_function_t, SAX, binary_t>::value &&
  7185. is_detected_exact<bool, start_object_function_t, SAX>::value &&
  7186. is_detected_exact<bool, key_function_t, SAX, string_t>::value &&
  7187. is_detected_exact<bool, end_object_function_t, SAX>::value &&
  7188. is_detected_exact<bool, start_array_function_t, SAX>::value &&
  7189. is_detected_exact<bool, end_array_function_t, SAX>::value &&
  7190. is_detected_exact<bool, parse_error_function_t, SAX, exception_t>::value;
  7191. };
  7192. template<typename SAX, typename BasicJsonType>
  7193. struct is_sax_static_asserts
  7194. {
  7195. private:
  7196. static_assert(is_basic_json<BasicJsonType>::value,
  7197. "BasicJsonType must be of type basic_json<...>");
  7198. using number_integer_t = typename BasicJsonType::number_integer_t;
  7199. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  7200. using number_float_t = typename BasicJsonType::number_float_t;
  7201. using string_t = typename BasicJsonType::string_t;
  7202. using binary_t = typename BasicJsonType::binary_t;
  7203. using exception_t = typename BasicJsonType::exception;
  7204. public:
  7205. static_assert(is_detected_exact<bool, null_function_t, SAX>::value,
  7206. "Missing/invalid function: bool null()");
  7207. static_assert(is_detected_exact<bool, boolean_function_t, SAX>::value,
  7208. "Missing/invalid function: bool boolean(bool)");
  7209. static_assert(is_detected_exact<bool, boolean_function_t, SAX>::value,
  7210. "Missing/invalid function: bool boolean(bool)");
  7211. static_assert(
  7212. is_detected_exact<bool, number_integer_function_t, SAX,
  7213. number_integer_t>::value,
  7214. "Missing/invalid function: bool number_integer(number_integer_t)");
  7215. static_assert(
  7216. is_detected_exact<bool, number_unsigned_function_t, SAX,
  7217. number_unsigned_t>::value,
  7218. "Missing/invalid function: bool number_unsigned(number_unsigned_t)");
  7219. static_assert(is_detected_exact<bool, number_float_function_t, SAX,
  7220. number_float_t, string_t>::value,
  7221. "Missing/invalid function: bool number_float(number_float_t, const string_t&)");
  7222. static_assert(
  7223. is_detected_exact<bool, string_function_t, SAX, string_t>::value,
  7224. "Missing/invalid function: bool string(string_t&)");
  7225. static_assert(
  7226. is_detected_exact<bool, binary_function_t, SAX, binary_t>::value,
  7227. "Missing/invalid function: bool binary(binary_t&)");
  7228. static_assert(is_detected_exact<bool, start_object_function_t, SAX>::value,
  7229. "Missing/invalid function: bool start_object(std::size_t)");
  7230. static_assert(is_detected_exact<bool, key_function_t, SAX, string_t>::value,
  7231. "Missing/invalid function: bool key(string_t&)");
  7232. static_assert(is_detected_exact<bool, end_object_function_t, SAX>::value,
  7233. "Missing/invalid function: bool end_object()");
  7234. static_assert(is_detected_exact<bool, start_array_function_t, SAX>::value,
  7235. "Missing/invalid function: bool start_array(std::size_t)");
  7236. static_assert(is_detected_exact<bool, end_array_function_t, SAX>::value,
  7237. "Missing/invalid function: bool end_array()");
  7238. static_assert(
  7239. is_detected_exact<bool, parse_error_function_t, SAX, exception_t>::value,
  7240. "Missing/invalid function: bool parse_error(std::size_t, const "
  7241. "std::string&, const exception&)");
  7242. };
  7243. } // namespace detail
  7244. } // namespace nlohmann
  7245. // #include <nlohmann/detail/meta/type_traits.hpp>
  7246. // #include <nlohmann/detail/value_t.hpp>
  7247. namespace nlohmann
  7248. {
  7249. namespace detail
  7250. {
  7251. /// how to treat CBOR tags
  7252. enum class cbor_tag_handler_t
  7253. {
  7254. error, ///< throw a parse_error exception in case of a tag
  7255. ignore, ///< ignore tags
  7256. store ///< store tags as binary type
  7257. };
  7258. /*!
  7259. @brief determine system byte order
  7260. @return true if and only if system's byte order is little endian
  7261. @note from https://stackoverflow.com/a/1001328/266378
  7262. */
  7263. static inline bool little_endianess(int num = 1) noexcept
  7264. {
  7265. return *reinterpret_cast<char*>(&num) == 1;
  7266. }
  7267. ///////////////////
  7268. // binary reader //
  7269. ///////////////////
  7270. /*!
  7271. @brief deserialization of CBOR, MessagePack, and UBJSON values
  7272. */
  7273. template<typename BasicJsonType, typename InputAdapterType, typename SAX = json_sax_dom_parser<BasicJsonType>>
  7274. class binary_reader
  7275. {
  7276. using number_integer_t = typename BasicJsonType::number_integer_t;
  7277. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  7278. using number_float_t = typename BasicJsonType::number_float_t;
  7279. using string_t = typename BasicJsonType::string_t;
  7280. using binary_t = typename BasicJsonType::binary_t;
  7281. using json_sax_t = SAX;
  7282. using char_type = typename InputAdapterType::char_type;
  7283. using char_int_type = typename std::char_traits<char_type>::int_type;
  7284. public:
  7285. /*!
  7286. @brief create a binary reader
  7287. @param[in] adapter input adapter to read from
  7288. */
  7289. explicit binary_reader(InputAdapterType&& adapter) noexcept : ia(std::move(adapter))
  7290. {
  7291. (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
  7292. }
  7293. // make class move-only
  7294. binary_reader(const binary_reader&) = delete;
  7295. binary_reader(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  7296. binary_reader& operator=(const binary_reader&) = delete;
  7297. binary_reader& operator=(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  7298. ~binary_reader() = default;
  7299. /*!
  7300. @param[in] format the binary format to parse
  7301. @param[in] sax_ a SAX event processor
  7302. @param[in] strict whether to expect the input to be consumed completed
  7303. @param[in] tag_handler how to treat CBOR tags
  7304. @return whether parsing was successful
  7305. */
  7306. JSON_HEDLEY_NON_NULL(3)
  7307. bool sax_parse(const input_format_t format,
  7308. json_sax_t* sax_,
  7309. const bool strict = true,
  7310. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  7311. {
  7312. sax = sax_;
  7313. bool result = false;
  7314. switch (format)
  7315. {
  7316. case input_format_t::bson:
  7317. result = parse_bson_internal();
  7318. break;
  7319. case input_format_t::cbor:
  7320. result = parse_cbor_internal(true, tag_handler);
  7321. break;
  7322. case input_format_t::msgpack:
  7323. result = parse_msgpack_internal();
  7324. break;
  7325. case input_format_t::ubjson:
  7326. result = parse_ubjson_internal();
  7327. break;
  7328. case input_format_t::json: // LCOV_EXCL_LINE
  7329. default: // LCOV_EXCL_LINE
  7330. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  7331. }
  7332. // strict mode: next byte must be EOF
  7333. if (result && strict)
  7334. {
  7335. if (format == input_format_t::ubjson)
  7336. {
  7337. get_ignore_noop();
  7338. }
  7339. else
  7340. {
  7341. get();
  7342. }
  7343. if (JSON_HEDLEY_UNLIKELY(current != std::char_traits<char_type>::eof()))
  7344. {
  7345. return sax->parse_error(chars_read, get_token_string(),
  7346. parse_error::create(110, chars_read, exception_message(format, "expected end of input; last byte: 0x" + get_token_string(), "value"), BasicJsonType()));
  7347. }
  7348. }
  7349. return result;
  7350. }
  7351. private:
  7352. //////////
  7353. // BSON //
  7354. //////////
  7355. /*!
  7356. @brief Reads in a BSON-object and passes it to the SAX-parser.
  7357. @return whether a valid BSON-value was passed to the SAX parser
  7358. */
  7359. bool parse_bson_internal()
  7360. {
  7361. std::int32_t document_size{};
  7362. get_number<std::int32_t, true>(input_format_t::bson, document_size);
  7363. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(std::size_t(-1))))
  7364. {
  7365. return false;
  7366. }
  7367. if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/false)))
  7368. {
  7369. return false;
  7370. }
  7371. return sax->end_object();
  7372. }
  7373. /*!
  7374. @brief Parses a C-style string from the BSON input.
  7375. @param[in,out] result A reference to the string variable where the read
  7376. string is to be stored.
  7377. @return `true` if the \x00-byte indicating the end of the string was
  7378. encountered before the EOF; false` indicates an unexpected EOF.
  7379. */
  7380. bool get_bson_cstr(string_t& result)
  7381. {
  7382. auto out = std::back_inserter(result);
  7383. while (true)
  7384. {
  7385. get();
  7386. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "cstring")))
  7387. {
  7388. return false;
  7389. }
  7390. if (current == 0x00)
  7391. {
  7392. return true;
  7393. }
  7394. *out++ = static_cast<typename string_t::value_type>(current);
  7395. }
  7396. }
  7397. /*!
  7398. @brief Parses a zero-terminated string of length @a len from the BSON
  7399. input.
  7400. @param[in] len The length (including the zero-byte at the end) of the
  7401. string to be read.
  7402. @param[in,out] result A reference to the string variable where the read
  7403. string is to be stored.
  7404. @tparam NumberType The type of the length @a len
  7405. @pre len >= 1
  7406. @return `true` if the string was successfully parsed
  7407. */
  7408. template<typename NumberType>
  7409. bool get_bson_string(const NumberType len, string_t& result)
  7410. {
  7411. if (JSON_HEDLEY_UNLIKELY(len < 1))
  7412. {
  7413. auto last_token = get_token_string();
  7414. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "string length must be at least 1, is " + std::to_string(len), "string"), BasicJsonType()));
  7415. }
  7416. return get_string(input_format_t::bson, len - static_cast<NumberType>(1), result) && get() != std::char_traits<char_type>::eof();
  7417. }
  7418. /*!
  7419. @brief Parses a byte array input of length @a len from the BSON input.
  7420. @param[in] len The length of the byte array to be read.
  7421. @param[in,out] result A reference to the binary variable where the read
  7422. array is to be stored.
  7423. @tparam NumberType The type of the length @a len
  7424. @pre len >= 0
  7425. @return `true` if the byte array was successfully parsed
  7426. */
  7427. template<typename NumberType>
  7428. bool get_bson_binary(const NumberType len, binary_t& result)
  7429. {
  7430. if (JSON_HEDLEY_UNLIKELY(len < 0))
  7431. {
  7432. auto last_token = get_token_string();
  7433. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "byte array length cannot be negative, is " + std::to_string(len), "binary"), BasicJsonType()));
  7434. }
  7435. // All BSON binary values have a subtype
  7436. std::uint8_t subtype{};
  7437. get_number<std::uint8_t>(input_format_t::bson, subtype);
  7438. result.set_subtype(subtype);
  7439. return get_binary(input_format_t::bson, len, result);
  7440. }
  7441. /*!
  7442. @brief Read a BSON document element of the given @a element_type.
  7443. @param[in] element_type The BSON element type, c.f. http://bsonspec.org/spec.html
  7444. @param[in] element_type_parse_position The position in the input stream,
  7445. where the `element_type` was read.
  7446. @warning Not all BSON element types are supported yet. An unsupported
  7447. @a element_type will give rise to a parse_error.114:
  7448. Unsupported BSON record type 0x...
  7449. @return whether a valid BSON-object/array was passed to the SAX parser
  7450. */
  7451. bool parse_bson_element_internal(const char_int_type element_type,
  7452. const std::size_t element_type_parse_position)
  7453. {
  7454. switch (element_type)
  7455. {
  7456. case 0x01: // double
  7457. {
  7458. double number{};
  7459. return get_number<double, true>(input_format_t::bson, number) && sax->number_float(static_cast<number_float_t>(number), "");
  7460. }
  7461. case 0x02: // string
  7462. {
  7463. std::int32_t len{};
  7464. string_t value;
  7465. return get_number<std::int32_t, true>(input_format_t::bson, len) && get_bson_string(len, value) && sax->string(value);
  7466. }
  7467. case 0x03: // object
  7468. {
  7469. return parse_bson_internal();
  7470. }
  7471. case 0x04: // array
  7472. {
  7473. return parse_bson_array();
  7474. }
  7475. case 0x05: // binary
  7476. {
  7477. std::int32_t len{};
  7478. binary_t value;
  7479. return get_number<std::int32_t, true>(input_format_t::bson, len) && get_bson_binary(len, value) && sax->binary(value);
  7480. }
  7481. case 0x08: // boolean
  7482. {
  7483. return sax->boolean(get() != 0);
  7484. }
  7485. case 0x0A: // null
  7486. {
  7487. return sax->null();
  7488. }
  7489. case 0x10: // int32
  7490. {
  7491. std::int32_t value{};
  7492. return get_number<std::int32_t, true>(input_format_t::bson, value) && sax->number_integer(value);
  7493. }
  7494. case 0x12: // int64
  7495. {
  7496. std::int64_t value{};
  7497. return get_number<std::int64_t, true>(input_format_t::bson, value) && sax->number_integer(value);
  7498. }
  7499. default: // anything else not supported (yet)
  7500. {
  7501. std::array<char, 3> cr{{}};
  7502. (std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(element_type)); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  7503. return sax->parse_error(element_type_parse_position, std::string(cr.data()), parse_error::create(114, element_type_parse_position, "Unsupported BSON record type 0x" + std::string(cr.data()), BasicJsonType()));
  7504. }
  7505. }
  7506. }
  7507. /*!
  7508. @brief Read a BSON element list (as specified in the BSON-spec)
  7509. The same binary layout is used for objects and arrays, hence it must be
  7510. indicated with the argument @a is_array which one is expected
  7511. (true --> array, false --> object).
  7512. @param[in] is_array Determines if the element list being read is to be
  7513. treated as an object (@a is_array == false), or as an
  7514. array (@a is_array == true).
  7515. @return whether a valid BSON-object/array was passed to the SAX parser
  7516. */
  7517. bool parse_bson_element_list(const bool is_array)
  7518. {
  7519. string_t key;
  7520. while (auto element_type = get())
  7521. {
  7522. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "element list")))
  7523. {
  7524. return false;
  7525. }
  7526. const std::size_t element_type_parse_position = chars_read;
  7527. if (JSON_HEDLEY_UNLIKELY(!get_bson_cstr(key)))
  7528. {
  7529. return false;
  7530. }
  7531. if (!is_array && !sax->key(key))
  7532. {
  7533. return false;
  7534. }
  7535. if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_internal(element_type, element_type_parse_position)))
  7536. {
  7537. return false;
  7538. }
  7539. // get_bson_cstr only appends
  7540. key.clear();
  7541. }
  7542. return true;
  7543. }
  7544. /*!
  7545. @brief Reads an array from the BSON input and passes it to the SAX-parser.
  7546. @return whether a valid BSON-array was passed to the SAX parser
  7547. */
  7548. bool parse_bson_array()
  7549. {
  7550. std::int32_t document_size{};
  7551. get_number<std::int32_t, true>(input_format_t::bson, document_size);
  7552. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(std::size_t(-1))))
  7553. {
  7554. return false;
  7555. }
  7556. if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/true)))
  7557. {
  7558. return false;
  7559. }
  7560. return sax->end_array();
  7561. }
  7562. //////////
  7563. // CBOR //
  7564. //////////
  7565. /*!
  7566. @param[in] get_char whether a new character should be retrieved from the
  7567. input (true) or whether the last read character should
  7568. be considered instead (false)
  7569. @param[in] tag_handler how CBOR tags should be treated
  7570. @return whether a valid CBOR value was passed to the SAX parser
  7571. */
  7572. bool parse_cbor_internal(const bool get_char,
  7573. const cbor_tag_handler_t tag_handler)
  7574. {
  7575. switch (get_char ? get() : current)
  7576. {
  7577. // EOF
  7578. case std::char_traits<char_type>::eof():
  7579. return unexpect_eof(input_format_t::cbor, "value");
  7580. // Integer 0x00..0x17 (0..23)
  7581. case 0x00:
  7582. case 0x01:
  7583. case 0x02:
  7584. case 0x03:
  7585. case 0x04:
  7586. case 0x05:
  7587. case 0x06:
  7588. case 0x07:
  7589. case 0x08:
  7590. case 0x09:
  7591. case 0x0A:
  7592. case 0x0B:
  7593. case 0x0C:
  7594. case 0x0D:
  7595. case 0x0E:
  7596. case 0x0F:
  7597. case 0x10:
  7598. case 0x11:
  7599. case 0x12:
  7600. case 0x13:
  7601. case 0x14:
  7602. case 0x15:
  7603. case 0x16:
  7604. case 0x17:
  7605. return sax->number_unsigned(static_cast<number_unsigned_t>(current));
  7606. case 0x18: // Unsigned integer (one-byte uint8_t follows)
  7607. {
  7608. std::uint8_t number{};
  7609. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  7610. }
  7611. case 0x19: // Unsigned integer (two-byte uint16_t follows)
  7612. {
  7613. std::uint16_t number{};
  7614. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  7615. }
  7616. case 0x1A: // Unsigned integer (four-byte uint32_t follows)
  7617. {
  7618. std::uint32_t number{};
  7619. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  7620. }
  7621. case 0x1B: // Unsigned integer (eight-byte uint64_t follows)
  7622. {
  7623. std::uint64_t number{};
  7624. return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
  7625. }
  7626. // Negative integer -1-0x00..-1-0x17 (-1..-24)
  7627. case 0x20:
  7628. case 0x21:
  7629. case 0x22:
  7630. case 0x23:
  7631. case 0x24:
  7632. case 0x25:
  7633. case 0x26:
  7634. case 0x27:
  7635. case 0x28:
  7636. case 0x29:
  7637. case 0x2A:
  7638. case 0x2B:
  7639. case 0x2C:
  7640. case 0x2D:
  7641. case 0x2E:
  7642. case 0x2F:
  7643. case 0x30:
  7644. case 0x31:
  7645. case 0x32:
  7646. case 0x33:
  7647. case 0x34:
  7648. case 0x35:
  7649. case 0x36:
  7650. case 0x37:
  7651. return sax->number_integer(static_cast<std::int8_t>(0x20 - 1 - current));
  7652. case 0x38: // Negative integer (one-byte uint8_t follows)
  7653. {
  7654. std::uint8_t number{};
  7655. return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
  7656. }
  7657. case 0x39: // Negative integer -1-n (two-byte uint16_t follows)
  7658. {
  7659. std::uint16_t number{};
  7660. return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
  7661. }
  7662. case 0x3A: // Negative integer -1-n (four-byte uint32_t follows)
  7663. {
  7664. std::uint32_t number{};
  7665. return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
  7666. }
  7667. case 0x3B: // Negative integer -1-n (eight-byte uint64_t follows)
  7668. {
  7669. std::uint64_t number{};
  7670. return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1)
  7671. - static_cast<number_integer_t>(number));
  7672. }
  7673. // Binary data (0x00..0x17 bytes follow)
  7674. case 0x40:
  7675. case 0x41:
  7676. case 0x42:
  7677. case 0x43:
  7678. case 0x44:
  7679. case 0x45:
  7680. case 0x46:
  7681. case 0x47:
  7682. case 0x48:
  7683. case 0x49:
  7684. case 0x4A:
  7685. case 0x4B:
  7686. case 0x4C:
  7687. case 0x4D:
  7688. case 0x4E:
  7689. case 0x4F:
  7690. case 0x50:
  7691. case 0x51:
  7692. case 0x52:
  7693. case 0x53:
  7694. case 0x54:
  7695. case 0x55:
  7696. case 0x56:
  7697. case 0x57:
  7698. case 0x58: // Binary data (one-byte uint8_t for n follows)
  7699. case 0x59: // Binary data (two-byte uint16_t for n follow)
  7700. case 0x5A: // Binary data (four-byte uint32_t for n follow)
  7701. case 0x5B: // Binary data (eight-byte uint64_t for n follow)
  7702. case 0x5F: // Binary data (indefinite length)
  7703. {
  7704. binary_t b;
  7705. return get_cbor_binary(b) && sax->binary(b);
  7706. }
  7707. // UTF-8 string (0x00..0x17 bytes follow)
  7708. case 0x60:
  7709. case 0x61:
  7710. case 0x62:
  7711. case 0x63:
  7712. case 0x64:
  7713. case 0x65:
  7714. case 0x66:
  7715. case 0x67:
  7716. case 0x68:
  7717. case 0x69:
  7718. case 0x6A:
  7719. case 0x6B:
  7720. case 0x6C:
  7721. case 0x6D:
  7722. case 0x6E:
  7723. case 0x6F:
  7724. case 0x70:
  7725. case 0x71:
  7726. case 0x72:
  7727. case 0x73:
  7728. case 0x74:
  7729. case 0x75:
  7730. case 0x76:
  7731. case 0x77:
  7732. case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
  7733. case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
  7734. case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
  7735. case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
  7736. case 0x7F: // UTF-8 string (indefinite length)
  7737. {
  7738. string_t s;
  7739. return get_cbor_string(s) && sax->string(s);
  7740. }
  7741. // array (0x00..0x17 data items follow)
  7742. case 0x80:
  7743. case 0x81:
  7744. case 0x82:
  7745. case 0x83:
  7746. case 0x84:
  7747. case 0x85:
  7748. case 0x86:
  7749. case 0x87:
  7750. case 0x88:
  7751. case 0x89:
  7752. case 0x8A:
  7753. case 0x8B:
  7754. case 0x8C:
  7755. case 0x8D:
  7756. case 0x8E:
  7757. case 0x8F:
  7758. case 0x90:
  7759. case 0x91:
  7760. case 0x92:
  7761. case 0x93:
  7762. case 0x94:
  7763. case 0x95:
  7764. case 0x96:
  7765. case 0x97:
  7766. return get_cbor_array(static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
  7767. case 0x98: // array (one-byte uint8_t for n follows)
  7768. {
  7769. std::uint8_t len{};
  7770. return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
  7771. }
  7772. case 0x99: // array (two-byte uint16_t for n follow)
  7773. {
  7774. std::uint16_t len{};
  7775. return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
  7776. }
  7777. case 0x9A: // array (four-byte uint32_t for n follow)
  7778. {
  7779. std::uint32_t len{};
  7780. return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
  7781. }
  7782. case 0x9B: // array (eight-byte uint64_t for n follow)
  7783. {
  7784. std::uint64_t len{};
  7785. return get_number(input_format_t::cbor, len) && get_cbor_array(detail::conditional_static_cast<std::size_t>(len), tag_handler);
  7786. }
  7787. case 0x9F: // array (indefinite length)
  7788. return get_cbor_array(std::size_t(-1), tag_handler);
  7789. // map (0x00..0x17 pairs of data items follow)
  7790. case 0xA0:
  7791. case 0xA1:
  7792. case 0xA2:
  7793. case 0xA3:
  7794. case 0xA4:
  7795. case 0xA5:
  7796. case 0xA6:
  7797. case 0xA7:
  7798. case 0xA8:
  7799. case 0xA9:
  7800. case 0xAA:
  7801. case 0xAB:
  7802. case 0xAC:
  7803. case 0xAD:
  7804. case 0xAE:
  7805. case 0xAF:
  7806. case 0xB0:
  7807. case 0xB1:
  7808. case 0xB2:
  7809. case 0xB3:
  7810. case 0xB4:
  7811. case 0xB5:
  7812. case 0xB6:
  7813. case 0xB7:
  7814. return get_cbor_object(static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
  7815. case 0xB8: // map (one-byte uint8_t for n follows)
  7816. {
  7817. std::uint8_t len{};
  7818. return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
  7819. }
  7820. case 0xB9: // map (two-byte uint16_t for n follow)
  7821. {
  7822. std::uint16_t len{};
  7823. return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
  7824. }
  7825. case 0xBA: // map (four-byte uint32_t for n follow)
  7826. {
  7827. std::uint32_t len{};
  7828. return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
  7829. }
  7830. case 0xBB: // map (eight-byte uint64_t for n follow)
  7831. {
  7832. std::uint64_t len{};
  7833. return get_number(input_format_t::cbor, len) && get_cbor_object(detail::conditional_static_cast<std::size_t>(len), tag_handler);
  7834. }
  7835. case 0xBF: // map (indefinite length)
  7836. return get_cbor_object(std::size_t(-1), tag_handler);
  7837. case 0xC6: // tagged item
  7838. case 0xC7:
  7839. case 0xC8:
  7840. case 0xC9:
  7841. case 0xCA:
  7842. case 0xCB:
  7843. case 0xCC:
  7844. case 0xCD:
  7845. case 0xCE:
  7846. case 0xCF:
  7847. case 0xD0:
  7848. case 0xD1:
  7849. case 0xD2:
  7850. case 0xD3:
  7851. case 0xD4:
  7852. case 0xD8: // tagged item (1 bytes follow)
  7853. case 0xD9: // tagged item (2 bytes follow)
  7854. case 0xDA: // tagged item (4 bytes follow)
  7855. case 0xDB: // tagged item (8 bytes follow)
  7856. {
  7857. switch (tag_handler)
  7858. {
  7859. case cbor_tag_handler_t::error:
  7860. {
  7861. auto last_token = get_token_string();
  7862. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"), BasicJsonType()));
  7863. }
  7864. case cbor_tag_handler_t::ignore:
  7865. {
  7866. // ignore binary subtype
  7867. switch (current)
  7868. {
  7869. case 0xD8:
  7870. {
  7871. std::uint8_t subtype_to_ignore{};
  7872. get_number(input_format_t::cbor, subtype_to_ignore);
  7873. break;
  7874. }
  7875. case 0xD9:
  7876. {
  7877. std::uint16_t subtype_to_ignore{};
  7878. get_number(input_format_t::cbor, subtype_to_ignore);
  7879. break;
  7880. }
  7881. case 0xDA:
  7882. {
  7883. std::uint32_t subtype_to_ignore{};
  7884. get_number(input_format_t::cbor, subtype_to_ignore);
  7885. break;
  7886. }
  7887. case 0xDB:
  7888. {
  7889. std::uint64_t subtype_to_ignore{};
  7890. get_number(input_format_t::cbor, subtype_to_ignore);
  7891. break;
  7892. }
  7893. default:
  7894. break;
  7895. }
  7896. return parse_cbor_internal(true, tag_handler);
  7897. }
  7898. case cbor_tag_handler_t::store:
  7899. {
  7900. binary_t b;
  7901. // use binary subtype and store in binary container
  7902. switch (current)
  7903. {
  7904. case 0xD8:
  7905. {
  7906. std::uint8_t subtype{};
  7907. get_number(input_format_t::cbor, subtype);
  7908. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  7909. break;
  7910. }
  7911. case 0xD9:
  7912. {
  7913. std::uint16_t subtype{};
  7914. get_number(input_format_t::cbor, subtype);
  7915. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  7916. break;
  7917. }
  7918. case 0xDA:
  7919. {
  7920. std::uint32_t subtype{};
  7921. get_number(input_format_t::cbor, subtype);
  7922. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  7923. break;
  7924. }
  7925. case 0xDB:
  7926. {
  7927. std::uint64_t subtype{};
  7928. get_number(input_format_t::cbor, subtype);
  7929. b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
  7930. break;
  7931. }
  7932. default:
  7933. return parse_cbor_internal(true, tag_handler);
  7934. }
  7935. get();
  7936. return get_cbor_binary(b) && sax->binary(b);
  7937. }
  7938. default: // LCOV_EXCL_LINE
  7939. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  7940. return false; // LCOV_EXCL_LINE
  7941. }
  7942. }
  7943. case 0xF4: // false
  7944. return sax->boolean(false);
  7945. case 0xF5: // true
  7946. return sax->boolean(true);
  7947. case 0xF6: // null
  7948. return sax->null();
  7949. case 0xF9: // Half-Precision Float (two-byte IEEE 754)
  7950. {
  7951. const auto byte1_raw = get();
  7952. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number")))
  7953. {
  7954. return false;
  7955. }
  7956. const auto byte2_raw = get();
  7957. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number")))
  7958. {
  7959. return false;
  7960. }
  7961. const auto byte1 = static_cast<unsigned char>(byte1_raw);
  7962. const auto byte2 = static_cast<unsigned char>(byte2_raw);
  7963. // code from RFC 7049, Appendix D, Figure 3:
  7964. // As half-precision floating-point numbers were only added
  7965. // to IEEE 754 in 2008, today's programming platforms often
  7966. // still only have limited support for them. It is very
  7967. // easy to include at least decoding support for them even
  7968. // without such support. An example of a small decoder for
  7969. // half-precision floating-point numbers in the C language
  7970. // is shown in Fig. 3.
  7971. const auto half = static_cast<unsigned int>((byte1 << 8u) + byte2);
  7972. const double val = [&half]
  7973. {
  7974. const int exp = (half >> 10u) & 0x1Fu;
  7975. const unsigned int mant = half & 0x3FFu;
  7976. JSON_ASSERT(0 <= exp&& exp <= 32);
  7977. JSON_ASSERT(mant <= 1024);
  7978. switch (exp)
  7979. {
  7980. case 0:
  7981. return std::ldexp(mant, -24);
  7982. case 31:
  7983. return (mant == 0)
  7984. ? std::numeric_limits<double>::infinity()
  7985. : std::numeric_limits<double>::quiet_NaN();
  7986. default:
  7987. return std::ldexp(mant + 1024, exp - 25);
  7988. }
  7989. }();
  7990. return sax->number_float((half & 0x8000u) != 0
  7991. ? static_cast<number_float_t>(-val)
  7992. : static_cast<number_float_t>(val), "");
  7993. }
  7994. case 0xFA: // Single-Precision Float (four-byte IEEE 754)
  7995. {
  7996. float number{};
  7997. return get_number(input_format_t::cbor, number) && sax->number_float(static_cast<number_float_t>(number), "");
  7998. }
  7999. case 0xFB: // Double-Precision Float (eight-byte IEEE 754)
  8000. {
  8001. double number{};
  8002. return get_number(input_format_t::cbor, number) && sax->number_float(static_cast<number_float_t>(number), "");
  8003. }
  8004. default: // anything else (0xFF is handled inside the other types)
  8005. {
  8006. auto last_token = get_token_string();
  8007. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"), BasicJsonType()));
  8008. }
  8009. }
  8010. }
  8011. /*!
  8012. @brief reads a CBOR string
  8013. This function first reads starting bytes to determine the expected
  8014. string length and then copies this number of bytes into a string.
  8015. Additionally, CBOR's strings with indefinite lengths are supported.
  8016. @param[out] result created string
  8017. @return whether string creation completed
  8018. */
  8019. bool get_cbor_string(string_t& result)
  8020. {
  8021. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "string")))
  8022. {
  8023. return false;
  8024. }
  8025. switch (current)
  8026. {
  8027. // UTF-8 string (0x00..0x17 bytes follow)
  8028. case 0x60:
  8029. case 0x61:
  8030. case 0x62:
  8031. case 0x63:
  8032. case 0x64:
  8033. case 0x65:
  8034. case 0x66:
  8035. case 0x67:
  8036. case 0x68:
  8037. case 0x69:
  8038. case 0x6A:
  8039. case 0x6B:
  8040. case 0x6C:
  8041. case 0x6D:
  8042. case 0x6E:
  8043. case 0x6F:
  8044. case 0x70:
  8045. case 0x71:
  8046. case 0x72:
  8047. case 0x73:
  8048. case 0x74:
  8049. case 0x75:
  8050. case 0x76:
  8051. case 0x77:
  8052. {
  8053. return get_string(input_format_t::cbor, static_cast<unsigned int>(current) & 0x1Fu, result);
  8054. }
  8055. case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
  8056. {
  8057. std::uint8_t len{};
  8058. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  8059. }
  8060. case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
  8061. {
  8062. std::uint16_t len{};
  8063. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  8064. }
  8065. case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
  8066. {
  8067. std::uint32_t len{};
  8068. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  8069. }
  8070. case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
  8071. {
  8072. std::uint64_t len{};
  8073. return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
  8074. }
  8075. case 0x7F: // UTF-8 string (indefinite length)
  8076. {
  8077. while (get() != 0xFF)
  8078. {
  8079. string_t chunk;
  8080. if (!get_cbor_string(chunk))
  8081. {
  8082. return false;
  8083. }
  8084. result.append(chunk);
  8085. }
  8086. return true;
  8087. }
  8088. default:
  8089. {
  8090. auto last_token = get_token_string();
  8091. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x" + last_token, "string"), BasicJsonType()));
  8092. }
  8093. }
  8094. }
  8095. /*!
  8096. @brief reads a CBOR byte array
  8097. This function first reads starting bytes to determine the expected
  8098. byte array length and then copies this number of bytes into the byte array.
  8099. Additionally, CBOR's byte arrays with indefinite lengths are supported.
  8100. @param[out] result created byte array
  8101. @return whether byte array creation completed
  8102. */
  8103. bool get_cbor_binary(binary_t& result)
  8104. {
  8105. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "binary")))
  8106. {
  8107. return false;
  8108. }
  8109. switch (current)
  8110. {
  8111. // Binary data (0x00..0x17 bytes follow)
  8112. case 0x40:
  8113. case 0x41:
  8114. case 0x42:
  8115. case 0x43:
  8116. case 0x44:
  8117. case 0x45:
  8118. case 0x46:
  8119. case 0x47:
  8120. case 0x48:
  8121. case 0x49:
  8122. case 0x4A:
  8123. case 0x4B:
  8124. case 0x4C:
  8125. case 0x4D:
  8126. case 0x4E:
  8127. case 0x4F:
  8128. case 0x50:
  8129. case 0x51:
  8130. case 0x52:
  8131. case 0x53:
  8132. case 0x54:
  8133. case 0x55:
  8134. case 0x56:
  8135. case 0x57:
  8136. {
  8137. return get_binary(input_format_t::cbor, static_cast<unsigned int>(current) & 0x1Fu, result);
  8138. }
  8139. case 0x58: // Binary data (one-byte uint8_t for n follows)
  8140. {
  8141. std::uint8_t len{};
  8142. return get_number(input_format_t::cbor, len) &&
  8143. get_binary(input_format_t::cbor, len, result);
  8144. }
  8145. case 0x59: // Binary data (two-byte uint16_t for n follow)
  8146. {
  8147. std::uint16_t len{};
  8148. return get_number(input_format_t::cbor, len) &&
  8149. get_binary(input_format_t::cbor, len, result);
  8150. }
  8151. case 0x5A: // Binary data (four-byte uint32_t for n follow)
  8152. {
  8153. std::uint32_t len{};
  8154. return get_number(input_format_t::cbor, len) &&
  8155. get_binary(input_format_t::cbor, len, result);
  8156. }
  8157. case 0x5B: // Binary data (eight-byte uint64_t for n follow)
  8158. {
  8159. std::uint64_t len{};
  8160. return get_number(input_format_t::cbor, len) &&
  8161. get_binary(input_format_t::cbor, len, result);
  8162. }
  8163. case 0x5F: // Binary data (indefinite length)
  8164. {
  8165. while (get() != 0xFF)
  8166. {
  8167. binary_t chunk;
  8168. if (!get_cbor_binary(chunk))
  8169. {
  8170. return false;
  8171. }
  8172. result.insert(result.end(), chunk.begin(), chunk.end());
  8173. }
  8174. return true;
  8175. }
  8176. default:
  8177. {
  8178. auto last_token = get_token_string();
  8179. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x40-0x5B) or indefinite binary array type (0x5F); last byte: 0x" + last_token, "binary"), BasicJsonType()));
  8180. }
  8181. }
  8182. }
  8183. /*!
  8184. @param[in] len the length of the array or std::size_t(-1) for an
  8185. array of indefinite size
  8186. @param[in] tag_handler how CBOR tags should be treated
  8187. @return whether array creation completed
  8188. */
  8189. bool get_cbor_array(const std::size_t len,
  8190. const cbor_tag_handler_t tag_handler)
  8191. {
  8192. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
  8193. {
  8194. return false;
  8195. }
  8196. if (len != std::size_t(-1))
  8197. {
  8198. for (std::size_t i = 0; i < len; ++i)
  8199. {
  8200. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
  8201. {
  8202. return false;
  8203. }
  8204. }
  8205. }
  8206. else
  8207. {
  8208. while (get() != 0xFF)
  8209. {
  8210. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(false, tag_handler)))
  8211. {
  8212. return false;
  8213. }
  8214. }
  8215. }
  8216. return sax->end_array();
  8217. }
  8218. /*!
  8219. @param[in] len the length of the object or std::size_t(-1) for an
  8220. object of indefinite size
  8221. @param[in] tag_handler how CBOR tags should be treated
  8222. @return whether object creation completed
  8223. */
  8224. bool get_cbor_object(const std::size_t len,
  8225. const cbor_tag_handler_t tag_handler)
  8226. {
  8227. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
  8228. {
  8229. return false;
  8230. }
  8231. if (len != 0)
  8232. {
  8233. string_t key;
  8234. if (len != std::size_t(-1))
  8235. {
  8236. for (std::size_t i = 0; i < len; ++i)
  8237. {
  8238. get();
  8239. if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
  8240. {
  8241. return false;
  8242. }
  8243. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
  8244. {
  8245. return false;
  8246. }
  8247. key.clear();
  8248. }
  8249. }
  8250. else
  8251. {
  8252. while (get() != 0xFF)
  8253. {
  8254. if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
  8255. {
  8256. return false;
  8257. }
  8258. if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
  8259. {
  8260. return false;
  8261. }
  8262. key.clear();
  8263. }
  8264. }
  8265. }
  8266. return sax->end_object();
  8267. }
  8268. /////////////
  8269. // MsgPack //
  8270. /////////////
  8271. /*!
  8272. @return whether a valid MessagePack value was passed to the SAX parser
  8273. */
  8274. bool parse_msgpack_internal()
  8275. {
  8276. switch (get())
  8277. {
  8278. // EOF
  8279. case std::char_traits<char_type>::eof():
  8280. return unexpect_eof(input_format_t::msgpack, "value");
  8281. // positive fixint
  8282. case 0x00:
  8283. case 0x01:
  8284. case 0x02:
  8285. case 0x03:
  8286. case 0x04:
  8287. case 0x05:
  8288. case 0x06:
  8289. case 0x07:
  8290. case 0x08:
  8291. case 0x09:
  8292. case 0x0A:
  8293. case 0x0B:
  8294. case 0x0C:
  8295. case 0x0D:
  8296. case 0x0E:
  8297. case 0x0F:
  8298. case 0x10:
  8299. case 0x11:
  8300. case 0x12:
  8301. case 0x13:
  8302. case 0x14:
  8303. case 0x15:
  8304. case 0x16:
  8305. case 0x17:
  8306. case 0x18:
  8307. case 0x19:
  8308. case 0x1A:
  8309. case 0x1B:
  8310. case 0x1C:
  8311. case 0x1D:
  8312. case 0x1E:
  8313. case 0x1F:
  8314. case 0x20:
  8315. case 0x21:
  8316. case 0x22:
  8317. case 0x23:
  8318. case 0x24:
  8319. case 0x25:
  8320. case 0x26:
  8321. case 0x27:
  8322. case 0x28:
  8323. case 0x29:
  8324. case 0x2A:
  8325. case 0x2B:
  8326. case 0x2C:
  8327. case 0x2D:
  8328. case 0x2E:
  8329. case 0x2F:
  8330. case 0x30:
  8331. case 0x31:
  8332. case 0x32:
  8333. case 0x33:
  8334. case 0x34:
  8335. case 0x35:
  8336. case 0x36:
  8337. case 0x37:
  8338. case 0x38:
  8339. case 0x39:
  8340. case 0x3A:
  8341. case 0x3B:
  8342. case 0x3C:
  8343. case 0x3D:
  8344. case 0x3E:
  8345. case 0x3F:
  8346. case 0x40:
  8347. case 0x41:
  8348. case 0x42:
  8349. case 0x43:
  8350. case 0x44:
  8351. case 0x45:
  8352. case 0x46:
  8353. case 0x47:
  8354. case 0x48:
  8355. case 0x49:
  8356. case 0x4A:
  8357. case 0x4B:
  8358. case 0x4C:
  8359. case 0x4D:
  8360. case 0x4E:
  8361. case 0x4F:
  8362. case 0x50:
  8363. case 0x51:
  8364. case 0x52:
  8365. case 0x53:
  8366. case 0x54:
  8367. case 0x55:
  8368. case 0x56:
  8369. case 0x57:
  8370. case 0x58:
  8371. case 0x59:
  8372. case 0x5A:
  8373. case 0x5B:
  8374. case 0x5C:
  8375. case 0x5D:
  8376. case 0x5E:
  8377. case 0x5F:
  8378. case 0x60:
  8379. case 0x61:
  8380. case 0x62:
  8381. case 0x63:
  8382. case 0x64:
  8383. case 0x65:
  8384. case 0x66:
  8385. case 0x67:
  8386. case 0x68:
  8387. case 0x69:
  8388. case 0x6A:
  8389. case 0x6B:
  8390. case 0x6C:
  8391. case 0x6D:
  8392. case 0x6E:
  8393. case 0x6F:
  8394. case 0x70:
  8395. case 0x71:
  8396. case 0x72:
  8397. case 0x73:
  8398. case 0x74:
  8399. case 0x75:
  8400. case 0x76:
  8401. case 0x77:
  8402. case 0x78:
  8403. case 0x79:
  8404. case 0x7A:
  8405. case 0x7B:
  8406. case 0x7C:
  8407. case 0x7D:
  8408. case 0x7E:
  8409. case 0x7F:
  8410. return sax->number_unsigned(static_cast<number_unsigned_t>(current));
  8411. // fixmap
  8412. case 0x80:
  8413. case 0x81:
  8414. case 0x82:
  8415. case 0x83:
  8416. case 0x84:
  8417. case 0x85:
  8418. case 0x86:
  8419. case 0x87:
  8420. case 0x88:
  8421. case 0x89:
  8422. case 0x8A:
  8423. case 0x8B:
  8424. case 0x8C:
  8425. case 0x8D:
  8426. case 0x8E:
  8427. case 0x8F:
  8428. return get_msgpack_object(static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
  8429. // fixarray
  8430. case 0x90:
  8431. case 0x91:
  8432. case 0x92:
  8433. case 0x93:
  8434. case 0x94:
  8435. case 0x95:
  8436. case 0x96:
  8437. case 0x97:
  8438. case 0x98:
  8439. case 0x99:
  8440. case 0x9A:
  8441. case 0x9B:
  8442. case 0x9C:
  8443. case 0x9D:
  8444. case 0x9E:
  8445. case 0x9F:
  8446. return get_msgpack_array(static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
  8447. // fixstr
  8448. case 0xA0:
  8449. case 0xA1:
  8450. case 0xA2:
  8451. case 0xA3:
  8452. case 0xA4:
  8453. case 0xA5:
  8454. case 0xA6:
  8455. case 0xA7:
  8456. case 0xA8:
  8457. case 0xA9:
  8458. case 0xAA:
  8459. case 0xAB:
  8460. case 0xAC:
  8461. case 0xAD:
  8462. case 0xAE:
  8463. case 0xAF:
  8464. case 0xB0:
  8465. case 0xB1:
  8466. case 0xB2:
  8467. case 0xB3:
  8468. case 0xB4:
  8469. case 0xB5:
  8470. case 0xB6:
  8471. case 0xB7:
  8472. case 0xB8:
  8473. case 0xB9:
  8474. case 0xBA:
  8475. case 0xBB:
  8476. case 0xBC:
  8477. case 0xBD:
  8478. case 0xBE:
  8479. case 0xBF:
  8480. case 0xD9: // str 8
  8481. case 0xDA: // str 16
  8482. case 0xDB: // str 32
  8483. {
  8484. string_t s;
  8485. return get_msgpack_string(s) && sax->string(s);
  8486. }
  8487. case 0xC0: // nil
  8488. return sax->null();
  8489. case 0xC2: // false
  8490. return sax->boolean(false);
  8491. case 0xC3: // true
  8492. return sax->boolean(true);
  8493. case 0xC4: // bin 8
  8494. case 0xC5: // bin 16
  8495. case 0xC6: // bin 32
  8496. case 0xC7: // ext 8
  8497. case 0xC8: // ext 16
  8498. case 0xC9: // ext 32
  8499. case 0xD4: // fixext 1
  8500. case 0xD5: // fixext 2
  8501. case 0xD6: // fixext 4
  8502. case 0xD7: // fixext 8
  8503. case 0xD8: // fixext 16
  8504. {
  8505. binary_t b;
  8506. return get_msgpack_binary(b) && sax->binary(b);
  8507. }
  8508. case 0xCA: // float 32
  8509. {
  8510. float number{};
  8511. return get_number(input_format_t::msgpack, number) && sax->number_float(static_cast<number_float_t>(number), "");
  8512. }
  8513. case 0xCB: // float 64
  8514. {
  8515. double number{};
  8516. return get_number(input_format_t::msgpack, number) && sax->number_float(static_cast<number_float_t>(number), "");
  8517. }
  8518. case 0xCC: // uint 8
  8519. {
  8520. std::uint8_t number{};
  8521. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  8522. }
  8523. case 0xCD: // uint 16
  8524. {
  8525. std::uint16_t number{};
  8526. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  8527. }
  8528. case 0xCE: // uint 32
  8529. {
  8530. std::uint32_t number{};
  8531. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  8532. }
  8533. case 0xCF: // uint 64
  8534. {
  8535. std::uint64_t number{};
  8536. return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
  8537. }
  8538. case 0xD0: // int 8
  8539. {
  8540. std::int8_t number{};
  8541. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  8542. }
  8543. case 0xD1: // int 16
  8544. {
  8545. std::int16_t number{};
  8546. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  8547. }
  8548. case 0xD2: // int 32
  8549. {
  8550. std::int32_t number{};
  8551. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  8552. }
  8553. case 0xD3: // int 64
  8554. {
  8555. std::int64_t number{};
  8556. return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
  8557. }
  8558. case 0xDC: // array 16
  8559. {
  8560. std::uint16_t len{};
  8561. return get_number(input_format_t::msgpack, len) && get_msgpack_array(static_cast<std::size_t>(len));
  8562. }
  8563. case 0xDD: // array 32
  8564. {
  8565. std::uint32_t len{};
  8566. return get_number(input_format_t::msgpack, len) && get_msgpack_array(static_cast<std::size_t>(len));
  8567. }
  8568. case 0xDE: // map 16
  8569. {
  8570. std::uint16_t len{};
  8571. return get_number(input_format_t::msgpack, len) && get_msgpack_object(static_cast<std::size_t>(len));
  8572. }
  8573. case 0xDF: // map 32
  8574. {
  8575. std::uint32_t len{};
  8576. return get_number(input_format_t::msgpack, len) && get_msgpack_object(static_cast<std::size_t>(len));
  8577. }
  8578. // negative fixint
  8579. case 0xE0:
  8580. case 0xE1:
  8581. case 0xE2:
  8582. case 0xE3:
  8583. case 0xE4:
  8584. case 0xE5:
  8585. case 0xE6:
  8586. case 0xE7:
  8587. case 0xE8:
  8588. case 0xE9:
  8589. case 0xEA:
  8590. case 0xEB:
  8591. case 0xEC:
  8592. case 0xED:
  8593. case 0xEE:
  8594. case 0xEF:
  8595. case 0xF0:
  8596. case 0xF1:
  8597. case 0xF2:
  8598. case 0xF3:
  8599. case 0xF4:
  8600. case 0xF5:
  8601. case 0xF6:
  8602. case 0xF7:
  8603. case 0xF8:
  8604. case 0xF9:
  8605. case 0xFA:
  8606. case 0xFB:
  8607. case 0xFC:
  8608. case 0xFD:
  8609. case 0xFE:
  8610. case 0xFF:
  8611. return sax->number_integer(static_cast<std::int8_t>(current));
  8612. default: // anything else
  8613. {
  8614. auto last_token = get_token_string();
  8615. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::msgpack, "invalid byte: 0x" + last_token, "value"), BasicJsonType()));
  8616. }
  8617. }
  8618. }
  8619. /*!
  8620. @brief reads a MessagePack string
  8621. This function first reads starting bytes to determine the expected
  8622. string length and then copies this number of bytes into a string.
  8623. @param[out] result created string
  8624. @return whether string creation completed
  8625. */
  8626. bool get_msgpack_string(string_t& result)
  8627. {
  8628. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::msgpack, "string")))
  8629. {
  8630. return false;
  8631. }
  8632. switch (current)
  8633. {
  8634. // fixstr
  8635. case 0xA0:
  8636. case 0xA1:
  8637. case 0xA2:
  8638. case 0xA3:
  8639. case 0xA4:
  8640. case 0xA5:
  8641. case 0xA6:
  8642. case 0xA7:
  8643. case 0xA8:
  8644. case 0xA9:
  8645. case 0xAA:
  8646. case 0xAB:
  8647. case 0xAC:
  8648. case 0xAD:
  8649. case 0xAE:
  8650. case 0xAF:
  8651. case 0xB0:
  8652. case 0xB1:
  8653. case 0xB2:
  8654. case 0xB3:
  8655. case 0xB4:
  8656. case 0xB5:
  8657. case 0xB6:
  8658. case 0xB7:
  8659. case 0xB8:
  8660. case 0xB9:
  8661. case 0xBA:
  8662. case 0xBB:
  8663. case 0xBC:
  8664. case 0xBD:
  8665. case 0xBE:
  8666. case 0xBF:
  8667. {
  8668. return get_string(input_format_t::msgpack, static_cast<unsigned int>(current) & 0x1Fu, result);
  8669. }
  8670. case 0xD9: // str 8
  8671. {
  8672. std::uint8_t len{};
  8673. return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
  8674. }
  8675. case 0xDA: // str 16
  8676. {
  8677. std::uint16_t len{};
  8678. return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
  8679. }
  8680. case 0xDB: // str 32
  8681. {
  8682. std::uint32_t len{};
  8683. return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
  8684. }
  8685. default:
  8686. {
  8687. auto last_token = get_token_string();
  8688. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::msgpack, "expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x" + last_token, "string"), BasicJsonType()));
  8689. }
  8690. }
  8691. }
  8692. /*!
  8693. @brief reads a MessagePack byte array
  8694. This function first reads starting bytes to determine the expected
  8695. byte array length and then copies this number of bytes into a byte array.
  8696. @param[out] result created byte array
  8697. @return whether byte array creation completed
  8698. */
  8699. bool get_msgpack_binary(binary_t& result)
  8700. {
  8701. // helper function to set the subtype
  8702. auto assign_and_return_true = [&result](std::int8_t subtype)
  8703. {
  8704. result.set_subtype(static_cast<std::uint8_t>(subtype));
  8705. return true;
  8706. };
  8707. switch (current)
  8708. {
  8709. case 0xC4: // bin 8
  8710. {
  8711. std::uint8_t len{};
  8712. return get_number(input_format_t::msgpack, len) &&
  8713. get_binary(input_format_t::msgpack, len, result);
  8714. }
  8715. case 0xC5: // bin 16
  8716. {
  8717. std::uint16_t len{};
  8718. return get_number(input_format_t::msgpack, len) &&
  8719. get_binary(input_format_t::msgpack, len, result);
  8720. }
  8721. case 0xC6: // bin 32
  8722. {
  8723. std::uint32_t len{};
  8724. return get_number(input_format_t::msgpack, len) &&
  8725. get_binary(input_format_t::msgpack, len, result);
  8726. }
  8727. case 0xC7: // ext 8
  8728. {
  8729. std::uint8_t len{};
  8730. std::int8_t subtype{};
  8731. return get_number(input_format_t::msgpack, len) &&
  8732. get_number(input_format_t::msgpack, subtype) &&
  8733. get_binary(input_format_t::msgpack, len, result) &&
  8734. assign_and_return_true(subtype);
  8735. }
  8736. case 0xC8: // ext 16
  8737. {
  8738. std::uint16_t len{};
  8739. std::int8_t subtype{};
  8740. return get_number(input_format_t::msgpack, len) &&
  8741. get_number(input_format_t::msgpack, subtype) &&
  8742. get_binary(input_format_t::msgpack, len, result) &&
  8743. assign_and_return_true(subtype);
  8744. }
  8745. case 0xC9: // ext 32
  8746. {
  8747. std::uint32_t len{};
  8748. std::int8_t subtype{};
  8749. return get_number(input_format_t::msgpack, len) &&
  8750. get_number(input_format_t::msgpack, subtype) &&
  8751. get_binary(input_format_t::msgpack, len, result) &&
  8752. assign_and_return_true(subtype);
  8753. }
  8754. case 0xD4: // fixext 1
  8755. {
  8756. std::int8_t subtype{};
  8757. return get_number(input_format_t::msgpack, subtype) &&
  8758. get_binary(input_format_t::msgpack, 1, result) &&
  8759. assign_and_return_true(subtype);
  8760. }
  8761. case 0xD5: // fixext 2
  8762. {
  8763. std::int8_t subtype{};
  8764. return get_number(input_format_t::msgpack, subtype) &&
  8765. get_binary(input_format_t::msgpack, 2, result) &&
  8766. assign_and_return_true(subtype);
  8767. }
  8768. case 0xD6: // fixext 4
  8769. {
  8770. std::int8_t subtype{};
  8771. return get_number(input_format_t::msgpack, subtype) &&
  8772. get_binary(input_format_t::msgpack, 4, result) &&
  8773. assign_and_return_true(subtype);
  8774. }
  8775. case 0xD7: // fixext 8
  8776. {
  8777. std::int8_t subtype{};
  8778. return get_number(input_format_t::msgpack, subtype) &&
  8779. get_binary(input_format_t::msgpack, 8, result) &&
  8780. assign_and_return_true(subtype);
  8781. }
  8782. case 0xD8: // fixext 16
  8783. {
  8784. std::int8_t subtype{};
  8785. return get_number(input_format_t::msgpack, subtype) &&
  8786. get_binary(input_format_t::msgpack, 16, result) &&
  8787. assign_and_return_true(subtype);
  8788. }
  8789. default: // LCOV_EXCL_LINE
  8790. return false; // LCOV_EXCL_LINE
  8791. }
  8792. }
  8793. /*!
  8794. @param[in] len the length of the array
  8795. @return whether array creation completed
  8796. */
  8797. bool get_msgpack_array(const std::size_t len)
  8798. {
  8799. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
  8800. {
  8801. return false;
  8802. }
  8803. for (std::size_t i = 0; i < len; ++i)
  8804. {
  8805. if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
  8806. {
  8807. return false;
  8808. }
  8809. }
  8810. return sax->end_array();
  8811. }
  8812. /*!
  8813. @param[in] len the length of the object
  8814. @return whether object creation completed
  8815. */
  8816. bool get_msgpack_object(const std::size_t len)
  8817. {
  8818. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
  8819. {
  8820. return false;
  8821. }
  8822. string_t key;
  8823. for (std::size_t i = 0; i < len; ++i)
  8824. {
  8825. get();
  8826. if (JSON_HEDLEY_UNLIKELY(!get_msgpack_string(key) || !sax->key(key)))
  8827. {
  8828. return false;
  8829. }
  8830. if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
  8831. {
  8832. return false;
  8833. }
  8834. key.clear();
  8835. }
  8836. return sax->end_object();
  8837. }
  8838. ////////////
  8839. // UBJSON //
  8840. ////////////
  8841. /*!
  8842. @param[in] get_char whether a new character should be retrieved from the
  8843. input (true, default) or whether the last read
  8844. character should be considered instead
  8845. @return whether a valid UBJSON value was passed to the SAX parser
  8846. */
  8847. bool parse_ubjson_internal(const bool get_char = true)
  8848. {
  8849. return get_ubjson_value(get_char ? get_ignore_noop() : current);
  8850. }
  8851. /*!
  8852. @brief reads a UBJSON string
  8853. This function is either called after reading the 'S' byte explicitly
  8854. indicating a string, or in case of an object key where the 'S' byte can be
  8855. left out.
  8856. @param[out] result created string
  8857. @param[in] get_char whether a new character should be retrieved from the
  8858. input (true, default) or whether the last read
  8859. character should be considered instead
  8860. @return whether string creation completed
  8861. */
  8862. bool get_ubjson_string(string_t& result, const bool get_char = true)
  8863. {
  8864. if (get_char)
  8865. {
  8866. get(); // TODO(niels): may we ignore N here?
  8867. }
  8868. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::ubjson, "value")))
  8869. {
  8870. return false;
  8871. }
  8872. switch (current)
  8873. {
  8874. case 'U':
  8875. {
  8876. std::uint8_t len{};
  8877. return get_number(input_format_t::ubjson, len) && get_string(input_format_t::ubjson, len, result);
  8878. }
  8879. case 'i':
  8880. {
  8881. std::int8_t len{};
  8882. return get_number(input_format_t::ubjson, len) && get_string(input_format_t::ubjson, len, result);
  8883. }
  8884. case 'I':
  8885. {
  8886. std::int16_t len{};
  8887. return get_number(input_format_t::ubjson, len) && get_string(input_format_t::ubjson, len, result);
  8888. }
  8889. case 'l':
  8890. {
  8891. std::int32_t len{};
  8892. return get_number(input_format_t::ubjson, len) && get_string(input_format_t::ubjson, len, result);
  8893. }
  8894. case 'L':
  8895. {
  8896. std::int64_t len{};
  8897. return get_number(input_format_t::ubjson, len) && get_string(input_format_t::ubjson, len, result);
  8898. }
  8899. default:
  8900. auto last_token = get_token_string();
  8901. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token, "string"), BasicJsonType()));
  8902. }
  8903. }
  8904. /*!
  8905. @param[out] result determined size
  8906. @return whether size determination completed
  8907. */
  8908. bool get_ubjson_size_value(std::size_t& result)
  8909. {
  8910. switch (get_ignore_noop())
  8911. {
  8912. case 'U':
  8913. {
  8914. std::uint8_t number{};
  8915. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format_t::ubjson, number)))
  8916. {
  8917. return false;
  8918. }
  8919. result = static_cast<std::size_t>(number);
  8920. return true;
  8921. }
  8922. case 'i':
  8923. {
  8924. std::int8_t number{};
  8925. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format_t::ubjson, number)))
  8926. {
  8927. return false;
  8928. }
  8929. result = static_cast<std::size_t>(number); // NOLINT(bugprone-signed-char-misuse,cert-str34-c): number is not a char
  8930. return true;
  8931. }
  8932. case 'I':
  8933. {
  8934. std::int16_t number{};
  8935. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format_t::ubjson, number)))
  8936. {
  8937. return false;
  8938. }
  8939. result = static_cast<std::size_t>(number);
  8940. return true;
  8941. }
  8942. case 'l':
  8943. {
  8944. std::int32_t number{};
  8945. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format_t::ubjson, number)))
  8946. {
  8947. return false;
  8948. }
  8949. result = static_cast<std::size_t>(number);
  8950. return true;
  8951. }
  8952. case 'L':
  8953. {
  8954. std::int64_t number{};
  8955. if (JSON_HEDLEY_UNLIKELY(!get_number(input_format_t::ubjson, number)))
  8956. {
  8957. return false;
  8958. }
  8959. result = static_cast<std::size_t>(number);
  8960. return true;
  8961. }
  8962. default:
  8963. {
  8964. auto last_token = get_token_string();
  8965. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token, "size"), BasicJsonType()));
  8966. }
  8967. }
  8968. }
  8969. /*!
  8970. @brief determine the type and size for a container
  8971. In the optimized UBJSON format, a type and a size can be provided to allow
  8972. for a more compact representation.
  8973. @param[out] result pair of the size and the type
  8974. @return whether pair creation completed
  8975. */
  8976. bool get_ubjson_size_type(std::pair<std::size_t, char_int_type>& result)
  8977. {
  8978. result.first = string_t::npos; // size
  8979. result.second = 0; // type
  8980. get_ignore_noop();
  8981. if (current == '$')
  8982. {
  8983. result.second = get(); // must not ignore 'N', because 'N' maybe the type
  8984. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::ubjson, "type")))
  8985. {
  8986. return false;
  8987. }
  8988. get_ignore_noop();
  8989. if (JSON_HEDLEY_UNLIKELY(current != '#'))
  8990. {
  8991. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::ubjson, "value")))
  8992. {
  8993. return false;
  8994. }
  8995. auto last_token = get_token_string();
  8996. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "expected '#' after type information; last byte: 0x" + last_token, "size"), BasicJsonType()));
  8997. }
  8998. return get_ubjson_size_value(result.first);
  8999. }
  9000. if (current == '#')
  9001. {
  9002. return get_ubjson_size_value(result.first);
  9003. }
  9004. return true;
  9005. }
  9006. /*!
  9007. @param prefix the previously read or set type prefix
  9008. @return whether value creation completed
  9009. */
  9010. bool get_ubjson_value(const char_int_type prefix)
  9011. {
  9012. switch (prefix)
  9013. {
  9014. case std::char_traits<char_type>::eof(): // EOF
  9015. return unexpect_eof(input_format_t::ubjson, "value");
  9016. case 'T': // true
  9017. return sax->boolean(true);
  9018. case 'F': // false
  9019. return sax->boolean(false);
  9020. case 'Z': // null
  9021. return sax->null();
  9022. case 'U':
  9023. {
  9024. std::uint8_t number{};
  9025. return get_number(input_format_t::ubjson, number) && sax->number_unsigned(number);
  9026. }
  9027. case 'i':
  9028. {
  9029. std::int8_t number{};
  9030. return get_number(input_format_t::ubjson, number) && sax->number_integer(number);
  9031. }
  9032. case 'I':
  9033. {
  9034. std::int16_t number{};
  9035. return get_number(input_format_t::ubjson, number) && sax->number_integer(number);
  9036. }
  9037. case 'l':
  9038. {
  9039. std::int32_t number{};
  9040. return get_number(input_format_t::ubjson, number) && sax->number_integer(number);
  9041. }
  9042. case 'L':
  9043. {
  9044. std::int64_t number{};
  9045. return get_number(input_format_t::ubjson, number) && sax->number_integer(number);
  9046. }
  9047. case 'd':
  9048. {
  9049. float number{};
  9050. return get_number(input_format_t::ubjson, number) && sax->number_float(static_cast<number_float_t>(number), "");
  9051. }
  9052. case 'D':
  9053. {
  9054. double number{};
  9055. return get_number(input_format_t::ubjson, number) && sax->number_float(static_cast<number_float_t>(number), "");
  9056. }
  9057. case 'H':
  9058. {
  9059. return get_ubjson_high_precision_number();
  9060. }
  9061. case 'C': // char
  9062. {
  9063. get();
  9064. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::ubjson, "char")))
  9065. {
  9066. return false;
  9067. }
  9068. if (JSON_HEDLEY_UNLIKELY(current > 127))
  9069. {
  9070. auto last_token = get_token_string();
  9071. return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "byte after 'C' must be in range 0x00..0x7F; last byte: 0x" + last_token, "char"), BasicJsonType()));
  9072. }
  9073. string_t s(1, static_cast<typename string_t::value_type>(current));
  9074. return sax->string(s);
  9075. }
  9076. case 'S': // string
  9077. {
  9078. string_t s;
  9079. return get_ubjson_string(s) && sax->string(s);
  9080. }
  9081. case '[': // array
  9082. return get_ubjson_array();
  9083. case '{': // object
  9084. return get_ubjson_object();
  9085. default: // anything else
  9086. {
  9087. auto last_token = get_token_string();
  9088. return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "invalid byte: 0x" + last_token, "value"), BasicJsonType()));
  9089. }
  9090. }
  9091. }
  9092. /*!
  9093. @return whether array creation completed
  9094. */
  9095. bool get_ubjson_array()
  9096. {
  9097. std::pair<std::size_t, char_int_type> size_and_type;
  9098. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type)))
  9099. {
  9100. return false;
  9101. }
  9102. if (size_and_type.first != string_t::npos)
  9103. {
  9104. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(size_and_type.first)))
  9105. {
  9106. return false;
  9107. }
  9108. if (size_and_type.second != 0)
  9109. {
  9110. if (size_and_type.second != 'N')
  9111. {
  9112. for (std::size_t i = 0; i < size_and_type.first; ++i)
  9113. {
  9114. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
  9115. {
  9116. return false;
  9117. }
  9118. }
  9119. }
  9120. }
  9121. else
  9122. {
  9123. for (std::size_t i = 0; i < size_and_type.first; ++i)
  9124. {
  9125. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
  9126. {
  9127. return false;
  9128. }
  9129. }
  9130. }
  9131. }
  9132. else
  9133. {
  9134. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(std::size_t(-1))))
  9135. {
  9136. return false;
  9137. }
  9138. while (current != ']')
  9139. {
  9140. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal(false)))
  9141. {
  9142. return false;
  9143. }
  9144. get_ignore_noop();
  9145. }
  9146. }
  9147. return sax->end_array();
  9148. }
  9149. /*!
  9150. @return whether object creation completed
  9151. */
  9152. bool get_ubjson_object()
  9153. {
  9154. std::pair<std::size_t, char_int_type> size_and_type;
  9155. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type)))
  9156. {
  9157. return false;
  9158. }
  9159. string_t key;
  9160. if (size_and_type.first != string_t::npos)
  9161. {
  9162. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(size_and_type.first)))
  9163. {
  9164. return false;
  9165. }
  9166. if (size_and_type.second != 0)
  9167. {
  9168. for (std::size_t i = 0; i < size_and_type.first; ++i)
  9169. {
  9170. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
  9171. {
  9172. return false;
  9173. }
  9174. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
  9175. {
  9176. return false;
  9177. }
  9178. key.clear();
  9179. }
  9180. }
  9181. else
  9182. {
  9183. for (std::size_t i = 0; i < size_and_type.first; ++i)
  9184. {
  9185. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
  9186. {
  9187. return false;
  9188. }
  9189. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
  9190. {
  9191. return false;
  9192. }
  9193. key.clear();
  9194. }
  9195. }
  9196. }
  9197. else
  9198. {
  9199. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(std::size_t(-1))))
  9200. {
  9201. return false;
  9202. }
  9203. while (current != '}')
  9204. {
  9205. if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key, false) || !sax->key(key)))
  9206. {
  9207. return false;
  9208. }
  9209. if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
  9210. {
  9211. return false;
  9212. }
  9213. get_ignore_noop();
  9214. key.clear();
  9215. }
  9216. }
  9217. return sax->end_object();
  9218. }
  9219. // Note, no reader for UBJSON binary types is implemented because they do
  9220. // not exist
  9221. bool get_ubjson_high_precision_number()
  9222. {
  9223. // get size of following number string
  9224. std::size_t size{};
  9225. auto res = get_ubjson_size_value(size);
  9226. if (JSON_HEDLEY_UNLIKELY(!res))
  9227. {
  9228. return res;
  9229. }
  9230. // get number string
  9231. std::vector<char> number_vector;
  9232. for (std::size_t i = 0; i < size; ++i)
  9233. {
  9234. get();
  9235. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::ubjson, "number")))
  9236. {
  9237. return false;
  9238. }
  9239. number_vector.push_back(static_cast<char>(current));
  9240. }
  9241. // parse number string
  9242. using ia_type = decltype(detail::input_adapter(number_vector));
  9243. auto number_lexer = detail::lexer<BasicJsonType, ia_type>(detail::input_adapter(number_vector), false);
  9244. const auto result_number = number_lexer.scan();
  9245. const auto number_string = number_lexer.get_token_string();
  9246. const auto result_remainder = number_lexer.scan();
  9247. using token_type = typename detail::lexer_base<BasicJsonType>::token_type;
  9248. if (JSON_HEDLEY_UNLIKELY(result_remainder != token_type::end_of_input))
  9249. {
  9250. return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, exception_message(input_format_t::ubjson, "invalid number text: " + number_lexer.get_token_string(), "high-precision number"), BasicJsonType()));
  9251. }
  9252. switch (result_number)
  9253. {
  9254. case token_type::value_integer:
  9255. return sax->number_integer(number_lexer.get_number_integer());
  9256. case token_type::value_unsigned:
  9257. return sax->number_unsigned(number_lexer.get_number_unsigned());
  9258. case token_type::value_float:
  9259. return sax->number_float(number_lexer.get_number_float(), std::move(number_string));
  9260. case token_type::uninitialized:
  9261. case token_type::literal_true:
  9262. case token_type::literal_false:
  9263. case token_type::literal_null:
  9264. case token_type::value_string:
  9265. case token_type::begin_array:
  9266. case token_type::begin_object:
  9267. case token_type::end_array:
  9268. case token_type::end_object:
  9269. case token_type::name_separator:
  9270. case token_type::value_separator:
  9271. case token_type::parse_error:
  9272. case token_type::end_of_input:
  9273. case token_type::literal_or_value:
  9274. default:
  9275. return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, exception_message(input_format_t::ubjson, "invalid number text: " + number_lexer.get_token_string(), "high-precision number"), BasicJsonType()));
  9276. }
  9277. }
  9278. ///////////////////////
  9279. // Utility functions //
  9280. ///////////////////////
  9281. /*!
  9282. @brief get next character from the input
  9283. This function provides the interface to the used input adapter. It does
  9284. not throw in case the input reached EOF, but returns a -'ve valued
  9285. `std::char_traits<char_type>::eof()` in that case.
  9286. @return character read from the input
  9287. */
  9288. char_int_type get()
  9289. {
  9290. ++chars_read;
  9291. return current = ia.get_character();
  9292. }
  9293. /*!
  9294. @return character read from the input after ignoring all 'N' entries
  9295. */
  9296. char_int_type get_ignore_noop()
  9297. {
  9298. do
  9299. {
  9300. get();
  9301. }
  9302. while (current == 'N');
  9303. return current;
  9304. }
  9305. /*
  9306. @brief read a number from the input
  9307. @tparam NumberType the type of the number
  9308. @param[in] format the current format (for diagnostics)
  9309. @param[out] result number of type @a NumberType
  9310. @return whether conversion completed
  9311. @note This function needs to respect the system's endianess, because
  9312. bytes in CBOR, MessagePack, and UBJSON are stored in network order
  9313. (big endian) and therefore need reordering on little endian systems.
  9314. */
  9315. template<typename NumberType, bool InputIsLittleEndian = false>
  9316. bool get_number(const input_format_t format, NumberType& result)
  9317. {
  9318. // step 1: read input into array with system's byte order
  9319. std::array<std::uint8_t, sizeof(NumberType)> vec{};
  9320. for (std::size_t i = 0; i < sizeof(NumberType); ++i)
  9321. {
  9322. get();
  9323. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "number")))
  9324. {
  9325. return false;
  9326. }
  9327. // reverse byte order prior to conversion if necessary
  9328. if (is_little_endian != InputIsLittleEndian)
  9329. {
  9330. vec[sizeof(NumberType) - i - 1] = static_cast<std::uint8_t>(current);
  9331. }
  9332. else
  9333. {
  9334. vec[i] = static_cast<std::uint8_t>(current); // LCOV_EXCL_LINE
  9335. }
  9336. }
  9337. // step 2: convert array into number of type T and return
  9338. std::memcpy(&result, vec.data(), sizeof(NumberType));
  9339. return true;
  9340. }
  9341. /*!
  9342. @brief create a string by reading characters from the input
  9343. @tparam NumberType the type of the number
  9344. @param[in] format the current format (for diagnostics)
  9345. @param[in] len number of characters to read
  9346. @param[out] result string created by reading @a len bytes
  9347. @return whether string creation completed
  9348. @note We can not reserve @a len bytes for the result, because @a len
  9349. may be too large. Usually, @ref unexpect_eof() detects the end of
  9350. the input before we run out of string memory.
  9351. */
  9352. template<typename NumberType>
  9353. bool get_string(const input_format_t format,
  9354. const NumberType len,
  9355. string_t& result)
  9356. {
  9357. bool success = true;
  9358. for (NumberType i = 0; i < len; i++)
  9359. {
  9360. get();
  9361. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "string")))
  9362. {
  9363. success = false;
  9364. break;
  9365. }
  9366. result.push_back(static_cast<typename string_t::value_type>(current));
  9367. }
  9368. return success;
  9369. }
  9370. /*!
  9371. @brief create a byte array by reading bytes from the input
  9372. @tparam NumberType the type of the number
  9373. @param[in] format the current format (for diagnostics)
  9374. @param[in] len number of bytes to read
  9375. @param[out] result byte array created by reading @a len bytes
  9376. @return whether byte array creation completed
  9377. @note We can not reserve @a len bytes for the result, because @a len
  9378. may be too large. Usually, @ref unexpect_eof() detects the end of
  9379. the input before we run out of memory.
  9380. */
  9381. template<typename NumberType>
  9382. bool get_binary(const input_format_t format,
  9383. const NumberType len,
  9384. binary_t& result)
  9385. {
  9386. bool success = true;
  9387. for (NumberType i = 0; i < len; i++)
  9388. {
  9389. get();
  9390. if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "binary")))
  9391. {
  9392. success = false;
  9393. break;
  9394. }
  9395. result.push_back(static_cast<std::uint8_t>(current));
  9396. }
  9397. return success;
  9398. }
  9399. /*!
  9400. @param[in] format the current format (for diagnostics)
  9401. @param[in] context further context information (for diagnostics)
  9402. @return whether the last read character is not EOF
  9403. */
  9404. JSON_HEDLEY_NON_NULL(3)
  9405. bool unexpect_eof(const input_format_t format, const char* context) const
  9406. {
  9407. if (JSON_HEDLEY_UNLIKELY(current == std::char_traits<char_type>::eof()))
  9408. {
  9409. return sax->parse_error(chars_read, "<end of file>",
  9410. parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), BasicJsonType()));
  9411. }
  9412. return true;
  9413. }
  9414. /*!
  9415. @return a string representation of the last read byte
  9416. */
  9417. std::string get_token_string() const
  9418. {
  9419. std::array<char, 3> cr{{}};
  9420. (std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(current)); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  9421. return std::string{cr.data()};
  9422. }
  9423. /*!
  9424. @param[in] format the current format
  9425. @param[in] detail a detailed error message
  9426. @param[in] context further context information
  9427. @return a message string to use in the parse_error exceptions
  9428. */
  9429. std::string exception_message(const input_format_t format,
  9430. const std::string& detail,
  9431. const std::string& context) const
  9432. {
  9433. std::string error_msg = "syntax error while parsing ";
  9434. switch (format)
  9435. {
  9436. case input_format_t::cbor:
  9437. error_msg += "CBOR";
  9438. break;
  9439. case input_format_t::msgpack:
  9440. error_msg += "MessagePack";
  9441. break;
  9442. case input_format_t::ubjson:
  9443. error_msg += "UBJSON";
  9444. break;
  9445. case input_format_t::bson:
  9446. error_msg += "BSON";
  9447. break;
  9448. case input_format_t::json: // LCOV_EXCL_LINE
  9449. default: // LCOV_EXCL_LINE
  9450. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  9451. }
  9452. return error_msg + " " + context + ": " + detail;
  9453. }
  9454. private:
  9455. /// input adapter
  9456. InputAdapterType ia;
  9457. /// the current character
  9458. char_int_type current = std::char_traits<char_type>::eof();
  9459. /// the number of characters read
  9460. std::size_t chars_read = 0;
  9461. /// whether we can assume little endianess
  9462. const bool is_little_endian = little_endianess();
  9463. /// the SAX parser
  9464. json_sax_t* sax = nullptr;
  9465. };
  9466. } // namespace detail
  9467. } // namespace nlohmann
  9468. // #include <nlohmann/detail/input/input_adapters.hpp>
  9469. // #include <nlohmann/detail/input/lexer.hpp>
  9470. // #include <nlohmann/detail/input/parser.hpp>
  9471. #include <cmath> // isfinite
  9472. #include <cstdint> // uint8_t
  9473. #include <functional> // function
  9474. #include <string> // string
  9475. #include <utility> // move
  9476. #include <vector> // vector
  9477. // #include <nlohmann/detail/exceptions.hpp>
  9478. // #include <nlohmann/detail/input/input_adapters.hpp>
  9479. // #include <nlohmann/detail/input/json_sax.hpp>
  9480. // #include <nlohmann/detail/input/lexer.hpp>
  9481. // #include <nlohmann/detail/macro_scope.hpp>
  9482. // #include <nlohmann/detail/meta/is_sax.hpp>
  9483. // #include <nlohmann/detail/value_t.hpp>
  9484. namespace nlohmann
  9485. {
  9486. namespace detail
  9487. {
  9488. ////////////
  9489. // parser //
  9490. ////////////
  9491. enum class parse_event_t : std::uint8_t
  9492. {
  9493. /// the parser read `{` and started to process a JSON object
  9494. object_start,
  9495. /// the parser read `}` and finished processing a JSON object
  9496. object_end,
  9497. /// the parser read `[` and started to process a JSON array
  9498. array_start,
  9499. /// the parser read `]` and finished processing a JSON array
  9500. array_end,
  9501. /// the parser read a key of a value in an object
  9502. key,
  9503. /// the parser finished reading a JSON value
  9504. value
  9505. };
  9506. template<typename BasicJsonType>
  9507. using parser_callback_t =
  9508. std::function<bool(int /*depth*/, parse_event_t /*event*/, BasicJsonType& /*parsed*/)>;
  9509. /*!
  9510. @brief syntax analysis
  9511. This class implements a recursive descent parser.
  9512. */
  9513. template<typename BasicJsonType, typename InputAdapterType>
  9514. class parser
  9515. {
  9516. using number_integer_t = typename BasicJsonType::number_integer_t;
  9517. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  9518. using number_float_t = typename BasicJsonType::number_float_t;
  9519. using string_t = typename BasicJsonType::string_t;
  9520. using lexer_t = lexer<BasicJsonType, InputAdapterType>;
  9521. using token_type = typename lexer_t::token_type;
  9522. public:
  9523. /// a parser reading from an input adapter
  9524. explicit parser(InputAdapterType&& adapter,
  9525. const parser_callback_t<BasicJsonType> cb = nullptr,
  9526. const bool allow_exceptions_ = true,
  9527. const bool skip_comments = false)
  9528. : callback(cb)
  9529. , m_lexer(std::move(adapter), skip_comments)
  9530. , allow_exceptions(allow_exceptions_)
  9531. {
  9532. // read first token
  9533. get_token();
  9534. }
  9535. /*!
  9536. @brief public parser interface
  9537. @param[in] strict whether to expect the last token to be EOF
  9538. @param[in,out] result parsed JSON value
  9539. @throw parse_error.101 in case of an unexpected token
  9540. @throw parse_error.102 if to_unicode fails or surrogate error
  9541. @throw parse_error.103 if to_unicode fails
  9542. */
  9543. void parse(const bool strict, BasicJsonType& result)
  9544. {
  9545. if (callback)
  9546. {
  9547. json_sax_dom_callback_parser<BasicJsonType> sdp(result, callback, allow_exceptions);
  9548. sax_parse_internal(&sdp);
  9549. // in strict mode, input must be completely read
  9550. if (strict && (get_token() != token_type::end_of_input))
  9551. {
  9552. sdp.parse_error(m_lexer.get_position(),
  9553. m_lexer.get_token_string(),
  9554. parse_error::create(101, m_lexer.get_position(),
  9555. exception_message(token_type::end_of_input, "value"), BasicJsonType()));
  9556. }
  9557. // in case of an error, return discarded value
  9558. if (sdp.is_errored())
  9559. {
  9560. result = value_t::discarded;
  9561. return;
  9562. }
  9563. // set top-level value to null if it was discarded by the callback
  9564. // function
  9565. if (result.is_discarded())
  9566. {
  9567. result = nullptr;
  9568. }
  9569. }
  9570. else
  9571. {
  9572. json_sax_dom_parser<BasicJsonType> sdp(result, allow_exceptions);
  9573. sax_parse_internal(&sdp);
  9574. // in strict mode, input must be completely read
  9575. if (strict && (get_token() != token_type::end_of_input))
  9576. {
  9577. sdp.parse_error(m_lexer.get_position(),
  9578. m_lexer.get_token_string(),
  9579. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), BasicJsonType()));
  9580. }
  9581. // in case of an error, return discarded value
  9582. if (sdp.is_errored())
  9583. {
  9584. result = value_t::discarded;
  9585. return;
  9586. }
  9587. }
  9588. result.assert_invariant();
  9589. }
  9590. /*!
  9591. @brief public accept interface
  9592. @param[in] strict whether to expect the last token to be EOF
  9593. @return whether the input is a proper JSON text
  9594. */
  9595. bool accept(const bool strict = true)
  9596. {
  9597. json_sax_acceptor<BasicJsonType> sax_acceptor;
  9598. return sax_parse(&sax_acceptor, strict);
  9599. }
  9600. template<typename SAX>
  9601. JSON_HEDLEY_NON_NULL(2)
  9602. bool sax_parse(SAX* sax, const bool strict = true)
  9603. {
  9604. (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
  9605. const bool result = sax_parse_internal(sax);
  9606. // strict mode: next byte must be EOF
  9607. if (result && strict && (get_token() != token_type::end_of_input))
  9608. {
  9609. return sax->parse_error(m_lexer.get_position(),
  9610. m_lexer.get_token_string(),
  9611. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), BasicJsonType()));
  9612. }
  9613. return result;
  9614. }
  9615. private:
  9616. template<typename SAX>
  9617. JSON_HEDLEY_NON_NULL(2)
  9618. bool sax_parse_internal(SAX* sax)
  9619. {
  9620. // stack to remember the hierarchy of structured values we are parsing
  9621. // true = array; false = object
  9622. std::vector<bool> states;
  9623. // value to avoid a goto (see comment where set to true)
  9624. bool skip_to_state_evaluation = false;
  9625. while (true)
  9626. {
  9627. if (!skip_to_state_evaluation)
  9628. {
  9629. // invariant: get_token() was called before each iteration
  9630. switch (last_token)
  9631. {
  9632. case token_type::begin_object:
  9633. {
  9634. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(std::size_t(-1))))
  9635. {
  9636. return false;
  9637. }
  9638. // closing } -> we are done
  9639. if (get_token() == token_type::end_object)
  9640. {
  9641. if (JSON_HEDLEY_UNLIKELY(!sax->end_object()))
  9642. {
  9643. return false;
  9644. }
  9645. break;
  9646. }
  9647. // parse key
  9648. if (JSON_HEDLEY_UNLIKELY(last_token != token_type::value_string))
  9649. {
  9650. return sax->parse_error(m_lexer.get_position(),
  9651. m_lexer.get_token_string(),
  9652. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), BasicJsonType()));
  9653. }
  9654. if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string())))
  9655. {
  9656. return false;
  9657. }
  9658. // parse separator (:)
  9659. if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
  9660. {
  9661. return sax->parse_error(m_lexer.get_position(),
  9662. m_lexer.get_token_string(),
  9663. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), BasicJsonType()));
  9664. }
  9665. // remember we are now inside an object
  9666. states.push_back(false);
  9667. // parse values
  9668. get_token();
  9669. continue;
  9670. }
  9671. case token_type::begin_array:
  9672. {
  9673. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(std::size_t(-1))))
  9674. {
  9675. return false;
  9676. }
  9677. // closing ] -> we are done
  9678. if (get_token() == token_type::end_array)
  9679. {
  9680. if (JSON_HEDLEY_UNLIKELY(!sax->end_array()))
  9681. {
  9682. return false;
  9683. }
  9684. break;
  9685. }
  9686. // remember we are now inside an array
  9687. states.push_back(true);
  9688. // parse values (no need to call get_token)
  9689. continue;
  9690. }
  9691. case token_type::value_float:
  9692. {
  9693. const auto res = m_lexer.get_number_float();
  9694. if (JSON_HEDLEY_UNLIKELY(!std::isfinite(res)))
  9695. {
  9696. return sax->parse_error(m_lexer.get_position(),
  9697. m_lexer.get_token_string(),
  9698. out_of_range::create(406, "number overflow parsing '" + m_lexer.get_token_string() + "'", BasicJsonType()));
  9699. }
  9700. if (JSON_HEDLEY_UNLIKELY(!sax->number_float(res, m_lexer.get_string())))
  9701. {
  9702. return false;
  9703. }
  9704. break;
  9705. }
  9706. case token_type::literal_false:
  9707. {
  9708. if (JSON_HEDLEY_UNLIKELY(!sax->boolean(false)))
  9709. {
  9710. return false;
  9711. }
  9712. break;
  9713. }
  9714. case token_type::literal_null:
  9715. {
  9716. if (JSON_HEDLEY_UNLIKELY(!sax->null()))
  9717. {
  9718. return false;
  9719. }
  9720. break;
  9721. }
  9722. case token_type::literal_true:
  9723. {
  9724. if (JSON_HEDLEY_UNLIKELY(!sax->boolean(true)))
  9725. {
  9726. return false;
  9727. }
  9728. break;
  9729. }
  9730. case token_type::value_integer:
  9731. {
  9732. if (JSON_HEDLEY_UNLIKELY(!sax->number_integer(m_lexer.get_number_integer())))
  9733. {
  9734. return false;
  9735. }
  9736. break;
  9737. }
  9738. case token_type::value_string:
  9739. {
  9740. if (JSON_HEDLEY_UNLIKELY(!sax->string(m_lexer.get_string())))
  9741. {
  9742. return false;
  9743. }
  9744. break;
  9745. }
  9746. case token_type::value_unsigned:
  9747. {
  9748. if (JSON_HEDLEY_UNLIKELY(!sax->number_unsigned(m_lexer.get_number_unsigned())))
  9749. {
  9750. return false;
  9751. }
  9752. break;
  9753. }
  9754. case token_type::parse_error:
  9755. {
  9756. // using "uninitialized" to avoid "expected" message
  9757. return sax->parse_error(m_lexer.get_position(),
  9758. m_lexer.get_token_string(),
  9759. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::uninitialized, "value"), BasicJsonType()));
  9760. }
  9761. case token_type::uninitialized:
  9762. case token_type::end_array:
  9763. case token_type::end_object:
  9764. case token_type::name_separator:
  9765. case token_type::value_separator:
  9766. case token_type::end_of_input:
  9767. case token_type::literal_or_value:
  9768. default: // the last token was unexpected
  9769. {
  9770. return sax->parse_error(m_lexer.get_position(),
  9771. m_lexer.get_token_string(),
  9772. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::literal_or_value, "value"), BasicJsonType()));
  9773. }
  9774. }
  9775. }
  9776. else
  9777. {
  9778. skip_to_state_evaluation = false;
  9779. }
  9780. // we reached this line after we successfully parsed a value
  9781. if (states.empty())
  9782. {
  9783. // empty stack: we reached the end of the hierarchy: done
  9784. return true;
  9785. }
  9786. if (states.back()) // array
  9787. {
  9788. // comma -> next value
  9789. if (get_token() == token_type::value_separator)
  9790. {
  9791. // parse a new value
  9792. get_token();
  9793. continue;
  9794. }
  9795. // closing ]
  9796. if (JSON_HEDLEY_LIKELY(last_token == token_type::end_array))
  9797. {
  9798. if (JSON_HEDLEY_UNLIKELY(!sax->end_array()))
  9799. {
  9800. return false;
  9801. }
  9802. // We are done with this array. Before we can parse a
  9803. // new value, we need to evaluate the new state first.
  9804. // By setting skip_to_state_evaluation to false, we
  9805. // are effectively jumping to the beginning of this if.
  9806. JSON_ASSERT(!states.empty());
  9807. states.pop_back();
  9808. skip_to_state_evaluation = true;
  9809. continue;
  9810. }
  9811. return sax->parse_error(m_lexer.get_position(),
  9812. m_lexer.get_token_string(),
  9813. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_array, "array"), BasicJsonType()));
  9814. }
  9815. // states.back() is false -> object
  9816. // comma -> next value
  9817. if (get_token() == token_type::value_separator)
  9818. {
  9819. // parse key
  9820. if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::value_string))
  9821. {
  9822. return sax->parse_error(m_lexer.get_position(),
  9823. m_lexer.get_token_string(),
  9824. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), BasicJsonType()));
  9825. }
  9826. if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string())))
  9827. {
  9828. return false;
  9829. }
  9830. // parse separator (:)
  9831. if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
  9832. {
  9833. return sax->parse_error(m_lexer.get_position(),
  9834. m_lexer.get_token_string(),
  9835. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), BasicJsonType()));
  9836. }
  9837. // parse values
  9838. get_token();
  9839. continue;
  9840. }
  9841. // closing }
  9842. if (JSON_HEDLEY_LIKELY(last_token == token_type::end_object))
  9843. {
  9844. if (JSON_HEDLEY_UNLIKELY(!sax->end_object()))
  9845. {
  9846. return false;
  9847. }
  9848. // We are done with this object. Before we can parse a
  9849. // new value, we need to evaluate the new state first.
  9850. // By setting skip_to_state_evaluation to false, we
  9851. // are effectively jumping to the beginning of this if.
  9852. JSON_ASSERT(!states.empty());
  9853. states.pop_back();
  9854. skip_to_state_evaluation = true;
  9855. continue;
  9856. }
  9857. return sax->parse_error(m_lexer.get_position(),
  9858. m_lexer.get_token_string(),
  9859. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_object, "object"), BasicJsonType()));
  9860. }
  9861. }
  9862. /// get next token from lexer
  9863. token_type get_token()
  9864. {
  9865. return last_token = m_lexer.scan();
  9866. }
  9867. std::string exception_message(const token_type expected, const std::string& context)
  9868. {
  9869. std::string error_msg = "syntax error ";
  9870. if (!context.empty())
  9871. {
  9872. error_msg += "while parsing " + context + " ";
  9873. }
  9874. error_msg += "- ";
  9875. if (last_token == token_type::parse_error)
  9876. {
  9877. error_msg += std::string(m_lexer.get_error_message()) + "; last read: '" +
  9878. m_lexer.get_token_string() + "'";
  9879. }
  9880. else
  9881. {
  9882. error_msg += "unexpected " + std::string(lexer_t::token_type_name(last_token));
  9883. }
  9884. if (expected != token_type::uninitialized)
  9885. {
  9886. error_msg += "; expected " + std::string(lexer_t::token_type_name(expected));
  9887. }
  9888. return error_msg;
  9889. }
  9890. private:
  9891. /// callback function
  9892. const parser_callback_t<BasicJsonType> callback = nullptr;
  9893. /// the type of the last read token
  9894. token_type last_token = token_type::uninitialized;
  9895. /// the lexer
  9896. lexer_t m_lexer;
  9897. /// whether to throw exceptions in case of errors
  9898. const bool allow_exceptions = true;
  9899. };
  9900. } // namespace detail
  9901. } // namespace nlohmann
  9902. // #include <nlohmann/detail/iterators/internal_iterator.hpp>
  9903. // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
  9904. #include <cstddef> // ptrdiff_t
  9905. #include <limits> // numeric_limits
  9906. // #include <nlohmann/detail/macro_scope.hpp>
  9907. namespace nlohmann
  9908. {
  9909. namespace detail
  9910. {
  9911. /*
  9912. @brief an iterator for primitive JSON types
  9913. This class models an iterator for primitive JSON types (boolean, number,
  9914. string). It's only purpose is to allow the iterator/const_iterator classes
  9915. to "iterate" over primitive values. Internally, the iterator is modeled by
  9916. a `difference_type` variable. Value begin_value (`0`) models the begin,
  9917. end_value (`1`) models past the end.
  9918. */
  9919. class primitive_iterator_t
  9920. {
  9921. private:
  9922. using difference_type = std::ptrdiff_t;
  9923. static constexpr difference_type begin_value = 0;
  9924. static constexpr difference_type end_value = begin_value + 1;
  9925. JSON_PRIVATE_UNLESS_TESTED:
  9926. /// iterator as signed integer type
  9927. difference_type m_it = (std::numeric_limits<std::ptrdiff_t>::min)();
  9928. public:
  9929. constexpr difference_type get_value() const noexcept
  9930. {
  9931. return m_it;
  9932. }
  9933. /// set iterator to a defined beginning
  9934. void set_begin() noexcept
  9935. {
  9936. m_it = begin_value;
  9937. }
  9938. /// set iterator to a defined past the end
  9939. void set_end() noexcept
  9940. {
  9941. m_it = end_value;
  9942. }
  9943. /// return whether the iterator can be dereferenced
  9944. constexpr bool is_begin() const noexcept
  9945. {
  9946. return m_it == begin_value;
  9947. }
  9948. /// return whether the iterator is at end
  9949. constexpr bool is_end() const noexcept
  9950. {
  9951. return m_it == end_value;
  9952. }
  9953. friend constexpr bool operator==(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
  9954. {
  9955. return lhs.m_it == rhs.m_it;
  9956. }
  9957. friend constexpr bool operator<(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
  9958. {
  9959. return lhs.m_it < rhs.m_it;
  9960. }
  9961. primitive_iterator_t operator+(difference_type n) noexcept
  9962. {
  9963. auto result = *this;
  9964. result += n;
  9965. return result;
  9966. }
  9967. friend constexpr difference_type operator-(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
  9968. {
  9969. return lhs.m_it - rhs.m_it;
  9970. }
  9971. primitive_iterator_t& operator++() noexcept
  9972. {
  9973. ++m_it;
  9974. return *this;
  9975. }
  9976. primitive_iterator_t const operator++(int) noexcept // NOLINT(readability-const-return-type)
  9977. {
  9978. auto result = *this;
  9979. ++m_it;
  9980. return result;
  9981. }
  9982. primitive_iterator_t& operator--() noexcept
  9983. {
  9984. --m_it;
  9985. return *this;
  9986. }
  9987. primitive_iterator_t const operator--(int) noexcept // NOLINT(readability-const-return-type)
  9988. {
  9989. auto result = *this;
  9990. --m_it;
  9991. return result;
  9992. }
  9993. primitive_iterator_t& operator+=(difference_type n) noexcept
  9994. {
  9995. m_it += n;
  9996. return *this;
  9997. }
  9998. primitive_iterator_t& operator-=(difference_type n) noexcept
  9999. {
  10000. m_it -= n;
  10001. return *this;
  10002. }
  10003. };
  10004. } // namespace detail
  10005. } // namespace nlohmann
  10006. namespace nlohmann
  10007. {
  10008. namespace detail
  10009. {
  10010. /*!
  10011. @brief an iterator value
  10012. @note This structure could easily be a union, but MSVC currently does not allow
  10013. unions members with complex constructors, see https://github.com/nlohmann/json/pull/105.
  10014. */
  10015. template<typename BasicJsonType> struct internal_iterator
  10016. {
  10017. /// iterator for JSON objects
  10018. typename BasicJsonType::object_t::iterator object_iterator {};
  10019. /// iterator for JSON arrays
  10020. typename BasicJsonType::array_t::iterator array_iterator {};
  10021. /// generic iterator for all other types
  10022. primitive_iterator_t primitive_iterator {};
  10023. };
  10024. } // namespace detail
  10025. } // namespace nlohmann
  10026. // #include <nlohmann/detail/iterators/iter_impl.hpp>
  10027. #include <iterator> // iterator, random_access_iterator_tag, bidirectional_iterator_tag, advance, next
  10028. #include <type_traits> // conditional, is_const, remove_const
  10029. // #include <nlohmann/detail/exceptions.hpp>
  10030. // #include <nlohmann/detail/iterators/internal_iterator.hpp>
  10031. // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
  10032. // #include <nlohmann/detail/macro_scope.hpp>
  10033. // #include <nlohmann/detail/meta/cpp_future.hpp>
  10034. // #include <nlohmann/detail/meta/type_traits.hpp>
  10035. // #include <nlohmann/detail/value_t.hpp>
  10036. namespace nlohmann
  10037. {
  10038. namespace detail
  10039. {
  10040. // forward declare, to be able to friend it later on
  10041. template<typename IteratorType> class iteration_proxy;
  10042. template<typename IteratorType> class iteration_proxy_value;
  10043. /*!
  10044. @brief a template for a bidirectional iterator for the @ref basic_json class
  10045. This class implements a both iterators (iterator and const_iterator) for the
  10046. @ref basic_json class.
  10047. @note An iterator is called *initialized* when a pointer to a JSON value has
  10048. been set (e.g., by a constructor or a copy assignment). If the iterator is
  10049. default-constructed, it is *uninitialized* and most methods are undefined.
  10050. **The library uses assertions to detect calls on uninitialized iterators.**
  10051. @requirement The class satisfies the following concept requirements:
  10052. -
  10053. [BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
  10054. The iterator that can be moved can be moved in both directions (i.e.
  10055. incremented and decremented).
  10056. @since version 1.0.0, simplified in version 2.0.9, change to bidirectional
  10057. iterators in version 3.0.0 (see https://github.com/nlohmann/json/issues/593)
  10058. */
  10059. template<typename BasicJsonType>
  10060. class iter_impl
  10061. {
  10062. /// the iterator with BasicJsonType of different const-ness
  10063. using other_iter_impl = iter_impl<typename std::conditional<std::is_const<BasicJsonType>::value, typename std::remove_const<BasicJsonType>::type, const BasicJsonType>::type>;
  10064. /// allow basic_json to access private members
  10065. friend other_iter_impl;
  10066. friend BasicJsonType;
  10067. friend iteration_proxy<iter_impl>;
  10068. friend iteration_proxy_value<iter_impl>;
  10069. using object_t = typename BasicJsonType::object_t;
  10070. using array_t = typename BasicJsonType::array_t;
  10071. // make sure BasicJsonType is basic_json or const basic_json
  10072. static_assert(is_basic_json<typename std::remove_const<BasicJsonType>::type>::value,
  10073. "iter_impl only accepts (const) basic_json");
  10074. public:
  10075. /// The std::iterator class template (used as a base class to provide typedefs) is deprecated in C++17.
  10076. /// The C++ Standard has never required user-defined iterators to derive from std::iterator.
  10077. /// A user-defined iterator should provide publicly accessible typedefs named
  10078. /// iterator_category, value_type, difference_type, pointer, and reference.
  10079. /// Note that value_type is required to be non-const, even for constant iterators.
  10080. using iterator_category = std::bidirectional_iterator_tag;
  10081. /// the type of the values when the iterator is dereferenced
  10082. using value_type = typename BasicJsonType::value_type;
  10083. /// a type to represent differences between iterators
  10084. using difference_type = typename BasicJsonType::difference_type;
  10085. /// defines a pointer to the type iterated over (value_type)
  10086. using pointer = typename std::conditional<std::is_const<BasicJsonType>::value,
  10087. typename BasicJsonType::const_pointer,
  10088. typename BasicJsonType::pointer>::type;
  10089. /// defines a reference to the type iterated over (value_type)
  10090. using reference =
  10091. typename std::conditional<std::is_const<BasicJsonType>::value,
  10092. typename BasicJsonType::const_reference,
  10093. typename BasicJsonType::reference>::type;
  10094. iter_impl() = default;
  10095. ~iter_impl() = default;
  10096. iter_impl(iter_impl&&) noexcept = default;
  10097. iter_impl& operator=(iter_impl&&) noexcept = default;
  10098. /*!
  10099. @brief constructor for a given JSON instance
  10100. @param[in] object pointer to a JSON object for this iterator
  10101. @pre object != nullptr
  10102. @post The iterator is initialized; i.e. `m_object != nullptr`.
  10103. */
  10104. explicit iter_impl(pointer object) noexcept : m_object(object)
  10105. {
  10106. JSON_ASSERT(m_object != nullptr);
  10107. switch (m_object->m_type)
  10108. {
  10109. case value_t::object:
  10110. {
  10111. m_it.object_iterator = typename object_t::iterator();
  10112. break;
  10113. }
  10114. case value_t::array:
  10115. {
  10116. m_it.array_iterator = typename array_t::iterator();
  10117. break;
  10118. }
  10119. case value_t::null:
  10120. case value_t::string:
  10121. case value_t::boolean:
  10122. case value_t::number_integer:
  10123. case value_t::number_unsigned:
  10124. case value_t::number_float:
  10125. case value_t::binary:
  10126. case value_t::discarded:
  10127. default:
  10128. {
  10129. m_it.primitive_iterator = primitive_iterator_t();
  10130. break;
  10131. }
  10132. }
  10133. }
  10134. /*!
  10135. @note The conventional copy constructor and copy assignment are implicitly
  10136. defined. Combined with the following converting constructor and
  10137. assignment, they support: (1) copy from iterator to iterator, (2)
  10138. copy from const iterator to const iterator, and (3) conversion from
  10139. iterator to const iterator. However conversion from const iterator
  10140. to iterator is not defined.
  10141. */
  10142. /*!
  10143. @brief const copy constructor
  10144. @param[in] other const iterator to copy from
  10145. @note This copy constructor had to be defined explicitly to circumvent a bug
  10146. occurring on msvc v19.0 compiler (VS 2015) debug build. For more
  10147. information refer to: https://github.com/nlohmann/json/issues/1608
  10148. */
  10149. iter_impl(const iter_impl<const BasicJsonType>& other) noexcept
  10150. : m_object(other.m_object), m_it(other.m_it)
  10151. {}
  10152. /*!
  10153. @brief converting assignment
  10154. @param[in] other const iterator to copy from
  10155. @return const/non-const iterator
  10156. @note It is not checked whether @a other is initialized.
  10157. */
  10158. iter_impl& operator=(const iter_impl<const BasicJsonType>& other) noexcept
  10159. {
  10160. if (&other != this)
  10161. {
  10162. m_object = other.m_object;
  10163. m_it = other.m_it;
  10164. }
  10165. return *this;
  10166. }
  10167. /*!
  10168. @brief converting constructor
  10169. @param[in] other non-const iterator to copy from
  10170. @note It is not checked whether @a other is initialized.
  10171. */
  10172. iter_impl(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept
  10173. : m_object(other.m_object), m_it(other.m_it)
  10174. {}
  10175. /*!
  10176. @brief converting assignment
  10177. @param[in] other non-const iterator to copy from
  10178. @return const/non-const iterator
  10179. @note It is not checked whether @a other is initialized.
  10180. */
  10181. iter_impl& operator=(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept // NOLINT(cert-oop54-cpp)
  10182. {
  10183. m_object = other.m_object;
  10184. m_it = other.m_it;
  10185. return *this;
  10186. }
  10187. JSON_PRIVATE_UNLESS_TESTED:
  10188. /*!
  10189. @brief set the iterator to the first value
  10190. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  10191. */
  10192. void set_begin() noexcept
  10193. {
  10194. JSON_ASSERT(m_object != nullptr);
  10195. switch (m_object->m_type)
  10196. {
  10197. case value_t::object:
  10198. {
  10199. m_it.object_iterator = m_object->m_value.object->begin();
  10200. break;
  10201. }
  10202. case value_t::array:
  10203. {
  10204. m_it.array_iterator = m_object->m_value.array->begin();
  10205. break;
  10206. }
  10207. case value_t::null:
  10208. {
  10209. // set to end so begin()==end() is true: null is empty
  10210. m_it.primitive_iterator.set_end();
  10211. break;
  10212. }
  10213. case value_t::string:
  10214. case value_t::boolean:
  10215. case value_t::number_integer:
  10216. case value_t::number_unsigned:
  10217. case value_t::number_float:
  10218. case value_t::binary:
  10219. case value_t::discarded:
  10220. default:
  10221. {
  10222. m_it.primitive_iterator.set_begin();
  10223. break;
  10224. }
  10225. }
  10226. }
  10227. /*!
  10228. @brief set the iterator past the last value
  10229. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  10230. */
  10231. void set_end() noexcept
  10232. {
  10233. JSON_ASSERT(m_object != nullptr);
  10234. switch (m_object->m_type)
  10235. {
  10236. case value_t::object:
  10237. {
  10238. m_it.object_iterator = m_object->m_value.object->end();
  10239. break;
  10240. }
  10241. case value_t::array:
  10242. {
  10243. m_it.array_iterator = m_object->m_value.array->end();
  10244. break;
  10245. }
  10246. case value_t::null:
  10247. case value_t::string:
  10248. case value_t::boolean:
  10249. case value_t::number_integer:
  10250. case value_t::number_unsigned:
  10251. case value_t::number_float:
  10252. case value_t::binary:
  10253. case value_t::discarded:
  10254. default:
  10255. {
  10256. m_it.primitive_iterator.set_end();
  10257. break;
  10258. }
  10259. }
  10260. }
  10261. public:
  10262. /*!
  10263. @brief return a reference to the value pointed to by the iterator
  10264. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  10265. */
  10266. reference operator*() const
  10267. {
  10268. JSON_ASSERT(m_object != nullptr);
  10269. switch (m_object->m_type)
  10270. {
  10271. case value_t::object:
  10272. {
  10273. JSON_ASSERT(m_it.object_iterator != m_object->m_value.object->end());
  10274. return m_it.object_iterator->second;
  10275. }
  10276. case value_t::array:
  10277. {
  10278. JSON_ASSERT(m_it.array_iterator != m_object->m_value.array->end());
  10279. return *m_it.array_iterator;
  10280. }
  10281. case value_t::null:
  10282. JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object));
  10283. case value_t::string:
  10284. case value_t::boolean:
  10285. case value_t::number_integer:
  10286. case value_t::number_unsigned:
  10287. case value_t::number_float:
  10288. case value_t::binary:
  10289. case value_t::discarded:
  10290. default:
  10291. {
  10292. if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin()))
  10293. {
  10294. return *m_object;
  10295. }
  10296. JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object));
  10297. }
  10298. }
  10299. }
  10300. /*!
  10301. @brief dereference the iterator
  10302. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  10303. */
  10304. pointer operator->() const
  10305. {
  10306. JSON_ASSERT(m_object != nullptr);
  10307. switch (m_object->m_type)
  10308. {
  10309. case value_t::object:
  10310. {
  10311. JSON_ASSERT(m_it.object_iterator != m_object->m_value.object->end());
  10312. return &(m_it.object_iterator->second);
  10313. }
  10314. case value_t::array:
  10315. {
  10316. JSON_ASSERT(m_it.array_iterator != m_object->m_value.array->end());
  10317. return &*m_it.array_iterator;
  10318. }
  10319. case value_t::null:
  10320. case value_t::string:
  10321. case value_t::boolean:
  10322. case value_t::number_integer:
  10323. case value_t::number_unsigned:
  10324. case value_t::number_float:
  10325. case value_t::binary:
  10326. case value_t::discarded:
  10327. default:
  10328. {
  10329. if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin()))
  10330. {
  10331. return m_object;
  10332. }
  10333. JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object));
  10334. }
  10335. }
  10336. }
  10337. /*!
  10338. @brief post-increment (it++)
  10339. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  10340. */
  10341. iter_impl const operator++(int) // NOLINT(readability-const-return-type)
  10342. {
  10343. auto result = *this;
  10344. ++(*this);
  10345. return result;
  10346. }
  10347. /*!
  10348. @brief pre-increment (++it)
  10349. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  10350. */
  10351. iter_impl& operator++()
  10352. {
  10353. JSON_ASSERT(m_object != nullptr);
  10354. switch (m_object->m_type)
  10355. {
  10356. case value_t::object:
  10357. {
  10358. std::advance(m_it.object_iterator, 1);
  10359. break;
  10360. }
  10361. case value_t::array:
  10362. {
  10363. std::advance(m_it.array_iterator, 1);
  10364. break;
  10365. }
  10366. case value_t::null:
  10367. case value_t::string:
  10368. case value_t::boolean:
  10369. case value_t::number_integer:
  10370. case value_t::number_unsigned:
  10371. case value_t::number_float:
  10372. case value_t::binary:
  10373. case value_t::discarded:
  10374. default:
  10375. {
  10376. ++m_it.primitive_iterator;
  10377. break;
  10378. }
  10379. }
  10380. return *this;
  10381. }
  10382. /*!
  10383. @brief post-decrement (it--)
  10384. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  10385. */
  10386. iter_impl const operator--(int) // NOLINT(readability-const-return-type)
  10387. {
  10388. auto result = *this;
  10389. --(*this);
  10390. return result;
  10391. }
  10392. /*!
  10393. @brief pre-decrement (--it)
  10394. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  10395. */
  10396. iter_impl& operator--()
  10397. {
  10398. JSON_ASSERT(m_object != nullptr);
  10399. switch (m_object->m_type)
  10400. {
  10401. case value_t::object:
  10402. {
  10403. std::advance(m_it.object_iterator, -1);
  10404. break;
  10405. }
  10406. case value_t::array:
  10407. {
  10408. std::advance(m_it.array_iterator, -1);
  10409. break;
  10410. }
  10411. case value_t::null:
  10412. case value_t::string:
  10413. case value_t::boolean:
  10414. case value_t::number_integer:
  10415. case value_t::number_unsigned:
  10416. case value_t::number_float:
  10417. case value_t::binary:
  10418. case value_t::discarded:
  10419. default:
  10420. {
  10421. --m_it.primitive_iterator;
  10422. break;
  10423. }
  10424. }
  10425. return *this;
  10426. }
  10427. /*!
  10428. @brief comparison: equal
  10429. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  10430. */
  10431. 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 >
  10432. bool operator==(const IterImpl& other) const
  10433. {
  10434. // if objects are not the same, the comparison is undefined
  10435. if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
  10436. {
  10437. JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", *m_object));
  10438. }
  10439. JSON_ASSERT(m_object != nullptr);
  10440. switch (m_object->m_type)
  10441. {
  10442. case value_t::object:
  10443. return (m_it.object_iterator == other.m_it.object_iterator);
  10444. case value_t::array:
  10445. return (m_it.array_iterator == other.m_it.array_iterator);
  10446. case value_t::null:
  10447. case value_t::string:
  10448. case value_t::boolean:
  10449. case value_t::number_integer:
  10450. case value_t::number_unsigned:
  10451. case value_t::number_float:
  10452. case value_t::binary:
  10453. case value_t::discarded:
  10454. default:
  10455. return (m_it.primitive_iterator == other.m_it.primitive_iterator);
  10456. }
  10457. }
  10458. /*!
  10459. @brief comparison: not equal
  10460. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  10461. */
  10462. 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 >
  10463. bool operator!=(const IterImpl& other) const
  10464. {
  10465. return !operator==(other);
  10466. }
  10467. /*!
  10468. @brief comparison: smaller
  10469. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  10470. */
  10471. bool operator<(const iter_impl& other) const
  10472. {
  10473. // if objects are not the same, the comparison is undefined
  10474. if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
  10475. {
  10476. JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", *m_object));
  10477. }
  10478. JSON_ASSERT(m_object != nullptr);
  10479. switch (m_object->m_type)
  10480. {
  10481. case value_t::object:
  10482. JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators", *m_object));
  10483. case value_t::array:
  10484. return (m_it.array_iterator < other.m_it.array_iterator);
  10485. case value_t::null:
  10486. case value_t::string:
  10487. case value_t::boolean:
  10488. case value_t::number_integer:
  10489. case value_t::number_unsigned:
  10490. case value_t::number_float:
  10491. case value_t::binary:
  10492. case value_t::discarded:
  10493. default:
  10494. return (m_it.primitive_iterator < other.m_it.primitive_iterator);
  10495. }
  10496. }
  10497. /*!
  10498. @brief comparison: less than or equal
  10499. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  10500. */
  10501. bool operator<=(const iter_impl& other) const
  10502. {
  10503. return !other.operator < (*this);
  10504. }
  10505. /*!
  10506. @brief comparison: greater than
  10507. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  10508. */
  10509. bool operator>(const iter_impl& other) const
  10510. {
  10511. return !operator<=(other);
  10512. }
  10513. /*!
  10514. @brief comparison: greater than or equal
  10515. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  10516. */
  10517. bool operator>=(const iter_impl& other) const
  10518. {
  10519. return !operator<(other);
  10520. }
  10521. /*!
  10522. @brief add to iterator
  10523. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  10524. */
  10525. iter_impl& operator+=(difference_type i)
  10526. {
  10527. JSON_ASSERT(m_object != nullptr);
  10528. switch (m_object->m_type)
  10529. {
  10530. case value_t::object:
  10531. JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", *m_object));
  10532. case value_t::array:
  10533. {
  10534. std::advance(m_it.array_iterator, i);
  10535. break;
  10536. }
  10537. case value_t::null:
  10538. case value_t::string:
  10539. case value_t::boolean:
  10540. case value_t::number_integer:
  10541. case value_t::number_unsigned:
  10542. case value_t::number_float:
  10543. case value_t::binary:
  10544. case value_t::discarded:
  10545. default:
  10546. {
  10547. m_it.primitive_iterator += i;
  10548. break;
  10549. }
  10550. }
  10551. return *this;
  10552. }
  10553. /*!
  10554. @brief subtract from iterator
  10555. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  10556. */
  10557. iter_impl& operator-=(difference_type i)
  10558. {
  10559. return operator+=(-i);
  10560. }
  10561. /*!
  10562. @brief add to iterator
  10563. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  10564. */
  10565. iter_impl operator+(difference_type i) const
  10566. {
  10567. auto result = *this;
  10568. result += i;
  10569. return result;
  10570. }
  10571. /*!
  10572. @brief addition of distance and iterator
  10573. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  10574. */
  10575. friend iter_impl operator+(difference_type i, const iter_impl& it)
  10576. {
  10577. auto result = it;
  10578. result += i;
  10579. return result;
  10580. }
  10581. /*!
  10582. @brief subtract from iterator
  10583. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  10584. */
  10585. iter_impl operator-(difference_type i) const
  10586. {
  10587. auto result = *this;
  10588. result -= i;
  10589. return result;
  10590. }
  10591. /*!
  10592. @brief return difference
  10593. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  10594. */
  10595. difference_type operator-(const iter_impl& other) const
  10596. {
  10597. JSON_ASSERT(m_object != nullptr);
  10598. switch (m_object->m_type)
  10599. {
  10600. case value_t::object:
  10601. JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", *m_object));
  10602. case value_t::array:
  10603. return m_it.array_iterator - other.m_it.array_iterator;
  10604. case value_t::null:
  10605. case value_t::string:
  10606. case value_t::boolean:
  10607. case value_t::number_integer:
  10608. case value_t::number_unsigned:
  10609. case value_t::number_float:
  10610. case value_t::binary:
  10611. case value_t::discarded:
  10612. default:
  10613. return m_it.primitive_iterator - other.m_it.primitive_iterator;
  10614. }
  10615. }
  10616. /*!
  10617. @brief access to successor
  10618. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  10619. */
  10620. reference operator[](difference_type n) const
  10621. {
  10622. JSON_ASSERT(m_object != nullptr);
  10623. switch (m_object->m_type)
  10624. {
  10625. case value_t::object:
  10626. JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators", *m_object));
  10627. case value_t::array:
  10628. return *std::next(m_it.array_iterator, n);
  10629. case value_t::null:
  10630. JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object));
  10631. case value_t::string:
  10632. case value_t::boolean:
  10633. case value_t::number_integer:
  10634. case value_t::number_unsigned:
  10635. case value_t::number_float:
  10636. case value_t::binary:
  10637. case value_t::discarded:
  10638. default:
  10639. {
  10640. if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.get_value() == -n))
  10641. {
  10642. return *m_object;
  10643. }
  10644. JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object));
  10645. }
  10646. }
  10647. }
  10648. /*!
  10649. @brief return the key of an object iterator
  10650. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  10651. */
  10652. const typename object_t::key_type& key() const
  10653. {
  10654. JSON_ASSERT(m_object != nullptr);
  10655. if (JSON_HEDLEY_LIKELY(m_object->is_object()))
  10656. {
  10657. return m_it.object_iterator->first;
  10658. }
  10659. JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators", *m_object));
  10660. }
  10661. /*!
  10662. @brief return the value of an iterator
  10663. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  10664. */
  10665. reference value() const
  10666. {
  10667. return operator*();
  10668. }
  10669. JSON_PRIVATE_UNLESS_TESTED:
  10670. /// associated JSON instance
  10671. pointer m_object = nullptr;
  10672. /// the actual iterator of the associated instance
  10673. internal_iterator<typename std::remove_const<BasicJsonType>::type> m_it {};
  10674. };
  10675. } // namespace detail
  10676. } // namespace nlohmann
  10677. // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
  10678. // #include <nlohmann/detail/iterators/json_reverse_iterator.hpp>
  10679. #include <cstddef> // ptrdiff_t
  10680. #include <iterator> // reverse_iterator
  10681. #include <utility> // declval
  10682. namespace nlohmann
  10683. {
  10684. namespace detail
  10685. {
  10686. //////////////////////
  10687. // reverse_iterator //
  10688. //////////////////////
  10689. /*!
  10690. @brief a template for a reverse iterator class
  10691. @tparam Base the base iterator type to reverse. Valid types are @ref
  10692. iterator (to create @ref reverse_iterator) and @ref const_iterator (to
  10693. create @ref const_reverse_iterator).
  10694. @requirement The class satisfies the following concept requirements:
  10695. -
  10696. [BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
  10697. The iterator that can be moved can be moved in both directions (i.e.
  10698. incremented and decremented).
  10699. - [OutputIterator](https://en.cppreference.com/w/cpp/named_req/OutputIterator):
  10700. It is possible to write to the pointed-to element (only if @a Base is
  10701. @ref iterator).
  10702. @since version 1.0.0
  10703. */
  10704. template<typename Base>
  10705. class json_reverse_iterator : public std::reverse_iterator<Base>
  10706. {
  10707. public:
  10708. using difference_type = std::ptrdiff_t;
  10709. /// shortcut to the reverse iterator adapter
  10710. using base_iterator = std::reverse_iterator<Base>;
  10711. /// the reference type for the pointed-to element
  10712. using reference = typename Base::reference;
  10713. /// create reverse iterator from iterator
  10714. explicit json_reverse_iterator(const typename base_iterator::iterator_type& it) noexcept
  10715. : base_iterator(it) {}
  10716. /// create reverse iterator from base class
  10717. explicit json_reverse_iterator(const base_iterator& it) noexcept : base_iterator(it) {}
  10718. /// post-increment (it++)
  10719. json_reverse_iterator const operator++(int) // NOLINT(readability-const-return-type)
  10720. {
  10721. return static_cast<json_reverse_iterator>(base_iterator::operator++(1));
  10722. }
  10723. /// pre-increment (++it)
  10724. json_reverse_iterator& operator++()
  10725. {
  10726. return static_cast<json_reverse_iterator&>(base_iterator::operator++());
  10727. }
  10728. /// post-decrement (it--)
  10729. json_reverse_iterator const operator--(int) // NOLINT(readability-const-return-type)
  10730. {
  10731. return static_cast<json_reverse_iterator>(base_iterator::operator--(1));
  10732. }
  10733. /// pre-decrement (--it)
  10734. json_reverse_iterator& operator--()
  10735. {
  10736. return static_cast<json_reverse_iterator&>(base_iterator::operator--());
  10737. }
  10738. /// add to iterator
  10739. json_reverse_iterator& operator+=(difference_type i)
  10740. {
  10741. return static_cast<json_reverse_iterator&>(base_iterator::operator+=(i));
  10742. }
  10743. /// add to iterator
  10744. json_reverse_iterator operator+(difference_type i) const
  10745. {
  10746. return static_cast<json_reverse_iterator>(base_iterator::operator+(i));
  10747. }
  10748. /// subtract from iterator
  10749. json_reverse_iterator operator-(difference_type i) const
  10750. {
  10751. return static_cast<json_reverse_iterator>(base_iterator::operator-(i));
  10752. }
  10753. /// return difference
  10754. difference_type operator-(const json_reverse_iterator& other) const
  10755. {
  10756. return base_iterator(*this) - base_iterator(other);
  10757. }
  10758. /// access to successor
  10759. reference operator[](difference_type n) const
  10760. {
  10761. return *(this->operator+(n));
  10762. }
  10763. /// return the key of an object iterator
  10764. auto key() const -> decltype(std::declval<Base>().key())
  10765. {
  10766. auto it = --this->base();
  10767. return it.key();
  10768. }
  10769. /// return the value of an iterator
  10770. reference value() const
  10771. {
  10772. auto it = --this->base();
  10773. return it.operator * ();
  10774. }
  10775. };
  10776. } // namespace detail
  10777. } // namespace nlohmann
  10778. // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
  10779. // #include <nlohmann/detail/json_pointer.hpp>
  10780. #include <algorithm> // all_of
  10781. #include <cctype> // isdigit
  10782. #include <limits> // max
  10783. #include <numeric> // accumulate
  10784. #include <string> // string
  10785. #include <utility> // move
  10786. #include <vector> // vector
  10787. // #include <nlohmann/detail/exceptions.hpp>
  10788. // #include <nlohmann/detail/macro_scope.hpp>
  10789. // #include <nlohmann/detail/string_escape.hpp>
  10790. // #include <nlohmann/detail/value_t.hpp>
  10791. namespace nlohmann
  10792. {
  10793. template<typename BasicJsonType>
  10794. class json_pointer
  10795. {
  10796. // allow basic_json to access private members
  10797. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  10798. friend class basic_json;
  10799. public:
  10800. /*!
  10801. @brief create JSON pointer
  10802. Create a JSON pointer according to the syntax described in
  10803. [Section 3 of RFC6901](https://tools.ietf.org/html/rfc6901#section-3).
  10804. @param[in] s string representing the JSON pointer; if omitted, the empty
  10805. string is assumed which references the whole JSON value
  10806. @throw parse_error.107 if the given JSON pointer @a s is nonempty and does
  10807. not begin with a slash (`/`); see example below
  10808. @throw parse_error.108 if a tilde (`~`) in the given JSON pointer @a s is
  10809. not followed by `0` (representing `~`) or `1` (representing `/`); see
  10810. example below
  10811. @liveexample{The example shows the construction several valid JSON pointers
  10812. as well as the exceptional behavior.,json_pointer}
  10813. @since version 2.0.0
  10814. */
  10815. explicit json_pointer(const std::string& s = "")
  10816. : reference_tokens(split(s))
  10817. {}
  10818. /*!
  10819. @brief return a string representation of the JSON pointer
  10820. @invariant For each JSON pointer `ptr`, it holds:
  10821. @code {.cpp}
  10822. ptr == json_pointer(ptr.to_string());
  10823. @endcode
  10824. @return a string representation of the JSON pointer
  10825. @liveexample{The example shows the result of `to_string`.,json_pointer__to_string}
  10826. @since version 2.0.0
  10827. */
  10828. std::string to_string() const
  10829. {
  10830. return std::accumulate(reference_tokens.begin(), reference_tokens.end(),
  10831. std::string{},
  10832. [](const std::string & a, const std::string & b)
  10833. {
  10834. return a + "/" + detail::escape(b);
  10835. });
  10836. }
  10837. /// @copydoc to_string()
  10838. operator std::string() const
  10839. {
  10840. return to_string();
  10841. }
  10842. /*!
  10843. @brief append another JSON pointer at the end of this JSON pointer
  10844. @param[in] ptr JSON pointer to append
  10845. @return JSON pointer with @a ptr appended
  10846. @liveexample{The example shows the usage of `operator/=`.,json_pointer__operator_add}
  10847. @complexity Linear in the length of @a ptr.
  10848. @sa see @ref operator/=(std::string) to append a reference token
  10849. @sa see @ref operator/=(std::size_t) to append an array index
  10850. @sa see @ref operator/(const json_pointer&, const json_pointer&) for a binary operator
  10851. @since version 3.6.0
  10852. */
  10853. json_pointer& operator/=(const json_pointer& ptr)
  10854. {
  10855. reference_tokens.insert(reference_tokens.end(),
  10856. ptr.reference_tokens.begin(),
  10857. ptr.reference_tokens.end());
  10858. return *this;
  10859. }
  10860. /*!
  10861. @brief append an unescaped reference token at the end of this JSON pointer
  10862. @param[in] token reference token to append
  10863. @return JSON pointer with @a token appended without escaping @a token
  10864. @liveexample{The example shows the usage of `operator/=`.,json_pointer__operator_add}
  10865. @complexity Amortized constant.
  10866. @sa see @ref operator/=(const json_pointer&) to append a JSON pointer
  10867. @sa see @ref operator/=(std::size_t) to append an array index
  10868. @sa see @ref operator/(const json_pointer&, std::size_t) for a binary operator
  10869. @since version 3.6.0
  10870. */
  10871. json_pointer& operator/=(std::string token)
  10872. {
  10873. push_back(std::move(token));
  10874. return *this;
  10875. }
  10876. /*!
  10877. @brief append an array index at the end of this JSON pointer
  10878. @param[in] array_idx array index to append
  10879. @return JSON pointer with @a array_idx appended
  10880. @liveexample{The example shows the usage of `operator/=`.,json_pointer__operator_add}
  10881. @complexity Amortized constant.
  10882. @sa see @ref operator/=(const json_pointer&) to append a JSON pointer
  10883. @sa see @ref operator/=(std::string) to append a reference token
  10884. @sa see @ref operator/(const json_pointer&, std::string) for a binary operator
  10885. @since version 3.6.0
  10886. */
  10887. json_pointer& operator/=(std::size_t array_idx)
  10888. {
  10889. return *this /= std::to_string(array_idx);
  10890. }
  10891. /*!
  10892. @brief create a new JSON pointer by appending the right JSON pointer at the end of the left JSON pointer
  10893. @param[in] lhs JSON pointer
  10894. @param[in] rhs JSON pointer
  10895. @return a new JSON pointer with @a rhs appended to @a lhs
  10896. @liveexample{The example shows the usage of `operator/`.,json_pointer__operator_add_binary}
  10897. @complexity Linear in the length of @a lhs and @a rhs.
  10898. @sa see @ref operator/=(const json_pointer&) to append a JSON pointer
  10899. @since version 3.6.0
  10900. */
  10901. friend json_pointer operator/(const json_pointer& lhs,
  10902. const json_pointer& rhs)
  10903. {
  10904. return json_pointer(lhs) /= rhs;
  10905. }
  10906. /*!
  10907. @brief create a new JSON pointer by appending the unescaped token at the end of the JSON pointer
  10908. @param[in] ptr JSON pointer
  10909. @param[in] token reference token
  10910. @return a new JSON pointer with unescaped @a token appended to @a ptr
  10911. @liveexample{The example shows the usage of `operator/`.,json_pointer__operator_add_binary}
  10912. @complexity Linear in the length of @a ptr.
  10913. @sa see @ref operator/=(std::string) to append a reference token
  10914. @since version 3.6.0
  10915. */
  10916. friend json_pointer operator/(const json_pointer& ptr, std::string token) // NOLINT(performance-unnecessary-value-param)
  10917. {
  10918. return json_pointer(ptr) /= std::move(token);
  10919. }
  10920. /*!
  10921. @brief create a new JSON pointer by appending the array-index-token at the end of the JSON pointer
  10922. @param[in] ptr JSON pointer
  10923. @param[in] array_idx array index
  10924. @return a new JSON pointer with @a array_idx appended to @a ptr
  10925. @liveexample{The example shows the usage of `operator/`.,json_pointer__operator_add_binary}
  10926. @complexity Linear in the length of @a ptr.
  10927. @sa see @ref operator/=(std::size_t) to append an array index
  10928. @since version 3.6.0
  10929. */
  10930. friend json_pointer operator/(const json_pointer& ptr, std::size_t array_idx)
  10931. {
  10932. return json_pointer(ptr) /= array_idx;
  10933. }
  10934. /*!
  10935. @brief returns the parent of this JSON pointer
  10936. @return parent of this JSON pointer; in case this JSON pointer is the root,
  10937. the root itself is returned
  10938. @complexity Linear in the length of the JSON pointer.
  10939. @liveexample{The example shows the result of `parent_pointer` for different
  10940. JSON Pointers.,json_pointer__parent_pointer}
  10941. @since version 3.6.0
  10942. */
  10943. json_pointer parent_pointer() const
  10944. {
  10945. if (empty())
  10946. {
  10947. return *this;
  10948. }
  10949. json_pointer res = *this;
  10950. res.pop_back();
  10951. return res;
  10952. }
  10953. /*!
  10954. @brief remove last reference token
  10955. @pre not `empty()`
  10956. @liveexample{The example shows the usage of `pop_back`.,json_pointer__pop_back}
  10957. @complexity Constant.
  10958. @throw out_of_range.405 if JSON pointer has no parent
  10959. @since version 3.6.0
  10960. */
  10961. void pop_back()
  10962. {
  10963. if (JSON_HEDLEY_UNLIKELY(empty()))
  10964. {
  10965. JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", BasicJsonType()));
  10966. }
  10967. reference_tokens.pop_back();
  10968. }
  10969. /*!
  10970. @brief return last reference token
  10971. @pre not `empty()`
  10972. @return last reference token
  10973. @liveexample{The example shows the usage of `back`.,json_pointer__back}
  10974. @complexity Constant.
  10975. @throw out_of_range.405 if JSON pointer has no parent
  10976. @since version 3.6.0
  10977. */
  10978. const std::string& back() const
  10979. {
  10980. if (JSON_HEDLEY_UNLIKELY(empty()))
  10981. {
  10982. JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", BasicJsonType()));
  10983. }
  10984. return reference_tokens.back();
  10985. }
  10986. /*!
  10987. @brief append an unescaped token at the end of the reference pointer
  10988. @param[in] token token to add
  10989. @complexity Amortized constant.
  10990. @liveexample{The example shows the result of `push_back` for different
  10991. JSON Pointers.,json_pointer__push_back}
  10992. @since version 3.6.0
  10993. */
  10994. void push_back(const std::string& token)
  10995. {
  10996. reference_tokens.push_back(token);
  10997. }
  10998. /// @copydoc push_back(const std::string&)
  10999. void push_back(std::string&& token)
  11000. {
  11001. reference_tokens.push_back(std::move(token));
  11002. }
  11003. /*!
  11004. @brief return whether pointer points to the root document
  11005. @return true iff the JSON pointer points to the root document
  11006. @complexity Constant.
  11007. @exceptionsafety No-throw guarantee: this function never throws exceptions.
  11008. @liveexample{The example shows the result of `empty` for different JSON
  11009. Pointers.,json_pointer__empty}
  11010. @since version 3.6.0
  11011. */
  11012. bool empty() const noexcept
  11013. {
  11014. return reference_tokens.empty();
  11015. }
  11016. private:
  11017. /*!
  11018. @param[in] s reference token to be converted into an array index
  11019. @return integer representation of @a s
  11020. @throw parse_error.106 if an array index begins with '0'
  11021. @throw parse_error.109 if an array index begins not with a digit
  11022. @throw out_of_range.404 if string @a s could not be converted to an integer
  11023. @throw out_of_range.410 if an array index exceeds size_type
  11024. */
  11025. static typename BasicJsonType::size_type array_index(const std::string& s)
  11026. {
  11027. using size_type = typename BasicJsonType::size_type;
  11028. // error condition (cf. RFC 6901, Sect. 4)
  11029. if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && s[0] == '0'))
  11030. {
  11031. JSON_THROW(detail::parse_error::create(106, 0, "array index '" + s + "' must not begin with '0'", BasicJsonType()));
  11032. }
  11033. // error condition (cf. RFC 6901, Sect. 4)
  11034. if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && !(s[0] >= '1' && s[0] <= '9')))
  11035. {
  11036. JSON_THROW(detail::parse_error::create(109, 0, "array index '" + s + "' is not a number", BasicJsonType()));
  11037. }
  11038. std::size_t processed_chars = 0;
  11039. unsigned long long res = 0; // NOLINT(runtime/int)
  11040. JSON_TRY
  11041. {
  11042. res = std::stoull(s, &processed_chars);
  11043. }
  11044. JSON_CATCH(std::out_of_range&)
  11045. {
  11046. JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'", BasicJsonType()));
  11047. }
  11048. // check if the string was completely read
  11049. if (JSON_HEDLEY_UNLIKELY(processed_chars != s.size()))
  11050. {
  11051. JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'", BasicJsonType()));
  11052. }
  11053. // only triggered on special platforms (like 32bit), see also
  11054. // https://github.com/nlohmann/json/pull/2203
  11055. if (res >= static_cast<unsigned long long>((std::numeric_limits<size_type>::max)())) // NOLINT(runtime/int)
  11056. {
  11057. JSON_THROW(detail::out_of_range::create(410, "array index " + s + " exceeds size_type", BasicJsonType())); // LCOV_EXCL_LINE
  11058. }
  11059. return static_cast<size_type>(res);
  11060. }
  11061. JSON_PRIVATE_UNLESS_TESTED:
  11062. json_pointer top() const
  11063. {
  11064. if (JSON_HEDLEY_UNLIKELY(empty()))
  11065. {
  11066. JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", BasicJsonType()));
  11067. }
  11068. json_pointer result = *this;
  11069. result.reference_tokens = {reference_tokens[0]};
  11070. return result;
  11071. }
  11072. private:
  11073. /*!
  11074. @brief create and return a reference to the pointed to value
  11075. @complexity Linear in the number of reference tokens.
  11076. @throw parse_error.109 if array index is not a number
  11077. @throw type_error.313 if value cannot be unflattened
  11078. */
  11079. BasicJsonType& get_and_create(BasicJsonType& j) const
  11080. {
  11081. auto* result = &j;
  11082. // in case no reference tokens exist, return a reference to the JSON value
  11083. // j which will be overwritten by a primitive value
  11084. for (const auto& reference_token : reference_tokens)
  11085. {
  11086. switch (result->type())
  11087. {
  11088. case detail::value_t::null:
  11089. {
  11090. if (reference_token == "0")
  11091. {
  11092. // start a new array if reference token is 0
  11093. result = &result->operator[](0);
  11094. }
  11095. else
  11096. {
  11097. // start a new object otherwise
  11098. result = &result->operator[](reference_token);
  11099. }
  11100. break;
  11101. }
  11102. case detail::value_t::object:
  11103. {
  11104. // create an entry in the object
  11105. result = &result->operator[](reference_token);
  11106. break;
  11107. }
  11108. case detail::value_t::array:
  11109. {
  11110. // create an entry in the array
  11111. result = &result->operator[](array_index(reference_token));
  11112. break;
  11113. }
  11114. /*
  11115. The following code is only reached if there exists a reference
  11116. token _and_ the current value is primitive. In this case, we have
  11117. an error situation, because primitive values may only occur as
  11118. single value; that is, with an empty list of reference tokens.
  11119. */
  11120. case detail::value_t::string:
  11121. case detail::value_t::boolean:
  11122. case detail::value_t::number_integer:
  11123. case detail::value_t::number_unsigned:
  11124. case detail::value_t::number_float:
  11125. case detail::value_t::binary:
  11126. case detail::value_t::discarded:
  11127. default:
  11128. JSON_THROW(detail::type_error::create(313, "invalid value to unflatten", j));
  11129. }
  11130. }
  11131. return *result;
  11132. }
  11133. /*!
  11134. @brief return a reference to the pointed to value
  11135. @note This version does not throw if a value is not present, but tries to
  11136. create nested values instead. For instance, calling this function
  11137. with pointer `"/this/that"` on a null value is equivalent to calling
  11138. `operator[]("this").operator[]("that")` on that value, effectively
  11139. changing the null value to an object.
  11140. @param[in] ptr a JSON value
  11141. @return reference to the JSON value pointed to by the JSON pointer
  11142. @complexity Linear in the length of the JSON pointer.
  11143. @throw parse_error.106 if an array index begins with '0'
  11144. @throw parse_error.109 if an array index was not a number
  11145. @throw out_of_range.404 if the JSON pointer can not be resolved
  11146. */
  11147. BasicJsonType& get_unchecked(BasicJsonType* ptr) const
  11148. {
  11149. for (const auto& reference_token : reference_tokens)
  11150. {
  11151. // convert null values to arrays or objects before continuing
  11152. if (ptr->is_null())
  11153. {
  11154. // check if reference token is a number
  11155. const bool nums =
  11156. std::all_of(reference_token.begin(), reference_token.end(),
  11157. [](const unsigned char x)
  11158. {
  11159. return std::isdigit(x);
  11160. });
  11161. // change value to array for numbers or "-" or to object otherwise
  11162. *ptr = (nums || reference_token == "-")
  11163. ? detail::value_t::array
  11164. : detail::value_t::object;
  11165. }
  11166. switch (ptr->type())
  11167. {
  11168. case detail::value_t::object:
  11169. {
  11170. // use unchecked object access
  11171. ptr = &ptr->operator[](reference_token);
  11172. break;
  11173. }
  11174. case detail::value_t::array:
  11175. {
  11176. if (reference_token == "-")
  11177. {
  11178. // explicitly treat "-" as index beyond the end
  11179. ptr = &ptr->operator[](ptr->m_value.array->size());
  11180. }
  11181. else
  11182. {
  11183. // convert array index to number; unchecked access
  11184. ptr = &ptr->operator[](array_index(reference_token));
  11185. }
  11186. break;
  11187. }
  11188. case detail::value_t::null:
  11189. case detail::value_t::string:
  11190. case detail::value_t::boolean:
  11191. case detail::value_t::number_integer:
  11192. case detail::value_t::number_unsigned:
  11193. case detail::value_t::number_float:
  11194. case detail::value_t::binary:
  11195. case detail::value_t::discarded:
  11196. default:
  11197. JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", *ptr));
  11198. }
  11199. }
  11200. return *ptr;
  11201. }
  11202. /*!
  11203. @throw parse_error.106 if an array index begins with '0'
  11204. @throw parse_error.109 if an array index was not a number
  11205. @throw out_of_range.402 if the array index '-' is used
  11206. @throw out_of_range.404 if the JSON pointer can not be resolved
  11207. */
  11208. BasicJsonType& get_checked(BasicJsonType* ptr) const
  11209. {
  11210. for (const auto& reference_token : reference_tokens)
  11211. {
  11212. switch (ptr->type())
  11213. {
  11214. case detail::value_t::object:
  11215. {
  11216. // note: at performs range check
  11217. ptr = &ptr->at(reference_token);
  11218. break;
  11219. }
  11220. case detail::value_t::array:
  11221. {
  11222. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  11223. {
  11224. // "-" always fails the range check
  11225. JSON_THROW(detail::out_of_range::create(402,
  11226. "array index '-' (" + std::to_string(ptr->m_value.array->size()) +
  11227. ") is out of range", *ptr));
  11228. }
  11229. // note: at performs range check
  11230. ptr = &ptr->at(array_index(reference_token));
  11231. break;
  11232. }
  11233. case detail::value_t::null:
  11234. case detail::value_t::string:
  11235. case detail::value_t::boolean:
  11236. case detail::value_t::number_integer:
  11237. case detail::value_t::number_unsigned:
  11238. case detail::value_t::number_float:
  11239. case detail::value_t::binary:
  11240. case detail::value_t::discarded:
  11241. default:
  11242. JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", *ptr));
  11243. }
  11244. }
  11245. return *ptr;
  11246. }
  11247. /*!
  11248. @brief return a const reference to the pointed to value
  11249. @param[in] ptr a JSON value
  11250. @return const reference to the JSON value pointed to by the JSON
  11251. pointer
  11252. @throw parse_error.106 if an array index begins with '0'
  11253. @throw parse_error.109 if an array index was not a number
  11254. @throw out_of_range.402 if the array index '-' is used
  11255. @throw out_of_range.404 if the JSON pointer can not be resolved
  11256. */
  11257. const BasicJsonType& get_unchecked(const BasicJsonType* ptr) const
  11258. {
  11259. for (const auto& reference_token : reference_tokens)
  11260. {
  11261. switch (ptr->type())
  11262. {
  11263. case detail::value_t::object:
  11264. {
  11265. // use unchecked object access
  11266. ptr = &ptr->operator[](reference_token);
  11267. break;
  11268. }
  11269. case detail::value_t::array:
  11270. {
  11271. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  11272. {
  11273. // "-" cannot be used for const access
  11274. JSON_THROW(detail::out_of_range::create(402, "array index '-' (" + std::to_string(ptr->m_value.array->size()) + ") is out of range", *ptr));
  11275. }
  11276. // use unchecked array access
  11277. ptr = &ptr->operator[](array_index(reference_token));
  11278. break;
  11279. }
  11280. case detail::value_t::null:
  11281. case detail::value_t::string:
  11282. case detail::value_t::boolean:
  11283. case detail::value_t::number_integer:
  11284. case detail::value_t::number_unsigned:
  11285. case detail::value_t::number_float:
  11286. case detail::value_t::binary:
  11287. case detail::value_t::discarded:
  11288. default:
  11289. JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", *ptr));
  11290. }
  11291. }
  11292. return *ptr;
  11293. }
  11294. /*!
  11295. @throw parse_error.106 if an array index begins with '0'
  11296. @throw parse_error.109 if an array index was not a number
  11297. @throw out_of_range.402 if the array index '-' is used
  11298. @throw out_of_range.404 if the JSON pointer can not be resolved
  11299. */
  11300. const BasicJsonType& get_checked(const BasicJsonType* ptr) const
  11301. {
  11302. for (const auto& reference_token : reference_tokens)
  11303. {
  11304. switch (ptr->type())
  11305. {
  11306. case detail::value_t::object:
  11307. {
  11308. // note: at performs range check
  11309. ptr = &ptr->at(reference_token);
  11310. break;
  11311. }
  11312. case detail::value_t::array:
  11313. {
  11314. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  11315. {
  11316. // "-" always fails the range check
  11317. JSON_THROW(detail::out_of_range::create(402,
  11318. "array index '-' (" + std::to_string(ptr->m_value.array->size()) +
  11319. ") is out of range", *ptr));
  11320. }
  11321. // note: at performs range check
  11322. ptr = &ptr->at(array_index(reference_token));
  11323. break;
  11324. }
  11325. case detail::value_t::null:
  11326. case detail::value_t::string:
  11327. case detail::value_t::boolean:
  11328. case detail::value_t::number_integer:
  11329. case detail::value_t::number_unsigned:
  11330. case detail::value_t::number_float:
  11331. case detail::value_t::binary:
  11332. case detail::value_t::discarded:
  11333. default:
  11334. JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", *ptr));
  11335. }
  11336. }
  11337. return *ptr;
  11338. }
  11339. /*!
  11340. @throw parse_error.106 if an array index begins with '0'
  11341. @throw parse_error.109 if an array index was not a number
  11342. */
  11343. bool contains(const BasicJsonType* ptr) const
  11344. {
  11345. for (const auto& reference_token : reference_tokens)
  11346. {
  11347. switch (ptr->type())
  11348. {
  11349. case detail::value_t::object:
  11350. {
  11351. if (!ptr->contains(reference_token))
  11352. {
  11353. // we did not find the key in the object
  11354. return false;
  11355. }
  11356. ptr = &ptr->operator[](reference_token);
  11357. break;
  11358. }
  11359. case detail::value_t::array:
  11360. {
  11361. if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
  11362. {
  11363. // "-" always fails the range check
  11364. return false;
  11365. }
  11366. if (JSON_HEDLEY_UNLIKELY(reference_token.size() == 1 && !("0" <= reference_token && reference_token <= "9")))
  11367. {
  11368. // invalid char
  11369. return false;
  11370. }
  11371. if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1))
  11372. {
  11373. if (JSON_HEDLEY_UNLIKELY(!('1' <= reference_token[0] && reference_token[0] <= '9')))
  11374. {
  11375. // first char should be between '1' and '9'
  11376. return false;
  11377. }
  11378. for (std::size_t i = 1; i < reference_token.size(); i++)
  11379. {
  11380. if (JSON_HEDLEY_UNLIKELY(!('0' <= reference_token[i] && reference_token[i] <= '9')))
  11381. {
  11382. // other char should be between '0' and '9'
  11383. return false;
  11384. }
  11385. }
  11386. }
  11387. const auto idx = array_index(reference_token);
  11388. if (idx >= ptr->size())
  11389. {
  11390. // index out of range
  11391. return false;
  11392. }
  11393. ptr = &ptr->operator[](idx);
  11394. break;
  11395. }
  11396. case detail::value_t::null:
  11397. case detail::value_t::string:
  11398. case detail::value_t::boolean:
  11399. case detail::value_t::number_integer:
  11400. case detail::value_t::number_unsigned:
  11401. case detail::value_t::number_float:
  11402. case detail::value_t::binary:
  11403. case detail::value_t::discarded:
  11404. default:
  11405. {
  11406. // we do not expect primitive values if there is still a
  11407. // reference token to process
  11408. return false;
  11409. }
  11410. }
  11411. }
  11412. // no reference token left means we found a primitive value
  11413. return true;
  11414. }
  11415. /*!
  11416. @brief split the string input to reference tokens
  11417. @note This function is only called by the json_pointer constructor.
  11418. All exceptions below are documented there.
  11419. @throw parse_error.107 if the pointer is not empty or begins with '/'
  11420. @throw parse_error.108 if character '~' is not followed by '0' or '1'
  11421. */
  11422. static std::vector<std::string> split(const std::string& reference_string)
  11423. {
  11424. std::vector<std::string> result;
  11425. // special case: empty reference string -> no reference tokens
  11426. if (reference_string.empty())
  11427. {
  11428. return result;
  11429. }
  11430. // check if nonempty reference string begins with slash
  11431. if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/'))
  11432. {
  11433. JSON_THROW(detail::parse_error::create(107, 1, "JSON pointer must be empty or begin with '/' - was: '" + reference_string + "'", BasicJsonType()));
  11434. }
  11435. // extract the reference tokens:
  11436. // - slash: position of the last read slash (or end of string)
  11437. // - start: position after the previous slash
  11438. for (
  11439. // search for the first slash after the first character
  11440. std::size_t slash = reference_string.find_first_of('/', 1),
  11441. // set the beginning of the first reference token
  11442. start = 1;
  11443. // we can stop if start == 0 (if slash == std::string::npos)
  11444. start != 0;
  11445. // set the beginning of the next reference token
  11446. // (will eventually be 0 if slash == std::string::npos)
  11447. start = (slash == std::string::npos) ? 0 : slash + 1,
  11448. // find next slash
  11449. slash = reference_string.find_first_of('/', start))
  11450. {
  11451. // use the text between the beginning of the reference token
  11452. // (start) and the last slash (slash).
  11453. auto reference_token = reference_string.substr(start, slash - start);
  11454. // check reference tokens are properly escaped
  11455. for (std::size_t pos = reference_token.find_first_of('~');
  11456. pos != std::string::npos;
  11457. pos = reference_token.find_first_of('~', pos + 1))
  11458. {
  11459. JSON_ASSERT(reference_token[pos] == '~');
  11460. // ~ must be followed by 0 or 1
  11461. if (JSON_HEDLEY_UNLIKELY(pos == reference_token.size() - 1 ||
  11462. (reference_token[pos + 1] != '0' &&
  11463. reference_token[pos + 1] != '1')))
  11464. {
  11465. JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'", BasicJsonType()));
  11466. }
  11467. }
  11468. // finally, store the reference token
  11469. detail::unescape(reference_token);
  11470. result.push_back(reference_token);
  11471. }
  11472. return result;
  11473. }
  11474. private:
  11475. /*!
  11476. @param[in] reference_string the reference string to the current value
  11477. @param[in] value the value to consider
  11478. @param[in,out] result the result object to insert values to
  11479. @note Empty objects or arrays are flattened to `null`.
  11480. */
  11481. static void flatten(const std::string& reference_string,
  11482. const BasicJsonType& value,
  11483. BasicJsonType& result)
  11484. {
  11485. switch (value.type())
  11486. {
  11487. case detail::value_t::array:
  11488. {
  11489. if (value.m_value.array->empty())
  11490. {
  11491. // flatten empty array as null
  11492. result[reference_string] = nullptr;
  11493. }
  11494. else
  11495. {
  11496. // iterate array and use index as reference string
  11497. for (std::size_t i = 0; i < value.m_value.array->size(); ++i)
  11498. {
  11499. flatten(reference_string + "/" + std::to_string(i),
  11500. value.m_value.array->operator[](i), result);
  11501. }
  11502. }
  11503. break;
  11504. }
  11505. case detail::value_t::object:
  11506. {
  11507. if (value.m_value.object->empty())
  11508. {
  11509. // flatten empty object as null
  11510. result[reference_string] = nullptr;
  11511. }
  11512. else
  11513. {
  11514. // iterate object and use keys as reference string
  11515. for (const auto& element : *value.m_value.object)
  11516. {
  11517. flatten(reference_string + "/" + detail::escape(element.first), element.second, result);
  11518. }
  11519. }
  11520. break;
  11521. }
  11522. case detail::value_t::null:
  11523. case detail::value_t::string:
  11524. case detail::value_t::boolean:
  11525. case detail::value_t::number_integer:
  11526. case detail::value_t::number_unsigned:
  11527. case detail::value_t::number_float:
  11528. case detail::value_t::binary:
  11529. case detail::value_t::discarded:
  11530. default:
  11531. {
  11532. // add primitive value with its reference string
  11533. result[reference_string] = value;
  11534. break;
  11535. }
  11536. }
  11537. }
  11538. /*!
  11539. @param[in] value flattened JSON
  11540. @return unflattened JSON
  11541. @throw parse_error.109 if array index is not a number
  11542. @throw type_error.314 if value is not an object
  11543. @throw type_error.315 if object values are not primitive
  11544. @throw type_error.313 if value cannot be unflattened
  11545. */
  11546. static BasicJsonType
  11547. unflatten(const BasicJsonType& value)
  11548. {
  11549. if (JSON_HEDLEY_UNLIKELY(!value.is_object()))
  11550. {
  11551. JSON_THROW(detail::type_error::create(314, "only objects can be unflattened", value));
  11552. }
  11553. BasicJsonType result;
  11554. // iterate the JSON object values
  11555. for (const auto& element : *value.m_value.object)
  11556. {
  11557. if (JSON_HEDLEY_UNLIKELY(!element.second.is_primitive()))
  11558. {
  11559. JSON_THROW(detail::type_error::create(315, "values in object must be primitive", element.second));
  11560. }
  11561. // assign value to reference pointed to by JSON pointer; Note that if
  11562. // the JSON pointer is "" (i.e., points to the whole value), function
  11563. // get_and_create returns a reference to result itself. An assignment
  11564. // will then create a primitive value.
  11565. json_pointer(element.first).get_and_create(result) = element.second;
  11566. }
  11567. return result;
  11568. }
  11569. /*!
  11570. @brief compares two JSON pointers for equality
  11571. @param[in] lhs JSON pointer to compare
  11572. @param[in] rhs JSON pointer to compare
  11573. @return whether @a lhs is equal to @a rhs
  11574. @complexity Linear in the length of the JSON pointer
  11575. @exceptionsafety No-throw guarantee: this function never throws exceptions.
  11576. */
  11577. friend bool operator==(json_pointer const& lhs,
  11578. json_pointer const& rhs) noexcept
  11579. {
  11580. return lhs.reference_tokens == rhs.reference_tokens;
  11581. }
  11582. /*!
  11583. @brief compares two JSON pointers for inequality
  11584. @param[in] lhs JSON pointer to compare
  11585. @param[in] rhs JSON pointer to compare
  11586. @return whether @a lhs is not equal @a rhs
  11587. @complexity Linear in the length of the JSON pointer
  11588. @exceptionsafety No-throw guarantee: this function never throws exceptions.
  11589. */
  11590. friend bool operator!=(json_pointer const& lhs,
  11591. json_pointer const& rhs) noexcept
  11592. {
  11593. return !(lhs == rhs);
  11594. }
  11595. /// the reference tokens
  11596. std::vector<std::string> reference_tokens;
  11597. };
  11598. } // namespace nlohmann
  11599. // #include <nlohmann/detail/json_ref.hpp>
  11600. #include <initializer_list>
  11601. #include <utility>
  11602. // #include <nlohmann/detail/meta/type_traits.hpp>
  11603. namespace nlohmann
  11604. {
  11605. namespace detail
  11606. {
  11607. template<typename BasicJsonType>
  11608. class json_ref
  11609. {
  11610. public:
  11611. using value_type = BasicJsonType;
  11612. json_ref(value_type&& value)
  11613. : owned_value(std::move(value))
  11614. {}
  11615. json_ref(const value_type& value)
  11616. : value_ref(&value)
  11617. {}
  11618. json_ref(std::initializer_list<json_ref> init)
  11619. : owned_value(init)
  11620. {}
  11621. template <
  11622. class... Args,
  11623. enable_if_t<std::is_constructible<value_type, Args...>::value, int> = 0 >
  11624. json_ref(Args && ... args)
  11625. : owned_value(std::forward<Args>(args)...)
  11626. {}
  11627. // class should be movable only
  11628. json_ref(json_ref&&) noexcept = default;
  11629. json_ref(const json_ref&) = delete;
  11630. json_ref& operator=(const json_ref&) = delete;
  11631. json_ref& operator=(json_ref&&) = delete;
  11632. ~json_ref() = default;
  11633. value_type moved_or_copied() const
  11634. {
  11635. if (value_ref == nullptr)
  11636. {
  11637. return std::move(owned_value);
  11638. }
  11639. return *value_ref;
  11640. }
  11641. value_type const& operator*() const
  11642. {
  11643. return value_ref ? *value_ref : owned_value;
  11644. }
  11645. value_type const* operator->() const
  11646. {
  11647. return &** this;
  11648. }
  11649. private:
  11650. mutable value_type owned_value = nullptr;
  11651. value_type const* value_ref = nullptr;
  11652. };
  11653. } // namespace detail
  11654. } // namespace nlohmann
  11655. // #include <nlohmann/detail/macro_scope.hpp>
  11656. // #include <nlohmann/detail/string_escape.hpp>
  11657. // #include <nlohmann/detail/meta/cpp_future.hpp>
  11658. // #include <nlohmann/detail/meta/type_traits.hpp>
  11659. // #include <nlohmann/detail/output/binary_writer.hpp>
  11660. #include <algorithm> // reverse
  11661. #include <array> // array
  11662. #include <cmath> // isnan, isinf
  11663. #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
  11664. #include <cstring> // memcpy
  11665. #include <limits> // numeric_limits
  11666. #include <string> // string
  11667. #include <utility> // move
  11668. // #include <nlohmann/detail/input/binary_reader.hpp>
  11669. // #include <nlohmann/detail/macro_scope.hpp>
  11670. // #include <nlohmann/detail/output/output_adapters.hpp>
  11671. #include <algorithm> // copy
  11672. #include <cstddef> // size_t
  11673. #include <iterator> // back_inserter
  11674. #include <memory> // shared_ptr, make_shared
  11675. #include <string> // basic_string
  11676. #include <vector> // vector
  11677. #ifndef JSON_NO_IO
  11678. #include <ios> // streamsize
  11679. #include <ostream> // basic_ostream
  11680. #endif // JSON_NO_IO
  11681. // #include <nlohmann/detail/macro_scope.hpp>
  11682. namespace nlohmann
  11683. {
  11684. namespace detail
  11685. {
  11686. /// abstract output adapter interface
  11687. template<typename CharType> struct output_adapter_protocol
  11688. {
  11689. virtual void write_character(CharType c) = 0;
  11690. virtual void write_characters(const CharType* s, std::size_t length) = 0;
  11691. virtual ~output_adapter_protocol() = default;
  11692. output_adapter_protocol() = default;
  11693. output_adapter_protocol(const output_adapter_protocol&) = default;
  11694. output_adapter_protocol(output_adapter_protocol&&) noexcept = default;
  11695. output_adapter_protocol& operator=(const output_adapter_protocol&) = default;
  11696. output_adapter_protocol& operator=(output_adapter_protocol&&) noexcept = default;
  11697. };
  11698. /// a type to simplify interfaces
  11699. template<typename CharType>
  11700. using output_adapter_t = std::shared_ptr<output_adapter_protocol<CharType>>;
  11701. /// output adapter for byte vectors
  11702. template<typename CharType>
  11703. class output_vector_adapter : public output_adapter_protocol<CharType>
  11704. {
  11705. public:
  11706. explicit output_vector_adapter(std::vector<CharType>& vec) noexcept
  11707. : v(vec)
  11708. {}
  11709. void write_character(CharType c) override
  11710. {
  11711. v.push_back(c);
  11712. }
  11713. JSON_HEDLEY_NON_NULL(2)
  11714. void write_characters(const CharType* s, std::size_t length) override
  11715. {
  11716. std::copy(s, s + length, std::back_inserter(v));
  11717. }
  11718. private:
  11719. std::vector<CharType>& v;
  11720. };
  11721. #ifndef JSON_NO_IO
  11722. /// output adapter for output streams
  11723. template<typename CharType>
  11724. class output_stream_adapter : public output_adapter_protocol<CharType>
  11725. {
  11726. public:
  11727. explicit output_stream_adapter(std::basic_ostream<CharType>& s) noexcept
  11728. : stream(s)
  11729. {}
  11730. void write_character(CharType c) override
  11731. {
  11732. stream.put(c);
  11733. }
  11734. JSON_HEDLEY_NON_NULL(2)
  11735. void write_characters(const CharType* s, std::size_t length) override
  11736. {
  11737. stream.write(s, static_cast<std::streamsize>(length));
  11738. }
  11739. private:
  11740. std::basic_ostream<CharType>& stream;
  11741. };
  11742. #endif // JSON_NO_IO
  11743. /// output adapter for basic_string
  11744. template<typename CharType, typename StringType = std::basic_string<CharType>>
  11745. class output_string_adapter : public output_adapter_protocol<CharType>
  11746. {
  11747. public:
  11748. explicit output_string_adapter(StringType& s) noexcept
  11749. : str(s)
  11750. {}
  11751. void write_character(CharType c) override
  11752. {
  11753. str.push_back(c);
  11754. }
  11755. JSON_HEDLEY_NON_NULL(2)
  11756. void write_characters(const CharType* s, std::size_t length) override
  11757. {
  11758. str.append(s, length);
  11759. }
  11760. private:
  11761. StringType& str;
  11762. };
  11763. template<typename CharType, typename StringType = std::basic_string<CharType>>
  11764. class output_adapter
  11765. {
  11766. public:
  11767. output_adapter(std::vector<CharType>& vec)
  11768. : oa(std::make_shared<output_vector_adapter<CharType>>(vec)) {}
  11769. #ifndef JSON_NO_IO
  11770. output_adapter(std::basic_ostream<CharType>& s)
  11771. : oa(std::make_shared<output_stream_adapter<CharType>>(s)) {}
  11772. #endif // JSON_NO_IO
  11773. output_adapter(StringType& s)
  11774. : oa(std::make_shared<output_string_adapter<CharType, StringType>>(s)) {}
  11775. operator output_adapter_t<CharType>()
  11776. {
  11777. return oa;
  11778. }
  11779. private:
  11780. output_adapter_t<CharType> oa = nullptr;
  11781. };
  11782. } // namespace detail
  11783. } // namespace nlohmann
  11784. namespace nlohmann
  11785. {
  11786. namespace detail
  11787. {
  11788. ///////////////////
  11789. // binary writer //
  11790. ///////////////////
  11791. /*!
  11792. @brief serialization to CBOR and MessagePack values
  11793. */
  11794. template<typename BasicJsonType, typename CharType>
  11795. class binary_writer
  11796. {
  11797. using string_t = typename BasicJsonType::string_t;
  11798. using binary_t = typename BasicJsonType::binary_t;
  11799. using number_float_t = typename BasicJsonType::number_float_t;
  11800. public:
  11801. /*!
  11802. @brief create a binary writer
  11803. @param[in] adapter output adapter to write to
  11804. */
  11805. explicit binary_writer(output_adapter_t<CharType> adapter) : oa(std::move(adapter))
  11806. {
  11807. JSON_ASSERT(oa);
  11808. }
  11809. /*!
  11810. @param[in] j JSON value to serialize
  11811. @pre j.type() == value_t::object
  11812. */
  11813. void write_bson(const BasicJsonType& j)
  11814. {
  11815. switch (j.type())
  11816. {
  11817. case value_t::object:
  11818. {
  11819. write_bson_object(*j.m_value.object);
  11820. break;
  11821. }
  11822. case value_t::null:
  11823. case value_t::array:
  11824. case value_t::string:
  11825. case value_t::boolean:
  11826. case value_t::number_integer:
  11827. case value_t::number_unsigned:
  11828. case value_t::number_float:
  11829. case value_t::binary:
  11830. case value_t::discarded:
  11831. default:
  11832. {
  11833. JSON_THROW(type_error::create(317, "to serialize to BSON, top-level type must be object, but is " + std::string(j.type_name()), j));
  11834. }
  11835. }
  11836. }
  11837. /*!
  11838. @param[in] j JSON value to serialize
  11839. */
  11840. void write_cbor(const BasicJsonType& j)
  11841. {
  11842. switch (j.type())
  11843. {
  11844. case value_t::null:
  11845. {
  11846. oa->write_character(to_char_type(0xF6));
  11847. break;
  11848. }
  11849. case value_t::boolean:
  11850. {
  11851. oa->write_character(j.m_value.boolean
  11852. ? to_char_type(0xF5)
  11853. : to_char_type(0xF4));
  11854. break;
  11855. }
  11856. case value_t::number_integer:
  11857. {
  11858. if (j.m_value.number_integer >= 0)
  11859. {
  11860. // CBOR does not differentiate between positive signed
  11861. // integers and unsigned integers. Therefore, we used the
  11862. // code from the value_t::number_unsigned case here.
  11863. if (j.m_value.number_integer <= 0x17)
  11864. {
  11865. write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
  11866. }
  11867. else if (j.m_value.number_integer <= (std::numeric_limits<std::uint8_t>::max)())
  11868. {
  11869. oa->write_character(to_char_type(0x18));
  11870. write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
  11871. }
  11872. else if (j.m_value.number_integer <= (std::numeric_limits<std::uint16_t>::max)())
  11873. {
  11874. oa->write_character(to_char_type(0x19));
  11875. write_number(static_cast<std::uint16_t>(j.m_value.number_integer));
  11876. }
  11877. else if (j.m_value.number_integer <= (std::numeric_limits<std::uint32_t>::max)())
  11878. {
  11879. oa->write_character(to_char_type(0x1A));
  11880. write_number(static_cast<std::uint32_t>(j.m_value.number_integer));
  11881. }
  11882. else
  11883. {
  11884. oa->write_character(to_char_type(0x1B));
  11885. write_number(static_cast<std::uint64_t>(j.m_value.number_integer));
  11886. }
  11887. }
  11888. else
  11889. {
  11890. // The conversions below encode the sign in the first
  11891. // byte, and the value is converted to a positive number.
  11892. const auto positive_number = -1 - j.m_value.number_integer;
  11893. if (j.m_value.number_integer >= -24)
  11894. {
  11895. write_number(static_cast<std::uint8_t>(0x20 + positive_number));
  11896. }
  11897. else if (positive_number <= (std::numeric_limits<std::uint8_t>::max)())
  11898. {
  11899. oa->write_character(to_char_type(0x38));
  11900. write_number(static_cast<std::uint8_t>(positive_number));
  11901. }
  11902. else if (positive_number <= (std::numeric_limits<std::uint16_t>::max)())
  11903. {
  11904. oa->write_character(to_char_type(0x39));
  11905. write_number(static_cast<std::uint16_t>(positive_number));
  11906. }
  11907. else if (positive_number <= (std::numeric_limits<std::uint32_t>::max)())
  11908. {
  11909. oa->write_character(to_char_type(0x3A));
  11910. write_number(static_cast<std::uint32_t>(positive_number));
  11911. }
  11912. else
  11913. {
  11914. oa->write_character(to_char_type(0x3B));
  11915. write_number(static_cast<std::uint64_t>(positive_number));
  11916. }
  11917. }
  11918. break;
  11919. }
  11920. case value_t::number_unsigned:
  11921. {
  11922. if (j.m_value.number_unsigned <= 0x17)
  11923. {
  11924. write_number(static_cast<std::uint8_t>(j.m_value.number_unsigned));
  11925. }
  11926. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
  11927. {
  11928. oa->write_character(to_char_type(0x18));
  11929. write_number(static_cast<std::uint8_t>(j.m_value.number_unsigned));
  11930. }
  11931. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
  11932. {
  11933. oa->write_character(to_char_type(0x19));
  11934. write_number(static_cast<std::uint16_t>(j.m_value.number_unsigned));
  11935. }
  11936. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
  11937. {
  11938. oa->write_character(to_char_type(0x1A));
  11939. write_number(static_cast<std::uint32_t>(j.m_value.number_unsigned));
  11940. }
  11941. else
  11942. {
  11943. oa->write_character(to_char_type(0x1B));
  11944. write_number(static_cast<std::uint64_t>(j.m_value.number_unsigned));
  11945. }
  11946. break;
  11947. }
  11948. case value_t::number_float:
  11949. {
  11950. if (std::isnan(j.m_value.number_float))
  11951. {
  11952. // NaN is 0xf97e00 in CBOR
  11953. oa->write_character(to_char_type(0xF9));
  11954. oa->write_character(to_char_type(0x7E));
  11955. oa->write_character(to_char_type(0x00));
  11956. }
  11957. else if (std::isinf(j.m_value.number_float))
  11958. {
  11959. // Infinity is 0xf97c00, -Infinity is 0xf9fc00
  11960. oa->write_character(to_char_type(0xf9));
  11961. oa->write_character(j.m_value.number_float > 0 ? to_char_type(0x7C) : to_char_type(0xFC));
  11962. oa->write_character(to_char_type(0x00));
  11963. }
  11964. else
  11965. {
  11966. write_compact_float(j.m_value.number_float, detail::input_format_t::cbor);
  11967. }
  11968. break;
  11969. }
  11970. case value_t::string:
  11971. {
  11972. // step 1: write control byte and the string length
  11973. const auto N = j.m_value.string->size();
  11974. if (N <= 0x17)
  11975. {
  11976. write_number(static_cast<std::uint8_t>(0x60 + N));
  11977. }
  11978. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  11979. {
  11980. oa->write_character(to_char_type(0x78));
  11981. write_number(static_cast<std::uint8_t>(N));
  11982. }
  11983. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  11984. {
  11985. oa->write_character(to_char_type(0x79));
  11986. write_number(static_cast<std::uint16_t>(N));
  11987. }
  11988. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  11989. {
  11990. oa->write_character(to_char_type(0x7A));
  11991. write_number(static_cast<std::uint32_t>(N));
  11992. }
  11993. // LCOV_EXCL_START
  11994. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  11995. {
  11996. oa->write_character(to_char_type(0x7B));
  11997. write_number(static_cast<std::uint64_t>(N));
  11998. }
  11999. // LCOV_EXCL_STOP
  12000. // step 2: write the string
  12001. oa->write_characters(
  12002. reinterpret_cast<const CharType*>(j.m_value.string->c_str()),
  12003. j.m_value.string->size());
  12004. break;
  12005. }
  12006. case value_t::array:
  12007. {
  12008. // step 1: write control byte and the array size
  12009. const auto N = j.m_value.array->size();
  12010. if (N <= 0x17)
  12011. {
  12012. write_number(static_cast<std::uint8_t>(0x80 + N));
  12013. }
  12014. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  12015. {
  12016. oa->write_character(to_char_type(0x98));
  12017. write_number(static_cast<std::uint8_t>(N));
  12018. }
  12019. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  12020. {
  12021. oa->write_character(to_char_type(0x99));
  12022. write_number(static_cast<std::uint16_t>(N));
  12023. }
  12024. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  12025. {
  12026. oa->write_character(to_char_type(0x9A));
  12027. write_number(static_cast<std::uint32_t>(N));
  12028. }
  12029. // LCOV_EXCL_START
  12030. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  12031. {
  12032. oa->write_character(to_char_type(0x9B));
  12033. write_number(static_cast<std::uint64_t>(N));
  12034. }
  12035. // LCOV_EXCL_STOP
  12036. // step 2: write each element
  12037. for (const auto& el : *j.m_value.array)
  12038. {
  12039. write_cbor(el);
  12040. }
  12041. break;
  12042. }
  12043. case value_t::binary:
  12044. {
  12045. if (j.m_value.binary->has_subtype())
  12046. {
  12047. if (j.m_value.binary->subtype() <= (std::numeric_limits<std::uint8_t>::max)())
  12048. {
  12049. write_number(static_cast<std::uint8_t>(0xd8));
  12050. write_number(static_cast<std::uint8_t>(j.m_value.binary->subtype()));
  12051. }
  12052. else if (j.m_value.binary->subtype() <= (std::numeric_limits<std::uint16_t>::max)())
  12053. {
  12054. write_number(static_cast<std::uint8_t>(0xd9));
  12055. write_number(static_cast<std::uint16_t>(j.m_value.binary->subtype()));
  12056. }
  12057. else if (j.m_value.binary->subtype() <= (std::numeric_limits<std::uint32_t>::max)())
  12058. {
  12059. write_number(static_cast<std::uint8_t>(0xda));
  12060. write_number(static_cast<std::uint32_t>(j.m_value.binary->subtype()));
  12061. }
  12062. else if (j.m_value.binary->subtype() <= (std::numeric_limits<std::uint64_t>::max)())
  12063. {
  12064. write_number(static_cast<std::uint8_t>(0xdb));
  12065. write_number(static_cast<std::uint64_t>(j.m_value.binary->subtype()));
  12066. }
  12067. }
  12068. // step 1: write control byte and the binary array size
  12069. const auto N = j.m_value.binary->size();
  12070. if (N <= 0x17)
  12071. {
  12072. write_number(static_cast<std::uint8_t>(0x40 + N));
  12073. }
  12074. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  12075. {
  12076. oa->write_character(to_char_type(0x58));
  12077. write_number(static_cast<std::uint8_t>(N));
  12078. }
  12079. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  12080. {
  12081. oa->write_character(to_char_type(0x59));
  12082. write_number(static_cast<std::uint16_t>(N));
  12083. }
  12084. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  12085. {
  12086. oa->write_character(to_char_type(0x5A));
  12087. write_number(static_cast<std::uint32_t>(N));
  12088. }
  12089. // LCOV_EXCL_START
  12090. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  12091. {
  12092. oa->write_character(to_char_type(0x5B));
  12093. write_number(static_cast<std::uint64_t>(N));
  12094. }
  12095. // LCOV_EXCL_STOP
  12096. // step 2: write each element
  12097. oa->write_characters(
  12098. reinterpret_cast<const CharType*>(j.m_value.binary->data()),
  12099. N);
  12100. break;
  12101. }
  12102. case value_t::object:
  12103. {
  12104. // step 1: write control byte and the object size
  12105. const auto N = j.m_value.object->size();
  12106. if (N <= 0x17)
  12107. {
  12108. write_number(static_cast<std::uint8_t>(0xA0 + N));
  12109. }
  12110. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  12111. {
  12112. oa->write_character(to_char_type(0xB8));
  12113. write_number(static_cast<std::uint8_t>(N));
  12114. }
  12115. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  12116. {
  12117. oa->write_character(to_char_type(0xB9));
  12118. write_number(static_cast<std::uint16_t>(N));
  12119. }
  12120. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  12121. {
  12122. oa->write_character(to_char_type(0xBA));
  12123. write_number(static_cast<std::uint32_t>(N));
  12124. }
  12125. // LCOV_EXCL_START
  12126. else if (N <= (std::numeric_limits<std::uint64_t>::max)())
  12127. {
  12128. oa->write_character(to_char_type(0xBB));
  12129. write_number(static_cast<std::uint64_t>(N));
  12130. }
  12131. // LCOV_EXCL_STOP
  12132. // step 2: write each element
  12133. for (const auto& el : *j.m_value.object)
  12134. {
  12135. write_cbor(el.first);
  12136. write_cbor(el.second);
  12137. }
  12138. break;
  12139. }
  12140. case value_t::discarded:
  12141. default:
  12142. break;
  12143. }
  12144. }
  12145. /*!
  12146. @param[in] j JSON value to serialize
  12147. */
  12148. void write_msgpack(const BasicJsonType& j)
  12149. {
  12150. switch (j.type())
  12151. {
  12152. case value_t::null: // nil
  12153. {
  12154. oa->write_character(to_char_type(0xC0));
  12155. break;
  12156. }
  12157. case value_t::boolean: // true and false
  12158. {
  12159. oa->write_character(j.m_value.boolean
  12160. ? to_char_type(0xC3)
  12161. : to_char_type(0xC2));
  12162. break;
  12163. }
  12164. case value_t::number_integer:
  12165. {
  12166. if (j.m_value.number_integer >= 0)
  12167. {
  12168. // MessagePack does not differentiate between positive
  12169. // signed integers and unsigned integers. Therefore, we used
  12170. // the code from the value_t::number_unsigned case here.
  12171. if (j.m_value.number_unsigned < 128)
  12172. {
  12173. // positive fixnum
  12174. write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
  12175. }
  12176. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
  12177. {
  12178. // uint 8
  12179. oa->write_character(to_char_type(0xCC));
  12180. write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
  12181. }
  12182. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
  12183. {
  12184. // uint 16
  12185. oa->write_character(to_char_type(0xCD));
  12186. write_number(static_cast<std::uint16_t>(j.m_value.number_integer));
  12187. }
  12188. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
  12189. {
  12190. // uint 32
  12191. oa->write_character(to_char_type(0xCE));
  12192. write_number(static_cast<std::uint32_t>(j.m_value.number_integer));
  12193. }
  12194. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
  12195. {
  12196. // uint 64
  12197. oa->write_character(to_char_type(0xCF));
  12198. write_number(static_cast<std::uint64_t>(j.m_value.number_integer));
  12199. }
  12200. }
  12201. else
  12202. {
  12203. if (j.m_value.number_integer >= -32)
  12204. {
  12205. // negative fixnum
  12206. write_number(static_cast<std::int8_t>(j.m_value.number_integer));
  12207. }
  12208. else if (j.m_value.number_integer >= (std::numeric_limits<std::int8_t>::min)() &&
  12209. j.m_value.number_integer <= (std::numeric_limits<std::int8_t>::max)())
  12210. {
  12211. // int 8
  12212. oa->write_character(to_char_type(0xD0));
  12213. write_number(static_cast<std::int8_t>(j.m_value.number_integer));
  12214. }
  12215. else if (j.m_value.number_integer >= (std::numeric_limits<std::int16_t>::min)() &&
  12216. j.m_value.number_integer <= (std::numeric_limits<std::int16_t>::max)())
  12217. {
  12218. // int 16
  12219. oa->write_character(to_char_type(0xD1));
  12220. write_number(static_cast<std::int16_t>(j.m_value.number_integer));
  12221. }
  12222. else if (j.m_value.number_integer >= (std::numeric_limits<std::int32_t>::min)() &&
  12223. j.m_value.number_integer <= (std::numeric_limits<std::int32_t>::max)())
  12224. {
  12225. // int 32
  12226. oa->write_character(to_char_type(0xD2));
  12227. write_number(static_cast<std::int32_t>(j.m_value.number_integer));
  12228. }
  12229. else if (j.m_value.number_integer >= (std::numeric_limits<std::int64_t>::min)() &&
  12230. j.m_value.number_integer <= (std::numeric_limits<std::int64_t>::max)())
  12231. {
  12232. // int 64
  12233. oa->write_character(to_char_type(0xD3));
  12234. write_number(static_cast<std::int64_t>(j.m_value.number_integer));
  12235. }
  12236. }
  12237. break;
  12238. }
  12239. case value_t::number_unsigned:
  12240. {
  12241. if (j.m_value.number_unsigned < 128)
  12242. {
  12243. // positive fixnum
  12244. write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
  12245. }
  12246. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
  12247. {
  12248. // uint 8
  12249. oa->write_character(to_char_type(0xCC));
  12250. write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
  12251. }
  12252. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
  12253. {
  12254. // uint 16
  12255. oa->write_character(to_char_type(0xCD));
  12256. write_number(static_cast<std::uint16_t>(j.m_value.number_integer));
  12257. }
  12258. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
  12259. {
  12260. // uint 32
  12261. oa->write_character(to_char_type(0xCE));
  12262. write_number(static_cast<std::uint32_t>(j.m_value.number_integer));
  12263. }
  12264. else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
  12265. {
  12266. // uint 64
  12267. oa->write_character(to_char_type(0xCF));
  12268. write_number(static_cast<std::uint64_t>(j.m_value.number_integer));
  12269. }
  12270. break;
  12271. }
  12272. case value_t::number_float:
  12273. {
  12274. write_compact_float(j.m_value.number_float, detail::input_format_t::msgpack);
  12275. break;
  12276. }
  12277. case value_t::string:
  12278. {
  12279. // step 1: write control byte and the string length
  12280. const auto N = j.m_value.string->size();
  12281. if (N <= 31)
  12282. {
  12283. // fixstr
  12284. write_number(static_cast<std::uint8_t>(0xA0 | N));
  12285. }
  12286. else if (N <= (std::numeric_limits<std::uint8_t>::max)())
  12287. {
  12288. // str 8
  12289. oa->write_character(to_char_type(0xD9));
  12290. write_number(static_cast<std::uint8_t>(N));
  12291. }
  12292. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  12293. {
  12294. // str 16
  12295. oa->write_character(to_char_type(0xDA));
  12296. write_number(static_cast<std::uint16_t>(N));
  12297. }
  12298. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  12299. {
  12300. // str 32
  12301. oa->write_character(to_char_type(0xDB));
  12302. write_number(static_cast<std::uint32_t>(N));
  12303. }
  12304. // step 2: write the string
  12305. oa->write_characters(
  12306. reinterpret_cast<const CharType*>(j.m_value.string->c_str()),
  12307. j.m_value.string->size());
  12308. break;
  12309. }
  12310. case value_t::array:
  12311. {
  12312. // step 1: write control byte and the array size
  12313. const auto N = j.m_value.array->size();
  12314. if (N <= 15)
  12315. {
  12316. // fixarray
  12317. write_number(static_cast<std::uint8_t>(0x90 | N));
  12318. }
  12319. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  12320. {
  12321. // array 16
  12322. oa->write_character(to_char_type(0xDC));
  12323. write_number(static_cast<std::uint16_t>(N));
  12324. }
  12325. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  12326. {
  12327. // array 32
  12328. oa->write_character(to_char_type(0xDD));
  12329. write_number(static_cast<std::uint32_t>(N));
  12330. }
  12331. // step 2: write each element
  12332. for (const auto& el : *j.m_value.array)
  12333. {
  12334. write_msgpack(el);
  12335. }
  12336. break;
  12337. }
  12338. case value_t::binary:
  12339. {
  12340. // step 0: determine if the binary type has a set subtype to
  12341. // determine whether or not to use the ext or fixext types
  12342. const bool use_ext = j.m_value.binary->has_subtype();
  12343. // step 1: write control byte and the byte string length
  12344. const auto N = j.m_value.binary->size();
  12345. if (N <= (std::numeric_limits<std::uint8_t>::max)())
  12346. {
  12347. std::uint8_t output_type{};
  12348. bool fixed = true;
  12349. if (use_ext)
  12350. {
  12351. switch (N)
  12352. {
  12353. case 1:
  12354. output_type = 0xD4; // fixext 1
  12355. break;
  12356. case 2:
  12357. output_type = 0xD5; // fixext 2
  12358. break;
  12359. case 4:
  12360. output_type = 0xD6; // fixext 4
  12361. break;
  12362. case 8:
  12363. output_type = 0xD7; // fixext 8
  12364. break;
  12365. case 16:
  12366. output_type = 0xD8; // fixext 16
  12367. break;
  12368. default:
  12369. output_type = 0xC7; // ext 8
  12370. fixed = false;
  12371. break;
  12372. }
  12373. }
  12374. else
  12375. {
  12376. output_type = 0xC4; // bin 8
  12377. fixed = false;
  12378. }
  12379. oa->write_character(to_char_type(output_type));
  12380. if (!fixed)
  12381. {
  12382. write_number(static_cast<std::uint8_t>(N));
  12383. }
  12384. }
  12385. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  12386. {
  12387. std::uint8_t output_type = use_ext
  12388. ? 0xC8 // ext 16
  12389. : 0xC5; // bin 16
  12390. oa->write_character(to_char_type(output_type));
  12391. write_number(static_cast<std::uint16_t>(N));
  12392. }
  12393. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  12394. {
  12395. std::uint8_t output_type = use_ext
  12396. ? 0xC9 // ext 32
  12397. : 0xC6; // bin 32
  12398. oa->write_character(to_char_type(output_type));
  12399. write_number(static_cast<std::uint32_t>(N));
  12400. }
  12401. // step 1.5: if this is an ext type, write the subtype
  12402. if (use_ext)
  12403. {
  12404. write_number(static_cast<std::int8_t>(j.m_value.binary->subtype()));
  12405. }
  12406. // step 2: write the byte string
  12407. oa->write_characters(
  12408. reinterpret_cast<const CharType*>(j.m_value.binary->data()),
  12409. N);
  12410. break;
  12411. }
  12412. case value_t::object:
  12413. {
  12414. // step 1: write control byte and the object size
  12415. const auto N = j.m_value.object->size();
  12416. if (N <= 15)
  12417. {
  12418. // fixmap
  12419. write_number(static_cast<std::uint8_t>(0x80 | (N & 0xF)));
  12420. }
  12421. else if (N <= (std::numeric_limits<std::uint16_t>::max)())
  12422. {
  12423. // map 16
  12424. oa->write_character(to_char_type(0xDE));
  12425. write_number(static_cast<std::uint16_t>(N));
  12426. }
  12427. else if (N <= (std::numeric_limits<std::uint32_t>::max)())
  12428. {
  12429. // map 32
  12430. oa->write_character(to_char_type(0xDF));
  12431. write_number(static_cast<std::uint32_t>(N));
  12432. }
  12433. // step 2: write each element
  12434. for (const auto& el : *j.m_value.object)
  12435. {
  12436. write_msgpack(el.first);
  12437. write_msgpack(el.second);
  12438. }
  12439. break;
  12440. }
  12441. case value_t::discarded:
  12442. default:
  12443. break;
  12444. }
  12445. }
  12446. /*!
  12447. @param[in] j JSON value to serialize
  12448. @param[in] use_count whether to use '#' prefixes (optimized format)
  12449. @param[in] use_type whether to use '$' prefixes (optimized format)
  12450. @param[in] add_prefix whether prefixes need to be used for this value
  12451. */
  12452. void write_ubjson(const BasicJsonType& j, const bool use_count,
  12453. const bool use_type, const bool add_prefix = true)
  12454. {
  12455. switch (j.type())
  12456. {
  12457. case value_t::null:
  12458. {
  12459. if (add_prefix)
  12460. {
  12461. oa->write_character(to_char_type('Z'));
  12462. }
  12463. break;
  12464. }
  12465. case value_t::boolean:
  12466. {
  12467. if (add_prefix)
  12468. {
  12469. oa->write_character(j.m_value.boolean
  12470. ? to_char_type('T')
  12471. : to_char_type('F'));
  12472. }
  12473. break;
  12474. }
  12475. case value_t::number_integer:
  12476. {
  12477. write_number_with_ubjson_prefix(j.m_value.number_integer, add_prefix);
  12478. break;
  12479. }
  12480. case value_t::number_unsigned:
  12481. {
  12482. write_number_with_ubjson_prefix(j.m_value.number_unsigned, add_prefix);
  12483. break;
  12484. }
  12485. case value_t::number_float:
  12486. {
  12487. write_number_with_ubjson_prefix(j.m_value.number_float, add_prefix);
  12488. break;
  12489. }
  12490. case value_t::string:
  12491. {
  12492. if (add_prefix)
  12493. {
  12494. oa->write_character(to_char_type('S'));
  12495. }
  12496. write_number_with_ubjson_prefix(j.m_value.string->size(), true);
  12497. oa->write_characters(
  12498. reinterpret_cast<const CharType*>(j.m_value.string->c_str()),
  12499. j.m_value.string->size());
  12500. break;
  12501. }
  12502. case value_t::array:
  12503. {
  12504. if (add_prefix)
  12505. {
  12506. oa->write_character(to_char_type('['));
  12507. }
  12508. bool prefix_required = true;
  12509. if (use_type && !j.m_value.array->empty())
  12510. {
  12511. JSON_ASSERT(use_count);
  12512. const CharType first_prefix = ubjson_prefix(j.front());
  12513. const bool same_prefix = std::all_of(j.begin() + 1, j.end(),
  12514. [this, first_prefix](const BasicJsonType & v)
  12515. {
  12516. return ubjson_prefix(v) == first_prefix;
  12517. });
  12518. if (same_prefix)
  12519. {
  12520. prefix_required = false;
  12521. oa->write_character(to_char_type('$'));
  12522. oa->write_character(first_prefix);
  12523. }
  12524. }
  12525. if (use_count)
  12526. {
  12527. oa->write_character(to_char_type('#'));
  12528. write_number_with_ubjson_prefix(j.m_value.array->size(), true);
  12529. }
  12530. for (const auto& el : *j.m_value.array)
  12531. {
  12532. write_ubjson(el, use_count, use_type, prefix_required);
  12533. }
  12534. if (!use_count)
  12535. {
  12536. oa->write_character(to_char_type(']'));
  12537. }
  12538. break;
  12539. }
  12540. case value_t::binary:
  12541. {
  12542. if (add_prefix)
  12543. {
  12544. oa->write_character(to_char_type('['));
  12545. }
  12546. if (use_type && !j.m_value.binary->empty())
  12547. {
  12548. JSON_ASSERT(use_count);
  12549. oa->write_character(to_char_type('$'));
  12550. oa->write_character('U');
  12551. }
  12552. if (use_count)
  12553. {
  12554. oa->write_character(to_char_type('#'));
  12555. write_number_with_ubjson_prefix(j.m_value.binary->size(), true);
  12556. }
  12557. if (use_type)
  12558. {
  12559. oa->write_characters(
  12560. reinterpret_cast<const CharType*>(j.m_value.binary->data()),
  12561. j.m_value.binary->size());
  12562. }
  12563. else
  12564. {
  12565. for (size_t i = 0; i < j.m_value.binary->size(); ++i)
  12566. {
  12567. oa->write_character(to_char_type('U'));
  12568. oa->write_character(j.m_value.binary->data()[i]);
  12569. }
  12570. }
  12571. if (!use_count)
  12572. {
  12573. oa->write_character(to_char_type(']'));
  12574. }
  12575. break;
  12576. }
  12577. case value_t::object:
  12578. {
  12579. if (add_prefix)
  12580. {
  12581. oa->write_character(to_char_type('{'));
  12582. }
  12583. bool prefix_required = true;
  12584. if (use_type && !j.m_value.object->empty())
  12585. {
  12586. JSON_ASSERT(use_count);
  12587. const CharType first_prefix = ubjson_prefix(j.front());
  12588. const bool same_prefix = std::all_of(j.begin(), j.end(),
  12589. [this, first_prefix](const BasicJsonType & v)
  12590. {
  12591. return ubjson_prefix(v) == first_prefix;
  12592. });
  12593. if (same_prefix)
  12594. {
  12595. prefix_required = false;
  12596. oa->write_character(to_char_type('$'));
  12597. oa->write_character(first_prefix);
  12598. }
  12599. }
  12600. if (use_count)
  12601. {
  12602. oa->write_character(to_char_type('#'));
  12603. write_number_with_ubjson_prefix(j.m_value.object->size(), true);
  12604. }
  12605. for (const auto& el : *j.m_value.object)
  12606. {
  12607. write_number_with_ubjson_prefix(el.first.size(), true);
  12608. oa->write_characters(
  12609. reinterpret_cast<const CharType*>(el.first.c_str()),
  12610. el.first.size());
  12611. write_ubjson(el.second, use_count, use_type, prefix_required);
  12612. }
  12613. if (!use_count)
  12614. {
  12615. oa->write_character(to_char_type('}'));
  12616. }
  12617. break;
  12618. }
  12619. case value_t::discarded:
  12620. default:
  12621. break;
  12622. }
  12623. }
  12624. private:
  12625. //////////
  12626. // BSON //
  12627. //////////
  12628. /*!
  12629. @return The size of a BSON document entry header, including the id marker
  12630. and the entry name size (and its null-terminator).
  12631. */
  12632. static std::size_t calc_bson_entry_header_size(const string_t& name, const BasicJsonType& j)
  12633. {
  12634. const auto it = name.find(static_cast<typename string_t::value_type>(0));
  12635. if (JSON_HEDLEY_UNLIKELY(it != BasicJsonType::string_t::npos))
  12636. {
  12637. JSON_THROW(out_of_range::create(409, "BSON key cannot contain code point U+0000 (at byte " + std::to_string(it) + ")", j));
  12638. static_cast<void>(j);
  12639. }
  12640. return /*id*/ 1ul + name.size() + /*zero-terminator*/1u;
  12641. }
  12642. /*!
  12643. @brief Writes the given @a element_type and @a name to the output adapter
  12644. */
  12645. void write_bson_entry_header(const string_t& name,
  12646. const std::uint8_t element_type)
  12647. {
  12648. oa->write_character(to_char_type(element_type)); // boolean
  12649. oa->write_characters(
  12650. reinterpret_cast<const CharType*>(name.c_str()),
  12651. name.size() + 1u);
  12652. }
  12653. /*!
  12654. @brief Writes a BSON element with key @a name and boolean value @a value
  12655. */
  12656. void write_bson_boolean(const string_t& name,
  12657. const bool value)
  12658. {
  12659. write_bson_entry_header(name, 0x08);
  12660. oa->write_character(value ? to_char_type(0x01) : to_char_type(0x00));
  12661. }
  12662. /*!
  12663. @brief Writes a BSON element with key @a name and double value @a value
  12664. */
  12665. void write_bson_double(const string_t& name,
  12666. const double value)
  12667. {
  12668. write_bson_entry_header(name, 0x01);
  12669. write_number<double, true>(value);
  12670. }
  12671. /*!
  12672. @return The size of the BSON-encoded string in @a value
  12673. */
  12674. static std::size_t calc_bson_string_size(const string_t& value)
  12675. {
  12676. return sizeof(std::int32_t) + value.size() + 1ul;
  12677. }
  12678. /*!
  12679. @brief Writes a BSON element with key @a name and string value @a value
  12680. */
  12681. void write_bson_string(const string_t& name,
  12682. const string_t& value)
  12683. {
  12684. write_bson_entry_header(name, 0x02);
  12685. write_number<std::int32_t, true>(static_cast<std::int32_t>(value.size() + 1ul));
  12686. oa->write_characters(
  12687. reinterpret_cast<const CharType*>(value.c_str()),
  12688. value.size() + 1);
  12689. }
  12690. /*!
  12691. @brief Writes a BSON element with key @a name and null value
  12692. */
  12693. void write_bson_null(const string_t& name)
  12694. {
  12695. write_bson_entry_header(name, 0x0A);
  12696. }
  12697. /*!
  12698. @return The size of the BSON-encoded integer @a value
  12699. */
  12700. static std::size_t calc_bson_integer_size(const std::int64_t value)
  12701. {
  12702. return (std::numeric_limits<std::int32_t>::min)() <= value && value <= (std::numeric_limits<std::int32_t>::max)()
  12703. ? sizeof(std::int32_t)
  12704. : sizeof(std::int64_t);
  12705. }
  12706. /*!
  12707. @brief Writes a BSON element with key @a name and integer @a value
  12708. */
  12709. void write_bson_integer(const string_t& name,
  12710. const std::int64_t value)
  12711. {
  12712. if ((std::numeric_limits<std::int32_t>::min)() <= value && value <= (std::numeric_limits<std::int32_t>::max)())
  12713. {
  12714. write_bson_entry_header(name, 0x10); // int32
  12715. write_number<std::int32_t, true>(static_cast<std::int32_t>(value));
  12716. }
  12717. else
  12718. {
  12719. write_bson_entry_header(name, 0x12); // int64
  12720. write_number<std::int64_t, true>(static_cast<std::int64_t>(value));
  12721. }
  12722. }
  12723. /*!
  12724. @return The size of the BSON-encoded unsigned integer in @a j
  12725. */
  12726. static constexpr std::size_t calc_bson_unsigned_size(const std::uint64_t value) noexcept
  12727. {
  12728. return (value <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  12729. ? sizeof(std::int32_t)
  12730. : sizeof(std::int64_t);
  12731. }
  12732. /*!
  12733. @brief Writes a BSON element with key @a name and unsigned @a value
  12734. */
  12735. void write_bson_unsigned(const string_t& name,
  12736. const BasicJsonType& j)
  12737. {
  12738. if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  12739. {
  12740. write_bson_entry_header(name, 0x10 /* int32 */);
  12741. write_number<std::int32_t, true>(static_cast<std::int32_t>(j.m_value.number_unsigned));
  12742. }
  12743. else if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
  12744. {
  12745. write_bson_entry_header(name, 0x12 /* int64 */);
  12746. write_number<std::int64_t, true>(static_cast<std::int64_t>(j.m_value.number_unsigned));
  12747. }
  12748. else
  12749. {
  12750. JSON_THROW(out_of_range::create(407, "integer number " + std::to_string(j.m_value.number_unsigned) + " cannot be represented by BSON as it does not fit int64", j));
  12751. }
  12752. }
  12753. /*!
  12754. @brief Writes a BSON element with key @a name and object @a value
  12755. */
  12756. void write_bson_object_entry(const string_t& name,
  12757. const typename BasicJsonType::object_t& value)
  12758. {
  12759. write_bson_entry_header(name, 0x03); // object
  12760. write_bson_object(value);
  12761. }
  12762. /*!
  12763. @return The size of the BSON-encoded array @a value
  12764. */
  12765. static std::size_t calc_bson_array_size(const typename BasicJsonType::array_t& value)
  12766. {
  12767. std::size_t array_index = 0ul;
  12768. const std::size_t embedded_document_size = std::accumulate(std::begin(value), std::end(value), std::size_t(0), [&array_index](std::size_t result, const typename BasicJsonType::array_t::value_type & el)
  12769. {
  12770. return result + calc_bson_element_size(std::to_string(array_index++), el);
  12771. });
  12772. return sizeof(std::int32_t) + embedded_document_size + 1ul;
  12773. }
  12774. /*!
  12775. @return The size of the BSON-encoded binary array @a value
  12776. */
  12777. static std::size_t calc_bson_binary_size(const typename BasicJsonType::binary_t& value)
  12778. {
  12779. return sizeof(std::int32_t) + value.size() + 1ul;
  12780. }
  12781. /*!
  12782. @brief Writes a BSON element with key @a name and array @a value
  12783. */
  12784. void write_bson_array(const string_t& name,
  12785. const typename BasicJsonType::array_t& value)
  12786. {
  12787. write_bson_entry_header(name, 0x04); // array
  12788. write_number<std::int32_t, true>(static_cast<std::int32_t>(calc_bson_array_size(value)));
  12789. std::size_t array_index = 0ul;
  12790. for (const auto& el : value)
  12791. {
  12792. write_bson_element(std::to_string(array_index++), el);
  12793. }
  12794. oa->write_character(to_char_type(0x00));
  12795. }
  12796. /*!
  12797. @brief Writes a BSON element with key @a name and binary value @a value
  12798. */
  12799. void write_bson_binary(const string_t& name,
  12800. const binary_t& value)
  12801. {
  12802. write_bson_entry_header(name, 0x05);
  12803. write_number<std::int32_t, true>(static_cast<std::int32_t>(value.size()));
  12804. write_number(value.has_subtype() ? static_cast<std::uint8_t>(value.subtype()) : std::uint8_t(0x00));
  12805. oa->write_characters(reinterpret_cast<const CharType*>(value.data()), value.size());
  12806. }
  12807. /*!
  12808. @brief Calculates the size necessary to serialize the JSON value @a j with its @a name
  12809. @return The calculated size for the BSON document entry for @a j with the given @a name.
  12810. */
  12811. static std::size_t calc_bson_element_size(const string_t& name,
  12812. const BasicJsonType& j)
  12813. {
  12814. const auto header_size = calc_bson_entry_header_size(name, j);
  12815. switch (j.type())
  12816. {
  12817. case value_t::object:
  12818. return header_size + calc_bson_object_size(*j.m_value.object);
  12819. case value_t::array:
  12820. return header_size + calc_bson_array_size(*j.m_value.array);
  12821. case value_t::binary:
  12822. return header_size + calc_bson_binary_size(*j.m_value.binary);
  12823. case value_t::boolean:
  12824. return header_size + 1ul;
  12825. case value_t::number_float:
  12826. return header_size + 8ul;
  12827. case value_t::number_integer:
  12828. return header_size + calc_bson_integer_size(j.m_value.number_integer);
  12829. case value_t::number_unsigned:
  12830. return header_size + calc_bson_unsigned_size(j.m_value.number_unsigned);
  12831. case value_t::string:
  12832. return header_size + calc_bson_string_size(*j.m_value.string);
  12833. case value_t::null:
  12834. return header_size + 0ul;
  12835. // LCOV_EXCL_START
  12836. case value_t::discarded:
  12837. default:
  12838. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert)
  12839. return 0ul;
  12840. // LCOV_EXCL_STOP
  12841. }
  12842. }
  12843. /*!
  12844. @brief Serializes the JSON value @a j to BSON and associates it with the
  12845. key @a name.
  12846. @param name The name to associate with the JSON entity @a j within the
  12847. current BSON document
  12848. */
  12849. void write_bson_element(const string_t& name,
  12850. const BasicJsonType& j)
  12851. {
  12852. switch (j.type())
  12853. {
  12854. case value_t::object:
  12855. return write_bson_object_entry(name, *j.m_value.object);
  12856. case value_t::array:
  12857. return write_bson_array(name, *j.m_value.array);
  12858. case value_t::binary:
  12859. return write_bson_binary(name, *j.m_value.binary);
  12860. case value_t::boolean:
  12861. return write_bson_boolean(name, j.m_value.boolean);
  12862. case value_t::number_float:
  12863. return write_bson_double(name, j.m_value.number_float);
  12864. case value_t::number_integer:
  12865. return write_bson_integer(name, j.m_value.number_integer);
  12866. case value_t::number_unsigned:
  12867. return write_bson_unsigned(name, j);
  12868. case value_t::string:
  12869. return write_bson_string(name, *j.m_value.string);
  12870. case value_t::null:
  12871. return write_bson_null(name);
  12872. // LCOV_EXCL_START
  12873. case value_t::discarded:
  12874. default:
  12875. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert)
  12876. return;
  12877. // LCOV_EXCL_STOP
  12878. }
  12879. }
  12880. /*!
  12881. @brief Calculates the size of the BSON serialization of the given
  12882. JSON-object @a j.
  12883. @param[in] value JSON value to serialize
  12884. @pre value.type() == value_t::object
  12885. */
  12886. static std::size_t calc_bson_object_size(const typename BasicJsonType::object_t& value)
  12887. {
  12888. std::size_t document_size = std::accumulate(value.begin(), value.end(), std::size_t(0),
  12889. [](size_t result, const typename BasicJsonType::object_t::value_type & el)
  12890. {
  12891. return result += calc_bson_element_size(el.first, el.second);
  12892. });
  12893. return sizeof(std::int32_t) + document_size + 1ul;
  12894. }
  12895. /*!
  12896. @param[in] value JSON value to serialize
  12897. @pre value.type() == value_t::object
  12898. */
  12899. void write_bson_object(const typename BasicJsonType::object_t& value)
  12900. {
  12901. write_number<std::int32_t, true>(static_cast<std::int32_t>(calc_bson_object_size(value)));
  12902. for (const auto& el : value)
  12903. {
  12904. write_bson_element(el.first, el.second);
  12905. }
  12906. oa->write_character(to_char_type(0x00));
  12907. }
  12908. //////////
  12909. // CBOR //
  12910. //////////
  12911. static constexpr CharType get_cbor_float_prefix(float /*unused*/)
  12912. {
  12913. return to_char_type(0xFA); // Single-Precision Float
  12914. }
  12915. static constexpr CharType get_cbor_float_prefix(double /*unused*/)
  12916. {
  12917. return to_char_type(0xFB); // Double-Precision Float
  12918. }
  12919. /////////////
  12920. // MsgPack //
  12921. /////////////
  12922. static constexpr CharType get_msgpack_float_prefix(float /*unused*/)
  12923. {
  12924. return to_char_type(0xCA); // float 32
  12925. }
  12926. static constexpr CharType get_msgpack_float_prefix(double /*unused*/)
  12927. {
  12928. return to_char_type(0xCB); // float 64
  12929. }
  12930. ////////////
  12931. // UBJSON //
  12932. ////////////
  12933. // UBJSON: write number (floating point)
  12934. template<typename NumberType, typename std::enable_if<
  12935. std::is_floating_point<NumberType>::value, int>::type = 0>
  12936. void write_number_with_ubjson_prefix(const NumberType n,
  12937. const bool add_prefix)
  12938. {
  12939. if (add_prefix)
  12940. {
  12941. oa->write_character(get_ubjson_float_prefix(n));
  12942. }
  12943. write_number(n);
  12944. }
  12945. // UBJSON: write number (unsigned integer)
  12946. template<typename NumberType, typename std::enable_if<
  12947. std::is_unsigned<NumberType>::value, int>::type = 0>
  12948. void write_number_with_ubjson_prefix(const NumberType n,
  12949. const bool add_prefix)
  12950. {
  12951. if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int8_t>::max)()))
  12952. {
  12953. if (add_prefix)
  12954. {
  12955. oa->write_character(to_char_type('i')); // int8
  12956. }
  12957. write_number(static_cast<std::uint8_t>(n));
  12958. }
  12959. else if (n <= (std::numeric_limits<std::uint8_t>::max)())
  12960. {
  12961. if (add_prefix)
  12962. {
  12963. oa->write_character(to_char_type('U')); // uint8
  12964. }
  12965. write_number(static_cast<std::uint8_t>(n));
  12966. }
  12967. else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int16_t>::max)()))
  12968. {
  12969. if (add_prefix)
  12970. {
  12971. oa->write_character(to_char_type('I')); // int16
  12972. }
  12973. write_number(static_cast<std::int16_t>(n));
  12974. }
  12975. else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  12976. {
  12977. if (add_prefix)
  12978. {
  12979. oa->write_character(to_char_type('l')); // int32
  12980. }
  12981. write_number(static_cast<std::int32_t>(n));
  12982. }
  12983. else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
  12984. {
  12985. if (add_prefix)
  12986. {
  12987. oa->write_character(to_char_type('L')); // int64
  12988. }
  12989. write_number(static_cast<std::int64_t>(n));
  12990. }
  12991. else
  12992. {
  12993. if (add_prefix)
  12994. {
  12995. oa->write_character(to_char_type('H')); // high-precision number
  12996. }
  12997. const auto number = BasicJsonType(n).dump();
  12998. write_number_with_ubjson_prefix(number.size(), true);
  12999. for (std::size_t i = 0; i < number.size(); ++i)
  13000. {
  13001. oa->write_character(to_char_type(static_cast<std::uint8_t>(number[i])));
  13002. }
  13003. }
  13004. }
  13005. // UBJSON: write number (signed integer)
  13006. template < typename NumberType, typename std::enable_if <
  13007. std::is_signed<NumberType>::value&&
  13008. !std::is_floating_point<NumberType>::value, int >::type = 0 >
  13009. void write_number_with_ubjson_prefix(const NumberType n,
  13010. const bool add_prefix)
  13011. {
  13012. if ((std::numeric_limits<std::int8_t>::min)() <= n && n <= (std::numeric_limits<std::int8_t>::max)())
  13013. {
  13014. if (add_prefix)
  13015. {
  13016. oa->write_character(to_char_type('i')); // int8
  13017. }
  13018. write_number(static_cast<std::int8_t>(n));
  13019. }
  13020. 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)()))
  13021. {
  13022. if (add_prefix)
  13023. {
  13024. oa->write_character(to_char_type('U')); // uint8
  13025. }
  13026. write_number(static_cast<std::uint8_t>(n));
  13027. }
  13028. else if ((std::numeric_limits<std::int16_t>::min)() <= n && n <= (std::numeric_limits<std::int16_t>::max)())
  13029. {
  13030. if (add_prefix)
  13031. {
  13032. oa->write_character(to_char_type('I')); // int16
  13033. }
  13034. write_number(static_cast<std::int16_t>(n));
  13035. }
  13036. else if ((std::numeric_limits<std::int32_t>::min)() <= n && n <= (std::numeric_limits<std::int32_t>::max)())
  13037. {
  13038. if (add_prefix)
  13039. {
  13040. oa->write_character(to_char_type('l')); // int32
  13041. }
  13042. write_number(static_cast<std::int32_t>(n));
  13043. }
  13044. else if ((std::numeric_limits<std::int64_t>::min)() <= n && n <= (std::numeric_limits<std::int64_t>::max)())
  13045. {
  13046. if (add_prefix)
  13047. {
  13048. oa->write_character(to_char_type('L')); // int64
  13049. }
  13050. write_number(static_cast<std::int64_t>(n));
  13051. }
  13052. // LCOV_EXCL_START
  13053. else
  13054. {
  13055. if (add_prefix)
  13056. {
  13057. oa->write_character(to_char_type('H')); // high-precision number
  13058. }
  13059. const auto number = BasicJsonType(n).dump();
  13060. write_number_with_ubjson_prefix(number.size(), true);
  13061. for (std::size_t i = 0; i < number.size(); ++i)
  13062. {
  13063. oa->write_character(to_char_type(static_cast<std::uint8_t>(number[i])));
  13064. }
  13065. }
  13066. // LCOV_EXCL_STOP
  13067. }
  13068. /*!
  13069. @brief determine the type prefix of container values
  13070. */
  13071. CharType ubjson_prefix(const BasicJsonType& j) const noexcept
  13072. {
  13073. switch (j.type())
  13074. {
  13075. case value_t::null:
  13076. return 'Z';
  13077. case value_t::boolean:
  13078. return j.m_value.boolean ? 'T' : 'F';
  13079. case value_t::number_integer:
  13080. {
  13081. 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)())
  13082. {
  13083. return 'i';
  13084. }
  13085. 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)())
  13086. {
  13087. return 'U';
  13088. }
  13089. 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)())
  13090. {
  13091. return 'I';
  13092. }
  13093. 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)())
  13094. {
  13095. return 'l';
  13096. }
  13097. 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)())
  13098. {
  13099. return 'L';
  13100. }
  13101. // anything else is treated as high-precision number
  13102. return 'H'; // LCOV_EXCL_LINE
  13103. }
  13104. case value_t::number_unsigned:
  13105. {
  13106. if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int8_t>::max)()))
  13107. {
  13108. return 'i';
  13109. }
  13110. if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint8_t>::max)()))
  13111. {
  13112. return 'U';
  13113. }
  13114. if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int16_t>::max)()))
  13115. {
  13116. return 'I';
  13117. }
  13118. if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  13119. {
  13120. return 'l';
  13121. }
  13122. if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
  13123. {
  13124. return 'L';
  13125. }
  13126. // anything else is treated as high-precision number
  13127. return 'H'; // LCOV_EXCL_LINE
  13128. }
  13129. case value_t::number_float:
  13130. return get_ubjson_float_prefix(j.m_value.number_float);
  13131. case value_t::string:
  13132. return 'S';
  13133. case value_t::array: // fallthrough
  13134. case value_t::binary:
  13135. return '[';
  13136. case value_t::object:
  13137. return '{';
  13138. case value_t::discarded:
  13139. default: // discarded values
  13140. return 'N';
  13141. }
  13142. }
  13143. static constexpr CharType get_ubjson_float_prefix(float /*unused*/)
  13144. {
  13145. return 'd'; // float 32
  13146. }
  13147. static constexpr CharType get_ubjson_float_prefix(double /*unused*/)
  13148. {
  13149. return 'D'; // float 64
  13150. }
  13151. ///////////////////////
  13152. // Utility functions //
  13153. ///////////////////////
  13154. /*
  13155. @brief write a number to output input
  13156. @param[in] n number of type @a NumberType
  13157. @tparam NumberType the type of the number
  13158. @tparam OutputIsLittleEndian Set to true if output data is
  13159. required to be little endian
  13160. @note This function needs to respect the system's endianess, because bytes
  13161. in CBOR, MessagePack, and UBJSON are stored in network order (big
  13162. endian) and therefore need reordering on little endian systems.
  13163. */
  13164. template<typename NumberType, bool OutputIsLittleEndian = false>
  13165. void write_number(const NumberType n)
  13166. {
  13167. // step 1: write number to array of length NumberType
  13168. std::array<CharType, sizeof(NumberType)> vec{};
  13169. std::memcpy(vec.data(), &n, sizeof(NumberType));
  13170. // step 2: write array to output (with possible reordering)
  13171. if (is_little_endian != OutputIsLittleEndian)
  13172. {
  13173. // reverse byte order prior to conversion if necessary
  13174. std::reverse(vec.begin(), vec.end());
  13175. }
  13176. oa->write_characters(vec.data(), sizeof(NumberType));
  13177. }
  13178. void write_compact_float(const number_float_t n, detail::input_format_t format)
  13179. {
  13180. #ifdef __GNUC__
  13181. #pragma GCC diagnostic push
  13182. #pragma GCC diagnostic ignored "-Wfloat-equal"
  13183. #endif
  13184. if (static_cast<double>(n) >= static_cast<double>(std::numeric_limits<float>::lowest()) &&
  13185. static_cast<double>(n) <= static_cast<double>((std::numeric_limits<float>::max)()) &&
  13186. static_cast<double>(static_cast<float>(n)) == static_cast<double>(n))
  13187. {
  13188. oa->write_character(format == detail::input_format_t::cbor
  13189. ? get_cbor_float_prefix(static_cast<float>(n))
  13190. : get_msgpack_float_prefix(static_cast<float>(n)));
  13191. write_number(static_cast<float>(n));
  13192. }
  13193. else
  13194. {
  13195. oa->write_character(format == detail::input_format_t::cbor
  13196. ? get_cbor_float_prefix(n)
  13197. : get_msgpack_float_prefix(n));
  13198. write_number(n);
  13199. }
  13200. #ifdef __GNUC__
  13201. #pragma GCC diagnostic pop
  13202. #endif
  13203. }
  13204. public:
  13205. // The following to_char_type functions are implement the conversion
  13206. // between uint8_t and CharType. In case CharType is not unsigned,
  13207. // such a conversion is required to allow values greater than 128.
  13208. // See <https://github.com/nlohmann/json/issues/1286> for a discussion.
  13209. template < typename C = CharType,
  13210. enable_if_t < std::is_signed<C>::value && std::is_signed<char>::value > * = nullptr >
  13211. static constexpr CharType to_char_type(std::uint8_t x) noexcept
  13212. {
  13213. return *reinterpret_cast<char*>(&x);
  13214. }
  13215. template < typename C = CharType,
  13216. enable_if_t < std::is_signed<C>::value && std::is_unsigned<char>::value > * = nullptr >
  13217. static CharType to_char_type(std::uint8_t x) noexcept
  13218. {
  13219. static_assert(sizeof(std::uint8_t) == sizeof(CharType), "size of CharType must be equal to std::uint8_t");
  13220. static_assert(std::is_trivial<CharType>::value, "CharType must be trivial");
  13221. CharType result;
  13222. std::memcpy(&result, &x, sizeof(x));
  13223. return result;
  13224. }
  13225. template<typename C = CharType,
  13226. enable_if_t<std::is_unsigned<C>::value>* = nullptr>
  13227. static constexpr CharType to_char_type(std::uint8_t x) noexcept
  13228. {
  13229. return x;
  13230. }
  13231. template < typename InputCharType, typename C = CharType,
  13232. enable_if_t <
  13233. std::is_signed<C>::value &&
  13234. std::is_signed<char>::value &&
  13235. std::is_same<char, typename std::remove_cv<InputCharType>::type>::value
  13236. > * = nullptr >
  13237. static constexpr CharType to_char_type(InputCharType x) noexcept
  13238. {
  13239. return x;
  13240. }
  13241. private:
  13242. /// whether we can assume little endianess
  13243. const bool is_little_endian = little_endianess();
  13244. /// the output
  13245. output_adapter_t<CharType> oa = nullptr;
  13246. };
  13247. } // namespace detail
  13248. } // namespace nlohmann
  13249. // #include <nlohmann/detail/output/output_adapters.hpp>
  13250. // #include <nlohmann/detail/output/serializer.hpp>
  13251. #include <algorithm> // reverse, remove, fill, find, none_of
  13252. #include <array> // array
  13253. #include <clocale> // localeconv, lconv
  13254. #include <cmath> // labs, isfinite, isnan, signbit
  13255. #include <cstddef> // size_t, ptrdiff_t
  13256. #include <cstdint> // uint8_t
  13257. #include <cstdio> // snprintf
  13258. #include <limits> // numeric_limits
  13259. #include <string> // string, char_traits
  13260. #include <type_traits> // is_same
  13261. #include <utility> // move
  13262. // #include <nlohmann/detail/conversions/to_chars.hpp>
  13263. #include <array> // array
  13264. #include <cmath> // signbit, isfinite
  13265. #include <cstdint> // intN_t, uintN_t
  13266. #include <cstring> // memcpy, memmove
  13267. #include <limits> // numeric_limits
  13268. #include <type_traits> // conditional
  13269. // #include <nlohmann/detail/macro_scope.hpp>
  13270. namespace nlohmann
  13271. {
  13272. namespace detail
  13273. {
  13274. /*!
  13275. @brief implements the Grisu2 algorithm for binary to decimal floating-point
  13276. conversion.
  13277. This implementation is a slightly modified version of the reference
  13278. implementation which may be obtained from
  13279. http://florian.loitsch.com/publications (bench.tar.gz).
  13280. The code is distributed under the MIT license, Copyright (c) 2009 Florian Loitsch.
  13281. For a detailed description of the algorithm see:
  13282. [1] Loitsch, "Printing Floating-Point Numbers Quickly and Accurately with
  13283. Integers", Proceedings of the ACM SIGPLAN 2010 Conference on Programming
  13284. Language Design and Implementation, PLDI 2010
  13285. [2] Burger, Dybvig, "Printing Floating-Point Numbers Quickly and Accurately",
  13286. Proceedings of the ACM SIGPLAN 1996 Conference on Programming Language
  13287. Design and Implementation, PLDI 1996
  13288. */
  13289. namespace dtoa_impl
  13290. {
  13291. template<typename Target, typename Source>
  13292. Target reinterpret_bits(const Source source)
  13293. {
  13294. static_assert(sizeof(Target) == sizeof(Source), "size mismatch");
  13295. Target target;
  13296. std::memcpy(&target, &source, sizeof(Source));
  13297. return target;
  13298. }
  13299. struct diyfp // f * 2^e
  13300. {
  13301. static constexpr int kPrecision = 64; // = q
  13302. std::uint64_t f = 0;
  13303. int e = 0;
  13304. constexpr diyfp(std::uint64_t f_, int e_) noexcept : f(f_), e(e_) {}
  13305. /*!
  13306. @brief returns x - y
  13307. @pre x.e == y.e and x.f >= y.f
  13308. */
  13309. static diyfp sub(const diyfp& x, const diyfp& y) noexcept
  13310. {
  13311. JSON_ASSERT(x.e == y.e);
  13312. JSON_ASSERT(x.f >= y.f);
  13313. return {x.f - y.f, x.e};
  13314. }
  13315. /*!
  13316. @brief returns x * y
  13317. @note The result is rounded. (Only the upper q bits are returned.)
  13318. */
  13319. static diyfp mul(const diyfp& x, const diyfp& y) noexcept
  13320. {
  13321. static_assert(kPrecision == 64, "internal error");
  13322. // Computes:
  13323. // f = round((x.f * y.f) / 2^q)
  13324. // e = x.e + y.e + q
  13325. // Emulate the 64-bit * 64-bit multiplication:
  13326. //
  13327. // p = u * v
  13328. // = (u_lo + 2^32 u_hi) (v_lo + 2^32 v_hi)
  13329. // = (u_lo v_lo ) + 2^32 ((u_lo v_hi ) + (u_hi v_lo )) + 2^64 (u_hi v_hi )
  13330. // = (p0 ) + 2^32 ((p1 ) + (p2 )) + 2^64 (p3 )
  13331. // = (p0_lo + 2^32 p0_hi) + 2^32 ((p1_lo + 2^32 p1_hi) + (p2_lo + 2^32 p2_hi)) + 2^64 (p3 )
  13332. // = (p0_lo ) + 2^32 (p0_hi + p1_lo + p2_lo ) + 2^64 (p1_hi + p2_hi + p3)
  13333. // = (p0_lo ) + 2^32 (Q ) + 2^64 (H )
  13334. // = (p0_lo ) + 2^32 (Q_lo + 2^32 Q_hi ) + 2^64 (H )
  13335. //
  13336. // (Since Q might be larger than 2^32 - 1)
  13337. //
  13338. // = (p0_lo + 2^32 Q_lo) + 2^64 (Q_hi + H)
  13339. //
  13340. // (Q_hi + H does not overflow a 64-bit int)
  13341. //
  13342. // = p_lo + 2^64 p_hi
  13343. const std::uint64_t u_lo = x.f & 0xFFFFFFFFu;
  13344. const std::uint64_t u_hi = x.f >> 32u;
  13345. const std::uint64_t v_lo = y.f & 0xFFFFFFFFu;
  13346. const std::uint64_t v_hi = y.f >> 32u;
  13347. const std::uint64_t p0 = u_lo * v_lo;
  13348. const std::uint64_t p1 = u_lo * v_hi;
  13349. const std::uint64_t p2 = u_hi * v_lo;
  13350. const std::uint64_t p3 = u_hi * v_hi;
  13351. const std::uint64_t p0_hi = p0 >> 32u;
  13352. const std::uint64_t p1_lo = p1 & 0xFFFFFFFFu;
  13353. const std::uint64_t p1_hi = p1 >> 32u;
  13354. const std::uint64_t p2_lo = p2 & 0xFFFFFFFFu;
  13355. const std::uint64_t p2_hi = p2 >> 32u;
  13356. std::uint64_t Q = p0_hi + p1_lo + p2_lo;
  13357. // The full product might now be computed as
  13358. //
  13359. // p_hi = p3 + p2_hi + p1_hi + (Q >> 32)
  13360. // p_lo = p0_lo + (Q << 32)
  13361. //
  13362. // But in this particular case here, the full p_lo is not required.
  13363. // Effectively we only need to add the highest bit in p_lo to p_hi (and
  13364. // Q_hi + 1 does not overflow).
  13365. Q += std::uint64_t{1} << (64u - 32u - 1u); // round, ties up
  13366. const std::uint64_t h = p3 + p2_hi + p1_hi + (Q >> 32u);
  13367. return {h, x.e + y.e + 64};
  13368. }
  13369. /*!
  13370. @brief normalize x such that the significand is >= 2^(q-1)
  13371. @pre x.f != 0
  13372. */
  13373. static diyfp normalize(diyfp x) noexcept
  13374. {
  13375. JSON_ASSERT(x.f != 0);
  13376. while ((x.f >> 63u) == 0)
  13377. {
  13378. x.f <<= 1u;
  13379. x.e--;
  13380. }
  13381. return x;
  13382. }
  13383. /*!
  13384. @brief normalize x such that the result has the exponent E
  13385. @pre e >= x.e and the upper e - x.e bits of x.f must be zero.
  13386. */
  13387. static diyfp normalize_to(const diyfp& x, const int target_exponent) noexcept
  13388. {
  13389. const int delta = x.e - target_exponent;
  13390. JSON_ASSERT(delta >= 0);
  13391. JSON_ASSERT(((x.f << delta) >> delta) == x.f);
  13392. return {x.f << delta, target_exponent};
  13393. }
  13394. };
  13395. struct boundaries
  13396. {
  13397. diyfp w;
  13398. diyfp minus;
  13399. diyfp plus;
  13400. };
  13401. /*!
  13402. Compute the (normalized) diyfp representing the input number 'value' and its
  13403. boundaries.
  13404. @pre value must be finite and positive
  13405. */
  13406. template<typename FloatType>
  13407. boundaries compute_boundaries(FloatType value)
  13408. {
  13409. JSON_ASSERT(std::isfinite(value));
  13410. JSON_ASSERT(value > 0);
  13411. // Convert the IEEE representation into a diyfp.
  13412. //
  13413. // If v is denormal:
  13414. // value = 0.F * 2^(1 - bias) = ( F) * 2^(1 - bias - (p-1))
  13415. // If v is normalized:
  13416. // value = 1.F * 2^(E - bias) = (2^(p-1) + F) * 2^(E - bias - (p-1))
  13417. static_assert(std::numeric_limits<FloatType>::is_iec559,
  13418. "internal error: dtoa_short requires an IEEE-754 floating-point implementation");
  13419. constexpr int kPrecision = std::numeric_limits<FloatType>::digits; // = p (includes the hidden bit)
  13420. constexpr int kBias = std::numeric_limits<FloatType>::max_exponent - 1 + (kPrecision - 1);
  13421. constexpr int kMinExp = 1 - kBias;
  13422. constexpr std::uint64_t kHiddenBit = std::uint64_t{1} << (kPrecision - 1); // = 2^(p-1)
  13423. using bits_type = typename std::conditional<kPrecision == 24, std::uint32_t, std::uint64_t >::type;
  13424. const auto bits = static_cast<std::uint64_t>(reinterpret_bits<bits_type>(value));
  13425. const std::uint64_t E = bits >> (kPrecision - 1);
  13426. const std::uint64_t F = bits & (kHiddenBit - 1);
  13427. const bool is_denormal = E == 0;
  13428. const diyfp v = is_denormal
  13429. ? diyfp(F, kMinExp)
  13430. : diyfp(F + kHiddenBit, static_cast<int>(E) - kBias);
  13431. // Compute the boundaries m- and m+ of the floating-point value
  13432. // v = f * 2^e.
  13433. //
  13434. // Determine v- and v+, the floating-point predecessor and successor if v,
  13435. // respectively.
  13436. //
  13437. // v- = v - 2^e if f != 2^(p-1) or e == e_min (A)
  13438. // = v - 2^(e-1) if f == 2^(p-1) and e > e_min (B)
  13439. //
  13440. // v+ = v + 2^e
  13441. //
  13442. // Let m- = (v- + v) / 2 and m+ = (v + v+) / 2. All real numbers _strictly_
  13443. // between m- and m+ round to v, regardless of how the input rounding
  13444. // algorithm breaks ties.
  13445. //
  13446. // ---+-------------+-------------+-------------+-------------+--- (A)
  13447. // v- m- v m+ v+
  13448. //
  13449. // -----------------+------+------+-------------+-------------+--- (B)
  13450. // v- m- v m+ v+
  13451. const bool lower_boundary_is_closer = F == 0 && E > 1;
  13452. const diyfp m_plus = diyfp(2 * v.f + 1, v.e - 1);
  13453. const diyfp m_minus = lower_boundary_is_closer
  13454. ? diyfp(4 * v.f - 1, v.e - 2) // (B)
  13455. : diyfp(2 * v.f - 1, v.e - 1); // (A)
  13456. // Determine the normalized w+ = m+.
  13457. const diyfp w_plus = diyfp::normalize(m_plus);
  13458. // Determine w- = m- such that e_(w-) = e_(w+).
  13459. const diyfp w_minus = diyfp::normalize_to(m_minus, w_plus.e);
  13460. return {diyfp::normalize(v), w_minus, w_plus};
  13461. }
  13462. // Given normalized diyfp w, Grisu needs to find a (normalized) cached
  13463. // power-of-ten c, such that the exponent of the product c * w = f * 2^e lies
  13464. // within a certain range [alpha, gamma] (Definition 3.2 from [1])
  13465. //
  13466. // alpha <= e = e_c + e_w + q <= gamma
  13467. //
  13468. // or
  13469. //
  13470. // f_c * f_w * 2^alpha <= f_c 2^(e_c) * f_w 2^(e_w) * 2^q
  13471. // <= f_c * f_w * 2^gamma
  13472. //
  13473. // Since c and w are normalized, i.e. 2^(q-1) <= f < 2^q, this implies
  13474. //
  13475. // 2^(q-1) * 2^(q-1) * 2^alpha <= c * w * 2^q < 2^q * 2^q * 2^gamma
  13476. //
  13477. // or
  13478. //
  13479. // 2^(q - 2 + alpha) <= c * w < 2^(q + gamma)
  13480. //
  13481. // The choice of (alpha,gamma) determines the size of the table and the form of
  13482. // the digit generation procedure. Using (alpha,gamma)=(-60,-32) works out well
  13483. // in practice:
  13484. //
  13485. // The idea is to cut the number c * w = f * 2^e into two parts, which can be
  13486. // processed independently: An integral part p1, and a fractional part p2:
  13487. //
  13488. // f * 2^e = ( (f div 2^-e) * 2^-e + (f mod 2^-e) ) * 2^e
  13489. // = (f div 2^-e) + (f mod 2^-e) * 2^e
  13490. // = p1 + p2 * 2^e
  13491. //
  13492. // The conversion of p1 into decimal form requires a series of divisions and
  13493. // modulos by (a power of) 10. These operations are faster for 32-bit than for
  13494. // 64-bit integers, so p1 should ideally fit into a 32-bit integer. This can be
  13495. // achieved by choosing
  13496. //
  13497. // -e >= 32 or e <= -32 := gamma
  13498. //
  13499. // In order to convert the fractional part
  13500. //
  13501. // p2 * 2^e = p2 / 2^-e = d[-1] / 10^1 + d[-2] / 10^2 + ...
  13502. //
  13503. // into decimal form, the fraction is repeatedly multiplied by 10 and the digits
  13504. // d[-i] are extracted in order:
  13505. //
  13506. // (10 * p2) div 2^-e = d[-1]
  13507. // (10 * p2) mod 2^-e = d[-2] / 10^1 + ...
  13508. //
  13509. // The multiplication by 10 must not overflow. It is sufficient to choose
  13510. //
  13511. // 10 * p2 < 16 * p2 = 2^4 * p2 <= 2^64.
  13512. //
  13513. // Since p2 = f mod 2^-e < 2^-e,
  13514. //
  13515. // -e <= 60 or e >= -60 := alpha
  13516. constexpr int kAlpha = -60;
  13517. constexpr int kGamma = -32;
  13518. struct cached_power // c = f * 2^e ~= 10^k
  13519. {
  13520. std::uint64_t f;
  13521. int e;
  13522. int k;
  13523. };
  13524. /*!
  13525. For a normalized diyfp w = f * 2^e, this function returns a (normalized) cached
  13526. power-of-ten c = f_c * 2^e_c, such that the exponent of the product w * c
  13527. satisfies (Definition 3.2 from [1])
  13528. alpha <= e_c + e + q <= gamma.
  13529. */
  13530. inline cached_power get_cached_power_for_binary_exponent(int e)
  13531. {
  13532. // Now
  13533. //
  13534. // alpha <= e_c + e + q <= gamma (1)
  13535. // ==> f_c * 2^alpha <= c * 2^e * 2^q
  13536. //
  13537. // and since the c's are normalized, 2^(q-1) <= f_c,
  13538. //
  13539. // ==> 2^(q - 1 + alpha) <= c * 2^(e + q)
  13540. // ==> 2^(alpha - e - 1) <= c
  13541. //
  13542. // If c were an exact power of ten, i.e. c = 10^k, one may determine k as
  13543. //
  13544. // k = ceil( log_10( 2^(alpha - e - 1) ) )
  13545. // = ceil( (alpha - e - 1) * log_10(2) )
  13546. //
  13547. // From the paper:
  13548. // "In theory the result of the procedure could be wrong since c is rounded,
  13549. // and the computation itself is approximated [...]. In practice, however,
  13550. // this simple function is sufficient."
  13551. //
  13552. // For IEEE double precision floating-point numbers converted into
  13553. // normalized diyfp's w = f * 2^e, with q = 64,
  13554. //
  13555. // e >= -1022 (min IEEE exponent)
  13556. // -52 (p - 1)
  13557. // -52 (p - 1, possibly normalize denormal IEEE numbers)
  13558. // -11 (normalize the diyfp)
  13559. // = -1137
  13560. //
  13561. // and
  13562. //
  13563. // e <= +1023 (max IEEE exponent)
  13564. // -52 (p - 1)
  13565. // -11 (normalize the diyfp)
  13566. // = 960
  13567. //
  13568. // This binary exponent range [-1137,960] results in a decimal exponent
  13569. // range [-307,324]. One does not need to store a cached power for each
  13570. // k in this range. For each such k it suffices to find a cached power
  13571. // such that the exponent of the product lies in [alpha,gamma].
  13572. // This implies that the difference of the decimal exponents of adjacent
  13573. // table entries must be less than or equal to
  13574. //
  13575. // floor( (gamma - alpha) * log_10(2) ) = 8.
  13576. //
  13577. // (A smaller distance gamma-alpha would require a larger table.)
  13578. // NB:
  13579. // Actually this function returns c, such that -60 <= e_c + e + 64 <= -34.
  13580. constexpr int kCachedPowersMinDecExp = -300;
  13581. constexpr int kCachedPowersDecStep = 8;
  13582. static constexpr std::array<cached_power, 79> kCachedPowers =
  13583. {
  13584. {
  13585. { 0xAB70FE17C79AC6CA, -1060, -300 },
  13586. { 0xFF77B1FCBEBCDC4F, -1034, -292 },
  13587. { 0xBE5691EF416BD60C, -1007, -284 },
  13588. { 0x8DD01FAD907FFC3C, -980, -276 },
  13589. { 0xD3515C2831559A83, -954, -268 },
  13590. { 0x9D71AC8FADA6C9B5, -927, -260 },
  13591. { 0xEA9C227723EE8BCB, -901, -252 },
  13592. { 0xAECC49914078536D, -874, -244 },
  13593. { 0x823C12795DB6CE57, -847, -236 },
  13594. { 0xC21094364DFB5637, -821, -228 },
  13595. { 0x9096EA6F3848984F, -794, -220 },
  13596. { 0xD77485CB25823AC7, -768, -212 },
  13597. { 0xA086CFCD97BF97F4, -741, -204 },
  13598. { 0xEF340A98172AACE5, -715, -196 },
  13599. { 0xB23867FB2A35B28E, -688, -188 },
  13600. { 0x84C8D4DFD2C63F3B, -661, -180 },
  13601. { 0xC5DD44271AD3CDBA, -635, -172 },
  13602. { 0x936B9FCEBB25C996, -608, -164 },
  13603. { 0xDBAC6C247D62A584, -582, -156 },
  13604. { 0xA3AB66580D5FDAF6, -555, -148 },
  13605. { 0xF3E2F893DEC3F126, -529, -140 },
  13606. { 0xB5B5ADA8AAFF80B8, -502, -132 },
  13607. { 0x87625F056C7C4A8B, -475, -124 },
  13608. { 0xC9BCFF6034C13053, -449, -116 },
  13609. { 0x964E858C91BA2655, -422, -108 },
  13610. { 0xDFF9772470297EBD, -396, -100 },
  13611. { 0xA6DFBD9FB8E5B88F, -369, -92 },
  13612. { 0xF8A95FCF88747D94, -343, -84 },
  13613. { 0xB94470938FA89BCF, -316, -76 },
  13614. { 0x8A08F0F8BF0F156B, -289, -68 },
  13615. { 0xCDB02555653131B6, -263, -60 },
  13616. { 0x993FE2C6D07B7FAC, -236, -52 },
  13617. { 0xE45C10C42A2B3B06, -210, -44 },
  13618. { 0xAA242499697392D3, -183, -36 },
  13619. { 0xFD87B5F28300CA0E, -157, -28 },
  13620. { 0xBCE5086492111AEB, -130, -20 },
  13621. { 0x8CBCCC096F5088CC, -103, -12 },
  13622. { 0xD1B71758E219652C, -77, -4 },
  13623. { 0x9C40000000000000, -50, 4 },
  13624. { 0xE8D4A51000000000, -24, 12 },
  13625. { 0xAD78EBC5AC620000, 3, 20 },
  13626. { 0x813F3978F8940984, 30, 28 },
  13627. { 0xC097CE7BC90715B3, 56, 36 },
  13628. { 0x8F7E32CE7BEA5C70, 83, 44 },
  13629. { 0xD5D238A4ABE98068, 109, 52 },
  13630. { 0x9F4F2726179A2245, 136, 60 },
  13631. { 0xED63A231D4C4FB27, 162, 68 },
  13632. { 0xB0DE65388CC8ADA8, 189, 76 },
  13633. { 0x83C7088E1AAB65DB, 216, 84 },
  13634. { 0xC45D1DF942711D9A, 242, 92 },
  13635. { 0x924D692CA61BE758, 269, 100 },
  13636. { 0xDA01EE641A708DEA, 295, 108 },
  13637. { 0xA26DA3999AEF774A, 322, 116 },
  13638. { 0xF209787BB47D6B85, 348, 124 },
  13639. { 0xB454E4A179DD1877, 375, 132 },
  13640. { 0x865B86925B9BC5C2, 402, 140 },
  13641. { 0xC83553C5C8965D3D, 428, 148 },
  13642. { 0x952AB45CFA97A0B3, 455, 156 },
  13643. { 0xDE469FBD99A05FE3, 481, 164 },
  13644. { 0xA59BC234DB398C25, 508, 172 },
  13645. { 0xF6C69A72A3989F5C, 534, 180 },
  13646. { 0xB7DCBF5354E9BECE, 561, 188 },
  13647. { 0x88FCF317F22241E2, 588, 196 },
  13648. { 0xCC20CE9BD35C78A5, 614, 204 },
  13649. { 0x98165AF37B2153DF, 641, 212 },
  13650. { 0xE2A0B5DC971F303A, 667, 220 },
  13651. { 0xA8D9D1535CE3B396, 694, 228 },
  13652. { 0xFB9B7CD9A4A7443C, 720, 236 },
  13653. { 0xBB764C4CA7A44410, 747, 244 },
  13654. { 0x8BAB8EEFB6409C1A, 774, 252 },
  13655. { 0xD01FEF10A657842C, 800, 260 },
  13656. { 0x9B10A4E5E9913129, 827, 268 },
  13657. { 0xE7109BFBA19C0C9D, 853, 276 },
  13658. { 0xAC2820D9623BF429, 880, 284 },
  13659. { 0x80444B5E7AA7CF85, 907, 292 },
  13660. { 0xBF21E44003ACDD2D, 933, 300 },
  13661. { 0x8E679C2F5E44FF8F, 960, 308 },
  13662. { 0xD433179D9C8CB841, 986, 316 },
  13663. { 0x9E19DB92B4E31BA9, 1013, 324 },
  13664. }
  13665. };
  13666. // This computation gives exactly the same results for k as
  13667. // k = ceil((kAlpha - e - 1) * 0.30102999566398114)
  13668. // for |e| <= 1500, but doesn't require floating-point operations.
  13669. // NB: log_10(2) ~= 78913 / 2^18
  13670. JSON_ASSERT(e >= -1500);
  13671. JSON_ASSERT(e <= 1500);
  13672. const int f = kAlpha - e - 1;
  13673. const int k = (f * 78913) / (1 << 18) + static_cast<int>(f > 0);
  13674. const int index = (-kCachedPowersMinDecExp + k + (kCachedPowersDecStep - 1)) / kCachedPowersDecStep;
  13675. JSON_ASSERT(index >= 0);
  13676. JSON_ASSERT(static_cast<std::size_t>(index) < kCachedPowers.size());
  13677. const cached_power cached = kCachedPowers[static_cast<std::size_t>(index)];
  13678. JSON_ASSERT(kAlpha <= cached.e + e + 64);
  13679. JSON_ASSERT(kGamma >= cached.e + e + 64);
  13680. return cached;
  13681. }
  13682. /*!
  13683. For n != 0, returns k, such that pow10 := 10^(k-1) <= n < 10^k.
  13684. For n == 0, returns 1 and sets pow10 := 1.
  13685. */
  13686. inline int find_largest_pow10(const std::uint32_t n, std::uint32_t& pow10)
  13687. {
  13688. // LCOV_EXCL_START
  13689. if (n >= 1000000000)
  13690. {
  13691. pow10 = 1000000000;
  13692. return 10;
  13693. }
  13694. // LCOV_EXCL_STOP
  13695. if (n >= 100000000)
  13696. {
  13697. pow10 = 100000000;
  13698. return 9;
  13699. }
  13700. if (n >= 10000000)
  13701. {
  13702. pow10 = 10000000;
  13703. return 8;
  13704. }
  13705. if (n >= 1000000)
  13706. {
  13707. pow10 = 1000000;
  13708. return 7;
  13709. }
  13710. if (n >= 100000)
  13711. {
  13712. pow10 = 100000;
  13713. return 6;
  13714. }
  13715. if (n >= 10000)
  13716. {
  13717. pow10 = 10000;
  13718. return 5;
  13719. }
  13720. if (n >= 1000)
  13721. {
  13722. pow10 = 1000;
  13723. return 4;
  13724. }
  13725. if (n >= 100)
  13726. {
  13727. pow10 = 100;
  13728. return 3;
  13729. }
  13730. if (n >= 10)
  13731. {
  13732. pow10 = 10;
  13733. return 2;
  13734. }
  13735. pow10 = 1;
  13736. return 1;
  13737. }
  13738. inline void grisu2_round(char* buf, int len, std::uint64_t dist, std::uint64_t delta,
  13739. std::uint64_t rest, std::uint64_t ten_k)
  13740. {
  13741. JSON_ASSERT(len >= 1);
  13742. JSON_ASSERT(dist <= delta);
  13743. JSON_ASSERT(rest <= delta);
  13744. JSON_ASSERT(ten_k > 0);
  13745. // <--------------------------- delta ---->
  13746. // <---- dist --------->
  13747. // --------------[------------------+-------------------]--------------
  13748. // M- w M+
  13749. //
  13750. // ten_k
  13751. // <------>
  13752. // <---- rest ---->
  13753. // --------------[------------------+----+--------------]--------------
  13754. // w V
  13755. // = buf * 10^k
  13756. //
  13757. // ten_k represents a unit-in-the-last-place in the decimal representation
  13758. // stored in buf.
  13759. // Decrement buf by ten_k while this takes buf closer to w.
  13760. // The tests are written in this order to avoid overflow in unsigned
  13761. // integer arithmetic.
  13762. while (rest < dist
  13763. && delta - rest >= ten_k
  13764. && (rest + ten_k < dist || dist - rest > rest + ten_k - dist))
  13765. {
  13766. JSON_ASSERT(buf[len - 1] != '0');
  13767. buf[len - 1]--;
  13768. rest += ten_k;
  13769. }
  13770. }
  13771. /*!
  13772. Generates V = buffer * 10^decimal_exponent, such that M- <= V <= M+.
  13773. M- and M+ must be normalized and share the same exponent -60 <= e <= -32.
  13774. */
  13775. inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent,
  13776. diyfp M_minus, diyfp w, diyfp M_plus)
  13777. {
  13778. static_assert(kAlpha >= -60, "internal error");
  13779. static_assert(kGamma <= -32, "internal error");
  13780. // Generates the digits (and the exponent) of a decimal floating-point
  13781. // number V = buffer * 10^decimal_exponent in the range [M-, M+]. The diyfp's
  13782. // w, M- and M+ share the same exponent e, which satisfies alpha <= e <= gamma.
  13783. //
  13784. // <--------------------------- delta ---->
  13785. // <---- dist --------->
  13786. // --------------[------------------+-------------------]--------------
  13787. // M- w M+
  13788. //
  13789. // Grisu2 generates the digits of M+ from left to right and stops as soon as
  13790. // V is in [M-,M+].
  13791. JSON_ASSERT(M_plus.e >= kAlpha);
  13792. JSON_ASSERT(M_plus.e <= kGamma);
  13793. std::uint64_t delta = diyfp::sub(M_plus, M_minus).f; // (significand of (M+ - M-), implicit exponent is e)
  13794. std::uint64_t dist = diyfp::sub(M_plus, w ).f; // (significand of (M+ - w ), implicit exponent is e)
  13795. // Split M+ = f * 2^e into two parts p1 and p2 (note: e < 0):
  13796. //
  13797. // M+ = f * 2^e
  13798. // = ((f div 2^-e) * 2^-e + (f mod 2^-e)) * 2^e
  13799. // = ((p1 ) * 2^-e + (p2 )) * 2^e
  13800. // = p1 + p2 * 2^e
  13801. const diyfp one(std::uint64_t{1} << -M_plus.e, M_plus.e);
  13802. 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.)
  13803. std::uint64_t p2 = M_plus.f & (one.f - 1); // p2 = f mod 2^-e
  13804. // 1)
  13805. //
  13806. // Generate the digits of the integral part p1 = d[n-1]...d[1]d[0]
  13807. JSON_ASSERT(p1 > 0);
  13808. std::uint32_t pow10{};
  13809. const int k = find_largest_pow10(p1, pow10);
  13810. // 10^(k-1) <= p1 < 10^k, pow10 = 10^(k-1)
  13811. //
  13812. // p1 = (p1 div 10^(k-1)) * 10^(k-1) + (p1 mod 10^(k-1))
  13813. // = (d[k-1] ) * 10^(k-1) + (p1 mod 10^(k-1))
  13814. //
  13815. // M+ = p1 + p2 * 2^e
  13816. // = d[k-1] * 10^(k-1) + (p1 mod 10^(k-1)) + p2 * 2^e
  13817. // = d[k-1] * 10^(k-1) + ((p1 mod 10^(k-1)) * 2^-e + p2) * 2^e
  13818. // = d[k-1] * 10^(k-1) + ( rest) * 2^e
  13819. //
  13820. // Now generate the digits d[n] of p1 from left to right (n = k-1,...,0)
  13821. //
  13822. // p1 = d[k-1]...d[n] * 10^n + d[n-1]...d[0]
  13823. //
  13824. // but stop as soon as
  13825. //
  13826. // rest * 2^e = (d[n-1]...d[0] * 2^-e + p2) * 2^e <= delta * 2^e
  13827. int n = k;
  13828. while (n > 0)
  13829. {
  13830. // Invariants:
  13831. // M+ = buffer * 10^n + (p1 + p2 * 2^e) (buffer = 0 for n = k)
  13832. // pow10 = 10^(n-1) <= p1 < 10^n
  13833. //
  13834. const std::uint32_t d = p1 / pow10; // d = p1 div 10^(n-1)
  13835. const std::uint32_t r = p1 % pow10; // r = p1 mod 10^(n-1)
  13836. //
  13837. // M+ = buffer * 10^n + (d * 10^(n-1) + r) + p2 * 2^e
  13838. // = (buffer * 10 + d) * 10^(n-1) + (r + p2 * 2^e)
  13839. //
  13840. JSON_ASSERT(d <= 9);
  13841. buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
  13842. //
  13843. // M+ = buffer * 10^(n-1) + (r + p2 * 2^e)
  13844. //
  13845. p1 = r;
  13846. n--;
  13847. //
  13848. // M+ = buffer * 10^n + (p1 + p2 * 2^e)
  13849. // pow10 = 10^n
  13850. //
  13851. // Now check if enough digits have been generated.
  13852. // Compute
  13853. //
  13854. // p1 + p2 * 2^e = (p1 * 2^-e + p2) * 2^e = rest * 2^e
  13855. //
  13856. // Note:
  13857. // Since rest and delta share the same exponent e, it suffices to
  13858. // compare the significands.
  13859. const std::uint64_t rest = (std::uint64_t{p1} << -one.e) + p2;
  13860. if (rest <= delta)
  13861. {
  13862. // V = buffer * 10^n, with M- <= V <= M+.
  13863. decimal_exponent += n;
  13864. // We may now just stop. But instead look if the buffer could be
  13865. // decremented to bring V closer to w.
  13866. //
  13867. // pow10 = 10^n is now 1 ulp in the decimal representation V.
  13868. // The rounding procedure works with diyfp's with an implicit
  13869. // exponent of e.
  13870. //
  13871. // 10^n = (10^n * 2^-e) * 2^e = ulp * 2^e
  13872. //
  13873. const std::uint64_t ten_n = std::uint64_t{pow10} << -one.e;
  13874. grisu2_round(buffer, length, dist, delta, rest, ten_n);
  13875. return;
  13876. }
  13877. pow10 /= 10;
  13878. //
  13879. // pow10 = 10^(n-1) <= p1 < 10^n
  13880. // Invariants restored.
  13881. }
  13882. // 2)
  13883. //
  13884. // The digits of the integral part have been generated:
  13885. //
  13886. // M+ = d[k-1]...d[1]d[0] + p2 * 2^e
  13887. // = buffer + p2 * 2^e
  13888. //
  13889. // Now generate the digits of the fractional part p2 * 2^e.
  13890. //
  13891. // Note:
  13892. // No decimal point is generated: the exponent is adjusted instead.
  13893. //
  13894. // p2 actually represents the fraction
  13895. //
  13896. // p2 * 2^e
  13897. // = p2 / 2^-e
  13898. // = d[-1] / 10^1 + d[-2] / 10^2 + ...
  13899. //
  13900. // Now generate the digits d[-m] of p1 from left to right (m = 1,2,...)
  13901. //
  13902. // p2 * 2^e = d[-1]d[-2]...d[-m] * 10^-m
  13903. // + 10^-m * (d[-m-1] / 10^1 + d[-m-2] / 10^2 + ...)
  13904. //
  13905. // using
  13906. //
  13907. // 10^m * p2 = ((10^m * p2) div 2^-e) * 2^-e + ((10^m * p2) mod 2^-e)
  13908. // = ( d) * 2^-e + ( r)
  13909. //
  13910. // or
  13911. // 10^m * p2 * 2^e = d + r * 2^e
  13912. //
  13913. // i.e.
  13914. //
  13915. // M+ = buffer + p2 * 2^e
  13916. // = buffer + 10^-m * (d + r * 2^e)
  13917. // = (buffer * 10^m + d) * 10^-m + 10^-m * r * 2^e
  13918. //
  13919. // and stop as soon as 10^-m * r * 2^e <= delta * 2^e
  13920. JSON_ASSERT(p2 > delta);
  13921. int m = 0;
  13922. for (;;)
  13923. {
  13924. // Invariant:
  13925. // M+ = buffer * 10^-m + 10^-m * (d[-m-1] / 10 + d[-m-2] / 10^2 + ...) * 2^e
  13926. // = buffer * 10^-m + 10^-m * (p2 ) * 2^e
  13927. // = buffer * 10^-m + 10^-m * (1/10 * (10 * p2) ) * 2^e
  13928. // = buffer * 10^-m + 10^-m * (1/10 * ((10*p2 div 2^-e) * 2^-e + (10*p2 mod 2^-e)) * 2^e
  13929. //
  13930. JSON_ASSERT(p2 <= (std::numeric_limits<std::uint64_t>::max)() / 10);
  13931. p2 *= 10;
  13932. const std::uint64_t d = p2 >> -one.e; // d = (10 * p2) div 2^-e
  13933. const std::uint64_t r = p2 & (one.f - 1); // r = (10 * p2) mod 2^-e
  13934. //
  13935. // M+ = buffer * 10^-m + 10^-m * (1/10 * (d * 2^-e + r) * 2^e
  13936. // = buffer * 10^-m + 10^-m * (1/10 * (d + r * 2^e))
  13937. // = (buffer * 10 + d) * 10^(-m-1) + 10^(-m-1) * r * 2^e
  13938. //
  13939. JSON_ASSERT(d <= 9);
  13940. buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
  13941. //
  13942. // M+ = buffer * 10^(-m-1) + 10^(-m-1) * r * 2^e
  13943. //
  13944. p2 = r;
  13945. m++;
  13946. //
  13947. // M+ = buffer * 10^-m + 10^-m * p2 * 2^e
  13948. // Invariant restored.
  13949. // Check if enough digits have been generated.
  13950. //
  13951. // 10^-m * p2 * 2^e <= delta * 2^e
  13952. // p2 * 2^e <= 10^m * delta * 2^e
  13953. // p2 <= 10^m * delta
  13954. delta *= 10;
  13955. dist *= 10;
  13956. if (p2 <= delta)
  13957. {
  13958. break;
  13959. }
  13960. }
  13961. // V = buffer * 10^-m, with M- <= V <= M+.
  13962. decimal_exponent -= m;
  13963. // 1 ulp in the decimal representation is now 10^-m.
  13964. // Since delta and dist are now scaled by 10^m, we need to do the
  13965. // same with ulp in order to keep the units in sync.
  13966. //
  13967. // 10^m * 10^-m = 1 = 2^-e * 2^e = ten_m * 2^e
  13968. //
  13969. const std::uint64_t ten_m = one.f;
  13970. grisu2_round(buffer, length, dist, delta, p2, ten_m);
  13971. // By construction this algorithm generates the shortest possible decimal
  13972. // number (Loitsch, Theorem 6.2) which rounds back to w.
  13973. // For an input number of precision p, at least
  13974. //
  13975. // N = 1 + ceil(p * log_10(2))
  13976. //
  13977. // decimal digits are sufficient to identify all binary floating-point
  13978. // numbers (Matula, "In-and-Out conversions").
  13979. // This implies that the algorithm does not produce more than N decimal
  13980. // digits.
  13981. //
  13982. // N = 17 for p = 53 (IEEE double precision)
  13983. // N = 9 for p = 24 (IEEE single precision)
  13984. }
  13985. /*!
  13986. v = buf * 10^decimal_exponent
  13987. len is the length of the buffer (number of decimal digits)
  13988. The buffer must be large enough, i.e. >= max_digits10.
  13989. */
  13990. JSON_HEDLEY_NON_NULL(1)
  13991. inline void grisu2(char* buf, int& len, int& decimal_exponent,
  13992. diyfp m_minus, diyfp v, diyfp m_plus)
  13993. {
  13994. JSON_ASSERT(m_plus.e == m_minus.e);
  13995. JSON_ASSERT(m_plus.e == v.e);
  13996. // --------(-----------------------+-----------------------)-------- (A)
  13997. // m- v m+
  13998. //
  13999. // --------------------(-----------+-----------------------)-------- (B)
  14000. // m- v m+
  14001. //
  14002. // First scale v (and m- and m+) such that the exponent is in the range
  14003. // [alpha, gamma].
  14004. const cached_power cached = get_cached_power_for_binary_exponent(m_plus.e);
  14005. const diyfp c_minus_k(cached.f, cached.e); // = c ~= 10^-k
  14006. // The exponent of the products is = v.e + c_minus_k.e + q and is in the range [alpha,gamma]
  14007. const diyfp w = diyfp::mul(v, c_minus_k);
  14008. const diyfp w_minus = diyfp::mul(m_minus, c_minus_k);
  14009. const diyfp w_plus = diyfp::mul(m_plus, c_minus_k);
  14010. // ----(---+---)---------------(---+---)---------------(---+---)----
  14011. // w- w w+
  14012. // = c*m- = c*v = c*m+
  14013. //
  14014. // diyfp::mul rounds its result and c_minus_k is approximated too. w, w- and
  14015. // w+ are now off by a small amount.
  14016. // In fact:
  14017. //
  14018. // w - v * 10^k < 1 ulp
  14019. //
  14020. // To account for this inaccuracy, add resp. subtract 1 ulp.
  14021. //
  14022. // --------+---[---------------(---+---)---------------]---+--------
  14023. // w- M- w M+ w+
  14024. //
  14025. // Now any number in [M-, M+] (bounds included) will round to w when input,
  14026. // regardless of how the input rounding algorithm breaks ties.
  14027. //
  14028. // And digit_gen generates the shortest possible such number in [M-, M+].
  14029. // Note that this does not mean that Grisu2 always generates the shortest
  14030. // possible number in the interval (m-, m+).
  14031. const diyfp M_minus(w_minus.f + 1, w_minus.e);
  14032. const diyfp M_plus (w_plus.f - 1, w_plus.e );
  14033. decimal_exponent = -cached.k; // = -(-k) = k
  14034. grisu2_digit_gen(buf, len, decimal_exponent, M_minus, w, M_plus);
  14035. }
  14036. /*!
  14037. v = buf * 10^decimal_exponent
  14038. len is the length of the buffer (number of decimal digits)
  14039. The buffer must be large enough, i.e. >= max_digits10.
  14040. */
  14041. template<typename FloatType>
  14042. JSON_HEDLEY_NON_NULL(1)
  14043. void grisu2(char* buf, int& len, int& decimal_exponent, FloatType value)
  14044. {
  14045. static_assert(diyfp::kPrecision >= std::numeric_limits<FloatType>::digits + 3,
  14046. "internal error: not enough precision");
  14047. JSON_ASSERT(std::isfinite(value));
  14048. JSON_ASSERT(value > 0);
  14049. // If the neighbors (and boundaries) of 'value' are always computed for double-precision
  14050. // numbers, all float's can be recovered using strtod (and strtof). However, the resulting
  14051. // decimal representations are not exactly "short".
  14052. //
  14053. // The documentation for 'std::to_chars' (https://en.cppreference.com/w/cpp/utility/to_chars)
  14054. // says "value is converted to a string as if by std::sprintf in the default ("C") locale"
  14055. // and since sprintf promotes float's to double's, I think this is exactly what 'std::to_chars'
  14056. // does.
  14057. // On the other hand, the documentation for 'std::to_chars' requires that "parsing the
  14058. // representation using the corresponding std::from_chars function recovers value exactly". That
  14059. // indicates that single precision floating-point numbers should be recovered using
  14060. // 'std::strtof'.
  14061. //
  14062. // NB: If the neighbors are computed for single-precision numbers, there is a single float
  14063. // (7.0385307e-26f) which can't be recovered using strtod. The resulting double precision
  14064. // value is off by 1 ulp.
  14065. #if 0
  14066. const boundaries w = compute_boundaries(static_cast<double>(value));
  14067. #else
  14068. const boundaries w = compute_boundaries(value);
  14069. #endif
  14070. grisu2(buf, len, decimal_exponent, w.minus, w.w, w.plus);
  14071. }
  14072. /*!
  14073. @brief appends a decimal representation of e to buf
  14074. @return a pointer to the element following the exponent.
  14075. @pre -1000 < e < 1000
  14076. */
  14077. JSON_HEDLEY_NON_NULL(1)
  14078. JSON_HEDLEY_RETURNS_NON_NULL
  14079. inline char* append_exponent(char* buf, int e)
  14080. {
  14081. JSON_ASSERT(e > -1000);
  14082. JSON_ASSERT(e < 1000);
  14083. if (e < 0)
  14084. {
  14085. e = -e;
  14086. *buf++ = '-';
  14087. }
  14088. else
  14089. {
  14090. *buf++ = '+';
  14091. }
  14092. auto k = static_cast<std::uint32_t>(e);
  14093. if (k < 10)
  14094. {
  14095. // Always print at least two digits in the exponent.
  14096. // This is for compatibility with printf("%g").
  14097. *buf++ = '0';
  14098. *buf++ = static_cast<char>('0' + k);
  14099. }
  14100. else if (k < 100)
  14101. {
  14102. *buf++ = static_cast<char>('0' + k / 10);
  14103. k %= 10;
  14104. *buf++ = static_cast<char>('0' + k);
  14105. }
  14106. else
  14107. {
  14108. *buf++ = static_cast<char>('0' + k / 100);
  14109. k %= 100;
  14110. *buf++ = static_cast<char>('0' + k / 10);
  14111. k %= 10;
  14112. *buf++ = static_cast<char>('0' + k);
  14113. }
  14114. return buf;
  14115. }
  14116. /*!
  14117. @brief prettify v = buf * 10^decimal_exponent
  14118. If v is in the range [10^min_exp, 10^max_exp) it will be printed in fixed-point
  14119. notation. Otherwise it will be printed in exponential notation.
  14120. @pre min_exp < 0
  14121. @pre max_exp > 0
  14122. */
  14123. JSON_HEDLEY_NON_NULL(1)
  14124. JSON_HEDLEY_RETURNS_NON_NULL
  14125. inline char* format_buffer(char* buf, int len, int decimal_exponent,
  14126. int min_exp, int max_exp)
  14127. {
  14128. JSON_ASSERT(min_exp < 0);
  14129. JSON_ASSERT(max_exp > 0);
  14130. const int k = len;
  14131. const int n = len + decimal_exponent;
  14132. // v = buf * 10^(n-k)
  14133. // k is the length of the buffer (number of decimal digits)
  14134. // n is the position of the decimal point relative to the start of the buffer.
  14135. if (k <= n && n <= max_exp)
  14136. {
  14137. // digits[000]
  14138. // len <= max_exp + 2
  14139. std::memset(buf + k, '0', static_cast<size_t>(n) - static_cast<size_t>(k));
  14140. // Make it look like a floating-point number (#362, #378)
  14141. buf[n + 0] = '.';
  14142. buf[n + 1] = '0';
  14143. return buf + (static_cast<size_t>(n) + 2);
  14144. }
  14145. if (0 < n && n <= max_exp)
  14146. {
  14147. // dig.its
  14148. // len <= max_digits10 + 1
  14149. JSON_ASSERT(k > n);
  14150. std::memmove(buf + (static_cast<size_t>(n) + 1), buf + n, static_cast<size_t>(k) - static_cast<size_t>(n));
  14151. buf[n] = '.';
  14152. return buf + (static_cast<size_t>(k) + 1U);
  14153. }
  14154. if (min_exp < n && n <= 0)
  14155. {
  14156. // 0.[000]digits
  14157. // len <= 2 + (-min_exp - 1) + max_digits10
  14158. std::memmove(buf + (2 + static_cast<size_t>(-n)), buf, static_cast<size_t>(k));
  14159. buf[0] = '0';
  14160. buf[1] = '.';
  14161. std::memset(buf + 2, '0', static_cast<size_t>(-n));
  14162. return buf + (2U + static_cast<size_t>(-n) + static_cast<size_t>(k));
  14163. }
  14164. if (k == 1)
  14165. {
  14166. // dE+123
  14167. // len <= 1 + 5
  14168. buf += 1;
  14169. }
  14170. else
  14171. {
  14172. // d.igitsE+123
  14173. // len <= max_digits10 + 1 + 5
  14174. std::memmove(buf + 2, buf + 1, static_cast<size_t>(k) - 1);
  14175. buf[1] = '.';
  14176. buf += 1 + static_cast<size_t>(k);
  14177. }
  14178. *buf++ = 'e';
  14179. return append_exponent(buf, n - 1);
  14180. }
  14181. } // namespace dtoa_impl
  14182. /*!
  14183. @brief generates a decimal representation of the floating-point number value in [first, last).
  14184. The format of the resulting decimal representation is similar to printf's %g
  14185. format. Returns an iterator pointing past-the-end of the decimal representation.
  14186. @note The input number must be finite, i.e. NaN's and Inf's are not supported.
  14187. @note The buffer must be large enough.
  14188. @note The result is NOT null-terminated.
  14189. */
  14190. template<typename FloatType>
  14191. JSON_HEDLEY_NON_NULL(1, 2)
  14192. JSON_HEDLEY_RETURNS_NON_NULL
  14193. char* to_chars(char* first, const char* last, FloatType value)
  14194. {
  14195. static_cast<void>(last); // maybe unused - fix warning
  14196. JSON_ASSERT(std::isfinite(value));
  14197. // Use signbit(value) instead of (value < 0) since signbit works for -0.
  14198. if (std::signbit(value))
  14199. {
  14200. value = -value;
  14201. *first++ = '-';
  14202. }
  14203. #ifdef __GNUC__
  14204. #pragma GCC diagnostic push
  14205. #pragma GCC diagnostic ignored "-Wfloat-equal"
  14206. #endif
  14207. if (value == 0) // +-0
  14208. {
  14209. *first++ = '0';
  14210. // Make it look like a floating-point number (#362, #378)
  14211. *first++ = '.';
  14212. *first++ = '0';
  14213. return first;
  14214. }
  14215. #ifdef __GNUC__
  14216. #pragma GCC diagnostic pop
  14217. #endif
  14218. JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10);
  14219. // Compute v = buffer * 10^decimal_exponent.
  14220. // The decimal digits are stored in the buffer, which needs to be interpreted
  14221. // as an unsigned decimal integer.
  14222. // len is the length of the buffer, i.e. the number of decimal digits.
  14223. int len = 0;
  14224. int decimal_exponent = 0;
  14225. dtoa_impl::grisu2(first, len, decimal_exponent, value);
  14226. JSON_ASSERT(len <= std::numeric_limits<FloatType>::max_digits10);
  14227. // Format the buffer like printf("%.*g", prec, value)
  14228. constexpr int kMinExp = -4;
  14229. // Use digits10 here to increase compatibility with version 2.
  14230. constexpr int kMaxExp = std::numeric_limits<FloatType>::digits10;
  14231. JSON_ASSERT(last - first >= kMaxExp + 2);
  14232. JSON_ASSERT(last - first >= 2 + (-kMinExp - 1) + std::numeric_limits<FloatType>::max_digits10);
  14233. JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10 + 6);
  14234. return dtoa_impl::format_buffer(first, len, decimal_exponent, kMinExp, kMaxExp);
  14235. }
  14236. } // namespace detail
  14237. } // namespace nlohmann
  14238. // #include <nlohmann/detail/exceptions.hpp>
  14239. // #include <nlohmann/detail/macro_scope.hpp>
  14240. // #include <nlohmann/detail/meta/cpp_future.hpp>
  14241. // #include <nlohmann/detail/output/binary_writer.hpp>
  14242. // #include <nlohmann/detail/output/output_adapters.hpp>
  14243. // #include <nlohmann/detail/value_t.hpp>
  14244. namespace nlohmann
  14245. {
  14246. namespace detail
  14247. {
  14248. ///////////////////
  14249. // serialization //
  14250. ///////////////////
  14251. /// how to treat decoding errors
  14252. enum class error_handler_t
  14253. {
  14254. strict, ///< throw a type_error exception in case of invalid UTF-8
  14255. replace, ///< replace invalid UTF-8 sequences with U+FFFD
  14256. ignore ///< ignore invalid UTF-8 sequences
  14257. };
  14258. template<typename BasicJsonType>
  14259. class serializer
  14260. {
  14261. using string_t = typename BasicJsonType::string_t;
  14262. using number_float_t = typename BasicJsonType::number_float_t;
  14263. using number_integer_t = typename BasicJsonType::number_integer_t;
  14264. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  14265. using binary_char_t = typename BasicJsonType::binary_t::value_type;
  14266. static constexpr std::uint8_t UTF8_ACCEPT = 0;
  14267. static constexpr std::uint8_t UTF8_REJECT = 1;
  14268. public:
  14269. /*!
  14270. @param[in] s output stream to serialize to
  14271. @param[in] ichar indentation character to use
  14272. @param[in] error_handler_ how to react on decoding errors
  14273. */
  14274. serializer(output_adapter_t<char> s, const char ichar,
  14275. error_handler_t error_handler_ = error_handler_t::strict)
  14276. : o(std::move(s))
  14277. , loc(std::localeconv())
  14278. , thousands_sep(loc->thousands_sep == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->thousands_sep)))
  14279. , decimal_point(loc->decimal_point == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->decimal_point)))
  14280. , indent_char(ichar)
  14281. , indent_string(512, indent_char)
  14282. , error_handler(error_handler_)
  14283. {}
  14284. // delete because of pointer members
  14285. serializer(const serializer&) = delete;
  14286. serializer& operator=(const serializer&) = delete;
  14287. serializer(serializer&&) = delete;
  14288. serializer& operator=(serializer&&) = delete;
  14289. ~serializer() = default;
  14290. /*!
  14291. @brief internal implementation of the serialization function
  14292. This function is called by the public member function dump and organizes
  14293. the serialization internally. The indentation level is propagated as
  14294. additional parameter. In case of arrays and objects, the function is
  14295. called recursively.
  14296. - strings and object keys are escaped using `escape_string()`
  14297. - integer numbers are converted implicitly via `operator<<`
  14298. - floating-point numbers are converted to a string using `"%g"` format
  14299. - binary values are serialized as objects containing the subtype and the
  14300. byte array
  14301. @param[in] val value to serialize
  14302. @param[in] pretty_print whether the output shall be pretty-printed
  14303. @param[in] ensure_ascii If @a ensure_ascii is true, all non-ASCII characters
  14304. in the output are escaped with `\uXXXX` sequences, and the result consists
  14305. of ASCII characters only.
  14306. @param[in] indent_step the indent level
  14307. @param[in] current_indent the current indent level (only used internally)
  14308. */
  14309. void dump(const BasicJsonType& val,
  14310. const bool pretty_print,
  14311. const bool ensure_ascii,
  14312. const unsigned int indent_step,
  14313. const unsigned int current_indent = 0)
  14314. {
  14315. switch (val.m_type)
  14316. {
  14317. case value_t::object:
  14318. {
  14319. if (val.m_value.object->empty())
  14320. {
  14321. o->write_characters("{}", 2);
  14322. return;
  14323. }
  14324. if (pretty_print)
  14325. {
  14326. o->write_characters("{\n", 2);
  14327. // variable to hold indentation for recursive calls
  14328. const auto new_indent = current_indent + indent_step;
  14329. if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
  14330. {
  14331. indent_string.resize(indent_string.size() * 2, ' ');
  14332. }
  14333. // first n-1 elements
  14334. auto i = val.m_value.object->cbegin();
  14335. for (std::size_t cnt = 0; cnt < val.m_value.object->size() - 1; ++cnt, ++i)
  14336. {
  14337. o->write_characters(indent_string.c_str(), new_indent);
  14338. o->write_character('\"');
  14339. dump_escaped(i->first, ensure_ascii);
  14340. o->write_characters("\": ", 3);
  14341. dump(i->second, true, ensure_ascii, indent_step, new_indent);
  14342. o->write_characters(",\n", 2);
  14343. }
  14344. // last element
  14345. JSON_ASSERT(i != val.m_value.object->cend());
  14346. JSON_ASSERT(std::next(i) == val.m_value.object->cend());
  14347. o->write_characters(indent_string.c_str(), new_indent);
  14348. o->write_character('\"');
  14349. dump_escaped(i->first, ensure_ascii);
  14350. o->write_characters("\": ", 3);
  14351. dump(i->second, true, ensure_ascii, indent_step, new_indent);
  14352. o->write_character('\n');
  14353. o->write_characters(indent_string.c_str(), current_indent);
  14354. o->write_character('}');
  14355. }
  14356. else
  14357. {
  14358. o->write_character('{');
  14359. // first n-1 elements
  14360. auto i = val.m_value.object->cbegin();
  14361. for (std::size_t cnt = 0; cnt < val.m_value.object->size() - 1; ++cnt, ++i)
  14362. {
  14363. o->write_character('\"');
  14364. dump_escaped(i->first, ensure_ascii);
  14365. o->write_characters("\":", 2);
  14366. dump(i->second, false, ensure_ascii, indent_step, current_indent);
  14367. o->write_character(',');
  14368. }
  14369. // last element
  14370. JSON_ASSERT(i != val.m_value.object->cend());
  14371. JSON_ASSERT(std::next(i) == val.m_value.object->cend());
  14372. o->write_character('\"');
  14373. dump_escaped(i->first, ensure_ascii);
  14374. o->write_characters("\":", 2);
  14375. dump(i->second, false, ensure_ascii, indent_step, current_indent);
  14376. o->write_character('}');
  14377. }
  14378. return;
  14379. }
  14380. case value_t::array:
  14381. {
  14382. if (val.m_value.array->empty())
  14383. {
  14384. o->write_characters("[]", 2);
  14385. return;
  14386. }
  14387. if (pretty_print)
  14388. {
  14389. o->write_characters("[\n", 2);
  14390. // variable to hold indentation for recursive calls
  14391. const auto new_indent = current_indent + indent_step;
  14392. if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
  14393. {
  14394. indent_string.resize(indent_string.size() * 2, ' ');
  14395. }
  14396. // first n-1 elements
  14397. for (auto i = val.m_value.array->cbegin();
  14398. i != val.m_value.array->cend() - 1; ++i)
  14399. {
  14400. o->write_characters(indent_string.c_str(), new_indent);
  14401. dump(*i, true, ensure_ascii, indent_step, new_indent);
  14402. o->write_characters(",\n", 2);
  14403. }
  14404. // last element
  14405. JSON_ASSERT(!val.m_value.array->empty());
  14406. o->write_characters(indent_string.c_str(), new_indent);
  14407. dump(val.m_value.array->back(), true, ensure_ascii, indent_step, new_indent);
  14408. o->write_character('\n');
  14409. o->write_characters(indent_string.c_str(), current_indent);
  14410. o->write_character(']');
  14411. }
  14412. else
  14413. {
  14414. o->write_character('[');
  14415. // first n-1 elements
  14416. for (auto i = val.m_value.array->cbegin();
  14417. i != val.m_value.array->cend() - 1; ++i)
  14418. {
  14419. dump(*i, false, ensure_ascii, indent_step, current_indent);
  14420. o->write_character(',');
  14421. }
  14422. // last element
  14423. JSON_ASSERT(!val.m_value.array->empty());
  14424. dump(val.m_value.array->back(), false, ensure_ascii, indent_step, current_indent);
  14425. o->write_character(']');
  14426. }
  14427. return;
  14428. }
  14429. case value_t::string:
  14430. {
  14431. o->write_character('\"');
  14432. dump_escaped(*val.m_value.string, ensure_ascii);
  14433. o->write_character('\"');
  14434. return;
  14435. }
  14436. case value_t::binary:
  14437. {
  14438. if (pretty_print)
  14439. {
  14440. o->write_characters("{\n", 2);
  14441. // variable to hold indentation for recursive calls
  14442. const auto new_indent = current_indent + indent_step;
  14443. if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
  14444. {
  14445. indent_string.resize(indent_string.size() * 2, ' ');
  14446. }
  14447. o->write_characters(indent_string.c_str(), new_indent);
  14448. o->write_characters("\"bytes\": [", 10);
  14449. if (!val.m_value.binary->empty())
  14450. {
  14451. for (auto i = val.m_value.binary->cbegin();
  14452. i != val.m_value.binary->cend() - 1; ++i)
  14453. {
  14454. dump_integer(*i);
  14455. o->write_characters(", ", 2);
  14456. }
  14457. dump_integer(val.m_value.binary->back());
  14458. }
  14459. o->write_characters("],\n", 3);
  14460. o->write_characters(indent_string.c_str(), new_indent);
  14461. o->write_characters("\"subtype\": ", 11);
  14462. if (val.m_value.binary->has_subtype())
  14463. {
  14464. dump_integer(val.m_value.binary->subtype());
  14465. }
  14466. else
  14467. {
  14468. o->write_characters("null", 4);
  14469. }
  14470. o->write_character('\n');
  14471. o->write_characters(indent_string.c_str(), current_indent);
  14472. o->write_character('}');
  14473. }
  14474. else
  14475. {
  14476. o->write_characters("{\"bytes\":[", 10);
  14477. if (!val.m_value.binary->empty())
  14478. {
  14479. for (auto i = val.m_value.binary->cbegin();
  14480. i != val.m_value.binary->cend() - 1; ++i)
  14481. {
  14482. dump_integer(*i);
  14483. o->write_character(',');
  14484. }
  14485. dump_integer(val.m_value.binary->back());
  14486. }
  14487. o->write_characters("],\"subtype\":", 12);
  14488. if (val.m_value.binary->has_subtype())
  14489. {
  14490. dump_integer(val.m_value.binary->subtype());
  14491. o->write_character('}');
  14492. }
  14493. else
  14494. {
  14495. o->write_characters("null}", 5);
  14496. }
  14497. }
  14498. return;
  14499. }
  14500. case value_t::boolean:
  14501. {
  14502. if (val.m_value.boolean)
  14503. {
  14504. o->write_characters("true", 4);
  14505. }
  14506. else
  14507. {
  14508. o->write_characters("false", 5);
  14509. }
  14510. return;
  14511. }
  14512. case value_t::number_integer:
  14513. {
  14514. dump_integer(val.m_value.number_integer);
  14515. return;
  14516. }
  14517. case value_t::number_unsigned:
  14518. {
  14519. dump_integer(val.m_value.number_unsigned);
  14520. return;
  14521. }
  14522. case value_t::number_float:
  14523. {
  14524. dump_float(val.m_value.number_float);
  14525. return;
  14526. }
  14527. case value_t::discarded:
  14528. {
  14529. o->write_characters("<discarded>", 11);
  14530. return;
  14531. }
  14532. case value_t::null:
  14533. {
  14534. o->write_characters("null", 4);
  14535. return;
  14536. }
  14537. default: // LCOV_EXCL_LINE
  14538. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  14539. }
  14540. }
  14541. JSON_PRIVATE_UNLESS_TESTED:
  14542. /*!
  14543. @brief dump escaped string
  14544. Escape a string by replacing certain special characters by a sequence of an
  14545. escape character (backslash) and another character and other control
  14546. characters by a sequence of "\u" followed by a four-digit hex
  14547. representation. The escaped string is written to output stream @a o.
  14548. @param[in] s the string to escape
  14549. @param[in] ensure_ascii whether to escape non-ASCII characters with
  14550. \uXXXX sequences
  14551. @complexity Linear in the length of string @a s.
  14552. */
  14553. void dump_escaped(const string_t& s, const bool ensure_ascii)
  14554. {
  14555. std::uint32_t codepoint{};
  14556. std::uint8_t state = UTF8_ACCEPT;
  14557. std::size_t bytes = 0; // number of bytes written to string_buffer
  14558. // number of bytes written at the point of the last valid byte
  14559. std::size_t bytes_after_last_accept = 0;
  14560. std::size_t undumped_chars = 0;
  14561. for (std::size_t i = 0; i < s.size(); ++i)
  14562. {
  14563. const auto byte = static_cast<std::uint8_t>(s[i]);
  14564. switch (decode(state, codepoint, byte))
  14565. {
  14566. case UTF8_ACCEPT: // decode found a new code point
  14567. {
  14568. switch (codepoint)
  14569. {
  14570. case 0x08: // backspace
  14571. {
  14572. string_buffer[bytes++] = '\\';
  14573. string_buffer[bytes++] = 'b';
  14574. break;
  14575. }
  14576. case 0x09: // horizontal tab
  14577. {
  14578. string_buffer[bytes++] = '\\';
  14579. string_buffer[bytes++] = 't';
  14580. break;
  14581. }
  14582. case 0x0A: // newline
  14583. {
  14584. string_buffer[bytes++] = '\\';
  14585. string_buffer[bytes++] = 'n';
  14586. break;
  14587. }
  14588. case 0x0C: // formfeed
  14589. {
  14590. string_buffer[bytes++] = '\\';
  14591. string_buffer[bytes++] = 'f';
  14592. break;
  14593. }
  14594. case 0x0D: // carriage return
  14595. {
  14596. string_buffer[bytes++] = '\\';
  14597. string_buffer[bytes++] = 'r';
  14598. break;
  14599. }
  14600. case 0x22: // quotation mark
  14601. {
  14602. string_buffer[bytes++] = '\\';
  14603. string_buffer[bytes++] = '\"';
  14604. break;
  14605. }
  14606. case 0x5C: // reverse solidus
  14607. {
  14608. string_buffer[bytes++] = '\\';
  14609. string_buffer[bytes++] = '\\';
  14610. break;
  14611. }
  14612. default:
  14613. {
  14614. // escape control characters (0x00..0x1F) or, if
  14615. // ensure_ascii parameter is used, non-ASCII characters
  14616. if ((codepoint <= 0x1F) || (ensure_ascii && (codepoint >= 0x7F)))
  14617. {
  14618. if (codepoint <= 0xFFFF)
  14619. {
  14620. // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  14621. (std::snprintf)(string_buffer.data() + bytes, 7, "\\u%04x",
  14622. static_cast<std::uint16_t>(codepoint));
  14623. bytes += 6;
  14624. }
  14625. else
  14626. {
  14627. // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  14628. (std::snprintf)(string_buffer.data() + bytes, 13, "\\u%04x\\u%04x",
  14629. static_cast<std::uint16_t>(0xD7C0u + (codepoint >> 10u)),
  14630. static_cast<std::uint16_t>(0xDC00u + (codepoint & 0x3FFu)));
  14631. bytes += 12;
  14632. }
  14633. }
  14634. else
  14635. {
  14636. // copy byte to buffer (all previous bytes
  14637. // been copied have in default case above)
  14638. string_buffer[bytes++] = s[i];
  14639. }
  14640. break;
  14641. }
  14642. }
  14643. // write buffer and reset index; there must be 13 bytes
  14644. // left, as this is the maximal number of bytes to be
  14645. // written ("\uxxxx\uxxxx\0") for one code point
  14646. if (string_buffer.size() - bytes < 13)
  14647. {
  14648. o->write_characters(string_buffer.data(), bytes);
  14649. bytes = 0;
  14650. }
  14651. // remember the byte position of this accept
  14652. bytes_after_last_accept = bytes;
  14653. undumped_chars = 0;
  14654. break;
  14655. }
  14656. case UTF8_REJECT: // decode found invalid UTF-8 byte
  14657. {
  14658. switch (error_handler)
  14659. {
  14660. case error_handler_t::strict:
  14661. {
  14662. std::string sn(9, '\0');
  14663. // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  14664. (std::snprintf)(&sn[0], sn.size(), "%.2X", byte);
  14665. JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + sn, BasicJsonType()));
  14666. }
  14667. case error_handler_t::ignore:
  14668. case error_handler_t::replace:
  14669. {
  14670. // in case we saw this character the first time, we
  14671. // would like to read it again, because the byte
  14672. // may be OK for itself, but just not OK for the
  14673. // previous sequence
  14674. if (undumped_chars > 0)
  14675. {
  14676. --i;
  14677. }
  14678. // reset length buffer to the last accepted index;
  14679. // thus removing/ignoring the invalid characters
  14680. bytes = bytes_after_last_accept;
  14681. if (error_handler == error_handler_t::replace)
  14682. {
  14683. // add a replacement character
  14684. if (ensure_ascii)
  14685. {
  14686. string_buffer[bytes++] = '\\';
  14687. string_buffer[bytes++] = 'u';
  14688. string_buffer[bytes++] = 'f';
  14689. string_buffer[bytes++] = 'f';
  14690. string_buffer[bytes++] = 'f';
  14691. string_buffer[bytes++] = 'd';
  14692. }
  14693. else
  14694. {
  14695. string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xEF');
  14696. string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xBF');
  14697. string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xBD');
  14698. }
  14699. // write buffer and reset index; there must be 13 bytes
  14700. // left, as this is the maximal number of bytes to be
  14701. // written ("\uxxxx\uxxxx\0") for one code point
  14702. if (string_buffer.size() - bytes < 13)
  14703. {
  14704. o->write_characters(string_buffer.data(), bytes);
  14705. bytes = 0;
  14706. }
  14707. bytes_after_last_accept = bytes;
  14708. }
  14709. undumped_chars = 0;
  14710. // continue processing the string
  14711. state = UTF8_ACCEPT;
  14712. break;
  14713. }
  14714. default: // LCOV_EXCL_LINE
  14715. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  14716. }
  14717. break;
  14718. }
  14719. default: // decode found yet incomplete multi-byte code point
  14720. {
  14721. if (!ensure_ascii)
  14722. {
  14723. // code point will not be escaped - copy byte to buffer
  14724. string_buffer[bytes++] = s[i];
  14725. }
  14726. ++undumped_chars;
  14727. break;
  14728. }
  14729. }
  14730. }
  14731. // we finished processing the string
  14732. if (JSON_HEDLEY_LIKELY(state == UTF8_ACCEPT))
  14733. {
  14734. // write buffer
  14735. if (bytes > 0)
  14736. {
  14737. o->write_characters(string_buffer.data(), bytes);
  14738. }
  14739. }
  14740. else
  14741. {
  14742. // we finish reading, but do not accept: string was incomplete
  14743. switch (error_handler)
  14744. {
  14745. case error_handler_t::strict:
  14746. {
  14747. std::string sn(9, '\0');
  14748. // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  14749. (std::snprintf)(&sn[0], sn.size(), "%.2X", static_cast<std::uint8_t>(s.back()));
  14750. JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + sn, BasicJsonType()));
  14751. }
  14752. case error_handler_t::ignore:
  14753. {
  14754. // write all accepted bytes
  14755. o->write_characters(string_buffer.data(), bytes_after_last_accept);
  14756. break;
  14757. }
  14758. case error_handler_t::replace:
  14759. {
  14760. // write all accepted bytes
  14761. o->write_characters(string_buffer.data(), bytes_after_last_accept);
  14762. // add a replacement character
  14763. if (ensure_ascii)
  14764. {
  14765. o->write_characters("\\ufffd", 6);
  14766. }
  14767. else
  14768. {
  14769. o->write_characters("\xEF\xBF\xBD", 3);
  14770. }
  14771. break;
  14772. }
  14773. default: // LCOV_EXCL_LINE
  14774. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  14775. }
  14776. }
  14777. }
  14778. private:
  14779. /*!
  14780. @brief count digits
  14781. Count the number of decimal (base 10) digits for an input unsigned integer.
  14782. @param[in] x unsigned integer number to count its digits
  14783. @return number of decimal digits
  14784. */
  14785. inline unsigned int count_digits(number_unsigned_t x) noexcept
  14786. {
  14787. unsigned int n_digits = 1;
  14788. for (;;)
  14789. {
  14790. if (x < 10)
  14791. {
  14792. return n_digits;
  14793. }
  14794. if (x < 100)
  14795. {
  14796. return n_digits + 1;
  14797. }
  14798. if (x < 1000)
  14799. {
  14800. return n_digits + 2;
  14801. }
  14802. if (x < 10000)
  14803. {
  14804. return n_digits + 3;
  14805. }
  14806. x = x / 10000u;
  14807. n_digits += 4;
  14808. }
  14809. }
  14810. /*!
  14811. @brief dump an integer
  14812. Dump a given integer to output stream @a o. Works internally with
  14813. @a number_buffer.
  14814. @param[in] x integer number (signed or unsigned) to dump
  14815. @tparam NumberType either @a number_integer_t or @a number_unsigned_t
  14816. */
  14817. template < typename NumberType, detail::enable_if_t <
  14818. std::is_integral<NumberType>::value ||
  14819. std::is_same<NumberType, number_unsigned_t>::value ||
  14820. std::is_same<NumberType, number_integer_t>::value ||
  14821. std::is_same<NumberType, binary_char_t>::value,
  14822. int > = 0 >
  14823. void dump_integer(NumberType x)
  14824. {
  14825. static constexpr std::array<std::array<char, 2>, 100> digits_to_99
  14826. {
  14827. {
  14828. {{'0', '0'}}, {{'0', '1'}}, {{'0', '2'}}, {{'0', '3'}}, {{'0', '4'}}, {{'0', '5'}}, {{'0', '6'}}, {{'0', '7'}}, {{'0', '8'}}, {{'0', '9'}},
  14829. {{'1', '0'}}, {{'1', '1'}}, {{'1', '2'}}, {{'1', '3'}}, {{'1', '4'}}, {{'1', '5'}}, {{'1', '6'}}, {{'1', '7'}}, {{'1', '8'}}, {{'1', '9'}},
  14830. {{'2', '0'}}, {{'2', '1'}}, {{'2', '2'}}, {{'2', '3'}}, {{'2', '4'}}, {{'2', '5'}}, {{'2', '6'}}, {{'2', '7'}}, {{'2', '8'}}, {{'2', '9'}},
  14831. {{'3', '0'}}, {{'3', '1'}}, {{'3', '2'}}, {{'3', '3'}}, {{'3', '4'}}, {{'3', '5'}}, {{'3', '6'}}, {{'3', '7'}}, {{'3', '8'}}, {{'3', '9'}},
  14832. {{'4', '0'}}, {{'4', '1'}}, {{'4', '2'}}, {{'4', '3'}}, {{'4', '4'}}, {{'4', '5'}}, {{'4', '6'}}, {{'4', '7'}}, {{'4', '8'}}, {{'4', '9'}},
  14833. {{'5', '0'}}, {{'5', '1'}}, {{'5', '2'}}, {{'5', '3'}}, {{'5', '4'}}, {{'5', '5'}}, {{'5', '6'}}, {{'5', '7'}}, {{'5', '8'}}, {{'5', '9'}},
  14834. {{'6', '0'}}, {{'6', '1'}}, {{'6', '2'}}, {{'6', '3'}}, {{'6', '4'}}, {{'6', '5'}}, {{'6', '6'}}, {{'6', '7'}}, {{'6', '8'}}, {{'6', '9'}},
  14835. {{'7', '0'}}, {{'7', '1'}}, {{'7', '2'}}, {{'7', '3'}}, {{'7', '4'}}, {{'7', '5'}}, {{'7', '6'}}, {{'7', '7'}}, {{'7', '8'}}, {{'7', '9'}},
  14836. {{'8', '0'}}, {{'8', '1'}}, {{'8', '2'}}, {{'8', '3'}}, {{'8', '4'}}, {{'8', '5'}}, {{'8', '6'}}, {{'8', '7'}}, {{'8', '8'}}, {{'8', '9'}},
  14837. {{'9', '0'}}, {{'9', '1'}}, {{'9', '2'}}, {{'9', '3'}}, {{'9', '4'}}, {{'9', '5'}}, {{'9', '6'}}, {{'9', '7'}}, {{'9', '8'}}, {{'9', '9'}},
  14838. }
  14839. };
  14840. // special case for "0"
  14841. if (x == 0)
  14842. {
  14843. o->write_character('0');
  14844. return;
  14845. }
  14846. // use a pointer to fill the buffer
  14847. auto buffer_ptr = number_buffer.begin(); // NOLINT(llvm-qualified-auto,readability-qualified-auto,cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  14848. const bool is_negative = std::is_signed<NumberType>::value && !(x >= 0); // see issue #755
  14849. number_unsigned_t abs_value;
  14850. unsigned int n_chars{};
  14851. if (is_negative)
  14852. {
  14853. *buffer_ptr = '-';
  14854. abs_value = remove_sign(static_cast<number_integer_t>(x));
  14855. // account one more byte for the minus sign
  14856. n_chars = 1 + count_digits(abs_value);
  14857. }
  14858. else
  14859. {
  14860. abs_value = static_cast<number_unsigned_t>(x);
  14861. n_chars = count_digits(abs_value);
  14862. }
  14863. // spare 1 byte for '\0'
  14864. JSON_ASSERT(n_chars < number_buffer.size() - 1);
  14865. // jump to the end to generate the string from backward
  14866. // so we later avoid reversing the result
  14867. buffer_ptr += n_chars;
  14868. // Fast int2ascii implementation inspired by "Fastware" talk by Andrei Alexandrescu
  14869. // See: https://www.youtube.com/watch?v=o4-CwDo2zpg
  14870. while (abs_value >= 100)
  14871. {
  14872. const auto digits_index = static_cast<unsigned>((abs_value % 100));
  14873. abs_value /= 100;
  14874. *(--buffer_ptr) = digits_to_99[digits_index][1];
  14875. *(--buffer_ptr) = digits_to_99[digits_index][0];
  14876. }
  14877. if (abs_value >= 10)
  14878. {
  14879. const auto digits_index = static_cast<unsigned>(abs_value);
  14880. *(--buffer_ptr) = digits_to_99[digits_index][1];
  14881. *(--buffer_ptr) = digits_to_99[digits_index][0];
  14882. }
  14883. else
  14884. {
  14885. *(--buffer_ptr) = static_cast<char>('0' + abs_value);
  14886. }
  14887. o->write_characters(number_buffer.data(), n_chars);
  14888. }
  14889. /*!
  14890. @brief dump a floating-point number
  14891. Dump a given floating-point number to output stream @a o. Works internally
  14892. with @a number_buffer.
  14893. @param[in] x floating-point number to dump
  14894. */
  14895. void dump_float(number_float_t x)
  14896. {
  14897. // NaN / inf
  14898. if (!std::isfinite(x))
  14899. {
  14900. o->write_characters("null", 4);
  14901. return;
  14902. }
  14903. // If number_float_t is an IEEE-754 single or double precision number,
  14904. // use the Grisu2 algorithm to produce short numbers which are
  14905. // guaranteed to round-trip, using strtof and strtod, resp.
  14906. //
  14907. // NB: The test below works if <long double> == <double>.
  14908. static constexpr bool is_ieee_single_or_double
  14909. = (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) ||
  14910. (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);
  14911. dump_float(x, std::integral_constant<bool, is_ieee_single_or_double>());
  14912. }
  14913. void dump_float(number_float_t x, std::true_type /*is_ieee_single_or_double*/)
  14914. {
  14915. auto* begin = number_buffer.data();
  14916. auto* end = ::nlohmann::detail::to_chars(begin, begin + number_buffer.size(), x);
  14917. o->write_characters(begin, static_cast<size_t>(end - begin));
  14918. }
  14919. void dump_float(number_float_t x, std::false_type /*is_ieee_single_or_double*/)
  14920. {
  14921. // get number of digits for a float -> text -> float round-trip
  14922. static constexpr auto d = std::numeric_limits<number_float_t>::max_digits10;
  14923. // the actual conversion
  14924. // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  14925. std::ptrdiff_t len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*g", d, x);
  14926. // negative value indicates an error
  14927. JSON_ASSERT(len > 0);
  14928. // check if buffer was large enough
  14929. JSON_ASSERT(static_cast<std::size_t>(len) < number_buffer.size());
  14930. // erase thousands separator
  14931. if (thousands_sep != '\0')
  14932. {
  14933. auto* const end = std::remove(number_buffer.begin(),
  14934. number_buffer.begin() + len, thousands_sep);
  14935. std::fill(end, number_buffer.end(), '\0');
  14936. JSON_ASSERT((end - number_buffer.begin()) <= len);
  14937. len = (end - number_buffer.begin());
  14938. }
  14939. // convert decimal point to '.'
  14940. if (decimal_point != '\0' && decimal_point != '.')
  14941. {
  14942. auto* const dec_pos = std::find(number_buffer.begin(), number_buffer.end(), decimal_point);
  14943. if (dec_pos != number_buffer.end())
  14944. {
  14945. *dec_pos = '.';
  14946. }
  14947. }
  14948. o->write_characters(number_buffer.data(), static_cast<std::size_t>(len));
  14949. // determine if need to append ".0"
  14950. const bool value_is_int_like =
  14951. std::none_of(number_buffer.begin(), number_buffer.begin() + len + 1,
  14952. [](char c)
  14953. {
  14954. return c == '.' || c == 'e';
  14955. });
  14956. if (value_is_int_like)
  14957. {
  14958. o->write_characters(".0", 2);
  14959. }
  14960. }
  14961. /*!
  14962. @brief check whether a string is UTF-8 encoded
  14963. The function checks each byte of a string whether it is UTF-8 encoded. The
  14964. result of the check is stored in the @a state parameter. The function must
  14965. be called initially with state 0 (accept). State 1 means the string must
  14966. be rejected, because the current byte is not allowed. If the string is
  14967. completely processed, but the state is non-zero, the string ended
  14968. prematurely; that is, the last byte indicated more bytes should have
  14969. followed.
  14970. @param[in,out] state the state of the decoding
  14971. @param[in,out] codep codepoint (valid only if resulting state is UTF8_ACCEPT)
  14972. @param[in] byte next byte to decode
  14973. @return new state
  14974. @note The function has been edited: a std::array is used.
  14975. @copyright Copyright (c) 2008-2009 Bjoern Hoehrmann <[email protected]>
  14976. @sa http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
  14977. */
  14978. static std::uint8_t decode(std::uint8_t& state, std::uint32_t& codep, const std::uint8_t byte) noexcept
  14979. {
  14980. static const std::array<std::uint8_t, 400> utf8d =
  14981. {
  14982. {
  14983. 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
  14984. 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
  14985. 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
  14986. 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
  14987. 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
  14988. 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
  14989. 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
  14990. 0xA, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x4, 0x3, 0x3, // E0..EF
  14991. 0xB, 0x6, 0x6, 0x6, 0x5, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, // F0..FF
  14992. 0x0, 0x1, 0x2, 0x3, 0x5, 0x8, 0x7, 0x1, 0x1, 0x1, 0x4, 0x6, 0x1, 0x1, 0x1, 0x1, // s0..s0
  14993. 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
  14994. 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
  14995. 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
  14996. 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
  14997. }
  14998. };
  14999. JSON_ASSERT(byte < utf8d.size());
  15000. const std::uint8_t type = utf8d[byte];
  15001. codep = (state != UTF8_ACCEPT)
  15002. ? (byte & 0x3fu) | (codep << 6u)
  15003. : (0xFFu >> type) & (byte);
  15004. std::size_t index = 256u + static_cast<size_t>(state) * 16u + static_cast<size_t>(type);
  15005. JSON_ASSERT(index < 400);
  15006. state = utf8d[index];
  15007. return state;
  15008. }
  15009. /*
  15010. * Overload to make the compiler happy while it is instantiating
  15011. * dump_integer for number_unsigned_t.
  15012. * Must never be called.
  15013. */
  15014. number_unsigned_t remove_sign(number_unsigned_t x)
  15015. {
  15016. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  15017. return x; // LCOV_EXCL_LINE
  15018. }
  15019. /*
  15020. * Helper function for dump_integer
  15021. *
  15022. * This function takes a negative signed integer and returns its absolute
  15023. * value as unsigned integer. The plus/minus shuffling is necessary as we can
  15024. * not directly remove the sign of an arbitrary signed integer as the
  15025. * absolute values of INT_MIN and INT_MAX are usually not the same. See
  15026. * #1708 for details.
  15027. */
  15028. inline number_unsigned_t remove_sign(number_integer_t x) noexcept
  15029. {
  15030. JSON_ASSERT(x < 0 && x < (std::numeric_limits<number_integer_t>::max)()); // NOLINT(misc-redundant-expression)
  15031. return static_cast<number_unsigned_t>(-(x + 1)) + 1;
  15032. }
  15033. private:
  15034. /// the output of the serializer
  15035. output_adapter_t<char> o = nullptr;
  15036. /// a (hopefully) large enough character buffer
  15037. std::array<char, 64> number_buffer{{}};
  15038. /// the locale
  15039. const std::lconv* loc = nullptr;
  15040. /// the locale's thousand separator character
  15041. const char thousands_sep = '\0';
  15042. /// the locale's decimal point character
  15043. const char decimal_point = '\0';
  15044. /// string buffer
  15045. std::array<char, 512> string_buffer{{}};
  15046. /// the indentation character
  15047. const char indent_char;
  15048. /// the indentation string
  15049. string_t indent_string;
  15050. /// error_handler how to react on decoding errors
  15051. const error_handler_t error_handler;
  15052. };
  15053. } // namespace detail
  15054. } // namespace nlohmann
  15055. // #include <nlohmann/detail/value_t.hpp>
  15056. // #include <nlohmann/json_fwd.hpp>
  15057. // #include <nlohmann/ordered_map.hpp>
  15058. #include <functional> // less
  15059. #include <initializer_list> // initializer_list
  15060. #include <iterator> // input_iterator_tag, iterator_traits
  15061. #include <memory> // allocator
  15062. #include <stdexcept> // for out_of_range
  15063. #include <type_traits> // enable_if, is_convertible
  15064. #include <utility> // pair
  15065. #include <vector> // vector
  15066. // #include <nlohmann/detail/macro_scope.hpp>
  15067. namespace nlohmann
  15068. {
  15069. /// ordered_map: a minimal map-like container that preserves insertion order
  15070. /// for use within nlohmann::basic_json<ordered_map>
  15071. template <class Key, class T, class IgnoredLess = std::less<Key>,
  15072. class Allocator = std::allocator<std::pair<const Key, T>>>
  15073. struct ordered_map : std::vector<std::pair<const Key, T>, Allocator>
  15074. {
  15075. using key_type = Key;
  15076. using mapped_type = T;
  15077. using Container = std::vector<std::pair<const Key, T>, Allocator>;
  15078. using typename Container::iterator;
  15079. using typename Container::const_iterator;
  15080. using typename Container::size_type;
  15081. using typename Container::value_type;
  15082. // Explicit constructors instead of `using Container::Container`
  15083. // otherwise older compilers choke on it (GCC <= 5.5, xcode <= 9.4)
  15084. ordered_map(const Allocator& alloc = Allocator()) : Container{alloc} {}
  15085. template <class It>
  15086. ordered_map(It first, It last, const Allocator& alloc = Allocator())
  15087. : Container{first, last, alloc} {}
  15088. ordered_map(std::initializer_list<T> init, const Allocator& alloc = Allocator() )
  15089. : Container{init, alloc} {}
  15090. std::pair<iterator, bool> emplace(const key_type& key, T&& t)
  15091. {
  15092. for (auto it = this->begin(); it != this->end(); ++it)
  15093. {
  15094. if (it->first == key)
  15095. {
  15096. return {it, false};
  15097. }
  15098. }
  15099. Container::emplace_back(key, t);
  15100. return {--this->end(), true};
  15101. }
  15102. T& operator[](const Key& key)
  15103. {
  15104. return emplace(key, T{}).first->second;
  15105. }
  15106. const T& operator[](const Key& key) const
  15107. {
  15108. return at(key);
  15109. }
  15110. T& at(const Key& key)
  15111. {
  15112. for (auto it = this->begin(); it != this->end(); ++it)
  15113. {
  15114. if (it->first == key)
  15115. {
  15116. return it->second;
  15117. }
  15118. }
  15119. JSON_THROW(std::out_of_range("key not found"));
  15120. }
  15121. const T& at(const Key& key) const
  15122. {
  15123. for (auto it = this->begin(); it != this->end(); ++it)
  15124. {
  15125. if (it->first == key)
  15126. {
  15127. return it->second;
  15128. }
  15129. }
  15130. JSON_THROW(std::out_of_range("key not found"));
  15131. }
  15132. size_type erase(const Key& key)
  15133. {
  15134. for (auto it = this->begin(); it != this->end(); ++it)
  15135. {
  15136. if (it->first == key)
  15137. {
  15138. // Since we cannot move const Keys, re-construct them in place
  15139. for (auto next = it; ++next != this->end(); ++it)
  15140. {
  15141. it->~value_type(); // Destroy but keep allocation
  15142. new (&*it) value_type{std::move(*next)};
  15143. }
  15144. Container::pop_back();
  15145. return 1;
  15146. }
  15147. }
  15148. return 0;
  15149. }
  15150. iterator erase(iterator pos)
  15151. {
  15152. auto it = pos;
  15153. // Since we cannot move const Keys, re-construct them in place
  15154. for (auto next = it; ++next != this->end(); ++it)
  15155. {
  15156. it->~value_type(); // Destroy but keep allocation
  15157. new (&*it) value_type{std::move(*next)};
  15158. }
  15159. Container::pop_back();
  15160. return pos;
  15161. }
  15162. size_type count(const Key& key) const
  15163. {
  15164. for (auto it = this->begin(); it != this->end(); ++it)
  15165. {
  15166. if (it->first == key)
  15167. {
  15168. return 1;
  15169. }
  15170. }
  15171. return 0;
  15172. }
  15173. iterator find(const Key& key)
  15174. {
  15175. for (auto it = this->begin(); it != this->end(); ++it)
  15176. {
  15177. if (it->first == key)
  15178. {
  15179. return it;
  15180. }
  15181. }
  15182. return Container::end();
  15183. }
  15184. const_iterator find(const Key& key) const
  15185. {
  15186. for (auto it = this->begin(); it != this->end(); ++it)
  15187. {
  15188. if (it->first == key)
  15189. {
  15190. return it;
  15191. }
  15192. }
  15193. return Container::end();
  15194. }
  15195. std::pair<iterator, bool> insert( value_type&& value )
  15196. {
  15197. return emplace(value.first, std::move(value.second));
  15198. }
  15199. std::pair<iterator, bool> insert( const value_type& value )
  15200. {
  15201. for (auto it = this->begin(); it != this->end(); ++it)
  15202. {
  15203. if (it->first == value.first)
  15204. {
  15205. return {it, false};
  15206. }
  15207. }
  15208. Container::push_back(value);
  15209. return {--this->end(), true};
  15210. }
  15211. template<typename InputIt>
  15212. using require_input_iter = typename std::enable_if<std::is_convertible<typename std::iterator_traits<InputIt>::iterator_category,
  15213. std::input_iterator_tag>::value>::type;
  15214. template<typename InputIt, typename = require_input_iter<InputIt>>
  15215. void insert(InputIt first, InputIt last)
  15216. {
  15217. for (auto it = first; it != last; ++it)
  15218. {
  15219. insert(*it);
  15220. }
  15221. }
  15222. };
  15223. } // namespace nlohmann
  15224. #if defined(JSON_HAS_CPP_17)
  15225. #include <string_view>
  15226. #endif
  15227. /*!
  15228. @brief namespace for Niels Lohmann
  15229. @see https://github.com/nlohmann
  15230. @since version 1.0.0
  15231. */
  15232. namespace nlohmann
  15233. {
  15234. /*!
  15235. @brief a class to store JSON values
  15236. @tparam ObjectType type for JSON objects (`std::map` by default; will be used
  15237. in @ref object_t)
  15238. @tparam ArrayType type for JSON arrays (`std::vector` by default; will be used
  15239. in @ref array_t)
  15240. @tparam StringType type for JSON strings and object keys (`std::string` by
  15241. default; will be used in @ref string_t)
  15242. @tparam BooleanType type for JSON booleans (`bool` by default; will be used
  15243. in @ref boolean_t)
  15244. @tparam NumberIntegerType type for JSON integer numbers (`int64_t` by
  15245. default; will be used in @ref number_integer_t)
  15246. @tparam NumberUnsignedType type for JSON unsigned integer numbers (@c
  15247. `uint64_t` by default; will be used in @ref number_unsigned_t)
  15248. @tparam NumberFloatType type for JSON floating-point numbers (`double` by
  15249. default; will be used in @ref number_float_t)
  15250. @tparam BinaryType type for packed binary data for compatibility with binary
  15251. serialization formats (`std::vector<std::uint8_t>` by default; will be used in
  15252. @ref binary_t)
  15253. @tparam AllocatorType type of the allocator to use (`std::allocator` by
  15254. default)
  15255. @tparam JSONSerializer the serializer to resolve internal calls to `to_json()`
  15256. and `from_json()` (@ref adl_serializer by default)
  15257. @requirement The class satisfies the following concept requirements:
  15258. - Basic
  15259. - [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible):
  15260. JSON values can be default constructed. The result will be a JSON null
  15261. value.
  15262. - [MoveConstructible](https://en.cppreference.com/w/cpp/named_req/MoveConstructible):
  15263. A JSON value can be constructed from an rvalue argument.
  15264. - [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible):
  15265. A JSON value can be copy-constructed from an lvalue expression.
  15266. - [MoveAssignable](https://en.cppreference.com/w/cpp/named_req/MoveAssignable):
  15267. A JSON value van be assigned from an rvalue argument.
  15268. - [CopyAssignable](https://en.cppreference.com/w/cpp/named_req/CopyAssignable):
  15269. A JSON value can be copy-assigned from an lvalue expression.
  15270. - [Destructible](https://en.cppreference.com/w/cpp/named_req/Destructible):
  15271. JSON values can be destructed.
  15272. - Layout
  15273. - [StandardLayoutType](https://en.cppreference.com/w/cpp/named_req/StandardLayoutType):
  15274. JSON values have
  15275. [standard layout](https://en.cppreference.com/w/cpp/language/data_members#Standard_layout):
  15276. All non-static data members are private and standard layout types, the
  15277. class has no virtual functions or (virtual) base classes.
  15278. - Library-wide
  15279. - [EqualityComparable](https://en.cppreference.com/w/cpp/named_req/EqualityComparable):
  15280. JSON values can be compared with `==`, see @ref
  15281. operator==(const_reference,const_reference).
  15282. - [LessThanComparable](https://en.cppreference.com/w/cpp/named_req/LessThanComparable):
  15283. JSON values can be compared with `<`, see @ref
  15284. operator<(const_reference,const_reference).
  15285. - [Swappable](https://en.cppreference.com/w/cpp/named_req/Swappable):
  15286. Any JSON lvalue or rvalue of can be swapped with any lvalue or rvalue of
  15287. other compatible types, using unqualified function call @ref swap().
  15288. - [NullablePointer](https://en.cppreference.com/w/cpp/named_req/NullablePointer):
  15289. JSON values can be compared against `std::nullptr_t` objects which are used
  15290. to model the `null` value.
  15291. - Container
  15292. - [Container](https://en.cppreference.com/w/cpp/named_req/Container):
  15293. JSON values can be used like STL containers and provide iterator access.
  15294. - [ReversibleContainer](https://en.cppreference.com/w/cpp/named_req/ReversibleContainer);
  15295. JSON values can be used like STL containers and provide reverse iterator
  15296. access.
  15297. @invariant The member variables @a m_value and @a m_type have the following
  15298. relationship:
  15299. - If `m_type == value_t::object`, then `m_value.object != nullptr`.
  15300. - If `m_type == value_t::array`, then `m_value.array != nullptr`.
  15301. - If `m_type == value_t::string`, then `m_value.string != nullptr`.
  15302. The invariants are checked by member function assert_invariant().
  15303. @internal
  15304. @note ObjectType trick from https://stackoverflow.com/a/9860911
  15305. @endinternal
  15306. @see [RFC 8259: The JavaScript Object Notation (JSON) Data Interchange
  15307. Format](https://tools.ietf.org/html/rfc8259)
  15308. @since version 1.0.0
  15309. @nosubgrouping
  15310. */
  15311. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  15312. class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions)
  15313. {
  15314. private:
  15315. template<detail::value_t> friend struct detail::external_constructor;
  15316. friend ::nlohmann::json_pointer<basic_json>;
  15317. template<typename BasicJsonType, typename InputType>
  15318. friend class ::nlohmann::detail::parser;
  15319. friend ::nlohmann::detail::serializer<basic_json>;
  15320. template<typename BasicJsonType>
  15321. friend class ::nlohmann::detail::iter_impl;
  15322. template<typename BasicJsonType, typename CharType>
  15323. friend class ::nlohmann::detail::binary_writer;
  15324. template<typename BasicJsonType, typename InputType, typename SAX>
  15325. friend class ::nlohmann::detail::binary_reader;
  15326. template<typename BasicJsonType>
  15327. friend class ::nlohmann::detail::json_sax_dom_parser;
  15328. template<typename BasicJsonType>
  15329. friend class ::nlohmann::detail::json_sax_dom_callback_parser;
  15330. friend class ::nlohmann::detail::exception;
  15331. /// workaround type for MSVC
  15332. using basic_json_t = NLOHMANN_BASIC_JSON_TPL;
  15333. JSON_PRIVATE_UNLESS_TESTED:
  15334. // convenience aliases for types residing in namespace detail;
  15335. using lexer = ::nlohmann::detail::lexer_base<basic_json>;
  15336. template<typename InputAdapterType>
  15337. static ::nlohmann::detail::parser<basic_json, InputAdapterType> parser(
  15338. InputAdapterType adapter,
  15339. detail::parser_callback_t<basic_json>cb = nullptr,
  15340. const bool allow_exceptions = true,
  15341. const bool ignore_comments = false
  15342. )
  15343. {
  15344. return ::nlohmann::detail::parser<basic_json, InputAdapterType>(std::move(adapter),
  15345. std::move(cb), allow_exceptions, ignore_comments);
  15346. }
  15347. private:
  15348. using primitive_iterator_t = ::nlohmann::detail::primitive_iterator_t;
  15349. template<typename BasicJsonType>
  15350. using internal_iterator = ::nlohmann::detail::internal_iterator<BasicJsonType>;
  15351. template<typename BasicJsonType>
  15352. using iter_impl = ::nlohmann::detail::iter_impl<BasicJsonType>;
  15353. template<typename Iterator>
  15354. using iteration_proxy = ::nlohmann::detail::iteration_proxy<Iterator>;
  15355. template<typename Base> using json_reverse_iterator = ::nlohmann::detail::json_reverse_iterator<Base>;
  15356. template<typename CharType>
  15357. using output_adapter_t = ::nlohmann::detail::output_adapter_t<CharType>;
  15358. template<typename InputType>
  15359. using binary_reader = ::nlohmann::detail::binary_reader<basic_json, InputType>;
  15360. template<typename CharType> using binary_writer = ::nlohmann::detail::binary_writer<basic_json, CharType>;
  15361. JSON_PRIVATE_UNLESS_TESTED:
  15362. using serializer = ::nlohmann::detail::serializer<basic_json>;
  15363. public:
  15364. using value_t = detail::value_t;
  15365. /// JSON Pointer, see @ref nlohmann::json_pointer
  15366. using json_pointer = ::nlohmann::json_pointer<basic_json>;
  15367. template<typename T, typename SFINAE>
  15368. using json_serializer = JSONSerializer<T, SFINAE>;
  15369. /// how to treat decoding errors
  15370. using error_handler_t = detail::error_handler_t;
  15371. /// how to treat CBOR tags
  15372. using cbor_tag_handler_t = detail::cbor_tag_handler_t;
  15373. /// helper type for initializer lists of basic_json values
  15374. using initializer_list_t = std::initializer_list<detail::json_ref<basic_json>>;
  15375. using input_format_t = detail::input_format_t;
  15376. /// SAX interface type, see @ref nlohmann::json_sax
  15377. using json_sax_t = json_sax<basic_json>;
  15378. ////////////////
  15379. // exceptions //
  15380. ////////////////
  15381. /// @name exceptions
  15382. /// Classes to implement user-defined exceptions.
  15383. /// @{
  15384. /// @copydoc detail::exception
  15385. using exception = detail::exception;
  15386. /// @copydoc detail::parse_error
  15387. using parse_error = detail::parse_error;
  15388. /// @copydoc detail::invalid_iterator
  15389. using invalid_iterator = detail::invalid_iterator;
  15390. /// @copydoc detail::type_error
  15391. using type_error = detail::type_error;
  15392. /// @copydoc detail::out_of_range
  15393. using out_of_range = detail::out_of_range;
  15394. /// @copydoc detail::other_error
  15395. using other_error = detail::other_error;
  15396. /// @}
  15397. /////////////////////
  15398. // container types //
  15399. /////////////////////
  15400. /// @name container types
  15401. /// The canonic container types to use @ref basic_json like any other STL
  15402. /// container.
  15403. /// @{
  15404. /// the type of elements in a basic_json container
  15405. using value_type = basic_json;
  15406. /// the type of an element reference
  15407. using reference = value_type&;
  15408. /// the type of an element const reference
  15409. using const_reference = const value_type&;
  15410. /// a type to represent differences between iterators
  15411. using difference_type = std::ptrdiff_t;
  15412. /// a type to represent container sizes
  15413. using size_type = std::size_t;
  15414. /// the allocator type
  15415. using allocator_type = AllocatorType<basic_json>;
  15416. /// the type of an element pointer
  15417. using pointer = typename std::allocator_traits<allocator_type>::pointer;
  15418. /// the type of an element const pointer
  15419. using const_pointer = typename std::allocator_traits<allocator_type>::const_pointer;
  15420. /// an iterator for a basic_json container
  15421. using iterator = iter_impl<basic_json>;
  15422. /// a const iterator for a basic_json container
  15423. using const_iterator = iter_impl<const basic_json>;
  15424. /// a reverse iterator for a basic_json container
  15425. using reverse_iterator = json_reverse_iterator<typename basic_json::iterator>;
  15426. /// a const reverse iterator for a basic_json container
  15427. using const_reverse_iterator = json_reverse_iterator<typename basic_json::const_iterator>;
  15428. /// @}
  15429. /*!
  15430. @brief returns the allocator associated with the container
  15431. */
  15432. static allocator_type get_allocator()
  15433. {
  15434. return allocator_type();
  15435. }
  15436. /*!
  15437. @brief returns version information on the library
  15438. This function returns a JSON object with information about the library,
  15439. including the version number and information on the platform and compiler.
  15440. @return JSON object holding version information
  15441. key | description
  15442. ----------- | ---------------
  15443. `compiler` | Information on the used compiler. It is an object with the following keys: `c++` (the used C++ standard), `family` (the compiler family; possible values are `clang`, `icc`, `gcc`, `ilecpp`, `msvc`, `pgcpp`, `sunpro`, and `unknown`), and `version` (the compiler version).
  15444. `copyright` | The copyright line for the library as string.
  15445. `name` | The name of the library as string.
  15446. `platform` | The used platform as string. Possible values are `win32`, `linux`, `apple`, `unix`, and `unknown`.
  15447. `url` | The URL of the project as string.
  15448. `version` | The version of the library. It is an object with the following keys: `major`, `minor`, and `patch` as defined by [Semantic Versioning](http://semver.org), and `string` (the version string).
  15449. @liveexample{The following code shows an example output of the `meta()`
  15450. function.,meta}
  15451. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  15452. changes to any JSON value.
  15453. @complexity Constant.
  15454. @since 2.1.0
  15455. */
  15456. JSON_HEDLEY_WARN_UNUSED_RESULT
  15457. static basic_json meta()
  15458. {
  15459. basic_json result;
  15460. result["copyright"] = "(C) 2013-2021 Niels Lohmann";
  15461. result["name"] = "JSON for Modern C++";
  15462. result["url"] = "https://github.com/nlohmann/json";
  15463. result["version"]["string"] =
  15464. std::to_string(NLOHMANN_JSON_VERSION_MAJOR) + "." +
  15465. std::to_string(NLOHMANN_JSON_VERSION_MINOR) + "." +
  15466. std::to_string(NLOHMANN_JSON_VERSION_PATCH);
  15467. result["version"]["major"] = NLOHMANN_JSON_VERSION_MAJOR;
  15468. result["version"]["minor"] = NLOHMANN_JSON_VERSION_MINOR;
  15469. result["version"]["patch"] = NLOHMANN_JSON_VERSION_PATCH;
  15470. #ifdef _WIN32
  15471. result["platform"] = "win32";
  15472. #elif defined __linux__
  15473. result["platform"] = "linux";
  15474. #elif defined __APPLE__
  15475. result["platform"] = "apple";
  15476. #elif defined __unix__
  15477. result["platform"] = "unix";
  15478. #else
  15479. result["platform"] = "unknown";
  15480. #endif
  15481. #if defined(__ICC) || defined(__INTEL_COMPILER)
  15482. result["compiler"] = {{"family", "icc"}, {"version", __INTEL_COMPILER}};
  15483. #elif defined(__clang__)
  15484. result["compiler"] = {{"family", "clang"}, {"version", __clang_version__}};
  15485. #elif defined(__GNUC__) || defined(__GNUG__)
  15486. result["compiler"] = {{"family", "gcc"}, {"version", std::to_string(__GNUC__) + "." + std::to_string(__GNUC_MINOR__) + "." + std::to_string(__GNUC_PATCHLEVEL__)}};
  15487. #elif defined(__HP_cc) || defined(__HP_aCC)
  15488. result["compiler"] = "hp"
  15489. #elif defined(__IBMCPP__)
  15490. result["compiler"] = {{"family", "ilecpp"}, {"version", __IBMCPP__}};
  15491. #elif defined(_MSC_VER)
  15492. result["compiler"] = {{"family", "msvc"}, {"version", _MSC_VER}};
  15493. #elif defined(__PGI)
  15494. result["compiler"] = {{"family", "pgcpp"}, {"version", __PGI}};
  15495. #elif defined(__SUNPRO_CC)
  15496. result["compiler"] = {{"family", "sunpro"}, {"version", __SUNPRO_CC}};
  15497. #else
  15498. result["compiler"] = {{"family", "unknown"}, {"version", "unknown"}};
  15499. #endif
  15500. #ifdef __cplusplus
  15501. result["compiler"]["c++"] = std::to_string(__cplusplus);
  15502. #else
  15503. result["compiler"]["c++"] = "unknown";
  15504. #endif
  15505. return result;
  15506. }
  15507. ///////////////////////////
  15508. // JSON value data types //
  15509. ///////////////////////////
  15510. /// @name JSON value data types
  15511. /// The data types to store a JSON value. These types are derived from
  15512. /// the template arguments passed to class @ref basic_json.
  15513. /// @{
  15514. #if defined(JSON_HAS_CPP_14)
  15515. // Use transparent comparator if possible, combined with perfect forwarding
  15516. // on find() and count() calls prevents unnecessary string construction.
  15517. using object_comparator_t = std::less<>;
  15518. #else
  15519. using object_comparator_t = std::less<StringType>;
  15520. #endif
  15521. /*!
  15522. @brief a type for an object
  15523. [RFC 8259](https://tools.ietf.org/html/rfc8259) describes JSON objects as follows:
  15524. > An object is an unordered collection of zero or more name/value pairs,
  15525. > where a name is a string and a value is a string, number, boolean, null,
  15526. > object, or array.
  15527. To store objects in C++, a type is defined by the template parameters
  15528. described below.
  15529. @tparam ObjectType the container to store objects (e.g., `std::map` or
  15530. `std::unordered_map`)
  15531. @tparam StringType the type of the keys or names (e.g., `std::string`).
  15532. The comparison function `std::less<StringType>` is used to order elements
  15533. inside the container.
  15534. @tparam AllocatorType the allocator to use for objects (e.g.,
  15535. `std::allocator`)
  15536. #### Default type
  15537. With the default values for @a ObjectType (`std::map`), @a StringType
  15538. (`std::string`), and @a AllocatorType (`std::allocator`), the default
  15539. value for @a object_t is:
  15540. @code {.cpp}
  15541. std::map<
  15542. std::string, // key_type
  15543. basic_json, // value_type
  15544. std::less<std::string>, // key_compare
  15545. std::allocator<std::pair<const std::string, basic_json>> // allocator_type
  15546. >
  15547. @endcode
  15548. #### Behavior
  15549. The choice of @a object_t influences the behavior of the JSON class. With
  15550. the default type, objects have the following behavior:
  15551. - When all names are unique, objects will be interoperable in the sense
  15552. that all software implementations receiving that object will agree on
  15553. the name-value mappings.
  15554. - When the names within an object are not unique, it is unspecified which
  15555. one of the values for a given key will be chosen. For instance,
  15556. `{"key": 2, "key": 1}` could be equal to either `{"key": 1}` or
  15557. `{"key": 2}`.
  15558. - Internally, name/value pairs are stored in lexicographical order of the
  15559. names. Objects will also be serialized (see @ref dump) in this order.
  15560. For instance, `{"b": 1, "a": 2}` and `{"a": 2, "b": 1}` will be stored
  15561. and serialized as `{"a": 2, "b": 1}`.
  15562. - When comparing objects, the order of the name/value pairs is irrelevant.
  15563. This makes objects interoperable in the sense that they will not be
  15564. affected by these differences. For instance, `{"b": 1, "a": 2}` and
  15565. `{"a": 2, "b": 1}` will be treated as equal.
  15566. #### Limits
  15567. [RFC 8259](https://tools.ietf.org/html/rfc8259) specifies:
  15568. > An implementation may set limits on the maximum depth of nesting.
  15569. In this class, the object's limit of nesting is not explicitly constrained.
  15570. However, a maximum depth of nesting may be introduced by the compiler or
  15571. runtime environment. A theoretical limit can be queried by calling the
  15572. @ref max_size function of a JSON object.
  15573. #### Storage
  15574. Objects are stored as pointers in a @ref basic_json type. That is, for any
  15575. access to object values, a pointer of type `object_t*` must be
  15576. dereferenced.
  15577. @sa see @ref array_t -- type for an array value
  15578. @since version 1.0.0
  15579. @note The order name/value pairs are added to the object is *not*
  15580. preserved by the library. Therefore, iterating an object may return
  15581. name/value pairs in a different order than they were originally stored. In
  15582. fact, keys will be traversed in alphabetical order as `std::map` with
  15583. `std::less` is used by default. Please note this behavior conforms to [RFC
  15584. 8259](https://tools.ietf.org/html/rfc8259), because any order implements the
  15585. specified "unordered" nature of JSON objects.
  15586. */
  15587. using object_t = ObjectType<StringType,
  15588. basic_json,
  15589. object_comparator_t,
  15590. AllocatorType<std::pair<const StringType,
  15591. basic_json>>>;
  15592. /*!
  15593. @brief a type for an array
  15594. [RFC 8259](https://tools.ietf.org/html/rfc8259) describes JSON arrays as follows:
  15595. > An array is an ordered sequence of zero or more values.
  15596. To store objects in C++, a type is defined by the template parameters
  15597. explained below.
  15598. @tparam ArrayType container type to store arrays (e.g., `std::vector` or
  15599. `std::list`)
  15600. @tparam AllocatorType allocator to use for arrays (e.g., `std::allocator`)
  15601. #### Default type
  15602. With the default values for @a ArrayType (`std::vector`) and @a
  15603. AllocatorType (`std::allocator`), the default value for @a array_t is:
  15604. @code {.cpp}
  15605. std::vector<
  15606. basic_json, // value_type
  15607. std::allocator<basic_json> // allocator_type
  15608. >
  15609. @endcode
  15610. #### Limits
  15611. [RFC 8259](https://tools.ietf.org/html/rfc8259) specifies:
  15612. > An implementation may set limits on the maximum depth of nesting.
  15613. In this class, the array's limit of nesting is not explicitly constrained.
  15614. However, a maximum depth of nesting may be introduced by the compiler or
  15615. runtime environment. A theoretical limit can be queried by calling the
  15616. @ref max_size function of a JSON array.
  15617. #### Storage
  15618. Arrays are stored as pointers in a @ref basic_json type. That is, for any
  15619. access to array values, a pointer of type `array_t*` must be dereferenced.
  15620. @sa see @ref object_t -- type for an object value
  15621. @since version 1.0.0
  15622. */
  15623. using array_t = ArrayType<basic_json, AllocatorType<basic_json>>;
  15624. /*!
  15625. @brief a type for a string
  15626. [RFC 8259](https://tools.ietf.org/html/rfc8259) describes JSON strings as follows:
  15627. > A string is a sequence of zero or more Unicode characters.
  15628. To store objects in C++, a type is defined by the template parameter
  15629. described below. Unicode values are split by the JSON class into
  15630. byte-sized characters during deserialization.
  15631. @tparam StringType the container to store strings (e.g., `std::string`).
  15632. Note this container is used for keys/names in objects, see @ref object_t.
  15633. #### Default type
  15634. With the default values for @a StringType (`std::string`), the default
  15635. value for @a string_t is:
  15636. @code {.cpp}
  15637. std::string
  15638. @endcode
  15639. #### Encoding
  15640. Strings are stored in UTF-8 encoding. Therefore, functions like
  15641. `std::string::size()` or `std::string::length()` return the number of
  15642. bytes in the string rather than the number of characters or glyphs.
  15643. #### String comparison
  15644. [RFC 8259](https://tools.ietf.org/html/rfc8259) states:
  15645. > Software implementations are typically required to test names of object
  15646. > members for equality. Implementations that transform the textual
  15647. > representation into sequences of Unicode code units and then perform the
  15648. > comparison numerically, code unit by code unit, are interoperable in the
  15649. > sense that implementations will agree in all cases on equality or
  15650. > inequality of two strings. For example, implementations that compare
  15651. > strings with escaped characters unconverted may incorrectly find that
  15652. > `"a\\b"` and `"a\u005Cb"` are not equal.
  15653. This implementation is interoperable as it does compare strings code unit
  15654. by code unit.
  15655. #### Storage
  15656. String values are stored as pointers in a @ref basic_json type. That is,
  15657. for any access to string values, a pointer of type `string_t*` must be
  15658. dereferenced.
  15659. @since version 1.0.0
  15660. */
  15661. using string_t = StringType;
  15662. /*!
  15663. @brief a type for a boolean
  15664. [RFC 8259](https://tools.ietf.org/html/rfc8259) implicitly describes a boolean as a
  15665. type which differentiates the two literals `true` and `false`.
  15666. To store objects in C++, a type is defined by the template parameter @a
  15667. BooleanType which chooses the type to use.
  15668. #### Default type
  15669. With the default values for @a BooleanType (`bool`), the default value for
  15670. @a boolean_t is:
  15671. @code {.cpp}
  15672. bool
  15673. @endcode
  15674. #### Storage
  15675. Boolean values are stored directly inside a @ref basic_json type.
  15676. @since version 1.0.0
  15677. */
  15678. using boolean_t = BooleanType;
  15679. /*!
  15680. @brief a type for a number (integer)
  15681. [RFC 8259](https://tools.ietf.org/html/rfc8259) describes numbers as follows:
  15682. > The representation of numbers is similar to that used in most
  15683. > programming languages. A number is represented in base 10 using decimal
  15684. > digits. It contains an integer component that may be prefixed with an
  15685. > optional minus sign, which may be followed by a fraction part and/or an
  15686. > exponent part. Leading zeros are not allowed. (...) Numeric values that
  15687. > cannot be represented in the grammar below (such as Infinity and NaN)
  15688. > are not permitted.
  15689. This description includes both integer and floating-point numbers.
  15690. However, C++ allows more precise storage if it is known whether the number
  15691. is a signed integer, an unsigned integer or a floating-point number.
  15692. Therefore, three different types, @ref number_integer_t, @ref
  15693. number_unsigned_t and @ref number_float_t are used.
  15694. To store integer numbers in C++, a type is defined by the template
  15695. parameter @a NumberIntegerType which chooses the type to use.
  15696. #### Default type
  15697. With the default values for @a NumberIntegerType (`int64_t`), the default
  15698. value for @a number_integer_t is:
  15699. @code {.cpp}
  15700. int64_t
  15701. @endcode
  15702. #### Default behavior
  15703. - The restrictions about leading zeros is not enforced in C++. Instead,
  15704. leading zeros in integer literals lead to an interpretation as octal
  15705. number. Internally, the value will be stored as decimal number. For
  15706. instance, the C++ integer literal `010` will be serialized to `8`.
  15707. During deserialization, leading zeros yield an error.
  15708. - Not-a-number (NaN) values will be serialized to `null`.
  15709. #### Limits
  15710. [RFC 8259](https://tools.ietf.org/html/rfc8259) specifies:
  15711. > An implementation may set limits on the range and precision of numbers.
  15712. When the default type is used, the maximal integer number that can be
  15713. stored is `9223372036854775807` (INT64_MAX) and the minimal integer number
  15714. that can be stored is `-9223372036854775808` (INT64_MIN). Integer numbers
  15715. that are out of range will yield over/underflow when used in a
  15716. constructor. During deserialization, too large or small integer numbers
  15717. will be automatically be stored as @ref number_unsigned_t or @ref
  15718. number_float_t.
  15719. [RFC 8259](https://tools.ietf.org/html/rfc8259) further states:
  15720. > Note that when such software is used, numbers that are integers and are
  15721. > in the range \f$[-2^{53}+1, 2^{53}-1]\f$ are interoperable in the sense
  15722. > that implementations will agree exactly on their numeric values.
  15723. As this range is a subrange of the exactly supported range [INT64_MIN,
  15724. INT64_MAX], this class's integer type is interoperable.
  15725. #### Storage
  15726. Integer number values are stored directly inside a @ref basic_json type.
  15727. @sa see @ref number_float_t -- type for number values (floating-point)
  15728. @sa see @ref number_unsigned_t -- type for number values (unsigned integer)
  15729. @since version 1.0.0
  15730. */
  15731. using number_integer_t = NumberIntegerType;
  15732. /*!
  15733. @brief a type for a number (unsigned)
  15734. [RFC 8259](https://tools.ietf.org/html/rfc8259) describes numbers as follows:
  15735. > The representation of numbers is similar to that used in most
  15736. > programming languages. A number is represented in base 10 using decimal
  15737. > digits. It contains an integer component that may be prefixed with an
  15738. > optional minus sign, which may be followed by a fraction part and/or an
  15739. > exponent part. Leading zeros are not allowed. (...) Numeric values that
  15740. > cannot be represented in the grammar below (such as Infinity and NaN)
  15741. > are not permitted.
  15742. This description includes both integer and floating-point numbers.
  15743. However, C++ allows more precise storage if it is known whether the number
  15744. is a signed integer, an unsigned integer or a floating-point number.
  15745. Therefore, three different types, @ref number_integer_t, @ref
  15746. number_unsigned_t and @ref number_float_t are used.
  15747. To store unsigned integer numbers in C++, a type is defined by the
  15748. template parameter @a NumberUnsignedType which chooses the type to use.
  15749. #### Default type
  15750. With the default values for @a NumberUnsignedType (`uint64_t`), the
  15751. default value for @a number_unsigned_t is:
  15752. @code {.cpp}
  15753. uint64_t
  15754. @endcode
  15755. #### Default behavior
  15756. - The restrictions about leading zeros is not enforced in C++. Instead,
  15757. leading zeros in integer literals lead to an interpretation as octal
  15758. number. Internally, the value will be stored as decimal number. For
  15759. instance, the C++ integer literal `010` will be serialized to `8`.
  15760. During deserialization, leading zeros yield an error.
  15761. - Not-a-number (NaN) values will be serialized to `null`.
  15762. #### Limits
  15763. [RFC 8259](https://tools.ietf.org/html/rfc8259) specifies:
  15764. > An implementation may set limits on the range and precision of numbers.
  15765. When the default type is used, the maximal integer number that can be
  15766. stored is `18446744073709551615` (UINT64_MAX) and the minimal integer
  15767. number that can be stored is `0`. Integer numbers that are out of range
  15768. will yield over/underflow when used in a constructor. During
  15769. deserialization, too large or small integer numbers will be automatically
  15770. be stored as @ref number_integer_t or @ref number_float_t.
  15771. [RFC 8259](https://tools.ietf.org/html/rfc8259) further states:
  15772. > Note that when such software is used, numbers that are integers and are
  15773. > in the range \f$[-2^{53}+1, 2^{53}-1]\f$ are interoperable in the sense
  15774. > that implementations will agree exactly on their numeric values.
  15775. As this range is a subrange (when considered in conjunction with the
  15776. number_integer_t type) of the exactly supported range [0, UINT64_MAX],
  15777. this class's integer type is interoperable.
  15778. #### Storage
  15779. Integer number values are stored directly inside a @ref basic_json type.
  15780. @sa see @ref number_float_t -- type for number values (floating-point)
  15781. @sa see @ref number_integer_t -- type for number values (integer)
  15782. @since version 2.0.0
  15783. */
  15784. using number_unsigned_t = NumberUnsignedType;
  15785. /*!
  15786. @brief a type for a number (floating-point)
  15787. [RFC 8259](https://tools.ietf.org/html/rfc8259) describes numbers as follows:
  15788. > The representation of numbers is similar to that used in most
  15789. > programming languages. A number is represented in base 10 using decimal
  15790. > digits. It contains an integer component that may be prefixed with an
  15791. > optional minus sign, which may be followed by a fraction part and/or an
  15792. > exponent part. Leading zeros are not allowed. (...) Numeric values that
  15793. > cannot be represented in the grammar below (such as Infinity and NaN)
  15794. > are not permitted.
  15795. This description includes both integer and floating-point numbers.
  15796. However, C++ allows more precise storage if it is known whether the number
  15797. is a signed integer, an unsigned integer or a floating-point number.
  15798. Therefore, three different types, @ref number_integer_t, @ref
  15799. number_unsigned_t and @ref number_float_t are used.
  15800. To store floating-point numbers in C++, a type is defined by the template
  15801. parameter @a NumberFloatType which chooses the type to use.
  15802. #### Default type
  15803. With the default values for @a NumberFloatType (`double`), the default
  15804. value for @a number_float_t is:
  15805. @code {.cpp}
  15806. double
  15807. @endcode
  15808. #### Default behavior
  15809. - The restrictions about leading zeros is not enforced in C++. Instead,
  15810. leading zeros in floating-point literals will be ignored. Internally,
  15811. the value will be stored as decimal number. For instance, the C++
  15812. floating-point literal `01.2` will be serialized to `1.2`. During
  15813. deserialization, leading zeros yield an error.
  15814. - Not-a-number (NaN) values will be serialized to `null`.
  15815. #### Limits
  15816. [RFC 8259](https://tools.ietf.org/html/rfc8259) states:
  15817. > This specification allows implementations to set limits on the range and
  15818. > precision of numbers accepted. Since software that implements IEEE
  15819. > 754-2008 binary64 (double precision) numbers is generally available and
  15820. > widely used, good interoperability can be achieved by implementations
  15821. > that expect no more precision or range than these provide, in the sense
  15822. > that implementations will approximate JSON numbers within the expected
  15823. > precision.
  15824. This implementation does exactly follow this approach, as it uses double
  15825. precision floating-point numbers. Note values smaller than
  15826. `-1.79769313486232e+308` and values greater than `1.79769313486232e+308`
  15827. will be stored as NaN internally and be serialized to `null`.
  15828. #### Storage
  15829. Floating-point number values are stored directly inside a @ref basic_json
  15830. type.
  15831. @sa see @ref number_integer_t -- type for number values (integer)
  15832. @sa see @ref number_unsigned_t -- type for number values (unsigned integer)
  15833. @since version 1.0.0
  15834. */
  15835. using number_float_t = NumberFloatType;
  15836. /*!
  15837. @brief a type for a packed binary type
  15838. This type is a type designed to carry binary data that appears in various
  15839. serialized formats, such as CBOR's Major Type 2, MessagePack's bin, and
  15840. BSON's generic binary subtype. This type is NOT a part of standard JSON and
  15841. exists solely for compatibility with these binary types. As such, it is
  15842. simply defined as an ordered sequence of zero or more byte values.
  15843. Additionally, as an implementation detail, the subtype of the binary data is
  15844. carried around as a `std::uint8_t`, which is compatible with both of the
  15845. binary data formats that use binary subtyping, (though the specific
  15846. numbering is incompatible with each other, and it is up to the user to
  15847. translate between them).
  15848. [CBOR's RFC 7049](https://tools.ietf.org/html/rfc7049) describes this type
  15849. as:
  15850. > Major type 2: a byte string. The string's length in bytes is represented
  15851. > following the rules for positive integers (major type 0).
  15852. [MessagePack's documentation on the bin type
  15853. family](https://github.com/msgpack/msgpack/blob/master/spec.md#bin-format-family)
  15854. describes this type as:
  15855. > Bin format family stores an byte array in 2, 3, or 5 bytes of extra bytes
  15856. > in addition to the size of the byte array.
  15857. [BSON's specifications](http://bsonspec.org/spec.html) describe several
  15858. binary types; however, this type is intended to represent the generic binary
  15859. type which has the description:
  15860. > Generic binary subtype - This is the most commonly used binary subtype and
  15861. > should be the 'default' for drivers and tools.
  15862. None of these impose any limitations on the internal representation other
  15863. than the basic unit of storage be some type of array whose parts are
  15864. decomposable into bytes.
  15865. The default representation of this binary format is a
  15866. `std::vector<std::uint8_t>`, which is a very common way to represent a byte
  15867. array in modern C++.
  15868. #### Default type
  15869. The default values for @a BinaryType is `std::vector<std::uint8_t>`
  15870. #### Storage
  15871. Binary Arrays are stored as pointers in a @ref basic_json type. That is,
  15872. for any access to array values, a pointer of the type `binary_t*` must be
  15873. dereferenced.
  15874. #### Notes on subtypes
  15875. - CBOR
  15876. - Binary values are represented as byte strings. Subtypes are serialized
  15877. as tagged values.
  15878. - MessagePack
  15879. - If a subtype is given and the binary array contains exactly 1, 2, 4, 8,
  15880. or 16 elements, the fixext family (fixext1, fixext2, fixext4, fixext8)
  15881. is used. For other sizes, the ext family (ext8, ext16, ext32) is used.
  15882. The subtype is then added as singed 8-bit integer.
  15883. - If no subtype is given, the bin family (bin8, bin16, bin32) is used.
  15884. - BSON
  15885. - If a subtype is given, it is used and added as unsigned 8-bit integer.
  15886. - If no subtype is given, the generic binary subtype 0x00 is used.
  15887. @sa see @ref binary -- create a binary array
  15888. @since version 3.8.0
  15889. */
  15890. using binary_t = nlohmann::byte_container_with_subtype<BinaryType>;
  15891. /// @}
  15892. private:
  15893. /// helper for exception-safe object creation
  15894. template<typename T, typename... Args>
  15895. JSON_HEDLEY_RETURNS_NON_NULL
  15896. static T* create(Args&& ... args)
  15897. {
  15898. AllocatorType<T> alloc;
  15899. using AllocatorTraits = std::allocator_traits<AllocatorType<T>>;
  15900. auto deleter = [&](T * obj)
  15901. {
  15902. AllocatorTraits::deallocate(alloc, obj, 1);
  15903. };
  15904. std::unique_ptr<T, decltype(deleter)> obj(AllocatorTraits::allocate(alloc, 1), deleter);
  15905. AllocatorTraits::construct(alloc, obj.get(), std::forward<Args>(args)...);
  15906. JSON_ASSERT(obj != nullptr);
  15907. return obj.release();
  15908. }
  15909. ////////////////////////
  15910. // JSON value storage //
  15911. ////////////////////////
  15912. JSON_PRIVATE_UNLESS_TESTED:
  15913. /*!
  15914. @brief a JSON value
  15915. The actual storage for a JSON value of the @ref basic_json class. This
  15916. union combines the different storage types for the JSON value types
  15917. defined in @ref value_t.
  15918. JSON type | value_t type | used type
  15919. --------- | --------------- | ------------------------
  15920. object | object | pointer to @ref object_t
  15921. array | array | pointer to @ref array_t
  15922. string | string | pointer to @ref string_t
  15923. boolean | boolean | @ref boolean_t
  15924. number | number_integer | @ref number_integer_t
  15925. number | number_unsigned | @ref number_unsigned_t
  15926. number | number_float | @ref number_float_t
  15927. binary | binary | pointer to @ref binary_t
  15928. null | null | *no value is stored*
  15929. @note Variable-length types (objects, arrays, and strings) are stored as
  15930. pointers. The size of the union should not exceed 64 bits if the default
  15931. value types are used.
  15932. @since version 1.0.0
  15933. */
  15934. union json_value
  15935. {
  15936. /// object (stored with pointer to save storage)
  15937. object_t* object;
  15938. /// array (stored with pointer to save storage)
  15939. array_t* array;
  15940. /// string (stored with pointer to save storage)
  15941. string_t* string;
  15942. /// binary (stored with pointer to save storage)
  15943. binary_t* binary;
  15944. /// boolean
  15945. boolean_t boolean;
  15946. /// number (integer)
  15947. number_integer_t number_integer;
  15948. /// number (unsigned integer)
  15949. number_unsigned_t number_unsigned;
  15950. /// number (floating-point)
  15951. number_float_t number_float;
  15952. /// default constructor (for null values)
  15953. json_value() = default;
  15954. /// constructor for booleans
  15955. json_value(boolean_t v) noexcept : boolean(v) {}
  15956. /// constructor for numbers (integer)
  15957. json_value(number_integer_t v) noexcept : number_integer(v) {}
  15958. /// constructor for numbers (unsigned)
  15959. json_value(number_unsigned_t v) noexcept : number_unsigned(v) {}
  15960. /// constructor for numbers (floating-point)
  15961. json_value(number_float_t v) noexcept : number_float(v) {}
  15962. /// constructor for empty values of a given type
  15963. json_value(value_t t)
  15964. {
  15965. switch (t)
  15966. {
  15967. case value_t::object:
  15968. {
  15969. object = create<object_t>();
  15970. break;
  15971. }
  15972. case value_t::array:
  15973. {
  15974. array = create<array_t>();
  15975. break;
  15976. }
  15977. case value_t::string:
  15978. {
  15979. string = create<string_t>("");
  15980. break;
  15981. }
  15982. case value_t::binary:
  15983. {
  15984. binary = create<binary_t>();
  15985. break;
  15986. }
  15987. case value_t::boolean:
  15988. {
  15989. boolean = boolean_t(false);
  15990. break;
  15991. }
  15992. case value_t::number_integer:
  15993. {
  15994. number_integer = number_integer_t(0);
  15995. break;
  15996. }
  15997. case value_t::number_unsigned:
  15998. {
  15999. number_unsigned = number_unsigned_t(0);
  16000. break;
  16001. }
  16002. case value_t::number_float:
  16003. {
  16004. number_float = number_float_t(0.0);
  16005. break;
  16006. }
  16007. case value_t::null:
  16008. {
  16009. object = nullptr; // silence warning, see #821
  16010. break;
  16011. }
  16012. case value_t::discarded:
  16013. default:
  16014. {
  16015. object = nullptr; // silence warning, see #821
  16016. if (JSON_HEDLEY_UNLIKELY(t == value_t::null))
  16017. {
  16018. JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.10.2", basic_json())); // LCOV_EXCL_LINE
  16019. }
  16020. break;
  16021. }
  16022. }
  16023. }
  16024. /// constructor for strings
  16025. json_value(const string_t& value)
  16026. {
  16027. string = create<string_t>(value);
  16028. }
  16029. /// constructor for rvalue strings
  16030. json_value(string_t&& value)
  16031. {
  16032. string = create<string_t>(std::move(value));
  16033. }
  16034. /// constructor for objects
  16035. json_value(const object_t& value)
  16036. {
  16037. object = create<object_t>(value);
  16038. }
  16039. /// constructor for rvalue objects
  16040. json_value(object_t&& value)
  16041. {
  16042. object = create<object_t>(std::move(value));
  16043. }
  16044. /// constructor for arrays
  16045. json_value(const array_t& value)
  16046. {
  16047. array = create<array_t>(value);
  16048. }
  16049. /// constructor for rvalue arrays
  16050. json_value(array_t&& value)
  16051. {
  16052. array = create<array_t>(std::move(value));
  16053. }
  16054. /// constructor for binary arrays
  16055. json_value(const typename binary_t::container_type& value)
  16056. {
  16057. binary = create<binary_t>(value);
  16058. }
  16059. /// constructor for rvalue binary arrays
  16060. json_value(typename binary_t::container_type&& value)
  16061. {
  16062. binary = create<binary_t>(std::move(value));
  16063. }
  16064. /// constructor for binary arrays (internal type)
  16065. json_value(const binary_t& value)
  16066. {
  16067. binary = create<binary_t>(value);
  16068. }
  16069. /// constructor for rvalue binary arrays (internal type)
  16070. json_value(binary_t&& value)
  16071. {
  16072. binary = create<binary_t>(std::move(value));
  16073. }
  16074. void destroy(value_t t)
  16075. {
  16076. if (t == value_t::array || t == value_t::object)
  16077. {
  16078. // flatten the current json_value to a heap-allocated stack
  16079. std::vector<basic_json> stack;
  16080. // move the top-level items to stack
  16081. if (t == value_t::array)
  16082. {
  16083. stack.reserve(array->size());
  16084. std::move(array->begin(), array->end(), std::back_inserter(stack));
  16085. }
  16086. else
  16087. {
  16088. stack.reserve(object->size());
  16089. for (auto&& it : *object)
  16090. {
  16091. stack.push_back(std::move(it.second));
  16092. }
  16093. }
  16094. while (!stack.empty())
  16095. {
  16096. // move the last item to local variable to be processed
  16097. basic_json current_item(std::move(stack.back()));
  16098. stack.pop_back();
  16099. // if current_item is array/object, move
  16100. // its children to the stack to be processed later
  16101. if (current_item.is_array())
  16102. {
  16103. std::move(current_item.m_value.array->begin(), current_item.m_value.array->end(), std::back_inserter(stack));
  16104. current_item.m_value.array->clear();
  16105. }
  16106. else if (current_item.is_object())
  16107. {
  16108. for (auto&& it : *current_item.m_value.object)
  16109. {
  16110. stack.push_back(std::move(it.second));
  16111. }
  16112. current_item.m_value.object->clear();
  16113. }
  16114. // it's now safe that current_item get destructed
  16115. // since it doesn't have any children
  16116. }
  16117. }
  16118. switch (t)
  16119. {
  16120. case value_t::object:
  16121. {
  16122. AllocatorType<object_t> alloc;
  16123. std::allocator_traits<decltype(alloc)>::destroy(alloc, object);
  16124. std::allocator_traits<decltype(alloc)>::deallocate(alloc, object, 1);
  16125. break;
  16126. }
  16127. case value_t::array:
  16128. {
  16129. AllocatorType<array_t> alloc;
  16130. std::allocator_traits<decltype(alloc)>::destroy(alloc, array);
  16131. std::allocator_traits<decltype(alloc)>::deallocate(alloc, array, 1);
  16132. break;
  16133. }
  16134. case value_t::string:
  16135. {
  16136. AllocatorType<string_t> alloc;
  16137. std::allocator_traits<decltype(alloc)>::destroy(alloc, string);
  16138. std::allocator_traits<decltype(alloc)>::deallocate(alloc, string, 1);
  16139. break;
  16140. }
  16141. case value_t::binary:
  16142. {
  16143. AllocatorType<binary_t> alloc;
  16144. std::allocator_traits<decltype(alloc)>::destroy(alloc, binary);
  16145. std::allocator_traits<decltype(alloc)>::deallocate(alloc, binary, 1);
  16146. break;
  16147. }
  16148. case value_t::null:
  16149. case value_t::boolean:
  16150. case value_t::number_integer:
  16151. case value_t::number_unsigned:
  16152. case value_t::number_float:
  16153. case value_t::discarded:
  16154. default:
  16155. {
  16156. break;
  16157. }
  16158. }
  16159. }
  16160. };
  16161. private:
  16162. /*!
  16163. @brief checks the class invariants
  16164. This function asserts the class invariants. It needs to be called at the
  16165. end of every constructor to make sure that created objects respect the
  16166. invariant. Furthermore, it has to be called each time the type of a JSON
  16167. value is changed, because the invariant expresses a relationship between
  16168. @a m_type and @a m_value.
  16169. Furthermore, the parent relation is checked for arrays and objects: If
  16170. @a check_parents true and the value is an array or object, then the
  16171. container's elements must have the current value as parent.
  16172. @param[in] check_parents whether the parent relation should be checked.
  16173. The value is true by default and should only be set to false
  16174. during destruction of objects when the invariant does not
  16175. need to hold.
  16176. */
  16177. void assert_invariant(bool check_parents = true) const noexcept
  16178. {
  16179. JSON_ASSERT(m_type != value_t::object || m_value.object != nullptr);
  16180. JSON_ASSERT(m_type != value_t::array || m_value.array != nullptr);
  16181. JSON_ASSERT(m_type != value_t::string || m_value.string != nullptr);
  16182. JSON_ASSERT(m_type != value_t::binary || m_value.binary != nullptr);
  16183. #if JSON_DIAGNOSTICS
  16184. JSON_TRY
  16185. {
  16186. // cppcheck-suppress assertWithSideEffect
  16187. JSON_ASSERT(!check_parents || !is_structured() || std::all_of(begin(), end(), [this](const basic_json & j)
  16188. {
  16189. return j.m_parent == this;
  16190. }));
  16191. }
  16192. JSON_CATCH(...) {} // LCOV_EXCL_LINE
  16193. #endif
  16194. static_cast<void>(check_parents);
  16195. }
  16196. void set_parents()
  16197. {
  16198. #if JSON_DIAGNOSTICS
  16199. switch (m_type)
  16200. {
  16201. case value_t::array:
  16202. {
  16203. for (auto& element : *m_value.array)
  16204. {
  16205. element.m_parent = this;
  16206. }
  16207. break;
  16208. }
  16209. case value_t::object:
  16210. {
  16211. for (auto& element : *m_value.object)
  16212. {
  16213. element.second.m_parent = this;
  16214. }
  16215. break;
  16216. }
  16217. case value_t::null:
  16218. case value_t::string:
  16219. case value_t::boolean:
  16220. case value_t::number_integer:
  16221. case value_t::number_unsigned:
  16222. case value_t::number_float:
  16223. case value_t::binary:
  16224. case value_t::discarded:
  16225. default:
  16226. break;
  16227. }
  16228. #endif
  16229. }
  16230. iterator set_parents(iterator it, typename iterator::difference_type count)
  16231. {
  16232. #if JSON_DIAGNOSTICS
  16233. for (typename iterator::difference_type i = 0; i < count; ++i)
  16234. {
  16235. (it + i)->m_parent = this;
  16236. }
  16237. #else
  16238. static_cast<void>(count);
  16239. #endif
  16240. return it;
  16241. }
  16242. reference set_parent(reference j, std::size_t old_capacity = std::size_t(-1))
  16243. {
  16244. #if JSON_DIAGNOSTICS
  16245. if (old_capacity != std::size_t(-1))
  16246. {
  16247. // see https://github.com/nlohmann/json/issues/2838
  16248. JSON_ASSERT(type() == value_t::array);
  16249. if (JSON_HEDLEY_UNLIKELY(m_value.array->capacity() != old_capacity))
  16250. {
  16251. // capacity has changed: update all parents
  16252. set_parents();
  16253. return j;
  16254. }
  16255. }
  16256. // ordered_json uses a vector internally, so pointers could have
  16257. // been invalidated; see https://github.com/nlohmann/json/issues/2962
  16258. #ifdef JSON_HEDLEY_MSVC_VERSION
  16259. #pragma warning(push )
  16260. #pragma warning(disable : 4127) // ignore warning to replace if with if constexpr
  16261. #endif
  16262. if (detail::is_ordered_map<object_t>::value)
  16263. {
  16264. set_parents();
  16265. return j;
  16266. }
  16267. #ifdef JSON_HEDLEY_MSVC_VERSION
  16268. #pragma warning( pop )
  16269. #endif
  16270. j.m_parent = this;
  16271. #else
  16272. static_cast<void>(j);
  16273. static_cast<void>(old_capacity);
  16274. #endif
  16275. return j;
  16276. }
  16277. public:
  16278. //////////////////////////
  16279. // JSON parser callback //
  16280. //////////////////////////
  16281. /*!
  16282. @brief parser event types
  16283. The parser callback distinguishes the following events:
  16284. - `object_start`: the parser read `{` and started to process a JSON object
  16285. - `key`: the parser read a key of a value in an object
  16286. - `object_end`: the parser read `}` and finished processing a JSON object
  16287. - `array_start`: the parser read `[` and started to process a JSON array
  16288. - `array_end`: the parser read `]` and finished processing a JSON array
  16289. - `value`: the parser finished reading a JSON value
  16290. @image html callback_events.png "Example when certain parse events are triggered"
  16291. @sa see @ref parser_callback_t for more information and examples
  16292. */
  16293. using parse_event_t = detail::parse_event_t;
  16294. /*!
  16295. @brief per-element parser callback type
  16296. With a parser callback function, the result of parsing a JSON text can be
  16297. influenced. When passed to @ref parse, it is called on certain events
  16298. (passed as @ref parse_event_t via parameter @a event) with a set recursion
  16299. depth @a depth and context JSON value @a parsed. The return value of the
  16300. callback function is a boolean indicating whether the element that emitted
  16301. the callback shall be kept or not.
  16302. We distinguish six scenarios (determined by the event type) in which the
  16303. callback function can be called. The following table describes the values
  16304. of the parameters @a depth, @a event, and @a parsed.
  16305. parameter @a event | description | parameter @a depth | parameter @a parsed
  16306. ------------------ | ----------- | ------------------ | -------------------
  16307. parse_event_t::object_start | the parser read `{` and started to process a JSON object | depth of the parent of the JSON object | a JSON value with type discarded
  16308. parse_event_t::key | the parser read a key of a value in an object | depth of the currently parsed JSON object | a JSON string containing the key
  16309. parse_event_t::object_end | the parser read `}` and finished processing a JSON object | depth of the parent of the JSON object | the parsed JSON object
  16310. parse_event_t::array_start | the parser read `[` and started to process a JSON array | depth of the parent of the JSON array | a JSON value with type discarded
  16311. parse_event_t::array_end | the parser read `]` and finished processing a JSON array | depth of the parent of the JSON array | the parsed JSON array
  16312. parse_event_t::value | the parser finished reading a JSON value | depth of the value | the parsed JSON value
  16313. @image html callback_events.png "Example when certain parse events are triggered"
  16314. Discarding a value (i.e., returning `false`) has different effects
  16315. depending on the context in which function was called:
  16316. - Discarded values in structured types are skipped. That is, the parser
  16317. will behave as if the discarded value was never read.
  16318. - In case a value outside a structured type is skipped, it is replaced
  16319. with `null`. This case happens if the top-level element is skipped.
  16320. @param[in] depth the depth of the recursion during parsing
  16321. @param[in] event an event of type parse_event_t indicating the context in
  16322. the callback function has been called
  16323. @param[in,out] parsed the current intermediate parse result; note that
  16324. writing to this value has no effect for parse_event_t::key events
  16325. @return Whether the JSON value which called the function during parsing
  16326. should be kept (`true`) or not (`false`). In the latter case, it is either
  16327. skipped completely or replaced by an empty discarded object.
  16328. @sa see @ref parse for examples
  16329. @since version 1.0.0
  16330. */
  16331. using parser_callback_t = detail::parser_callback_t<basic_json>;
  16332. //////////////////
  16333. // constructors //
  16334. //////////////////
  16335. /// @name constructors and destructors
  16336. /// Constructors of class @ref basic_json, copy/move constructor, copy
  16337. /// assignment, static functions creating objects, and the destructor.
  16338. /// @{
  16339. /*!
  16340. @brief create an empty value with a given type
  16341. Create an empty JSON value with a given type. The value will be default
  16342. initialized with an empty value which depends on the type:
  16343. Value type | initial value
  16344. ----------- | -------------
  16345. null | `null`
  16346. boolean | `false`
  16347. string | `""`
  16348. number | `0`
  16349. object | `{}`
  16350. array | `[]`
  16351. binary | empty array
  16352. @param[in] v the type of the value to create
  16353. @complexity Constant.
  16354. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  16355. changes to any JSON value.
  16356. @liveexample{The following code shows the constructor for different @ref
  16357. value_t values,basic_json__value_t}
  16358. @sa see @ref clear() -- restores the postcondition of this constructor
  16359. @since version 1.0.0
  16360. */
  16361. basic_json(const value_t v)
  16362. : m_type(v), m_value(v)
  16363. {
  16364. assert_invariant();
  16365. }
  16366. /*!
  16367. @brief create a null object
  16368. Create a `null` JSON value. It either takes a null pointer as parameter
  16369. (explicitly creating `null`) or no parameter (implicitly creating `null`).
  16370. The passed null pointer itself is not read -- it is only used to choose
  16371. the right constructor.
  16372. @complexity Constant.
  16373. @exceptionsafety No-throw guarantee: this constructor never throws
  16374. exceptions.
  16375. @liveexample{The following code shows the constructor with and without a
  16376. null pointer parameter.,basic_json__nullptr_t}
  16377. @since version 1.0.0
  16378. */
  16379. basic_json(std::nullptr_t = nullptr) noexcept
  16380. : basic_json(value_t::null)
  16381. {
  16382. assert_invariant();
  16383. }
  16384. /*!
  16385. @brief create a JSON value
  16386. This is a "catch all" constructor for all compatible JSON types; that is,
  16387. types for which a `to_json()` method exists. The constructor forwards the
  16388. parameter @a val to that method (to `json_serializer<U>::to_json` method
  16389. with `U = uncvref_t<CompatibleType>`, to be exact).
  16390. Template type @a CompatibleType includes, but is not limited to, the
  16391. following types:
  16392. - **arrays**: @ref array_t and all kinds of compatible containers such as
  16393. `std::vector`, `std::deque`, `std::list`, `std::forward_list`,
  16394. `std::array`, `std::valarray`, `std::set`, `std::unordered_set`,
  16395. `std::multiset`, and `std::unordered_multiset` with a `value_type` from
  16396. which a @ref basic_json value can be constructed.
  16397. - **objects**: @ref object_t and all kinds of compatible associative
  16398. containers such as `std::map`, `std::unordered_map`, `std::multimap`,
  16399. and `std::unordered_multimap` with a `key_type` compatible to
  16400. @ref string_t and a `value_type` from which a @ref basic_json value can
  16401. be constructed.
  16402. - **strings**: @ref string_t, string literals, and all compatible string
  16403. containers can be used.
  16404. - **numbers**: @ref number_integer_t, @ref number_unsigned_t,
  16405. @ref number_float_t, and all convertible number types such as `int`,
  16406. `size_t`, `int64_t`, `float` or `double` can be used.
  16407. - **boolean**: @ref boolean_t / `bool` can be used.
  16408. - **binary**: @ref binary_t / `std::vector<std::uint8_t>` may be used,
  16409. unfortunately because string literals cannot be distinguished from binary
  16410. character arrays by the C++ type system, all types compatible with `const
  16411. char*` will be directed to the string constructor instead. This is both
  16412. for backwards compatibility, and due to the fact that a binary type is not
  16413. a standard JSON type.
  16414. See the examples below.
  16415. @tparam CompatibleType a type such that:
  16416. - @a CompatibleType is not derived from `std::istream`,
  16417. - @a CompatibleType is not @ref basic_json (to avoid hijacking copy/move
  16418. constructors),
  16419. - @a CompatibleType is not a different @ref basic_json type (i.e. with different template arguments)
  16420. - @a CompatibleType is not a @ref basic_json nested type (e.g.,
  16421. @ref json_pointer, @ref iterator, etc ...)
  16422. - `json_serializer<U>` has a `to_json(basic_json_t&, CompatibleType&&)` method
  16423. @tparam U = `uncvref_t<CompatibleType>`
  16424. @param[in] val the value to be forwarded to the respective constructor
  16425. @complexity Usually linear in the size of the passed @a val, also
  16426. depending on the implementation of the called `to_json()`
  16427. method.
  16428. @exceptionsafety Depends on the called constructor. For types directly
  16429. supported by the library (i.e., all types for which no `to_json()` function
  16430. was provided), strong guarantee holds: if an exception is thrown, there are
  16431. no changes to any JSON value.
  16432. @liveexample{The following code shows the constructor with several
  16433. compatible types.,basic_json__CompatibleType}
  16434. @since version 2.1.0
  16435. */
  16436. template < typename CompatibleType,
  16437. typename U = detail::uncvref_t<CompatibleType>,
  16438. detail::enable_if_t <
  16439. !detail::is_basic_json<U>::value && detail::is_compatible_type<basic_json_t, U>::value, int > = 0 >
  16440. basic_json(CompatibleType && val) noexcept(noexcept( // NOLINT(bugprone-forwarding-reference-overload,bugprone-exception-escape)
  16441. JSONSerializer<U>::to_json(std::declval<basic_json_t&>(),
  16442. std::forward<CompatibleType>(val))))
  16443. {
  16444. JSONSerializer<U>::to_json(*this, std::forward<CompatibleType>(val));
  16445. set_parents();
  16446. assert_invariant();
  16447. }
  16448. /*!
  16449. @brief create a JSON value from an existing one
  16450. This is a constructor for existing @ref basic_json types.
  16451. It does not hijack copy/move constructors, since the parameter has different
  16452. template arguments than the current ones.
  16453. The constructor tries to convert the internal @ref m_value of the parameter.
  16454. @tparam BasicJsonType a type such that:
  16455. - @a BasicJsonType is a @ref basic_json type.
  16456. - @a BasicJsonType has different template arguments than @ref basic_json_t.
  16457. @param[in] val the @ref basic_json value to be converted.
  16458. @complexity Usually linear in the size of the passed @a val, also
  16459. depending on the implementation of the called `to_json()`
  16460. method.
  16461. @exceptionsafety Depends on the called constructor. For types directly
  16462. supported by the library (i.e., all types for which no `to_json()` function
  16463. was provided), strong guarantee holds: if an exception is thrown, there are
  16464. no changes to any JSON value.
  16465. @since version 3.2.0
  16466. */
  16467. template < typename BasicJsonType,
  16468. detail::enable_if_t <
  16469. detail::is_basic_json<BasicJsonType>::value&& !std::is_same<basic_json, BasicJsonType>::value, int > = 0 >
  16470. basic_json(const BasicJsonType& val)
  16471. {
  16472. using other_boolean_t = typename BasicJsonType::boolean_t;
  16473. using other_number_float_t = typename BasicJsonType::number_float_t;
  16474. using other_number_integer_t = typename BasicJsonType::number_integer_t;
  16475. using other_number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  16476. using other_string_t = typename BasicJsonType::string_t;
  16477. using other_object_t = typename BasicJsonType::object_t;
  16478. using other_array_t = typename BasicJsonType::array_t;
  16479. using other_binary_t = typename BasicJsonType::binary_t;
  16480. switch (val.type())
  16481. {
  16482. case value_t::boolean:
  16483. JSONSerializer<other_boolean_t>::to_json(*this, val.template get<other_boolean_t>());
  16484. break;
  16485. case value_t::number_float:
  16486. JSONSerializer<other_number_float_t>::to_json(*this, val.template get<other_number_float_t>());
  16487. break;
  16488. case value_t::number_integer:
  16489. JSONSerializer<other_number_integer_t>::to_json(*this, val.template get<other_number_integer_t>());
  16490. break;
  16491. case value_t::number_unsigned:
  16492. JSONSerializer<other_number_unsigned_t>::to_json(*this, val.template get<other_number_unsigned_t>());
  16493. break;
  16494. case value_t::string:
  16495. JSONSerializer<other_string_t>::to_json(*this, val.template get_ref<const other_string_t&>());
  16496. break;
  16497. case value_t::object:
  16498. JSONSerializer<other_object_t>::to_json(*this, val.template get_ref<const other_object_t&>());
  16499. break;
  16500. case value_t::array:
  16501. JSONSerializer<other_array_t>::to_json(*this, val.template get_ref<const other_array_t&>());
  16502. break;
  16503. case value_t::binary:
  16504. JSONSerializer<other_binary_t>::to_json(*this, val.template get_ref<const other_binary_t&>());
  16505. break;
  16506. case value_t::null:
  16507. *this = nullptr;
  16508. break;
  16509. case value_t::discarded:
  16510. m_type = value_t::discarded;
  16511. break;
  16512. default: // LCOV_EXCL_LINE
  16513. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  16514. }
  16515. set_parents();
  16516. assert_invariant();
  16517. }
  16518. /*!
  16519. @brief create a container (array or object) from an initializer list
  16520. Creates a JSON value of type array or object from the passed initializer
  16521. list @a init. In case @a type_deduction is `true` (default), the type of
  16522. the JSON value to be created is deducted from the initializer list @a init
  16523. according to the following rules:
  16524. 1. If the list is empty, an empty JSON object value `{}` is created.
  16525. 2. If the list consists of pairs whose first element is a string, a JSON
  16526. object value is created where the first elements of the pairs are
  16527. treated as keys and the second elements are as values.
  16528. 3. In all other cases, an array is created.
  16529. The rules aim to create the best fit between a C++ initializer list and
  16530. JSON values. The rationale is as follows:
  16531. 1. The empty initializer list is written as `{}` which is exactly an empty
  16532. JSON object.
  16533. 2. C++ has no way of describing mapped types other than to list a list of
  16534. pairs. As JSON requires that keys must be of type string, rule 2 is the
  16535. weakest constraint one can pose on initializer lists to interpret them
  16536. as an object.
  16537. 3. In all other cases, the initializer list could not be interpreted as
  16538. JSON object type, so interpreting it as JSON array type is safe.
  16539. With the rules described above, the following JSON values cannot be
  16540. expressed by an initializer list:
  16541. - the empty array (`[]`): use @ref array(initializer_list_t)
  16542. with an empty initializer list in this case
  16543. - arrays whose elements satisfy rule 2: use @ref
  16544. array(initializer_list_t) with the same initializer list
  16545. in this case
  16546. @note When used without parentheses around an empty initializer list, @ref
  16547. basic_json() is called instead of this function, yielding the JSON null
  16548. value.
  16549. @param[in] init initializer list with JSON values
  16550. @param[in] type_deduction internal parameter; when set to `true`, the type
  16551. of the JSON value is deducted from the initializer list @a init; when set
  16552. to `false`, the type provided via @a manual_type is forced. This mode is
  16553. used by the functions @ref array(initializer_list_t) and
  16554. @ref object(initializer_list_t).
  16555. @param[in] manual_type internal parameter; when @a type_deduction is set
  16556. to `false`, the created JSON value will use the provided type (only @ref
  16557. value_t::array and @ref value_t::object are valid); when @a type_deduction
  16558. is set to `true`, this parameter has no effect
  16559. @throw type_error.301 if @a type_deduction is `false`, @a manual_type is
  16560. `value_t::object`, but @a init contains an element which is not a pair
  16561. whose first element is a string. In this case, the constructor could not
  16562. create an object. If @a type_deduction would have be `true`, an array
  16563. would have been created. See @ref object(initializer_list_t)
  16564. for an example.
  16565. @complexity Linear in the size of the initializer list @a init.
  16566. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  16567. changes to any JSON value.
  16568. @liveexample{The example below shows how JSON values are created from
  16569. initializer lists.,basic_json__list_init_t}
  16570. @sa see @ref array(initializer_list_t) -- create a JSON array
  16571. value from an initializer list
  16572. @sa see @ref object(initializer_list_t) -- create a JSON object
  16573. value from an initializer list
  16574. @since version 1.0.0
  16575. */
  16576. basic_json(initializer_list_t init,
  16577. bool type_deduction = true,
  16578. value_t manual_type = value_t::array)
  16579. {
  16580. // check if each element is an array with two elements whose first
  16581. // element is a string
  16582. bool is_an_object = std::all_of(init.begin(), init.end(),
  16583. [](const detail::json_ref<basic_json>& element_ref)
  16584. {
  16585. return element_ref->is_array() && element_ref->size() == 2 && (*element_ref)[0].is_string();
  16586. });
  16587. // adjust type if type deduction is not wanted
  16588. if (!type_deduction)
  16589. {
  16590. // if array is wanted, do not create an object though possible
  16591. if (manual_type == value_t::array)
  16592. {
  16593. is_an_object = false;
  16594. }
  16595. // if object is wanted but impossible, throw an exception
  16596. if (JSON_HEDLEY_UNLIKELY(manual_type == value_t::object && !is_an_object))
  16597. {
  16598. JSON_THROW(type_error::create(301, "cannot create object from initializer list", basic_json()));
  16599. }
  16600. }
  16601. if (is_an_object)
  16602. {
  16603. // the initializer list is a list of pairs -> create object
  16604. m_type = value_t::object;
  16605. m_value = value_t::object;
  16606. for (auto& element_ref : init)
  16607. {
  16608. auto element = element_ref.moved_or_copied();
  16609. m_value.object->emplace(
  16610. std::move(*((*element.m_value.array)[0].m_value.string)),
  16611. std::move((*element.m_value.array)[1]));
  16612. }
  16613. }
  16614. else
  16615. {
  16616. // the initializer list describes an array -> create array
  16617. m_type = value_t::array;
  16618. m_value.array = create<array_t>(init.begin(), init.end());
  16619. }
  16620. set_parents();
  16621. assert_invariant();
  16622. }
  16623. /*!
  16624. @brief explicitly create a binary array (without subtype)
  16625. Creates a JSON binary array value from a given binary container. Binary
  16626. values are part of various binary formats, such as CBOR, MessagePack, and
  16627. BSON. This constructor is used to create a value for serialization to those
  16628. formats.
  16629. @note Note, this function exists because of the difficulty in correctly
  16630. specifying the correct template overload in the standard value ctor, as both
  16631. JSON arrays and JSON binary arrays are backed with some form of a
  16632. `std::vector`. Because JSON binary arrays are a non-standard extension it
  16633. was decided that it would be best to prevent automatic initialization of a
  16634. binary array type, for backwards compatibility and so it does not happen on
  16635. accident.
  16636. @param[in] init container containing bytes to use as binary type
  16637. @return JSON binary array value
  16638. @complexity Linear in the size of @a init.
  16639. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  16640. changes to any JSON value.
  16641. @since version 3.8.0
  16642. */
  16643. JSON_HEDLEY_WARN_UNUSED_RESULT
  16644. static basic_json binary(const typename binary_t::container_type& init)
  16645. {
  16646. auto res = basic_json();
  16647. res.m_type = value_t::binary;
  16648. res.m_value = init;
  16649. return res;
  16650. }
  16651. /*!
  16652. @brief explicitly create a binary array (with subtype)
  16653. Creates a JSON binary array value from a given binary container. Binary
  16654. values are part of various binary formats, such as CBOR, MessagePack, and
  16655. BSON. This constructor is used to create a value for serialization to those
  16656. formats.
  16657. @note Note, this function exists because of the difficulty in correctly
  16658. specifying the correct template overload in the standard value ctor, as both
  16659. JSON arrays and JSON binary arrays are backed with some form of a
  16660. `std::vector`. Because JSON binary arrays are a non-standard extension it
  16661. was decided that it would be best to prevent automatic initialization of a
  16662. binary array type, for backwards compatibility and so it does not happen on
  16663. accident.
  16664. @param[in] init container containing bytes to use as binary type
  16665. @param[in] subtype subtype to use in MessagePack and BSON
  16666. @return JSON binary array value
  16667. @complexity Linear in the size of @a init.
  16668. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  16669. changes to any JSON value.
  16670. @since version 3.8.0
  16671. */
  16672. JSON_HEDLEY_WARN_UNUSED_RESULT
  16673. static basic_json binary(const typename binary_t::container_type& init, typename binary_t::subtype_type subtype)
  16674. {
  16675. auto res = basic_json();
  16676. res.m_type = value_t::binary;
  16677. res.m_value = binary_t(init, subtype);
  16678. return res;
  16679. }
  16680. /// @copydoc binary(const typename binary_t::container_type&)
  16681. JSON_HEDLEY_WARN_UNUSED_RESULT
  16682. static basic_json binary(typename binary_t::container_type&& init)
  16683. {
  16684. auto res = basic_json();
  16685. res.m_type = value_t::binary;
  16686. res.m_value = std::move(init);
  16687. return res;
  16688. }
  16689. /// @copydoc binary(const typename binary_t::container_type&, typename binary_t::subtype_type)
  16690. JSON_HEDLEY_WARN_UNUSED_RESULT
  16691. static basic_json binary(typename binary_t::container_type&& init, typename binary_t::subtype_type subtype)
  16692. {
  16693. auto res = basic_json();
  16694. res.m_type = value_t::binary;
  16695. res.m_value = binary_t(std::move(init), subtype);
  16696. return res;
  16697. }
  16698. /*!
  16699. @brief explicitly create an array from an initializer list
  16700. Creates a JSON array value from a given initializer list. That is, given a
  16701. list of values `a, b, c`, creates the JSON value `[a, b, c]`. If the
  16702. initializer list is empty, the empty array `[]` is created.
  16703. @note This function is only needed to express two edge cases that cannot
  16704. be realized with the initializer list constructor (@ref
  16705. basic_json(initializer_list_t, bool, value_t)). These cases
  16706. are:
  16707. 1. creating an array whose elements are all pairs whose first element is a
  16708. string -- in this case, the initializer list constructor would create an
  16709. object, taking the first elements as keys
  16710. 2. creating an empty array -- passing the empty initializer list to the
  16711. initializer list constructor yields an empty object
  16712. @param[in] init initializer list with JSON values to create an array from
  16713. (optional)
  16714. @return JSON array value
  16715. @complexity Linear in the size of @a init.
  16716. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  16717. changes to any JSON value.
  16718. @liveexample{The following code shows an example for the `array`
  16719. function.,array}
  16720. @sa see @ref basic_json(initializer_list_t, bool, value_t) --
  16721. create a JSON value from an initializer list
  16722. @sa see @ref object(initializer_list_t) -- create a JSON object
  16723. value from an initializer list
  16724. @since version 1.0.0
  16725. */
  16726. JSON_HEDLEY_WARN_UNUSED_RESULT
  16727. static basic_json array(initializer_list_t init = {})
  16728. {
  16729. return basic_json(init, false, value_t::array);
  16730. }
  16731. /*!
  16732. @brief explicitly create an object from an initializer list
  16733. Creates a JSON object value from a given initializer list. The initializer
  16734. lists elements must be pairs, and their first elements must be strings. If
  16735. the initializer list is empty, the empty object `{}` is created.
  16736. @note This function is only added for symmetry reasons. In contrast to the
  16737. related function @ref array(initializer_list_t), there are
  16738. no cases which can only be expressed by this function. That is, any
  16739. initializer list @a init can also be passed to the initializer list
  16740. constructor @ref basic_json(initializer_list_t, bool, value_t).
  16741. @param[in] init initializer list to create an object from (optional)
  16742. @return JSON object value
  16743. @throw type_error.301 if @a init is not a list of pairs whose first
  16744. elements are strings. In this case, no object can be created. When such a
  16745. value is passed to @ref basic_json(initializer_list_t, bool, value_t),
  16746. an array would have been created from the passed initializer list @a init.
  16747. See example below.
  16748. @complexity Linear in the size of @a init.
  16749. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  16750. changes to any JSON value.
  16751. @liveexample{The following code shows an example for the `object`
  16752. function.,object}
  16753. @sa see @ref basic_json(initializer_list_t, bool, value_t) --
  16754. create a JSON value from an initializer list
  16755. @sa see @ref array(initializer_list_t) -- create a JSON array
  16756. value from an initializer list
  16757. @since version 1.0.0
  16758. */
  16759. JSON_HEDLEY_WARN_UNUSED_RESULT
  16760. static basic_json object(initializer_list_t init = {})
  16761. {
  16762. return basic_json(init, false, value_t::object);
  16763. }
  16764. /*!
  16765. @brief construct an array with count copies of given value
  16766. Constructs a JSON array value by creating @a cnt copies of a passed value.
  16767. In case @a cnt is `0`, an empty array is created.
  16768. @param[in] cnt the number of JSON copies of @a val to create
  16769. @param[in] val the JSON value to copy
  16770. @post `std::distance(begin(),end()) == cnt` holds.
  16771. @complexity Linear in @a cnt.
  16772. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  16773. changes to any JSON value.
  16774. @liveexample{The following code shows examples for the @ref
  16775. basic_json(size_type\, const basic_json&)
  16776. constructor.,basic_json__size_type_basic_json}
  16777. @since version 1.0.0
  16778. */
  16779. basic_json(size_type cnt, const basic_json& val)
  16780. : m_type(value_t::array)
  16781. {
  16782. m_value.array = create<array_t>(cnt, val);
  16783. set_parents();
  16784. assert_invariant();
  16785. }
  16786. /*!
  16787. @brief construct a JSON container given an iterator range
  16788. Constructs the JSON value with the contents of the range `[first, last)`.
  16789. The semantics depends on the different types a JSON value can have:
  16790. - In case of a null type, invalid_iterator.206 is thrown.
  16791. - In case of other primitive types (number, boolean, or string), @a first
  16792. must be `begin()` and @a last must be `end()`. In this case, the value is
  16793. copied. Otherwise, invalid_iterator.204 is thrown.
  16794. - In case of structured types (array, object), the constructor behaves as
  16795. similar versions for `std::vector` or `std::map`; that is, a JSON array
  16796. or object is constructed from the values in the range.
  16797. @tparam InputIT an input iterator type (@ref iterator or @ref
  16798. const_iterator)
  16799. @param[in] first begin of the range to copy from (included)
  16800. @param[in] last end of the range to copy from (excluded)
  16801. @pre Iterators @a first and @a last must be initialized. **This
  16802. precondition is enforced with an assertion (see warning).** If
  16803. assertions are switched off, a violation of this precondition yields
  16804. undefined behavior.
  16805. @pre Range `[first, last)` is valid. Usually, this precondition cannot be
  16806. checked efficiently. Only certain edge cases are detected; see the
  16807. description of the exceptions below. A violation of this precondition
  16808. yields undefined behavior.
  16809. @warning A precondition is enforced with a runtime assertion that will
  16810. result in calling `std::abort` if this precondition is not met.
  16811. Assertions can be disabled by defining `NDEBUG` at compile time.
  16812. See https://en.cppreference.com/w/cpp/error/assert for more
  16813. information.
  16814. @throw invalid_iterator.201 if iterators @a first and @a last are not
  16815. compatible (i.e., do not belong to the same JSON value). In this case,
  16816. the range `[first, last)` is undefined.
  16817. @throw invalid_iterator.204 if iterators @a first and @a last belong to a
  16818. primitive type (number, boolean, or string), but @a first does not point
  16819. to the first element any more. In this case, the range `[first, last)` is
  16820. undefined. See example code below.
  16821. @throw invalid_iterator.206 if iterators @a first and @a last belong to a
  16822. null value. In this case, the range `[first, last)` is undefined.
  16823. @complexity Linear in distance between @a first and @a last.
  16824. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  16825. changes to any JSON value.
  16826. @liveexample{The example below shows several ways to create JSON values by
  16827. specifying a subrange with iterators.,basic_json__InputIt_InputIt}
  16828. @since version 1.0.0
  16829. */
  16830. template < class InputIT, typename std::enable_if <
  16831. std::is_same<InputIT, typename basic_json_t::iterator>::value ||
  16832. std::is_same<InputIT, typename basic_json_t::const_iterator>::value, int >::type = 0 >
  16833. basic_json(InputIT first, InputIT last)
  16834. {
  16835. JSON_ASSERT(first.m_object != nullptr);
  16836. JSON_ASSERT(last.m_object != nullptr);
  16837. // make sure iterator fits the current value
  16838. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  16839. {
  16840. JSON_THROW(invalid_iterator::create(201, "iterators are not compatible", basic_json()));
  16841. }
  16842. // copy type from first iterator
  16843. m_type = first.m_object->m_type;
  16844. // check if iterator range is complete for primitive values
  16845. switch (m_type)
  16846. {
  16847. case value_t::boolean:
  16848. case value_t::number_float:
  16849. case value_t::number_integer:
  16850. case value_t::number_unsigned:
  16851. case value_t::string:
  16852. {
  16853. if (JSON_HEDLEY_UNLIKELY(!first.m_it.primitive_iterator.is_begin()
  16854. || !last.m_it.primitive_iterator.is_end()))
  16855. {
  16856. JSON_THROW(invalid_iterator::create(204, "iterators out of range", *first.m_object));
  16857. }
  16858. break;
  16859. }
  16860. case value_t::null:
  16861. case value_t::object:
  16862. case value_t::array:
  16863. case value_t::binary:
  16864. case value_t::discarded:
  16865. default:
  16866. break;
  16867. }
  16868. switch (m_type)
  16869. {
  16870. case value_t::number_integer:
  16871. {
  16872. m_value.number_integer = first.m_object->m_value.number_integer;
  16873. break;
  16874. }
  16875. case value_t::number_unsigned:
  16876. {
  16877. m_value.number_unsigned = first.m_object->m_value.number_unsigned;
  16878. break;
  16879. }
  16880. case value_t::number_float:
  16881. {
  16882. m_value.number_float = first.m_object->m_value.number_float;
  16883. break;
  16884. }
  16885. case value_t::boolean:
  16886. {
  16887. m_value.boolean = first.m_object->m_value.boolean;
  16888. break;
  16889. }
  16890. case value_t::string:
  16891. {
  16892. m_value = *first.m_object->m_value.string;
  16893. break;
  16894. }
  16895. case value_t::object:
  16896. {
  16897. m_value.object = create<object_t>(first.m_it.object_iterator,
  16898. last.m_it.object_iterator);
  16899. break;
  16900. }
  16901. case value_t::array:
  16902. {
  16903. m_value.array = create<array_t>(first.m_it.array_iterator,
  16904. last.m_it.array_iterator);
  16905. break;
  16906. }
  16907. case value_t::binary:
  16908. {
  16909. m_value = *first.m_object->m_value.binary;
  16910. break;
  16911. }
  16912. case value_t::null:
  16913. case value_t::discarded:
  16914. default:
  16915. JSON_THROW(invalid_iterator::create(206, "cannot construct with iterators from " + std::string(first.m_object->type_name()), *first.m_object));
  16916. }
  16917. set_parents();
  16918. assert_invariant();
  16919. }
  16920. ///////////////////////////////////////
  16921. // other constructors and destructor //
  16922. ///////////////////////////////////////
  16923. template<typename JsonRef,
  16924. detail::enable_if_t<detail::conjunction<detail::is_json_ref<JsonRef>,
  16925. std::is_same<typename JsonRef::value_type, basic_json>>::value, int> = 0 >
  16926. basic_json(const JsonRef& ref) : basic_json(ref.moved_or_copied()) {}
  16927. /*!
  16928. @brief copy constructor
  16929. Creates a copy of a given JSON value.
  16930. @param[in] other the JSON value to copy
  16931. @post `*this == other`
  16932. @complexity Linear in the size of @a other.
  16933. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  16934. changes to any JSON value.
  16935. @requirement This function helps `basic_json` satisfying the
  16936. [Container](https://en.cppreference.com/w/cpp/named_req/Container)
  16937. requirements:
  16938. - The complexity is linear.
  16939. - As postcondition, it holds: `other == basic_json(other)`.
  16940. @liveexample{The following code shows an example for the copy
  16941. constructor.,basic_json__basic_json}
  16942. @since version 1.0.0
  16943. */
  16944. basic_json(const basic_json& other)
  16945. : m_type(other.m_type)
  16946. {
  16947. // check of passed value is valid
  16948. other.assert_invariant();
  16949. switch (m_type)
  16950. {
  16951. case value_t::object:
  16952. {
  16953. m_value = *other.m_value.object;
  16954. break;
  16955. }
  16956. case value_t::array:
  16957. {
  16958. m_value = *other.m_value.array;
  16959. break;
  16960. }
  16961. case value_t::string:
  16962. {
  16963. m_value = *other.m_value.string;
  16964. break;
  16965. }
  16966. case value_t::boolean:
  16967. {
  16968. m_value = other.m_value.boolean;
  16969. break;
  16970. }
  16971. case value_t::number_integer:
  16972. {
  16973. m_value = other.m_value.number_integer;
  16974. break;
  16975. }
  16976. case value_t::number_unsigned:
  16977. {
  16978. m_value = other.m_value.number_unsigned;
  16979. break;
  16980. }
  16981. case value_t::number_float:
  16982. {
  16983. m_value = other.m_value.number_float;
  16984. break;
  16985. }
  16986. case value_t::binary:
  16987. {
  16988. m_value = *other.m_value.binary;
  16989. break;
  16990. }
  16991. case value_t::null:
  16992. case value_t::discarded:
  16993. default:
  16994. break;
  16995. }
  16996. set_parents();
  16997. assert_invariant();
  16998. }
  16999. /*!
  17000. @brief move constructor
  17001. Move constructor. Constructs a JSON value with the contents of the given
  17002. value @a other using move semantics. It "steals" the resources from @a
  17003. other and leaves it as JSON null value.
  17004. @param[in,out] other value to move to this object
  17005. @post `*this` has the same value as @a other before the call.
  17006. @post @a other is a JSON null value.
  17007. @complexity Constant.
  17008. @exceptionsafety No-throw guarantee: this constructor never throws
  17009. exceptions.
  17010. @requirement This function helps `basic_json` satisfying the
  17011. [MoveConstructible](https://en.cppreference.com/w/cpp/named_req/MoveConstructible)
  17012. requirements.
  17013. @liveexample{The code below shows the move constructor explicitly called
  17014. via std::move.,basic_json__moveconstructor}
  17015. @since version 1.0.0
  17016. */
  17017. basic_json(basic_json&& other) noexcept
  17018. : m_type(std::move(other.m_type)),
  17019. m_value(std::move(other.m_value))
  17020. {
  17021. // check that passed value is valid
  17022. other.assert_invariant(false);
  17023. // invalidate payload
  17024. other.m_type = value_t::null;
  17025. other.m_value = {};
  17026. set_parents();
  17027. assert_invariant();
  17028. }
  17029. /*!
  17030. @brief copy assignment
  17031. Copy assignment operator. Copies a JSON value via the "copy and swap"
  17032. strategy: It is expressed in terms of the copy constructor, destructor,
  17033. and the `swap()` member function.
  17034. @param[in] other value to copy from
  17035. @complexity Linear.
  17036. @requirement This function helps `basic_json` satisfying the
  17037. [Container](https://en.cppreference.com/w/cpp/named_req/Container)
  17038. requirements:
  17039. - The complexity is linear.
  17040. @liveexample{The code below shows and example for the copy assignment. It
  17041. creates a copy of value `a` which is then swapped with `b`. Finally\, the
  17042. copy of `a` (which is the null value after the swap) is
  17043. destroyed.,basic_json__copyassignment}
  17044. @since version 1.0.0
  17045. */
  17046. basic_json& operator=(basic_json other) noexcept (
  17047. std::is_nothrow_move_constructible<value_t>::value&&
  17048. std::is_nothrow_move_assignable<value_t>::value&&
  17049. std::is_nothrow_move_constructible<json_value>::value&&
  17050. std::is_nothrow_move_assignable<json_value>::value
  17051. )
  17052. {
  17053. // check that passed value is valid
  17054. other.assert_invariant();
  17055. using std::swap;
  17056. swap(m_type, other.m_type);
  17057. swap(m_value, other.m_value);
  17058. set_parents();
  17059. assert_invariant();
  17060. return *this;
  17061. }
  17062. /*!
  17063. @brief destructor
  17064. Destroys the JSON value and frees all allocated memory.
  17065. @complexity Linear.
  17066. @requirement This function helps `basic_json` satisfying the
  17067. [Container](https://en.cppreference.com/w/cpp/named_req/Container)
  17068. requirements:
  17069. - The complexity is linear.
  17070. - All stored elements are destroyed and all memory is freed.
  17071. @since version 1.0.0
  17072. */
  17073. ~basic_json() noexcept
  17074. {
  17075. assert_invariant(false);
  17076. m_value.destroy(m_type);
  17077. }
  17078. /// @}
  17079. public:
  17080. ///////////////////////
  17081. // object inspection //
  17082. ///////////////////////
  17083. /// @name object inspection
  17084. /// Functions to inspect the type of a JSON value.
  17085. /// @{
  17086. /*!
  17087. @brief serialization
  17088. Serialization function for JSON values. The function tries to mimic
  17089. Python's `json.dumps()` function, and currently supports its @a indent
  17090. and @a ensure_ascii parameters.
  17091. @param[in] indent If indent is nonnegative, then array elements and object
  17092. members will be pretty-printed with that indent level. An indent level of
  17093. `0` will only insert newlines. `-1` (the default) selects the most compact
  17094. representation.
  17095. @param[in] indent_char The character to use for indentation if @a indent is
  17096. greater than `0`. The default is ` ` (space).
  17097. @param[in] ensure_ascii If @a ensure_ascii is true, all non-ASCII characters
  17098. in the output are escaped with `\uXXXX` sequences, and the result consists
  17099. of ASCII characters only.
  17100. @param[in] error_handler how to react on decoding errors; there are three
  17101. possible values: `strict` (throws and exception in case a decoding error
  17102. occurs; default), `replace` (replace invalid UTF-8 sequences with U+FFFD),
  17103. and `ignore` (ignore invalid UTF-8 sequences during serialization; all
  17104. bytes are copied to the output unchanged).
  17105. @return string containing the serialization of the JSON value
  17106. @throw type_error.316 if a string stored inside the JSON value is not
  17107. UTF-8 encoded and @a error_handler is set to strict
  17108. @note Binary values are serialized as object containing two keys:
  17109. - "bytes": an array of bytes as integers
  17110. - "subtype": the subtype as integer or "null" if the binary has no subtype
  17111. @complexity Linear.
  17112. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  17113. changes in the JSON value.
  17114. @liveexample{The following example shows the effect of different @a indent\,
  17115. @a indent_char\, and @a ensure_ascii parameters to the result of the
  17116. serialization.,dump}
  17117. @see https://docs.python.org/2/library/json.html#json.dump
  17118. @since version 1.0.0; indentation character @a indent_char, option
  17119. @a ensure_ascii and exceptions added in version 3.0.0; error
  17120. handlers added in version 3.4.0; serialization of binary values added
  17121. in version 3.8.0.
  17122. */
  17123. string_t dump(const int indent = -1,
  17124. const char indent_char = ' ',
  17125. const bool ensure_ascii = false,
  17126. const error_handler_t error_handler = error_handler_t::strict) const
  17127. {
  17128. string_t result;
  17129. serializer s(detail::output_adapter<char, string_t>(result), indent_char, error_handler);
  17130. if (indent >= 0)
  17131. {
  17132. s.dump(*this, true, ensure_ascii, static_cast<unsigned int>(indent));
  17133. }
  17134. else
  17135. {
  17136. s.dump(*this, false, ensure_ascii, 0);
  17137. }
  17138. return result;
  17139. }
  17140. /*!
  17141. @brief return the type of the JSON value (explicit)
  17142. Return the type of the JSON value as a value from the @ref value_t
  17143. enumeration.
  17144. @return the type of the JSON value
  17145. Value type | return value
  17146. ------------------------- | -------------------------
  17147. null | value_t::null
  17148. boolean | value_t::boolean
  17149. string | value_t::string
  17150. number (integer) | value_t::number_integer
  17151. number (unsigned integer) | value_t::number_unsigned
  17152. number (floating-point) | value_t::number_float
  17153. object | value_t::object
  17154. array | value_t::array
  17155. binary | value_t::binary
  17156. discarded | value_t::discarded
  17157. @complexity Constant.
  17158. @exceptionsafety No-throw guarantee: this member function never throws
  17159. exceptions.
  17160. @liveexample{The following code exemplifies `type()` for all JSON
  17161. types.,type}
  17162. @sa see @ref operator value_t() -- return the type of the JSON value (implicit)
  17163. @sa see @ref type_name() -- return the type as string
  17164. @since version 1.0.0
  17165. */
  17166. constexpr value_t type() const noexcept
  17167. {
  17168. return m_type;
  17169. }
  17170. /*!
  17171. @brief return whether type is primitive
  17172. This function returns true if and only if the JSON type is primitive
  17173. (string, number, boolean, or null).
  17174. @return `true` if type is primitive (string, number, boolean, or null),
  17175. `false` otherwise.
  17176. @complexity Constant.
  17177. @exceptionsafety No-throw guarantee: this member function never throws
  17178. exceptions.
  17179. @liveexample{The following code exemplifies `is_primitive()` for all JSON
  17180. types.,is_primitive}
  17181. @sa see @ref is_structured() -- returns whether JSON value is structured
  17182. @sa see @ref is_null() -- returns whether JSON value is `null`
  17183. @sa see @ref is_string() -- returns whether JSON value is a string
  17184. @sa see @ref is_boolean() -- returns whether JSON value is a boolean
  17185. @sa see @ref is_number() -- returns whether JSON value is a number
  17186. @sa see @ref is_binary() -- returns whether JSON value is a binary array
  17187. @since version 1.0.0
  17188. */
  17189. constexpr bool is_primitive() const noexcept
  17190. {
  17191. return is_null() || is_string() || is_boolean() || is_number() || is_binary();
  17192. }
  17193. /*!
  17194. @brief return whether type is structured
  17195. This function returns true if and only if the JSON type is structured
  17196. (array or object).
  17197. @return `true` if type is structured (array or object), `false` otherwise.
  17198. @complexity Constant.
  17199. @exceptionsafety No-throw guarantee: this member function never throws
  17200. exceptions.
  17201. @liveexample{The following code exemplifies `is_structured()` for all JSON
  17202. types.,is_structured}
  17203. @sa see @ref is_primitive() -- returns whether value is primitive
  17204. @sa see @ref is_array() -- returns whether value is an array
  17205. @sa see @ref is_object() -- returns whether value is an object
  17206. @since version 1.0.0
  17207. */
  17208. constexpr bool is_structured() const noexcept
  17209. {
  17210. return is_array() || is_object();
  17211. }
  17212. /*!
  17213. @brief return whether value is null
  17214. This function returns true if and only if the JSON value is null.
  17215. @return `true` if type is null, `false` otherwise.
  17216. @complexity Constant.
  17217. @exceptionsafety No-throw guarantee: this member function never throws
  17218. exceptions.
  17219. @liveexample{The following code exemplifies `is_null()` for all JSON
  17220. types.,is_null}
  17221. @since version 1.0.0
  17222. */
  17223. constexpr bool is_null() const noexcept
  17224. {
  17225. return m_type == value_t::null;
  17226. }
  17227. /*!
  17228. @brief return whether value is a boolean
  17229. This function returns true if and only if the JSON value is a boolean.
  17230. @return `true` if type is boolean, `false` otherwise.
  17231. @complexity Constant.
  17232. @exceptionsafety No-throw guarantee: this member function never throws
  17233. exceptions.
  17234. @liveexample{The following code exemplifies `is_boolean()` for all JSON
  17235. types.,is_boolean}
  17236. @since version 1.0.0
  17237. */
  17238. constexpr bool is_boolean() const noexcept
  17239. {
  17240. return m_type == value_t::boolean;
  17241. }
  17242. /*!
  17243. @brief return whether value is a number
  17244. This function returns true if and only if the JSON value is a number. This
  17245. includes both integer (signed and unsigned) and floating-point values.
  17246. @return `true` if type is number (regardless whether integer, unsigned
  17247. integer or floating-type), `false` otherwise.
  17248. @complexity Constant.
  17249. @exceptionsafety No-throw guarantee: this member function never throws
  17250. exceptions.
  17251. @liveexample{The following code exemplifies `is_number()` for all JSON
  17252. types.,is_number}
  17253. @sa see @ref is_number_integer() -- check if value is an integer or unsigned
  17254. integer number
  17255. @sa see @ref is_number_unsigned() -- check if value is an unsigned integer
  17256. number
  17257. @sa see @ref is_number_float() -- check if value is a floating-point number
  17258. @since version 1.0.0
  17259. */
  17260. constexpr bool is_number() const noexcept
  17261. {
  17262. return is_number_integer() || is_number_float();
  17263. }
  17264. /*!
  17265. @brief return whether value is an integer number
  17266. This function returns true if and only if the JSON value is a signed or
  17267. unsigned integer number. This excludes floating-point values.
  17268. @return `true` if type is an integer or unsigned integer number, `false`
  17269. otherwise.
  17270. @complexity Constant.
  17271. @exceptionsafety No-throw guarantee: this member function never throws
  17272. exceptions.
  17273. @liveexample{The following code exemplifies `is_number_integer()` for all
  17274. JSON types.,is_number_integer}
  17275. @sa see @ref is_number() -- check if value is a number
  17276. @sa see @ref is_number_unsigned() -- check if value is an unsigned integer
  17277. number
  17278. @sa see @ref is_number_float() -- check if value is a floating-point number
  17279. @since version 1.0.0
  17280. */
  17281. constexpr bool is_number_integer() const noexcept
  17282. {
  17283. return m_type == value_t::number_integer || m_type == value_t::number_unsigned;
  17284. }
  17285. /*!
  17286. @brief return whether value is an unsigned integer number
  17287. This function returns true if and only if the JSON value is an unsigned
  17288. integer number. This excludes floating-point and signed integer values.
  17289. @return `true` if type is an unsigned integer number, `false` otherwise.
  17290. @complexity Constant.
  17291. @exceptionsafety No-throw guarantee: this member function never throws
  17292. exceptions.
  17293. @liveexample{The following code exemplifies `is_number_unsigned()` for all
  17294. JSON types.,is_number_unsigned}
  17295. @sa see @ref is_number() -- check if value is a number
  17296. @sa see @ref is_number_integer() -- check if value is an integer or unsigned
  17297. integer number
  17298. @sa see @ref is_number_float() -- check if value is a floating-point number
  17299. @since version 2.0.0
  17300. */
  17301. constexpr bool is_number_unsigned() const noexcept
  17302. {
  17303. return m_type == value_t::number_unsigned;
  17304. }
  17305. /*!
  17306. @brief return whether value is a floating-point number
  17307. This function returns true if and only if the JSON value is a
  17308. floating-point number. This excludes signed and unsigned integer values.
  17309. @return `true` if type is a floating-point number, `false` otherwise.
  17310. @complexity Constant.
  17311. @exceptionsafety No-throw guarantee: this member function never throws
  17312. exceptions.
  17313. @liveexample{The following code exemplifies `is_number_float()` for all
  17314. JSON types.,is_number_float}
  17315. @sa see @ref is_number() -- check if value is number
  17316. @sa see @ref is_number_integer() -- check if value is an integer number
  17317. @sa see @ref is_number_unsigned() -- check if value is an unsigned integer
  17318. number
  17319. @since version 1.0.0
  17320. */
  17321. constexpr bool is_number_float() const noexcept
  17322. {
  17323. return m_type == value_t::number_float;
  17324. }
  17325. /*!
  17326. @brief return whether value is an object
  17327. This function returns true if and only if the JSON value is an object.
  17328. @return `true` if type is object, `false` otherwise.
  17329. @complexity Constant.
  17330. @exceptionsafety No-throw guarantee: this member function never throws
  17331. exceptions.
  17332. @liveexample{The following code exemplifies `is_object()` for all JSON
  17333. types.,is_object}
  17334. @since version 1.0.0
  17335. */
  17336. constexpr bool is_object() const noexcept
  17337. {
  17338. return m_type == value_t::object;
  17339. }
  17340. /*!
  17341. @brief return whether value is an array
  17342. This function returns true if and only if the JSON value is an array.
  17343. @return `true` if type is array, `false` otherwise.
  17344. @complexity Constant.
  17345. @exceptionsafety No-throw guarantee: this member function never throws
  17346. exceptions.
  17347. @liveexample{The following code exemplifies `is_array()` for all JSON
  17348. types.,is_array}
  17349. @since version 1.0.0
  17350. */
  17351. constexpr bool is_array() const noexcept
  17352. {
  17353. return m_type == value_t::array;
  17354. }
  17355. /*!
  17356. @brief return whether value is a string
  17357. This function returns true if and only if the JSON value is a string.
  17358. @return `true` if type is string, `false` otherwise.
  17359. @complexity Constant.
  17360. @exceptionsafety No-throw guarantee: this member function never throws
  17361. exceptions.
  17362. @liveexample{The following code exemplifies `is_string()` for all JSON
  17363. types.,is_string}
  17364. @since version 1.0.0
  17365. */
  17366. constexpr bool is_string() const noexcept
  17367. {
  17368. return m_type == value_t::string;
  17369. }
  17370. /*!
  17371. @brief return whether value is a binary array
  17372. This function returns true if and only if the JSON value is a binary array.
  17373. @return `true` if type is binary array, `false` otherwise.
  17374. @complexity Constant.
  17375. @exceptionsafety No-throw guarantee: this member function never throws
  17376. exceptions.
  17377. @liveexample{The following code exemplifies `is_binary()` for all JSON
  17378. types.,is_binary}
  17379. @since version 3.8.0
  17380. */
  17381. constexpr bool is_binary() const noexcept
  17382. {
  17383. return m_type == value_t::binary;
  17384. }
  17385. /*!
  17386. @brief return whether value is discarded
  17387. This function returns true if and only if the JSON value was discarded
  17388. during parsing with a callback function (see @ref parser_callback_t).
  17389. @note This function will always be `false` for JSON values after parsing.
  17390. That is, discarded values can only occur during parsing, but will be
  17391. removed when inside a structured value or replaced by null in other cases.
  17392. @return `true` if type is discarded, `false` otherwise.
  17393. @complexity Constant.
  17394. @exceptionsafety No-throw guarantee: this member function never throws
  17395. exceptions.
  17396. @liveexample{The following code exemplifies `is_discarded()` for all JSON
  17397. types.,is_discarded}
  17398. @since version 1.0.0
  17399. */
  17400. constexpr bool is_discarded() const noexcept
  17401. {
  17402. return m_type == value_t::discarded;
  17403. }
  17404. /*!
  17405. @brief return the type of the JSON value (implicit)
  17406. Implicitly return the type of the JSON value as a value from the @ref
  17407. value_t enumeration.
  17408. @return the type of the JSON value
  17409. @complexity Constant.
  17410. @exceptionsafety No-throw guarantee: this member function never throws
  17411. exceptions.
  17412. @liveexample{The following code exemplifies the @ref value_t operator for
  17413. all JSON types.,operator__value_t}
  17414. @sa see @ref type() -- return the type of the JSON value (explicit)
  17415. @sa see @ref type_name() -- return the type as string
  17416. @since version 1.0.0
  17417. */
  17418. constexpr operator value_t() const noexcept
  17419. {
  17420. return m_type;
  17421. }
  17422. /// @}
  17423. private:
  17424. //////////////////
  17425. // value access //
  17426. //////////////////
  17427. /// get a boolean (explicit)
  17428. boolean_t get_impl(boolean_t* /*unused*/) const
  17429. {
  17430. if (JSON_HEDLEY_LIKELY(is_boolean()))
  17431. {
  17432. return m_value.boolean;
  17433. }
  17434. JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(type_name()), *this));
  17435. }
  17436. /// get a pointer to the value (object)
  17437. object_t* get_impl_ptr(object_t* /*unused*/) noexcept
  17438. {
  17439. return is_object() ? m_value.object : nullptr;
  17440. }
  17441. /// get a pointer to the value (object)
  17442. constexpr const object_t* get_impl_ptr(const object_t* /*unused*/) const noexcept
  17443. {
  17444. return is_object() ? m_value.object : nullptr;
  17445. }
  17446. /// get a pointer to the value (array)
  17447. array_t* get_impl_ptr(array_t* /*unused*/) noexcept
  17448. {
  17449. return is_array() ? m_value.array : nullptr;
  17450. }
  17451. /// get a pointer to the value (array)
  17452. constexpr const array_t* get_impl_ptr(const array_t* /*unused*/) const noexcept
  17453. {
  17454. return is_array() ? m_value.array : nullptr;
  17455. }
  17456. /// get a pointer to the value (string)
  17457. string_t* get_impl_ptr(string_t* /*unused*/) noexcept
  17458. {
  17459. return is_string() ? m_value.string : nullptr;
  17460. }
  17461. /// get a pointer to the value (string)
  17462. constexpr const string_t* get_impl_ptr(const string_t* /*unused*/) const noexcept
  17463. {
  17464. return is_string() ? m_value.string : nullptr;
  17465. }
  17466. /// get a pointer to the value (boolean)
  17467. boolean_t* get_impl_ptr(boolean_t* /*unused*/) noexcept
  17468. {
  17469. return is_boolean() ? &m_value.boolean : nullptr;
  17470. }
  17471. /// get a pointer to the value (boolean)
  17472. constexpr const boolean_t* get_impl_ptr(const boolean_t* /*unused*/) const noexcept
  17473. {
  17474. return is_boolean() ? &m_value.boolean : nullptr;
  17475. }
  17476. /// get a pointer to the value (integer number)
  17477. number_integer_t* get_impl_ptr(number_integer_t* /*unused*/) noexcept
  17478. {
  17479. return is_number_integer() ? &m_value.number_integer : nullptr;
  17480. }
  17481. /// get a pointer to the value (integer number)
  17482. constexpr const number_integer_t* get_impl_ptr(const number_integer_t* /*unused*/) const noexcept
  17483. {
  17484. return is_number_integer() ? &m_value.number_integer : nullptr;
  17485. }
  17486. /// get a pointer to the value (unsigned number)
  17487. number_unsigned_t* get_impl_ptr(number_unsigned_t* /*unused*/) noexcept
  17488. {
  17489. return is_number_unsigned() ? &m_value.number_unsigned : nullptr;
  17490. }
  17491. /// get a pointer to the value (unsigned number)
  17492. constexpr const number_unsigned_t* get_impl_ptr(const number_unsigned_t* /*unused*/) const noexcept
  17493. {
  17494. return is_number_unsigned() ? &m_value.number_unsigned : nullptr;
  17495. }
  17496. /// get a pointer to the value (floating-point number)
  17497. number_float_t* get_impl_ptr(number_float_t* /*unused*/) noexcept
  17498. {
  17499. return is_number_float() ? &m_value.number_float : nullptr;
  17500. }
  17501. /// get a pointer to the value (floating-point number)
  17502. constexpr const number_float_t* get_impl_ptr(const number_float_t* /*unused*/) const noexcept
  17503. {
  17504. return is_number_float() ? &m_value.number_float : nullptr;
  17505. }
  17506. /// get a pointer to the value (binary)
  17507. binary_t* get_impl_ptr(binary_t* /*unused*/) noexcept
  17508. {
  17509. return is_binary() ? m_value.binary : nullptr;
  17510. }
  17511. /// get a pointer to the value (binary)
  17512. constexpr const binary_t* get_impl_ptr(const binary_t* /*unused*/) const noexcept
  17513. {
  17514. return is_binary() ? m_value.binary : nullptr;
  17515. }
  17516. /*!
  17517. @brief helper function to implement get_ref()
  17518. This function helps to implement get_ref() without code duplication for
  17519. const and non-const overloads
  17520. @tparam ThisType will be deduced as `basic_json` or `const basic_json`
  17521. @throw type_error.303 if ReferenceType does not match underlying value
  17522. type of the current JSON
  17523. */
  17524. template<typename ReferenceType, typename ThisType>
  17525. static ReferenceType get_ref_impl(ThisType& obj)
  17526. {
  17527. // delegate the call to get_ptr<>()
  17528. auto* ptr = obj.template get_ptr<typename std::add_pointer<ReferenceType>::type>();
  17529. if (JSON_HEDLEY_LIKELY(ptr != nullptr))
  17530. {
  17531. return *ptr;
  17532. }
  17533. JSON_THROW(type_error::create(303, "incompatible ReferenceType for get_ref, actual type is " + std::string(obj.type_name()), obj));
  17534. }
  17535. public:
  17536. /// @name value access
  17537. /// Direct access to the stored value of a JSON value.
  17538. /// @{
  17539. /*!
  17540. @brief get a pointer value (implicit)
  17541. Implicit pointer access to the internally stored JSON value. No copies are
  17542. made.
  17543. @warning Writing data to the pointee of the result yields an undefined
  17544. state.
  17545. @tparam PointerType pointer type; must be a pointer to @ref array_t, @ref
  17546. object_t, @ref string_t, @ref boolean_t, @ref number_integer_t,
  17547. @ref number_unsigned_t, or @ref number_float_t. Enforced by a static
  17548. assertion.
  17549. @return pointer to the internally stored JSON value if the requested
  17550. pointer type @a PointerType fits to the JSON value; `nullptr` otherwise
  17551. @complexity Constant.
  17552. @liveexample{The example below shows how pointers to internal values of a
  17553. JSON value can be requested. Note that no type conversions are made and a
  17554. `nullptr` is returned if the value and the requested pointer type does not
  17555. match.,get_ptr}
  17556. @since version 1.0.0
  17557. */
  17558. template<typename PointerType, typename std::enable_if<
  17559. std::is_pointer<PointerType>::value, int>::type = 0>
  17560. auto get_ptr() noexcept -> decltype(std::declval<basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
  17561. {
  17562. // delegate the call to get_impl_ptr<>()
  17563. return get_impl_ptr(static_cast<PointerType>(nullptr));
  17564. }
  17565. /*!
  17566. @brief get a pointer value (implicit)
  17567. @copydoc get_ptr()
  17568. */
  17569. template < typename PointerType, typename std::enable_if <
  17570. std::is_pointer<PointerType>::value&&
  17571. std::is_const<typename std::remove_pointer<PointerType>::type>::value, int >::type = 0 >
  17572. constexpr auto get_ptr() const noexcept -> decltype(std::declval<const basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
  17573. {
  17574. // delegate the call to get_impl_ptr<>() const
  17575. return get_impl_ptr(static_cast<PointerType>(nullptr));
  17576. }
  17577. private:
  17578. /*!
  17579. @brief get a value (explicit)
  17580. Explicit type conversion between the JSON value and a compatible value
  17581. which is [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)
  17582. and [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible).
  17583. The value is converted by calling the @ref json_serializer<ValueType>
  17584. `from_json()` method.
  17585. The function is equivalent to executing
  17586. @code {.cpp}
  17587. ValueType ret;
  17588. JSONSerializer<ValueType>::from_json(*this, ret);
  17589. return ret;
  17590. @endcode
  17591. This overloads is chosen if:
  17592. - @a ValueType is not @ref basic_json,
  17593. - @ref json_serializer<ValueType> has a `from_json()` method of the form
  17594. `void from_json(const basic_json&, ValueType&)`, and
  17595. - @ref json_serializer<ValueType> does not have a `from_json()` method of
  17596. the form `ValueType from_json(const basic_json&)`
  17597. @tparam ValueType the returned value type
  17598. @return copy of the JSON value, converted to @a ValueType
  17599. @throw what @ref json_serializer<ValueType> `from_json()` method throws
  17600. @liveexample{The example below shows several conversions from JSON values
  17601. to other types. There a few things to note: (1) Floating-point numbers can
  17602. be converted to integers\, (2) A JSON array can be converted to a standard
  17603. `std::vector<short>`\, (3) A JSON object can be converted to C++
  17604. associative containers such as `std::unordered_map<std::string\,
  17605. json>`.,get__ValueType_const}
  17606. @since version 2.1.0
  17607. */
  17608. template < typename ValueType,
  17609. detail::enable_if_t <
  17610. detail::is_default_constructible<ValueType>::value&&
  17611. detail::has_from_json<basic_json_t, ValueType>::value,
  17612. int > = 0 >
  17613. ValueType get_impl(detail::priority_tag<0> /*unused*/) const noexcept(noexcept(
  17614. JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), std::declval<ValueType&>())))
  17615. {
  17616. ValueType ret{};
  17617. JSONSerializer<ValueType>::from_json(*this, ret);
  17618. return ret;
  17619. }
  17620. /*!
  17621. @brief get a value (explicit); special case
  17622. Explicit type conversion between the JSON value and a compatible value
  17623. which is **not** [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)
  17624. and **not** [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible).
  17625. The value is converted by calling the @ref json_serializer<ValueType>
  17626. `from_json()` method.
  17627. The function is equivalent to executing
  17628. @code {.cpp}
  17629. return JSONSerializer<ValueType>::from_json(*this);
  17630. @endcode
  17631. This overloads is chosen if:
  17632. - @a ValueType is not @ref basic_json and
  17633. - @ref json_serializer<ValueType> has a `from_json()` method of the form
  17634. `ValueType from_json(const basic_json&)`
  17635. @note If @ref json_serializer<ValueType> has both overloads of
  17636. `from_json()`, this one is chosen.
  17637. @tparam ValueType the returned value type
  17638. @return copy of the JSON value, converted to @a ValueType
  17639. @throw what @ref json_serializer<ValueType> `from_json()` method throws
  17640. @since version 2.1.0
  17641. */
  17642. template < typename ValueType,
  17643. detail::enable_if_t <
  17644. detail::has_non_default_from_json<basic_json_t, ValueType>::value,
  17645. int > = 0 >
  17646. ValueType get_impl(detail::priority_tag<1> /*unused*/) const noexcept(noexcept(
  17647. JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>())))
  17648. {
  17649. return JSONSerializer<ValueType>::from_json(*this);
  17650. }
  17651. /*!
  17652. @brief get special-case overload
  17653. This overloads converts the current @ref basic_json in a different
  17654. @ref basic_json type
  17655. @tparam BasicJsonType == @ref basic_json
  17656. @return a copy of *this, converted into @a BasicJsonType
  17657. @complexity Depending on the implementation of the called `from_json()`
  17658. method.
  17659. @since version 3.2.0
  17660. */
  17661. template < typename BasicJsonType,
  17662. detail::enable_if_t <
  17663. detail::is_basic_json<BasicJsonType>::value,
  17664. int > = 0 >
  17665. BasicJsonType get_impl(detail::priority_tag<2> /*unused*/) const
  17666. {
  17667. return *this;
  17668. }
  17669. /*!
  17670. @brief get special-case overload
  17671. This overloads avoids a lot of template boilerplate, it can be seen as the
  17672. identity method
  17673. @tparam BasicJsonType == @ref basic_json
  17674. @return a copy of *this
  17675. @complexity Constant.
  17676. @since version 2.1.0
  17677. */
  17678. template<typename BasicJsonType,
  17679. detail::enable_if_t<
  17680. std::is_same<BasicJsonType, basic_json_t>::value,
  17681. int> = 0>
  17682. basic_json get_impl(detail::priority_tag<3> /*unused*/) const
  17683. {
  17684. return *this;
  17685. }
  17686. /*!
  17687. @brief get a pointer value (explicit)
  17688. @copydoc get()
  17689. */
  17690. template<typename PointerType,
  17691. detail::enable_if_t<
  17692. std::is_pointer<PointerType>::value,
  17693. int> = 0>
  17694. constexpr auto get_impl(detail::priority_tag<4> /*unused*/) const noexcept
  17695. -> decltype(std::declval<const basic_json_t&>().template get_ptr<PointerType>())
  17696. {
  17697. // delegate the call to get_ptr
  17698. return get_ptr<PointerType>();
  17699. }
  17700. public:
  17701. /*!
  17702. @brief get a (pointer) value (explicit)
  17703. Performs explicit type conversion between the JSON value and a compatible value if required.
  17704. - If the requested type is a pointer to the internally stored JSON value that pointer is returned.
  17705. No copies are made.
  17706. - If the requested type is the current @ref basic_json, or a different @ref basic_json convertible
  17707. from the current @ref basic_json.
  17708. - Otherwise the value is converted by calling the @ref json_serializer<ValueType> `from_json()`
  17709. method.
  17710. @tparam ValueTypeCV the provided value type
  17711. @tparam ValueType the returned value type
  17712. @return copy of the JSON value, converted to @tparam ValueType if necessary
  17713. @throw what @ref json_serializer<ValueType> `from_json()` method throws if conversion is required
  17714. @since version 2.1.0
  17715. */
  17716. template < typename ValueTypeCV, typename ValueType = detail::uncvref_t<ValueTypeCV>>
  17717. #if defined(JSON_HAS_CPP_14)
  17718. constexpr
  17719. #endif
  17720. auto get() const noexcept(
  17721. noexcept(std::declval<const basic_json_t&>().template get_impl<ValueType>(detail::priority_tag<4> {})))
  17722. -> decltype(std::declval<const basic_json_t&>().template get_impl<ValueType>(detail::priority_tag<4> {}))
  17723. {
  17724. // we cannot static_assert on ValueTypeCV being non-const, because
  17725. // there is support for get<const basic_json_t>(), which is why we
  17726. // still need the uncvref
  17727. static_assert(!std::is_reference<ValueTypeCV>::value,
  17728. "get() cannot be used with reference types, you might want to use get_ref()");
  17729. return get_impl<ValueType>(detail::priority_tag<4> {});
  17730. }
  17731. /*!
  17732. @brief get a pointer value (explicit)
  17733. Explicit pointer access to the internally stored JSON value. No copies are
  17734. made.
  17735. @warning The pointer becomes invalid if the underlying JSON object
  17736. changes.
  17737. @tparam PointerType pointer type; must be a pointer to @ref array_t, @ref
  17738. object_t, @ref string_t, @ref boolean_t, @ref number_integer_t,
  17739. @ref number_unsigned_t, or @ref number_float_t.
  17740. @return pointer to the internally stored JSON value if the requested
  17741. pointer type @a PointerType fits to the JSON value; `nullptr` otherwise
  17742. @complexity Constant.
  17743. @liveexample{The example below shows how pointers to internal values of a
  17744. JSON value can be requested. Note that no type conversions are made and a
  17745. `nullptr` is returned if the value and the requested pointer type does not
  17746. match.,get__PointerType}
  17747. @sa see @ref get_ptr() for explicit pointer-member access
  17748. @since version 1.0.0
  17749. */
  17750. template<typename PointerType, typename std::enable_if<
  17751. std::is_pointer<PointerType>::value, int>::type = 0>
  17752. auto get() noexcept -> decltype(std::declval<basic_json_t&>().template get_ptr<PointerType>())
  17753. {
  17754. // delegate the call to get_ptr
  17755. return get_ptr<PointerType>();
  17756. }
  17757. /*!
  17758. @brief get a value (explicit)
  17759. Explicit type conversion between the JSON value and a compatible value.
  17760. The value is filled into the input parameter by calling the @ref json_serializer<ValueType>
  17761. `from_json()` method.
  17762. The function is equivalent to executing
  17763. @code {.cpp}
  17764. ValueType v;
  17765. JSONSerializer<ValueType>::from_json(*this, v);
  17766. @endcode
  17767. This overloads is chosen if:
  17768. - @a ValueType is not @ref basic_json,
  17769. - @ref json_serializer<ValueType> has a `from_json()` method of the form
  17770. `void from_json(const basic_json&, ValueType&)`, and
  17771. @tparam ValueType the input parameter type.
  17772. @return the input parameter, allowing chaining calls.
  17773. @throw what @ref json_serializer<ValueType> `from_json()` method throws
  17774. @liveexample{The example below shows several conversions from JSON values
  17775. to other types. There a few things to note: (1) Floating-point numbers can
  17776. be converted to integers\, (2) A JSON array can be converted to a standard
  17777. `std::vector<short>`\, (3) A JSON object can be converted to C++
  17778. associative containers such as `std::unordered_map<std::string\,
  17779. json>`.,get_to}
  17780. @since version 3.3.0
  17781. */
  17782. template < typename ValueType,
  17783. detail::enable_if_t <
  17784. !detail::is_basic_json<ValueType>::value&&
  17785. detail::has_from_json<basic_json_t, ValueType>::value,
  17786. int > = 0 >
  17787. ValueType & get_to(ValueType& v) const noexcept(noexcept(
  17788. JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), v)))
  17789. {
  17790. JSONSerializer<ValueType>::from_json(*this, v);
  17791. return v;
  17792. }
  17793. // specialization to allow to call get_to with a basic_json value
  17794. // see https://github.com/nlohmann/json/issues/2175
  17795. template<typename ValueType,
  17796. detail::enable_if_t <
  17797. detail::is_basic_json<ValueType>::value,
  17798. int> = 0>
  17799. ValueType & get_to(ValueType& v) const
  17800. {
  17801. v = *this;
  17802. return v;
  17803. }
  17804. template <
  17805. typename T, std::size_t N,
  17806. typename Array = T (&)[N], // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  17807. detail::enable_if_t <
  17808. detail::has_from_json<basic_json_t, Array>::value, int > = 0 >
  17809. Array get_to(T (&v)[N]) const // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  17810. noexcept(noexcept(JSONSerializer<Array>::from_json(
  17811. std::declval<const basic_json_t&>(), v)))
  17812. {
  17813. JSONSerializer<Array>::from_json(*this, v);
  17814. return v;
  17815. }
  17816. /*!
  17817. @brief get a reference value (implicit)
  17818. Implicit reference access to the internally stored JSON value. No copies
  17819. are made.
  17820. @warning Writing data to the referee of the result yields an undefined
  17821. state.
  17822. @tparam ReferenceType reference type; must be a reference to @ref array_t,
  17823. @ref object_t, @ref string_t, @ref boolean_t, @ref number_integer_t, or
  17824. @ref number_float_t. Enforced by static assertion.
  17825. @return reference to the internally stored JSON value if the requested
  17826. reference type @a ReferenceType fits to the JSON value; throws
  17827. type_error.303 otherwise
  17828. @throw type_error.303 in case passed type @a ReferenceType is incompatible
  17829. with the stored JSON value; see example below
  17830. @complexity Constant.
  17831. @liveexample{The example shows several calls to `get_ref()`.,get_ref}
  17832. @since version 1.1.0
  17833. */
  17834. template<typename ReferenceType, typename std::enable_if<
  17835. std::is_reference<ReferenceType>::value, int>::type = 0>
  17836. ReferenceType get_ref()
  17837. {
  17838. // delegate call to get_ref_impl
  17839. return get_ref_impl<ReferenceType>(*this);
  17840. }
  17841. /*!
  17842. @brief get a reference value (implicit)
  17843. @copydoc get_ref()
  17844. */
  17845. template < typename ReferenceType, typename std::enable_if <
  17846. std::is_reference<ReferenceType>::value&&
  17847. std::is_const<typename std::remove_reference<ReferenceType>::type>::value, int >::type = 0 >
  17848. ReferenceType get_ref() const
  17849. {
  17850. // delegate call to get_ref_impl
  17851. return get_ref_impl<ReferenceType>(*this);
  17852. }
  17853. /*!
  17854. @brief get a value (implicit)
  17855. Implicit type conversion between the JSON value and a compatible value.
  17856. The call is realized by calling @ref get() const.
  17857. @tparam ValueType non-pointer type compatible to the JSON value, for
  17858. instance `int` for JSON integer numbers, `bool` for JSON booleans, or
  17859. `std::vector` types for JSON arrays. The character type of @ref string_t
  17860. as well as an initializer list of this type is excluded to avoid
  17861. ambiguities as these types implicitly convert to `std::string`.
  17862. @return copy of the JSON value, converted to type @a ValueType
  17863. @throw type_error.302 in case passed type @a ValueType is incompatible
  17864. to the JSON value type (e.g., the JSON value is of type boolean, but a
  17865. string is requested); see example below
  17866. @complexity Linear in the size of the JSON value.
  17867. @liveexample{The example below shows several conversions from JSON values
  17868. to other types. There a few things to note: (1) Floating-point numbers can
  17869. be converted to integers\, (2) A JSON array can be converted to a standard
  17870. `std::vector<short>`\, (3) A JSON object can be converted to C++
  17871. associative containers such as `std::unordered_map<std::string\,
  17872. json>`.,operator__ValueType}
  17873. @since version 1.0.0
  17874. */
  17875. template < typename ValueType, typename std::enable_if <
  17876. detail::conjunction <
  17877. detail::negation<std::is_pointer<ValueType>>,
  17878. detail::negation<std::is_same<ValueType, detail::json_ref<basic_json>>>,
  17879. detail::negation<std::is_same<ValueType, typename string_t::value_type>>,
  17880. detail::negation<detail::is_basic_json<ValueType>>,
  17881. detail::negation<std::is_same<ValueType, std::initializer_list<typename string_t::value_type>>>,
  17882. #if defined(JSON_HAS_CPP_17) && (defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER <= 1914))
  17883. detail::negation<std::is_same<ValueType, std::string_view>>,
  17884. #endif
  17885. detail::is_detected_lazy<detail::get_template_function, const basic_json_t&, ValueType>
  17886. >::value, int >::type = 0 >
  17887. JSON_EXPLICIT operator ValueType() const
  17888. {
  17889. // delegate the call to get<>() const
  17890. return get<ValueType>();
  17891. }
  17892. /*!
  17893. @return reference to the binary value
  17894. @throw type_error.302 if the value is not binary
  17895. @sa see @ref is_binary() to check if the value is binary
  17896. @since version 3.8.0
  17897. */
  17898. binary_t& get_binary()
  17899. {
  17900. if (!is_binary())
  17901. {
  17902. JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(type_name()), *this));
  17903. }
  17904. return *get_ptr<binary_t*>();
  17905. }
  17906. /// @copydoc get_binary()
  17907. const binary_t& get_binary() const
  17908. {
  17909. if (!is_binary())
  17910. {
  17911. JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(type_name()), *this));
  17912. }
  17913. return *get_ptr<const binary_t*>();
  17914. }
  17915. /// @}
  17916. ////////////////////
  17917. // element access //
  17918. ////////////////////
  17919. /// @name element access
  17920. /// Access to the JSON value.
  17921. /// @{
  17922. /*!
  17923. @brief access specified array element with bounds checking
  17924. Returns a reference to the element at specified location @a idx, with
  17925. bounds checking.
  17926. @param[in] idx index of the element to access
  17927. @return reference to the element at index @a idx
  17928. @throw type_error.304 if the JSON value is not an array; in this case,
  17929. calling `at` with an index makes no sense. See example below.
  17930. @throw out_of_range.401 if the index @a idx is out of range of the array;
  17931. that is, `idx >= size()`. See example below.
  17932. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  17933. changes in the JSON value.
  17934. @complexity Constant.
  17935. @since version 1.0.0
  17936. @liveexample{The example below shows how array elements can be read and
  17937. written using `at()`. It also demonstrates the different exceptions that
  17938. can be thrown.,at__size_type}
  17939. */
  17940. reference at(size_type idx)
  17941. {
  17942. // at only works for arrays
  17943. if (JSON_HEDLEY_LIKELY(is_array()))
  17944. {
  17945. JSON_TRY
  17946. {
  17947. return set_parent(m_value.array->at(idx));
  17948. }
  17949. JSON_CATCH (std::out_of_range&)
  17950. {
  17951. // create better exception explanation
  17952. JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", *this));
  17953. }
  17954. }
  17955. else
  17956. {
  17957. JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), *this));
  17958. }
  17959. }
  17960. /*!
  17961. @brief access specified array element with bounds checking
  17962. Returns a const reference to the element at specified location @a idx,
  17963. with bounds checking.
  17964. @param[in] idx index of the element to access
  17965. @return const reference to the element at index @a idx
  17966. @throw type_error.304 if the JSON value is not an array; in this case,
  17967. calling `at` with an index makes no sense. See example below.
  17968. @throw out_of_range.401 if the index @a idx is out of range of the array;
  17969. that is, `idx >= size()`. See example below.
  17970. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  17971. changes in the JSON value.
  17972. @complexity Constant.
  17973. @since version 1.0.0
  17974. @liveexample{The example below shows how array elements can be read using
  17975. `at()`. It also demonstrates the different exceptions that can be thrown.,
  17976. at__size_type_const}
  17977. */
  17978. const_reference at(size_type idx) const
  17979. {
  17980. // at only works for arrays
  17981. if (JSON_HEDLEY_LIKELY(is_array()))
  17982. {
  17983. JSON_TRY
  17984. {
  17985. return m_value.array->at(idx);
  17986. }
  17987. JSON_CATCH (std::out_of_range&)
  17988. {
  17989. // create better exception explanation
  17990. JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", *this));
  17991. }
  17992. }
  17993. else
  17994. {
  17995. JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), *this));
  17996. }
  17997. }
  17998. /*!
  17999. @brief access specified object element with bounds checking
  18000. Returns a reference to the element at with specified key @a key, with
  18001. bounds checking.
  18002. @param[in] key key of the element to access
  18003. @return reference to the element at key @a key
  18004. @throw type_error.304 if the JSON value is not an object; in this case,
  18005. calling `at` with a key makes no sense. See example below.
  18006. @throw out_of_range.403 if the key @a key is is not stored in the object;
  18007. that is, `find(key) == end()`. See example below.
  18008. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  18009. changes in the JSON value.
  18010. @complexity Logarithmic in the size of the container.
  18011. @sa see @ref operator[](const typename object_t::key_type&) for unchecked
  18012. access by reference
  18013. @sa see @ref value() for access by value with a default value
  18014. @since version 1.0.0
  18015. @liveexample{The example below shows how object elements can be read and
  18016. written using `at()`. It also demonstrates the different exceptions that
  18017. can be thrown.,at__object_t_key_type}
  18018. */
  18019. reference at(const typename object_t::key_type& key)
  18020. {
  18021. // at only works for objects
  18022. if (JSON_HEDLEY_LIKELY(is_object()))
  18023. {
  18024. JSON_TRY
  18025. {
  18026. return set_parent(m_value.object->at(key));
  18027. }
  18028. JSON_CATCH (std::out_of_range&)
  18029. {
  18030. // create better exception explanation
  18031. JSON_THROW(out_of_range::create(403, "key '" + key + "' not found", *this));
  18032. }
  18033. }
  18034. else
  18035. {
  18036. JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), *this));
  18037. }
  18038. }
  18039. /*!
  18040. @brief access specified object element with bounds checking
  18041. Returns a const reference to the element at with specified key @a key,
  18042. with bounds checking.
  18043. @param[in] key key of the element to access
  18044. @return const reference to the element at key @a key
  18045. @throw type_error.304 if the JSON value is not an object; in this case,
  18046. calling `at` with a key makes no sense. See example below.
  18047. @throw out_of_range.403 if the key @a key is is not stored in the object;
  18048. that is, `find(key) == end()`. See example below.
  18049. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  18050. changes in the JSON value.
  18051. @complexity Logarithmic in the size of the container.
  18052. @sa see @ref operator[](const typename object_t::key_type&) for unchecked
  18053. access by reference
  18054. @sa see @ref value() for access by value with a default value
  18055. @since version 1.0.0
  18056. @liveexample{The example below shows how object elements can be read using
  18057. `at()`. It also demonstrates the different exceptions that can be thrown.,
  18058. at__object_t_key_type_const}
  18059. */
  18060. const_reference at(const typename object_t::key_type& key) const
  18061. {
  18062. // at only works for objects
  18063. if (JSON_HEDLEY_LIKELY(is_object()))
  18064. {
  18065. JSON_TRY
  18066. {
  18067. return m_value.object->at(key);
  18068. }
  18069. JSON_CATCH (std::out_of_range&)
  18070. {
  18071. // create better exception explanation
  18072. JSON_THROW(out_of_range::create(403, "key '" + key + "' not found", *this));
  18073. }
  18074. }
  18075. else
  18076. {
  18077. JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), *this));
  18078. }
  18079. }
  18080. /*!
  18081. @brief access specified array element
  18082. Returns a reference to the element at specified location @a idx.
  18083. @note If @a idx is beyond the range of the array (i.e., `idx >= size()`),
  18084. then the array is silently filled up with `null` values to make `idx` a
  18085. valid reference to the last stored element.
  18086. @param[in] idx index of the element to access
  18087. @return reference to the element at index @a idx
  18088. @throw type_error.305 if the JSON value is not an array or null; in that
  18089. cases, using the [] operator with an index makes no sense.
  18090. @complexity Constant if @a idx is in the range of the array. Otherwise
  18091. linear in `idx - size()`.
  18092. @liveexample{The example below shows how array elements can be read and
  18093. written using `[]` operator. Note the addition of `null`
  18094. values.,operatorarray__size_type}
  18095. @since version 1.0.0
  18096. */
  18097. reference operator[](size_type idx)
  18098. {
  18099. // implicitly convert null value to an empty array
  18100. if (is_null())
  18101. {
  18102. m_type = value_t::array;
  18103. m_value.array = create<array_t>();
  18104. assert_invariant();
  18105. }
  18106. // operator[] only works for arrays
  18107. if (JSON_HEDLEY_LIKELY(is_array()))
  18108. {
  18109. // fill up array with null values if given idx is outside range
  18110. if (idx >= m_value.array->size())
  18111. {
  18112. #if JSON_DIAGNOSTICS
  18113. // remember array size before resizing
  18114. const auto previous_size = m_value.array->size();
  18115. #endif
  18116. m_value.array->resize(idx + 1);
  18117. #if JSON_DIAGNOSTICS
  18118. // set parent for values added above
  18119. set_parents(begin() + static_cast<typename iterator::difference_type>(previous_size), static_cast<typename iterator::difference_type>(idx + 1 - previous_size));
  18120. #endif
  18121. }
  18122. return m_value.array->operator[](idx);
  18123. }
  18124. JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()), *this));
  18125. }
  18126. /*!
  18127. @brief access specified array element
  18128. Returns a const reference to the element at specified location @a idx.
  18129. @param[in] idx index of the element to access
  18130. @return const reference to the element at index @a idx
  18131. @throw type_error.305 if the JSON value is not an array; in that case,
  18132. using the [] operator with an index makes no sense.
  18133. @complexity Constant.
  18134. @liveexample{The example below shows how array elements can be read using
  18135. the `[]` operator.,operatorarray__size_type_const}
  18136. @since version 1.0.0
  18137. */
  18138. const_reference operator[](size_type idx) const
  18139. {
  18140. // const operator[] only works for arrays
  18141. if (JSON_HEDLEY_LIKELY(is_array()))
  18142. {
  18143. return m_value.array->operator[](idx);
  18144. }
  18145. JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()), *this));
  18146. }
  18147. /*!
  18148. @brief access specified object element
  18149. Returns a reference to the element at with specified key @a key.
  18150. @note If @a key is not found in the object, then it is silently added to
  18151. the object and filled with a `null` value to make `key` a valid reference.
  18152. In case the value was `null` before, it is converted to an object.
  18153. @param[in] key key of the element to access
  18154. @return reference to the element at key @a key
  18155. @throw type_error.305 if the JSON value is not an object or null; in that
  18156. cases, using the [] operator with a key makes no sense.
  18157. @complexity Logarithmic in the size of the container.
  18158. @liveexample{The example below shows how object elements can be read and
  18159. written using the `[]` operator.,operatorarray__key_type}
  18160. @sa see @ref at(const typename object_t::key_type&) for access by reference
  18161. with range checking
  18162. @sa see @ref value() for access by value with a default value
  18163. @since version 1.0.0
  18164. */
  18165. reference operator[](const typename object_t::key_type& key)
  18166. {
  18167. // implicitly convert null value to an empty object
  18168. if (is_null())
  18169. {
  18170. m_type = value_t::object;
  18171. m_value.object = create<object_t>();
  18172. assert_invariant();
  18173. }
  18174. // operator[] only works for objects
  18175. if (JSON_HEDLEY_LIKELY(is_object()))
  18176. {
  18177. return set_parent(m_value.object->operator[](key));
  18178. }
  18179. JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this));
  18180. }
  18181. /*!
  18182. @brief read-only access specified object element
  18183. Returns a const reference to the element at with specified key @a key. No
  18184. bounds checking is performed.
  18185. @warning If the element with key @a key does not exist, the behavior is
  18186. undefined.
  18187. @param[in] key key of the element to access
  18188. @return const reference to the element at key @a key
  18189. @pre The element with key @a key must exist. **This precondition is
  18190. enforced with an assertion.**
  18191. @throw type_error.305 if the JSON value is not an object; in that case,
  18192. using the [] operator with a key makes no sense.
  18193. @complexity Logarithmic in the size of the container.
  18194. @liveexample{The example below shows how object elements can be read using
  18195. the `[]` operator.,operatorarray__key_type_const}
  18196. @sa see @ref at(const typename object_t::key_type&) for access by reference
  18197. with range checking
  18198. @sa see @ref value() for access by value with a default value
  18199. @since version 1.0.0
  18200. */
  18201. const_reference operator[](const typename object_t::key_type& key) const
  18202. {
  18203. // const operator[] only works for objects
  18204. if (JSON_HEDLEY_LIKELY(is_object()))
  18205. {
  18206. JSON_ASSERT(m_value.object->find(key) != m_value.object->end());
  18207. return m_value.object->find(key)->second;
  18208. }
  18209. JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this));
  18210. }
  18211. /*!
  18212. @brief access specified object element
  18213. Returns a reference to the element at with specified key @a key.
  18214. @note If @a key is not found in the object, then it is silently added to
  18215. the object and filled with a `null` value to make `key` a valid reference.
  18216. In case the value was `null` before, it is converted to an object.
  18217. @param[in] key key of the element to access
  18218. @return reference to the element at key @a key
  18219. @throw type_error.305 if the JSON value is not an object or null; in that
  18220. cases, using the [] operator with a key makes no sense.
  18221. @complexity Logarithmic in the size of the container.
  18222. @liveexample{The example below shows how object elements can be read and
  18223. written using the `[]` operator.,operatorarray__key_type}
  18224. @sa see @ref at(const typename object_t::key_type&) for access by reference
  18225. with range checking
  18226. @sa see @ref value() for access by value with a default value
  18227. @since version 1.1.0
  18228. */
  18229. template<typename T>
  18230. JSON_HEDLEY_NON_NULL(2)
  18231. reference operator[](T* key)
  18232. {
  18233. // implicitly convert null to object
  18234. if (is_null())
  18235. {
  18236. m_type = value_t::object;
  18237. m_value = value_t::object;
  18238. assert_invariant();
  18239. }
  18240. // at only works for objects
  18241. if (JSON_HEDLEY_LIKELY(is_object()))
  18242. {
  18243. return set_parent(m_value.object->operator[](key));
  18244. }
  18245. JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this));
  18246. }
  18247. /*!
  18248. @brief read-only access specified object element
  18249. Returns a const reference to the element at with specified key @a key. No
  18250. bounds checking is performed.
  18251. @warning If the element with key @a key does not exist, the behavior is
  18252. undefined.
  18253. @param[in] key key of the element to access
  18254. @return const reference to the element at key @a key
  18255. @pre The element with key @a key must exist. **This precondition is
  18256. enforced with an assertion.**
  18257. @throw type_error.305 if the JSON value is not an object; in that case,
  18258. using the [] operator with a key makes no sense.
  18259. @complexity Logarithmic in the size of the container.
  18260. @liveexample{The example below shows how object elements can be read using
  18261. the `[]` operator.,operatorarray__key_type_const}
  18262. @sa see @ref at(const typename object_t::key_type&) for access by reference
  18263. with range checking
  18264. @sa see @ref value() for access by value with a default value
  18265. @since version 1.1.0
  18266. */
  18267. template<typename T>
  18268. JSON_HEDLEY_NON_NULL(2)
  18269. const_reference operator[](T* key) const
  18270. {
  18271. // at only works for objects
  18272. if (JSON_HEDLEY_LIKELY(is_object()))
  18273. {
  18274. JSON_ASSERT(m_value.object->find(key) != m_value.object->end());
  18275. return m_value.object->find(key)->second;
  18276. }
  18277. JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this));
  18278. }
  18279. /*!
  18280. @brief access specified object element with default value
  18281. Returns either a copy of an object's element at the specified key @a key
  18282. or a given default value if no element with key @a key exists.
  18283. The function is basically equivalent to executing
  18284. @code {.cpp}
  18285. try {
  18286. return at(key);
  18287. } catch(out_of_range) {
  18288. return default_value;
  18289. }
  18290. @endcode
  18291. @note Unlike @ref at(const typename object_t::key_type&), this function
  18292. does not throw if the given key @a key was not found.
  18293. @note Unlike @ref operator[](const typename object_t::key_type& key), this
  18294. function does not implicitly add an element to the position defined by @a
  18295. key. This function is furthermore also applicable to const objects.
  18296. @param[in] key key of the element to access
  18297. @param[in] default_value the value to return if @a key is not found
  18298. @tparam ValueType type compatible to JSON values, for instance `int` for
  18299. JSON integer numbers, `bool` for JSON booleans, or `std::vector` types for
  18300. JSON arrays. Note the type of the expected value at @a key and the default
  18301. value @a default_value must be compatible.
  18302. @return copy of the element at key @a key or @a default_value if @a key
  18303. is not found
  18304. @throw type_error.302 if @a default_value does not match the type of the
  18305. value at @a key
  18306. @throw type_error.306 if the JSON value is not an object; in that case,
  18307. using `value()` with a key makes no sense.
  18308. @complexity Logarithmic in the size of the container.
  18309. @liveexample{The example below shows how object elements can be queried
  18310. with a default value.,basic_json__value}
  18311. @sa see @ref at(const typename object_t::key_type&) for access by reference
  18312. with range checking
  18313. @sa see @ref operator[](const typename object_t::key_type&) for unchecked
  18314. access by reference
  18315. @since version 1.0.0
  18316. */
  18317. // using std::is_convertible in a std::enable_if will fail when using explicit conversions
  18318. template < class ValueType, typename std::enable_if <
  18319. detail::is_getable<basic_json_t, ValueType>::value
  18320. && !std::is_same<value_t, ValueType>::value, int >::type = 0 >
  18321. ValueType value(const typename object_t::key_type& key, const ValueType& default_value) const
  18322. {
  18323. // at only works for objects
  18324. if (JSON_HEDLEY_LIKELY(is_object()))
  18325. {
  18326. // if key is found, return value and given default value otherwise
  18327. const auto it = find(key);
  18328. if (it != end())
  18329. {
  18330. return it->template get<ValueType>();
  18331. }
  18332. return default_value;
  18333. }
  18334. JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()), *this));
  18335. }
  18336. /*!
  18337. @brief overload for a default value of type const char*
  18338. @copydoc basic_json::value(const typename object_t::key_type&, const ValueType&) const
  18339. */
  18340. string_t value(const typename object_t::key_type& key, const char* default_value) const
  18341. {
  18342. return value(key, string_t(default_value));
  18343. }
  18344. /*!
  18345. @brief access specified object element via JSON Pointer with default value
  18346. Returns either a copy of an object's element at the specified key @a key
  18347. or a given default value if no element with key @a key exists.
  18348. The function is basically equivalent to executing
  18349. @code {.cpp}
  18350. try {
  18351. return at(ptr);
  18352. } catch(out_of_range) {
  18353. return default_value;
  18354. }
  18355. @endcode
  18356. @note Unlike @ref at(const json_pointer&), this function does not throw
  18357. if the given key @a key was not found.
  18358. @param[in] ptr a JSON pointer to the element to access
  18359. @param[in] default_value the value to return if @a ptr found no value
  18360. @tparam ValueType type compatible to JSON values, for instance `int` for
  18361. JSON integer numbers, `bool` for JSON booleans, or `std::vector` types for
  18362. JSON arrays. Note the type of the expected value at @a key and the default
  18363. value @a default_value must be compatible.
  18364. @return copy of the element at key @a key or @a default_value if @a key
  18365. is not found
  18366. @throw type_error.302 if @a default_value does not match the type of the
  18367. value at @a ptr
  18368. @throw type_error.306 if the JSON value is not an object; in that case,
  18369. using `value()` with a key makes no sense.
  18370. @complexity Logarithmic in the size of the container.
  18371. @liveexample{The example below shows how object elements can be queried
  18372. with a default value.,basic_json__value_ptr}
  18373. @sa see @ref operator[](const json_pointer&) for unchecked access by reference
  18374. @since version 2.0.2
  18375. */
  18376. template<class ValueType, typename std::enable_if<
  18377. detail::is_getable<basic_json_t, ValueType>::value, int>::type = 0>
  18378. ValueType value(const json_pointer& ptr, const ValueType& default_value) const
  18379. {
  18380. // at only works for objects
  18381. if (JSON_HEDLEY_LIKELY(is_object()))
  18382. {
  18383. // if pointer resolves a value, return it or use default value
  18384. JSON_TRY
  18385. {
  18386. return ptr.get_checked(this).template get<ValueType>();
  18387. }
  18388. JSON_INTERNAL_CATCH (out_of_range&)
  18389. {
  18390. return default_value;
  18391. }
  18392. }
  18393. JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()), *this));
  18394. }
  18395. /*!
  18396. @brief overload for a default value of type const char*
  18397. @copydoc basic_json::value(const json_pointer&, ValueType) const
  18398. */
  18399. JSON_HEDLEY_NON_NULL(3)
  18400. string_t value(const json_pointer& ptr, const char* default_value) const
  18401. {
  18402. return value(ptr, string_t(default_value));
  18403. }
  18404. /*!
  18405. @brief access the first element
  18406. Returns a reference to the first element in the container. For a JSON
  18407. container `c`, the expression `c.front()` is equivalent to `*c.begin()`.
  18408. @return In case of a structured type (array or object), a reference to the
  18409. first element is returned. In case of number, string, boolean, or binary
  18410. values, a reference to the value is returned.
  18411. @complexity Constant.
  18412. @pre The JSON value must not be `null` (would throw `std::out_of_range`)
  18413. or an empty array or object (undefined behavior, **guarded by
  18414. assertions**).
  18415. @post The JSON value remains unchanged.
  18416. @throw invalid_iterator.214 when called on `null` value
  18417. @liveexample{The following code shows an example for `front()`.,front}
  18418. @sa see @ref back() -- access the last element
  18419. @since version 1.0.0
  18420. */
  18421. reference front()
  18422. {
  18423. return *begin();
  18424. }
  18425. /*!
  18426. @copydoc basic_json::front()
  18427. */
  18428. const_reference front() const
  18429. {
  18430. return *cbegin();
  18431. }
  18432. /*!
  18433. @brief access the last element
  18434. Returns a reference to the last element in the container. For a JSON
  18435. container `c`, the expression `c.back()` is equivalent to
  18436. @code {.cpp}
  18437. auto tmp = c.end();
  18438. --tmp;
  18439. return *tmp;
  18440. @endcode
  18441. @return In case of a structured type (array or object), a reference to the
  18442. last element is returned. In case of number, string, boolean, or binary
  18443. values, a reference to the value is returned.
  18444. @complexity Constant.
  18445. @pre The JSON value must not be `null` (would throw `std::out_of_range`)
  18446. or an empty array or object (undefined behavior, **guarded by
  18447. assertions**).
  18448. @post The JSON value remains unchanged.
  18449. @throw invalid_iterator.214 when called on a `null` value. See example
  18450. below.
  18451. @liveexample{The following code shows an example for `back()`.,back}
  18452. @sa see @ref front() -- access the first element
  18453. @since version 1.0.0
  18454. */
  18455. reference back()
  18456. {
  18457. auto tmp = end();
  18458. --tmp;
  18459. return *tmp;
  18460. }
  18461. /*!
  18462. @copydoc basic_json::back()
  18463. */
  18464. const_reference back() const
  18465. {
  18466. auto tmp = cend();
  18467. --tmp;
  18468. return *tmp;
  18469. }
  18470. /*!
  18471. @brief remove element given an iterator
  18472. Removes the element specified by iterator @a pos. The iterator @a pos must
  18473. be valid and dereferenceable. Thus the `end()` iterator (which is valid,
  18474. but is not dereferenceable) cannot be used as a value for @a pos.
  18475. If called on a primitive type other than `null`, the resulting JSON value
  18476. will be `null`.
  18477. @param[in] pos iterator to the element to remove
  18478. @return Iterator following the last removed element. If the iterator @a
  18479. pos refers to the last element, the `end()` iterator is returned.
  18480. @tparam IteratorType an @ref iterator or @ref const_iterator
  18481. @post Invalidates iterators and references at or after the point of the
  18482. erase, including the `end()` iterator.
  18483. @throw type_error.307 if called on a `null` value; example: `"cannot use
  18484. erase() with null"`
  18485. @throw invalid_iterator.202 if called on an iterator which does not belong
  18486. to the current JSON value; example: `"iterator does not fit current
  18487. value"`
  18488. @throw invalid_iterator.205 if called on a primitive type with invalid
  18489. iterator (i.e., any iterator which is not `begin()`); example: `"iterator
  18490. out of range"`
  18491. @complexity The complexity depends on the type:
  18492. - objects: amortized constant
  18493. - arrays: linear in distance between @a pos and the end of the container
  18494. - strings and binary: linear in the length of the member
  18495. - other types: constant
  18496. @liveexample{The example shows the result of `erase()` for different JSON
  18497. types.,erase__IteratorType}
  18498. @sa see @ref erase(IteratorType, IteratorType) -- removes the elements in
  18499. the given range
  18500. @sa see @ref erase(const typename object_t::key_type&) -- removes the element
  18501. from an object at the given key
  18502. @sa see @ref erase(const size_type) -- removes the element from an array at
  18503. the given index
  18504. @since version 1.0.0
  18505. */
  18506. template < class IteratorType, typename std::enable_if <
  18507. std::is_same<IteratorType, typename basic_json_t::iterator>::value ||
  18508. std::is_same<IteratorType, typename basic_json_t::const_iterator>::value, int >::type
  18509. = 0 >
  18510. IteratorType erase(IteratorType pos)
  18511. {
  18512. // make sure iterator fits the current value
  18513. if (JSON_HEDLEY_UNLIKELY(this != pos.m_object))
  18514. {
  18515. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", *this));
  18516. }
  18517. IteratorType result = end();
  18518. switch (m_type)
  18519. {
  18520. case value_t::boolean:
  18521. case value_t::number_float:
  18522. case value_t::number_integer:
  18523. case value_t::number_unsigned:
  18524. case value_t::string:
  18525. case value_t::binary:
  18526. {
  18527. if (JSON_HEDLEY_UNLIKELY(!pos.m_it.primitive_iterator.is_begin()))
  18528. {
  18529. JSON_THROW(invalid_iterator::create(205, "iterator out of range", *this));
  18530. }
  18531. if (is_string())
  18532. {
  18533. AllocatorType<string_t> alloc;
  18534. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.string);
  18535. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.string, 1);
  18536. m_value.string = nullptr;
  18537. }
  18538. else if (is_binary())
  18539. {
  18540. AllocatorType<binary_t> alloc;
  18541. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.binary);
  18542. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.binary, 1);
  18543. m_value.binary = nullptr;
  18544. }
  18545. m_type = value_t::null;
  18546. assert_invariant();
  18547. break;
  18548. }
  18549. case value_t::object:
  18550. {
  18551. result.m_it.object_iterator = m_value.object->erase(pos.m_it.object_iterator);
  18552. break;
  18553. }
  18554. case value_t::array:
  18555. {
  18556. result.m_it.array_iterator = m_value.array->erase(pos.m_it.array_iterator);
  18557. break;
  18558. }
  18559. case value_t::null:
  18560. case value_t::discarded:
  18561. default:
  18562. JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), *this));
  18563. }
  18564. return result;
  18565. }
  18566. /*!
  18567. @brief remove elements given an iterator range
  18568. Removes the element specified by the range `[first; last)`. The iterator
  18569. @a first does not need to be dereferenceable if `first == last`: erasing
  18570. an empty range is a no-op.
  18571. If called on a primitive type other than `null`, the resulting JSON value
  18572. will be `null`.
  18573. @param[in] first iterator to the beginning of the range to remove
  18574. @param[in] last iterator past the end of the range to remove
  18575. @return Iterator following the last removed element. If the iterator @a
  18576. second refers to the last element, the `end()` iterator is returned.
  18577. @tparam IteratorType an @ref iterator or @ref const_iterator
  18578. @post Invalidates iterators and references at or after the point of the
  18579. erase, including the `end()` iterator.
  18580. @throw type_error.307 if called on a `null` value; example: `"cannot use
  18581. erase() with null"`
  18582. @throw invalid_iterator.203 if called on iterators which does not belong
  18583. to the current JSON value; example: `"iterators do not fit current value"`
  18584. @throw invalid_iterator.204 if called on a primitive type with invalid
  18585. iterators (i.e., if `first != begin()` and `last != end()`); example:
  18586. `"iterators out of range"`
  18587. @complexity The complexity depends on the type:
  18588. - objects: `log(size()) + std::distance(first, last)`
  18589. - arrays: linear in the distance between @a first and @a last, plus linear
  18590. in the distance between @a last and end of the container
  18591. - strings and binary: linear in the length of the member
  18592. - other types: constant
  18593. @liveexample{The example shows the result of `erase()` for different JSON
  18594. types.,erase__IteratorType_IteratorType}
  18595. @sa see @ref erase(IteratorType) -- removes the element at a given position
  18596. @sa see @ref erase(const typename object_t::key_type&) -- removes the element
  18597. from an object at the given key
  18598. @sa see @ref erase(const size_type) -- removes the element from an array at
  18599. the given index
  18600. @since version 1.0.0
  18601. */
  18602. template < class IteratorType, typename std::enable_if <
  18603. std::is_same<IteratorType, typename basic_json_t::iterator>::value ||
  18604. std::is_same<IteratorType, typename basic_json_t::const_iterator>::value, int >::type
  18605. = 0 >
  18606. IteratorType erase(IteratorType first, IteratorType last)
  18607. {
  18608. // make sure iterator fits the current value
  18609. if (JSON_HEDLEY_UNLIKELY(this != first.m_object || this != last.m_object))
  18610. {
  18611. JSON_THROW(invalid_iterator::create(203, "iterators do not fit current value", *this));
  18612. }
  18613. IteratorType result = end();
  18614. switch (m_type)
  18615. {
  18616. case value_t::boolean:
  18617. case value_t::number_float:
  18618. case value_t::number_integer:
  18619. case value_t::number_unsigned:
  18620. case value_t::string:
  18621. case value_t::binary:
  18622. {
  18623. if (JSON_HEDLEY_LIKELY(!first.m_it.primitive_iterator.is_begin()
  18624. || !last.m_it.primitive_iterator.is_end()))
  18625. {
  18626. JSON_THROW(invalid_iterator::create(204, "iterators out of range", *this));
  18627. }
  18628. if (is_string())
  18629. {
  18630. AllocatorType<string_t> alloc;
  18631. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.string);
  18632. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.string, 1);
  18633. m_value.string = nullptr;
  18634. }
  18635. else if (is_binary())
  18636. {
  18637. AllocatorType<binary_t> alloc;
  18638. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.binary);
  18639. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.binary, 1);
  18640. m_value.binary = nullptr;
  18641. }
  18642. m_type = value_t::null;
  18643. assert_invariant();
  18644. break;
  18645. }
  18646. case value_t::object:
  18647. {
  18648. result.m_it.object_iterator = m_value.object->erase(first.m_it.object_iterator,
  18649. last.m_it.object_iterator);
  18650. break;
  18651. }
  18652. case value_t::array:
  18653. {
  18654. result.m_it.array_iterator = m_value.array->erase(first.m_it.array_iterator,
  18655. last.m_it.array_iterator);
  18656. break;
  18657. }
  18658. case value_t::null:
  18659. case value_t::discarded:
  18660. default:
  18661. JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), *this));
  18662. }
  18663. return result;
  18664. }
  18665. /*!
  18666. @brief remove element from a JSON object given a key
  18667. Removes elements from a JSON object with the key value @a key.
  18668. @param[in] key value of the elements to remove
  18669. @return Number of elements removed. If @a ObjectType is the default
  18670. `std::map` type, the return value will always be `0` (@a key was not
  18671. found) or `1` (@a key was found).
  18672. @post References and iterators to the erased elements are invalidated.
  18673. Other references and iterators are not affected.
  18674. @throw type_error.307 when called on a type other than JSON object;
  18675. example: `"cannot use erase() with null"`
  18676. @complexity `log(size()) + count(key)`
  18677. @liveexample{The example shows the effect of `erase()`.,erase__key_type}
  18678. @sa see @ref erase(IteratorType) -- removes the element at a given position
  18679. @sa see @ref erase(IteratorType, IteratorType) -- removes the elements in
  18680. the given range
  18681. @sa see @ref erase(const size_type) -- removes the element from an array at
  18682. the given index
  18683. @since version 1.0.0
  18684. */
  18685. size_type erase(const typename object_t::key_type& key)
  18686. {
  18687. // this erase only works for objects
  18688. if (JSON_HEDLEY_LIKELY(is_object()))
  18689. {
  18690. return m_value.object->erase(key);
  18691. }
  18692. JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), *this));
  18693. }
  18694. /*!
  18695. @brief remove element from a JSON array given an index
  18696. Removes element from a JSON array at the index @a idx.
  18697. @param[in] idx index of the element to remove
  18698. @throw type_error.307 when called on a type other than JSON object;
  18699. example: `"cannot use erase() with null"`
  18700. @throw out_of_range.401 when `idx >= size()`; example: `"array index 17
  18701. is out of range"`
  18702. @complexity Linear in distance between @a idx and the end of the container.
  18703. @liveexample{The example shows the effect of `erase()`.,erase__size_type}
  18704. @sa see @ref erase(IteratorType) -- removes the element at a given position
  18705. @sa see @ref erase(IteratorType, IteratorType) -- removes the elements in
  18706. the given range
  18707. @sa see @ref erase(const typename object_t::key_type&) -- removes the element
  18708. from an object at the given key
  18709. @since version 1.0.0
  18710. */
  18711. void erase(const size_type idx)
  18712. {
  18713. // this erase only works for arrays
  18714. if (JSON_HEDLEY_LIKELY(is_array()))
  18715. {
  18716. if (JSON_HEDLEY_UNLIKELY(idx >= size()))
  18717. {
  18718. JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", *this));
  18719. }
  18720. m_value.array->erase(m_value.array->begin() + static_cast<difference_type>(idx));
  18721. }
  18722. else
  18723. {
  18724. JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), *this));
  18725. }
  18726. }
  18727. /// @}
  18728. ////////////
  18729. // lookup //
  18730. ////////////
  18731. /// @name lookup
  18732. /// @{
  18733. /*!
  18734. @brief find an element in a JSON object
  18735. Finds an element in a JSON object with key equivalent to @a key. If the
  18736. element is not found or the JSON value is not an object, end() is
  18737. returned.
  18738. @note This method always returns @ref end() when executed on a JSON type
  18739. that is not an object.
  18740. @param[in] key key value of the element to search for.
  18741. @return Iterator to an element with key equivalent to @a key. If no such
  18742. element is found or the JSON value is not an object, past-the-end (see
  18743. @ref end()) iterator is returned.
  18744. @complexity Logarithmic in the size of the JSON object.
  18745. @liveexample{The example shows how `find()` is used.,find__key_type}
  18746. @sa see @ref contains(KeyT&&) const -- checks whether a key exists
  18747. @since version 1.0.0
  18748. */
  18749. template<typename KeyT>
  18750. iterator find(KeyT&& key)
  18751. {
  18752. auto result = end();
  18753. if (is_object())
  18754. {
  18755. result.m_it.object_iterator = m_value.object->find(std::forward<KeyT>(key));
  18756. }
  18757. return result;
  18758. }
  18759. /*!
  18760. @brief find an element in a JSON object
  18761. @copydoc find(KeyT&&)
  18762. */
  18763. template<typename KeyT>
  18764. const_iterator find(KeyT&& key) const
  18765. {
  18766. auto result = cend();
  18767. if (is_object())
  18768. {
  18769. result.m_it.object_iterator = m_value.object->find(std::forward<KeyT>(key));
  18770. }
  18771. return result;
  18772. }
  18773. /*!
  18774. @brief returns the number of occurrences of a key in a JSON object
  18775. Returns the number of elements with key @a key. If ObjectType is the
  18776. default `std::map` type, the return value will always be `0` (@a key was
  18777. not found) or `1` (@a key was found).
  18778. @note This method always returns `0` when executed on a JSON type that is
  18779. not an object.
  18780. @param[in] key key value of the element to count
  18781. @return Number of elements with key @a key. If the JSON value is not an
  18782. object, the return value will be `0`.
  18783. @complexity Logarithmic in the size of the JSON object.
  18784. @liveexample{The example shows how `count()` is used.,count}
  18785. @since version 1.0.0
  18786. */
  18787. template<typename KeyT>
  18788. size_type count(KeyT&& key) const
  18789. {
  18790. // return 0 for all nonobject types
  18791. return is_object() ? m_value.object->count(std::forward<KeyT>(key)) : 0;
  18792. }
  18793. /*!
  18794. @brief check the existence of an element in a JSON object
  18795. Check whether an element exists in a JSON object with key equivalent to
  18796. @a key. If the element is not found or the JSON value is not an object,
  18797. false is returned.
  18798. @note This method always returns false when executed on a JSON type
  18799. that is not an object.
  18800. @param[in] key key value to check its existence.
  18801. @return true if an element with specified @a key exists. If no such
  18802. element with such key is found or the JSON value is not an object,
  18803. false is returned.
  18804. @complexity Logarithmic in the size of the JSON object.
  18805. @liveexample{The following code shows an example for `contains()`.,contains}
  18806. @sa see @ref find(KeyT&&) -- returns an iterator to an object element
  18807. @sa see @ref contains(const json_pointer&) const -- checks the existence for a JSON pointer
  18808. @since version 3.6.0
  18809. */
  18810. template < typename KeyT, typename std::enable_if <
  18811. !std::is_same<typename std::decay<KeyT>::type, json_pointer>::value, int >::type = 0 >
  18812. bool contains(KeyT && key) const
  18813. {
  18814. return is_object() && m_value.object->find(std::forward<KeyT>(key)) != m_value.object->end();
  18815. }
  18816. /*!
  18817. @brief check the existence of an element in a JSON object given a JSON pointer
  18818. Check whether the given JSON pointer @a ptr can be resolved in the current
  18819. JSON value.
  18820. @note This method can be executed on any JSON value type.
  18821. @param[in] ptr JSON pointer to check its existence.
  18822. @return true if the JSON pointer can be resolved to a stored value, false
  18823. otherwise.
  18824. @post If `j.contains(ptr)` returns true, it is safe to call `j[ptr]`.
  18825. @throw parse_error.106 if an array index begins with '0'
  18826. @throw parse_error.109 if an array index was not a number
  18827. @complexity Logarithmic in the size of the JSON object.
  18828. @liveexample{The following code shows an example for `contains()`.,contains_json_pointer}
  18829. @sa see @ref contains(KeyT &&) const -- checks the existence of a key
  18830. @since version 3.7.0
  18831. */
  18832. bool contains(const json_pointer& ptr) const
  18833. {
  18834. return ptr.contains(this);
  18835. }
  18836. /// @}
  18837. ///////////////
  18838. // iterators //
  18839. ///////////////
  18840. /// @name iterators
  18841. /// @{
  18842. /*!
  18843. @brief returns an iterator to the first element
  18844. Returns an iterator to the first element.
  18845. @image html range-begin-end.svg "Illustration from cppreference.com"
  18846. @return iterator to the first element
  18847. @complexity Constant.
  18848. @requirement This function helps `basic_json` satisfying the
  18849. [Container](https://en.cppreference.com/w/cpp/named_req/Container)
  18850. requirements:
  18851. - The complexity is constant.
  18852. @liveexample{The following code shows an example for `begin()`.,begin}
  18853. @sa see @ref cbegin() -- returns a const iterator to the beginning
  18854. @sa see @ref end() -- returns an iterator to the end
  18855. @sa see @ref cend() -- returns a const iterator to the end
  18856. @since version 1.0.0
  18857. */
  18858. iterator begin() noexcept
  18859. {
  18860. iterator result(this);
  18861. result.set_begin();
  18862. return result;
  18863. }
  18864. /*!
  18865. @copydoc basic_json::cbegin()
  18866. */
  18867. const_iterator begin() const noexcept
  18868. {
  18869. return cbegin();
  18870. }
  18871. /*!
  18872. @brief returns a const iterator to the first element
  18873. Returns a const iterator to the first element.
  18874. @image html range-begin-end.svg "Illustration from cppreference.com"
  18875. @return const iterator to the first element
  18876. @complexity Constant.
  18877. @requirement This function helps `basic_json` satisfying the
  18878. [Container](https://en.cppreference.com/w/cpp/named_req/Container)
  18879. requirements:
  18880. - The complexity is constant.
  18881. - Has the semantics of `const_cast<const basic_json&>(*this).begin()`.
  18882. @liveexample{The following code shows an example for `cbegin()`.,cbegin}
  18883. @sa see @ref begin() -- returns an iterator to the beginning
  18884. @sa see @ref end() -- returns an iterator to the end
  18885. @sa see @ref cend() -- returns a const iterator to the end
  18886. @since version 1.0.0
  18887. */
  18888. const_iterator cbegin() const noexcept
  18889. {
  18890. const_iterator result(this);
  18891. result.set_begin();
  18892. return result;
  18893. }
  18894. /*!
  18895. @brief returns an iterator to one past the last element
  18896. Returns an iterator to one past the last element.
  18897. @image html range-begin-end.svg "Illustration from cppreference.com"
  18898. @return iterator one past the last element
  18899. @complexity Constant.
  18900. @requirement This function helps `basic_json` satisfying the
  18901. [Container](https://en.cppreference.com/w/cpp/named_req/Container)
  18902. requirements:
  18903. - The complexity is constant.
  18904. @liveexample{The following code shows an example for `end()`.,end}
  18905. @sa see @ref cend() -- returns a const iterator to the end
  18906. @sa see @ref begin() -- returns an iterator to the beginning
  18907. @sa see @ref cbegin() -- returns a const iterator to the beginning
  18908. @since version 1.0.0
  18909. */
  18910. iterator end() noexcept
  18911. {
  18912. iterator result(this);
  18913. result.set_end();
  18914. return result;
  18915. }
  18916. /*!
  18917. @copydoc basic_json::cend()
  18918. */
  18919. const_iterator end() const noexcept
  18920. {
  18921. return cend();
  18922. }
  18923. /*!
  18924. @brief returns a const iterator to one past the last element
  18925. Returns a const iterator to one past the last element.
  18926. @image html range-begin-end.svg "Illustration from cppreference.com"
  18927. @return const iterator one past the last element
  18928. @complexity Constant.
  18929. @requirement This function helps `basic_json` satisfying the
  18930. [Container](https://en.cppreference.com/w/cpp/named_req/Container)
  18931. requirements:
  18932. - The complexity is constant.
  18933. - Has the semantics of `const_cast<const basic_json&>(*this).end()`.
  18934. @liveexample{The following code shows an example for `cend()`.,cend}
  18935. @sa see @ref end() -- returns an iterator to the end
  18936. @sa see @ref begin() -- returns an iterator to the beginning
  18937. @sa see @ref cbegin() -- returns a const iterator to the beginning
  18938. @since version 1.0.0
  18939. */
  18940. const_iterator cend() const noexcept
  18941. {
  18942. const_iterator result(this);
  18943. result.set_end();
  18944. return result;
  18945. }
  18946. /*!
  18947. @brief returns an iterator to the reverse-beginning
  18948. Returns an iterator to the reverse-beginning; that is, the last element.
  18949. @image html range-rbegin-rend.svg "Illustration from cppreference.com"
  18950. @complexity Constant.
  18951. @requirement This function helps `basic_json` satisfying the
  18952. [ReversibleContainer](https://en.cppreference.com/w/cpp/named_req/ReversibleContainer)
  18953. requirements:
  18954. - The complexity is constant.
  18955. - Has the semantics of `reverse_iterator(end())`.
  18956. @liveexample{The following code shows an example for `rbegin()`.,rbegin}
  18957. @sa see @ref crbegin() -- returns a const reverse iterator to the beginning
  18958. @sa see @ref rend() -- returns a reverse iterator to the end
  18959. @sa see @ref crend() -- returns a const reverse iterator to the end
  18960. @since version 1.0.0
  18961. */
  18962. reverse_iterator rbegin() noexcept
  18963. {
  18964. return reverse_iterator(end());
  18965. }
  18966. /*!
  18967. @copydoc basic_json::crbegin()
  18968. */
  18969. const_reverse_iterator rbegin() const noexcept
  18970. {
  18971. return crbegin();
  18972. }
  18973. /*!
  18974. @brief returns an iterator to the reverse-end
  18975. Returns an iterator to the reverse-end; that is, one before the first
  18976. element.
  18977. @image html range-rbegin-rend.svg "Illustration from cppreference.com"
  18978. @complexity Constant.
  18979. @requirement This function helps `basic_json` satisfying the
  18980. [ReversibleContainer](https://en.cppreference.com/w/cpp/named_req/ReversibleContainer)
  18981. requirements:
  18982. - The complexity is constant.
  18983. - Has the semantics of `reverse_iterator(begin())`.
  18984. @liveexample{The following code shows an example for `rend()`.,rend}
  18985. @sa see @ref crend() -- returns a const reverse iterator to the end
  18986. @sa see @ref rbegin() -- returns a reverse iterator to the beginning
  18987. @sa see @ref crbegin() -- returns a const reverse iterator to the beginning
  18988. @since version 1.0.0
  18989. */
  18990. reverse_iterator rend() noexcept
  18991. {
  18992. return reverse_iterator(begin());
  18993. }
  18994. /*!
  18995. @copydoc basic_json::crend()
  18996. */
  18997. const_reverse_iterator rend() const noexcept
  18998. {
  18999. return crend();
  19000. }
  19001. /*!
  19002. @brief returns a const reverse iterator to the last element
  19003. Returns a const iterator to the reverse-beginning; that is, the last
  19004. element.
  19005. @image html range-rbegin-rend.svg "Illustration from cppreference.com"
  19006. @complexity Constant.
  19007. @requirement This function helps `basic_json` satisfying the
  19008. [ReversibleContainer](https://en.cppreference.com/w/cpp/named_req/ReversibleContainer)
  19009. requirements:
  19010. - The complexity is constant.
  19011. - Has the semantics of `const_cast<const basic_json&>(*this).rbegin()`.
  19012. @liveexample{The following code shows an example for `crbegin()`.,crbegin}
  19013. @sa see @ref rbegin() -- returns a reverse iterator to the beginning
  19014. @sa see @ref rend() -- returns a reverse iterator to the end
  19015. @sa see @ref crend() -- returns a const reverse iterator to the end
  19016. @since version 1.0.0
  19017. */
  19018. const_reverse_iterator crbegin() const noexcept
  19019. {
  19020. return const_reverse_iterator(cend());
  19021. }
  19022. /*!
  19023. @brief returns a const reverse iterator to one before the first
  19024. Returns a const reverse iterator to the reverse-end; that is, one before
  19025. the first element.
  19026. @image html range-rbegin-rend.svg "Illustration from cppreference.com"
  19027. @complexity Constant.
  19028. @requirement This function helps `basic_json` satisfying the
  19029. [ReversibleContainer](https://en.cppreference.com/w/cpp/named_req/ReversibleContainer)
  19030. requirements:
  19031. - The complexity is constant.
  19032. - Has the semantics of `const_cast<const basic_json&>(*this).rend()`.
  19033. @liveexample{The following code shows an example for `crend()`.,crend}
  19034. @sa see @ref rend() -- returns a reverse iterator to the end
  19035. @sa see @ref rbegin() -- returns a reverse iterator to the beginning
  19036. @sa see @ref crbegin() -- returns a const reverse iterator to the beginning
  19037. @since version 1.0.0
  19038. */
  19039. const_reverse_iterator crend() const noexcept
  19040. {
  19041. return const_reverse_iterator(cbegin());
  19042. }
  19043. public:
  19044. /*!
  19045. @brief wrapper to access iterator member functions in range-based for
  19046. This function allows to access @ref iterator::key() and @ref
  19047. iterator::value() during range-based for loops. In these loops, a
  19048. reference to the JSON values is returned, so there is no access to the
  19049. underlying iterator.
  19050. For loop without iterator_wrapper:
  19051. @code{cpp}
  19052. for (auto it = j_object.begin(); it != j_object.end(); ++it)
  19053. {
  19054. std::cout << "key: " << it.key() << ", value:" << it.value() << '\n';
  19055. }
  19056. @endcode
  19057. Range-based for loop without iterator proxy:
  19058. @code{cpp}
  19059. for (auto it : j_object)
  19060. {
  19061. // "it" is of type json::reference and has no key() member
  19062. std::cout << "value: " << it << '\n';
  19063. }
  19064. @endcode
  19065. Range-based for loop with iterator proxy:
  19066. @code{cpp}
  19067. for (auto it : json::iterator_wrapper(j_object))
  19068. {
  19069. std::cout << "key: " << it.key() << ", value:" << it.value() << '\n';
  19070. }
  19071. @endcode
  19072. @note When iterating over an array, `key()` will return the index of the
  19073. element as string (see example).
  19074. @param[in] ref reference to a JSON value
  19075. @return iteration proxy object wrapping @a ref with an interface to use in
  19076. range-based for loops
  19077. @liveexample{The following code shows how the wrapper is used,iterator_wrapper}
  19078. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  19079. changes in the JSON value.
  19080. @complexity Constant.
  19081. @note The name of this function is not yet final and may change in the
  19082. future.
  19083. @deprecated This stream operator is deprecated and will be removed in
  19084. future 4.0.0 of the library. Please use @ref items() instead;
  19085. that is, replace `json::iterator_wrapper(j)` with `j.items()`.
  19086. */
  19087. JSON_HEDLEY_DEPRECATED_FOR(3.1.0, items())
  19088. static iteration_proxy<iterator> iterator_wrapper(reference ref) noexcept
  19089. {
  19090. return ref.items();
  19091. }
  19092. /*!
  19093. @copydoc iterator_wrapper(reference)
  19094. */
  19095. JSON_HEDLEY_DEPRECATED_FOR(3.1.0, items())
  19096. static iteration_proxy<const_iterator> iterator_wrapper(const_reference ref) noexcept
  19097. {
  19098. return ref.items();
  19099. }
  19100. /*!
  19101. @brief helper to access iterator member functions in range-based for
  19102. This function allows to access @ref iterator::key() and @ref
  19103. iterator::value() during range-based for loops. In these loops, a
  19104. reference to the JSON values is returned, so there is no access to the
  19105. underlying iterator.
  19106. For loop without `items()` function:
  19107. @code{cpp}
  19108. for (auto it = j_object.begin(); it != j_object.end(); ++it)
  19109. {
  19110. std::cout << "key: " << it.key() << ", value:" << it.value() << '\n';
  19111. }
  19112. @endcode
  19113. Range-based for loop without `items()` function:
  19114. @code{cpp}
  19115. for (auto it : j_object)
  19116. {
  19117. // "it" is of type json::reference and has no key() member
  19118. std::cout << "value: " << it << '\n';
  19119. }
  19120. @endcode
  19121. Range-based for loop with `items()` function:
  19122. @code{cpp}
  19123. for (auto& el : j_object.items())
  19124. {
  19125. std::cout << "key: " << el.key() << ", value:" << el.value() << '\n';
  19126. }
  19127. @endcode
  19128. The `items()` function also allows to use
  19129. [structured bindings](https://en.cppreference.com/w/cpp/language/structured_binding)
  19130. (C++17):
  19131. @code{cpp}
  19132. for (auto& [key, val] : j_object.items())
  19133. {
  19134. std::cout << "key: " << key << ", value:" << val << '\n';
  19135. }
  19136. @endcode
  19137. @note When iterating over an array, `key()` will return the index of the
  19138. element as string (see example). For primitive types (e.g., numbers),
  19139. `key()` returns an empty string.
  19140. @warning Using `items()` on temporary objects is dangerous. Make sure the
  19141. object's lifetime exeeds the iteration. See
  19142. <https://github.com/nlohmann/json/issues/2040> for more
  19143. information.
  19144. @return iteration proxy object wrapping @a ref with an interface to use in
  19145. range-based for loops
  19146. @liveexample{The following code shows how the function is used.,items}
  19147. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  19148. changes in the JSON value.
  19149. @complexity Constant.
  19150. @since version 3.1.0, structured bindings support since 3.5.0.
  19151. */
  19152. iteration_proxy<iterator> items() noexcept
  19153. {
  19154. return iteration_proxy<iterator>(*this);
  19155. }
  19156. /*!
  19157. @copydoc items()
  19158. */
  19159. iteration_proxy<const_iterator> items() const noexcept
  19160. {
  19161. return iteration_proxy<const_iterator>(*this);
  19162. }
  19163. /// @}
  19164. //////////////
  19165. // capacity //
  19166. //////////////
  19167. /// @name capacity
  19168. /// @{
  19169. /*!
  19170. @brief checks whether the container is empty.
  19171. Checks if a JSON value has no elements (i.e. whether its @ref size is `0`).
  19172. @return The return value depends on the different types and is
  19173. defined as follows:
  19174. Value type | return value
  19175. ----------- | -------------
  19176. null | `true`
  19177. boolean | `false`
  19178. string | `false`
  19179. number | `false`
  19180. binary | `false`
  19181. object | result of function `object_t::empty()`
  19182. array | result of function `array_t::empty()`
  19183. @liveexample{The following code uses `empty()` to check if a JSON
  19184. object contains any elements.,empty}
  19185. @complexity Constant, as long as @ref array_t and @ref object_t satisfy
  19186. the Container concept; that is, their `empty()` functions have constant
  19187. complexity.
  19188. @iterators No changes.
  19189. @exceptionsafety No-throw guarantee: this function never throws exceptions.
  19190. @note This function does not return whether a string stored as JSON value
  19191. is empty - it returns whether the JSON container itself is empty which is
  19192. false in the case of a string.
  19193. @requirement This function helps `basic_json` satisfying the
  19194. [Container](https://en.cppreference.com/w/cpp/named_req/Container)
  19195. requirements:
  19196. - The complexity is constant.
  19197. - Has the semantics of `begin() == end()`.
  19198. @sa see @ref size() -- returns the number of elements
  19199. @since version 1.0.0
  19200. */
  19201. bool empty() const noexcept
  19202. {
  19203. switch (m_type)
  19204. {
  19205. case value_t::null:
  19206. {
  19207. // null values are empty
  19208. return true;
  19209. }
  19210. case value_t::array:
  19211. {
  19212. // delegate call to array_t::empty()
  19213. return m_value.array->empty();
  19214. }
  19215. case value_t::object:
  19216. {
  19217. // delegate call to object_t::empty()
  19218. return m_value.object->empty();
  19219. }
  19220. case value_t::string:
  19221. case value_t::boolean:
  19222. case value_t::number_integer:
  19223. case value_t::number_unsigned:
  19224. case value_t::number_float:
  19225. case value_t::binary:
  19226. case value_t::discarded:
  19227. default:
  19228. {
  19229. // all other types are nonempty
  19230. return false;
  19231. }
  19232. }
  19233. }
  19234. /*!
  19235. @brief returns the number of elements
  19236. Returns the number of elements in a JSON value.
  19237. @return The return value depends on the different types and is
  19238. defined as follows:
  19239. Value type | return value
  19240. ----------- | -------------
  19241. null | `0`
  19242. boolean | `1`
  19243. string | `1`
  19244. number | `1`
  19245. binary | `1`
  19246. object | result of function object_t::size()
  19247. array | result of function array_t::size()
  19248. @liveexample{The following code calls `size()` on the different value
  19249. types.,size}
  19250. @complexity Constant, as long as @ref array_t and @ref object_t satisfy
  19251. the Container concept; that is, their size() functions have constant
  19252. complexity.
  19253. @iterators No changes.
  19254. @exceptionsafety No-throw guarantee: this function never throws exceptions.
  19255. @note This function does not return the length of a string stored as JSON
  19256. value - it returns the number of elements in the JSON value which is 1 in
  19257. the case of a string.
  19258. @requirement This function helps `basic_json` satisfying the
  19259. [Container](https://en.cppreference.com/w/cpp/named_req/Container)
  19260. requirements:
  19261. - The complexity is constant.
  19262. - Has the semantics of `std::distance(begin(), end())`.
  19263. @sa see @ref empty() -- checks whether the container is empty
  19264. @sa see @ref max_size() -- returns the maximal number of elements
  19265. @since version 1.0.0
  19266. */
  19267. size_type size() const noexcept
  19268. {
  19269. switch (m_type)
  19270. {
  19271. case value_t::null:
  19272. {
  19273. // null values are empty
  19274. return 0;
  19275. }
  19276. case value_t::array:
  19277. {
  19278. // delegate call to array_t::size()
  19279. return m_value.array->size();
  19280. }
  19281. case value_t::object:
  19282. {
  19283. // delegate call to object_t::size()
  19284. return m_value.object->size();
  19285. }
  19286. case value_t::string:
  19287. case value_t::boolean:
  19288. case value_t::number_integer:
  19289. case value_t::number_unsigned:
  19290. case value_t::number_float:
  19291. case value_t::binary:
  19292. case value_t::discarded:
  19293. default:
  19294. {
  19295. // all other types have size 1
  19296. return 1;
  19297. }
  19298. }
  19299. }
  19300. /*!
  19301. @brief returns the maximum possible number of elements
  19302. Returns the maximum number of elements a JSON value is able to hold due to
  19303. system or library implementation limitations, i.e. `std::distance(begin(),
  19304. end())` for the JSON value.
  19305. @return The return value depends on the different types and is
  19306. defined as follows:
  19307. Value type | return value
  19308. ----------- | -------------
  19309. null | `0` (same as `size()`)
  19310. boolean | `1` (same as `size()`)
  19311. string | `1` (same as `size()`)
  19312. number | `1` (same as `size()`)
  19313. binary | `1` (same as `size()`)
  19314. object | result of function `object_t::max_size()`
  19315. array | result of function `array_t::max_size()`
  19316. @liveexample{The following code calls `max_size()` on the different value
  19317. types. Note the output is implementation specific.,max_size}
  19318. @complexity Constant, as long as @ref array_t and @ref object_t satisfy
  19319. the Container concept; that is, their `max_size()` functions have constant
  19320. complexity.
  19321. @iterators No changes.
  19322. @exceptionsafety No-throw guarantee: this function never throws exceptions.
  19323. @requirement This function helps `basic_json` satisfying the
  19324. [Container](https://en.cppreference.com/w/cpp/named_req/Container)
  19325. requirements:
  19326. - The complexity is constant.
  19327. - Has the semantics of returning `b.size()` where `b` is the largest
  19328. possible JSON value.
  19329. @sa see @ref size() -- returns the number of elements
  19330. @since version 1.0.0
  19331. */
  19332. size_type max_size() const noexcept
  19333. {
  19334. switch (m_type)
  19335. {
  19336. case value_t::array:
  19337. {
  19338. // delegate call to array_t::max_size()
  19339. return m_value.array->max_size();
  19340. }
  19341. case value_t::object:
  19342. {
  19343. // delegate call to object_t::max_size()
  19344. return m_value.object->max_size();
  19345. }
  19346. case value_t::null:
  19347. case value_t::string:
  19348. case value_t::boolean:
  19349. case value_t::number_integer:
  19350. case value_t::number_unsigned:
  19351. case value_t::number_float:
  19352. case value_t::binary:
  19353. case value_t::discarded:
  19354. default:
  19355. {
  19356. // all other types have max_size() == size()
  19357. return size();
  19358. }
  19359. }
  19360. }
  19361. /// @}
  19362. ///////////////
  19363. // modifiers //
  19364. ///////////////
  19365. /// @name modifiers
  19366. /// @{
  19367. /*!
  19368. @brief clears the contents
  19369. Clears the content of a JSON value and resets it to the default value as
  19370. if @ref basic_json(value_t) would have been called with the current value
  19371. type from @ref type():
  19372. Value type | initial value
  19373. ----------- | -------------
  19374. null | `null`
  19375. boolean | `false`
  19376. string | `""`
  19377. number | `0`
  19378. binary | An empty byte vector
  19379. object | `{}`
  19380. array | `[]`
  19381. @post Has the same effect as calling
  19382. @code {.cpp}
  19383. *this = basic_json(type());
  19384. @endcode
  19385. @liveexample{The example below shows the effect of `clear()` to different
  19386. JSON types.,clear}
  19387. @complexity Linear in the size of the JSON value.
  19388. @iterators All iterators, pointers and references related to this container
  19389. are invalidated.
  19390. @exceptionsafety No-throw guarantee: this function never throws exceptions.
  19391. @sa see @ref basic_json(value_t) -- constructor that creates an object with the
  19392. same value than calling `clear()`
  19393. @since version 1.0.0
  19394. */
  19395. void clear() noexcept
  19396. {
  19397. switch (m_type)
  19398. {
  19399. case value_t::number_integer:
  19400. {
  19401. m_value.number_integer = 0;
  19402. break;
  19403. }
  19404. case value_t::number_unsigned:
  19405. {
  19406. m_value.number_unsigned = 0;
  19407. break;
  19408. }
  19409. case value_t::number_float:
  19410. {
  19411. m_value.number_float = 0.0;
  19412. break;
  19413. }
  19414. case value_t::boolean:
  19415. {
  19416. m_value.boolean = false;
  19417. break;
  19418. }
  19419. case value_t::string:
  19420. {
  19421. m_value.string->clear();
  19422. break;
  19423. }
  19424. case value_t::binary:
  19425. {
  19426. m_value.binary->clear();
  19427. break;
  19428. }
  19429. case value_t::array:
  19430. {
  19431. m_value.array->clear();
  19432. break;
  19433. }
  19434. case value_t::object:
  19435. {
  19436. m_value.object->clear();
  19437. break;
  19438. }
  19439. case value_t::null:
  19440. case value_t::discarded:
  19441. default:
  19442. break;
  19443. }
  19444. }
  19445. /*!
  19446. @brief add an object to an array
  19447. Appends the given element @a val to the end of the JSON value. If the
  19448. function is called on a JSON null value, an empty array is created before
  19449. appending @a val.
  19450. @param[in] val the value to add to the JSON array
  19451. @throw type_error.308 when called on a type other than JSON array or
  19452. null; example: `"cannot use push_back() with number"`
  19453. @complexity Amortized constant.
  19454. @liveexample{The example shows how `push_back()` and `+=` can be used to
  19455. add elements to a JSON array. Note how the `null` value was silently
  19456. converted to a JSON array.,push_back}
  19457. @since version 1.0.0
  19458. */
  19459. void push_back(basic_json&& val)
  19460. {
  19461. // push_back only works for null objects or arrays
  19462. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
  19463. {
  19464. JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), *this));
  19465. }
  19466. // transform null object into an array
  19467. if (is_null())
  19468. {
  19469. m_type = value_t::array;
  19470. m_value = value_t::array;
  19471. assert_invariant();
  19472. }
  19473. // add element to array (move semantics)
  19474. const auto old_capacity = m_value.array->capacity();
  19475. m_value.array->push_back(std::move(val));
  19476. set_parent(m_value.array->back(), old_capacity);
  19477. // if val is moved from, basic_json move constructor marks it null so we do not call the destructor
  19478. }
  19479. /*!
  19480. @brief add an object to an array
  19481. @copydoc push_back(basic_json&&)
  19482. */
  19483. reference operator+=(basic_json&& val)
  19484. {
  19485. push_back(std::move(val));
  19486. return *this;
  19487. }
  19488. /*!
  19489. @brief add an object to an array
  19490. @copydoc push_back(basic_json&&)
  19491. */
  19492. void push_back(const basic_json& val)
  19493. {
  19494. // push_back only works for null objects or arrays
  19495. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
  19496. {
  19497. JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), *this));
  19498. }
  19499. // transform null object into an array
  19500. if (is_null())
  19501. {
  19502. m_type = value_t::array;
  19503. m_value = value_t::array;
  19504. assert_invariant();
  19505. }
  19506. // add element to array
  19507. const auto old_capacity = m_value.array->capacity();
  19508. m_value.array->push_back(val);
  19509. set_parent(m_value.array->back(), old_capacity);
  19510. }
  19511. /*!
  19512. @brief add an object to an array
  19513. @copydoc push_back(basic_json&&)
  19514. */
  19515. reference operator+=(const basic_json& val)
  19516. {
  19517. push_back(val);
  19518. return *this;
  19519. }
  19520. /*!
  19521. @brief add an object to an object
  19522. Inserts the given element @a val to the JSON object. If the function is
  19523. called on a JSON null value, an empty object is created before inserting
  19524. @a val.
  19525. @param[in] val the value to add to the JSON object
  19526. @throw type_error.308 when called on a type other than JSON object or
  19527. null; example: `"cannot use push_back() with number"`
  19528. @complexity Logarithmic in the size of the container, O(log(`size()`)).
  19529. @liveexample{The example shows how `push_back()` and `+=` can be used to
  19530. add elements to a JSON object. Note how the `null` value was silently
  19531. converted to a JSON object.,push_back__object_t__value}
  19532. @since version 1.0.0
  19533. */
  19534. void push_back(const typename object_t::value_type& val)
  19535. {
  19536. // push_back only works for null objects or objects
  19537. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object())))
  19538. {
  19539. JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), *this));
  19540. }
  19541. // transform null object into an object
  19542. if (is_null())
  19543. {
  19544. m_type = value_t::object;
  19545. m_value = value_t::object;
  19546. assert_invariant();
  19547. }
  19548. // add element to object
  19549. auto res = m_value.object->insert(val);
  19550. set_parent(res.first->second);
  19551. }
  19552. /*!
  19553. @brief add an object to an object
  19554. @copydoc push_back(const typename object_t::value_type&)
  19555. */
  19556. reference operator+=(const typename object_t::value_type& val)
  19557. {
  19558. push_back(val);
  19559. return *this;
  19560. }
  19561. /*!
  19562. @brief add an object to an object
  19563. This function allows to use `push_back` with an initializer list. In case
  19564. 1. the current value is an object,
  19565. 2. the initializer list @a init contains only two elements, and
  19566. 3. the first element of @a init is a string,
  19567. @a init is converted into an object element and added using
  19568. @ref push_back(const typename object_t::value_type&). Otherwise, @a init
  19569. is converted to a JSON value and added using @ref push_back(basic_json&&).
  19570. @param[in] init an initializer list
  19571. @complexity Linear in the size of the initializer list @a init.
  19572. @note This function is required to resolve an ambiguous overload error,
  19573. because pairs like `{"key", "value"}` can be both interpreted as
  19574. `object_t::value_type` or `std::initializer_list<basic_json>`, see
  19575. https://github.com/nlohmann/json/issues/235 for more information.
  19576. @liveexample{The example shows how initializer lists are treated as
  19577. objects when possible.,push_back__initializer_list}
  19578. */
  19579. void push_back(initializer_list_t init)
  19580. {
  19581. if (is_object() && init.size() == 2 && (*init.begin())->is_string())
  19582. {
  19583. basic_json&& key = init.begin()->moved_or_copied();
  19584. push_back(typename object_t::value_type(
  19585. std::move(key.get_ref<string_t&>()), (init.begin() + 1)->moved_or_copied()));
  19586. }
  19587. else
  19588. {
  19589. push_back(basic_json(init));
  19590. }
  19591. }
  19592. /*!
  19593. @brief add an object to an object
  19594. @copydoc push_back(initializer_list_t)
  19595. */
  19596. reference operator+=(initializer_list_t init)
  19597. {
  19598. push_back(init);
  19599. return *this;
  19600. }
  19601. /*!
  19602. @brief add an object to an array
  19603. Creates a JSON value from the passed parameters @a args to the end of the
  19604. JSON value. If the function is called on a JSON null value, an empty array
  19605. is created before appending the value created from @a args.
  19606. @param[in] args arguments to forward to a constructor of @ref basic_json
  19607. @tparam Args compatible types to create a @ref basic_json object
  19608. @return reference to the inserted element
  19609. @throw type_error.311 when called on a type other than JSON array or
  19610. null; example: `"cannot use emplace_back() with number"`
  19611. @complexity Amortized constant.
  19612. @liveexample{The example shows how `push_back()` can be used to add
  19613. elements to a JSON array. Note how the `null` value was silently converted
  19614. to a JSON array.,emplace_back}
  19615. @since version 2.0.8, returns reference since 3.7.0
  19616. */
  19617. template<class... Args>
  19618. reference emplace_back(Args&& ... args)
  19619. {
  19620. // emplace_back only works for null objects or arrays
  19621. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
  19622. {
  19623. JSON_THROW(type_error::create(311, "cannot use emplace_back() with " + std::string(type_name()), *this));
  19624. }
  19625. // transform null object into an array
  19626. if (is_null())
  19627. {
  19628. m_type = value_t::array;
  19629. m_value = value_t::array;
  19630. assert_invariant();
  19631. }
  19632. // add element to array (perfect forwarding)
  19633. const auto old_capacity = m_value.array->capacity();
  19634. m_value.array->emplace_back(std::forward<Args>(args)...);
  19635. return set_parent(m_value.array->back(), old_capacity);
  19636. }
  19637. /*!
  19638. @brief add an object to an object if key does not exist
  19639. Inserts a new element into a JSON object constructed in-place with the
  19640. given @a args if there is no element with the key in the container. If the
  19641. function is called on a JSON null value, an empty object is created before
  19642. appending the value created from @a args.
  19643. @param[in] args arguments to forward to a constructor of @ref basic_json
  19644. @tparam Args compatible types to create a @ref basic_json object
  19645. @return a pair consisting of an iterator to the inserted element, or the
  19646. already-existing element if no insertion happened, and a bool
  19647. denoting whether the insertion took place.
  19648. @throw type_error.311 when called on a type other than JSON object or
  19649. null; example: `"cannot use emplace() with number"`
  19650. @complexity Logarithmic in the size of the container, O(log(`size()`)).
  19651. @liveexample{The example shows how `emplace()` can be used to add elements
  19652. to a JSON object. Note how the `null` value was silently converted to a
  19653. JSON object. Further note how no value is added if there was already one
  19654. value stored with the same key.,emplace}
  19655. @since version 2.0.8
  19656. */
  19657. template<class... Args>
  19658. std::pair<iterator, bool> emplace(Args&& ... args)
  19659. {
  19660. // emplace only works for null objects or arrays
  19661. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object())))
  19662. {
  19663. JSON_THROW(type_error::create(311, "cannot use emplace() with " + std::string(type_name()), *this));
  19664. }
  19665. // transform null object into an object
  19666. if (is_null())
  19667. {
  19668. m_type = value_t::object;
  19669. m_value = value_t::object;
  19670. assert_invariant();
  19671. }
  19672. // add element to array (perfect forwarding)
  19673. auto res = m_value.object->emplace(std::forward<Args>(args)...);
  19674. set_parent(res.first->second);
  19675. // create result iterator and set iterator to the result of emplace
  19676. auto it = begin();
  19677. it.m_it.object_iterator = res.first;
  19678. // return pair of iterator and boolean
  19679. return {it, res.second};
  19680. }
  19681. /// Helper for insertion of an iterator
  19682. /// @note: This uses std::distance to support GCC 4.8,
  19683. /// see https://github.com/nlohmann/json/pull/1257
  19684. template<typename... Args>
  19685. iterator insert_iterator(const_iterator pos, Args&& ... args)
  19686. {
  19687. iterator result(this);
  19688. JSON_ASSERT(m_value.array != nullptr);
  19689. auto insert_pos = std::distance(m_value.array->begin(), pos.m_it.array_iterator);
  19690. m_value.array->insert(pos.m_it.array_iterator, std::forward<Args>(args)...);
  19691. result.m_it.array_iterator = m_value.array->begin() + insert_pos;
  19692. // This could have been written as:
  19693. // result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, cnt, val);
  19694. // but the return value of insert is missing in GCC 4.8, so it is written this way instead.
  19695. set_parents();
  19696. return result;
  19697. }
  19698. /*!
  19699. @brief inserts element
  19700. Inserts element @a val before iterator @a pos.
  19701. @param[in] pos iterator before which the content will be inserted; may be
  19702. the end() iterator
  19703. @param[in] val element to insert
  19704. @return iterator pointing to the inserted @a val.
  19705. @throw type_error.309 if called on JSON values other than arrays;
  19706. example: `"cannot use insert() with string"`
  19707. @throw invalid_iterator.202 if @a pos is not an iterator of *this;
  19708. example: `"iterator does not fit current value"`
  19709. @complexity Constant plus linear in the distance between @a pos and end of
  19710. the container.
  19711. @liveexample{The example shows how `insert()` is used.,insert}
  19712. @since version 1.0.0
  19713. */
  19714. iterator insert(const_iterator pos, const basic_json& val)
  19715. {
  19716. // insert only works for arrays
  19717. if (JSON_HEDLEY_LIKELY(is_array()))
  19718. {
  19719. // check if iterator pos fits to this JSON value
  19720. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  19721. {
  19722. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", *this));
  19723. }
  19724. // insert to array and return iterator
  19725. return insert_iterator(pos, val);
  19726. }
  19727. JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this));
  19728. }
  19729. /*!
  19730. @brief inserts element
  19731. @copydoc insert(const_iterator, const basic_json&)
  19732. */
  19733. iterator insert(const_iterator pos, basic_json&& val)
  19734. {
  19735. return insert(pos, val);
  19736. }
  19737. /*!
  19738. @brief inserts elements
  19739. Inserts @a cnt copies of @a val before iterator @a pos.
  19740. @param[in] pos iterator before which the content will be inserted; may be
  19741. the end() iterator
  19742. @param[in] cnt number of copies of @a val to insert
  19743. @param[in] val element to insert
  19744. @return iterator pointing to the first element inserted, or @a pos if
  19745. `cnt==0`
  19746. @throw type_error.309 if called on JSON values other than arrays; example:
  19747. `"cannot use insert() with string"`
  19748. @throw invalid_iterator.202 if @a pos is not an iterator of *this;
  19749. example: `"iterator does not fit current value"`
  19750. @complexity Linear in @a cnt plus linear in the distance between @a pos
  19751. and end of the container.
  19752. @liveexample{The example shows how `insert()` is used.,insert__count}
  19753. @since version 1.0.0
  19754. */
  19755. iterator insert(const_iterator pos, size_type cnt, const basic_json& val)
  19756. {
  19757. // insert only works for arrays
  19758. if (JSON_HEDLEY_LIKELY(is_array()))
  19759. {
  19760. // check if iterator pos fits to this JSON value
  19761. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  19762. {
  19763. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", *this));
  19764. }
  19765. // insert to array and return iterator
  19766. return insert_iterator(pos, cnt, val);
  19767. }
  19768. JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this));
  19769. }
  19770. /*!
  19771. @brief inserts elements
  19772. Inserts elements from range `[first, last)` before iterator @a pos.
  19773. @param[in] pos iterator before which the content will be inserted; may be
  19774. the end() iterator
  19775. @param[in] first begin of the range of elements to insert
  19776. @param[in] last end of the range of elements to insert
  19777. @throw type_error.309 if called on JSON values other than arrays; example:
  19778. `"cannot use insert() with string"`
  19779. @throw invalid_iterator.202 if @a pos is not an iterator of *this;
  19780. example: `"iterator does not fit current value"`
  19781. @throw invalid_iterator.210 if @a first and @a last do not belong to the
  19782. same JSON value; example: `"iterators do not fit"`
  19783. @throw invalid_iterator.211 if @a first or @a last are iterators into
  19784. container for which insert is called; example: `"passed iterators may not
  19785. belong to container"`
  19786. @return iterator pointing to the first element inserted, or @a pos if
  19787. `first==last`
  19788. @complexity Linear in `std::distance(first, last)` plus linear in the
  19789. distance between @a pos and end of the container.
  19790. @liveexample{The example shows how `insert()` is used.,insert__range}
  19791. @since version 1.0.0
  19792. */
  19793. iterator insert(const_iterator pos, const_iterator first, const_iterator last)
  19794. {
  19795. // insert only works for arrays
  19796. if (JSON_HEDLEY_UNLIKELY(!is_array()))
  19797. {
  19798. JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this));
  19799. }
  19800. // check if iterator pos fits to this JSON value
  19801. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  19802. {
  19803. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", *this));
  19804. }
  19805. // check if range iterators belong to the same JSON object
  19806. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  19807. {
  19808. JSON_THROW(invalid_iterator::create(210, "iterators do not fit", *this));
  19809. }
  19810. if (JSON_HEDLEY_UNLIKELY(first.m_object == this))
  19811. {
  19812. JSON_THROW(invalid_iterator::create(211, "passed iterators may not belong to container", *this));
  19813. }
  19814. // insert to array and return iterator
  19815. return insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator);
  19816. }
  19817. /*!
  19818. @brief inserts elements
  19819. Inserts elements from initializer list @a ilist before iterator @a pos.
  19820. @param[in] pos iterator before which the content will be inserted; may be
  19821. the end() iterator
  19822. @param[in] ilist initializer list to insert the values from
  19823. @throw type_error.309 if called on JSON values other than arrays; example:
  19824. `"cannot use insert() with string"`
  19825. @throw invalid_iterator.202 if @a pos is not an iterator of *this;
  19826. example: `"iterator does not fit current value"`
  19827. @return iterator pointing to the first element inserted, or @a pos if
  19828. `ilist` is empty
  19829. @complexity Linear in `ilist.size()` plus linear in the distance between
  19830. @a pos and end of the container.
  19831. @liveexample{The example shows how `insert()` is used.,insert__ilist}
  19832. @since version 1.0.0
  19833. */
  19834. iterator insert(const_iterator pos, initializer_list_t ilist)
  19835. {
  19836. // insert only works for arrays
  19837. if (JSON_HEDLEY_UNLIKELY(!is_array()))
  19838. {
  19839. JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this));
  19840. }
  19841. // check if iterator pos fits to this JSON value
  19842. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  19843. {
  19844. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", *this));
  19845. }
  19846. // insert to array and return iterator
  19847. return insert_iterator(pos, ilist.begin(), ilist.end());
  19848. }
  19849. /*!
  19850. @brief inserts elements
  19851. Inserts elements from range `[first, last)`.
  19852. @param[in] first begin of the range of elements to insert
  19853. @param[in] last end of the range of elements to insert
  19854. @throw type_error.309 if called on JSON values other than objects; example:
  19855. `"cannot use insert() with string"`
  19856. @throw invalid_iterator.202 if iterator @a first or @a last does does not
  19857. point to an object; example: `"iterators first and last must point to
  19858. objects"`
  19859. @throw invalid_iterator.210 if @a first and @a last do not belong to the
  19860. same JSON value; example: `"iterators do not fit"`
  19861. @complexity Logarithmic: `O(N*log(size() + N))`, where `N` is the number
  19862. of elements to insert.
  19863. @liveexample{The example shows how `insert()` is used.,insert__range_object}
  19864. @since version 3.0.0
  19865. */
  19866. void insert(const_iterator first, const_iterator last)
  19867. {
  19868. // insert only works for objects
  19869. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  19870. {
  19871. JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this));
  19872. }
  19873. // check if range iterators belong to the same JSON object
  19874. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  19875. {
  19876. JSON_THROW(invalid_iterator::create(210, "iterators do not fit", *this));
  19877. }
  19878. // passed iterators must belong to objects
  19879. if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()))
  19880. {
  19881. JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", *this));
  19882. }
  19883. m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator);
  19884. }
  19885. /*!
  19886. @brief updates a JSON object from another object, overwriting existing keys
  19887. Inserts all values from JSON object @a j and overwrites existing keys.
  19888. @param[in] j JSON object to read values from
  19889. @throw type_error.312 if called on JSON values other than objects; example:
  19890. `"cannot use update() with string"`
  19891. @complexity O(N*log(size() + N)), where N is the number of elements to
  19892. insert.
  19893. @liveexample{The example shows how `update()` is used.,update}
  19894. @sa https://docs.python.org/3.6/library/stdtypes.html#dict.update
  19895. @since version 3.0.0
  19896. */
  19897. void update(const_reference j)
  19898. {
  19899. // implicitly convert null value to an empty object
  19900. if (is_null())
  19901. {
  19902. m_type = value_t::object;
  19903. m_value.object = create<object_t>();
  19904. assert_invariant();
  19905. }
  19906. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  19907. {
  19908. JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name()), *this));
  19909. }
  19910. if (JSON_HEDLEY_UNLIKELY(!j.is_object()))
  19911. {
  19912. JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(j.type_name()), *this));
  19913. }
  19914. for (auto it = j.cbegin(); it != j.cend(); ++it)
  19915. {
  19916. m_value.object->operator[](it.key()) = it.value();
  19917. }
  19918. }
  19919. /*!
  19920. @brief updates a JSON object from another object, overwriting existing keys
  19921. Inserts all values from from range `[first, last)` and overwrites existing
  19922. keys.
  19923. @param[in] first begin of the range of elements to insert
  19924. @param[in] last end of the range of elements to insert
  19925. @throw type_error.312 if called on JSON values other than objects; example:
  19926. `"cannot use update() with string"`
  19927. @throw invalid_iterator.202 if iterator @a first or @a last does does not
  19928. point to an object; example: `"iterators first and last must point to
  19929. objects"`
  19930. @throw invalid_iterator.210 if @a first and @a last do not belong to the
  19931. same JSON value; example: `"iterators do not fit"`
  19932. @complexity O(N*log(size() + N)), where N is the number of elements to
  19933. insert.
  19934. @liveexample{The example shows how `update()` is used__range.,update}
  19935. @sa https://docs.python.org/3.6/library/stdtypes.html#dict.update
  19936. @since version 3.0.0
  19937. */
  19938. void update(const_iterator first, const_iterator last)
  19939. {
  19940. // implicitly convert null value to an empty object
  19941. if (is_null())
  19942. {
  19943. m_type = value_t::object;
  19944. m_value.object = create<object_t>();
  19945. assert_invariant();
  19946. }
  19947. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  19948. {
  19949. JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name()), *this));
  19950. }
  19951. // check if range iterators belong to the same JSON object
  19952. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  19953. {
  19954. JSON_THROW(invalid_iterator::create(210, "iterators do not fit", *this));
  19955. }
  19956. // passed iterators must belong to objects
  19957. if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()
  19958. || !last.m_object->is_object()))
  19959. {
  19960. JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", *this));
  19961. }
  19962. for (auto it = first; it != last; ++it)
  19963. {
  19964. m_value.object->operator[](it.key()) = it.value();
  19965. }
  19966. }
  19967. /*!
  19968. @brief exchanges the values
  19969. Exchanges the contents of the JSON value with those of @a other. Does not
  19970. invoke any move, copy, or swap operations on individual elements. All
  19971. iterators and references remain valid. The past-the-end iterator is
  19972. invalidated.
  19973. @param[in,out] other JSON value to exchange the contents with
  19974. @complexity Constant.
  19975. @liveexample{The example below shows how JSON values can be swapped with
  19976. `swap()`.,swap__reference}
  19977. @since version 1.0.0
  19978. */
  19979. void swap(reference other) noexcept (
  19980. std::is_nothrow_move_constructible<value_t>::value&&
  19981. std::is_nothrow_move_assignable<value_t>::value&&
  19982. std::is_nothrow_move_constructible<json_value>::value&&
  19983. std::is_nothrow_move_assignable<json_value>::value
  19984. )
  19985. {
  19986. std::swap(m_type, other.m_type);
  19987. std::swap(m_value, other.m_value);
  19988. set_parents();
  19989. other.set_parents();
  19990. assert_invariant();
  19991. }
  19992. /*!
  19993. @brief exchanges the values
  19994. Exchanges the contents of the JSON value from @a left with those of @a right. Does not
  19995. invoke any move, copy, or swap operations on individual elements. All
  19996. iterators and references remain valid. The past-the-end iterator is
  19997. invalidated. implemented as a friend function callable via ADL.
  19998. @param[in,out] left JSON value to exchange the contents with
  19999. @param[in,out] right JSON value to exchange the contents with
  20000. @complexity Constant.
  20001. @liveexample{The example below shows how JSON values can be swapped with
  20002. `swap()`.,swap__reference}
  20003. @since version 1.0.0
  20004. */
  20005. friend void swap(reference left, reference right) noexcept (
  20006. std::is_nothrow_move_constructible<value_t>::value&&
  20007. std::is_nothrow_move_assignable<value_t>::value&&
  20008. std::is_nothrow_move_constructible<json_value>::value&&
  20009. std::is_nothrow_move_assignable<json_value>::value
  20010. )
  20011. {
  20012. left.swap(right);
  20013. }
  20014. /*!
  20015. @brief exchanges the values
  20016. Exchanges the contents of a JSON array with those of @a other. Does not
  20017. invoke any move, copy, or swap operations on individual elements. All
  20018. iterators and references remain valid. The past-the-end iterator is
  20019. invalidated.
  20020. @param[in,out] other array to exchange the contents with
  20021. @throw type_error.310 when JSON value is not an array; example: `"cannot
  20022. use swap() with string"`
  20023. @complexity Constant.
  20024. @liveexample{The example below shows how arrays can be swapped with
  20025. `swap()`.,swap__array_t}
  20026. @since version 1.0.0
  20027. */
  20028. void swap(array_t& other) // NOLINT(bugprone-exception-escape)
  20029. {
  20030. // swap only works for arrays
  20031. if (JSON_HEDLEY_LIKELY(is_array()))
  20032. {
  20033. std::swap(*(m_value.array), other);
  20034. }
  20035. else
  20036. {
  20037. JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), *this));
  20038. }
  20039. }
  20040. /*!
  20041. @brief exchanges the values
  20042. Exchanges the contents of a JSON object with those of @a other. Does not
  20043. invoke any move, copy, or swap operations on individual elements. All
  20044. iterators and references remain valid. The past-the-end iterator is
  20045. invalidated.
  20046. @param[in,out] other object to exchange the contents with
  20047. @throw type_error.310 when JSON value is not an object; example:
  20048. `"cannot use swap() with string"`
  20049. @complexity Constant.
  20050. @liveexample{The example below shows how objects can be swapped with
  20051. `swap()`.,swap__object_t}
  20052. @since version 1.0.0
  20053. */
  20054. void swap(object_t& other) // NOLINT(bugprone-exception-escape)
  20055. {
  20056. // swap only works for objects
  20057. if (JSON_HEDLEY_LIKELY(is_object()))
  20058. {
  20059. std::swap(*(m_value.object), other);
  20060. }
  20061. else
  20062. {
  20063. JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), *this));
  20064. }
  20065. }
  20066. /*!
  20067. @brief exchanges the values
  20068. Exchanges the contents of a JSON string with those of @a other. Does not
  20069. invoke any move, copy, or swap operations on individual elements. All
  20070. iterators and references remain valid. The past-the-end iterator is
  20071. invalidated.
  20072. @param[in,out] other string to exchange the contents with
  20073. @throw type_error.310 when JSON value is not a string; example: `"cannot
  20074. use swap() with boolean"`
  20075. @complexity Constant.
  20076. @liveexample{The example below shows how strings can be swapped with
  20077. `swap()`.,swap__string_t}
  20078. @since version 1.0.0
  20079. */
  20080. void swap(string_t& other) // NOLINT(bugprone-exception-escape)
  20081. {
  20082. // swap only works for strings
  20083. if (JSON_HEDLEY_LIKELY(is_string()))
  20084. {
  20085. std::swap(*(m_value.string), other);
  20086. }
  20087. else
  20088. {
  20089. JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), *this));
  20090. }
  20091. }
  20092. /*!
  20093. @brief exchanges the values
  20094. Exchanges the contents of a JSON string with those of @a other. Does not
  20095. invoke any move, copy, or swap operations on individual elements. All
  20096. iterators and references remain valid. The past-the-end iterator is
  20097. invalidated.
  20098. @param[in,out] other binary to exchange the contents with
  20099. @throw type_error.310 when JSON value is not a string; example: `"cannot
  20100. use swap() with boolean"`
  20101. @complexity Constant.
  20102. @liveexample{The example below shows how strings can be swapped with
  20103. `swap()`.,swap__binary_t}
  20104. @since version 3.8.0
  20105. */
  20106. void swap(binary_t& other) // NOLINT(bugprone-exception-escape)
  20107. {
  20108. // swap only works for strings
  20109. if (JSON_HEDLEY_LIKELY(is_binary()))
  20110. {
  20111. std::swap(*(m_value.binary), other);
  20112. }
  20113. else
  20114. {
  20115. JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), *this));
  20116. }
  20117. }
  20118. /// @copydoc swap(binary_t&)
  20119. void swap(typename binary_t::container_type& other) // NOLINT(bugprone-exception-escape)
  20120. {
  20121. // swap only works for strings
  20122. if (JSON_HEDLEY_LIKELY(is_binary()))
  20123. {
  20124. std::swap(*(m_value.binary), other);
  20125. }
  20126. else
  20127. {
  20128. JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), *this));
  20129. }
  20130. }
  20131. /// @}
  20132. public:
  20133. //////////////////////////////////////////
  20134. // lexicographical comparison operators //
  20135. //////////////////////////////////////////
  20136. /// @name lexicographical comparison operators
  20137. /// @{
  20138. /*!
  20139. @brief comparison: equal
  20140. Compares two JSON values for equality according to the following rules:
  20141. - Two JSON values are equal if (1) they are from the same type and (2)
  20142. their stored values are the same according to their respective
  20143. `operator==`.
  20144. - Integer and floating-point numbers are automatically converted before
  20145. comparison. Note that two NaN values are always treated as unequal.
  20146. - Two JSON null values are equal.
  20147. @note Floating-point inside JSON values numbers are compared with
  20148. `json::number_float_t::operator==` which is `double::operator==` by
  20149. default. To compare floating-point while respecting an epsilon, an alternative
  20150. [comparison function](https://github.com/mariokonrad/marnav/blob/master/include/marnav/math/floatingpoint.hpp#L34-#L39)
  20151. could be used, for instance
  20152. @code {.cpp}
  20153. template<typename T, typename = typename std::enable_if<std::is_floating_point<T>::value, T>::type>
  20154. inline bool is_same(T a, T b, T epsilon = std::numeric_limits<T>::epsilon()) noexcept
  20155. {
  20156. return std::abs(a - b) <= epsilon;
  20157. }
  20158. @endcode
  20159. Or you can self-defined operator equal function like this:
  20160. @code {.cpp}
  20161. bool my_equal(const_reference lhs, const_reference rhs) {
  20162. const auto lhs_type lhs.type();
  20163. const auto rhs_type rhs.type();
  20164. if (lhs_type == rhs_type) {
  20165. switch(lhs_type)
  20166. // self_defined case
  20167. case value_t::number_float:
  20168. return std::abs(lhs - rhs) <= std::numeric_limits<float>::epsilon();
  20169. // other cases remain the same with the original
  20170. ...
  20171. }
  20172. ...
  20173. }
  20174. @endcode
  20175. @note NaN values never compare equal to themselves or to other NaN values.
  20176. @param[in] lhs first JSON value to consider
  20177. @param[in] rhs second JSON value to consider
  20178. @return whether the values @a lhs and @a rhs are equal
  20179. @exceptionsafety No-throw guarantee: this function never throws exceptions.
  20180. @complexity Linear.
  20181. @liveexample{The example demonstrates comparing several JSON
  20182. types.,operator__equal}
  20183. @since version 1.0.0
  20184. */
  20185. friend bool operator==(const_reference lhs, const_reference rhs) noexcept
  20186. {
  20187. #ifdef __GNUC__
  20188. #pragma GCC diagnostic push
  20189. #pragma GCC diagnostic ignored "-Wfloat-equal"
  20190. #endif
  20191. const auto lhs_type = lhs.type();
  20192. const auto rhs_type = rhs.type();
  20193. if (lhs_type == rhs_type)
  20194. {
  20195. switch (lhs_type)
  20196. {
  20197. case value_t::array:
  20198. return *lhs.m_value.array == *rhs.m_value.array;
  20199. case value_t::object:
  20200. return *lhs.m_value.object == *rhs.m_value.object;
  20201. case value_t::null:
  20202. return true;
  20203. case value_t::string:
  20204. return *lhs.m_value.string == *rhs.m_value.string;
  20205. case value_t::boolean:
  20206. return lhs.m_value.boolean == rhs.m_value.boolean;
  20207. case value_t::number_integer:
  20208. return lhs.m_value.number_integer == rhs.m_value.number_integer;
  20209. case value_t::number_unsigned:
  20210. return lhs.m_value.number_unsigned == rhs.m_value.number_unsigned;
  20211. case value_t::number_float:
  20212. return lhs.m_value.number_float == rhs.m_value.number_float;
  20213. case value_t::binary:
  20214. return *lhs.m_value.binary == *rhs.m_value.binary;
  20215. case value_t::discarded:
  20216. default:
  20217. return false;
  20218. }
  20219. }
  20220. else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_float)
  20221. {
  20222. return static_cast<number_float_t>(lhs.m_value.number_integer) == rhs.m_value.number_float;
  20223. }
  20224. else if (lhs_type == value_t::number_float && rhs_type == value_t::number_integer)
  20225. {
  20226. return lhs.m_value.number_float == static_cast<number_float_t>(rhs.m_value.number_integer);
  20227. }
  20228. else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_float)
  20229. {
  20230. return static_cast<number_float_t>(lhs.m_value.number_unsigned) == rhs.m_value.number_float;
  20231. }
  20232. else if (lhs_type == value_t::number_float && rhs_type == value_t::number_unsigned)
  20233. {
  20234. return lhs.m_value.number_float == static_cast<number_float_t>(rhs.m_value.number_unsigned);
  20235. }
  20236. else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_integer)
  20237. {
  20238. return static_cast<number_integer_t>(lhs.m_value.number_unsigned) == rhs.m_value.number_integer;
  20239. }
  20240. else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_unsigned)
  20241. {
  20242. return lhs.m_value.number_integer == static_cast<number_integer_t>(rhs.m_value.number_unsigned);
  20243. }
  20244. return false;
  20245. #ifdef __GNUC__
  20246. #pragma GCC diagnostic pop
  20247. #endif
  20248. }
  20249. /*!
  20250. @brief comparison: equal
  20251. @copydoc operator==(const_reference, const_reference)
  20252. */
  20253. template<typename ScalarType, typename std::enable_if<
  20254. std::is_scalar<ScalarType>::value, int>::type = 0>
  20255. friend bool operator==(const_reference lhs, ScalarType rhs) noexcept
  20256. {
  20257. return lhs == basic_json(rhs);
  20258. }
  20259. /*!
  20260. @brief comparison: equal
  20261. @copydoc operator==(const_reference, const_reference)
  20262. */
  20263. template<typename ScalarType, typename std::enable_if<
  20264. std::is_scalar<ScalarType>::value, int>::type = 0>
  20265. friend bool operator==(ScalarType lhs, const_reference rhs) noexcept
  20266. {
  20267. return basic_json(lhs) == rhs;
  20268. }
  20269. /*!
  20270. @brief comparison: not equal
  20271. Compares two JSON values for inequality by calculating `not (lhs == rhs)`.
  20272. @param[in] lhs first JSON value to consider
  20273. @param[in] rhs second JSON value to consider
  20274. @return whether the values @a lhs and @a rhs are not equal
  20275. @complexity Linear.
  20276. @exceptionsafety No-throw guarantee: this function never throws exceptions.
  20277. @liveexample{The example demonstrates comparing several JSON
  20278. types.,operator__notequal}
  20279. @since version 1.0.0
  20280. */
  20281. friend bool operator!=(const_reference lhs, const_reference rhs) noexcept
  20282. {
  20283. return !(lhs == rhs);
  20284. }
  20285. /*!
  20286. @brief comparison: not equal
  20287. @copydoc operator!=(const_reference, const_reference)
  20288. */
  20289. template<typename ScalarType, typename std::enable_if<
  20290. std::is_scalar<ScalarType>::value, int>::type = 0>
  20291. friend bool operator!=(const_reference lhs, ScalarType rhs) noexcept
  20292. {
  20293. return lhs != basic_json(rhs);
  20294. }
  20295. /*!
  20296. @brief comparison: not equal
  20297. @copydoc operator!=(const_reference, const_reference)
  20298. */
  20299. template<typename ScalarType, typename std::enable_if<
  20300. std::is_scalar<ScalarType>::value, int>::type = 0>
  20301. friend bool operator!=(ScalarType lhs, const_reference rhs) noexcept
  20302. {
  20303. return basic_json(lhs) != rhs;
  20304. }
  20305. /*!
  20306. @brief comparison: less than
  20307. Compares whether one JSON value @a lhs is less than another JSON value @a
  20308. rhs according to the following rules:
  20309. - If @a lhs and @a rhs have the same type, the values are compared using
  20310. the default `<` operator.
  20311. - Integer and floating-point numbers are automatically converted before
  20312. comparison
  20313. - In case @a lhs and @a rhs have different types, the values are ignored
  20314. and the order of the types is considered, see
  20315. @ref operator<(const value_t, const value_t).
  20316. @param[in] lhs first JSON value to consider
  20317. @param[in] rhs second JSON value to consider
  20318. @return whether @a lhs is less than @a rhs
  20319. @complexity Linear.
  20320. @exceptionsafety No-throw guarantee: this function never throws exceptions.
  20321. @liveexample{The example demonstrates comparing several JSON
  20322. types.,operator__less}
  20323. @since version 1.0.0
  20324. */
  20325. friend bool operator<(const_reference lhs, const_reference rhs) noexcept
  20326. {
  20327. const auto lhs_type = lhs.type();
  20328. const auto rhs_type = rhs.type();
  20329. if (lhs_type == rhs_type)
  20330. {
  20331. switch (lhs_type)
  20332. {
  20333. case value_t::array:
  20334. // note parentheses are necessary, see
  20335. // https://github.com/nlohmann/json/issues/1530
  20336. return (*lhs.m_value.array) < (*rhs.m_value.array);
  20337. case value_t::object:
  20338. return (*lhs.m_value.object) < (*rhs.m_value.object);
  20339. case value_t::null:
  20340. return false;
  20341. case value_t::string:
  20342. return (*lhs.m_value.string) < (*rhs.m_value.string);
  20343. case value_t::boolean:
  20344. return (lhs.m_value.boolean) < (rhs.m_value.boolean);
  20345. case value_t::number_integer:
  20346. return (lhs.m_value.number_integer) < (rhs.m_value.number_integer);
  20347. case value_t::number_unsigned:
  20348. return (lhs.m_value.number_unsigned) < (rhs.m_value.number_unsigned);
  20349. case value_t::number_float:
  20350. return (lhs.m_value.number_float) < (rhs.m_value.number_float);
  20351. case value_t::binary:
  20352. return (*lhs.m_value.binary) < (*rhs.m_value.binary);
  20353. case value_t::discarded:
  20354. default:
  20355. return false;
  20356. }
  20357. }
  20358. else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_float)
  20359. {
  20360. return static_cast<number_float_t>(lhs.m_value.number_integer) < rhs.m_value.number_float;
  20361. }
  20362. else if (lhs_type == value_t::number_float && rhs_type == value_t::number_integer)
  20363. {
  20364. return lhs.m_value.number_float < static_cast<number_float_t>(rhs.m_value.number_integer);
  20365. }
  20366. else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_float)
  20367. {
  20368. return static_cast<number_float_t>(lhs.m_value.number_unsigned) < rhs.m_value.number_float;
  20369. }
  20370. else if (lhs_type == value_t::number_float && rhs_type == value_t::number_unsigned)
  20371. {
  20372. return lhs.m_value.number_float < static_cast<number_float_t>(rhs.m_value.number_unsigned);
  20373. }
  20374. else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_unsigned)
  20375. {
  20376. return lhs.m_value.number_integer < static_cast<number_integer_t>(rhs.m_value.number_unsigned);
  20377. }
  20378. else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_integer)
  20379. {
  20380. return static_cast<number_integer_t>(lhs.m_value.number_unsigned) < rhs.m_value.number_integer;
  20381. }
  20382. // We only reach this line if we cannot compare values. In that case,
  20383. // we compare types. Note we have to call the operator explicitly,
  20384. // because MSVC has problems otherwise.
  20385. return operator<(lhs_type, rhs_type);
  20386. }
  20387. /*!
  20388. @brief comparison: less than
  20389. @copydoc operator<(const_reference, const_reference)
  20390. */
  20391. template<typename ScalarType, typename std::enable_if<
  20392. std::is_scalar<ScalarType>::value, int>::type = 0>
  20393. friend bool operator<(const_reference lhs, ScalarType rhs) noexcept
  20394. {
  20395. return lhs < basic_json(rhs);
  20396. }
  20397. /*!
  20398. @brief comparison: less than
  20399. @copydoc operator<(const_reference, const_reference)
  20400. */
  20401. template<typename ScalarType, typename std::enable_if<
  20402. std::is_scalar<ScalarType>::value, int>::type = 0>
  20403. friend bool operator<(ScalarType lhs, const_reference rhs) noexcept
  20404. {
  20405. return basic_json(lhs) < rhs;
  20406. }
  20407. /*!
  20408. @brief comparison: less than or equal
  20409. Compares whether one JSON value @a lhs is less than or equal to another
  20410. JSON value by calculating `not (rhs < lhs)`.
  20411. @param[in] lhs first JSON value to consider
  20412. @param[in] rhs second JSON value to consider
  20413. @return whether @a lhs is less than or equal to @a rhs
  20414. @complexity Linear.
  20415. @exceptionsafety No-throw guarantee: this function never throws exceptions.
  20416. @liveexample{The example demonstrates comparing several JSON
  20417. types.,operator__greater}
  20418. @since version 1.0.0
  20419. */
  20420. friend bool operator<=(const_reference lhs, const_reference rhs) noexcept
  20421. {
  20422. return !(rhs < lhs);
  20423. }
  20424. /*!
  20425. @brief comparison: less than or equal
  20426. @copydoc operator<=(const_reference, const_reference)
  20427. */
  20428. template<typename ScalarType, typename std::enable_if<
  20429. std::is_scalar<ScalarType>::value, int>::type = 0>
  20430. friend bool operator<=(const_reference lhs, ScalarType rhs) noexcept
  20431. {
  20432. return lhs <= basic_json(rhs);
  20433. }
  20434. /*!
  20435. @brief comparison: less than or equal
  20436. @copydoc operator<=(const_reference, const_reference)
  20437. */
  20438. template<typename ScalarType, typename std::enable_if<
  20439. std::is_scalar<ScalarType>::value, int>::type = 0>
  20440. friend bool operator<=(ScalarType lhs, const_reference rhs) noexcept
  20441. {
  20442. return basic_json(lhs) <= rhs;
  20443. }
  20444. /*!
  20445. @brief comparison: greater than
  20446. Compares whether one JSON value @a lhs is greater than another
  20447. JSON value by calculating `not (lhs <= rhs)`.
  20448. @param[in] lhs first JSON value to consider
  20449. @param[in] rhs second JSON value to consider
  20450. @return whether @a lhs is greater than to @a rhs
  20451. @complexity Linear.
  20452. @exceptionsafety No-throw guarantee: this function never throws exceptions.
  20453. @liveexample{The example demonstrates comparing several JSON
  20454. types.,operator__lessequal}
  20455. @since version 1.0.0
  20456. */
  20457. friend bool operator>(const_reference lhs, const_reference rhs) noexcept
  20458. {
  20459. return !(lhs <= rhs);
  20460. }
  20461. /*!
  20462. @brief comparison: greater than
  20463. @copydoc operator>(const_reference, const_reference)
  20464. */
  20465. template<typename ScalarType, typename std::enable_if<
  20466. std::is_scalar<ScalarType>::value, int>::type = 0>
  20467. friend bool operator>(const_reference lhs, ScalarType rhs) noexcept
  20468. {
  20469. return lhs > basic_json(rhs);
  20470. }
  20471. /*!
  20472. @brief comparison: greater than
  20473. @copydoc operator>(const_reference, const_reference)
  20474. */
  20475. template<typename ScalarType, typename std::enable_if<
  20476. std::is_scalar<ScalarType>::value, int>::type = 0>
  20477. friend bool operator>(ScalarType lhs, const_reference rhs) noexcept
  20478. {
  20479. return basic_json(lhs) > rhs;
  20480. }
  20481. /*!
  20482. @brief comparison: greater than or equal
  20483. Compares whether one JSON value @a lhs is greater than or equal to another
  20484. JSON value by calculating `not (lhs < rhs)`.
  20485. @param[in] lhs first JSON value to consider
  20486. @param[in] rhs second JSON value to consider
  20487. @return whether @a lhs is greater than or equal to @a rhs
  20488. @complexity Linear.
  20489. @exceptionsafety No-throw guarantee: this function never throws exceptions.
  20490. @liveexample{The example demonstrates comparing several JSON
  20491. types.,operator__greaterequal}
  20492. @since version 1.0.0
  20493. */
  20494. friend bool operator>=(const_reference lhs, const_reference rhs) noexcept
  20495. {
  20496. return !(lhs < rhs);
  20497. }
  20498. /*!
  20499. @brief comparison: greater than or equal
  20500. @copydoc operator>=(const_reference, const_reference)
  20501. */
  20502. template<typename ScalarType, typename std::enable_if<
  20503. std::is_scalar<ScalarType>::value, int>::type = 0>
  20504. friend bool operator>=(const_reference lhs, ScalarType rhs) noexcept
  20505. {
  20506. return lhs >= basic_json(rhs);
  20507. }
  20508. /*!
  20509. @brief comparison: greater than or equal
  20510. @copydoc operator>=(const_reference, const_reference)
  20511. */
  20512. template<typename ScalarType, typename std::enable_if<
  20513. std::is_scalar<ScalarType>::value, int>::type = 0>
  20514. friend bool operator>=(ScalarType lhs, const_reference rhs) noexcept
  20515. {
  20516. return basic_json(lhs) >= rhs;
  20517. }
  20518. /// @}
  20519. ///////////////////
  20520. // serialization //
  20521. ///////////////////
  20522. /// @name serialization
  20523. /// @{
  20524. #ifndef JSON_NO_IO
  20525. /*!
  20526. @brief serialize to stream
  20527. Serialize the given JSON value @a j to the output stream @a o. The JSON
  20528. value will be serialized using the @ref dump member function.
  20529. - The indentation of the output can be controlled with the member variable
  20530. `width` of the output stream @a o. For instance, using the manipulator
  20531. `std::setw(4)` on @a o sets the indentation level to `4` and the
  20532. serialization result is the same as calling `dump(4)`.
  20533. - The indentation character can be controlled with the member variable
  20534. `fill` of the output stream @a o. For instance, the manipulator
  20535. `std::setfill('\\t')` sets indentation to use a tab character rather than
  20536. the default space character.
  20537. @param[in,out] o stream to serialize to
  20538. @param[in] j JSON value to serialize
  20539. @return the stream @a o
  20540. @throw type_error.316 if a string stored inside the JSON value is not
  20541. UTF-8 encoded
  20542. @complexity Linear.
  20543. @liveexample{The example below shows the serialization with different
  20544. parameters to `width` to adjust the indentation level.,operator_serialize}
  20545. @since version 1.0.0; indentation character added in version 3.0.0
  20546. */
  20547. friend std::ostream& operator<<(std::ostream& o, const basic_json& j)
  20548. {
  20549. // read width member and use it as indentation parameter if nonzero
  20550. const bool pretty_print = o.width() > 0;
  20551. const auto indentation = pretty_print ? o.width() : 0;
  20552. // reset width to 0 for subsequent calls to this stream
  20553. o.width(0);
  20554. // do the actual serialization
  20555. serializer s(detail::output_adapter<char>(o), o.fill());
  20556. s.dump(j, pretty_print, false, static_cast<unsigned int>(indentation));
  20557. return o;
  20558. }
  20559. /*!
  20560. @brief serialize to stream
  20561. @deprecated This stream operator is deprecated and will be removed in
  20562. future 4.0.0 of the library. Please use
  20563. @ref operator<<(std::ostream&, const basic_json&)
  20564. instead; that is, replace calls like `j >> o;` with `o << j;`.
  20565. @since version 1.0.0; deprecated since version 3.0.0
  20566. */
  20567. JSON_HEDLEY_DEPRECATED_FOR(3.0.0, operator<<(std::ostream&, const basic_json&))
  20568. friend std::ostream& operator>>(const basic_json& j, std::ostream& o)
  20569. {
  20570. return o << j;
  20571. }
  20572. #endif // JSON_NO_IO
  20573. /// @}
  20574. /////////////////////
  20575. // deserialization //
  20576. /////////////////////
  20577. /// @name deserialization
  20578. /// @{
  20579. /*!
  20580. @brief deserialize from a compatible input
  20581. @tparam InputType A compatible input, for instance
  20582. - an std::istream object
  20583. - a FILE pointer
  20584. - a C-style array of characters
  20585. - a pointer to a null-terminated string of single byte characters
  20586. - an object obj for which begin(obj) and end(obj) produces a valid pair of
  20587. iterators.
  20588. @param[in] i input to read from
  20589. @param[in] cb a parser callback function of type @ref parser_callback_t
  20590. which is used to control the deserialization by filtering unwanted values
  20591. (optional)
  20592. @param[in] allow_exceptions whether to throw exceptions in case of a
  20593. parse error (optional, true by default)
  20594. @param[in] ignore_comments whether comments should be ignored and treated
  20595. like whitespace (true) or yield a parse error (true); (optional, false by
  20596. default)
  20597. @return deserialized JSON value; in case of a parse error and
  20598. @a allow_exceptions set to `false`, the return value will be
  20599. value_t::discarded.
  20600. @throw parse_error.101 if a parse error occurs; example: `""unexpected end
  20601. of input; expected string literal""`
  20602. @throw parse_error.102 if to_unicode fails or surrogate error
  20603. @throw parse_error.103 if to_unicode fails
  20604. @complexity Linear in the length of the input. The parser is a predictive
  20605. LL(1) parser. The complexity can be higher if the parser callback function
  20606. @a cb or reading from the input @a i has a super-linear complexity.
  20607. @note A UTF-8 byte order mark is silently ignored.
  20608. @liveexample{The example below demonstrates the `parse()` function reading
  20609. from an array.,parse__array__parser_callback_t}
  20610. @liveexample{The example below demonstrates the `parse()` function with
  20611. and without callback function.,parse__string__parser_callback_t}
  20612. @liveexample{The example below demonstrates the `parse()` function with
  20613. and without callback function.,parse__istream__parser_callback_t}
  20614. @liveexample{The example below demonstrates the `parse()` function reading
  20615. from a contiguous container.,parse__contiguouscontainer__parser_callback_t}
  20616. @since version 2.0.3 (contiguous containers); version 3.9.0 allowed to
  20617. ignore comments.
  20618. */
  20619. template<typename InputType>
  20620. JSON_HEDLEY_WARN_UNUSED_RESULT
  20621. static basic_json parse(InputType&& i,
  20622. const parser_callback_t cb = nullptr,
  20623. const bool allow_exceptions = true,
  20624. const bool ignore_comments = false)
  20625. {
  20626. basic_json result;
  20627. parser(detail::input_adapter(std::forward<InputType>(i)), cb, allow_exceptions, ignore_comments).parse(true, result);
  20628. return result;
  20629. }
  20630. /*!
  20631. @brief deserialize from a pair of character iterators
  20632. The value_type of the iterator must be a integral type with size of 1, 2 or
  20633. 4 bytes, which will be interpreted respectively as UTF-8, UTF-16 and UTF-32.
  20634. @param[in] first iterator to start of character range
  20635. @param[in] last iterator to end of character range
  20636. @param[in] cb a parser callback function of type @ref parser_callback_t
  20637. which is used to control the deserialization by filtering unwanted values
  20638. (optional)
  20639. @param[in] allow_exceptions whether to throw exceptions in case of a
  20640. parse error (optional, true by default)
  20641. @param[in] ignore_comments whether comments should be ignored and treated
  20642. like whitespace (true) or yield a parse error (true); (optional, false by
  20643. default)
  20644. @return deserialized JSON value; in case of a parse error and
  20645. @a allow_exceptions set to `false`, the return value will be
  20646. value_t::discarded.
  20647. @throw parse_error.101 if a parse error occurs; example: `""unexpected end
  20648. of input; expected string literal""`
  20649. @throw parse_error.102 if to_unicode fails or surrogate error
  20650. @throw parse_error.103 if to_unicode fails
  20651. */
  20652. template<typename IteratorType>
  20653. JSON_HEDLEY_WARN_UNUSED_RESULT
  20654. static basic_json parse(IteratorType first,
  20655. IteratorType last,
  20656. const parser_callback_t cb = nullptr,
  20657. const bool allow_exceptions = true,
  20658. const bool ignore_comments = false)
  20659. {
  20660. basic_json result;
  20661. parser(detail::input_adapter(std::move(first), std::move(last)), cb, allow_exceptions, ignore_comments).parse(true, result);
  20662. return result;
  20663. }
  20664. JSON_HEDLEY_WARN_UNUSED_RESULT
  20665. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, parse(ptr, ptr + len))
  20666. static basic_json parse(detail::span_input_adapter&& i,
  20667. const parser_callback_t cb = nullptr,
  20668. const bool allow_exceptions = true,
  20669. const bool ignore_comments = false)
  20670. {
  20671. basic_json result;
  20672. parser(i.get(), cb, allow_exceptions, ignore_comments).parse(true, result);
  20673. return result;
  20674. }
  20675. /*!
  20676. @brief check if the input is valid JSON
  20677. Unlike the @ref parse(InputType&&, const parser_callback_t,const bool)
  20678. function, this function neither throws an exception in case of invalid JSON
  20679. input (i.e., a parse error) nor creates diagnostic information.
  20680. @tparam InputType A compatible input, for instance
  20681. - an std::istream object
  20682. - a FILE pointer
  20683. - a C-style array of characters
  20684. - a pointer to a null-terminated string of single byte characters
  20685. - an object obj for which begin(obj) and end(obj) produces a valid pair of
  20686. iterators.
  20687. @param[in] i input to read from
  20688. @param[in] ignore_comments whether comments should be ignored and treated
  20689. like whitespace (true) or yield a parse error (true); (optional, false by
  20690. default)
  20691. @return Whether the input read from @a i is valid JSON.
  20692. @complexity Linear in the length of the input. The parser is a predictive
  20693. LL(1) parser.
  20694. @note A UTF-8 byte order mark is silently ignored.
  20695. @liveexample{The example below demonstrates the `accept()` function reading
  20696. from a string.,accept__string}
  20697. */
  20698. template<typename InputType>
  20699. static bool accept(InputType&& i,
  20700. const bool ignore_comments = false)
  20701. {
  20702. return parser(detail::input_adapter(std::forward<InputType>(i)), nullptr, false, ignore_comments).accept(true);
  20703. }
  20704. template<typename IteratorType>
  20705. static bool accept(IteratorType first, IteratorType last,
  20706. const bool ignore_comments = false)
  20707. {
  20708. return parser(detail::input_adapter(std::move(first), std::move(last)), nullptr, false, ignore_comments).accept(true);
  20709. }
  20710. JSON_HEDLEY_WARN_UNUSED_RESULT
  20711. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, accept(ptr, ptr + len))
  20712. static bool accept(detail::span_input_adapter&& i,
  20713. const bool ignore_comments = false)
  20714. {
  20715. return parser(i.get(), nullptr, false, ignore_comments).accept(true);
  20716. }
  20717. /*!
  20718. @brief generate SAX events
  20719. The SAX event lister must follow the interface of @ref json_sax.
  20720. This function reads from a compatible input. Examples are:
  20721. - an std::istream object
  20722. - a FILE pointer
  20723. - a C-style array of characters
  20724. - a pointer to a null-terminated string of single byte characters
  20725. - an object obj for which begin(obj) and end(obj) produces a valid pair of
  20726. iterators.
  20727. @param[in] i input to read from
  20728. @param[in,out] sax SAX event listener
  20729. @param[in] format the format to parse (JSON, CBOR, MessagePack, or UBJSON)
  20730. @param[in] strict whether the input has to be consumed completely
  20731. @param[in] ignore_comments whether comments should be ignored and treated
  20732. like whitespace (true) or yield a parse error (true); (optional, false by
  20733. default); only applies to the JSON file format.
  20734. @return return value of the last processed SAX event
  20735. @throw parse_error.101 if a parse error occurs; example: `""unexpected end
  20736. of input; expected string literal""`
  20737. @throw parse_error.102 if to_unicode fails or surrogate error
  20738. @throw parse_error.103 if to_unicode fails
  20739. @complexity Linear in the length of the input. The parser is a predictive
  20740. LL(1) parser. The complexity can be higher if the SAX consumer @a sax has
  20741. a super-linear complexity.
  20742. @note A UTF-8 byte order mark is silently ignored.
  20743. @liveexample{The example below demonstrates the `sax_parse()` function
  20744. reading from string and processing the events with a user-defined SAX
  20745. event consumer.,sax_parse}
  20746. @since version 3.2.0
  20747. */
  20748. template <typename InputType, typename SAX>
  20749. JSON_HEDLEY_NON_NULL(2)
  20750. static bool sax_parse(InputType&& i, SAX* sax,
  20751. input_format_t format = input_format_t::json,
  20752. const bool strict = true,
  20753. const bool ignore_comments = false)
  20754. {
  20755. auto ia = detail::input_adapter(std::forward<InputType>(i));
  20756. return format == input_format_t::json
  20757. ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
  20758. : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia)).sax_parse(format, sax, strict);
  20759. }
  20760. template<class IteratorType, class SAX>
  20761. JSON_HEDLEY_NON_NULL(3)
  20762. static bool sax_parse(IteratorType first, IteratorType last, SAX* sax,
  20763. input_format_t format = input_format_t::json,
  20764. const bool strict = true,
  20765. const bool ignore_comments = false)
  20766. {
  20767. auto ia = detail::input_adapter(std::move(first), std::move(last));
  20768. return format == input_format_t::json
  20769. ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
  20770. : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia)).sax_parse(format, sax, strict);
  20771. }
  20772. template <typename SAX>
  20773. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, sax_parse(ptr, ptr + len, ...))
  20774. JSON_HEDLEY_NON_NULL(2)
  20775. static bool sax_parse(detail::span_input_adapter&& i, SAX* sax,
  20776. input_format_t format = input_format_t::json,
  20777. const bool strict = true,
  20778. const bool ignore_comments = false)
  20779. {
  20780. auto ia = i.get();
  20781. return format == input_format_t::json
  20782. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  20783. ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
  20784. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  20785. : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia)).sax_parse(format, sax, strict);
  20786. }
  20787. #ifndef JSON_NO_IO
  20788. /*!
  20789. @brief deserialize from stream
  20790. @deprecated This stream operator is deprecated and will be removed in
  20791. version 4.0.0 of the library. Please use
  20792. @ref operator>>(std::istream&, basic_json&)
  20793. instead; that is, replace calls like `j << i;` with `i >> j;`.
  20794. @since version 1.0.0; deprecated since version 3.0.0
  20795. */
  20796. JSON_HEDLEY_DEPRECATED_FOR(3.0.0, operator>>(std::istream&, basic_json&))
  20797. friend std::istream& operator<<(basic_json& j, std::istream& i)
  20798. {
  20799. return operator>>(i, j);
  20800. }
  20801. /*!
  20802. @brief deserialize from stream
  20803. Deserializes an input stream to a JSON value.
  20804. @param[in,out] i input stream to read a serialized JSON value from
  20805. @param[in,out] j JSON value to write the deserialized input to
  20806. @throw parse_error.101 in case of an unexpected token
  20807. @throw parse_error.102 if to_unicode fails or surrogate error
  20808. @throw parse_error.103 if to_unicode fails
  20809. @complexity Linear in the length of the input. The parser is a predictive
  20810. LL(1) parser.
  20811. @note A UTF-8 byte order mark is silently ignored.
  20812. @liveexample{The example below shows how a JSON value is constructed by
  20813. reading a serialization from a stream.,operator_deserialize}
  20814. @sa parse(std::istream&, const parser_callback_t) for a variant with a
  20815. parser callback function to filter values while parsing
  20816. @since version 1.0.0
  20817. */
  20818. friend std::istream& operator>>(std::istream& i, basic_json& j)
  20819. {
  20820. parser(detail::input_adapter(i)).parse(false, j);
  20821. return i;
  20822. }
  20823. #endif // JSON_NO_IO
  20824. /// @}
  20825. ///////////////////////////
  20826. // convenience functions //
  20827. ///////////////////////////
  20828. /*!
  20829. @brief return the type as string
  20830. Returns the type name as string to be used in error messages - usually to
  20831. indicate that a function was called on a wrong JSON type.
  20832. @return a string representation of a the @a m_type member:
  20833. Value type | return value
  20834. ----------- | -------------
  20835. null | `"null"`
  20836. boolean | `"boolean"`
  20837. string | `"string"`
  20838. number | `"number"` (for all number types)
  20839. object | `"object"`
  20840. array | `"array"`
  20841. binary | `"binary"`
  20842. discarded | `"discarded"`
  20843. @exceptionsafety No-throw guarantee: this function never throws exceptions.
  20844. @complexity Constant.
  20845. @liveexample{The following code exemplifies `type_name()` for all JSON
  20846. types.,type_name}
  20847. @sa see @ref type() -- return the type of the JSON value
  20848. @sa see @ref operator value_t() -- return the type of the JSON value (implicit)
  20849. @since version 1.0.0, public since 2.1.0, `const char*` and `noexcept`
  20850. since 3.0.0
  20851. */
  20852. JSON_HEDLEY_RETURNS_NON_NULL
  20853. const char* type_name() const noexcept
  20854. {
  20855. {
  20856. switch (m_type)
  20857. {
  20858. case value_t::null:
  20859. return "null";
  20860. case value_t::object:
  20861. return "object";
  20862. case value_t::array:
  20863. return "array";
  20864. case value_t::string:
  20865. return "string";
  20866. case value_t::boolean:
  20867. return "boolean";
  20868. case value_t::binary:
  20869. return "binary";
  20870. case value_t::discarded:
  20871. return "discarded";
  20872. case value_t::number_integer:
  20873. case value_t::number_unsigned:
  20874. case value_t::number_float:
  20875. default:
  20876. return "number";
  20877. }
  20878. }
  20879. }
  20880. JSON_PRIVATE_UNLESS_TESTED:
  20881. //////////////////////
  20882. // member variables //
  20883. //////////////////////
  20884. /// the type of the current element
  20885. value_t m_type = value_t::null;
  20886. /// the value of the current element
  20887. json_value m_value = {};
  20888. #if JSON_DIAGNOSTICS
  20889. /// a pointer to a parent value (for debugging purposes)
  20890. basic_json* m_parent = nullptr;
  20891. #endif
  20892. //////////////////////////////////////////
  20893. // binary serialization/deserialization //
  20894. //////////////////////////////////////////
  20895. /// @name binary serialization/deserialization support
  20896. /// @{
  20897. public:
  20898. /*!
  20899. @brief create a CBOR serialization of a given JSON value
  20900. Serializes a given JSON value @a j to a byte vector using the CBOR (Concise
  20901. Binary Object Representation) serialization format. CBOR is a binary
  20902. serialization format which aims to be more compact than JSON itself, yet
  20903. more efficient to parse.
  20904. The library uses the following mapping from JSON values types to
  20905. CBOR types according to the CBOR specification (RFC 7049):
  20906. JSON value type | value/range | CBOR type | first byte
  20907. --------------- | ------------------------------------------ | ---------------------------------- | ---------------
  20908. null | `null` | Null | 0xF6
  20909. boolean | `true` | True | 0xF5
  20910. boolean | `false` | False | 0xF4
  20911. number_integer | -9223372036854775808..-2147483649 | Negative integer (8 bytes follow) | 0x3B
  20912. number_integer | -2147483648..-32769 | Negative integer (4 bytes follow) | 0x3A
  20913. number_integer | -32768..-129 | Negative integer (2 bytes follow) | 0x39
  20914. number_integer | -128..-25 | Negative integer (1 byte follow) | 0x38
  20915. number_integer | -24..-1 | Negative integer | 0x20..0x37
  20916. number_integer | 0..23 | Integer | 0x00..0x17
  20917. number_integer | 24..255 | Unsigned integer (1 byte follow) | 0x18
  20918. number_integer | 256..65535 | Unsigned integer (2 bytes follow) | 0x19
  20919. number_integer | 65536..4294967295 | Unsigned integer (4 bytes follow) | 0x1A
  20920. number_integer | 4294967296..18446744073709551615 | Unsigned integer (8 bytes follow) | 0x1B
  20921. number_unsigned | 0..23 | Integer | 0x00..0x17
  20922. number_unsigned | 24..255 | Unsigned integer (1 byte follow) | 0x18
  20923. number_unsigned | 256..65535 | Unsigned integer (2 bytes follow) | 0x19
  20924. number_unsigned | 65536..4294967295 | Unsigned integer (4 bytes follow) | 0x1A
  20925. number_unsigned | 4294967296..18446744073709551615 | Unsigned integer (8 bytes follow) | 0x1B
  20926. number_float | *any value representable by a float* | Single-Precision Float | 0xFA
  20927. number_float | *any value NOT representable by a float* | Double-Precision Float | 0xFB
  20928. string | *length*: 0..23 | UTF-8 string | 0x60..0x77
  20929. string | *length*: 23..255 | UTF-8 string (1 byte follow) | 0x78
  20930. string | *length*: 256..65535 | UTF-8 string (2 bytes follow) | 0x79
  20931. string | *length*: 65536..4294967295 | UTF-8 string (4 bytes follow) | 0x7A
  20932. string | *length*: 4294967296..18446744073709551615 | UTF-8 string (8 bytes follow) | 0x7B
  20933. array | *size*: 0..23 | array | 0x80..0x97
  20934. array | *size*: 23..255 | array (1 byte follow) | 0x98
  20935. array | *size*: 256..65535 | array (2 bytes follow) | 0x99
  20936. array | *size*: 65536..4294967295 | array (4 bytes follow) | 0x9A
  20937. array | *size*: 4294967296..18446744073709551615 | array (8 bytes follow) | 0x9B
  20938. object | *size*: 0..23 | map | 0xA0..0xB7
  20939. object | *size*: 23..255 | map (1 byte follow) | 0xB8
  20940. object | *size*: 256..65535 | map (2 bytes follow) | 0xB9
  20941. object | *size*: 65536..4294967295 | map (4 bytes follow) | 0xBA
  20942. object | *size*: 4294967296..18446744073709551615 | map (8 bytes follow) | 0xBB
  20943. binary | *size*: 0..23 | byte string | 0x40..0x57
  20944. binary | *size*: 23..255 | byte string (1 byte follow) | 0x58
  20945. binary | *size*: 256..65535 | byte string (2 bytes follow) | 0x59
  20946. binary | *size*: 65536..4294967295 | byte string (4 bytes follow) | 0x5A
  20947. binary | *size*: 4294967296..18446744073709551615 | byte string (8 bytes follow) | 0x5B
  20948. Binary values with subtype are mapped to tagged values (0xD8..0xDB)
  20949. depending on the subtype, followed by a byte string, see "binary" cells
  20950. in the table above.
  20951. @note The mapping is **complete** in the sense that any JSON value type
  20952. can be converted to a CBOR value.
  20953. @note If NaN or Infinity are stored inside a JSON number, they are
  20954. serialized properly. This behavior differs from the @ref dump()
  20955. function which serializes NaN or Infinity to `null`.
  20956. @note The following CBOR types are not used in the conversion:
  20957. - UTF-8 strings terminated by "break" (0x7F)
  20958. - arrays terminated by "break" (0x9F)
  20959. - maps terminated by "break" (0xBF)
  20960. - byte strings terminated by "break" (0x5F)
  20961. - date/time (0xC0..0xC1)
  20962. - bignum (0xC2..0xC3)
  20963. - decimal fraction (0xC4)
  20964. - bigfloat (0xC5)
  20965. - expected conversions (0xD5..0xD7)
  20966. - simple values (0xE0..0xF3, 0xF8)
  20967. - undefined (0xF7)
  20968. - half-precision floats (0xF9)
  20969. - break (0xFF)
  20970. @param[in] j JSON value to serialize
  20971. @return CBOR serialization as byte vector
  20972. @complexity Linear in the size of the JSON value @a j.
  20973. @liveexample{The example shows the serialization of a JSON value to a byte
  20974. vector in CBOR format.,to_cbor}
  20975. @sa http://cbor.io
  20976. @sa see @ref from_cbor(InputType&&, const bool, const bool, const cbor_tag_handler_t) for the
  20977. analogous deserialization
  20978. @sa see @ref to_msgpack(const basic_json&) for the related MessagePack format
  20979. @sa see @ref to_ubjson(const basic_json&, const bool, const bool) for the
  20980. related UBJSON format
  20981. @since version 2.0.9; compact representation of floating-point numbers
  20982. since version 3.8.0
  20983. */
  20984. static std::vector<std::uint8_t> to_cbor(const basic_json& j)
  20985. {
  20986. std::vector<std::uint8_t> result;
  20987. to_cbor(j, result);
  20988. return result;
  20989. }
  20990. static void to_cbor(const basic_json& j, detail::output_adapter<std::uint8_t> o)
  20991. {
  20992. binary_writer<std::uint8_t>(o).write_cbor(j);
  20993. }
  20994. static void to_cbor(const basic_json& j, detail::output_adapter<char> o)
  20995. {
  20996. binary_writer<char>(o).write_cbor(j);
  20997. }
  20998. /*!
  20999. @brief create a MessagePack serialization of a given JSON value
  21000. Serializes a given JSON value @a j to a byte vector using the MessagePack
  21001. serialization format. MessagePack is a binary serialization format which
  21002. aims to be more compact than JSON itself, yet more efficient to parse.
  21003. The library uses the following mapping from JSON values types to
  21004. MessagePack types according to the MessagePack specification:
  21005. JSON value type | value/range | MessagePack type | first byte
  21006. --------------- | --------------------------------- | ---------------- | ----------
  21007. null | `null` | nil | 0xC0
  21008. boolean | `true` | true | 0xC3
  21009. boolean | `false` | false | 0xC2
  21010. number_integer | -9223372036854775808..-2147483649 | int64 | 0xD3
  21011. number_integer | -2147483648..-32769 | int32 | 0xD2
  21012. number_integer | -32768..-129 | int16 | 0xD1
  21013. number_integer | -128..-33 | int8 | 0xD0
  21014. number_integer | -32..-1 | negative fixint | 0xE0..0xFF
  21015. number_integer | 0..127 | positive fixint | 0x00..0x7F
  21016. number_integer | 128..255 | uint 8 | 0xCC
  21017. number_integer | 256..65535 | uint 16 | 0xCD
  21018. number_integer | 65536..4294967295 | uint 32 | 0xCE
  21019. number_integer | 4294967296..18446744073709551615 | uint 64 | 0xCF
  21020. number_unsigned | 0..127 | positive fixint | 0x00..0x7F
  21021. number_unsigned | 128..255 | uint 8 | 0xCC
  21022. number_unsigned | 256..65535 | uint 16 | 0xCD
  21023. number_unsigned | 65536..4294967295 | uint 32 | 0xCE
  21024. number_unsigned | 4294967296..18446744073709551615 | uint 64 | 0xCF
  21025. number_float | *any value representable by a float* | float 32 | 0xCA
  21026. number_float | *any value NOT representable by a float* | float 64 | 0xCB
  21027. string | *length*: 0..31 | fixstr | 0xA0..0xBF
  21028. string | *length*: 32..255 | str 8 | 0xD9
  21029. string | *length*: 256..65535 | str 16 | 0xDA
  21030. string | *length*: 65536..4294967295 | str 32 | 0xDB
  21031. array | *size*: 0..15 | fixarray | 0x90..0x9F
  21032. array | *size*: 16..65535 | array 16 | 0xDC
  21033. array | *size*: 65536..4294967295 | array 32 | 0xDD
  21034. object | *size*: 0..15 | fix map | 0x80..0x8F
  21035. object | *size*: 16..65535 | map 16 | 0xDE
  21036. object | *size*: 65536..4294967295 | map 32 | 0xDF
  21037. binary | *size*: 0..255 | bin 8 | 0xC4
  21038. binary | *size*: 256..65535 | bin 16 | 0xC5
  21039. binary | *size*: 65536..4294967295 | bin 32 | 0xC6
  21040. @note The mapping is **complete** in the sense that any JSON value type
  21041. can be converted to a MessagePack value.
  21042. @note The following values can **not** be converted to a MessagePack value:
  21043. - strings with more than 4294967295 bytes
  21044. - byte strings with more than 4294967295 bytes
  21045. - arrays with more than 4294967295 elements
  21046. - objects with more than 4294967295 elements
  21047. @note Any MessagePack output created @ref to_msgpack can be successfully
  21048. parsed by @ref from_msgpack.
  21049. @note If NaN or Infinity are stored inside a JSON number, they are
  21050. serialized properly. This behavior differs from the @ref dump()
  21051. function which serializes NaN or Infinity to `null`.
  21052. @param[in] j JSON value to serialize
  21053. @return MessagePack serialization as byte vector
  21054. @complexity Linear in the size of the JSON value @a j.
  21055. @liveexample{The example shows the serialization of a JSON value to a byte
  21056. vector in MessagePack format.,to_msgpack}
  21057. @sa http://msgpack.org
  21058. @sa see @ref from_msgpack for the analogous deserialization
  21059. @sa see @ref to_cbor(const basic_json& for the related CBOR format
  21060. @sa see @ref to_ubjson(const basic_json&, const bool, const bool) for the
  21061. related UBJSON format
  21062. @since version 2.0.9
  21063. */
  21064. static std::vector<std::uint8_t> to_msgpack(const basic_json& j)
  21065. {
  21066. std::vector<std::uint8_t> result;
  21067. to_msgpack(j, result);
  21068. return result;
  21069. }
  21070. static void to_msgpack(const basic_json& j, detail::output_adapter<std::uint8_t> o)
  21071. {
  21072. binary_writer<std::uint8_t>(o).write_msgpack(j);
  21073. }
  21074. static void to_msgpack(const basic_json& j, detail::output_adapter<char> o)
  21075. {
  21076. binary_writer<char>(o).write_msgpack(j);
  21077. }
  21078. /*!
  21079. @brief create a UBJSON serialization of a given JSON value
  21080. Serializes a given JSON value @a j to a byte vector using the UBJSON
  21081. (Universal Binary JSON) serialization format. UBJSON aims to be more compact
  21082. than JSON itself, yet more efficient to parse.
  21083. The library uses the following mapping from JSON values types to
  21084. UBJSON types according to the UBJSON specification:
  21085. JSON value type | value/range | UBJSON type | marker
  21086. --------------- | --------------------------------- | ----------- | ------
  21087. null | `null` | null | `Z`
  21088. boolean | `true` | true | `T`
  21089. boolean | `false` | false | `F`
  21090. number_integer | -9223372036854775808..-2147483649 | int64 | `L`
  21091. number_integer | -2147483648..-32769 | int32 | `l`
  21092. number_integer | -32768..-129 | int16 | `I`
  21093. number_integer | -128..127 | int8 | `i`
  21094. number_integer | 128..255 | uint8 | `U`
  21095. number_integer | 256..32767 | int16 | `I`
  21096. number_integer | 32768..2147483647 | int32 | `l`
  21097. number_integer | 2147483648..9223372036854775807 | int64 | `L`
  21098. number_unsigned | 0..127 | int8 | `i`
  21099. number_unsigned | 128..255 | uint8 | `U`
  21100. number_unsigned | 256..32767 | int16 | `I`
  21101. number_unsigned | 32768..2147483647 | int32 | `l`
  21102. number_unsigned | 2147483648..9223372036854775807 | int64 | `L`
  21103. number_unsigned | 2147483649..18446744073709551615 | high-precision | `H`
  21104. number_float | *any value* | float64 | `D`
  21105. string | *with shortest length indicator* | string | `S`
  21106. array | *see notes on optimized format* | array | `[`
  21107. object | *see notes on optimized format* | map | `{`
  21108. @note The mapping is **complete** in the sense that any JSON value type
  21109. can be converted to a UBJSON value.
  21110. @note The following values can **not** be converted to a UBJSON value:
  21111. - strings with more than 9223372036854775807 bytes (theoretical)
  21112. @note The following markers are not used in the conversion:
  21113. - `Z`: no-op values are not created.
  21114. - `C`: single-byte strings are serialized with `S` markers.
  21115. @note Any UBJSON output created @ref to_ubjson can be successfully parsed
  21116. by @ref from_ubjson.
  21117. @note If NaN or Infinity are stored inside a JSON number, they are
  21118. serialized properly. This behavior differs from the @ref dump()
  21119. function which serializes NaN or Infinity to `null`.
  21120. @note The optimized formats for containers are supported: Parameter
  21121. @a use_size adds size information to the beginning of a container and
  21122. removes the closing marker. Parameter @a use_type further checks
  21123. whether all elements of a container have the same type and adds the
  21124. type marker to the beginning of the container. The @a use_type
  21125. parameter must only be used together with @a use_size = true. Note
  21126. that @a use_size = true alone may result in larger representations -
  21127. the benefit of this parameter is that the receiving side is
  21128. immediately informed on the number of elements of the container.
  21129. @note If the JSON data contains the binary type, the value stored is a list
  21130. of integers, as suggested by the UBJSON documentation. In particular,
  21131. this means that serialization and the deserialization of a JSON
  21132. containing binary values into UBJSON and back will result in a
  21133. different JSON object.
  21134. @param[in] j JSON value to serialize
  21135. @param[in] use_size whether to add size annotations to container types
  21136. @param[in] use_type whether to add type annotations to container types
  21137. (must be combined with @a use_size = true)
  21138. @return UBJSON serialization as byte vector
  21139. @complexity Linear in the size of the JSON value @a j.
  21140. @liveexample{The example shows the serialization of a JSON value to a byte
  21141. vector in UBJSON format.,to_ubjson}
  21142. @sa http://ubjson.org
  21143. @sa see @ref from_ubjson(InputType&&, const bool, const bool) for the
  21144. analogous deserialization
  21145. @sa see @ref to_cbor(const basic_json& for the related CBOR format
  21146. @sa see @ref to_msgpack(const basic_json&) for the related MessagePack format
  21147. @since version 3.1.0
  21148. */
  21149. static std::vector<std::uint8_t> to_ubjson(const basic_json& j,
  21150. const bool use_size = false,
  21151. const bool use_type = false)
  21152. {
  21153. std::vector<std::uint8_t> result;
  21154. to_ubjson(j, result, use_size, use_type);
  21155. return result;
  21156. }
  21157. static void to_ubjson(const basic_json& j, detail::output_adapter<std::uint8_t> o,
  21158. const bool use_size = false, const bool use_type = false)
  21159. {
  21160. binary_writer<std::uint8_t>(o).write_ubjson(j, use_size, use_type);
  21161. }
  21162. static void to_ubjson(const basic_json& j, detail::output_adapter<char> o,
  21163. const bool use_size = false, const bool use_type = false)
  21164. {
  21165. binary_writer<char>(o).write_ubjson(j, use_size, use_type);
  21166. }
  21167. /*!
  21168. @brief Serializes the given JSON object `j` to BSON and returns a vector
  21169. containing the corresponding BSON-representation.
  21170. BSON (Binary JSON) is a binary format in which zero or more ordered key/value pairs are
  21171. stored as a single entity (a so-called document).
  21172. The library uses the following mapping from JSON values types to BSON types:
  21173. JSON value type | value/range | BSON type | marker
  21174. --------------- | --------------------------------- | ----------- | ------
  21175. null | `null` | null | 0x0A
  21176. boolean | `true`, `false` | boolean | 0x08
  21177. number_integer | -9223372036854775808..-2147483649 | int64 | 0x12
  21178. number_integer | -2147483648..2147483647 | int32 | 0x10
  21179. number_integer | 2147483648..9223372036854775807 | int64 | 0x12
  21180. number_unsigned | 0..2147483647 | int32 | 0x10
  21181. number_unsigned | 2147483648..9223372036854775807 | int64 | 0x12
  21182. number_unsigned | 9223372036854775808..18446744073709551615| -- | --
  21183. number_float | *any value* | double | 0x01
  21184. string | *any value* | string | 0x02
  21185. array | *any value* | document | 0x04
  21186. object | *any value* | document | 0x03
  21187. binary | *any value* | binary | 0x05
  21188. @warning The mapping is **incomplete**, since only JSON-objects (and things
  21189. contained therein) can be serialized to BSON.
  21190. Also, integers larger than 9223372036854775807 cannot be serialized to BSON,
  21191. and the keys may not contain U+0000, since they are serialized a
  21192. zero-terminated c-strings.
  21193. @throw out_of_range.407 if `j.is_number_unsigned() && j.get<std::uint64_t>() > 9223372036854775807`
  21194. @throw out_of_range.409 if a key in `j` contains a NULL (U+0000)
  21195. @throw type_error.317 if `!j.is_object()`
  21196. @pre The input `j` is required to be an object: `j.is_object() == true`.
  21197. @note Any BSON output created via @ref to_bson can be successfully parsed
  21198. by @ref from_bson.
  21199. @param[in] j JSON value to serialize
  21200. @return BSON serialization as byte vector
  21201. @complexity Linear in the size of the JSON value @a j.
  21202. @liveexample{The example shows the serialization of a JSON value to a byte
  21203. vector in BSON format.,to_bson}
  21204. @sa http://bsonspec.org/spec.html
  21205. @sa see @ref from_bson(detail::input_adapter&&, const bool strict) for the
  21206. analogous deserialization
  21207. @sa see @ref to_ubjson(const basic_json&, const bool, const bool) for the
  21208. related UBJSON format
  21209. @sa see @ref to_cbor(const basic_json&) for the related CBOR format
  21210. @sa see @ref to_msgpack(const basic_json&) for the related MessagePack format
  21211. */
  21212. static std::vector<std::uint8_t> to_bson(const basic_json& j)
  21213. {
  21214. std::vector<std::uint8_t> result;
  21215. to_bson(j, result);
  21216. return result;
  21217. }
  21218. /*!
  21219. @brief Serializes the given JSON object `j` to BSON and forwards the
  21220. corresponding BSON-representation to the given output_adapter `o`.
  21221. @param j The JSON object to convert to BSON.
  21222. @param o The output adapter that receives the binary BSON representation.
  21223. @pre The input `j` shall be an object: `j.is_object() == true`
  21224. @sa see @ref to_bson(const basic_json&)
  21225. */
  21226. static void to_bson(const basic_json& j, detail::output_adapter<std::uint8_t> o)
  21227. {
  21228. binary_writer<std::uint8_t>(o).write_bson(j);
  21229. }
  21230. /*!
  21231. @copydoc to_bson(const basic_json&, detail::output_adapter<std::uint8_t>)
  21232. */
  21233. static void to_bson(const basic_json& j, detail::output_adapter<char> o)
  21234. {
  21235. binary_writer<char>(o).write_bson(j);
  21236. }
  21237. /*!
  21238. @brief create a JSON value from an input in CBOR format
  21239. Deserializes a given input @a i to a JSON value using the CBOR (Concise
  21240. Binary Object Representation) serialization format.
  21241. The library maps CBOR types to JSON value types as follows:
  21242. CBOR type | JSON value type | first byte
  21243. ---------------------- | --------------- | ----------
  21244. Integer | number_unsigned | 0x00..0x17
  21245. Unsigned integer | number_unsigned | 0x18
  21246. Unsigned integer | number_unsigned | 0x19
  21247. Unsigned integer | number_unsigned | 0x1A
  21248. Unsigned integer | number_unsigned | 0x1B
  21249. Negative integer | number_integer | 0x20..0x37
  21250. Negative integer | number_integer | 0x38
  21251. Negative integer | number_integer | 0x39
  21252. Negative integer | number_integer | 0x3A
  21253. Negative integer | number_integer | 0x3B
  21254. Byte string | binary | 0x40..0x57
  21255. Byte string | binary | 0x58
  21256. Byte string | binary | 0x59
  21257. Byte string | binary | 0x5A
  21258. Byte string | binary | 0x5B
  21259. UTF-8 string | string | 0x60..0x77
  21260. UTF-8 string | string | 0x78
  21261. UTF-8 string | string | 0x79
  21262. UTF-8 string | string | 0x7A
  21263. UTF-8 string | string | 0x7B
  21264. UTF-8 string | string | 0x7F
  21265. array | array | 0x80..0x97
  21266. array | array | 0x98
  21267. array | array | 0x99
  21268. array | array | 0x9A
  21269. array | array | 0x9B
  21270. array | array | 0x9F
  21271. map | object | 0xA0..0xB7
  21272. map | object | 0xB8
  21273. map | object | 0xB9
  21274. map | object | 0xBA
  21275. map | object | 0xBB
  21276. map | object | 0xBF
  21277. False | `false` | 0xF4
  21278. True | `true` | 0xF5
  21279. Null | `null` | 0xF6
  21280. Half-Precision Float | number_float | 0xF9
  21281. Single-Precision Float | number_float | 0xFA
  21282. Double-Precision Float | number_float | 0xFB
  21283. @warning The mapping is **incomplete** in the sense that not all CBOR
  21284. types can be converted to a JSON value. The following CBOR types
  21285. are not supported and will yield parse errors (parse_error.112):
  21286. - date/time (0xC0..0xC1)
  21287. - bignum (0xC2..0xC3)
  21288. - decimal fraction (0xC4)
  21289. - bigfloat (0xC5)
  21290. - expected conversions (0xD5..0xD7)
  21291. - simple values (0xE0..0xF3, 0xF8)
  21292. - undefined (0xF7)
  21293. @warning CBOR allows map keys of any type, whereas JSON only allows
  21294. strings as keys in object values. Therefore, CBOR maps with keys
  21295. other than UTF-8 strings are rejected (parse_error.113).
  21296. @note Any CBOR output created @ref to_cbor can be successfully parsed by
  21297. @ref from_cbor.
  21298. @param[in] i an input in CBOR format convertible to an input adapter
  21299. @param[in] strict whether to expect the input to be consumed until EOF
  21300. (true by default)
  21301. @param[in] allow_exceptions whether to throw exceptions in case of a
  21302. parse error (optional, true by default)
  21303. @param[in] tag_handler how to treat CBOR tags (optional, error by default)
  21304. @return deserialized JSON value; in case of a parse error and
  21305. @a allow_exceptions set to `false`, the return value will be
  21306. value_t::discarded.
  21307. @throw parse_error.110 if the given input ends prematurely or the end of
  21308. file was not reached when @a strict was set to true
  21309. @throw parse_error.112 if unsupported features from CBOR were
  21310. used in the given input @a v or if the input is not valid CBOR
  21311. @throw parse_error.113 if a string was expected as map key, but not found
  21312. @complexity Linear in the size of the input @a i.
  21313. @liveexample{The example shows the deserialization of a byte vector in CBOR
  21314. format to a JSON value.,from_cbor}
  21315. @sa http://cbor.io
  21316. @sa see @ref to_cbor(const basic_json&) for the analogous serialization
  21317. @sa see @ref from_msgpack(InputType&&, const bool, const bool) for the
  21318. related MessagePack format
  21319. @sa see @ref from_ubjson(InputType&&, const bool, const bool) for the
  21320. related UBJSON format
  21321. @since version 2.0.9; parameter @a start_index since 2.1.1; changed to
  21322. consume input adapters, removed start_index parameter, and added
  21323. @a strict parameter since 3.0.0; added @a allow_exceptions parameter
  21324. since 3.2.0; added @a tag_handler parameter since 3.9.0.
  21325. */
  21326. template<typename InputType>
  21327. JSON_HEDLEY_WARN_UNUSED_RESULT
  21328. static basic_json from_cbor(InputType&& i,
  21329. const bool strict = true,
  21330. const bool allow_exceptions = true,
  21331. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  21332. {
  21333. basic_json result;
  21334. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  21335. auto ia = detail::input_adapter(std::forward<InputType>(i));
  21336. const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler);
  21337. return res ? result : basic_json(value_t::discarded);
  21338. }
  21339. /*!
  21340. @copydoc from_cbor(InputType&&, const bool, const bool, const cbor_tag_handler_t)
  21341. */
  21342. template<typename IteratorType>
  21343. JSON_HEDLEY_WARN_UNUSED_RESULT
  21344. static basic_json from_cbor(IteratorType first, IteratorType last,
  21345. const bool strict = true,
  21346. const bool allow_exceptions = true,
  21347. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  21348. {
  21349. basic_json result;
  21350. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  21351. auto ia = detail::input_adapter(std::move(first), std::move(last));
  21352. const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler);
  21353. return res ? result : basic_json(value_t::discarded);
  21354. }
  21355. template<typename T>
  21356. JSON_HEDLEY_WARN_UNUSED_RESULT
  21357. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_cbor(ptr, ptr + len))
  21358. static basic_json from_cbor(const T* ptr, std::size_t len,
  21359. const bool strict = true,
  21360. const bool allow_exceptions = true,
  21361. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  21362. {
  21363. return from_cbor(ptr, ptr + len, strict, allow_exceptions, tag_handler);
  21364. }
  21365. JSON_HEDLEY_WARN_UNUSED_RESULT
  21366. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_cbor(ptr, ptr + len))
  21367. static basic_json from_cbor(detail::span_input_adapter&& i,
  21368. const bool strict = true,
  21369. const bool allow_exceptions = true,
  21370. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  21371. {
  21372. basic_json result;
  21373. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  21374. auto ia = i.get();
  21375. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  21376. const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler);
  21377. return res ? result : basic_json(value_t::discarded);
  21378. }
  21379. /*!
  21380. @brief create a JSON value from an input in MessagePack format
  21381. Deserializes a given input @a i to a JSON value using the MessagePack
  21382. serialization format.
  21383. The library maps MessagePack types to JSON value types as follows:
  21384. MessagePack type | JSON value type | first byte
  21385. ---------------- | --------------- | ----------
  21386. positive fixint | number_unsigned | 0x00..0x7F
  21387. fixmap | object | 0x80..0x8F
  21388. fixarray | array | 0x90..0x9F
  21389. fixstr | string | 0xA0..0xBF
  21390. nil | `null` | 0xC0
  21391. false | `false` | 0xC2
  21392. true | `true` | 0xC3
  21393. float 32 | number_float | 0xCA
  21394. float 64 | number_float | 0xCB
  21395. uint 8 | number_unsigned | 0xCC
  21396. uint 16 | number_unsigned | 0xCD
  21397. uint 32 | number_unsigned | 0xCE
  21398. uint 64 | number_unsigned | 0xCF
  21399. int 8 | number_integer | 0xD0
  21400. int 16 | number_integer | 0xD1
  21401. int 32 | number_integer | 0xD2
  21402. int 64 | number_integer | 0xD3
  21403. str 8 | string | 0xD9
  21404. str 16 | string | 0xDA
  21405. str 32 | string | 0xDB
  21406. array 16 | array | 0xDC
  21407. array 32 | array | 0xDD
  21408. map 16 | object | 0xDE
  21409. map 32 | object | 0xDF
  21410. bin 8 | binary | 0xC4
  21411. bin 16 | binary | 0xC5
  21412. bin 32 | binary | 0xC6
  21413. ext 8 | binary | 0xC7
  21414. ext 16 | binary | 0xC8
  21415. ext 32 | binary | 0xC9
  21416. fixext 1 | binary | 0xD4
  21417. fixext 2 | binary | 0xD5
  21418. fixext 4 | binary | 0xD6
  21419. fixext 8 | binary | 0xD7
  21420. fixext 16 | binary | 0xD8
  21421. negative fixint | number_integer | 0xE0-0xFF
  21422. @note Any MessagePack output created @ref to_msgpack can be successfully
  21423. parsed by @ref from_msgpack.
  21424. @param[in] i an input in MessagePack format convertible to an input
  21425. adapter
  21426. @param[in] strict whether to expect the input to be consumed until EOF
  21427. (true by default)
  21428. @param[in] allow_exceptions whether to throw exceptions in case of a
  21429. parse error (optional, true by default)
  21430. @return deserialized JSON value; in case of a parse error and
  21431. @a allow_exceptions set to `false`, the return value will be
  21432. value_t::discarded.
  21433. @throw parse_error.110 if the given input ends prematurely or the end of
  21434. file was not reached when @a strict was set to true
  21435. @throw parse_error.112 if unsupported features from MessagePack were
  21436. used in the given input @a i or if the input is not valid MessagePack
  21437. @throw parse_error.113 if a string was expected as map key, but not found
  21438. @complexity Linear in the size of the input @a i.
  21439. @liveexample{The example shows the deserialization of a byte vector in
  21440. MessagePack format to a JSON value.,from_msgpack}
  21441. @sa http://msgpack.org
  21442. @sa see @ref to_msgpack(const basic_json&) for the analogous serialization
  21443. @sa see @ref from_cbor(InputType&&, const bool, const bool, const cbor_tag_handler_t) for the
  21444. related CBOR format
  21445. @sa see @ref from_ubjson(InputType&&, const bool, const bool) for
  21446. the related UBJSON format
  21447. @sa see @ref from_bson(InputType&&, const bool, const bool) for
  21448. the related BSON format
  21449. @since version 2.0.9; parameter @a start_index since 2.1.1; changed to
  21450. consume input adapters, removed start_index parameter, and added
  21451. @a strict parameter since 3.0.0; added @a allow_exceptions parameter
  21452. since 3.2.0
  21453. */
  21454. template<typename InputType>
  21455. JSON_HEDLEY_WARN_UNUSED_RESULT
  21456. static basic_json from_msgpack(InputType&& i,
  21457. const bool strict = true,
  21458. const bool allow_exceptions = true)
  21459. {
  21460. basic_json result;
  21461. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  21462. auto ia = detail::input_adapter(std::forward<InputType>(i));
  21463. const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::msgpack, &sdp, strict);
  21464. return res ? result : basic_json(value_t::discarded);
  21465. }
  21466. /*!
  21467. @copydoc from_msgpack(InputType&&, const bool, const bool)
  21468. */
  21469. template<typename IteratorType>
  21470. JSON_HEDLEY_WARN_UNUSED_RESULT
  21471. static basic_json from_msgpack(IteratorType first, IteratorType last,
  21472. const bool strict = true,
  21473. const bool allow_exceptions = true)
  21474. {
  21475. basic_json result;
  21476. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  21477. auto ia = detail::input_adapter(std::move(first), std::move(last));
  21478. const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::msgpack, &sdp, strict);
  21479. return res ? result : basic_json(value_t::discarded);
  21480. }
  21481. template<typename T>
  21482. JSON_HEDLEY_WARN_UNUSED_RESULT
  21483. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_msgpack(ptr, ptr + len))
  21484. static basic_json from_msgpack(const T* ptr, std::size_t len,
  21485. const bool strict = true,
  21486. const bool allow_exceptions = true)
  21487. {
  21488. return from_msgpack(ptr, ptr + len, strict, allow_exceptions);
  21489. }
  21490. JSON_HEDLEY_WARN_UNUSED_RESULT
  21491. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_msgpack(ptr, ptr + len))
  21492. static basic_json from_msgpack(detail::span_input_adapter&& i,
  21493. const bool strict = true,
  21494. const bool allow_exceptions = true)
  21495. {
  21496. basic_json result;
  21497. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  21498. auto ia = i.get();
  21499. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  21500. const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::msgpack, &sdp, strict);
  21501. return res ? result : basic_json(value_t::discarded);
  21502. }
  21503. /*!
  21504. @brief create a JSON value from an input in UBJSON format
  21505. Deserializes a given input @a i to a JSON value using the UBJSON (Universal
  21506. Binary JSON) serialization format.
  21507. The library maps UBJSON types to JSON value types as follows:
  21508. UBJSON type | JSON value type | marker
  21509. ----------- | --------------------------------------- | ------
  21510. no-op | *no value, next value is read* | `N`
  21511. null | `null` | `Z`
  21512. false | `false` | `F`
  21513. true | `true` | `T`
  21514. float32 | number_float | `d`
  21515. float64 | number_float | `D`
  21516. uint8 | number_unsigned | `U`
  21517. int8 | number_integer | `i`
  21518. int16 | number_integer | `I`
  21519. int32 | number_integer | `l`
  21520. int64 | number_integer | `L`
  21521. high-precision number | number_integer, number_unsigned, or number_float - depends on number string | 'H'
  21522. string | string | `S`
  21523. char | string | `C`
  21524. array | array (optimized values are supported) | `[`
  21525. object | object (optimized values are supported) | `{`
  21526. @note The mapping is **complete** in the sense that any UBJSON value can
  21527. be converted to a JSON value.
  21528. @param[in] i an input in UBJSON format convertible to an input adapter
  21529. @param[in] strict whether to expect the input to be consumed until EOF
  21530. (true by default)
  21531. @param[in] allow_exceptions whether to throw exceptions in case of a
  21532. parse error (optional, true by default)
  21533. @return deserialized JSON value; in case of a parse error and
  21534. @a allow_exceptions set to `false`, the return value will be
  21535. value_t::discarded.
  21536. @throw parse_error.110 if the given input ends prematurely or the end of
  21537. file was not reached when @a strict was set to true
  21538. @throw parse_error.112 if a parse error occurs
  21539. @throw parse_error.113 if a string could not be parsed successfully
  21540. @complexity Linear in the size of the input @a i.
  21541. @liveexample{The example shows the deserialization of a byte vector in
  21542. UBJSON format to a JSON value.,from_ubjson}
  21543. @sa http://ubjson.org
  21544. @sa see @ref to_ubjson(const basic_json&, const bool, const bool) for the
  21545. analogous serialization
  21546. @sa see @ref from_cbor(InputType&&, const bool, const bool, const cbor_tag_handler_t) for the
  21547. related CBOR format
  21548. @sa see @ref from_msgpack(InputType&&, const bool, const bool) for
  21549. the related MessagePack format
  21550. @sa see @ref from_bson(InputType&&, const bool, const bool) for
  21551. the related BSON format
  21552. @since version 3.1.0; added @a allow_exceptions parameter since 3.2.0
  21553. */
  21554. template<typename InputType>
  21555. JSON_HEDLEY_WARN_UNUSED_RESULT
  21556. static basic_json from_ubjson(InputType&& i,
  21557. const bool strict = true,
  21558. const bool allow_exceptions = true)
  21559. {
  21560. basic_json result;
  21561. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  21562. auto ia = detail::input_adapter(std::forward<InputType>(i));
  21563. const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::ubjson, &sdp, strict);
  21564. return res ? result : basic_json(value_t::discarded);
  21565. }
  21566. /*!
  21567. @copydoc from_ubjson(InputType&&, const bool, const bool)
  21568. */
  21569. template<typename IteratorType>
  21570. JSON_HEDLEY_WARN_UNUSED_RESULT
  21571. static basic_json from_ubjson(IteratorType first, IteratorType last,
  21572. const bool strict = true,
  21573. const bool allow_exceptions = true)
  21574. {
  21575. basic_json result;
  21576. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  21577. auto ia = detail::input_adapter(std::move(first), std::move(last));
  21578. const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::ubjson, &sdp, strict);
  21579. return res ? result : basic_json(value_t::discarded);
  21580. }
  21581. template<typename T>
  21582. JSON_HEDLEY_WARN_UNUSED_RESULT
  21583. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_ubjson(ptr, ptr + len))
  21584. static basic_json from_ubjson(const T* ptr, std::size_t len,
  21585. const bool strict = true,
  21586. const bool allow_exceptions = true)
  21587. {
  21588. return from_ubjson(ptr, ptr + len, strict, allow_exceptions);
  21589. }
  21590. JSON_HEDLEY_WARN_UNUSED_RESULT
  21591. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_ubjson(ptr, ptr + len))
  21592. static basic_json from_ubjson(detail::span_input_adapter&& i,
  21593. const bool strict = true,
  21594. const bool allow_exceptions = true)
  21595. {
  21596. basic_json result;
  21597. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  21598. auto ia = i.get();
  21599. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  21600. const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::ubjson, &sdp, strict);
  21601. return res ? result : basic_json(value_t::discarded);
  21602. }
  21603. /*!
  21604. @brief Create a JSON value from an input in BSON format
  21605. Deserializes a given input @a i to a JSON value using the BSON (Binary JSON)
  21606. serialization format.
  21607. The library maps BSON record types to JSON value types as follows:
  21608. BSON type | BSON marker byte | JSON value type
  21609. --------------- | ---------------- | ---------------------------
  21610. double | 0x01 | number_float
  21611. string | 0x02 | string
  21612. document | 0x03 | object
  21613. array | 0x04 | array
  21614. binary | 0x05 | binary
  21615. undefined | 0x06 | still unsupported
  21616. ObjectId | 0x07 | still unsupported
  21617. boolean | 0x08 | boolean
  21618. UTC Date-Time | 0x09 | still unsupported
  21619. null | 0x0A | null
  21620. Regular Expr. | 0x0B | still unsupported
  21621. DB Pointer | 0x0C | still unsupported
  21622. JavaScript Code | 0x0D | still unsupported
  21623. Symbol | 0x0E | still unsupported
  21624. JavaScript Code | 0x0F | still unsupported
  21625. int32 | 0x10 | number_integer
  21626. Timestamp | 0x11 | still unsupported
  21627. 128-bit decimal float | 0x13 | still unsupported
  21628. Max Key | 0x7F | still unsupported
  21629. Min Key | 0xFF | still unsupported
  21630. @warning The mapping is **incomplete**. The unsupported mappings
  21631. are indicated in the table above.
  21632. @param[in] i an input in BSON format convertible to an input adapter
  21633. @param[in] strict whether to expect the input to be consumed until EOF
  21634. (true by default)
  21635. @param[in] allow_exceptions whether to throw exceptions in case of a
  21636. parse error (optional, true by default)
  21637. @return deserialized JSON value; in case of a parse error and
  21638. @a allow_exceptions set to `false`, the return value will be
  21639. value_t::discarded.
  21640. @throw parse_error.114 if an unsupported BSON record type is encountered
  21641. @complexity Linear in the size of the input @a i.
  21642. @liveexample{The example shows the deserialization of a byte vector in
  21643. BSON format to a JSON value.,from_bson}
  21644. @sa http://bsonspec.org/spec.html
  21645. @sa see @ref to_bson(const basic_json&) for the analogous serialization
  21646. @sa see @ref from_cbor(InputType&&, const bool, const bool, const cbor_tag_handler_t) for the
  21647. related CBOR format
  21648. @sa see @ref from_msgpack(InputType&&, const bool, const bool) for
  21649. the related MessagePack format
  21650. @sa see @ref from_ubjson(InputType&&, const bool, const bool) for the
  21651. related UBJSON format
  21652. */
  21653. template<typename InputType>
  21654. JSON_HEDLEY_WARN_UNUSED_RESULT
  21655. static basic_json from_bson(InputType&& i,
  21656. const bool strict = true,
  21657. const bool allow_exceptions = true)
  21658. {
  21659. basic_json result;
  21660. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  21661. auto ia = detail::input_adapter(std::forward<InputType>(i));
  21662. const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::bson, &sdp, strict);
  21663. return res ? result : basic_json(value_t::discarded);
  21664. }
  21665. /*!
  21666. @copydoc from_bson(InputType&&, const bool, const bool)
  21667. */
  21668. template<typename IteratorType>
  21669. JSON_HEDLEY_WARN_UNUSED_RESULT
  21670. static basic_json from_bson(IteratorType first, IteratorType last,
  21671. const bool strict = true,
  21672. const bool allow_exceptions = true)
  21673. {
  21674. basic_json result;
  21675. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  21676. auto ia = detail::input_adapter(std::move(first), std::move(last));
  21677. const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::bson, &sdp, strict);
  21678. return res ? result : basic_json(value_t::discarded);
  21679. }
  21680. template<typename T>
  21681. JSON_HEDLEY_WARN_UNUSED_RESULT
  21682. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_bson(ptr, ptr + len))
  21683. static basic_json from_bson(const T* ptr, std::size_t len,
  21684. const bool strict = true,
  21685. const bool allow_exceptions = true)
  21686. {
  21687. return from_bson(ptr, ptr + len, strict, allow_exceptions);
  21688. }
  21689. JSON_HEDLEY_WARN_UNUSED_RESULT
  21690. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_bson(ptr, ptr + len))
  21691. static basic_json from_bson(detail::span_input_adapter&& i,
  21692. const bool strict = true,
  21693. const bool allow_exceptions = true)
  21694. {
  21695. basic_json result;
  21696. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  21697. auto ia = i.get();
  21698. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  21699. const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::bson, &sdp, strict);
  21700. return res ? result : basic_json(value_t::discarded);
  21701. }
  21702. /// @}
  21703. //////////////////////////
  21704. // JSON Pointer support //
  21705. //////////////////////////
  21706. /// @name JSON Pointer functions
  21707. /// @{
  21708. /*!
  21709. @brief access specified element via JSON Pointer
  21710. Uses a JSON pointer to retrieve a reference to the respective JSON value.
  21711. No bound checking is performed. Similar to @ref operator[](const typename
  21712. object_t::key_type&), `null` values are created in arrays and objects if
  21713. necessary.
  21714. In particular:
  21715. - If the JSON pointer points to an object key that does not exist, it
  21716. is created an filled with a `null` value before a reference to it
  21717. is returned.
  21718. - If the JSON pointer points to an array index that does not exist, it
  21719. is created an filled with a `null` value before a reference to it
  21720. is returned. All indices between the current maximum and the given
  21721. index are also filled with `null`.
  21722. - The special value `-` is treated as a synonym for the index past the
  21723. end.
  21724. @param[in] ptr a JSON pointer
  21725. @return reference to the element pointed to by @a ptr
  21726. @complexity Constant.
  21727. @throw parse_error.106 if an array index begins with '0'
  21728. @throw parse_error.109 if an array index was not a number
  21729. @throw out_of_range.404 if the JSON pointer can not be resolved
  21730. @liveexample{The behavior is shown in the example.,operatorjson_pointer}
  21731. @since version 2.0.0
  21732. */
  21733. reference operator[](const json_pointer& ptr)
  21734. {
  21735. return ptr.get_unchecked(this);
  21736. }
  21737. /*!
  21738. @brief access specified element via JSON Pointer
  21739. Uses a JSON pointer to retrieve a reference to the respective JSON value.
  21740. No bound checking is performed. The function does not change the JSON
  21741. value; no `null` values are created. In particular, the special value
  21742. `-` yields an exception.
  21743. @param[in] ptr JSON pointer to the desired element
  21744. @return const reference to the element pointed to by @a ptr
  21745. @complexity Constant.
  21746. @throw parse_error.106 if an array index begins with '0'
  21747. @throw parse_error.109 if an array index was not a number
  21748. @throw out_of_range.402 if the array index '-' is used
  21749. @throw out_of_range.404 if the JSON pointer can not be resolved
  21750. @liveexample{The behavior is shown in the example.,operatorjson_pointer_const}
  21751. @since version 2.0.0
  21752. */
  21753. const_reference operator[](const json_pointer& ptr) const
  21754. {
  21755. return ptr.get_unchecked(this);
  21756. }
  21757. /*!
  21758. @brief access specified element via JSON Pointer
  21759. Returns a reference to the element at with specified JSON pointer @a ptr,
  21760. with bounds checking.
  21761. @param[in] ptr JSON pointer to the desired element
  21762. @return reference to the element pointed to by @a ptr
  21763. @throw parse_error.106 if an array index in the passed JSON pointer @a ptr
  21764. begins with '0'. See example below.
  21765. @throw parse_error.109 if an array index in the passed JSON pointer @a ptr
  21766. is not a number. See example below.
  21767. @throw out_of_range.401 if an array index in the passed JSON pointer @a ptr
  21768. is out of range. See example below.
  21769. @throw out_of_range.402 if the array index '-' is used in the passed JSON
  21770. pointer @a ptr. As `at` provides checked access (and no elements are
  21771. implicitly inserted), the index '-' is always invalid. See example below.
  21772. @throw out_of_range.403 if the JSON pointer describes a key of an object
  21773. which cannot be found. See example below.
  21774. @throw out_of_range.404 if the JSON pointer @a ptr can not be resolved.
  21775. See example below.
  21776. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  21777. changes in the JSON value.
  21778. @complexity Constant.
  21779. @since version 2.0.0
  21780. @liveexample{The behavior is shown in the example.,at_json_pointer}
  21781. */
  21782. reference at(const json_pointer& ptr)
  21783. {
  21784. return ptr.get_checked(this);
  21785. }
  21786. /*!
  21787. @brief access specified element via JSON Pointer
  21788. Returns a const reference to the element at with specified JSON pointer @a
  21789. ptr, with bounds checking.
  21790. @param[in] ptr JSON pointer to the desired element
  21791. @return reference to the element pointed to by @a ptr
  21792. @throw parse_error.106 if an array index in the passed JSON pointer @a ptr
  21793. begins with '0'. See example below.
  21794. @throw parse_error.109 if an array index in the passed JSON pointer @a ptr
  21795. is not a number. See example below.
  21796. @throw out_of_range.401 if an array index in the passed JSON pointer @a ptr
  21797. is out of range. See example below.
  21798. @throw out_of_range.402 if the array index '-' is used in the passed JSON
  21799. pointer @a ptr. As `at` provides checked access (and no elements are
  21800. implicitly inserted), the index '-' is always invalid. See example below.
  21801. @throw out_of_range.403 if the JSON pointer describes a key of an object
  21802. which cannot be found. See example below.
  21803. @throw out_of_range.404 if the JSON pointer @a ptr can not be resolved.
  21804. See example below.
  21805. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  21806. changes in the JSON value.
  21807. @complexity Constant.
  21808. @since version 2.0.0
  21809. @liveexample{The behavior is shown in the example.,at_json_pointer_const}
  21810. */
  21811. const_reference at(const json_pointer& ptr) const
  21812. {
  21813. return ptr.get_checked(this);
  21814. }
  21815. /*!
  21816. @brief return flattened JSON value
  21817. The function creates a JSON object whose keys are JSON pointers (see [RFC
  21818. 6901](https://tools.ietf.org/html/rfc6901)) and whose values are all
  21819. primitive. The original JSON value can be restored using the @ref
  21820. unflatten() function.
  21821. @return an object that maps JSON pointers to primitive values
  21822. @note Empty objects and arrays are flattened to `null` and will not be
  21823. reconstructed correctly by the @ref unflatten() function.
  21824. @complexity Linear in the size the JSON value.
  21825. @liveexample{The following code shows how a JSON object is flattened to an
  21826. object whose keys consist of JSON pointers.,flatten}
  21827. @sa see @ref unflatten() for the reverse function
  21828. @since version 2.0.0
  21829. */
  21830. basic_json flatten() const
  21831. {
  21832. basic_json result(value_t::object);
  21833. json_pointer::flatten("", *this, result);
  21834. return result;
  21835. }
  21836. /*!
  21837. @brief unflatten a previously flattened JSON value
  21838. The function restores the arbitrary nesting of a JSON value that has been
  21839. flattened before using the @ref flatten() function. The JSON value must
  21840. meet certain constraints:
  21841. 1. The value must be an object.
  21842. 2. The keys must be JSON pointers (see
  21843. [RFC 6901](https://tools.ietf.org/html/rfc6901))
  21844. 3. The mapped values must be primitive JSON types.
  21845. @return the original JSON from a flattened version
  21846. @note Empty objects and arrays are flattened by @ref flatten() to `null`
  21847. values and can not unflattened to their original type. Apart from
  21848. this example, for a JSON value `j`, the following is always true:
  21849. `j == j.flatten().unflatten()`.
  21850. @complexity Linear in the size the JSON value.
  21851. @throw type_error.314 if value is not an object
  21852. @throw type_error.315 if object values are not primitive
  21853. @liveexample{The following code shows how a flattened JSON object is
  21854. unflattened into the original nested JSON object.,unflatten}
  21855. @sa see @ref flatten() for the reverse function
  21856. @since version 2.0.0
  21857. */
  21858. basic_json unflatten() const
  21859. {
  21860. return json_pointer::unflatten(*this);
  21861. }
  21862. /// @}
  21863. //////////////////////////
  21864. // JSON Patch functions //
  21865. //////////////////////////
  21866. /// @name JSON Patch functions
  21867. /// @{
  21868. /*!
  21869. @brief applies a JSON patch
  21870. [JSON Patch](http://jsonpatch.com) defines a JSON document structure for
  21871. expressing a sequence of operations to apply to a JSON) document. With
  21872. this function, a JSON Patch is applied to the current JSON value by
  21873. executing all operations from the patch.
  21874. @param[in] json_patch JSON patch document
  21875. @return patched document
  21876. @note The application of a patch is atomic: Either all operations succeed
  21877. and the patched document is returned or an exception is thrown. In
  21878. any case, the original value is not changed: the patch is applied
  21879. to a copy of the value.
  21880. @throw parse_error.104 if the JSON patch does not consist of an array of
  21881. objects
  21882. @throw parse_error.105 if the JSON patch is malformed (e.g., mandatory
  21883. attributes are missing); example: `"operation add must have member path"`
  21884. @throw out_of_range.401 if an array index is out of range.
  21885. @throw out_of_range.403 if a JSON pointer inside the patch could not be
  21886. resolved successfully in the current JSON value; example: `"key baz not
  21887. found"`
  21888. @throw out_of_range.405 if JSON pointer has no parent ("add", "remove",
  21889. "move")
  21890. @throw other_error.501 if "test" operation was unsuccessful
  21891. @complexity Linear in the size of the JSON value and the length of the
  21892. JSON patch. As usually only a fraction of the JSON value is affected by
  21893. the patch, the complexity can usually be neglected.
  21894. @liveexample{The following code shows how a JSON patch is applied to a
  21895. value.,patch}
  21896. @sa see @ref diff -- create a JSON patch by comparing two JSON values
  21897. @sa [RFC 6902 (JSON Patch)](https://tools.ietf.org/html/rfc6902)
  21898. @sa [RFC 6901 (JSON Pointer)](https://tools.ietf.org/html/rfc6901)
  21899. @since version 2.0.0
  21900. */
  21901. basic_json patch(const basic_json& json_patch) const
  21902. {
  21903. // make a working copy to apply the patch to
  21904. basic_json result = *this;
  21905. // the valid JSON Patch operations
  21906. enum class patch_operations {add, remove, replace, move, copy, test, invalid};
  21907. const auto get_op = [](const std::string & op)
  21908. {
  21909. if (op == "add")
  21910. {
  21911. return patch_operations::add;
  21912. }
  21913. if (op == "remove")
  21914. {
  21915. return patch_operations::remove;
  21916. }
  21917. if (op == "replace")
  21918. {
  21919. return patch_operations::replace;
  21920. }
  21921. if (op == "move")
  21922. {
  21923. return patch_operations::move;
  21924. }
  21925. if (op == "copy")
  21926. {
  21927. return patch_operations::copy;
  21928. }
  21929. if (op == "test")
  21930. {
  21931. return patch_operations::test;
  21932. }
  21933. return patch_operations::invalid;
  21934. };
  21935. // wrapper for "add" operation; add value at ptr
  21936. const auto operation_add = [&result](json_pointer & ptr, basic_json val)
  21937. {
  21938. // adding to the root of the target document means replacing it
  21939. if (ptr.empty())
  21940. {
  21941. result = val;
  21942. return;
  21943. }
  21944. // make sure the top element of the pointer exists
  21945. json_pointer top_pointer = ptr.top();
  21946. if (top_pointer != ptr)
  21947. {
  21948. result.at(top_pointer);
  21949. }
  21950. // get reference to parent of JSON pointer ptr
  21951. const auto last_path = ptr.back();
  21952. ptr.pop_back();
  21953. basic_json& parent = result[ptr];
  21954. switch (parent.m_type)
  21955. {
  21956. case value_t::null:
  21957. case value_t::object:
  21958. {
  21959. // use operator[] to add value
  21960. parent[last_path] = val;
  21961. break;
  21962. }
  21963. case value_t::array:
  21964. {
  21965. if (last_path == "-")
  21966. {
  21967. // special case: append to back
  21968. parent.push_back(val);
  21969. }
  21970. else
  21971. {
  21972. const auto idx = json_pointer::array_index(last_path);
  21973. if (JSON_HEDLEY_UNLIKELY(idx > parent.size()))
  21974. {
  21975. // avoid undefined behavior
  21976. JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", parent));
  21977. }
  21978. // default case: insert add offset
  21979. parent.insert(parent.begin() + static_cast<difference_type>(idx), val);
  21980. }
  21981. break;
  21982. }
  21983. // if there exists a parent it cannot be primitive
  21984. case value_t::string: // LCOV_EXCL_LINE
  21985. case value_t::boolean: // LCOV_EXCL_LINE
  21986. case value_t::number_integer: // LCOV_EXCL_LINE
  21987. case value_t::number_unsigned: // LCOV_EXCL_LINE
  21988. case value_t::number_float: // LCOV_EXCL_LINE
  21989. case value_t::binary: // LCOV_EXCL_LINE
  21990. case value_t::discarded: // LCOV_EXCL_LINE
  21991. default: // LCOV_EXCL_LINE
  21992. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  21993. }
  21994. };
  21995. // wrapper for "remove" operation; remove value at ptr
  21996. const auto operation_remove = [this, &result](json_pointer & ptr)
  21997. {
  21998. // get reference to parent of JSON pointer ptr
  21999. const auto last_path = ptr.back();
  22000. ptr.pop_back();
  22001. basic_json& parent = result.at(ptr);
  22002. // remove child
  22003. if (parent.is_object())
  22004. {
  22005. // perform range check
  22006. auto it = parent.find(last_path);
  22007. if (JSON_HEDLEY_LIKELY(it != parent.end()))
  22008. {
  22009. parent.erase(it);
  22010. }
  22011. else
  22012. {
  22013. JSON_THROW(out_of_range::create(403, "key '" + last_path + "' not found", *this));
  22014. }
  22015. }
  22016. else if (parent.is_array())
  22017. {
  22018. // note erase performs range check
  22019. parent.erase(json_pointer::array_index(last_path));
  22020. }
  22021. };
  22022. // type check: top level value must be an array
  22023. if (JSON_HEDLEY_UNLIKELY(!json_patch.is_array()))
  22024. {
  22025. JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", json_patch));
  22026. }
  22027. // iterate and apply the operations
  22028. for (const auto& val : json_patch)
  22029. {
  22030. // wrapper to get a value for an operation
  22031. const auto get_value = [&val](const std::string & op,
  22032. const std::string & member,
  22033. bool string_type) -> basic_json &
  22034. {
  22035. // find value
  22036. auto it = val.m_value.object->find(member);
  22037. // context-sensitive error message
  22038. const auto error_msg = (op == "op") ? "operation" : "operation '" + op + "'";
  22039. // check if desired value is present
  22040. if (JSON_HEDLEY_UNLIKELY(it == val.m_value.object->end()))
  22041. {
  22042. // NOLINTNEXTLINE(performance-inefficient-string-concatenation)
  22043. JSON_THROW(parse_error::create(105, 0, error_msg + " must have member '" + member + "'", val));
  22044. }
  22045. // check if result is of type string
  22046. if (JSON_HEDLEY_UNLIKELY(string_type && !it->second.is_string()))
  22047. {
  22048. // NOLINTNEXTLINE(performance-inefficient-string-concatenation)
  22049. JSON_THROW(parse_error::create(105, 0, error_msg + " must have string member '" + member + "'", val));
  22050. }
  22051. // no error: return value
  22052. return it->second;
  22053. };
  22054. // type check: every element of the array must be an object
  22055. if (JSON_HEDLEY_UNLIKELY(!val.is_object()))
  22056. {
  22057. JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", val));
  22058. }
  22059. // collect mandatory members
  22060. const auto op = get_value("op", "op", true).template get<std::string>();
  22061. const auto path = get_value(op, "path", true).template get<std::string>();
  22062. json_pointer ptr(path);
  22063. switch (get_op(op))
  22064. {
  22065. case patch_operations::add:
  22066. {
  22067. operation_add(ptr, get_value("add", "value", false));
  22068. break;
  22069. }
  22070. case patch_operations::remove:
  22071. {
  22072. operation_remove(ptr);
  22073. break;
  22074. }
  22075. case patch_operations::replace:
  22076. {
  22077. // the "path" location must exist - use at()
  22078. result.at(ptr) = get_value("replace", "value", false);
  22079. break;
  22080. }
  22081. case patch_operations::move:
  22082. {
  22083. const auto from_path = get_value("move", "from", true).template get<std::string>();
  22084. json_pointer from_ptr(from_path);
  22085. // the "from" location must exist - use at()
  22086. basic_json v = result.at(from_ptr);
  22087. // The move operation is functionally identical to a
  22088. // "remove" operation on the "from" location, followed
  22089. // immediately by an "add" operation at the target
  22090. // location with the value that was just removed.
  22091. operation_remove(from_ptr);
  22092. operation_add(ptr, v);
  22093. break;
  22094. }
  22095. case patch_operations::copy:
  22096. {
  22097. const auto from_path = get_value("copy", "from", true).template get<std::string>();
  22098. const json_pointer from_ptr(from_path);
  22099. // the "from" location must exist - use at()
  22100. basic_json v = result.at(from_ptr);
  22101. // The copy is functionally identical to an "add"
  22102. // operation at the target location using the value
  22103. // specified in the "from" member.
  22104. operation_add(ptr, v);
  22105. break;
  22106. }
  22107. case patch_operations::test:
  22108. {
  22109. bool success = false;
  22110. JSON_TRY
  22111. {
  22112. // check if "value" matches the one at "path"
  22113. // the "path" location must exist - use at()
  22114. success = (result.at(ptr) == get_value("test", "value", false));
  22115. }
  22116. JSON_INTERNAL_CATCH (out_of_range&)
  22117. {
  22118. // ignore out of range errors: success remains false
  22119. }
  22120. // throw an exception if test fails
  22121. if (JSON_HEDLEY_UNLIKELY(!success))
  22122. {
  22123. JSON_THROW(other_error::create(501, "unsuccessful: " + val.dump(), val));
  22124. }
  22125. break;
  22126. }
  22127. case patch_operations::invalid:
  22128. default:
  22129. {
  22130. // op must be "add", "remove", "replace", "move", "copy", or
  22131. // "test"
  22132. JSON_THROW(parse_error::create(105, 0, "operation value '" + op + "' is invalid", val));
  22133. }
  22134. }
  22135. }
  22136. return result;
  22137. }
  22138. /*!
  22139. @brief creates a diff as a JSON patch
  22140. Creates a [JSON Patch](http://jsonpatch.com) so that value @a source can
  22141. be changed into the value @a target by calling @ref patch function.
  22142. @invariant For two JSON values @a source and @a target, the following code
  22143. yields always `true`:
  22144. @code {.cpp}
  22145. source.patch(diff(source, target)) == target;
  22146. @endcode
  22147. @note Currently, only `remove`, `add`, and `replace` operations are
  22148. generated.
  22149. @param[in] source JSON value to compare from
  22150. @param[in] target JSON value to compare against
  22151. @param[in] path helper value to create JSON pointers
  22152. @return a JSON patch to convert the @a source to @a target
  22153. @complexity Linear in the lengths of @a source and @a target.
  22154. @liveexample{The following code shows how a JSON patch is created as a
  22155. diff for two JSON values.,diff}
  22156. @sa see @ref patch -- apply a JSON patch
  22157. @sa see @ref merge_patch -- apply a JSON Merge Patch
  22158. @sa [RFC 6902 (JSON Patch)](https://tools.ietf.org/html/rfc6902)
  22159. @since version 2.0.0
  22160. */
  22161. JSON_HEDLEY_WARN_UNUSED_RESULT
  22162. static basic_json diff(const basic_json& source, const basic_json& target,
  22163. const std::string& path = "")
  22164. {
  22165. // the patch
  22166. basic_json result(value_t::array);
  22167. // if the values are the same, return empty patch
  22168. if (source == target)
  22169. {
  22170. return result;
  22171. }
  22172. if (source.type() != target.type())
  22173. {
  22174. // different types: replace value
  22175. result.push_back(
  22176. {
  22177. {"op", "replace"}, {"path", path}, {"value", target}
  22178. });
  22179. return result;
  22180. }
  22181. switch (source.type())
  22182. {
  22183. case value_t::array:
  22184. {
  22185. // first pass: traverse common elements
  22186. std::size_t i = 0;
  22187. while (i < source.size() && i < target.size())
  22188. {
  22189. // recursive call to compare array values at index i
  22190. auto temp_diff = diff(source[i], target[i], path + "/" + std::to_string(i));
  22191. result.insert(result.end(), temp_diff.begin(), temp_diff.end());
  22192. ++i;
  22193. }
  22194. // i now reached the end of at least one array
  22195. // in a second pass, traverse the remaining elements
  22196. // remove my remaining elements
  22197. const auto end_index = static_cast<difference_type>(result.size());
  22198. while (i < source.size())
  22199. {
  22200. // add operations in reverse order to avoid invalid
  22201. // indices
  22202. result.insert(result.begin() + end_index, object(
  22203. {
  22204. {"op", "remove"},
  22205. {"path", path + "/" + std::to_string(i)}
  22206. }));
  22207. ++i;
  22208. }
  22209. // add other remaining elements
  22210. while (i < target.size())
  22211. {
  22212. result.push_back(
  22213. {
  22214. {"op", "add"},
  22215. {"path", path + "/-"},
  22216. {"value", target[i]}
  22217. });
  22218. ++i;
  22219. }
  22220. break;
  22221. }
  22222. case value_t::object:
  22223. {
  22224. // first pass: traverse this object's elements
  22225. for (auto it = source.cbegin(); it != source.cend(); ++it)
  22226. {
  22227. // escape the key name to be used in a JSON patch
  22228. const auto path_key = path + "/" + detail::escape(it.key());
  22229. if (target.find(it.key()) != target.end())
  22230. {
  22231. // recursive call to compare object values at key it
  22232. auto temp_diff = diff(it.value(), target[it.key()], path_key);
  22233. result.insert(result.end(), temp_diff.begin(), temp_diff.end());
  22234. }
  22235. else
  22236. {
  22237. // found a key that is not in o -> remove it
  22238. result.push_back(object(
  22239. {
  22240. {"op", "remove"}, {"path", path_key}
  22241. }));
  22242. }
  22243. }
  22244. // second pass: traverse other object's elements
  22245. for (auto it = target.cbegin(); it != target.cend(); ++it)
  22246. {
  22247. if (source.find(it.key()) == source.end())
  22248. {
  22249. // found a key that is not in this -> add it
  22250. const auto path_key = path + "/" + detail::escape(it.key());
  22251. result.push_back(
  22252. {
  22253. {"op", "add"}, {"path", path_key},
  22254. {"value", it.value()}
  22255. });
  22256. }
  22257. }
  22258. break;
  22259. }
  22260. case value_t::null:
  22261. case value_t::string:
  22262. case value_t::boolean:
  22263. case value_t::number_integer:
  22264. case value_t::number_unsigned:
  22265. case value_t::number_float:
  22266. case value_t::binary:
  22267. case value_t::discarded:
  22268. default:
  22269. {
  22270. // both primitive type: replace value
  22271. result.push_back(
  22272. {
  22273. {"op", "replace"}, {"path", path}, {"value", target}
  22274. });
  22275. break;
  22276. }
  22277. }
  22278. return result;
  22279. }
  22280. /// @}
  22281. ////////////////////////////////
  22282. // JSON Merge Patch functions //
  22283. ////////////////////////////////
  22284. /// @name JSON Merge Patch functions
  22285. /// @{
  22286. /*!
  22287. @brief applies a JSON Merge Patch
  22288. The merge patch format is primarily intended for use with the HTTP PATCH
  22289. method as a means of describing a set of modifications to a target
  22290. resource's content. This function applies a merge patch to the current
  22291. JSON value.
  22292. The function implements the following algorithm from Section 2 of
  22293. [RFC 7396 (JSON Merge Patch)](https://tools.ietf.org/html/rfc7396):
  22294. ```
  22295. define MergePatch(Target, Patch):
  22296. if Patch is an Object:
  22297. if Target is not an Object:
  22298. Target = {} // Ignore the contents and set it to an empty Object
  22299. for each Name/Value pair in Patch:
  22300. if Value is null:
  22301. if Name exists in Target:
  22302. remove the Name/Value pair from Target
  22303. else:
  22304. Target[Name] = MergePatch(Target[Name], Value)
  22305. return Target
  22306. else:
  22307. return Patch
  22308. ```
  22309. Thereby, `Target` is the current object; that is, the patch is applied to
  22310. the current value.
  22311. @param[in] apply_patch the patch to apply
  22312. @complexity Linear in the lengths of @a patch.
  22313. @liveexample{The following code shows how a JSON Merge Patch is applied to
  22314. a JSON document.,merge_patch}
  22315. @sa see @ref patch -- apply a JSON patch
  22316. @sa [RFC 7396 (JSON Merge Patch)](https://tools.ietf.org/html/rfc7396)
  22317. @since version 3.0.0
  22318. */
  22319. void merge_patch(const basic_json& apply_patch)
  22320. {
  22321. if (apply_patch.is_object())
  22322. {
  22323. if (!is_object())
  22324. {
  22325. *this = object();
  22326. }
  22327. for (auto it = apply_patch.begin(); it != apply_patch.end(); ++it)
  22328. {
  22329. if (it.value().is_null())
  22330. {
  22331. erase(it.key());
  22332. }
  22333. else
  22334. {
  22335. operator[](it.key()).merge_patch(it.value());
  22336. }
  22337. }
  22338. }
  22339. else
  22340. {
  22341. *this = apply_patch;
  22342. }
  22343. }
  22344. /// @}
  22345. };
  22346. /*!
  22347. @brief user-defined to_string function for JSON values
  22348. This function implements a user-defined to_string for JSON objects.
  22349. @param[in] j a JSON object
  22350. @return a std::string object
  22351. */
  22352. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  22353. std::string to_string(const NLOHMANN_BASIC_JSON_TPL& j)
  22354. {
  22355. return j.dump();
  22356. }
  22357. } // namespace nlohmann
  22358. ///////////////////////
  22359. // nonmember support //
  22360. ///////////////////////
  22361. // specialization of std::swap, and std::hash
  22362. namespace std
  22363. {
  22364. /// hash value for JSON objects
  22365. template<>
  22366. struct hash<nlohmann::json>
  22367. {
  22368. /*!
  22369. @brief return a hash value for a JSON object
  22370. @since version 1.0.0
  22371. */
  22372. std::size_t operator()(const nlohmann::json& j) const
  22373. {
  22374. return nlohmann::detail::hash(j);
  22375. }
  22376. };
  22377. /// specialization for std::less<value_t>
  22378. /// @note: do not remove the space after '<',
  22379. /// see https://github.com/nlohmann/json/pull/679
  22380. template<>
  22381. struct less<::nlohmann::detail::value_t>
  22382. {
  22383. /*!
  22384. @brief compare two value_t enum values
  22385. @since version 3.0.0
  22386. */
  22387. bool operator()(nlohmann::detail::value_t lhs,
  22388. nlohmann::detail::value_t rhs) const noexcept
  22389. {
  22390. return nlohmann::detail::operator<(lhs, rhs);
  22391. }
  22392. };
  22393. // C++20 prohibit function specialization in the std namespace.
  22394. #ifndef JSON_HAS_CPP_20
  22395. /*!
  22396. @brief exchanges the values of two JSON objects
  22397. @since version 1.0.0
  22398. */
  22399. template<>
  22400. inline void swap<nlohmann::json>(nlohmann::json& j1, nlohmann::json& j2) noexcept( // NOLINT(readability-inconsistent-declaration-parameter-name)
  22401. is_nothrow_move_constructible<nlohmann::json>::value&& // NOLINT(misc-redundant-expression)
  22402. is_nothrow_move_assignable<nlohmann::json>::value
  22403. )
  22404. {
  22405. j1.swap(j2);
  22406. }
  22407. #endif
  22408. } // namespace std
  22409. /*!
  22410. @brief user-defined string literal for JSON values
  22411. This operator implements a user-defined string literal for JSON objects. It
  22412. can be used by adding `"_json"` to a string literal and returns a JSON object
  22413. if no parse error occurred.
  22414. @param[in] s a string representation of a JSON object
  22415. @param[in] n the length of string @a s
  22416. @return a JSON object
  22417. @since version 1.0.0
  22418. */
  22419. JSON_HEDLEY_NON_NULL(1)
  22420. inline nlohmann::json operator "" _json(const char* s, std::size_t n)
  22421. {
  22422. return nlohmann::json::parse(s, s + n);
  22423. }
  22424. /*!
  22425. @brief user-defined string literal for JSON pointer
  22426. This operator implements a user-defined string literal for JSON Pointers. It
  22427. can be used by adding `"_json_pointer"` to a string literal and returns a JSON pointer
  22428. object if no parse error occurred.
  22429. @param[in] s a string representation of a JSON Pointer
  22430. @param[in] n the length of string @a s
  22431. @return a JSON pointer object
  22432. @since version 2.0.0
  22433. */
  22434. JSON_HEDLEY_NON_NULL(1)
  22435. inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std::size_t n)
  22436. {
  22437. return nlohmann::json::json_pointer(std::string(s, n));
  22438. }
  22439. // #include <nlohmann/detail/macro_unscope.hpp>
  22440. // restore clang diagnostic settings
  22441. #if defined(__clang__)
  22442. #pragma clang diagnostic pop
  22443. #endif
  22444. // clean up
  22445. #undef JSON_ASSERT
  22446. #undef JSON_INTERNAL_CATCH
  22447. #undef JSON_CATCH
  22448. #undef JSON_THROW
  22449. #undef JSON_TRY
  22450. #undef JSON_PRIVATE_UNLESS_TESTED
  22451. #undef JSON_HAS_CPP_11
  22452. #undef JSON_HAS_CPP_14
  22453. #undef JSON_HAS_CPP_17
  22454. #undef JSON_HAS_CPP_20
  22455. #undef NLOHMANN_BASIC_JSON_TPL_DECLARATION
  22456. #undef NLOHMANN_BASIC_JSON_TPL
  22457. #undef JSON_EXPLICIT
  22458. // #include <nlohmann/thirdparty/hedley/hedley_undef.hpp>
  22459. #undef JSON_HEDLEY_ALWAYS_INLINE
  22460. #undef JSON_HEDLEY_ARM_VERSION
  22461. #undef JSON_HEDLEY_ARM_VERSION_CHECK
  22462. #undef JSON_HEDLEY_ARRAY_PARAM
  22463. #undef JSON_HEDLEY_ASSUME
  22464. #undef JSON_HEDLEY_BEGIN_C_DECLS
  22465. #undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE
  22466. #undef JSON_HEDLEY_CLANG_HAS_BUILTIN
  22467. #undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE
  22468. #undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE
  22469. #undef JSON_HEDLEY_CLANG_HAS_EXTENSION
  22470. #undef JSON_HEDLEY_CLANG_HAS_FEATURE
  22471. #undef JSON_HEDLEY_CLANG_HAS_WARNING
  22472. #undef JSON_HEDLEY_COMPCERT_VERSION
  22473. #undef JSON_HEDLEY_COMPCERT_VERSION_CHECK
  22474. #undef JSON_HEDLEY_CONCAT
  22475. #undef JSON_HEDLEY_CONCAT3
  22476. #undef JSON_HEDLEY_CONCAT3_EX
  22477. #undef JSON_HEDLEY_CONCAT_EX
  22478. #undef JSON_HEDLEY_CONST
  22479. #undef JSON_HEDLEY_CONSTEXPR
  22480. #undef JSON_HEDLEY_CONST_CAST
  22481. #undef JSON_HEDLEY_CPP_CAST
  22482. #undef JSON_HEDLEY_CRAY_VERSION
  22483. #undef JSON_HEDLEY_CRAY_VERSION_CHECK
  22484. #undef JSON_HEDLEY_C_DECL
  22485. #undef JSON_HEDLEY_DEPRECATED
  22486. #undef JSON_HEDLEY_DEPRECATED_FOR
  22487. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
  22488. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_
  22489. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
  22490. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
  22491. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
  22492. #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
  22493. #undef JSON_HEDLEY_DIAGNOSTIC_POP
  22494. #undef JSON_HEDLEY_DIAGNOSTIC_PUSH
  22495. #undef JSON_HEDLEY_DMC_VERSION
  22496. #undef JSON_HEDLEY_DMC_VERSION_CHECK
  22497. #undef JSON_HEDLEY_EMPTY_BASES
  22498. #undef JSON_HEDLEY_EMSCRIPTEN_VERSION
  22499. #undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK
  22500. #undef JSON_HEDLEY_END_C_DECLS
  22501. #undef JSON_HEDLEY_FLAGS
  22502. #undef JSON_HEDLEY_FLAGS_CAST
  22503. #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE
  22504. #undef JSON_HEDLEY_GCC_HAS_BUILTIN
  22505. #undef JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE
  22506. #undef JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE
  22507. #undef JSON_HEDLEY_GCC_HAS_EXTENSION
  22508. #undef JSON_HEDLEY_GCC_HAS_FEATURE
  22509. #undef JSON_HEDLEY_GCC_HAS_WARNING
  22510. #undef JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK
  22511. #undef JSON_HEDLEY_GCC_VERSION
  22512. #undef JSON_HEDLEY_GCC_VERSION_CHECK
  22513. #undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE
  22514. #undef JSON_HEDLEY_GNUC_HAS_BUILTIN
  22515. #undef JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE
  22516. #undef JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE
  22517. #undef JSON_HEDLEY_GNUC_HAS_EXTENSION
  22518. #undef JSON_HEDLEY_GNUC_HAS_FEATURE
  22519. #undef JSON_HEDLEY_GNUC_HAS_WARNING
  22520. #undef JSON_HEDLEY_GNUC_VERSION
  22521. #undef JSON_HEDLEY_GNUC_VERSION_CHECK
  22522. #undef JSON_HEDLEY_HAS_ATTRIBUTE
  22523. #undef JSON_HEDLEY_HAS_BUILTIN
  22524. #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE
  22525. #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS
  22526. #undef JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE
  22527. #undef JSON_HEDLEY_HAS_EXTENSION
  22528. #undef JSON_HEDLEY_HAS_FEATURE
  22529. #undef JSON_HEDLEY_HAS_WARNING
  22530. #undef JSON_HEDLEY_IAR_VERSION
  22531. #undef JSON_HEDLEY_IAR_VERSION_CHECK
  22532. #undef JSON_HEDLEY_IBM_VERSION
  22533. #undef JSON_HEDLEY_IBM_VERSION_CHECK
  22534. #undef JSON_HEDLEY_IMPORT
  22535. #undef JSON_HEDLEY_INLINE
  22536. #undef JSON_HEDLEY_INTEL_CL_VERSION
  22537. #undef JSON_HEDLEY_INTEL_CL_VERSION_CHECK
  22538. #undef JSON_HEDLEY_INTEL_VERSION
  22539. #undef JSON_HEDLEY_INTEL_VERSION_CHECK
  22540. #undef JSON_HEDLEY_IS_CONSTANT
  22541. #undef JSON_HEDLEY_IS_CONSTEXPR_
  22542. #undef JSON_HEDLEY_LIKELY
  22543. #undef JSON_HEDLEY_MALLOC
  22544. #undef JSON_HEDLEY_MCST_LCC_VERSION
  22545. #undef JSON_HEDLEY_MCST_LCC_VERSION_CHECK
  22546. #undef JSON_HEDLEY_MESSAGE
  22547. #undef JSON_HEDLEY_MSVC_VERSION
  22548. #undef JSON_HEDLEY_MSVC_VERSION_CHECK
  22549. #undef JSON_HEDLEY_NEVER_INLINE
  22550. #undef JSON_HEDLEY_NON_NULL
  22551. #undef JSON_HEDLEY_NO_ESCAPE
  22552. #undef JSON_HEDLEY_NO_RETURN
  22553. #undef JSON_HEDLEY_NO_THROW
  22554. #undef JSON_HEDLEY_NULL
  22555. #undef JSON_HEDLEY_PELLES_VERSION
  22556. #undef JSON_HEDLEY_PELLES_VERSION_CHECK
  22557. #undef JSON_HEDLEY_PGI_VERSION
  22558. #undef JSON_HEDLEY_PGI_VERSION_CHECK
  22559. #undef JSON_HEDLEY_PREDICT
  22560. #undef JSON_HEDLEY_PRINTF_FORMAT
  22561. #undef JSON_HEDLEY_PRIVATE
  22562. #undef JSON_HEDLEY_PUBLIC
  22563. #undef JSON_HEDLEY_PURE
  22564. #undef JSON_HEDLEY_REINTERPRET_CAST
  22565. #undef JSON_HEDLEY_REQUIRE
  22566. #undef JSON_HEDLEY_REQUIRE_CONSTEXPR
  22567. #undef JSON_HEDLEY_REQUIRE_MSG
  22568. #undef JSON_HEDLEY_RESTRICT
  22569. #undef JSON_HEDLEY_RETURNS_NON_NULL
  22570. #undef JSON_HEDLEY_SENTINEL
  22571. #undef JSON_HEDLEY_STATIC_ASSERT
  22572. #undef JSON_HEDLEY_STATIC_CAST
  22573. #undef JSON_HEDLEY_STRINGIFY
  22574. #undef JSON_HEDLEY_STRINGIFY_EX
  22575. #undef JSON_HEDLEY_SUNPRO_VERSION
  22576. #undef JSON_HEDLEY_SUNPRO_VERSION_CHECK
  22577. #undef JSON_HEDLEY_TINYC_VERSION
  22578. #undef JSON_HEDLEY_TINYC_VERSION_CHECK
  22579. #undef JSON_HEDLEY_TI_ARMCL_VERSION
  22580. #undef JSON_HEDLEY_TI_ARMCL_VERSION_CHECK
  22581. #undef JSON_HEDLEY_TI_CL2000_VERSION
  22582. #undef JSON_HEDLEY_TI_CL2000_VERSION_CHECK
  22583. #undef JSON_HEDLEY_TI_CL430_VERSION
  22584. #undef JSON_HEDLEY_TI_CL430_VERSION_CHECK
  22585. #undef JSON_HEDLEY_TI_CL6X_VERSION
  22586. #undef JSON_HEDLEY_TI_CL6X_VERSION_CHECK
  22587. #undef JSON_HEDLEY_TI_CL7X_VERSION
  22588. #undef JSON_HEDLEY_TI_CL7X_VERSION_CHECK
  22589. #undef JSON_HEDLEY_TI_CLPRU_VERSION
  22590. #undef JSON_HEDLEY_TI_CLPRU_VERSION_CHECK
  22591. #undef JSON_HEDLEY_TI_VERSION
  22592. #undef JSON_HEDLEY_TI_VERSION_CHECK
  22593. #undef JSON_HEDLEY_UNAVAILABLE
  22594. #undef JSON_HEDLEY_UNLIKELY
  22595. #undef JSON_HEDLEY_UNPREDICTABLE
  22596. #undef JSON_HEDLEY_UNREACHABLE
  22597. #undef JSON_HEDLEY_UNREACHABLE_RETURN
  22598. #undef JSON_HEDLEY_VERSION
  22599. #undef JSON_HEDLEY_VERSION_DECODE_MAJOR
  22600. #undef JSON_HEDLEY_VERSION_DECODE_MINOR
  22601. #undef JSON_HEDLEY_VERSION_DECODE_REVISION
  22602. #undef JSON_HEDLEY_VERSION_ENCODE
  22603. #undef JSON_HEDLEY_WARNING
  22604. #undef JSON_HEDLEY_WARN_UNUSED_RESULT
  22605. #undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG
  22606. #undef JSON_HEDLEY_FALL_THROUGH
  22607. #endif // INCLUDE_NLOHMANN_JSON_HPP_