gl3d.h 1.1 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378
  1. ////////////////////////////////////////////////
  2. //gl3D --Vlad Luta --
  3. //built on 2024-01-16
  4. ////////////////////////////////////////////////
  5. ////////////////////////////////////////////////
  6. //Core.h
  7. ////////////////////////////////////////////////
  8. #pragma region Core
  9. #pragma once
  10. #define GLM_ENABLE_EXPERIMENTAL
  11. #define TINYGLTF_NO_INCLUDE_STB_IMAGE
  12. #include <glad/glad.h>
  13. #include <stb_image/stb_image.h>
  14. #define GL3D_REMOVE_IOSTREAM 0 //you can remove this if neded to. It is just used for the default errorcallback
  15. #define GL3D_REMOVE_FSTREAM 0 //you can remove this if neded to. It is used for the default file callback, supply your own function for file oppening so the library still works :))
  16. #define GL3D_REMOVE_ASSERTS 0 //you can remove asserts in production
  17. #define GL3D_OPTIMIZE_CACHED_SEARCH 1 //this was used by the dev to test some things, if you are using the library just keep it to 1.
  18. #if GL3D_REMOVE_IOSTREAM == 0
  19. #include <iostream>
  20. #endif
  21. #if GL3D_REMOVE_FSTREAM == 0
  22. #include <fstream>
  23. #endif
  24. #include <glm/vec4.hpp>
  25. #include <glm/vec3.hpp>
  26. #include <glm/mat4x4.hpp>
  27. #include <glm/gtc/matrix_transform.hpp>
  28. #include <glm/gtc/type_ptr.hpp>
  29. #include <glm/mat3x3.hpp>
  30. #include <glm/gtx/transform.hpp>
  31. #undef min
  32. #undef max
  33. #define GL3D_ADD_FLAG(NAME, SETNAME, VALUE) \
  34. bool NAME() {return (flags & ((unsigned char)1 << VALUE) );} \
  35. void SETNAME(bool s) \
  36. { if (s) { flags = flags | ((unsigned char)1 << VALUE); } \
  37. else { flags = flags & ~((unsigned char)1 << VALUE); } \
  38. }
  39. namespace gl3d
  40. {
  41. //todo optimization also hold the last found position
  42. //todo why id isn't unsigned?
  43. #define CREATE_RENDERER_OBJECT_HANDLE(x) \
  44. struct x \
  45. { \
  46. int id_ = {}; \
  47. int lastFoundPos_ = {}; \
  48. x (int id=0):id_(id){}; \
  49. }
  50. CREATE_RENDERER_OBJECT_HANDLE(Material);
  51. CREATE_RENDERER_OBJECT_HANDLE(Entity);
  52. CREATE_RENDERER_OBJECT_HANDLE(Model);
  53. CREATE_RENDERER_OBJECT_HANDLE(Texture);
  54. CREATE_RENDERER_OBJECT_HANDLE(SpotLight);
  55. CREATE_RENDERER_OBJECT_HANDLE(PointLight);
  56. CREATE_RENDERER_OBJECT_HANDLE(DirectionalLight);
  57. #undef CREATE_RENDERER_OBJECT_HANDLE(x)
  58. struct PBRTexture
  59. {
  60. Texture texture = {}; //rough metalness ambient oclusion
  61. // (roughnessLoaded) -> 0b100
  62. // (metallicLoaded) -> 0b010
  63. // (ambientLoaded) -> 0b001
  64. int RMA_loadedTextures = {};
  65. };
  66. struct TextureDataForMaterial
  67. {
  68. Texture albedoTexture = {};
  69. Texture normalMapTexture = {};
  70. Texture emissiveTexture= {};
  71. PBRTexture pbrTexture = {};
  72. bool operator==(const TextureDataForMaterial& other)
  73. {
  74. return
  75. (albedoTexture.id_ == other.albedoTexture.id_)
  76. && (normalMapTexture.id_ == other.normalMapTexture.id_)
  77. && (emissiveTexture.id_ == other.emissiveTexture.id_)
  78. && (pbrTexture.texture.id_ == other.pbrTexture.texture.id_)
  79. && (pbrTexture.RMA_loadedTextures == other.pbrTexture.RMA_loadedTextures)
  80. ;
  81. };
  82. bool operator!=(const TextureDataForMaterial& other)
  83. {
  84. return !(*this == other);
  85. };
  86. };
  87. //note this is the gpu material
  88. struct MaterialValues
  89. {
  90. glm::vec4 kd = glm::vec4(1);
  91. //rma
  92. float roughness = 0.5f;
  93. float metallic = 0.1;
  94. float ao = 1;
  95. float emmisive = 0;
  96. //rma
  97. GLuint64 albedoSampler = 0;
  98. GLuint64 rmaSampler = 0;
  99. GLuint64 emmissiveSampler = 0;
  100. int rmaLoaded = {};
  101. int notUsed;
  102. MaterialValues setDefaultMaterial()
  103. {
  104. *this = MaterialValues();
  105. return *this;
  106. }
  107. bool operator==(const MaterialValues& other)
  108. {
  109. return
  110. (kd == other.kd)
  111. && (roughness == other.roughness)
  112. && (metallic == other.metallic)
  113. && (ao == other.ao)
  114. && (emmisive == other.emmisive)
  115. ;
  116. };
  117. bool operator!=(const MaterialValues& other)
  118. {
  119. return !(*this == other);
  120. };
  121. };
  122. //todo move
  123. namespace internal
  124. {
  125. //todo move
  126. struct GpuPointLight
  127. {
  128. glm::vec3 position = {};
  129. float dist = 20;
  130. glm::vec3 color = { 1,1,1 };
  131. float attenuation = 2;
  132. int castShadowsIndex = 1;
  133. float hardness = 1;
  134. int castShadows = 1;
  135. int changedThisFrame = 1;
  136. };
  137. struct GpuDirectionalLight
  138. {
  139. glm::vec3 direction = {0,-1,0};
  140. int castShadowsIndex = 1;
  141. int changedThisFrame = 1;
  142. int castShadows = 1;
  143. int notUsed1 = 0;
  144. int notUsed2 = 0;
  145. glm::vec3 color = { 1,1,1 };
  146. float hardness = 1;
  147. glm::mat4 lightSpaceMatrix[3]; //magic number (cascades)
  148. };
  149. struct GpuSpotLight
  150. {
  151. glm::vec3 position = {};
  152. float cosHalfAngle = std::cos(3.14159/4.f);
  153. glm::vec3 direction = { 0,-1,0 };
  154. float dist = 20;
  155. glm::vec3 color = { 1, 1, 1 };
  156. float attenuation = 2;
  157. float hardness = 1;
  158. int shadowIndex = 0;
  159. int castShadows = 1;
  160. int changedThisFrame = 1; //this is sent to the gpu but not used there
  161. float nearPlane = 0.1;
  162. float farPlane = 10;
  163. float notUsed1 = 0;
  164. float notUsed2 = 0;
  165. glm::mat4 lightSpaceMatrix;
  166. };
  167. };
  168. void GLAPIENTRY glDebugOutput(GLenum source,
  169. GLenum type,
  170. unsigned int id,
  171. GLenum severity,
  172. GLsizei length,
  173. const char *message,
  174. const void *userParam);
  175. void assertFunc(const char *expression,
  176. const char *file_name,
  177. unsigned const line_number,
  178. const char *comment = "---");
  179. };
  180. #if GL3D_REMOVE_ASSERTS == 0
  181. #define gl3dAssert(expression) (void)( \
  182. (!!(expression)) || \
  183. (gl3d::assertFunc(#expression, __FILE__, (unsigned)(__LINE__)), 0) \
  184. )
  185. #define gl3dAssertComment(expression, comment) (void)( \
  186. (!!(expression)) || \
  187. (gl3d::assertFunc(#expression, __FILE__, (unsigned)(__LINE__), comment), 0)\
  188. )
  189. #else
  190. #define gl3dAssert(expression)
  191. #define gl3dAssertComment(expression, comment)
  192. #endif
  193. #pragma endregion
  194. ////////////////////////////////////////////////
  195. //ErrorReporting.h
  196. ////////////////////////////////////////////////
  197. #pragma region ErrorReporting
  198. #pragma once
  199. #include <string>
  200. #include <vector>
  201. namespace gl3d
  202. {
  203. void defaultErrorCallback(std::string err, void *userData);
  204. std::string defaultReadEntireFile(const char *fileName, bool &couldNotOpen, void *userData);
  205. std::vector<char> defaultReadEntireFileBinary(const char *fileName, bool &couldNotOpen, void *userData);
  206. bool defaultFileExists(const char *fileName, void *userData);
  207. using ErrorCallback_t = decltype(defaultErrorCallback);
  208. using ReadEntireFile_t = decltype(defaultReadEntireFile);
  209. using ReadEntireFileBinart_t = decltype(defaultReadEntireFileBinary);
  210. using FileExists_t = decltype(defaultFileExists);
  211. struct ErrorReporter
  212. {
  213. ErrorCallback_t *currentErrorCallback = defaultErrorCallback;
  214. void *userData = nullptr;
  215. void callErrorCallback(std::string s);
  216. };
  217. struct FileOpener
  218. {
  219. ReadEntireFile_t *readEntireFileCallback = defaultReadEntireFile;
  220. ReadEntireFileBinart_t *readEntireFileBinaryCallback = defaultReadEntireFileBinary;
  221. FileExists_t *fileExistsCallback = defaultFileExists;
  222. void *userData = nullptr;
  223. std::string operator()(const char *fileName, bool &couldNotOpen)
  224. {
  225. return readEntireFileCallback(fileName, couldNotOpen, userData);
  226. }
  227. std::vector<char> binary(const char *fileName, bool &couldNotOpen)
  228. {
  229. return readEntireFileBinaryCallback(fileName, couldNotOpen, userData);
  230. }
  231. bool exists(const char *fileName)
  232. {
  233. return fileExistsCallback(fileName, userData);
  234. }
  235. };
  236. };
  237. #pragma endregion
  238. ////////////////////////////////////////////////
  239. //Animations.h
  240. ////////////////////////////////////////////////
  241. #pragma region Animations
  242. #pragma once
  243. #include <string>
  244. #include <glm/mat4x4.hpp>
  245. #include <glm/vec4.hpp>
  246. #include <glm/vec3.hpp>
  247. #include <vector>
  248. #include <glm/gtc/quaternion.hpp>
  249. #include <glm/gtx/quaternion.hpp>
  250. namespace gl3d
  251. {
  252. //glm::mat4 animatedTransform{ 1.f }; //transform current default state to desired pos (model space)
  253. struct Joint
  254. {
  255. glm::mat4 inverseBindTransform{ 1.f };
  256. glm::mat4 localBindTransform{1.f};
  257. std::string name{};
  258. std::vector<int> children;
  259. glm::quat rotation{ 0.f,0.f,0.f,1.f };
  260. glm::vec3 trans{ 1.f,1.f,1.f };
  261. glm::vec3 scale{ 1.f,1.f,1.f };
  262. glm::mat4 worldMatrix; //for extracting position data per bone
  263. //int index{};
  264. //int root = 0;
  265. };
  266. struct KeyFrame
  267. {
  268. glm::quat rotation{0.f,0.f,0.f,1.f};
  269. glm::vec3 translation{};
  270. float timeStamp{};
  271. glm::vec3 scale{1.f, 1.f, 1.f};
  272. unsigned char rotationSet = 0;
  273. unsigned char translationSet = 0;
  274. unsigned char scaleSet = 0;
  275. unsigned char notUsed = 0;
  276. };
  277. struct KeyFrameRotation
  278. {
  279. glm::quat rotation{ 0.f,0.f,0.f,1.f };
  280. float timeStamp{};
  281. };
  282. struct KeyFrameTranslation
  283. {
  284. glm::vec3 translation{};
  285. float timeStamp{};
  286. };
  287. struct KeyFrameScale
  288. {
  289. glm::vec3 scale{ 1.f, 1.f, 1.f };
  290. float timeStamp{};
  291. };
  292. struct TimeStamps
  293. {
  294. float passedTimeRot = 0;
  295. float passedTimeTrans = 0;
  296. float passedTimeScale = 0;
  297. };
  298. struct Animation
  299. {
  300. std::string name;
  301. //for each joint we have keyframes
  302. std::vector<std::vector<KeyFrameRotation>> keyFramesRot;
  303. std::vector<std::vector<KeyFrameTranslation>> keyFramesTrans;
  304. std::vector<std::vector<KeyFrameScale>> keyFramesScale;
  305. //std::vector<TimeStamps> timeStamps;
  306. float animationDuration=0;
  307. std::vector<int> root = {};
  308. //std::vector<float> timePassed;
  309. //std::vector<std::vector<KeyFrame>> keyFrames;
  310. };
  311. };
  312. #pragma endregion
  313. ////////////////////////////////////////////////
  314. //json.h
  315. ////////////////////////////////////////////////
  316. #pragma region json
  317. /*
  318. __ _____ _____ _____
  319. __| | __| | | | JSON for Modern C++
  320. | | |__ | | | | | | version 3.5.0
  321. |_____|_____|_____|_|___| https://github.com/nlohmann/json
  322. Licensed under the MIT License <http://opensource.org/licenses/MIT>.
  323. SPDX-License-Identifier: MIT
  324. Copyright (c) 2013-2018 Niels Lohmann <http://nlohmann.me>.
  325. Permission is hereby granted, free of charge, to any person obtaining a copy
  326. of this software and associated documentation files (the "Software"), to deal
  327. in the Software without restriction, including without limitation the rights
  328. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  329. copies of the Software, and to permit persons to whom the Software is
  330. furnished to do so, subject to the following conditions:
  331. The above copyright notice and this permission notice shall be included in all
  332. copies or substantial portions of the Software.
  333. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  334. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  335. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  336. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  337. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  338. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  339. SOFTWARE.
  340. */
  341. #ifndef NLOHMANN_JSON_HPP
  342. #define NLOHMANN_JSON_HPP
  343. #define NLOHMANN_JSON_VERSION_MAJOR 3
  344. #define NLOHMANN_JSON_VERSION_MINOR 5
  345. #define NLOHMANN_JSON_VERSION_PATCH 0
  346. #include <algorithm> // all_of, find, for_each
  347. #include <cassert> // assert
  348. #include <ciso646> // and, not, or
  349. #include <cstddef> // nullptr_t, ptrdiff_t, size_t
  350. #include <functional> // hash, less
  351. #include <initializer_list> // initializer_list
  352. #include <iosfwd> // istream, ostream
  353. #include <iterator> // random_access_iterator_tag
  354. #include <numeric> // accumulate
  355. #include <string> // string, stoi, to_string
  356. #include <utility> // declval, forward, move, pair, swap
  357. // #include <nlohmann/json_fwd.hpp>
  358. #ifndef NLOHMANN_JSON_FWD_HPP
  359. #define NLOHMANN_JSON_FWD_HPP
  360. #include <cstdint> // int64_t, uint64_t
  361. #include <map> // map
  362. #include <memory> // allocator
  363. #include <string> // string
  364. #include <vector> // vector
  365. /*!
  366. @brief namespace for Niels Lohmann
  367. @see https://github.com/nlohmann
  368. @since version 1.0.0
  369. */
  370. namespace nlohmann
  371. {
  372. /*!
  373. @brief default JSONSerializer template argument
  374. This serializer ignores the template arguments and uses ADL
  375. ([argument-dependent lookup](https://en.cppreference.com/w/cpp/language/adl))
  376. for serialization.
  377. */
  378. template<typename T = void, typename SFINAE = void>
  379. struct adl_serializer;
  380. template<template<typename U, typename V, typename... Args> class ObjectType =
  381. std::map,
  382. template<typename U, typename... Args> class ArrayType = std::vector,
  383. class StringType = std::string, class BooleanType = bool,
  384. class NumberIntegerType = std::int64_t,
  385. class NumberUnsignedType = std::uint64_t,
  386. class NumberFloatType = double,
  387. template<typename U> class AllocatorType = std::allocator,
  388. template<typename T, typename SFINAE = void> class JSONSerializer =
  389. adl_serializer>
  390. class basic_json;
  391. /*!
  392. @brief JSON Pointer
  393. A JSON pointer defines a string syntax for identifying a specific value
  394. within a JSON document. It can be used with functions `at` and
  395. `operator[]`. Furthermore, JSON pointers are the base for JSON patches.
  396. @sa [RFC 6901](https://tools.ietf.org/html/rfc6901)
  397. @since version 2.0.0
  398. */
  399. template<typename BasicJsonType>
  400. class json_pointer;
  401. /*!
  402. @brief default JSON class
  403. This type is the default specialization of the @ref basic_json class which
  404. uses the standard template types.
  405. @since version 1.0.0
  406. */
  407. using json = basic_json<>;
  408. } // namespace nlohmann
  409. #endif
  410. // #include <nlohmann/detail/macro_scope.hpp>
  411. // This file contains all internal macro definitions
  412. // You MUST include macro_unscope.hpp at the end of json.hpp to undef all of them
  413. // exclude unsupported compilers
  414. #if !defined(JSON_SKIP_UNSUPPORTED_COMPILER_CHECK)
  415. #if defined(__clang__)
  416. #if (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) < 30400
  417. #error "unsupported Clang version - see https://github.com/nlohmann/json#supported-compilers"
  418. #endif
  419. #elif defined(__GNUC__) && !(defined(__ICC) || defined(__INTEL_COMPILER))
  420. #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40800
  421. #error "unsupported GCC version - see https://github.com/nlohmann/json#supported-compilers"
  422. #endif
  423. #endif
  424. #endif
  425. // disable float-equal warnings on GCC/clang
  426. #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
  427. #pragma GCC diagnostic push
  428. #pragma GCC diagnostic ignored "-Wfloat-equal"
  429. #endif
  430. // disable documentation warnings on clang
  431. #if defined(__clang__)
  432. #pragma GCC diagnostic push
  433. #pragma GCC diagnostic ignored "-Wdocumentation"
  434. #endif
  435. // allow for portable deprecation warnings
  436. #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
  437. #define JSON_DEPRECATED __attribute__((deprecated))
  438. #elif defined(_MSC_VER)
  439. #define JSON_DEPRECATED __declspec(deprecated)
  440. #else
  441. #define JSON_DEPRECATED
  442. #endif
  443. // allow to disable exceptions
  444. #if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION)
  445. #define JSON_THROW(exception) throw exception
  446. #define JSON_TRY try
  447. #define JSON_CATCH(exception) catch(exception)
  448. #define JSON_INTERNAL_CATCH(exception) catch(exception)
  449. #else
  450. #define JSON_THROW(exception) std::abort()
  451. #define JSON_TRY if(true)
  452. #define JSON_CATCH(exception) if(false)
  453. #define JSON_INTERNAL_CATCH(exception) if(false)
  454. #endif
  455. // override exception macros
  456. #if defined(JSON_THROW_USER)
  457. #undef JSON_THROW
  458. #define JSON_THROW JSON_THROW_USER
  459. #endif
  460. #if defined(JSON_TRY_USER)
  461. #undef JSON_TRY
  462. #define JSON_TRY JSON_TRY_USER
  463. #endif
  464. #if defined(JSON_CATCH_USER)
  465. #undef JSON_CATCH
  466. #define JSON_CATCH JSON_CATCH_USER
  467. #undef JSON_INTERNAL_CATCH
  468. #define JSON_INTERNAL_CATCH JSON_CATCH_USER
  469. #endif
  470. #if defined(JSON_INTERNAL_CATCH_USER)
  471. #undef JSON_INTERNAL_CATCH
  472. #define JSON_INTERNAL_CATCH JSON_INTERNAL_CATCH_USER
  473. #endif
  474. // manual branch prediction
  475. #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
  476. #define JSON_LIKELY(x) __builtin_expect(!!(x), 1)
  477. #define JSON_UNLIKELY(x) __builtin_expect(!!(x), 0)
  478. #else
  479. #define JSON_LIKELY(x) x
  480. #define JSON_UNLIKELY(x) x
  481. #endif
  482. // C++ language standard detection
  483. #if (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464
  484. #define JSON_HAS_CPP_17
  485. #define JSON_HAS_CPP_14
  486. #elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1)
  487. #define JSON_HAS_CPP_14
  488. #endif
  489. /*!
  490. @brief macro to briefly define a mapping between an enum and JSON
  491. @def NLOHMANN_JSON_SERIALIZE_ENUM
  492. @since version 3.4.0
  493. */
  494. #define NLOHMANN_JSON_SERIALIZE_ENUM(ENUM_TYPE, ...) \
  495. template<typename BasicJsonType> \
  496. inline void to_json(BasicJsonType& j, const ENUM_TYPE& e) \
  497. { \
  498. static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
  499. static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
  500. auto it = std::find_if(std::begin(m), std::end(m), \
  501. [e](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
  502. { \
  503. return ej_pair.first == e; \
  504. }); \
  505. j = ((it != std::end(m)) ? it : std::begin(m))->second; \
  506. } \
  507. template<typename BasicJsonType> \
  508. inline void from_json(const BasicJsonType& j, ENUM_TYPE& e) \
  509. { \
  510. static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
  511. static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
  512. auto it = std::find_if(std::begin(m), std::end(m), \
  513. [j](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
  514. { \
  515. return ej_pair.second == j; \
  516. }); \
  517. e = ((it != std::end(m)) ? it : std::begin(m))->first; \
  518. }
  519. // Ugly macros to avoid uglier copy-paste when specializing basic_json. They
  520. // may be removed in the future once the class is split.
  521. #define NLOHMANN_BASIC_JSON_TPL_DECLARATION \
  522. template<template<typename, typename, typename...> class ObjectType, \
  523. template<typename, typename...> class ArrayType, \
  524. class StringType, class BooleanType, class NumberIntegerType, \
  525. class NumberUnsignedType, class NumberFloatType, \
  526. template<typename> class AllocatorType, \
  527. template<typename, typename = void> class JSONSerializer>
  528. #define NLOHMANN_BASIC_JSON_TPL \
  529. basic_json<ObjectType, ArrayType, StringType, BooleanType, \
  530. NumberIntegerType, NumberUnsignedType, NumberFloatType, \
  531. AllocatorType, JSONSerializer>
  532. // #include <nlohmann/detail/meta/cpp_future.hpp>
  533. #include <ciso646> // not
  534. #include <cstddef> // size_t
  535. #include <type_traits> // conditional, enable_if, false_type, integral_constant, is_constructible, is_integral, is_same, remove_cv, remove_reference, true_type
  536. namespace nlohmann
  537. {
  538. namespace detail
  539. {
  540. // alias templates to reduce boilerplate
  541. template<bool B, typename T = void>
  542. using enable_if_t = typename std::enable_if<B, T>::type;
  543. template<typename T>
  544. using uncvref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
  545. // implementation of C++14 index_sequence and affiliates
  546. // source: https://stackoverflow.com/a/32223343
  547. template<std::size_t... Ints>
  548. struct index_sequence
  549. {
  550. using type = index_sequence;
  551. using value_type = std::size_t;
  552. static constexpr std::size_t size() noexcept
  553. {
  554. return sizeof...(Ints);
  555. }
  556. };
  557. template<class Sequence1, class Sequence2>
  558. struct merge_and_renumber;
  559. template<std::size_t... I1, std::size_t... I2>
  560. struct merge_and_renumber<index_sequence<I1...>, index_sequence<I2...>>
  561. : index_sequence < I1..., (sizeof...(I1) + I2)... > {};
  562. template<std::size_t N>
  563. struct make_index_sequence
  564. : merge_and_renumber < typename make_index_sequence < N / 2 >::type,
  565. typename make_index_sequence < N - N / 2 >::type > {};
  566. template<> struct make_index_sequence<0> : index_sequence<> {};
  567. template<> struct make_index_sequence<1> : index_sequence<0> {};
  568. template<typename... Ts>
  569. using index_sequence_for = make_index_sequence<sizeof...(Ts)>;
  570. // dispatch utility (taken from ranges-v3)
  571. template<unsigned N> struct priority_tag : priority_tag < N - 1 > {};
  572. template<> struct priority_tag<0> {};
  573. // taken from ranges-v3
  574. template<typename T>
  575. struct static_const
  576. {
  577. static constexpr T value{};
  578. };
  579. template<typename T>
  580. constexpr T static_const<T>::value;
  581. } // namespace detail
  582. } // namespace nlohmann
  583. // #include <nlohmann/detail/meta/type_traits.hpp>
  584. #include <ciso646> // not
  585. #include <limits> // numeric_limits
  586. #include <type_traits> // false_type, is_constructible, is_integral, is_same, true_type
  587. #include <utility> // declval
  588. // #include <nlohmann/json_fwd.hpp>
  589. // #include <nlohmann/detail/iterators/iterator_traits.hpp>
  590. #include <iterator> // random_access_iterator_tag
  591. // #include <nlohmann/detail/meta/void_t.hpp>
  592. namespace nlohmann
  593. {
  594. namespace detail
  595. {
  596. template <typename ...Ts> struct make_void
  597. {
  598. using type = void;
  599. };
  600. template <typename ...Ts> using void_t = typename make_void<Ts...>::type;
  601. } // namespace detail
  602. } // namespace nlohmann
  603. // #include <nlohmann/detail/meta/cpp_future.hpp>
  604. namespace nlohmann
  605. {
  606. namespace detail
  607. {
  608. template <typename It, typename = void>
  609. struct iterator_types {};
  610. template <typename It>
  611. struct iterator_types <
  612. It,
  613. void_t<typename It::difference_type, typename It::value_type, typename It::pointer,
  614. typename It::reference, typename It::iterator_category >>
  615. {
  616. using difference_type = typename It::difference_type;
  617. using value_type = typename It::value_type;
  618. using pointer = typename It::pointer;
  619. using reference = typename It::reference;
  620. using iterator_category = typename It::iterator_category;
  621. };
  622. // This is required as some compilers implement std::iterator_traits in a way that
  623. // doesn't work with SFINAE. See https://github.com/nlohmann/json/issues/1341.
  624. template <typename T, typename = void>
  625. struct iterator_traits
  626. {
  627. };
  628. template <typename T>
  629. struct iterator_traits < T, enable_if_t < !std::is_pointer<T>::value >>
  630. : iterator_types<T>
  631. {
  632. };
  633. template <typename T>
  634. struct iterator_traits<T*, enable_if_t<std::is_object<T>::value>>
  635. {
  636. using iterator_category = std::random_access_iterator_tag;
  637. using value_type = T;
  638. using difference_type = ptrdiff_t;
  639. using pointer = T*;
  640. using reference = T&;
  641. };
  642. }
  643. }
  644. // #include <nlohmann/detail/meta/cpp_future.hpp>
  645. // #include <nlohmann/detail/meta/detected.hpp>
  646. #include <type_traits>
  647. // #include <nlohmann/detail/meta/void_t.hpp>
  648. // http://en.cppreference.com/w/cpp/experimental/is_detected
  649. namespace nlohmann
  650. {
  651. namespace detail
  652. {
  653. struct nonesuch
  654. {
  655. nonesuch() = delete;
  656. ~nonesuch() = delete;
  657. nonesuch(nonesuch const&) = delete;
  658. void operator=(nonesuch const&) = delete;
  659. };
  660. template <class Default,
  661. class AlwaysVoid,
  662. template <class...> class Op,
  663. class... Args>
  664. struct detector
  665. {
  666. using value_t = std::false_type;
  667. using type = Default;
  668. };
  669. template <class Default, template <class...> class Op, class... Args>
  670. struct detector<Default, void_t<Op<Args...>>, Op, Args...>
  671. {
  672. using value_t = std::true_type;
  673. using type = Op<Args...>;
  674. };
  675. template <template <class...> class Op, class... Args>
  676. using is_detected = typename detector<nonesuch, void, Op, Args...>::value_t;
  677. template <template <class...> class Op, class... Args>
  678. using detected_t = typename detector<nonesuch, void, Op, Args...>::type;
  679. template <class Default, template <class...> class Op, class... Args>
  680. using detected_or = detector<Default, void, Op, Args...>;
  681. template <class Default, template <class...> class Op, class... Args>
  682. using detected_or_t = typename detected_or<Default, Op, Args...>::type;
  683. template <class Expected, template <class...> class Op, class... Args>
  684. using is_detected_exact = std::is_same<Expected, detected_t<Op, Args...>>;
  685. template <class To, template <class...> class Op, class... Args>
  686. using is_detected_convertible =
  687. std::is_convertible<detected_t<Op, Args...>, To>;
  688. } // namespace detail
  689. } // namespace nlohmann
  690. // #include <nlohmann/detail/macro_scope.hpp>
  691. namespace nlohmann
  692. {
  693. /*!
  694. @brief detail namespace with internal helper functions
  695. This namespace collects functions that should not be exposed,
  696. implementations of some @ref basic_json methods, and meta-programming helpers.
  697. @since version 2.1.0
  698. */
  699. namespace detail
  700. {
  701. /////////////
  702. // helpers //
  703. /////////////
  704. // Note to maintainers:
  705. //
  706. // Every trait in this file expects a non CV-qualified type.
  707. // The only exceptions are in the 'aliases for detected' section
  708. // (i.e. those of the form: decltype(T::member_function(std::declval<T>())))
  709. //
  710. // In this case, T has to be properly CV-qualified to constraint the function arguments
  711. // (e.g. to_json(BasicJsonType&, const T&))
  712. template<typename> struct is_basic_json : std::false_type {};
  713. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  714. struct is_basic_json<NLOHMANN_BASIC_JSON_TPL> : std::true_type {};
  715. //////////////////////////
  716. // aliases for detected //
  717. //////////////////////////
  718. template <typename T>
  719. using mapped_type_t = typename T::mapped_type;
  720. template <typename T>
  721. using key_type_t = typename T::key_type;
  722. template <typename T>
  723. using value_type_t = typename T::value_type;
  724. template <typename T>
  725. using difference_type_t = typename T::difference_type;
  726. template <typename T>
  727. using pointer_t = typename T::pointer;
  728. template <typename T>
  729. using reference_t = typename T::reference;
  730. template <typename T>
  731. using iterator_category_t = typename T::iterator_category;
  732. template <typename T>
  733. using iterator_t = typename T::iterator;
  734. template <typename T, typename... Args>
  735. using to_json_function = decltype(T::to_json(std::declval<Args>()...));
  736. template <typename T, typename... Args>
  737. using from_json_function = decltype(T::from_json(std::declval<Args>()...));
  738. template <typename T, typename U>
  739. using get_template_function = decltype(std::declval<T>().template get<U>());
  740. // trait checking if JSONSerializer<T>::from_json(json const&, udt&) exists
  741. template <typename BasicJsonType, typename T, typename = void>
  742. struct has_from_json : std::false_type {};
  743. template <typename BasicJsonType, typename T>
  744. struct has_from_json<BasicJsonType, T,
  745. enable_if_t<not is_basic_json<T>::value>>
  746. {
  747. using serializer = typename BasicJsonType::template json_serializer<T, void>;
  748. static constexpr bool value =
  749. is_detected_exact<void, from_json_function, serializer,
  750. const BasicJsonType&, T&>::value;
  751. };
  752. // This trait checks if JSONSerializer<T>::from_json(json const&) exists
  753. // this overload is used for non-default-constructible user-defined-types
  754. template <typename BasicJsonType, typename T, typename = void>
  755. struct has_non_default_from_json : std::false_type {};
  756. template<typename BasicJsonType, typename T>
  757. struct has_non_default_from_json<BasicJsonType, T, enable_if_t<not is_basic_json<T>::value>>
  758. {
  759. using serializer = typename BasicJsonType::template json_serializer<T, void>;
  760. static constexpr bool value =
  761. is_detected_exact<T, from_json_function, serializer,
  762. const BasicJsonType&>::value;
  763. };
  764. // This trait checks if BasicJsonType::json_serializer<T>::to_json exists
  765. // Do not evaluate the trait when T is a basic_json type, to avoid template instantiation infinite recursion.
  766. template <typename BasicJsonType, typename T, typename = void>
  767. struct has_to_json : std::false_type {};
  768. template <typename BasicJsonType, typename T>
  769. struct has_to_json<BasicJsonType, T, enable_if_t<not is_basic_json<T>::value>>
  770. {
  771. using serializer = typename BasicJsonType::template json_serializer<T, void>;
  772. static constexpr bool value =
  773. is_detected_exact<void, to_json_function, serializer, BasicJsonType&,
  774. T>::value;
  775. };
  776. ///////////////////
  777. // is_ functions //
  778. ///////////////////
  779. template <typename T, typename = void>
  780. struct is_iterator_traits : std::false_type {};
  781. template <typename T>
  782. struct is_iterator_traits<iterator_traits<T>>
  783. {
  784. private:
  785. using traits = iterator_traits<T>;
  786. public:
  787. static constexpr auto value =
  788. is_detected<value_type_t, traits>::value &&
  789. is_detected<difference_type_t, traits>::value &&
  790. is_detected<pointer_t, traits>::value &&
  791. is_detected<iterator_category_t, traits>::value &&
  792. is_detected<reference_t, traits>::value;
  793. };
  794. // source: https://stackoverflow.com/a/37193089/4116453
  795. template <typename T, typename = void>
  796. struct is_complete_type : std::false_type {};
  797. template <typename T>
  798. struct is_complete_type<T, decltype(void(sizeof(T)))> : std::true_type {};
  799. template <typename BasicJsonType, typename CompatibleObjectType,
  800. typename = void>
  801. struct is_compatible_object_type_impl : std::false_type {};
  802. template <typename BasicJsonType, typename CompatibleObjectType>
  803. struct is_compatible_object_type_impl <
  804. BasicJsonType, CompatibleObjectType,
  805. enable_if_t<is_detected<mapped_type_t, CompatibleObjectType>::value and
  806. is_detected<key_type_t, CompatibleObjectType>::value >>
  807. {
  808. using object_t = typename BasicJsonType::object_t;
  809. // macOS's is_constructible does not play well with nonesuch...
  810. static constexpr bool value =
  811. std::is_constructible<typename object_t::key_type,
  812. typename CompatibleObjectType::key_type>::value and
  813. std::is_constructible<typename object_t::mapped_type,
  814. typename CompatibleObjectType::mapped_type>::value;
  815. };
  816. template <typename BasicJsonType, typename CompatibleObjectType>
  817. struct is_compatible_object_type
  818. : is_compatible_object_type_impl<BasicJsonType, CompatibleObjectType> {};
  819. template <typename BasicJsonType, typename ConstructibleObjectType,
  820. typename = void>
  821. struct is_constructible_object_type_impl : std::false_type {};
  822. template <typename BasicJsonType, typename ConstructibleObjectType>
  823. struct is_constructible_object_type_impl <
  824. BasicJsonType, ConstructibleObjectType,
  825. enable_if_t<is_detected<mapped_type_t, ConstructibleObjectType>::value and
  826. is_detected<key_type_t, ConstructibleObjectType>::value >>
  827. {
  828. using object_t = typename BasicJsonType::object_t;
  829. static constexpr bool value =
  830. (std::is_constructible<typename ConstructibleObjectType::key_type, typename object_t::key_type>::value and
  831. std::is_same<typename object_t::mapped_type, typename ConstructibleObjectType::mapped_type>::value) or
  832. (has_from_json<BasicJsonType, typename ConstructibleObjectType::mapped_type>::value or
  833. has_non_default_from_json<BasicJsonType, typename ConstructibleObjectType::mapped_type >::value);
  834. };
  835. template <typename BasicJsonType, typename ConstructibleObjectType>
  836. struct is_constructible_object_type
  837. : is_constructible_object_type_impl<BasicJsonType,
  838. ConstructibleObjectType> {};
  839. template <typename BasicJsonType, typename CompatibleStringType,
  840. typename = void>
  841. struct is_compatible_string_type_impl : std::false_type {};
  842. template <typename BasicJsonType, typename CompatibleStringType>
  843. struct is_compatible_string_type_impl <
  844. BasicJsonType, CompatibleStringType,
  845. enable_if_t<is_detected_exact<typename BasicJsonType::string_t::value_type,
  846. value_type_t, CompatibleStringType>::value >>
  847. {
  848. static constexpr auto value =
  849. std::is_constructible<typename BasicJsonType::string_t, CompatibleStringType>::value;
  850. };
  851. template <typename BasicJsonType, typename ConstructibleStringType>
  852. struct is_compatible_string_type
  853. : is_compatible_string_type_impl<BasicJsonType, ConstructibleStringType> {};
  854. template <typename BasicJsonType, typename ConstructibleStringType,
  855. typename = void>
  856. struct is_constructible_string_type_impl : std::false_type {};
  857. template <typename BasicJsonType, typename ConstructibleStringType>
  858. struct is_constructible_string_type_impl <
  859. BasicJsonType, ConstructibleStringType,
  860. enable_if_t<is_detected_exact<typename BasicJsonType::string_t::value_type,
  861. value_type_t, ConstructibleStringType>::value >>
  862. {
  863. static constexpr auto value =
  864. std::is_constructible<ConstructibleStringType,
  865. typename BasicJsonType::string_t>::value;
  866. };
  867. template <typename BasicJsonType, typename ConstructibleStringType>
  868. struct is_constructible_string_type
  869. : is_constructible_string_type_impl<BasicJsonType, ConstructibleStringType> {};
  870. template <typename BasicJsonType, typename CompatibleArrayType, typename = void>
  871. struct is_compatible_array_type_impl : std::false_type {};
  872. template <typename BasicJsonType, typename CompatibleArrayType>
  873. struct is_compatible_array_type_impl <
  874. BasicJsonType, CompatibleArrayType,
  875. enable_if_t<is_detected<value_type_t, CompatibleArrayType>::value and
  876. is_detected<iterator_t, CompatibleArrayType>::value and
  877. // This is needed because json_reverse_iterator has a ::iterator type...
  878. // Therefore it is detected as a CompatibleArrayType.
  879. // The real fix would be to have an Iterable concept.
  880. not is_iterator_traits<
  881. iterator_traits<CompatibleArrayType>>::value >>
  882. {
  883. static constexpr bool value =
  884. std::is_constructible<BasicJsonType,
  885. typename CompatibleArrayType::value_type>::value;
  886. };
  887. template <typename BasicJsonType, typename CompatibleArrayType>
  888. struct is_compatible_array_type
  889. : is_compatible_array_type_impl<BasicJsonType, CompatibleArrayType> {};
  890. template <typename BasicJsonType, typename ConstructibleArrayType, typename = void>
  891. struct is_constructible_array_type_impl : std::false_type {};
  892. template <typename BasicJsonType, typename ConstructibleArrayType>
  893. struct is_constructible_array_type_impl <
  894. BasicJsonType, ConstructibleArrayType,
  895. enable_if_t<std::is_same<ConstructibleArrayType,
  896. typename BasicJsonType::value_type>::value >>
  897. : std::true_type {};
  898. template <typename BasicJsonType, typename ConstructibleArrayType>
  899. struct is_constructible_array_type_impl <
  900. BasicJsonType, ConstructibleArrayType,
  901. enable_if_t<not std::is_same<ConstructibleArrayType,
  902. typename BasicJsonType::value_type>::value and
  903. is_detected<value_type_t, ConstructibleArrayType>::value and
  904. is_detected<iterator_t, ConstructibleArrayType>::value and
  905. is_complete_type<
  906. detected_t<value_type_t, ConstructibleArrayType>>::value >>
  907. {
  908. static constexpr bool value =
  909. // This is needed because json_reverse_iterator has a ::iterator type,
  910. // furthermore, std::back_insert_iterator (and other iterators) have a base class `iterator`...
  911. // Therefore it is detected as a ConstructibleArrayType.
  912. // The real fix would be to have an Iterable concept.
  913. not is_iterator_traits <
  914. iterator_traits<ConstructibleArrayType >>::value and
  915. (std::is_same<typename ConstructibleArrayType::value_type, typename BasicJsonType::array_t::value_type>::value or
  916. has_from_json<BasicJsonType,
  917. typename ConstructibleArrayType::value_type>::value or
  918. has_non_default_from_json <
  919. BasicJsonType, typename ConstructibleArrayType::value_type >::value);
  920. };
  921. template <typename BasicJsonType, typename ConstructibleArrayType>
  922. struct is_constructible_array_type
  923. : is_constructible_array_type_impl<BasicJsonType, ConstructibleArrayType> {};
  924. template <typename RealIntegerType, typename CompatibleNumberIntegerType,
  925. typename = void>
  926. struct is_compatible_integer_type_impl : std::false_type {};
  927. template <typename RealIntegerType, typename CompatibleNumberIntegerType>
  928. struct is_compatible_integer_type_impl <
  929. RealIntegerType, CompatibleNumberIntegerType,
  930. enable_if_t<std::is_integral<RealIntegerType>::value and
  931. std::is_integral<CompatibleNumberIntegerType>::value and
  932. not std::is_same<bool, CompatibleNumberIntegerType>::value >>
  933. {
  934. // is there an assert somewhere on overflows?
  935. using RealLimits = std::numeric_limits<RealIntegerType>;
  936. using CompatibleLimits = std::numeric_limits<CompatibleNumberIntegerType>;
  937. static constexpr auto value =
  938. std::is_constructible<RealIntegerType,
  939. CompatibleNumberIntegerType>::value and
  940. CompatibleLimits::is_integer and
  941. RealLimits::is_signed == CompatibleLimits::is_signed;
  942. };
  943. template <typename RealIntegerType, typename CompatibleNumberIntegerType>
  944. struct is_compatible_integer_type
  945. : is_compatible_integer_type_impl<RealIntegerType,
  946. CompatibleNumberIntegerType> {};
  947. template <typename BasicJsonType, typename CompatibleType, typename = void>
  948. struct is_compatible_type_impl: std::false_type {};
  949. template <typename BasicJsonType, typename CompatibleType>
  950. struct is_compatible_type_impl <
  951. BasicJsonType, CompatibleType,
  952. enable_if_t<is_complete_type<CompatibleType>::value >>
  953. {
  954. static constexpr bool value =
  955. has_to_json<BasicJsonType, CompatibleType>::value;
  956. };
  957. template <typename BasicJsonType, typename CompatibleType>
  958. struct is_compatible_type
  959. : is_compatible_type_impl<BasicJsonType, CompatibleType> {};
  960. } // namespace detail
  961. } // namespace nlohmann
  962. // #include <nlohmann/detail/exceptions.hpp>
  963. #include <exception> // exception
  964. #include <stdexcept> // runtime_error
  965. #include <string> // to_string
  966. // #include <nlohmann/detail/input/position_t.hpp>
  967. #include <cstddef> // size_t
  968. namespace nlohmann
  969. {
  970. namespace detail
  971. {
  972. /// struct to capture the start position of the current token
  973. struct position_t
  974. {
  975. /// the total number of characters read
  976. std::size_t chars_read_total = 0;
  977. /// the number of characters read in the current line
  978. std::size_t chars_read_current_line = 0;
  979. /// the number of lines read
  980. std::size_t lines_read = 0;
  981. /// conversion to size_t to preserve SAX interface
  982. constexpr operator size_t() const
  983. {
  984. return chars_read_total;
  985. }
  986. };
  987. }
  988. }
  989. namespace nlohmann
  990. {
  991. namespace detail
  992. {
  993. ////////////////
  994. // exceptions //
  995. ////////////////
  996. /*!
  997. @brief general exception of the @ref basic_json class
  998. This class is an extension of `std::exception` objects with a member @a id for
  999. exception ids. It is used as the base class for all exceptions thrown by the
  1000. @ref basic_json class. This class can hence be used as "wildcard" to catch
  1001. exceptions.
  1002. Subclasses:
  1003. - @ref parse_error for exceptions indicating a parse error
  1004. - @ref invalid_iterator for exceptions indicating errors with iterators
  1005. - @ref type_error for exceptions indicating executing a member function with
  1006. a wrong type
  1007. - @ref out_of_range for exceptions indicating access out of the defined range
  1008. - @ref other_error for exceptions indicating other library errors
  1009. @internal
  1010. @note To have nothrow-copy-constructible exceptions, we internally use
  1011. `std::runtime_error` which can cope with arbitrary-length error messages.
  1012. Intermediate strings are built with static functions and then passed to
  1013. the actual constructor.
  1014. @endinternal
  1015. @liveexample{The following code shows how arbitrary library exceptions can be
  1016. caught.,exception}
  1017. @since version 3.0.0
  1018. */
  1019. class exception : public std::exception
  1020. {
  1021. public:
  1022. /// returns the explanatory string
  1023. const char* what() const noexcept override
  1024. {
  1025. return m.what();
  1026. }
  1027. /// the id of the exception
  1028. const int id;
  1029. protected:
  1030. exception(int id_, const char* what_arg) : id(id_), m(what_arg) {}
  1031. static std::string name(const std::string& ename, int id_)
  1032. {
  1033. return "[json.exception." + ename + "." + std::to_string(id_) + "] ";
  1034. }
  1035. private:
  1036. /// an exception object as storage for error messages
  1037. std::runtime_error m;
  1038. };
  1039. /*!
  1040. @brief exception indicating a parse error
  1041. This exception is thrown by the library when a parse error occurs. Parse errors
  1042. can occur during the deserialization of JSON text, CBOR, MessagePack, as well
  1043. as when using JSON Patch.
  1044. Member @a byte holds the byte index of the last read character in the input
  1045. file.
  1046. Exceptions have ids 1xx.
  1047. name / id | example message | description
  1048. ------------------------------ | --------------- | -------------------------
  1049. 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.
  1050. 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.
  1051. 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.
  1052. 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.
  1053. 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.
  1054. 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`.
  1055. 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.
  1056. 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.
  1057. json.exception.parse_error.109 | parse error: array index 'one' is not a number | A JSON Pointer array index must be a number.
  1058. 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.
  1059. 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.
  1060. 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.
  1061. json.exception.parse_error.114 | parse error: Unsupported BSON record type 0x0F | The parsing of the corresponding BSON record type is not implemented (yet).
  1062. @note For an input with n bytes, 1 is the index of the first character and n+1
  1063. is the index of the terminating null byte or the end of file. This also
  1064. holds true when reading a byte vector (CBOR or MessagePack).
  1065. @liveexample{The following code shows how a `parse_error` exception can be
  1066. caught.,parse_error}
  1067. @sa @ref exception for the base class of the library exceptions
  1068. @sa @ref invalid_iterator for exceptions indicating errors with iterators
  1069. @sa @ref type_error for exceptions indicating executing a member function with
  1070. a wrong type
  1071. @sa @ref out_of_range for exceptions indicating access out of the defined range
  1072. @sa @ref other_error for exceptions indicating other library errors
  1073. @since version 3.0.0
  1074. */
  1075. class parse_error : public exception
  1076. {
  1077. public:
  1078. /*!
  1079. @brief create a parse error exception
  1080. @param[in] id_ the id of the exception
  1081. @param[in] position the position where the error occurred (or with
  1082. chars_read_total=0 if the position cannot be
  1083. determined)
  1084. @param[in] what_arg the explanatory string
  1085. @return parse_error object
  1086. */
  1087. static parse_error create(int id_, const position_t& pos, const std::string& what_arg)
  1088. {
  1089. std::string w = exception::name("parse_error", id_) + "parse error" +
  1090. position_string(pos) + ": " + what_arg;
  1091. return parse_error(id_, pos.chars_read_total, w.c_str());
  1092. }
  1093. static parse_error create(int id_, std::size_t byte_, const std::string& what_arg)
  1094. {
  1095. std::string w = exception::name("parse_error", id_) + "parse error" +
  1096. (byte_ != 0 ? (" at byte " + std::to_string(byte_)) : "") +
  1097. ": " + what_arg;
  1098. return parse_error(id_, byte_, w.c_str());
  1099. }
  1100. /*!
  1101. @brief byte index of the parse error
  1102. The byte index of the last read character in the input file.
  1103. @note For an input with n bytes, 1 is the index of the first character and
  1104. n+1 is the index of the terminating null byte or the end of file.
  1105. This also holds true when reading a byte vector (CBOR or MessagePack).
  1106. */
  1107. const std::size_t byte;
  1108. private:
  1109. parse_error(int id_, std::size_t byte_, const char* what_arg)
  1110. : exception(id_, what_arg), byte(byte_) {}
  1111. static std::string position_string(const position_t& pos)
  1112. {
  1113. return " at line " + std::to_string(pos.lines_read + 1) +
  1114. ", column " + std::to_string(pos.chars_read_current_line);
  1115. }
  1116. };
  1117. /*!
  1118. @brief exception indicating errors with iterators
  1119. This exception is thrown if iterators passed to a library function do not match
  1120. the expected semantics.
  1121. Exceptions have ids 2xx.
  1122. name / id | example message | description
  1123. ----------------------------------- | --------------- | -------------------------
  1124. 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.
  1125. 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.
  1126. 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.
  1127. 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.
  1128. 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.
  1129. 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.
  1130. 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.
  1131. 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.
  1132. 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.
  1133. 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.
  1134. 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.
  1135. json.exception.invalid_iterator.212 | cannot compare iterators of different containers | When two iterators are compared, they must belong to the same container.
  1136. json.exception.invalid_iterator.213 | cannot compare order of object iterators | The order of object iterators cannot be compared, because JSON objects are unordered.
  1137. 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().
  1138. @liveexample{The following code shows how an `invalid_iterator` exception can be
  1139. caught.,invalid_iterator}
  1140. @sa @ref exception for the base class of the library exceptions
  1141. @sa @ref parse_error for exceptions indicating a parse error
  1142. @sa @ref type_error for exceptions indicating executing a member function with
  1143. a wrong type
  1144. @sa @ref out_of_range for exceptions indicating access out of the defined range
  1145. @sa @ref other_error for exceptions indicating other library errors
  1146. @since version 3.0.0
  1147. */
  1148. class invalid_iterator : public exception
  1149. {
  1150. public:
  1151. static invalid_iterator create(int id_, const std::string& what_arg)
  1152. {
  1153. std::string w = exception::name("invalid_iterator", id_) + what_arg;
  1154. return invalid_iterator(id_, w.c_str());
  1155. }
  1156. private:
  1157. invalid_iterator(int id_, const char* what_arg)
  1158. : exception(id_, what_arg) {}
  1159. };
  1160. /*!
  1161. @brief exception indicating executing a member function with a wrong type
  1162. This exception is thrown in case of a type error; that is, a library function is
  1163. executed on a JSON value whose type does not match the expected semantics.
  1164. Exceptions have ids 3xx.
  1165. name / id | example message | description
  1166. ----------------------------- | --------------- | -------------------------
  1167. 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.
  1168. 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.
  1169. 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&.
  1170. json.exception.type_error.304 | cannot use at() with string | The @ref at() member functions can only be executed for certain JSON types.
  1171. json.exception.type_error.305 | cannot use operator[] with string | The @ref operator[] member functions can only be executed for certain JSON types.
  1172. json.exception.type_error.306 | cannot use value() with string | The @ref value() member functions can only be executed for certain JSON types.
  1173. json.exception.type_error.307 | cannot use erase() with string | The @ref erase() member functions can only be executed for certain JSON types.
  1174. 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.
  1175. json.exception.type_error.309 | cannot use insert() with | The @ref insert() member functions can only be executed for certain JSON types.
  1176. json.exception.type_error.310 | cannot use swap() with number | The @ref swap() member functions can only be executed for certain JSON types.
  1177. 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.
  1178. json.exception.type_error.312 | cannot use update() with string | The @ref update() member functions can only be executed for certain JSON types.
  1179. 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.
  1180. json.exception.type_error.314 | only objects can be unflattened | The @ref unflatten function only works for an object whose keys are JSON Pointers.
  1181. 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.
  1182. 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. |
  1183. 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) |
  1184. @liveexample{The following code shows how a `type_error` exception can be
  1185. caught.,type_error}
  1186. @sa @ref exception for the base class of the library exceptions
  1187. @sa @ref parse_error for exceptions indicating a parse error
  1188. @sa @ref invalid_iterator for exceptions indicating errors with iterators
  1189. @sa @ref out_of_range for exceptions indicating access out of the defined range
  1190. @sa @ref other_error for exceptions indicating other library errors
  1191. @since version 3.0.0
  1192. */
  1193. class type_error : public exception
  1194. {
  1195. public:
  1196. static type_error create(int id_, const std::string& what_arg)
  1197. {
  1198. std::string w = exception::name("type_error", id_) + what_arg;
  1199. return type_error(id_, w.c_str());
  1200. }
  1201. private:
  1202. type_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
  1203. };
  1204. /*!
  1205. @brief exception indicating access out of the defined range
  1206. This exception is thrown in case a library function is called on an input
  1207. parameter that exceeds the expected range, for instance in case of array
  1208. indices or nonexisting object keys.
  1209. Exceptions have ids 4xx.
  1210. name / id | example message | description
  1211. ------------------------------- | --------------- | -------------------------
  1212. 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.
  1213. 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.
  1214. json.exception.out_of_range.403 | key 'foo' not found | The provided key was not found in the JSON object.
  1215. json.exception.out_of_range.404 | unresolved reference token 'foo' | A reference token in a JSON Pointer could not be resolved.
  1216. 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.
  1217. 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.
  1218. json.exception.out_of_range.407 | number overflow serializing '9223372036854775808' | UBJSON and BSON only support integer numbers up to 9223372036854775807. |
  1219. json.exception.out_of_range.408 | excessive array size: 8658170730974374167 | The size (following `#`) of an UBJSON array or object exceeds the maximal capacity. |
  1220. 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 |
  1221. @liveexample{The following code shows how an `out_of_range` exception can be
  1222. caught.,out_of_range}
  1223. @sa @ref exception for the base class of the library exceptions
  1224. @sa @ref parse_error for exceptions indicating a parse error
  1225. @sa @ref invalid_iterator for exceptions indicating errors with iterators
  1226. @sa @ref type_error for exceptions indicating executing a member function with
  1227. a wrong type
  1228. @sa @ref other_error for exceptions indicating other library errors
  1229. @since version 3.0.0
  1230. */
  1231. class out_of_range : public exception
  1232. {
  1233. public:
  1234. static out_of_range create(int id_, const std::string& what_arg)
  1235. {
  1236. std::string w = exception::name("out_of_range", id_) + what_arg;
  1237. return out_of_range(id_, w.c_str());
  1238. }
  1239. private:
  1240. out_of_range(int id_, const char* what_arg) : exception(id_, what_arg) {}
  1241. };
  1242. /*!
  1243. @brief exception indicating other library errors
  1244. This exception is thrown in case of errors that cannot be classified with the
  1245. other exception types.
  1246. Exceptions have ids 5xx.
  1247. name / id | example message | description
  1248. ------------------------------ | --------------- | -------------------------
  1249. json.exception.other_error.501 | unsuccessful: {"op":"test","path":"/baz", "value":"bar"} | A JSON Patch operation 'test' failed. The unsuccessful operation is also printed.
  1250. @sa @ref exception for the base class of the library exceptions
  1251. @sa @ref parse_error for exceptions indicating a parse error
  1252. @sa @ref invalid_iterator for exceptions indicating errors with iterators
  1253. @sa @ref type_error for exceptions indicating executing a member function with
  1254. a wrong type
  1255. @sa @ref out_of_range for exceptions indicating access out of the defined range
  1256. @liveexample{The following code shows how an `other_error` exception can be
  1257. caught.,other_error}
  1258. @since version 3.0.0
  1259. */
  1260. class other_error : public exception
  1261. {
  1262. public:
  1263. static other_error create(int id_, const std::string& what_arg)
  1264. {
  1265. std::string w = exception::name("other_error", id_) + what_arg;
  1266. return other_error(id_, w.c_str());
  1267. }
  1268. private:
  1269. other_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
  1270. };
  1271. } // namespace detail
  1272. } // namespace nlohmann
  1273. // #include <nlohmann/detail/value_t.hpp>
  1274. #include <array> // array
  1275. #include <ciso646> // and
  1276. #include <cstddef> // size_t
  1277. #include <cstdint> // uint8_t
  1278. namespace nlohmann
  1279. {
  1280. namespace detail
  1281. {
  1282. ///////////////////////////
  1283. // JSON type enumeration //
  1284. ///////////////////////////
  1285. /*!
  1286. @brief the JSON type enumeration
  1287. This enumeration collects the different JSON types. It is internally used to
  1288. distinguish the stored values, and the functions @ref basic_json::is_null(),
  1289. @ref basic_json::is_object(), @ref basic_json::is_array(),
  1290. @ref basic_json::is_string(), @ref basic_json::is_boolean(),
  1291. @ref basic_json::is_number() (with @ref basic_json::is_number_integer(),
  1292. @ref basic_json::is_number_unsigned(), and @ref basic_json::is_number_float()),
  1293. @ref basic_json::is_discarded(), @ref basic_json::is_primitive(), and
  1294. @ref basic_json::is_structured() rely on it.
  1295. @note There are three enumeration entries (number_integer, number_unsigned, and
  1296. number_float), because the library distinguishes these three types for numbers:
  1297. @ref basic_json::number_unsigned_t is used for unsigned integers,
  1298. @ref basic_json::number_integer_t is used for signed integers, and
  1299. @ref basic_json::number_float_t is used for floating-point numbers or to
  1300. approximate integers which do not fit in the limits of their respective type.
  1301. @sa @ref basic_json::basic_json(const value_t value_type) -- create a JSON
  1302. value with the default value for a given type
  1303. @since version 1.0.0
  1304. */
  1305. enum class value_t : std::uint8_t
  1306. {
  1307. null, ///< null value
  1308. object, ///< object (unordered set of name/value pairs)
  1309. array, ///< array (ordered collection of values)
  1310. string, ///< string value
  1311. boolean, ///< boolean value
  1312. number_integer, ///< number value (signed integer)
  1313. number_unsigned, ///< number value (unsigned integer)
  1314. number_float, ///< number value (floating-point)
  1315. discarded ///< discarded by the the parser callback function
  1316. };
  1317. /*!
  1318. @brief comparison operator for JSON types
  1319. Returns an ordering that is similar to Python:
  1320. - order: null < boolean < number < object < array < string
  1321. - furthermore, each type is not smaller than itself
  1322. - discarded values are not comparable
  1323. @since version 1.0.0
  1324. */
  1325. inline bool operator<(const value_t lhs, const value_t rhs) noexcept
  1326. {
  1327. static constexpr std::array<std::uint8_t, 8> order = {{
  1328. 0 /* null */, 3 /* object */, 4 /* array */, 5 /* string */,
  1329. 1 /* boolean */, 2 /* integer */, 2 /* unsigned */, 2 /* float */
  1330. }
  1331. };
  1332. const auto l_index = static_cast<std::size_t>(lhs);
  1333. const auto r_index = static_cast<std::size_t>(rhs);
  1334. return l_index < order.size() and r_index < order.size() and order[l_index] < order[r_index];
  1335. }
  1336. } // namespace detail
  1337. } // namespace nlohmann
  1338. // #include <nlohmann/detail/conversions/from_json.hpp>
  1339. #include <algorithm> // transform
  1340. #include <array> // array
  1341. #include <ciso646> // and, not
  1342. #include <forward_list> // forward_list
  1343. #include <iterator> // inserter, front_inserter, end
  1344. #include <map> // map
  1345. #include <string> // string
  1346. #include <tuple> // tuple, make_tuple
  1347. #include <type_traits> // is_arithmetic, is_same, is_enum, underlying_type, is_convertible
  1348. #include <unordered_map> // unordered_map
  1349. #include <utility> // pair, declval
  1350. #include <valarray> // valarray
  1351. // #include <nlohmann/detail/exceptions.hpp>
  1352. // #include <nlohmann/detail/macro_scope.hpp>
  1353. // #include <nlohmann/detail/meta/cpp_future.hpp>
  1354. // #include <nlohmann/detail/meta/type_traits.hpp>
  1355. // #include <nlohmann/detail/value_t.hpp>
  1356. namespace nlohmann
  1357. {
  1358. namespace detail
  1359. {
  1360. template<typename BasicJsonType>
  1361. void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
  1362. {
  1363. if (JSON_UNLIKELY(not j.is_null()))
  1364. {
  1365. JSON_THROW(type_error::create(302, "type must be null, but is " + std::string(j.type_name())));
  1366. }
  1367. n = nullptr;
  1368. }
  1369. // overloads for basic_json template parameters
  1370. template<typename BasicJsonType, typename ArithmeticType,
  1371. enable_if_t<std::is_arithmetic<ArithmeticType>::value and
  1372. not std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
  1373. int> = 0>
  1374. void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val)
  1375. {
  1376. switch (static_cast<value_t>(j))
  1377. {
  1378. case value_t::number_unsigned:
  1379. {
  1380. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
  1381. break;
  1382. }
  1383. case value_t::number_integer:
  1384. {
  1385. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
  1386. break;
  1387. }
  1388. case value_t::number_float:
  1389. {
  1390. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
  1391. break;
  1392. }
  1393. default:
  1394. JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name())));
  1395. }
  1396. }
  1397. template<typename BasicJsonType>
  1398. void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
  1399. {
  1400. if (JSON_UNLIKELY(not j.is_boolean()))
  1401. {
  1402. JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(j.type_name())));
  1403. }
  1404. b = *j.template get_ptr<const typename BasicJsonType::boolean_t*>();
  1405. }
  1406. template<typename BasicJsonType>
  1407. void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s)
  1408. {
  1409. if (JSON_UNLIKELY(not j.is_string()))
  1410. {
  1411. JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name())));
  1412. }
  1413. s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
  1414. }
  1415. template <
  1416. typename BasicJsonType, typename ConstructibleStringType,
  1417. enable_if_t <
  1418. is_constructible_string_type<BasicJsonType, ConstructibleStringType>::value and
  1419. not std::is_same<typename BasicJsonType::string_t,
  1420. ConstructibleStringType>::value,
  1421. int > = 0 >
  1422. void from_json(const BasicJsonType& j, ConstructibleStringType& s)
  1423. {
  1424. if (JSON_UNLIKELY(not j.is_string()))
  1425. {
  1426. JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name())));
  1427. }
  1428. s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
  1429. }
  1430. template<typename BasicJsonType>
  1431. void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val)
  1432. {
  1433. get_arithmetic_value(j, val);
  1434. }
  1435. template<typename BasicJsonType>
  1436. void from_json(const BasicJsonType& j, typename BasicJsonType::number_unsigned_t& val)
  1437. {
  1438. get_arithmetic_value(j, val);
  1439. }
  1440. template<typename BasicJsonType>
  1441. void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& val)
  1442. {
  1443. get_arithmetic_value(j, val);
  1444. }
  1445. template<typename BasicJsonType, typename EnumType,
  1446. enable_if_t<std::is_enum<EnumType>::value, int> = 0>
  1447. void from_json(const BasicJsonType& j, EnumType& e)
  1448. {
  1449. typename std::underlying_type<EnumType>::type val;
  1450. get_arithmetic_value(j, val);
  1451. e = static_cast<EnumType>(val);
  1452. }
  1453. // forward_list doesn't have an insert method
  1454. template<typename BasicJsonType, typename T, typename Allocator,
  1455. enable_if_t<std::is_convertible<BasicJsonType, T>::value, int> = 0>
  1456. void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
  1457. {
  1458. if (JSON_UNLIKELY(not j.is_array()))
  1459. {
  1460. JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name())));
  1461. }
  1462. std::transform(j.rbegin(), j.rend(),
  1463. std::front_inserter(l), [](const BasicJsonType & i)
  1464. {
  1465. return i.template get<T>();
  1466. });
  1467. }
  1468. // valarray doesn't have an insert method
  1469. template<typename BasicJsonType, typename T,
  1470. enable_if_t<std::is_convertible<BasicJsonType, T>::value, int> = 0>
  1471. void from_json(const BasicJsonType& j, std::valarray<T>& l)
  1472. {
  1473. if (JSON_UNLIKELY(not j.is_array()))
  1474. {
  1475. JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name())));
  1476. }
  1477. l.resize(j.size());
  1478. std::copy(j.m_value.array->begin(), j.m_value.array->end(), std::begin(l));
  1479. }
  1480. template<typename BasicJsonType>
  1481. void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType::array_t& arr, priority_tag<3> /*unused*/)
  1482. {
  1483. arr = *j.template get_ptr<const typename BasicJsonType::array_t*>();
  1484. }
  1485. template <typename BasicJsonType, typename T, std::size_t N>
  1486. auto from_json_array_impl(const BasicJsonType& j, std::array<T, N>& arr,
  1487. priority_tag<2> /*unused*/)
  1488. -> decltype(j.template get<T>(), void())
  1489. {
  1490. for (std::size_t i = 0; i < N; ++i)
  1491. {
  1492. arr[i] = j.at(i).template get<T>();
  1493. }
  1494. }
  1495. template<typename BasicJsonType, typename ConstructibleArrayType>
  1496. auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, priority_tag<1> /*unused*/)
  1497. -> decltype(
  1498. arr.reserve(std::declval<typename ConstructibleArrayType::size_type>()),
  1499. j.template get<typename ConstructibleArrayType::value_type>(),
  1500. void())
  1501. {
  1502. using std::end;
  1503. arr.reserve(j.size());
  1504. std::transform(j.begin(), j.end(),
  1505. std::inserter(arr, end(arr)), [](const BasicJsonType & i)
  1506. {
  1507. // get<BasicJsonType>() returns *this, this won't call a from_json
  1508. // method when value_type is BasicJsonType
  1509. return i.template get<typename ConstructibleArrayType::value_type>();
  1510. });
  1511. }
  1512. template <typename BasicJsonType, typename ConstructibleArrayType>
  1513. void from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr,
  1514. priority_tag<0> /*unused*/)
  1515. {
  1516. using std::end;
  1517. std::transform(
  1518. j.begin(), j.end(), std::inserter(arr, end(arr)),
  1519. [](const BasicJsonType & i)
  1520. {
  1521. // get<BasicJsonType>() returns *this, this won't call a from_json
  1522. // method when value_type is BasicJsonType
  1523. return i.template get<typename ConstructibleArrayType::value_type>();
  1524. });
  1525. }
  1526. template <typename BasicJsonType, typename ConstructibleArrayType,
  1527. enable_if_t <
  1528. is_constructible_array_type<BasicJsonType, ConstructibleArrayType>::value and
  1529. not is_constructible_object_type<BasicJsonType, ConstructibleArrayType>::value and
  1530. not is_constructible_string_type<BasicJsonType, ConstructibleArrayType>::value and
  1531. not is_basic_json<ConstructibleArrayType>::value,
  1532. int > = 0 >
  1533. auto from_json(const BasicJsonType& j, ConstructibleArrayType& arr)
  1534. -> decltype(from_json_array_impl(j, arr, priority_tag<3> {}),
  1535. j.template get<typename ConstructibleArrayType::value_type>(),
  1536. void())
  1537. {
  1538. if (JSON_UNLIKELY(not j.is_array()))
  1539. {
  1540. JSON_THROW(type_error::create(302, "type must be array, but is " +
  1541. std::string(j.type_name())));
  1542. }
  1543. from_json_array_impl(j, arr, priority_tag<3> {});
  1544. }
  1545. template<typename BasicJsonType, typename ConstructibleObjectType,
  1546. enable_if_t<is_constructible_object_type<BasicJsonType, ConstructibleObjectType>::value, int> = 0>
  1547. void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
  1548. {
  1549. if (JSON_UNLIKELY(not j.is_object()))
  1550. {
  1551. JSON_THROW(type_error::create(302, "type must be object, but is " + std::string(j.type_name())));
  1552. }
  1553. auto inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
  1554. using value_type = typename ConstructibleObjectType::value_type;
  1555. std::transform(
  1556. inner_object->begin(), inner_object->end(),
  1557. std::inserter(obj, obj.begin()),
  1558. [](typename BasicJsonType::object_t::value_type const & p)
  1559. {
  1560. return value_type(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
  1561. });
  1562. }
  1563. // overload for arithmetic types, not chosen for basic_json template arguments
  1564. // (BooleanType, etc..); note: Is it really necessary to provide explicit
  1565. // overloads for boolean_t etc. in case of a custom BooleanType which is not
  1566. // an arithmetic type?
  1567. template<typename BasicJsonType, typename ArithmeticType,
  1568. enable_if_t <
  1569. std::is_arithmetic<ArithmeticType>::value and
  1570. not std::is_same<ArithmeticType, typename BasicJsonType::number_unsigned_t>::value and
  1571. not std::is_same<ArithmeticType, typename BasicJsonType::number_integer_t>::value and
  1572. not std::is_same<ArithmeticType, typename BasicJsonType::number_float_t>::value and
  1573. not std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
  1574. int> = 0>
  1575. void from_json(const BasicJsonType& j, ArithmeticType& val)
  1576. {
  1577. switch (static_cast<value_t>(j))
  1578. {
  1579. case value_t::number_unsigned:
  1580. {
  1581. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
  1582. break;
  1583. }
  1584. case value_t::number_integer:
  1585. {
  1586. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
  1587. break;
  1588. }
  1589. case value_t::number_float:
  1590. {
  1591. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
  1592. break;
  1593. }
  1594. case value_t::boolean:
  1595. {
  1596. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::boolean_t*>());
  1597. break;
  1598. }
  1599. default:
  1600. JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name())));
  1601. }
  1602. }
  1603. template<typename BasicJsonType, typename A1, typename A2>
  1604. void from_json(const BasicJsonType& j, std::pair<A1, A2>& p)
  1605. {
  1606. p = {j.at(0).template get<A1>(), j.at(1).template get<A2>()};
  1607. }
  1608. template<typename BasicJsonType, typename Tuple, std::size_t... Idx>
  1609. void from_json_tuple_impl(const BasicJsonType& j, Tuple& t, index_sequence<Idx...> /*unused*/)
  1610. {
  1611. t = std::make_tuple(j.at(Idx).template get<typename std::tuple_element<Idx, Tuple>::type>()...);
  1612. }
  1613. template<typename BasicJsonType, typename... Args>
  1614. void from_json(const BasicJsonType& j, std::tuple<Args...>& t)
  1615. {
  1616. from_json_tuple_impl(j, t, index_sequence_for<Args...> {});
  1617. }
  1618. template <typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator,
  1619. typename = enable_if_t<not std::is_constructible<
  1620. typename BasicJsonType::string_t, Key>::value>>
  1621. void from_json(const BasicJsonType& j, std::map<Key, Value, Compare, Allocator>& m)
  1622. {
  1623. if (JSON_UNLIKELY(not j.is_array()))
  1624. {
  1625. JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name())));
  1626. }
  1627. for (const auto& p : j)
  1628. {
  1629. if (JSON_UNLIKELY(not p.is_array()))
  1630. {
  1631. JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name())));
  1632. }
  1633. m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
  1634. }
  1635. }
  1636. template <typename BasicJsonType, typename Key, typename Value, typename Hash, typename KeyEqual, typename Allocator,
  1637. typename = enable_if_t<not std::is_constructible<
  1638. typename BasicJsonType::string_t, Key>::value>>
  1639. void from_json(const BasicJsonType& j, std::unordered_map<Key, Value, Hash, KeyEqual, Allocator>& m)
  1640. {
  1641. if (JSON_UNLIKELY(not j.is_array()))
  1642. {
  1643. JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name())));
  1644. }
  1645. for (const auto& p : j)
  1646. {
  1647. if (JSON_UNLIKELY(not p.is_array()))
  1648. {
  1649. JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name())));
  1650. }
  1651. m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
  1652. }
  1653. }
  1654. struct from_json_fn
  1655. {
  1656. template<typename BasicJsonType, typename T>
  1657. auto operator()(const BasicJsonType& j, T& val) const
  1658. noexcept(noexcept(from_json(j, val)))
  1659. -> decltype(from_json(j, val), void())
  1660. {
  1661. return from_json(j, val);
  1662. }
  1663. };
  1664. } // namespace detail
  1665. /// namespace to hold default `from_json` function
  1666. /// to see why this is required:
  1667. /// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
  1668. namespace
  1669. {
  1670. constexpr const auto& from_json = detail::static_const<detail::from_json_fn>::value;
  1671. } // namespace
  1672. } // namespace nlohmann
  1673. // #include <nlohmann/detail/conversions/to_json.hpp>
  1674. #include <ciso646> // or, and, not
  1675. #include <iterator> // begin, end
  1676. #include <tuple> // tuple, get
  1677. #include <type_traits> // is_same, is_constructible, is_floating_point, is_enum, underlying_type
  1678. #include <utility> // move, forward, declval, pair
  1679. #include <valarray> // valarray
  1680. #include <vector> // vector
  1681. // #include <nlohmann/detail/meta/cpp_future.hpp>
  1682. // #include <nlohmann/detail/meta/type_traits.hpp>
  1683. // #include <nlohmann/detail/value_t.hpp>
  1684. // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
  1685. #include <cstddef> // size_t
  1686. #include <string> // string, to_string
  1687. #include <iterator> // input_iterator_tag
  1688. #include <tuple> // tuple_size, get, tuple_element
  1689. // #include <nlohmann/detail/value_t.hpp>
  1690. // #include <nlohmann/detail/meta/type_traits.hpp>
  1691. namespace nlohmann
  1692. {
  1693. namespace detail
  1694. {
  1695. template <typename IteratorType> class iteration_proxy_value
  1696. {
  1697. public:
  1698. using difference_type = std::ptrdiff_t;
  1699. using value_type = iteration_proxy_value;
  1700. using pointer = value_type * ;
  1701. using reference = value_type & ;
  1702. using iterator_category = std::input_iterator_tag;
  1703. private:
  1704. /// the iterator
  1705. IteratorType anchor;
  1706. /// an index for arrays (used to create key names)
  1707. std::size_t array_index = 0;
  1708. /// last stringified array index
  1709. mutable std::size_t array_index_last = 0;
  1710. /// a string representation of the array index
  1711. mutable std::string array_index_str = "0";
  1712. /// an empty string (to return a reference for primitive values)
  1713. const std::string empty_str = "";
  1714. public:
  1715. explicit iteration_proxy_value(IteratorType it) noexcept : anchor(it) {}
  1716. /// dereference operator (needed for range-based for)
  1717. iteration_proxy_value& operator*()
  1718. {
  1719. return *this;
  1720. }
  1721. /// increment operator (needed for range-based for)
  1722. iteration_proxy_value& operator++()
  1723. {
  1724. ++anchor;
  1725. ++array_index;
  1726. return *this;
  1727. }
  1728. /// equality operator (needed for InputIterator)
  1729. bool operator==(const iteration_proxy_value& o) const noexcept
  1730. {
  1731. return anchor == o.anchor;
  1732. }
  1733. /// inequality operator (needed for range-based for)
  1734. bool operator!=(const iteration_proxy_value& o) const noexcept
  1735. {
  1736. return anchor != o.anchor;
  1737. }
  1738. /// return key of the iterator
  1739. const std::string& key() const
  1740. {
  1741. assert(anchor.m_object != nullptr);
  1742. switch (anchor.m_object->type())
  1743. {
  1744. // use integer array index as key
  1745. case value_t::array:
  1746. {
  1747. if (array_index != array_index_last)
  1748. {
  1749. array_index_str = std::to_string(array_index);
  1750. array_index_last = array_index;
  1751. }
  1752. return array_index_str;
  1753. }
  1754. // use key from the object
  1755. case value_t::object:
  1756. return anchor.key();
  1757. // use an empty key for all primitive types
  1758. default:
  1759. return empty_str;
  1760. }
  1761. }
  1762. /// return value of the iterator
  1763. typename IteratorType::reference value() const
  1764. {
  1765. return anchor.value();
  1766. }
  1767. };
  1768. /// proxy class for the items() function
  1769. template<typename IteratorType> class iteration_proxy
  1770. {
  1771. private:
  1772. /// the container to iterate
  1773. typename IteratorType::reference container;
  1774. public:
  1775. /// construct iteration proxy from a container
  1776. explicit iteration_proxy(typename IteratorType::reference cont) noexcept
  1777. : container(cont) {}
  1778. /// return iterator begin (needed for range-based for)
  1779. iteration_proxy_value<IteratorType> begin() noexcept
  1780. {
  1781. return iteration_proxy_value<IteratorType>(container.begin());
  1782. }
  1783. /// return iterator end (needed for range-based for)
  1784. iteration_proxy_value<IteratorType> end() noexcept
  1785. {
  1786. return iteration_proxy_value<IteratorType>(container.end());
  1787. }
  1788. };
  1789. // Structured Bindings Support
  1790. // For further reference see https://blog.tartanllama.xyz/structured-bindings/
  1791. // And see https://github.com/nlohmann/json/pull/1391
  1792. template <std::size_t N, typename IteratorType, enable_if_t<N == 0, int> = 0>
  1793. auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.key())
  1794. {
  1795. return i.key();
  1796. }
  1797. // Structured Bindings Support
  1798. // For further reference see https://blog.tartanllama.xyz/structured-bindings/
  1799. // And see https://github.com/nlohmann/json/pull/1391
  1800. template <std::size_t N, typename IteratorType, enable_if_t<N == 1, int> = 0>
  1801. auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.value())
  1802. {
  1803. return i.value();
  1804. }
  1805. } // namespace detail
  1806. } // namespace nlohmann
  1807. // The Addition to the STD Namespace is required to add
  1808. // Structured Bindings Support to the iteration_proxy_value class
  1809. // For further reference see https://blog.tartanllama.xyz/structured-bindings/
  1810. // And see https://github.com/nlohmann/json/pull/1391
  1811. namespace std
  1812. {
  1813. template <typename IteratorType>
  1814. class tuple_size<::nlohmann::detail::iteration_proxy_value<IteratorType>>
  1815. : public std::integral_constant<std::size_t, 2> {};
  1816. template <std::size_t N, typename IteratorType>
  1817. class tuple_element<N, ::nlohmann::detail::iteration_proxy_value<IteratorType >>
  1818. {
  1819. public:
  1820. using type = decltype(
  1821. get<N>(std::declval <
  1822. ::nlohmann::detail::iteration_proxy_value<IteratorType >> ()));
  1823. };
  1824. }
  1825. namespace nlohmann
  1826. {
  1827. namespace detail
  1828. {
  1829. //////////////////
  1830. // constructors //
  1831. //////////////////
  1832. template<value_t> struct external_constructor;
  1833. template<>
  1834. struct external_constructor<value_t::boolean>
  1835. {
  1836. template<typename BasicJsonType>
  1837. static void construct(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept
  1838. {
  1839. j.m_type = value_t::boolean;
  1840. j.m_value = b;
  1841. j.assert_invariant();
  1842. }
  1843. };
  1844. template<>
  1845. struct external_constructor<value_t::string>
  1846. {
  1847. template<typename BasicJsonType>
  1848. static void construct(BasicJsonType& j, const typename BasicJsonType::string_t& s)
  1849. {
  1850. j.m_type = value_t::string;
  1851. j.m_value = s;
  1852. j.assert_invariant();
  1853. }
  1854. template<typename BasicJsonType>
  1855. static void construct(BasicJsonType& j, typename BasicJsonType::string_t&& s)
  1856. {
  1857. j.m_type = value_t::string;
  1858. j.m_value = std::move(s);
  1859. j.assert_invariant();
  1860. }
  1861. template<typename BasicJsonType, typename CompatibleStringType,
  1862. enable_if_t<not std::is_same<CompatibleStringType, typename BasicJsonType::string_t>::value,
  1863. int> = 0>
  1864. static void construct(BasicJsonType& j, const CompatibleStringType& str)
  1865. {
  1866. j.m_type = value_t::string;
  1867. j.m_value.string = j.template create<typename BasicJsonType::string_t>(str);
  1868. j.assert_invariant();
  1869. }
  1870. };
  1871. template<>
  1872. struct external_constructor<value_t::number_float>
  1873. {
  1874. template<typename BasicJsonType>
  1875. static void construct(BasicJsonType& j, typename BasicJsonType::number_float_t val) noexcept
  1876. {
  1877. j.m_type = value_t::number_float;
  1878. j.m_value = val;
  1879. j.assert_invariant();
  1880. }
  1881. };
  1882. template<>
  1883. struct external_constructor<value_t::number_unsigned>
  1884. {
  1885. template<typename BasicJsonType>
  1886. static void construct(BasicJsonType& j, typename BasicJsonType::number_unsigned_t val) noexcept
  1887. {
  1888. j.m_type = value_t::number_unsigned;
  1889. j.m_value = val;
  1890. j.assert_invariant();
  1891. }
  1892. };
  1893. template<>
  1894. struct external_constructor<value_t::number_integer>
  1895. {
  1896. template<typename BasicJsonType>
  1897. static void construct(BasicJsonType& j, typename BasicJsonType::number_integer_t val) noexcept
  1898. {
  1899. j.m_type = value_t::number_integer;
  1900. j.m_value = val;
  1901. j.assert_invariant();
  1902. }
  1903. };
  1904. template<>
  1905. struct external_constructor<value_t::array>
  1906. {
  1907. template<typename BasicJsonType>
  1908. static void construct(BasicJsonType& j, const typename BasicJsonType::array_t& arr)
  1909. {
  1910. j.m_type = value_t::array;
  1911. j.m_value = arr;
  1912. j.assert_invariant();
  1913. }
  1914. template<typename BasicJsonType>
  1915. static void construct(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
  1916. {
  1917. j.m_type = value_t::array;
  1918. j.m_value = std::move(arr);
  1919. j.assert_invariant();
  1920. }
  1921. template<typename BasicJsonType, typename CompatibleArrayType,
  1922. enable_if_t<not std::is_same<CompatibleArrayType, typename BasicJsonType::array_t>::value,
  1923. int> = 0>
  1924. static void construct(BasicJsonType& j, const CompatibleArrayType& arr)
  1925. {
  1926. using std::begin;
  1927. using std::end;
  1928. j.m_type = value_t::array;
  1929. j.m_value.array = j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
  1930. j.assert_invariant();
  1931. }
  1932. template<typename BasicJsonType>
  1933. static void construct(BasicJsonType& j, const std::vector<bool>& arr)
  1934. {
  1935. j.m_type = value_t::array;
  1936. j.m_value = value_t::array;
  1937. j.m_value.array->reserve(arr.size());
  1938. for (const bool x : arr)
  1939. {
  1940. j.m_value.array->push_back(x);
  1941. }
  1942. j.assert_invariant();
  1943. }
  1944. template<typename BasicJsonType, typename T,
  1945. enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
  1946. static void construct(BasicJsonType& j, const std::valarray<T>& arr)
  1947. {
  1948. j.m_type = value_t::array;
  1949. j.m_value = value_t::array;
  1950. j.m_value.array->resize(arr.size());
  1951. std::copy(std::begin(arr), std::end(arr), j.m_value.array->begin());
  1952. j.assert_invariant();
  1953. }
  1954. };
  1955. template<>
  1956. struct external_constructor<value_t::object>
  1957. {
  1958. template<typename BasicJsonType>
  1959. static void construct(BasicJsonType& j, const typename BasicJsonType::object_t& obj)
  1960. {
  1961. j.m_type = value_t::object;
  1962. j.m_value = obj;
  1963. j.assert_invariant();
  1964. }
  1965. template<typename BasicJsonType>
  1966. static void construct(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
  1967. {
  1968. j.m_type = value_t::object;
  1969. j.m_value = std::move(obj);
  1970. j.assert_invariant();
  1971. }
  1972. template<typename BasicJsonType, typename CompatibleObjectType,
  1973. enable_if_t<not std::is_same<CompatibleObjectType, typename BasicJsonType::object_t>::value, int> = 0>
  1974. static void construct(BasicJsonType& j, const CompatibleObjectType& obj)
  1975. {
  1976. using std::begin;
  1977. using std::end;
  1978. j.m_type = value_t::object;
  1979. j.m_value.object = j.template create<typename BasicJsonType::object_t>(begin(obj), end(obj));
  1980. j.assert_invariant();
  1981. }
  1982. };
  1983. /////////////
  1984. // to_json //
  1985. /////////////
  1986. template<typename BasicJsonType, typename T,
  1987. enable_if_t<std::is_same<T, typename BasicJsonType::boolean_t>::value, int> = 0>
  1988. void to_json(BasicJsonType& j, T b) noexcept
  1989. {
  1990. external_constructor<value_t::boolean>::construct(j, b);
  1991. }
  1992. template<typename BasicJsonType, typename CompatibleString,
  1993. enable_if_t<std::is_constructible<typename BasicJsonType::string_t, CompatibleString>::value, int> = 0>
  1994. void to_json(BasicJsonType& j, const CompatibleString& s)
  1995. {
  1996. external_constructor<value_t::string>::construct(j, s);
  1997. }
  1998. template<typename BasicJsonType>
  1999. void to_json(BasicJsonType& j, typename BasicJsonType::string_t&& s)
  2000. {
  2001. external_constructor<value_t::string>::construct(j, std::move(s));
  2002. }
  2003. template<typename BasicJsonType, typename FloatType,
  2004. enable_if_t<std::is_floating_point<FloatType>::value, int> = 0>
  2005. void to_json(BasicJsonType& j, FloatType val) noexcept
  2006. {
  2007. external_constructor<value_t::number_float>::construct(j, static_cast<typename BasicJsonType::number_float_t>(val));
  2008. }
  2009. template<typename BasicJsonType, typename CompatibleNumberUnsignedType,
  2010. enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_unsigned_t, CompatibleNumberUnsignedType>::value, int> = 0>
  2011. void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val) noexcept
  2012. {
  2013. external_constructor<value_t::number_unsigned>::construct(j, static_cast<typename BasicJsonType::number_unsigned_t>(val));
  2014. }
  2015. template<typename BasicJsonType, typename CompatibleNumberIntegerType,
  2016. enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_integer_t, CompatibleNumberIntegerType>::value, int> = 0>
  2017. void to_json(BasicJsonType& j, CompatibleNumberIntegerType val) noexcept
  2018. {
  2019. external_constructor<value_t::number_integer>::construct(j, static_cast<typename BasicJsonType::number_integer_t>(val));
  2020. }
  2021. template<typename BasicJsonType, typename EnumType,
  2022. enable_if_t<std::is_enum<EnumType>::value, int> = 0>
  2023. void to_json(BasicJsonType& j, EnumType e) noexcept
  2024. {
  2025. using underlying_type = typename std::underlying_type<EnumType>::type;
  2026. external_constructor<value_t::number_integer>::construct(j, static_cast<underlying_type>(e));
  2027. }
  2028. template<typename BasicJsonType>
  2029. void to_json(BasicJsonType& j, const std::vector<bool>& e)
  2030. {
  2031. external_constructor<value_t::array>::construct(j, e);
  2032. }
  2033. template <typename BasicJsonType, typename CompatibleArrayType,
  2034. enable_if_t<is_compatible_array_type<BasicJsonType,
  2035. CompatibleArrayType>::value and
  2036. not is_compatible_object_type<
  2037. BasicJsonType, CompatibleArrayType>::value and
  2038. not is_compatible_string_type<BasicJsonType, CompatibleArrayType>::value and
  2039. not is_basic_json<CompatibleArrayType>::value,
  2040. int> = 0>
  2041. void to_json(BasicJsonType& j, const CompatibleArrayType& arr)
  2042. {
  2043. external_constructor<value_t::array>::construct(j, arr);
  2044. }
  2045. template<typename BasicJsonType, typename T,
  2046. enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
  2047. void to_json(BasicJsonType& j, const std::valarray<T>& arr)
  2048. {
  2049. external_constructor<value_t::array>::construct(j, std::move(arr));
  2050. }
  2051. template<typename BasicJsonType>
  2052. void to_json(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
  2053. {
  2054. external_constructor<value_t::array>::construct(j, std::move(arr));
  2055. }
  2056. template<typename BasicJsonType, typename CompatibleObjectType,
  2057. enable_if_t<is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value and not is_basic_json<CompatibleObjectType>::value, int> = 0>
  2058. void to_json(BasicJsonType& j, const CompatibleObjectType& obj)
  2059. {
  2060. external_constructor<value_t::object>::construct(j, obj);
  2061. }
  2062. template<typename BasicJsonType>
  2063. void to_json(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
  2064. {
  2065. external_constructor<value_t::object>::construct(j, std::move(obj));
  2066. }
  2067. template <
  2068. typename BasicJsonType, typename T, std::size_t N,
  2069. enable_if_t<not std::is_constructible<typename BasicJsonType::string_t,
  2070. const T(&)[N]>::value,
  2071. int> = 0 >
  2072. void to_json(BasicJsonType& j, const T(&arr)[N])
  2073. {
  2074. external_constructor<value_t::array>::construct(j, arr);
  2075. }
  2076. template<typename BasicJsonType, typename... Args>
  2077. void to_json(BasicJsonType& j, const std::pair<Args...>& p)
  2078. {
  2079. j = { p.first, p.second };
  2080. }
  2081. // for https://github.com/nlohmann/json/pull/1134
  2082. template < typename BasicJsonType, typename T,
  2083. enable_if_t<std::is_same<T, iteration_proxy_value<typename BasicJsonType::iterator>>::value, int> = 0>
  2084. void to_json(BasicJsonType& j, const T& b)
  2085. {
  2086. j = { {b.key(), b.value()} };
  2087. }
  2088. template<typename BasicJsonType, typename Tuple, std::size_t... Idx>
  2089. void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence<Idx...> /*unused*/)
  2090. {
  2091. j = { std::get<Idx>(t)... };
  2092. }
  2093. template<typename BasicJsonType, typename... Args>
  2094. void to_json(BasicJsonType& j, const std::tuple<Args...>& t)
  2095. {
  2096. to_json_tuple_impl(j, t, index_sequence_for<Args...> {});
  2097. }
  2098. struct to_json_fn
  2099. {
  2100. template<typename BasicJsonType, typename T>
  2101. auto operator()(BasicJsonType& j, T&& val) const noexcept(noexcept(to_json(j, std::forward<T>(val))))
  2102. -> decltype(to_json(j, std::forward<T>(val)), void())
  2103. {
  2104. return to_json(j, std::forward<T>(val));
  2105. }
  2106. };
  2107. } // namespace detail
  2108. /// namespace to hold default `to_json` function
  2109. namespace
  2110. {
  2111. constexpr const auto& to_json = detail::static_const<detail::to_json_fn>::value;
  2112. } // namespace
  2113. } // namespace nlohmann
  2114. // #include <nlohmann/detail/input/input_adapters.hpp>
  2115. #include <cassert> // assert
  2116. #include <cstddef> // size_t
  2117. #include <cstring> // strlen
  2118. #include <istream> // istream
  2119. #include <iterator> // begin, end, iterator_traits, random_access_iterator_tag, distance, next
  2120. #include <memory> // shared_ptr, make_shared, addressof
  2121. #include <numeric> // accumulate
  2122. #include <string> // string, char_traits
  2123. #include <type_traits> // enable_if, is_base_of, is_pointer, is_integral, remove_pointer
  2124. #include <utility> // pair, declval
  2125. #include <cstdio> //FILE *
  2126. // #include <nlohmann/detail/macro_scope.hpp>
  2127. namespace nlohmann
  2128. {
  2129. namespace detail
  2130. {
  2131. /// the supported input formats
  2132. enum class input_format_t { json, cbor, msgpack, ubjson, bson };
  2133. ////////////////////
  2134. // input adapters //
  2135. ////////////////////
  2136. /*!
  2137. @brief abstract input adapter interface
  2138. Produces a stream of std::char_traits<char>::int_type characters from a
  2139. std::istream, a buffer, or some other input type. Accepts the return of
  2140. exactly one non-EOF character for future input. The int_type characters
  2141. returned consist of all valid char values as positive values (typically
  2142. unsigned char), plus an EOF value outside that range, specified by the value
  2143. of the function std::char_traits<char>::eof(). This value is typically -1, but
  2144. could be any arbitrary value which is not a valid char value.
  2145. */
  2146. struct input_adapter_protocol
  2147. {
  2148. /// get a character [0,255] or std::char_traits<char>::eof().
  2149. virtual std::char_traits<char>::int_type get_character() = 0;
  2150. virtual ~input_adapter_protocol() = default;
  2151. };
  2152. /// a type to simplify interfaces
  2153. using input_adapter_t = std::shared_ptr<input_adapter_protocol>;
  2154. /*!
  2155. Input adapter for stdio file access. This adapter read only 1 byte and do not use any
  2156. buffer. This adapter is a very low level adapter.
  2157. */
  2158. class file_input_adapter : public input_adapter_protocol
  2159. {
  2160. public:
  2161. explicit file_input_adapter(std::FILE* f) noexcept
  2162. : m_file(f)
  2163. {}
  2164. std::char_traits<char>::int_type get_character() noexcept override
  2165. {
  2166. return std::fgetc(m_file);
  2167. }
  2168. private:
  2169. /// the file pointer to read from
  2170. std::FILE* m_file;
  2171. };
  2172. /*!
  2173. Input adapter for a (caching) istream. Ignores a UFT Byte Order Mark at
  2174. beginning of input. Does not support changing the underlying std::streambuf
  2175. in mid-input. Maintains underlying std::istream and std::streambuf to support
  2176. subsequent use of standard std::istream operations to process any input
  2177. characters following those used in parsing the JSON input. Clears the
  2178. std::istream flags; any input errors (e.g., EOF) will be detected by the first
  2179. subsequent call for input from the std::istream.
  2180. */
  2181. class input_stream_adapter : public input_adapter_protocol
  2182. {
  2183. public:
  2184. ~input_stream_adapter() override
  2185. {
  2186. // clear stream flags; we use underlying streambuf I/O, do not
  2187. // maintain ifstream flags, except eof
  2188. is.clear(is.rdstate() & std::ios::eofbit);
  2189. }
  2190. explicit input_stream_adapter(std::istream& i)
  2191. : is(i), sb(*i.rdbuf())
  2192. {}
  2193. // delete because of pointer members
  2194. input_stream_adapter(const input_stream_adapter&) = delete;
  2195. input_stream_adapter& operator=(input_stream_adapter&) = delete;
  2196. input_stream_adapter(input_stream_adapter&&) = delete;
  2197. input_stream_adapter& operator=(input_stream_adapter&&) = delete;
  2198. // std::istream/std::streambuf use std::char_traits<char>::to_int_type, to
  2199. // ensure that std::char_traits<char>::eof() and the character 0xFF do not
  2200. // end up as the same value, eg. 0xFFFFFFFF.
  2201. std::char_traits<char>::int_type get_character() override
  2202. {
  2203. auto res = sb.sbumpc();
  2204. // set eof manually, as we don't use the istream interface.
  2205. if (res == EOF)
  2206. {
  2207. is.clear(is.rdstate() | std::ios::eofbit);
  2208. }
  2209. return res;
  2210. }
  2211. private:
  2212. /// the associated input stream
  2213. std::istream& is;
  2214. std::streambuf& sb;
  2215. };
  2216. /// input adapter for buffer input
  2217. class input_buffer_adapter : public input_adapter_protocol
  2218. {
  2219. public:
  2220. input_buffer_adapter(const char* b, const std::size_t l) noexcept
  2221. : cursor(b), limit(b + l)
  2222. {}
  2223. // delete because of pointer members
  2224. input_buffer_adapter(const input_buffer_adapter&) = delete;
  2225. input_buffer_adapter& operator=(input_buffer_adapter&) = delete;
  2226. input_buffer_adapter(input_buffer_adapter&&) = delete;
  2227. input_buffer_adapter& operator=(input_buffer_adapter&&) = delete;
  2228. ~input_buffer_adapter() override = default;
  2229. std::char_traits<char>::int_type get_character() noexcept override
  2230. {
  2231. if (JSON_LIKELY(cursor < limit))
  2232. {
  2233. return std::char_traits<char>::to_int_type(*(cursor++));
  2234. }
  2235. return std::char_traits<char>::eof();
  2236. }
  2237. private:
  2238. /// pointer to the current character
  2239. const char* cursor;
  2240. /// pointer past the last character
  2241. const char* const limit;
  2242. };
  2243. template<typename WideStringType, size_t T>
  2244. struct wide_string_input_helper
  2245. {
  2246. // UTF-32
  2247. static void fill_buffer(const WideStringType& str, size_t& current_wchar, std::array<std::char_traits<char>::int_type, 4>& utf8_bytes, size_t& utf8_bytes_index, size_t& utf8_bytes_filled)
  2248. {
  2249. utf8_bytes_index = 0;
  2250. if (current_wchar == str.size())
  2251. {
  2252. utf8_bytes[0] = std::char_traits<char>::eof();
  2253. utf8_bytes_filled = 1;
  2254. }
  2255. else
  2256. {
  2257. // get the current character
  2258. const auto wc = static_cast<int>(str[current_wchar++]);
  2259. // UTF-32 to UTF-8 encoding
  2260. if (wc < 0x80)
  2261. {
  2262. utf8_bytes[0] = wc;
  2263. utf8_bytes_filled = 1;
  2264. }
  2265. else if (wc <= 0x7FF)
  2266. {
  2267. utf8_bytes[0] = 0xC0 | ((wc >> 6) & 0x1F);
  2268. utf8_bytes[1] = 0x80 | (wc & 0x3F);
  2269. utf8_bytes_filled = 2;
  2270. }
  2271. else if (wc <= 0xFFFF)
  2272. {
  2273. utf8_bytes[0] = 0xE0 | ((wc >> 12) & 0x0F);
  2274. utf8_bytes[1] = 0x80 | ((wc >> 6) & 0x3F);
  2275. utf8_bytes[2] = 0x80 | (wc & 0x3F);
  2276. utf8_bytes_filled = 3;
  2277. }
  2278. else if (wc <= 0x10FFFF)
  2279. {
  2280. utf8_bytes[0] = 0xF0 | ((wc >> 18) & 0x07);
  2281. utf8_bytes[1] = 0x80 | ((wc >> 12) & 0x3F);
  2282. utf8_bytes[2] = 0x80 | ((wc >> 6) & 0x3F);
  2283. utf8_bytes[3] = 0x80 | (wc & 0x3F);
  2284. utf8_bytes_filled = 4;
  2285. }
  2286. else
  2287. {
  2288. // unknown character
  2289. utf8_bytes[0] = wc;
  2290. utf8_bytes_filled = 1;
  2291. }
  2292. }
  2293. }
  2294. };
  2295. template<typename WideStringType>
  2296. struct wide_string_input_helper<WideStringType, 2>
  2297. {
  2298. // UTF-16
  2299. static void fill_buffer(const WideStringType& str, size_t& current_wchar, std::array<std::char_traits<char>::int_type, 4>& utf8_bytes, size_t& utf8_bytes_index, size_t& utf8_bytes_filled)
  2300. {
  2301. utf8_bytes_index = 0;
  2302. if (current_wchar == str.size())
  2303. {
  2304. utf8_bytes[0] = std::char_traits<char>::eof();
  2305. utf8_bytes_filled = 1;
  2306. }
  2307. else
  2308. {
  2309. // get the current character
  2310. const auto wc = static_cast<int>(str[current_wchar++]);
  2311. // UTF-16 to UTF-8 encoding
  2312. if (wc < 0x80)
  2313. {
  2314. utf8_bytes[0] = wc;
  2315. utf8_bytes_filled = 1;
  2316. }
  2317. else if (wc <= 0x7FF)
  2318. {
  2319. utf8_bytes[0] = 0xC0 | ((wc >> 6));
  2320. utf8_bytes[1] = 0x80 | (wc & 0x3F);
  2321. utf8_bytes_filled = 2;
  2322. }
  2323. else if (0xD800 > wc or wc >= 0xE000)
  2324. {
  2325. utf8_bytes[0] = 0xE0 | ((wc >> 12));
  2326. utf8_bytes[1] = 0x80 | ((wc >> 6) & 0x3F);
  2327. utf8_bytes[2] = 0x80 | (wc & 0x3F);
  2328. utf8_bytes_filled = 3;
  2329. }
  2330. else
  2331. {
  2332. if (current_wchar < str.size())
  2333. {
  2334. const auto wc2 = static_cast<int>(str[current_wchar++]);
  2335. const int charcode = 0x10000 + (((wc & 0x3FF) << 10) | (wc2 & 0x3FF));
  2336. utf8_bytes[0] = 0xf0 | (charcode >> 18);
  2337. utf8_bytes[1] = 0x80 | ((charcode >> 12) & 0x3F);
  2338. utf8_bytes[2] = 0x80 | ((charcode >> 6) & 0x3F);
  2339. utf8_bytes[3] = 0x80 | (charcode & 0x3F);
  2340. utf8_bytes_filled = 4;
  2341. }
  2342. else
  2343. {
  2344. // unknown character
  2345. ++current_wchar;
  2346. utf8_bytes[0] = wc;
  2347. utf8_bytes_filled = 1;
  2348. }
  2349. }
  2350. }
  2351. }
  2352. };
  2353. template<typename WideStringType>
  2354. class wide_string_input_adapter : public input_adapter_protocol
  2355. {
  2356. public:
  2357. explicit wide_string_input_adapter(const WideStringType& w) noexcept
  2358. : str(w)
  2359. {}
  2360. std::char_traits<char>::int_type get_character() noexcept override
  2361. {
  2362. // check if buffer needs to be filled
  2363. if (utf8_bytes_index == utf8_bytes_filled)
  2364. {
  2365. fill_buffer<sizeof(typename WideStringType::value_type)>();
  2366. assert(utf8_bytes_filled > 0);
  2367. assert(utf8_bytes_index == 0);
  2368. }
  2369. // use buffer
  2370. assert(utf8_bytes_filled > 0);
  2371. assert(utf8_bytes_index < utf8_bytes_filled);
  2372. return utf8_bytes[utf8_bytes_index++];
  2373. }
  2374. private:
  2375. template<size_t T>
  2376. void fill_buffer()
  2377. {
  2378. wide_string_input_helper<WideStringType, T>::fill_buffer(str, current_wchar, utf8_bytes, utf8_bytes_index, utf8_bytes_filled);
  2379. }
  2380. /// the wstring to process
  2381. const WideStringType& str;
  2382. /// index of the current wchar in str
  2383. std::size_t current_wchar = 0;
  2384. /// a buffer for UTF-8 bytes
  2385. std::array<std::char_traits<char>::int_type, 4> utf8_bytes = {{0, 0, 0, 0}};
  2386. /// index to the utf8_codes array for the next valid byte
  2387. std::size_t utf8_bytes_index = 0;
  2388. /// number of valid bytes in the utf8_codes array
  2389. std::size_t utf8_bytes_filled = 0;
  2390. };
  2391. class input_adapter
  2392. {
  2393. public:
  2394. // native support
  2395. input_adapter(std::FILE* file)
  2396. : ia(std::make_shared<file_input_adapter>(file)) {}
  2397. /// input adapter for input stream
  2398. input_adapter(std::istream& i)
  2399. : ia(std::make_shared<input_stream_adapter>(i)) {}
  2400. /// input adapter for input stream
  2401. input_adapter(std::istream&& i)
  2402. : ia(std::make_shared<input_stream_adapter>(i)) {}
  2403. input_adapter(const std::wstring& ws)
  2404. : ia(std::make_shared<wide_string_input_adapter<std::wstring>>(ws)) {}
  2405. input_adapter(const std::u16string& ws)
  2406. : ia(std::make_shared<wide_string_input_adapter<std::u16string>>(ws)) {}
  2407. input_adapter(const std::u32string& ws)
  2408. : ia(std::make_shared<wide_string_input_adapter<std::u32string>>(ws)) {}
  2409. /// input adapter for buffer
  2410. template<typename CharT,
  2411. typename std::enable_if<
  2412. std::is_pointer<CharT>::value and
  2413. std::is_integral<typename std::remove_pointer<CharT>::type>::value and
  2414. sizeof(typename std::remove_pointer<CharT>::type) == 1,
  2415. int>::type = 0>
  2416. input_adapter(CharT b, std::size_t l)
  2417. : ia(std::make_shared<input_buffer_adapter>(reinterpret_cast<const char*>(b), l)) {}
  2418. // derived support
  2419. /// input adapter for string literal
  2420. template<typename CharT,
  2421. typename std::enable_if<
  2422. std::is_pointer<CharT>::value and
  2423. std::is_integral<typename std::remove_pointer<CharT>::type>::value and
  2424. sizeof(typename std::remove_pointer<CharT>::type) == 1,
  2425. int>::type = 0>
  2426. input_adapter(CharT b)
  2427. : input_adapter(reinterpret_cast<const char*>(b),
  2428. std::strlen(reinterpret_cast<const char*>(b))) {}
  2429. /// input adapter for iterator range with contiguous storage
  2430. template<class IteratorType,
  2431. typename std::enable_if<
  2432. std::is_same<typename iterator_traits<IteratorType>::iterator_category, std::random_access_iterator_tag>::value,
  2433. int>::type = 0>
  2434. input_adapter(IteratorType first, IteratorType last)
  2435. {
  2436. #ifndef NDEBUG
  2437. // assertion to check that the iterator range is indeed contiguous,
  2438. // see http://stackoverflow.com/a/35008842/266378 for more discussion
  2439. const auto is_contiguous = std::accumulate(
  2440. first, last, std::pair<bool, int>(true, 0),
  2441. [&first](std::pair<bool, int> res, decltype(*first) val)
  2442. {
  2443. res.first &= (val == *(std::next(std::addressof(*first), res.second++)));
  2444. return res;
  2445. }).first;
  2446. assert(is_contiguous);
  2447. #endif
  2448. // assertion to check that each element is 1 byte long
  2449. static_assert(
  2450. sizeof(typename iterator_traits<IteratorType>::value_type) == 1,
  2451. "each element in the iterator range must have the size of 1 byte");
  2452. const auto len = static_cast<size_t>(std::distance(first, last));
  2453. if (JSON_LIKELY(len > 0))
  2454. {
  2455. // there is at least one element: use the address of first
  2456. ia = std::make_shared<input_buffer_adapter>(reinterpret_cast<const char*>(&(*first)), len);
  2457. }
  2458. else
  2459. {
  2460. // the address of first cannot be used: use nullptr
  2461. ia = std::make_shared<input_buffer_adapter>(nullptr, len);
  2462. }
  2463. }
  2464. /// input adapter for array
  2465. template<class T, std::size_t N>
  2466. input_adapter(T (&array)[N])
  2467. : input_adapter(std::begin(array), std::end(array)) {}
  2468. /// input adapter for contiguous container
  2469. template<class ContiguousContainer, typename
  2470. std::enable_if<not std::is_pointer<ContiguousContainer>::value and
  2471. std::is_base_of<std::random_access_iterator_tag, typename iterator_traits<decltype(std::begin(std::declval<ContiguousContainer const>()))>::iterator_category>::value,
  2472. int>::type = 0>
  2473. input_adapter(const ContiguousContainer& c)
  2474. : input_adapter(std::begin(c), std::end(c)) {}
  2475. operator input_adapter_t()
  2476. {
  2477. return ia;
  2478. }
  2479. private:
  2480. /// the actual adapter
  2481. input_adapter_t ia = nullptr;
  2482. };
  2483. } // namespace detail
  2484. } // namespace nlohmann
  2485. // #include <nlohmann/detail/input/lexer.hpp>
  2486. #include <clocale> // localeconv
  2487. #include <cstddef> // size_t
  2488. #include <cstdlib> // strtof, strtod, strtold, strtoll, strtoull
  2489. #include <cstdio> // snprintf
  2490. #include <initializer_list> // initializer_list
  2491. #include <string> // char_traits, string
  2492. #include <vector> // vector
  2493. // #include <nlohmann/detail/macro_scope.hpp>
  2494. // #include <nlohmann/detail/input/input_adapters.hpp>
  2495. // #include <nlohmann/detail/input/position_t.hpp>
  2496. namespace nlohmann
  2497. {
  2498. namespace detail
  2499. {
  2500. ///////////
  2501. // lexer //
  2502. ///////////
  2503. /*!
  2504. @brief lexical analysis
  2505. This class organizes the lexical analysis during JSON deserialization.
  2506. */
  2507. template<typename BasicJsonType>
  2508. class lexer
  2509. {
  2510. using number_integer_t = typename BasicJsonType::number_integer_t;
  2511. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  2512. using number_float_t = typename BasicJsonType::number_float_t;
  2513. using string_t = typename BasicJsonType::string_t;
  2514. public:
  2515. /// token types for the parser
  2516. enum class token_type
  2517. {
  2518. uninitialized, ///< indicating the scanner is uninitialized
  2519. literal_true, ///< the `true` literal
  2520. literal_false, ///< the `false` literal
  2521. literal_null, ///< the `null` literal
  2522. value_string, ///< a string -- use get_string() for actual value
  2523. value_unsigned, ///< an unsigned integer -- use get_number_unsigned() for actual value
  2524. value_integer, ///< a signed integer -- use get_number_integer() for actual value
  2525. value_float, ///< an floating point number -- use get_number_float() for actual value
  2526. begin_array, ///< the character for array begin `[`
  2527. begin_object, ///< the character for object begin `{`
  2528. end_array, ///< the character for array end `]`
  2529. end_object, ///< the character for object end `}`
  2530. name_separator, ///< the name separator `:`
  2531. value_separator, ///< the value separator `,`
  2532. parse_error, ///< indicating a parse error
  2533. end_of_input, ///< indicating the end of the input buffer
  2534. literal_or_value ///< a literal or the begin of a value (only for diagnostics)
  2535. };
  2536. /// return name of values of type token_type (only used for errors)
  2537. static const char* token_type_name(const token_type t) noexcept
  2538. {
  2539. switch (t)
  2540. {
  2541. case token_type::uninitialized:
  2542. return "<uninitialized>";
  2543. case token_type::literal_true:
  2544. return "true literal";
  2545. case token_type::literal_false:
  2546. return "false literal";
  2547. case token_type::literal_null:
  2548. return "null literal";
  2549. case token_type::value_string:
  2550. return "string literal";
  2551. case lexer::token_type::value_unsigned:
  2552. case lexer::token_type::value_integer:
  2553. case lexer::token_type::value_float:
  2554. return "number literal";
  2555. case token_type::begin_array:
  2556. return "'['";
  2557. case token_type::begin_object:
  2558. return "'{'";
  2559. case token_type::end_array:
  2560. return "']'";
  2561. case token_type::end_object:
  2562. return "'}'";
  2563. case token_type::name_separator:
  2564. return "':'";
  2565. case token_type::value_separator:
  2566. return "','";
  2567. case token_type::parse_error:
  2568. return "<parse error>";
  2569. case token_type::end_of_input:
  2570. return "end of input";
  2571. case token_type::literal_or_value:
  2572. return "'[', '{', or a literal";
  2573. // LCOV_EXCL_START
  2574. default: // catch non-enum values
  2575. return "unknown token";
  2576. // LCOV_EXCL_STOP
  2577. }
  2578. }
  2579. explicit lexer(detail::input_adapter_t&& adapter)
  2580. : ia(std::move(adapter)), decimal_point_char(get_decimal_point()) {}
  2581. // delete because of pointer members
  2582. lexer(const lexer&) = delete;
  2583. lexer(lexer&&) = delete;
  2584. lexer& operator=(lexer&) = delete;
  2585. lexer& operator=(lexer&&) = delete;
  2586. ~lexer() = default;
  2587. private:
  2588. /////////////////////
  2589. // locales
  2590. /////////////////////
  2591. /// return the locale-dependent decimal point
  2592. static char get_decimal_point() noexcept
  2593. {
  2594. const auto loc = localeconv();
  2595. assert(loc != nullptr);
  2596. return (loc->decimal_point == nullptr) ? '.' : *(loc->decimal_point);
  2597. }
  2598. /////////////////////
  2599. // scan functions
  2600. /////////////////////
  2601. /*!
  2602. @brief get codepoint from 4 hex characters following `\u`
  2603. For input "\u c1 c2 c3 c4" the codepoint is:
  2604. (c1 * 0x1000) + (c2 * 0x0100) + (c3 * 0x0010) + c4
  2605. = (c1 << 12) + (c2 << 8) + (c3 << 4) + (c4 << 0)
  2606. Furthermore, the possible characters '0'..'9', 'A'..'F', and 'a'..'f'
  2607. must be converted to the integers 0x0..0x9, 0xA..0xF, 0xA..0xF, resp. The
  2608. conversion is done by subtracting the offset (0x30, 0x37, and 0x57)
  2609. between the ASCII value of the character and the desired integer value.
  2610. @return codepoint (0x0000..0xFFFF) or -1 in case of an error (e.g. EOF or
  2611. non-hex character)
  2612. */
  2613. int get_codepoint()
  2614. {
  2615. // this function only makes sense after reading `\u`
  2616. assert(current == 'u');
  2617. int codepoint = 0;
  2618. const auto factors = { 12, 8, 4, 0 };
  2619. for (const auto factor : factors)
  2620. {
  2621. get();
  2622. if (current >= '0' and current <= '9')
  2623. {
  2624. codepoint += ((current - 0x30) << factor);
  2625. }
  2626. else if (current >= 'A' and current <= 'F')
  2627. {
  2628. codepoint += ((current - 0x37) << factor);
  2629. }
  2630. else if (current >= 'a' and current <= 'f')
  2631. {
  2632. codepoint += ((current - 0x57) << factor);
  2633. }
  2634. else
  2635. {
  2636. return -1;
  2637. }
  2638. }
  2639. assert(0x0000 <= codepoint and codepoint <= 0xFFFF);
  2640. return codepoint;
  2641. }
  2642. /*!
  2643. @brief check if the next byte(s) are inside a given range
  2644. Adds the current byte and, for each passed range, reads a new byte and
  2645. checks if it is inside the range. If a violation was detected, set up an
  2646. error message and return false. Otherwise, return true.
  2647. @param[in] ranges list of integers; interpreted as list of pairs of
  2648. inclusive lower and upper bound, respectively
  2649. @pre The passed list @a ranges must have 2, 4, or 6 elements; that is,
  2650. 1, 2, or 3 pairs. This precondition is enforced by an assertion.
  2651. @return true if and only if no range violation was detected
  2652. */
  2653. bool next_byte_in_range(std::initializer_list<int> ranges)
  2654. {
  2655. assert(ranges.size() == 2 or ranges.size() == 4 or ranges.size() == 6);
  2656. add(current);
  2657. for (auto range = ranges.begin(); range != ranges.end(); ++range)
  2658. {
  2659. get();
  2660. if (JSON_LIKELY(*range <= current and current <= *(++range)))
  2661. {
  2662. add(current);
  2663. }
  2664. else
  2665. {
  2666. error_message = "invalid string: ill-formed UTF-8 byte";
  2667. return false;
  2668. }
  2669. }
  2670. return true;
  2671. }
  2672. /*!
  2673. @brief scan a string literal
  2674. This function scans a string according to Sect. 7 of RFC 7159. While
  2675. scanning, bytes are escaped and copied into buffer token_buffer. Then the
  2676. function returns successfully, token_buffer is *not* null-terminated (as it
  2677. may contain \0 bytes), and token_buffer.size() is the number of bytes in the
  2678. string.
  2679. @return token_type::value_string if string could be successfully scanned,
  2680. token_type::parse_error otherwise
  2681. @note In case of errors, variable error_message contains a textual
  2682. description.
  2683. */
  2684. token_type scan_string()
  2685. {
  2686. // reset token_buffer (ignore opening quote)
  2687. reset();
  2688. // we entered the function by reading an open quote
  2689. assert(current == '\"');
  2690. while (true)
  2691. {
  2692. // get next character
  2693. switch (get())
  2694. {
  2695. // end of file while parsing string
  2696. case std::char_traits<char>::eof():
  2697. {
  2698. error_message = "invalid string: missing closing quote";
  2699. return token_type::parse_error;
  2700. }
  2701. // closing quote
  2702. case '\"':
  2703. {
  2704. return token_type::value_string;
  2705. }
  2706. // escapes
  2707. case '\\':
  2708. {
  2709. switch (get())
  2710. {
  2711. // quotation mark
  2712. case '\"':
  2713. add('\"');
  2714. break;
  2715. // reverse solidus
  2716. case '\\':
  2717. add('\\');
  2718. break;
  2719. // solidus
  2720. case '/':
  2721. add('/');
  2722. break;
  2723. // backspace
  2724. case 'b':
  2725. add('\b');
  2726. break;
  2727. // form feed
  2728. case 'f':
  2729. add('\f');
  2730. break;
  2731. // line feed
  2732. case 'n':
  2733. add('\n');
  2734. break;
  2735. // carriage return
  2736. case 'r':
  2737. add('\r');
  2738. break;
  2739. // tab
  2740. case 't':
  2741. add('\t');
  2742. break;
  2743. // unicode escapes
  2744. case 'u':
  2745. {
  2746. const int codepoint1 = get_codepoint();
  2747. int codepoint = codepoint1; // start with codepoint1
  2748. if (JSON_UNLIKELY(codepoint1 == -1))
  2749. {
  2750. error_message = "invalid string: '\\u' must be followed by 4 hex digits";
  2751. return token_type::parse_error;
  2752. }
  2753. // check if code point is a high surrogate
  2754. if (0xD800 <= codepoint1 and codepoint1 <= 0xDBFF)
  2755. {
  2756. // expect next \uxxxx entry
  2757. if (JSON_LIKELY(get() == '\\' and get() == 'u'))
  2758. {
  2759. const int codepoint2 = get_codepoint();
  2760. if (JSON_UNLIKELY(codepoint2 == -1))
  2761. {
  2762. error_message = "invalid string: '\\u' must be followed by 4 hex digits";
  2763. return token_type::parse_error;
  2764. }
  2765. // check if codepoint2 is a low surrogate
  2766. if (JSON_LIKELY(0xDC00 <= codepoint2 and codepoint2 <= 0xDFFF))
  2767. {
  2768. // overwrite codepoint
  2769. codepoint =
  2770. // high surrogate occupies the most significant 22 bits
  2771. (codepoint1 << 10)
  2772. // low surrogate occupies the least significant 15 bits
  2773. + codepoint2
  2774. // there is still the 0xD800, 0xDC00 and 0x10000 noise
  2775. // in the result so we have to subtract with:
  2776. // (0xD800 << 10) + DC00 - 0x10000 = 0x35FDC00
  2777. - 0x35FDC00;
  2778. }
  2779. else
  2780. {
  2781. error_message = "invalid string: surrogate U+DC00..U+DFFF must be followed by U+DC00..U+DFFF";
  2782. return token_type::parse_error;
  2783. }
  2784. }
  2785. else
  2786. {
  2787. error_message = "invalid string: surrogate U+DC00..U+DFFF must be followed by U+DC00..U+DFFF";
  2788. return token_type::parse_error;
  2789. }
  2790. }
  2791. else
  2792. {
  2793. if (JSON_UNLIKELY(0xDC00 <= codepoint1 and codepoint1 <= 0xDFFF))
  2794. {
  2795. error_message = "invalid string: surrogate U+DC00..U+DFFF must follow U+D800..U+DBFF";
  2796. return token_type::parse_error;
  2797. }
  2798. }
  2799. // result of the above calculation yields a proper codepoint
  2800. assert(0x00 <= codepoint and codepoint <= 0x10FFFF);
  2801. // translate codepoint into bytes
  2802. if (codepoint < 0x80)
  2803. {
  2804. // 1-byte characters: 0xxxxxxx (ASCII)
  2805. add(codepoint);
  2806. }
  2807. else if (codepoint <= 0x7FF)
  2808. {
  2809. // 2-byte characters: 110xxxxx 10xxxxxx
  2810. add(0xC0 | (codepoint >> 6));
  2811. add(0x80 | (codepoint & 0x3F));
  2812. }
  2813. else if (codepoint <= 0xFFFF)
  2814. {
  2815. // 3-byte characters: 1110xxxx 10xxxxxx 10xxxxxx
  2816. add(0xE0 | (codepoint >> 12));
  2817. add(0x80 | ((codepoint >> 6) & 0x3F));
  2818. add(0x80 | (codepoint & 0x3F));
  2819. }
  2820. else
  2821. {
  2822. // 4-byte characters: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  2823. add(0xF0 | (codepoint >> 18));
  2824. add(0x80 | ((codepoint >> 12) & 0x3F));
  2825. add(0x80 | ((codepoint >> 6) & 0x3F));
  2826. add(0x80 | (codepoint & 0x3F));
  2827. }
  2828. break;
  2829. }
  2830. // other characters after escape
  2831. default:
  2832. error_message = "invalid string: forbidden character after backslash";
  2833. return token_type::parse_error;
  2834. }
  2835. break;
  2836. }
  2837. // invalid control characters
  2838. case 0x00:
  2839. {
  2840. error_message = "invalid string: control character U+0000 (NUL) must be escaped to \\u0000";
  2841. return token_type::parse_error;
  2842. }
  2843. case 0x01:
  2844. {
  2845. error_message = "invalid string: control character U+0001 (SOH) must be escaped to \\u0001";
  2846. return token_type::parse_error;
  2847. }
  2848. case 0x02:
  2849. {
  2850. error_message = "invalid string: control character U+0002 (STX) must be escaped to \\u0002";
  2851. return token_type::parse_error;
  2852. }
  2853. case 0x03:
  2854. {
  2855. error_message = "invalid string: control character U+0003 (ETX) must be escaped to \\u0003";
  2856. return token_type::parse_error;
  2857. }
  2858. case 0x04:
  2859. {
  2860. error_message = "invalid string: control character U+0004 (EOT) must be escaped to \\u0004";
  2861. return token_type::parse_error;
  2862. }
  2863. case 0x05:
  2864. {
  2865. error_message = "invalid string: control character U+0005 (ENQ) must be escaped to \\u0005";
  2866. return token_type::parse_error;
  2867. }
  2868. case 0x06:
  2869. {
  2870. error_message = "invalid string: control character U+0006 (ACK) must be escaped to \\u0006";
  2871. return token_type::parse_error;
  2872. }
  2873. case 0x07:
  2874. {
  2875. error_message = "invalid string: control character U+0007 (BEL) must be escaped to \\u0007";
  2876. return token_type::parse_error;
  2877. }
  2878. case 0x08:
  2879. {
  2880. error_message = "invalid string: control character U+0008 (BS) must be escaped to \\u0008 or \\b";
  2881. return token_type::parse_error;
  2882. }
  2883. case 0x09:
  2884. {
  2885. error_message = "invalid string: control character U+0009 (HT) must be escaped to \\u0009 or \\t";
  2886. return token_type::parse_error;
  2887. }
  2888. case 0x0A:
  2889. {
  2890. error_message = "invalid string: control character U+000A (LF) must be escaped to \\u000A or \\n";
  2891. return token_type::parse_error;
  2892. }
  2893. case 0x0B:
  2894. {
  2895. error_message = "invalid string: control character U+000B (VT) must be escaped to \\u000B";
  2896. return token_type::parse_error;
  2897. }
  2898. case 0x0C:
  2899. {
  2900. error_message = "invalid string: control character U+000C (FF) must be escaped to \\u000C or \\f";
  2901. return token_type::parse_error;
  2902. }
  2903. case 0x0D:
  2904. {
  2905. error_message = "invalid string: control character U+000D (CR) must be escaped to \\u000D or \\r";
  2906. return token_type::parse_error;
  2907. }
  2908. case 0x0E:
  2909. {
  2910. error_message = "invalid string: control character U+000E (SO) must be escaped to \\u000E";
  2911. return token_type::parse_error;
  2912. }
  2913. case 0x0F:
  2914. {
  2915. error_message = "invalid string: control character U+000F (SI) must be escaped to \\u000F";
  2916. return token_type::parse_error;
  2917. }
  2918. case 0x10:
  2919. {
  2920. error_message = "invalid string: control character U+0010 (DLE) must be escaped to \\u0010";
  2921. return token_type::parse_error;
  2922. }
  2923. case 0x11:
  2924. {
  2925. error_message = "invalid string: control character U+0011 (DC1) must be escaped to \\u0011";
  2926. return token_type::parse_error;
  2927. }
  2928. case 0x12:
  2929. {
  2930. error_message = "invalid string: control character U+0012 (DC2) must be escaped to \\u0012";
  2931. return token_type::parse_error;
  2932. }
  2933. case 0x13:
  2934. {
  2935. error_message = "invalid string: control character U+0013 (DC3) must be escaped to \\u0013";
  2936. return token_type::parse_error;
  2937. }
  2938. case 0x14:
  2939. {
  2940. error_message = "invalid string: control character U+0014 (DC4) must be escaped to \\u0014";
  2941. return token_type::parse_error;
  2942. }
  2943. case 0x15:
  2944. {
  2945. error_message = "invalid string: control character U+0015 (NAK) must be escaped to \\u0015";
  2946. return token_type::parse_error;
  2947. }
  2948. case 0x16:
  2949. {
  2950. error_message = "invalid string: control character U+0016 (SYN) must be escaped to \\u0016";
  2951. return token_type::parse_error;
  2952. }
  2953. case 0x17:
  2954. {
  2955. error_message = "invalid string: control character U+0017 (ETB) must be escaped to \\u0017";
  2956. return token_type::parse_error;
  2957. }
  2958. case 0x18:
  2959. {
  2960. error_message = "invalid string: control character U+0018 (CAN) must be escaped to \\u0018";
  2961. return token_type::parse_error;
  2962. }
  2963. case 0x19:
  2964. {
  2965. error_message = "invalid string: control character U+0019 (EM) must be escaped to \\u0019";
  2966. return token_type::parse_error;
  2967. }
  2968. case 0x1A:
  2969. {
  2970. error_message = "invalid string: control character U+001A (SUB) must be escaped to \\u001A";
  2971. return token_type::parse_error;
  2972. }
  2973. case 0x1B:
  2974. {
  2975. error_message = "invalid string: control character U+001B (ESC) must be escaped to \\u001B";
  2976. return token_type::parse_error;
  2977. }
  2978. case 0x1C:
  2979. {
  2980. error_message = "invalid string: control character U+001C (FS) must be escaped to \\u001C";
  2981. return token_type::parse_error;
  2982. }
  2983. case 0x1D:
  2984. {
  2985. error_message = "invalid string: control character U+001D (GS) must be escaped to \\u001D";
  2986. return token_type::parse_error;
  2987. }
  2988. case 0x1E:
  2989. {
  2990. error_message = "invalid string: control character U+001E (RS) must be escaped to \\u001E";
  2991. return token_type::parse_error;
  2992. }
  2993. case 0x1F:
  2994. {
  2995. error_message = "invalid string: control character U+001F (US) must be escaped to \\u001F";
  2996. return token_type::parse_error;
  2997. }
  2998. // U+0020..U+007F (except U+0022 (quote) and U+005C (backspace))
  2999. case 0x20:
  3000. case 0x21:
  3001. case 0x23:
  3002. case 0x24:
  3003. case 0x25:
  3004. case 0x26:
  3005. case 0x27:
  3006. case 0x28:
  3007. case 0x29:
  3008. case 0x2A:
  3009. case 0x2B:
  3010. case 0x2C:
  3011. case 0x2D:
  3012. case 0x2E:
  3013. case 0x2F:
  3014. case 0x30:
  3015. case 0x31:
  3016. case 0x32:
  3017. case 0x33:
  3018. case 0x34:
  3019. case 0x35:
  3020. case 0x36:
  3021. case 0x37:
  3022. case 0x38:
  3023. case 0x39:
  3024. case 0x3A:
  3025. case 0x3B:
  3026. case 0x3C:
  3027. case 0x3D:
  3028. case 0x3E:
  3029. case 0x3F:
  3030. case 0x40:
  3031. case 0x41:
  3032. case 0x42:
  3033. case 0x43:
  3034. case 0x44:
  3035. case 0x45:
  3036. case 0x46:
  3037. case 0x47:
  3038. case 0x48:
  3039. case 0x49:
  3040. case 0x4A:
  3041. case 0x4B:
  3042. case 0x4C:
  3043. case 0x4D:
  3044. case 0x4E:
  3045. case 0x4F:
  3046. case 0x50:
  3047. case 0x51:
  3048. case 0x52:
  3049. case 0x53:
  3050. case 0x54:
  3051. case 0x55:
  3052. case 0x56:
  3053. case 0x57:
  3054. case 0x58:
  3055. case 0x59:
  3056. case 0x5A:
  3057. case 0x5B:
  3058. case 0x5D:
  3059. case 0x5E:
  3060. case 0x5F:
  3061. case 0x60:
  3062. case 0x61:
  3063. case 0x62:
  3064. case 0x63:
  3065. case 0x64:
  3066. case 0x65:
  3067. case 0x66:
  3068. case 0x67:
  3069. case 0x68:
  3070. case 0x69:
  3071. case 0x6A:
  3072. case 0x6B:
  3073. case 0x6C:
  3074. case 0x6D:
  3075. case 0x6E:
  3076. case 0x6F:
  3077. case 0x70:
  3078. case 0x71:
  3079. case 0x72:
  3080. case 0x73:
  3081. case 0x74:
  3082. case 0x75:
  3083. case 0x76:
  3084. case 0x77:
  3085. case 0x78:
  3086. case 0x79:
  3087. case 0x7A:
  3088. case 0x7B:
  3089. case 0x7C:
  3090. case 0x7D:
  3091. case 0x7E:
  3092. case 0x7F:
  3093. {
  3094. add(current);
  3095. break;
  3096. }
  3097. // U+0080..U+07FF: bytes C2..DF 80..BF
  3098. case 0xC2:
  3099. case 0xC3:
  3100. case 0xC4:
  3101. case 0xC5:
  3102. case 0xC6:
  3103. case 0xC7:
  3104. case 0xC8:
  3105. case 0xC9:
  3106. case 0xCA:
  3107. case 0xCB:
  3108. case 0xCC:
  3109. case 0xCD:
  3110. case 0xCE:
  3111. case 0xCF:
  3112. case 0xD0:
  3113. case 0xD1:
  3114. case 0xD2:
  3115. case 0xD3:
  3116. case 0xD4:
  3117. case 0xD5:
  3118. case 0xD6:
  3119. case 0xD7:
  3120. case 0xD8:
  3121. case 0xD9:
  3122. case 0xDA:
  3123. case 0xDB:
  3124. case 0xDC:
  3125. case 0xDD:
  3126. case 0xDE:
  3127. case 0xDF:
  3128. {
  3129. if (JSON_UNLIKELY(not next_byte_in_range({0x80, 0xBF})))
  3130. {
  3131. return token_type::parse_error;
  3132. }
  3133. break;
  3134. }
  3135. // U+0800..U+0FFF: bytes E0 A0..BF 80..BF
  3136. case 0xE0:
  3137. {
  3138. if (JSON_UNLIKELY(not (next_byte_in_range({0xA0, 0xBF, 0x80, 0xBF}))))
  3139. {
  3140. return token_type::parse_error;
  3141. }
  3142. break;
  3143. }
  3144. // U+1000..U+CFFF: bytes E1..EC 80..BF 80..BF
  3145. // U+E000..U+FFFF: bytes EE..EF 80..BF 80..BF
  3146. case 0xE1:
  3147. case 0xE2:
  3148. case 0xE3:
  3149. case 0xE4:
  3150. case 0xE5:
  3151. case 0xE6:
  3152. case 0xE7:
  3153. case 0xE8:
  3154. case 0xE9:
  3155. case 0xEA:
  3156. case 0xEB:
  3157. case 0xEC:
  3158. case 0xEE:
  3159. case 0xEF:
  3160. {
  3161. if (JSON_UNLIKELY(not (next_byte_in_range({0x80, 0xBF, 0x80, 0xBF}))))
  3162. {
  3163. return token_type::parse_error;
  3164. }
  3165. break;
  3166. }
  3167. // U+D000..U+D7FF: bytes ED 80..9F 80..BF
  3168. case 0xED:
  3169. {
  3170. if (JSON_UNLIKELY(not (next_byte_in_range({0x80, 0x9F, 0x80, 0xBF}))))
  3171. {
  3172. return token_type::parse_error;
  3173. }
  3174. break;
  3175. }
  3176. // U+10000..U+3FFFF F0 90..BF 80..BF 80..BF
  3177. case 0xF0:
  3178. {
  3179. if (JSON_UNLIKELY(not (next_byte_in_range({0x90, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
  3180. {
  3181. return token_type::parse_error;
  3182. }
  3183. break;
  3184. }
  3185. // U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF
  3186. case 0xF1:
  3187. case 0xF2:
  3188. case 0xF3:
  3189. {
  3190. if (JSON_UNLIKELY(not (next_byte_in_range({0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
  3191. {
  3192. return token_type::parse_error;
  3193. }
  3194. break;
  3195. }
  3196. // U+100000..U+10FFFF F4 80..8F 80..BF 80..BF
  3197. case 0xF4:
  3198. {
  3199. if (JSON_UNLIKELY(not (next_byte_in_range({0x80, 0x8F, 0x80, 0xBF, 0x80, 0xBF}))))
  3200. {
  3201. return token_type::parse_error;
  3202. }
  3203. break;
  3204. }
  3205. // remaining bytes (80..C1 and F5..FF) are ill-formed
  3206. default:
  3207. {
  3208. error_message = "invalid string: ill-formed UTF-8 byte";
  3209. return token_type::parse_error;
  3210. }
  3211. }
  3212. }
  3213. }
  3214. static void strtof(float& f, const char* str, char** endptr) noexcept
  3215. {
  3216. f = std::strtof(str, endptr);
  3217. }
  3218. static void strtof(double& f, const char* str, char** endptr) noexcept
  3219. {
  3220. f = std::strtod(str, endptr);
  3221. }
  3222. static void strtof(long double& f, const char* str, char** endptr) noexcept
  3223. {
  3224. f = std::strtold(str, endptr);
  3225. }
  3226. /*!
  3227. @brief scan a number literal
  3228. This function scans a string according to Sect. 6 of RFC 7159.
  3229. The function is realized with a deterministic finite state machine derived
  3230. from the grammar described in RFC 7159. Starting in state "init", the
  3231. input is read and used to determined the next state. Only state "done"
  3232. accepts the number. State "error" is a trap state to model errors. In the
  3233. table below, "anything" means any character but the ones listed before.
  3234. state | 0 | 1-9 | e E | + | - | . | anything
  3235. ---------|----------|----------|----------|---------|---------|----------|-----------
  3236. init | zero | any1 | [error] | [error] | minus | [error] | [error]
  3237. minus | zero | any1 | [error] | [error] | [error] | [error] | [error]
  3238. zero | done | done | exponent | done | done | decimal1 | done
  3239. any1 | any1 | any1 | exponent | done | done | decimal1 | done
  3240. decimal1 | decimal2 | [error] | [error] | [error] | [error] | [error] | [error]
  3241. decimal2 | decimal2 | decimal2 | exponent | done | done | done | done
  3242. exponent | any2 | any2 | [error] | sign | sign | [error] | [error]
  3243. sign | any2 | any2 | [error] | [error] | [error] | [error] | [error]
  3244. any2 | any2 | any2 | done | done | done | done | done
  3245. The state machine is realized with one label per state (prefixed with
  3246. "scan_number_") and `goto` statements between them. The state machine
  3247. contains cycles, but any cycle can be left when EOF is read. Therefore,
  3248. the function is guaranteed to terminate.
  3249. During scanning, the read bytes are stored in token_buffer. This string is
  3250. then converted to a signed integer, an unsigned integer, or a
  3251. floating-point number.
  3252. @return token_type::value_unsigned, token_type::value_integer, or
  3253. token_type::value_float if number could be successfully scanned,
  3254. token_type::parse_error otherwise
  3255. @note The scanner is independent of the current locale. Internally, the
  3256. locale's decimal point is used instead of `.` to work with the
  3257. locale-dependent converters.
  3258. */
  3259. token_type scan_number() // lgtm [cpp/use-of-goto]
  3260. {
  3261. // reset token_buffer to store the number's bytes
  3262. reset();
  3263. // the type of the parsed number; initially set to unsigned; will be
  3264. // changed if minus sign, decimal point or exponent is read
  3265. token_type number_type = token_type::value_unsigned;
  3266. // state (init): we just found out we need to scan a number
  3267. switch (current)
  3268. {
  3269. case '-':
  3270. {
  3271. add(current);
  3272. goto scan_number_minus;
  3273. }
  3274. case '0':
  3275. {
  3276. add(current);
  3277. goto scan_number_zero;
  3278. }
  3279. case '1':
  3280. case '2':
  3281. case '3':
  3282. case '4':
  3283. case '5':
  3284. case '6':
  3285. case '7':
  3286. case '8':
  3287. case '9':
  3288. {
  3289. add(current);
  3290. goto scan_number_any1;
  3291. }
  3292. // LCOV_EXCL_START
  3293. default:
  3294. {
  3295. // all other characters are rejected outside scan_number()
  3296. assert(false);
  3297. }
  3298. // LCOV_EXCL_STOP
  3299. }
  3300. scan_number_minus:
  3301. // state: we just parsed a leading minus sign
  3302. number_type = token_type::value_integer;
  3303. switch (get())
  3304. {
  3305. case '0':
  3306. {
  3307. add(current);
  3308. goto scan_number_zero;
  3309. }
  3310. case '1':
  3311. case '2':
  3312. case '3':
  3313. case '4':
  3314. case '5':
  3315. case '6':
  3316. case '7':
  3317. case '8':
  3318. case '9':
  3319. {
  3320. add(current);
  3321. goto scan_number_any1;
  3322. }
  3323. default:
  3324. {
  3325. error_message = "invalid number; expected digit after '-'";
  3326. return token_type::parse_error;
  3327. }
  3328. }
  3329. scan_number_zero:
  3330. // state: we just parse a zero (maybe with a leading minus sign)
  3331. switch (get())
  3332. {
  3333. case '.':
  3334. {
  3335. add(decimal_point_char);
  3336. goto scan_number_decimal1;
  3337. }
  3338. case 'e':
  3339. case 'E':
  3340. {
  3341. add(current);
  3342. goto scan_number_exponent;
  3343. }
  3344. default:
  3345. goto scan_number_done;
  3346. }
  3347. scan_number_any1:
  3348. // state: we just parsed a number 0-9 (maybe with a leading minus sign)
  3349. switch (get())
  3350. {
  3351. case '0':
  3352. case '1':
  3353. case '2':
  3354. case '3':
  3355. case '4':
  3356. case '5':
  3357. case '6':
  3358. case '7':
  3359. case '8':
  3360. case '9':
  3361. {
  3362. add(current);
  3363. goto scan_number_any1;
  3364. }
  3365. case '.':
  3366. {
  3367. add(decimal_point_char);
  3368. goto scan_number_decimal1;
  3369. }
  3370. case 'e':
  3371. case 'E':
  3372. {
  3373. add(current);
  3374. goto scan_number_exponent;
  3375. }
  3376. default:
  3377. goto scan_number_done;
  3378. }
  3379. scan_number_decimal1:
  3380. // state: we just parsed a decimal point
  3381. number_type = token_type::value_float;
  3382. switch (get())
  3383. {
  3384. case '0':
  3385. case '1':
  3386. case '2':
  3387. case '3':
  3388. case '4':
  3389. case '5':
  3390. case '6':
  3391. case '7':
  3392. case '8':
  3393. case '9':
  3394. {
  3395. add(current);
  3396. goto scan_number_decimal2;
  3397. }
  3398. default:
  3399. {
  3400. error_message = "invalid number; expected digit after '.'";
  3401. return token_type::parse_error;
  3402. }
  3403. }
  3404. scan_number_decimal2:
  3405. // we just parsed at least one number after a decimal point
  3406. switch (get())
  3407. {
  3408. case '0':
  3409. case '1':
  3410. case '2':
  3411. case '3':
  3412. case '4':
  3413. case '5':
  3414. case '6':
  3415. case '7':
  3416. case '8':
  3417. case '9':
  3418. {
  3419. add(current);
  3420. goto scan_number_decimal2;
  3421. }
  3422. case 'e':
  3423. case 'E':
  3424. {
  3425. add(current);
  3426. goto scan_number_exponent;
  3427. }
  3428. default:
  3429. goto scan_number_done;
  3430. }
  3431. scan_number_exponent:
  3432. // we just parsed an exponent
  3433. number_type = token_type::value_float;
  3434. switch (get())
  3435. {
  3436. case '+':
  3437. case '-':
  3438. {
  3439. add(current);
  3440. goto scan_number_sign;
  3441. }
  3442. case '0':
  3443. case '1':
  3444. case '2':
  3445. case '3':
  3446. case '4':
  3447. case '5':
  3448. case '6':
  3449. case '7':
  3450. case '8':
  3451. case '9':
  3452. {
  3453. add(current);
  3454. goto scan_number_any2;
  3455. }
  3456. default:
  3457. {
  3458. error_message =
  3459. "invalid number; expected '+', '-', or digit after exponent";
  3460. return token_type::parse_error;
  3461. }
  3462. }
  3463. scan_number_sign:
  3464. // we just parsed an exponent sign
  3465. switch (get())
  3466. {
  3467. case '0':
  3468. case '1':
  3469. case '2':
  3470. case '3':
  3471. case '4':
  3472. case '5':
  3473. case '6':
  3474. case '7':
  3475. case '8':
  3476. case '9':
  3477. {
  3478. add(current);
  3479. goto scan_number_any2;
  3480. }
  3481. default:
  3482. {
  3483. error_message = "invalid number; expected digit after exponent sign";
  3484. return token_type::parse_error;
  3485. }
  3486. }
  3487. scan_number_any2:
  3488. // we just parsed a number after the exponent or exponent sign
  3489. switch (get())
  3490. {
  3491. case '0':
  3492. case '1':
  3493. case '2':
  3494. case '3':
  3495. case '4':
  3496. case '5':
  3497. case '6':
  3498. case '7':
  3499. case '8':
  3500. case '9':
  3501. {
  3502. add(current);
  3503. goto scan_number_any2;
  3504. }
  3505. default:
  3506. goto scan_number_done;
  3507. }
  3508. scan_number_done:
  3509. // unget the character after the number (we only read it to know that
  3510. // we are done scanning a number)
  3511. unget();
  3512. char* endptr = nullptr;
  3513. errno = 0;
  3514. // try to parse integers first and fall back to floats
  3515. if (number_type == token_type::value_unsigned)
  3516. {
  3517. const auto x = std::strtoull(token_buffer.data(), &endptr, 10);
  3518. // we checked the number format before
  3519. assert(endptr == token_buffer.data() + token_buffer.size());
  3520. if (errno == 0)
  3521. {
  3522. value_unsigned = static_cast<number_unsigned_t>(x);
  3523. if (value_unsigned == x)
  3524. {
  3525. return token_type::value_unsigned;
  3526. }
  3527. }
  3528. }
  3529. else if (number_type == token_type::value_integer)
  3530. {
  3531. const auto x = std::strtoll(token_buffer.data(), &endptr, 10);
  3532. // we checked the number format before
  3533. assert(endptr == token_buffer.data() + token_buffer.size());
  3534. if (errno == 0)
  3535. {
  3536. value_integer = static_cast<number_integer_t>(x);
  3537. if (value_integer == x)
  3538. {
  3539. return token_type::value_integer;
  3540. }
  3541. }
  3542. }
  3543. // this code is reached if we parse a floating-point number or if an
  3544. // integer conversion above failed
  3545. strtof(value_float, token_buffer.data(), &endptr);
  3546. // we checked the number format before
  3547. assert(endptr == token_buffer.data() + token_buffer.size());
  3548. return token_type::value_float;
  3549. }
  3550. /*!
  3551. @param[in] literal_text the literal text to expect
  3552. @param[in] length the length of the passed literal text
  3553. @param[in] return_type the token type to return on success
  3554. */
  3555. token_type scan_literal(const char* literal_text, const std::size_t length,
  3556. token_type return_type)
  3557. {
  3558. assert(current == literal_text[0]);
  3559. for (std::size_t i = 1; i < length; ++i)
  3560. {
  3561. if (JSON_UNLIKELY(get() != literal_text[i]))
  3562. {
  3563. error_message = "invalid literal";
  3564. return token_type::parse_error;
  3565. }
  3566. }
  3567. return return_type;
  3568. }
  3569. /////////////////////
  3570. // input management
  3571. /////////////////////
  3572. /// reset token_buffer; current character is beginning of token
  3573. void reset() noexcept
  3574. {
  3575. token_buffer.clear();
  3576. token_string.clear();
  3577. token_string.push_back(std::char_traits<char>::to_char_type(current));
  3578. }
  3579. /*
  3580. @brief get next character from the input
  3581. This function provides the interface to the used input adapter. It does
  3582. not throw in case the input reached EOF, but returns a
  3583. `std::char_traits<char>::eof()` in that case. Stores the scanned characters
  3584. for use in error messages.
  3585. @return character read from the input
  3586. */
  3587. std::char_traits<char>::int_type get()
  3588. {
  3589. ++position.chars_read_total;
  3590. ++position.chars_read_current_line;
  3591. if (next_unget)
  3592. {
  3593. // just reset the next_unget variable and work with current
  3594. next_unget = false;
  3595. }
  3596. else
  3597. {
  3598. current = ia->get_character();
  3599. }
  3600. if (JSON_LIKELY(current != std::char_traits<char>::eof()))
  3601. {
  3602. token_string.push_back(std::char_traits<char>::to_char_type(current));
  3603. }
  3604. if (current == '\n')
  3605. {
  3606. ++position.lines_read;
  3607. ++position.chars_read_current_line = 0;
  3608. }
  3609. return current;
  3610. }
  3611. /*!
  3612. @brief unget current character (read it again on next get)
  3613. We implement unget by setting variable next_unget to true. The input is not
  3614. changed - we just simulate ungetting by modifying chars_read_total,
  3615. chars_read_current_line, and token_string. The next call to get() will
  3616. behave as if the unget character is read again.
  3617. */
  3618. void unget()
  3619. {
  3620. next_unget = true;
  3621. --position.chars_read_total;
  3622. // in case we "unget" a newline, we have to also decrement the lines_read
  3623. if (position.chars_read_current_line == 0)
  3624. {
  3625. if (position.lines_read > 0)
  3626. {
  3627. --position.lines_read;
  3628. }
  3629. }
  3630. else
  3631. {
  3632. --position.chars_read_current_line;
  3633. }
  3634. if (JSON_LIKELY(current != std::char_traits<char>::eof()))
  3635. {
  3636. assert(token_string.size() != 0);
  3637. token_string.pop_back();
  3638. }
  3639. }
  3640. /// add a character to token_buffer
  3641. void add(int c)
  3642. {
  3643. token_buffer.push_back(std::char_traits<char>::to_char_type(c));
  3644. }
  3645. public:
  3646. /////////////////////
  3647. // value getters
  3648. /////////////////////
  3649. /// return integer value
  3650. constexpr number_integer_t get_number_integer() const noexcept
  3651. {
  3652. return value_integer;
  3653. }
  3654. /// return unsigned integer value
  3655. constexpr number_unsigned_t get_number_unsigned() const noexcept
  3656. {
  3657. return value_unsigned;
  3658. }
  3659. /// return floating-point value
  3660. constexpr number_float_t get_number_float() const noexcept
  3661. {
  3662. return value_float;
  3663. }
  3664. /// return current string value (implicitly resets the token; useful only once)
  3665. string_t& get_string()
  3666. {
  3667. return token_buffer;
  3668. }
  3669. /////////////////////
  3670. // diagnostics
  3671. /////////////////////
  3672. /// return position of last read token
  3673. constexpr position_t get_position() const noexcept
  3674. {
  3675. return position;
  3676. }
  3677. /// return the last read token (for errors only). Will never contain EOF
  3678. /// (an arbitrary value that is not a valid char value, often -1), because
  3679. /// 255 may legitimately occur. May contain NUL, which should be escaped.
  3680. std::string get_token_string() const
  3681. {
  3682. // escape control characters
  3683. std::string result;
  3684. for (const auto c : token_string)
  3685. {
  3686. if ('\x00' <= c and c <= '\x1F')
  3687. {
  3688. // escape control characters
  3689. char cs[9];
  3690. (std::snprintf)(cs, 9, "<U+%.4X>", static_cast<unsigned char>(c));
  3691. result += cs;
  3692. }
  3693. else
  3694. {
  3695. // add character as is
  3696. result.push_back(c);
  3697. }
  3698. }
  3699. return result;
  3700. }
  3701. /// return syntax error message
  3702. constexpr const char* get_error_message() const noexcept
  3703. {
  3704. return error_message;
  3705. }
  3706. /////////////////////
  3707. // actual scanner
  3708. /////////////////////
  3709. /*!
  3710. @brief skip the UTF-8 byte order mark
  3711. @return true iff there is no BOM or the correct BOM has been skipped
  3712. */
  3713. bool skip_bom()
  3714. {
  3715. if (get() == 0xEF)
  3716. {
  3717. // check if we completely parse the BOM
  3718. return get() == 0xBB and get() == 0xBF;
  3719. }
  3720. // the first character is not the beginning of the BOM; unget it to
  3721. // process is later
  3722. unget();
  3723. return true;
  3724. }
  3725. token_type scan()
  3726. {
  3727. // initially, skip the BOM
  3728. if (position.chars_read_total == 0 and not skip_bom())
  3729. {
  3730. error_message = "invalid BOM; must be 0xEF 0xBB 0xBF if given";
  3731. return token_type::parse_error;
  3732. }
  3733. // read next character and ignore whitespace
  3734. do
  3735. {
  3736. get();
  3737. }
  3738. while (current == ' ' or current == '\t' or current == '\n' or current == '\r');
  3739. switch (current)
  3740. {
  3741. // structural characters
  3742. case '[':
  3743. return token_type::begin_array;
  3744. case ']':
  3745. return token_type::end_array;
  3746. case '{':
  3747. return token_type::begin_object;
  3748. case '}':
  3749. return token_type::end_object;
  3750. case ':':
  3751. return token_type::name_separator;
  3752. case ',':
  3753. return token_type::value_separator;
  3754. // literals
  3755. case 't':
  3756. return scan_literal("true", 4, token_type::literal_true);
  3757. case 'f':
  3758. return scan_literal("false", 5, token_type::literal_false);
  3759. case 'n':
  3760. return scan_literal("null", 4, token_type::literal_null);
  3761. // string
  3762. case '\"':
  3763. return scan_string();
  3764. // number
  3765. case '-':
  3766. case '0':
  3767. case '1':
  3768. case '2':
  3769. case '3':
  3770. case '4':
  3771. case '5':
  3772. case '6':
  3773. case '7':
  3774. case '8':
  3775. case '9':
  3776. return scan_number();
  3777. // end of input (the null byte is needed when parsing from
  3778. // string literals)
  3779. case '\0':
  3780. case std::char_traits<char>::eof():
  3781. return token_type::end_of_input;
  3782. // error
  3783. default:
  3784. error_message = "invalid literal";
  3785. return token_type::parse_error;
  3786. }
  3787. }
  3788. private:
  3789. /// input adapter
  3790. detail::input_adapter_t ia = nullptr;
  3791. /// the current character
  3792. std::char_traits<char>::int_type current = std::char_traits<char>::eof();
  3793. /// whether the next get() call should just return current
  3794. bool next_unget = false;
  3795. /// the start position of the current token
  3796. position_t position;
  3797. /// raw input token string (for error messages)
  3798. std::vector<char> token_string {};
  3799. /// buffer for variable-length tokens (numbers, strings)
  3800. string_t token_buffer {};
  3801. /// a description of occurred lexer errors
  3802. const char* error_message = "";
  3803. // number values
  3804. number_integer_t value_integer = 0;
  3805. number_unsigned_t value_unsigned = 0;
  3806. number_float_t value_float = 0;
  3807. /// the decimal point
  3808. const char decimal_point_char = '.';
  3809. };
  3810. } // namespace detail
  3811. } // namespace nlohmann
  3812. // #include <nlohmann/detail/input/parser.hpp>
  3813. #include <cassert> // assert
  3814. #include <cmath> // isfinite
  3815. #include <cstdint> // uint8_t
  3816. #include <functional> // function
  3817. #include <string> // string
  3818. #include <utility> // move
  3819. // #include <nlohmann/detail/exceptions.hpp>
  3820. // #include <nlohmann/detail/macro_scope.hpp>
  3821. // #include <nlohmann/detail/meta/is_sax.hpp>
  3822. #include <cstdint> // size_t
  3823. #include <utility> // declval
  3824. // #include <nlohmann/detail/meta/detected.hpp>
  3825. // #include <nlohmann/detail/meta/type_traits.hpp>
  3826. namespace nlohmann
  3827. {
  3828. namespace detail
  3829. {
  3830. template <typename T>
  3831. using null_function_t = decltype(std::declval<T&>().null());
  3832. template <typename T>
  3833. using boolean_function_t =
  3834. decltype(std::declval<T&>().boolean(std::declval<bool>()));
  3835. template <typename T, typename Integer>
  3836. using number_integer_function_t =
  3837. decltype(std::declval<T&>().number_integer(std::declval<Integer>()));
  3838. template <typename T, typename Unsigned>
  3839. using number_unsigned_function_t =
  3840. decltype(std::declval<T&>().number_unsigned(std::declval<Unsigned>()));
  3841. template <typename T, typename Float, typename String>
  3842. using number_float_function_t = decltype(std::declval<T&>().number_float(
  3843. std::declval<Float>(), std::declval<const String&>()));
  3844. template <typename T, typename String>
  3845. using string_function_t =
  3846. decltype(std::declval<T&>().string(std::declval<String&>()));
  3847. template <typename T>
  3848. using start_object_function_t =
  3849. decltype(std::declval<T&>().start_object(std::declval<std::size_t>()));
  3850. template <typename T, typename String>
  3851. using key_function_t =
  3852. decltype(std::declval<T&>().key(std::declval<String&>()));
  3853. template <typename T>
  3854. using end_object_function_t = decltype(std::declval<T&>().end_object());
  3855. template <typename T>
  3856. using start_array_function_t =
  3857. decltype(std::declval<T&>().start_array(std::declval<std::size_t>()));
  3858. template <typename T>
  3859. using end_array_function_t = decltype(std::declval<T&>().end_array());
  3860. template <typename T, typename Exception>
  3861. using parse_error_function_t = decltype(std::declval<T&>().parse_error(
  3862. std::declval<std::size_t>(), std::declval<const std::string&>(),
  3863. std::declval<const Exception&>()));
  3864. template <typename SAX, typename BasicJsonType>
  3865. struct is_sax
  3866. {
  3867. private:
  3868. static_assert(is_basic_json<BasicJsonType>::value,
  3869. "BasicJsonType must be of type basic_json<...>");
  3870. using number_integer_t = typename BasicJsonType::number_integer_t;
  3871. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  3872. using number_float_t = typename BasicJsonType::number_float_t;
  3873. using string_t = typename BasicJsonType::string_t;
  3874. using exception_t = typename BasicJsonType::exception;
  3875. public:
  3876. static constexpr bool value =
  3877. is_detected_exact<bool, null_function_t, SAX>::value &&
  3878. is_detected_exact<bool, boolean_function_t, SAX>::value &&
  3879. is_detected_exact<bool, number_integer_function_t, SAX,
  3880. number_integer_t>::value &&
  3881. is_detected_exact<bool, number_unsigned_function_t, SAX,
  3882. number_unsigned_t>::value &&
  3883. is_detected_exact<bool, number_float_function_t, SAX, number_float_t,
  3884. string_t>::value &&
  3885. is_detected_exact<bool, string_function_t, SAX, string_t>::value &&
  3886. is_detected_exact<bool, start_object_function_t, SAX>::value &&
  3887. is_detected_exact<bool, key_function_t, SAX, string_t>::value &&
  3888. is_detected_exact<bool, end_object_function_t, SAX>::value &&
  3889. is_detected_exact<bool, start_array_function_t, SAX>::value &&
  3890. is_detected_exact<bool, end_array_function_t, SAX>::value &&
  3891. is_detected_exact<bool, parse_error_function_t, SAX, exception_t>::value;
  3892. };
  3893. template <typename SAX, typename BasicJsonType>
  3894. struct is_sax_static_asserts
  3895. {
  3896. private:
  3897. static_assert(is_basic_json<BasicJsonType>::value,
  3898. "BasicJsonType must be of type basic_json<...>");
  3899. using number_integer_t = typename BasicJsonType::number_integer_t;
  3900. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  3901. using number_float_t = typename BasicJsonType::number_float_t;
  3902. using string_t = typename BasicJsonType::string_t;
  3903. using exception_t = typename BasicJsonType::exception;
  3904. public:
  3905. static_assert(is_detected_exact<bool, null_function_t, SAX>::value,
  3906. "Missing/invalid function: bool null()");
  3907. static_assert(is_detected_exact<bool, boolean_function_t, SAX>::value,
  3908. "Missing/invalid function: bool boolean(bool)");
  3909. static_assert(is_detected_exact<bool, boolean_function_t, SAX>::value,
  3910. "Missing/invalid function: bool boolean(bool)");
  3911. static_assert(
  3912. is_detected_exact<bool, number_integer_function_t, SAX,
  3913. number_integer_t>::value,
  3914. "Missing/invalid function: bool number_integer(number_integer_t)");
  3915. static_assert(
  3916. is_detected_exact<bool, number_unsigned_function_t, SAX,
  3917. number_unsigned_t>::value,
  3918. "Missing/invalid function: bool number_unsigned(number_unsigned_t)");
  3919. static_assert(is_detected_exact<bool, number_float_function_t, SAX,
  3920. number_float_t, string_t>::value,
  3921. "Missing/invalid function: bool number_float(number_float_t, const string_t&)");
  3922. static_assert(
  3923. is_detected_exact<bool, string_function_t, SAX, string_t>::value,
  3924. "Missing/invalid function: bool string(string_t&)");
  3925. static_assert(is_detected_exact<bool, start_object_function_t, SAX>::value,
  3926. "Missing/invalid function: bool start_object(std::size_t)");
  3927. static_assert(is_detected_exact<bool, key_function_t, SAX, string_t>::value,
  3928. "Missing/invalid function: bool key(string_t&)");
  3929. static_assert(is_detected_exact<bool, end_object_function_t, SAX>::value,
  3930. "Missing/invalid function: bool end_object()");
  3931. static_assert(is_detected_exact<bool, start_array_function_t, SAX>::value,
  3932. "Missing/invalid function: bool start_array(std::size_t)");
  3933. static_assert(is_detected_exact<bool, end_array_function_t, SAX>::value,
  3934. "Missing/invalid function: bool end_array()");
  3935. static_assert(
  3936. is_detected_exact<bool, parse_error_function_t, SAX, exception_t>::value,
  3937. "Missing/invalid function: bool parse_error(std::size_t, const "
  3938. "std::string&, const exception&)");
  3939. };
  3940. } // namespace detail
  3941. } // namespace nlohmann
  3942. // #include <nlohmann/detail/input/input_adapters.hpp>
  3943. // #include <nlohmann/detail/input/json_sax.hpp>
  3944. #include <cstddef>
  3945. #include <string>
  3946. #include <vector>
  3947. // #include <nlohmann/detail/input/parser.hpp>
  3948. // #include <nlohmann/detail/exceptions.hpp>
  3949. namespace nlohmann
  3950. {
  3951. /*!
  3952. @brief SAX interface
  3953. This class describes the SAX interface used by @ref nlohmann::json::sax_parse.
  3954. Each function is called in different situations while the input is parsed. The
  3955. boolean return value informs the parser whether to continue processing the
  3956. input.
  3957. */
  3958. template<typename BasicJsonType>
  3959. struct json_sax
  3960. {
  3961. /// type for (signed) integers
  3962. using number_integer_t = typename BasicJsonType::number_integer_t;
  3963. /// type for unsigned integers
  3964. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  3965. /// type for floating-point numbers
  3966. using number_float_t = typename BasicJsonType::number_float_t;
  3967. /// type for strings
  3968. using string_t = typename BasicJsonType::string_t;
  3969. /*!
  3970. @brief a null value was read
  3971. @return whether parsing should proceed
  3972. */
  3973. virtual bool null() = 0;
  3974. /*!
  3975. @brief a boolean value was read
  3976. @param[in] val boolean value
  3977. @return whether parsing should proceed
  3978. */
  3979. virtual bool boolean(bool val) = 0;
  3980. /*!
  3981. @brief an integer number was read
  3982. @param[in] val integer value
  3983. @return whether parsing should proceed
  3984. */
  3985. virtual bool number_integer(number_integer_t val) = 0;
  3986. /*!
  3987. @brief an unsigned integer number was read
  3988. @param[in] val unsigned integer value
  3989. @return whether parsing should proceed
  3990. */
  3991. virtual bool number_unsigned(number_unsigned_t val) = 0;
  3992. /*!
  3993. @brief an floating-point number was read
  3994. @param[in] val floating-point value
  3995. @param[in] s raw token value
  3996. @return whether parsing should proceed
  3997. */
  3998. virtual bool number_float(number_float_t val, const string_t& s) = 0;
  3999. /*!
  4000. @brief a string was read
  4001. @param[in] val string value
  4002. @return whether parsing should proceed
  4003. @note It is safe to move the passed string.
  4004. */
  4005. virtual bool string(string_t& val) = 0;
  4006. /*!
  4007. @brief the beginning of an object was read
  4008. @param[in] elements number of object elements or -1 if unknown
  4009. @return whether parsing should proceed
  4010. @note binary formats may report the number of elements
  4011. */
  4012. virtual bool start_object(std::size_t elements) = 0;
  4013. /*!
  4014. @brief an object key was read
  4015. @param[in] val object key
  4016. @return whether parsing should proceed
  4017. @note It is safe to move the passed string.
  4018. */
  4019. virtual bool key(string_t& val) = 0;
  4020. /*!
  4021. @brief the end of an object was read
  4022. @return whether parsing should proceed
  4023. */
  4024. virtual bool end_object() = 0;
  4025. /*!
  4026. @brief the beginning of an array was read
  4027. @param[in] elements number of array elements or -1 if unknown
  4028. @return whether parsing should proceed
  4029. @note binary formats may report the number of elements
  4030. */
  4031. virtual bool start_array(std::size_t elements) = 0;
  4032. /*!
  4033. @brief the end of an array was read
  4034. @return whether parsing should proceed
  4035. */
  4036. virtual bool end_array() = 0;
  4037. /*!
  4038. @brief a parse error occurred
  4039. @param[in] position the position in the input where the error occurs
  4040. @param[in] last_token the last read token
  4041. @param[in] ex an exception object describing the error
  4042. @return whether parsing should proceed (must return false)
  4043. */
  4044. virtual bool parse_error(std::size_t position,
  4045. const std::string& last_token,
  4046. const detail::exception& ex) = 0;
  4047. virtual ~json_sax() = default;
  4048. };
  4049. namespace detail
  4050. {
  4051. /*!
  4052. @brief SAX implementation to create a JSON value from SAX events
  4053. This class implements the @ref json_sax interface and processes the SAX events
  4054. to create a JSON value which makes it basically a DOM parser. The structure or
  4055. hierarchy of the JSON value is managed by the stack `ref_stack` which contains
  4056. a pointer to the respective array or object for each recursion depth.
  4057. After successful parsing, the value that is passed by reference to the
  4058. constructor contains the parsed value.
  4059. @tparam BasicJsonType the JSON type
  4060. */
  4061. template<typename BasicJsonType>
  4062. class json_sax_dom_parser
  4063. {
  4064. public:
  4065. using number_integer_t = typename BasicJsonType::number_integer_t;
  4066. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  4067. using number_float_t = typename BasicJsonType::number_float_t;
  4068. using string_t = typename BasicJsonType::string_t;
  4069. /*!
  4070. @param[in, out] r reference to a JSON value that is manipulated while
  4071. parsing
  4072. @param[in] allow_exceptions_ whether parse errors yield exceptions
  4073. */
  4074. explicit json_sax_dom_parser(BasicJsonType& r, const bool allow_exceptions_ = true)
  4075. : root(r), allow_exceptions(allow_exceptions_)
  4076. {}
  4077. bool null()
  4078. {
  4079. handle_value(nullptr);
  4080. return true;
  4081. }
  4082. bool boolean(bool val)
  4083. {
  4084. handle_value(val);
  4085. return true;
  4086. }
  4087. bool number_integer(number_integer_t val)
  4088. {
  4089. handle_value(val);
  4090. return true;
  4091. }
  4092. bool number_unsigned(number_unsigned_t val)
  4093. {
  4094. handle_value(val);
  4095. return true;
  4096. }
  4097. bool number_float(number_float_t val, const string_t& /*unused*/)
  4098. {
  4099. handle_value(val);
  4100. return true;
  4101. }
  4102. bool string(string_t& val)
  4103. {
  4104. handle_value(val);
  4105. return true;
  4106. }
  4107. bool start_object(std::size_t len)
  4108. {
  4109. ref_stack.push_back(handle_value(BasicJsonType::value_t::object));
  4110. if (JSON_UNLIKELY(len != std::size_t(-1) and len > ref_stack.back()->max_size()))
  4111. {
  4112. JSON_THROW(out_of_range::create(408,
  4113. "excessive object size: " + std::to_string(len)));
  4114. }
  4115. return true;
  4116. }
  4117. bool key(string_t& val)
  4118. {
  4119. // add null at given key and store the reference for later
  4120. object_element = &(ref_stack.back()->m_value.object->operator[](val));
  4121. return true;
  4122. }
  4123. bool end_object()
  4124. {
  4125. ref_stack.pop_back();
  4126. return true;
  4127. }
  4128. bool start_array(std::size_t len)
  4129. {
  4130. ref_stack.push_back(handle_value(BasicJsonType::value_t::array));
  4131. if (JSON_UNLIKELY(len != std::size_t(-1) and len > ref_stack.back()->max_size()))
  4132. {
  4133. JSON_THROW(out_of_range::create(408,
  4134. "excessive array size: " + std::to_string(len)));
  4135. }
  4136. return true;
  4137. }
  4138. bool end_array()
  4139. {
  4140. ref_stack.pop_back();
  4141. return true;
  4142. }
  4143. bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
  4144. const detail::exception& ex)
  4145. {
  4146. errored = true;
  4147. if (allow_exceptions)
  4148. {
  4149. // determine the proper exception type from the id
  4150. switch ((ex.id / 100) % 100)
  4151. {
  4152. case 1:
  4153. JSON_THROW(*reinterpret_cast<const detail::parse_error*>(&ex));
  4154. case 4:
  4155. JSON_THROW(*reinterpret_cast<const detail::out_of_range*>(&ex));
  4156. // LCOV_EXCL_START
  4157. case 2:
  4158. JSON_THROW(*reinterpret_cast<const detail::invalid_iterator*>(&ex));
  4159. case 3:
  4160. JSON_THROW(*reinterpret_cast<const detail::type_error*>(&ex));
  4161. case 5:
  4162. JSON_THROW(*reinterpret_cast<const detail::other_error*>(&ex));
  4163. default:
  4164. assert(false);
  4165. // LCOV_EXCL_STOP
  4166. }
  4167. }
  4168. return false;
  4169. }
  4170. constexpr bool is_errored() const
  4171. {
  4172. return errored;
  4173. }
  4174. private:
  4175. /*!
  4176. @invariant If the ref stack is empty, then the passed value will be the new
  4177. root.
  4178. @invariant If the ref stack contains a value, then it is an array or an
  4179. object to which we can add elements
  4180. */
  4181. template<typename Value>
  4182. BasicJsonType* handle_value(Value&& v)
  4183. {
  4184. if (ref_stack.empty())
  4185. {
  4186. root = BasicJsonType(std::forward<Value>(v));
  4187. return &root;
  4188. }
  4189. assert(ref_stack.back()->is_array() or ref_stack.back()->is_object());
  4190. if (ref_stack.back()->is_array())
  4191. {
  4192. ref_stack.back()->m_value.array->emplace_back(std::forward<Value>(v));
  4193. return &(ref_stack.back()->m_value.array->back());
  4194. }
  4195. else
  4196. {
  4197. assert(object_element);
  4198. *object_element = BasicJsonType(std::forward<Value>(v));
  4199. return object_element;
  4200. }
  4201. }
  4202. /// the parsed JSON value
  4203. BasicJsonType& root;
  4204. /// stack to model hierarchy of values
  4205. std::vector<BasicJsonType*> ref_stack;
  4206. /// helper to hold the reference for the next object element
  4207. BasicJsonType* object_element = nullptr;
  4208. /// whether a syntax error occurred
  4209. bool errored = false;
  4210. /// whether to throw exceptions in case of errors
  4211. const bool allow_exceptions = true;
  4212. };
  4213. template<typename BasicJsonType>
  4214. class json_sax_dom_callback_parser
  4215. {
  4216. public:
  4217. using number_integer_t = typename BasicJsonType::number_integer_t;
  4218. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  4219. using number_float_t = typename BasicJsonType::number_float_t;
  4220. using string_t = typename BasicJsonType::string_t;
  4221. using parser_callback_t = typename BasicJsonType::parser_callback_t;
  4222. using parse_event_t = typename BasicJsonType::parse_event_t;
  4223. json_sax_dom_callback_parser(BasicJsonType& r,
  4224. const parser_callback_t cb,
  4225. const bool allow_exceptions_ = true)
  4226. : root(r), callback(cb), allow_exceptions(allow_exceptions_)
  4227. {
  4228. keep_stack.push_back(true);
  4229. }
  4230. bool null()
  4231. {
  4232. handle_value(nullptr);
  4233. return true;
  4234. }
  4235. bool boolean(bool val)
  4236. {
  4237. handle_value(val);
  4238. return true;
  4239. }
  4240. bool number_integer(number_integer_t val)
  4241. {
  4242. handle_value(val);
  4243. return true;
  4244. }
  4245. bool number_unsigned(number_unsigned_t val)
  4246. {
  4247. handle_value(val);
  4248. return true;
  4249. }
  4250. bool number_float(number_float_t val, const string_t& /*unused*/)
  4251. {
  4252. handle_value(val);
  4253. return true;
  4254. }
  4255. bool string(string_t& val)
  4256. {
  4257. handle_value(val);
  4258. return true;
  4259. }
  4260. bool start_object(std::size_t len)
  4261. {
  4262. // check callback for object start
  4263. const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::object_start, discarded);
  4264. keep_stack.push_back(keep);
  4265. auto val = handle_value(BasicJsonType::value_t::object, true);
  4266. ref_stack.push_back(val.second);
  4267. // check object limit
  4268. if (ref_stack.back())
  4269. {
  4270. if (JSON_UNLIKELY(len != std::size_t(-1) and len > ref_stack.back()->max_size()))
  4271. {
  4272. JSON_THROW(out_of_range::create(408,
  4273. "excessive object size: " + std::to_string(len)));
  4274. }
  4275. }
  4276. return true;
  4277. }
  4278. bool key(string_t& val)
  4279. {
  4280. BasicJsonType k = BasicJsonType(val);
  4281. // check callback for key
  4282. const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::key, k);
  4283. key_keep_stack.push_back(keep);
  4284. // add discarded value at given key and store the reference for later
  4285. if (keep and ref_stack.back())
  4286. {
  4287. object_element = &(ref_stack.back()->m_value.object->operator[](val) = discarded);
  4288. }
  4289. return true;
  4290. }
  4291. bool end_object()
  4292. {
  4293. if (ref_stack.back())
  4294. {
  4295. if (not callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back()))
  4296. {
  4297. // discard object
  4298. *ref_stack.back() = discarded;
  4299. }
  4300. }
  4301. assert(not ref_stack.empty());
  4302. assert(not keep_stack.empty());
  4303. ref_stack.pop_back();
  4304. keep_stack.pop_back();
  4305. if (not ref_stack.empty() and ref_stack.back())
  4306. {
  4307. // remove discarded value
  4308. if (ref_stack.back()->is_object())
  4309. {
  4310. for (auto it = ref_stack.back()->begin(); it != ref_stack.back()->end(); ++it)
  4311. {
  4312. if (it->is_discarded())
  4313. {
  4314. ref_stack.back()->erase(it);
  4315. break;
  4316. }
  4317. }
  4318. }
  4319. }
  4320. return true;
  4321. }
  4322. bool start_array(std::size_t len)
  4323. {
  4324. const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::array_start, discarded);
  4325. keep_stack.push_back(keep);
  4326. auto val = handle_value(BasicJsonType::value_t::array, true);
  4327. ref_stack.push_back(val.second);
  4328. // check array limit
  4329. if (ref_stack.back())
  4330. {
  4331. if (JSON_UNLIKELY(len != std::size_t(-1) and len > ref_stack.back()->max_size()))
  4332. {
  4333. JSON_THROW(out_of_range::create(408,
  4334. "excessive array size: " + std::to_string(len)));
  4335. }
  4336. }
  4337. return true;
  4338. }
  4339. bool end_array()
  4340. {
  4341. bool keep = true;
  4342. if (ref_stack.back())
  4343. {
  4344. keep = callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::array_end, *ref_stack.back());
  4345. if (not keep)
  4346. {
  4347. // discard array
  4348. *ref_stack.back() = discarded;
  4349. }
  4350. }
  4351. assert(not ref_stack.empty());
  4352. assert(not keep_stack.empty());
  4353. ref_stack.pop_back();
  4354. keep_stack.pop_back();
  4355. // remove discarded value
  4356. if (not keep and not ref_stack.empty())
  4357. {
  4358. if (ref_stack.back()->is_array())
  4359. {
  4360. ref_stack.back()->m_value.array->pop_back();
  4361. }
  4362. }
  4363. return true;
  4364. }
  4365. bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
  4366. const detail::exception& ex)
  4367. {
  4368. errored = true;
  4369. if (allow_exceptions)
  4370. {
  4371. // determine the proper exception type from the id
  4372. switch ((ex.id / 100) % 100)
  4373. {
  4374. case 1:
  4375. JSON_THROW(*reinterpret_cast<const detail::parse_error*>(&ex));
  4376. case 4:
  4377. JSON_THROW(*reinterpret_cast<const detail::out_of_range*>(&ex));
  4378. // LCOV_EXCL_START
  4379. case 2:
  4380. JSON_THROW(*reinterpret_cast<const detail::invalid_iterator*>(&ex));
  4381. case 3:
  4382. JSON_THROW(*reinterpret_cast<const detail::type_error*>(&ex));
  4383. case 5:
  4384. JSON_THROW(*reinterpret_cast<const detail::other_error*>(&ex));
  4385. default:
  4386. assert(false);
  4387. // LCOV_EXCL_STOP
  4388. }
  4389. }
  4390. return false;
  4391. }
  4392. constexpr bool is_errored() const
  4393. {
  4394. return errored;
  4395. }
  4396. private:
  4397. /*!
  4398. @param[in] v value to add to the JSON value we build during parsing
  4399. @param[in] skip_callback whether we should skip calling the callback
  4400. function; this is required after start_array() and
  4401. start_object() SAX events, because otherwise we would call the
  4402. callback function with an empty array or object, respectively.
  4403. @invariant If the ref stack is empty, then the passed value will be the new
  4404. root.
  4405. @invariant If the ref stack contains a value, then it is an array or an
  4406. object to which we can add elements
  4407. @return pair of boolean (whether value should be kept) and pointer (to the
  4408. passed value in the ref_stack hierarchy; nullptr if not kept)
  4409. */
  4410. template<typename Value>
  4411. std::pair<bool, BasicJsonType*> handle_value(Value&& v, const bool skip_callback = false)
  4412. {
  4413. assert(not keep_stack.empty());
  4414. // do not handle this value if we know it would be added to a discarded
  4415. // container
  4416. if (not keep_stack.back())
  4417. {
  4418. return {false, nullptr};
  4419. }
  4420. // create value
  4421. auto value = BasicJsonType(std::forward<Value>(v));
  4422. // check callback
  4423. const bool keep = skip_callback or callback(static_cast<int>(ref_stack.size()), parse_event_t::value, value);
  4424. // do not handle this value if we just learnt it shall be discarded
  4425. if (not keep)
  4426. {
  4427. return {false, nullptr};
  4428. }
  4429. if (ref_stack.empty())
  4430. {
  4431. root = std::move(value);
  4432. return {true, &root};
  4433. }
  4434. // skip this value if we already decided to skip the parent
  4435. // (https://github.com/nlohmann/json/issues/971#issuecomment-413678360)
  4436. if (not ref_stack.back())
  4437. {
  4438. return {false, nullptr};
  4439. }
  4440. // we now only expect arrays and objects
  4441. assert(ref_stack.back()->is_array() or ref_stack.back()->is_object());
  4442. if (ref_stack.back()->is_array())
  4443. {
  4444. ref_stack.back()->m_value.array->push_back(std::move(value));
  4445. return {true, &(ref_stack.back()->m_value.array->back())};
  4446. }
  4447. else
  4448. {
  4449. // check if we should store an element for the current key
  4450. assert(not key_keep_stack.empty());
  4451. const bool store_element = key_keep_stack.back();
  4452. key_keep_stack.pop_back();
  4453. if (not store_element)
  4454. {
  4455. return {false, nullptr};
  4456. }
  4457. assert(object_element);
  4458. *object_element = std::move(value);
  4459. return {true, object_element};
  4460. }
  4461. }
  4462. /// the parsed JSON value
  4463. BasicJsonType& root;
  4464. /// stack to model hierarchy of values
  4465. std::vector<BasicJsonType*> ref_stack;
  4466. /// stack to manage which values to keep
  4467. std::vector<bool> keep_stack;
  4468. /// stack to manage which object keys to keep
  4469. std::vector<bool> key_keep_stack;
  4470. /// helper to hold the reference for the next object element
  4471. BasicJsonType* object_element = nullptr;
  4472. /// whether a syntax error occurred
  4473. bool errored = false;
  4474. /// callback function
  4475. const parser_callback_t callback = nullptr;
  4476. /// whether to throw exceptions in case of errors
  4477. const bool allow_exceptions = true;
  4478. /// a discarded value for the callback
  4479. BasicJsonType discarded = BasicJsonType::value_t::discarded;
  4480. };
  4481. template<typename BasicJsonType>
  4482. class json_sax_acceptor
  4483. {
  4484. public:
  4485. using number_integer_t = typename BasicJsonType::number_integer_t;
  4486. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  4487. using number_float_t = typename BasicJsonType::number_float_t;
  4488. using string_t = typename BasicJsonType::string_t;
  4489. bool null()
  4490. {
  4491. return true;
  4492. }
  4493. bool boolean(bool /*unused*/)
  4494. {
  4495. return true;
  4496. }
  4497. bool number_integer(number_integer_t /*unused*/)
  4498. {
  4499. return true;
  4500. }
  4501. bool number_unsigned(number_unsigned_t /*unused*/)
  4502. {
  4503. return true;
  4504. }
  4505. bool number_float(number_float_t /*unused*/, const string_t& /*unused*/)
  4506. {
  4507. return true;
  4508. }
  4509. bool string(string_t& /*unused*/)
  4510. {
  4511. return true;
  4512. }
  4513. bool start_object(std::size_t /*unused*/ = std::size_t(-1))
  4514. {
  4515. return true;
  4516. }
  4517. bool key(string_t& /*unused*/)
  4518. {
  4519. return true;
  4520. }
  4521. bool end_object()
  4522. {
  4523. return true;
  4524. }
  4525. bool start_array(std::size_t /*unused*/ = std::size_t(-1))
  4526. {
  4527. return true;
  4528. }
  4529. bool end_array()
  4530. {
  4531. return true;
  4532. }
  4533. bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const detail::exception& /*unused*/)
  4534. {
  4535. return false;
  4536. }
  4537. };
  4538. } // namespace detail
  4539. } // namespace nlohmann
  4540. // #include <nlohmann/detail/input/lexer.hpp>
  4541. // #include <nlohmann/detail/value_t.hpp>
  4542. namespace nlohmann
  4543. {
  4544. namespace detail
  4545. {
  4546. ////////////
  4547. // parser //
  4548. ////////////
  4549. /*!
  4550. @brief syntax analysis
  4551. This class implements a recursive decent parser.
  4552. */
  4553. template<typename BasicJsonType>
  4554. class parser
  4555. {
  4556. using number_integer_t = typename BasicJsonType::number_integer_t;
  4557. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  4558. using number_float_t = typename BasicJsonType::number_float_t;
  4559. using string_t = typename BasicJsonType::string_t;
  4560. using lexer_t = lexer<BasicJsonType>;
  4561. using token_type = typename lexer_t::token_type;
  4562. public:
  4563. enum class parse_event_t : uint8_t
  4564. {
  4565. /// the parser read `{` and started to process a JSON object
  4566. object_start,
  4567. /// the parser read `}` and finished processing a JSON object
  4568. object_end,
  4569. /// the parser read `[` and started to process a JSON array
  4570. array_start,
  4571. /// the parser read `]` and finished processing a JSON array
  4572. array_end,
  4573. /// the parser read a key of a value in an object
  4574. key,
  4575. /// the parser finished reading a JSON value
  4576. value
  4577. };
  4578. using parser_callback_t =
  4579. std::function<bool(int depth, parse_event_t event, BasicJsonType& parsed)>;
  4580. /// a parser reading from an input adapter
  4581. explicit parser(detail::input_adapter_t&& adapter,
  4582. const parser_callback_t cb = nullptr,
  4583. const bool allow_exceptions_ = true)
  4584. : callback(cb), m_lexer(std::move(adapter)), allow_exceptions(allow_exceptions_)
  4585. {
  4586. // read first token
  4587. get_token();
  4588. }
  4589. /*!
  4590. @brief public parser interface
  4591. @param[in] strict whether to expect the last token to be EOF
  4592. @param[in,out] result parsed JSON value
  4593. @throw parse_error.101 in case of an unexpected token
  4594. @throw parse_error.102 if to_unicode fails or surrogate error
  4595. @throw parse_error.103 if to_unicode fails
  4596. */
  4597. void parse(const bool strict, BasicJsonType& result)
  4598. {
  4599. if (callback)
  4600. {
  4601. json_sax_dom_callback_parser<BasicJsonType> sdp(result, callback, allow_exceptions);
  4602. sax_parse_internal(&sdp);
  4603. result.assert_invariant();
  4604. // in strict mode, input must be completely read
  4605. if (strict and (get_token() != token_type::end_of_input))
  4606. {
  4607. sdp.parse_error(m_lexer.get_position(),
  4608. m_lexer.get_token_string(),
  4609. parse_error::create(101, m_lexer.get_position(),
  4610. exception_message(token_type::end_of_input, "value")));
  4611. }
  4612. // in case of an error, return discarded value
  4613. if (sdp.is_errored())
  4614. {
  4615. result = value_t::discarded;
  4616. return;
  4617. }
  4618. // set top-level value to null if it was discarded by the callback
  4619. // function
  4620. if (result.is_discarded())
  4621. {
  4622. result = nullptr;
  4623. }
  4624. }
  4625. else
  4626. {
  4627. json_sax_dom_parser<BasicJsonType> sdp(result, allow_exceptions);
  4628. sax_parse_internal(&sdp);
  4629. result.assert_invariant();
  4630. // in strict mode, input must be completely read
  4631. if (strict and (get_token() != token_type::end_of_input))
  4632. {
  4633. sdp.parse_error(m_lexer.get_position(),
  4634. m_lexer.get_token_string(),
  4635. parse_error::create(101, m_lexer.get_position(),
  4636. exception_message(token_type::end_of_input, "value")));
  4637. }
  4638. // in case of an error, return discarded value
  4639. if (sdp.is_errored())
  4640. {
  4641. result = value_t::discarded;
  4642. return;
  4643. }
  4644. }
  4645. }
  4646. /*!
  4647. @brief public accept interface
  4648. @param[in] strict whether to expect the last token to be EOF
  4649. @return whether the input is a proper JSON text
  4650. */
  4651. bool accept(const bool strict = true)
  4652. {
  4653. json_sax_acceptor<BasicJsonType> sax_acceptor;
  4654. return sax_parse(&sax_acceptor, strict);
  4655. }
  4656. template <typename SAX>
  4657. bool sax_parse(SAX* sax, const bool strict = true)
  4658. {
  4659. (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
  4660. const bool result = sax_parse_internal(sax);
  4661. // strict mode: next byte must be EOF
  4662. if (result and strict and (get_token() != token_type::end_of_input))
  4663. {
  4664. return sax->parse_error(m_lexer.get_position(),
  4665. m_lexer.get_token_string(),
  4666. parse_error::create(101, m_lexer.get_position(),
  4667. exception_message(token_type::end_of_input, "value")));
  4668. }
  4669. return result;
  4670. }
  4671. private:
  4672. template <typename SAX>
  4673. bool sax_parse_internal(SAX* sax)
  4674. {
  4675. // stack to remember the hierarchy of structured values we are parsing
  4676. // true = array; false = object
  4677. std::vector<bool> states;
  4678. // value to avoid a goto (see comment where set to true)
  4679. bool skip_to_state_evaluation = false;
  4680. while (true)
  4681. {
  4682. if (not skip_to_state_evaluation)
  4683. {
  4684. // invariant: get_token() was called before each iteration
  4685. switch (last_token)
  4686. {
  4687. case token_type::begin_object:
  4688. {
  4689. if (JSON_UNLIKELY(not sax->start_object(std::size_t(-1))))
  4690. {
  4691. return false;
  4692. }
  4693. // closing } -> we are done
  4694. if (get_token() == token_type::end_object)
  4695. {
  4696. if (JSON_UNLIKELY(not sax->end_object()))
  4697. {
  4698. return false;
  4699. }
  4700. break;
  4701. }
  4702. // parse key
  4703. if (JSON_UNLIKELY(last_token != token_type::value_string))
  4704. {
  4705. return sax->parse_error(m_lexer.get_position(),
  4706. m_lexer.get_token_string(),
  4707. parse_error::create(101, m_lexer.get_position(),
  4708. exception_message(token_type::value_string, "object key")));
  4709. }
  4710. if (JSON_UNLIKELY(not sax->key(m_lexer.get_string())))
  4711. {
  4712. return false;
  4713. }
  4714. // parse separator (:)
  4715. if (JSON_UNLIKELY(get_token() != token_type::name_separator))
  4716. {
  4717. return sax->parse_error(m_lexer.get_position(),
  4718. m_lexer.get_token_string(),
  4719. parse_error::create(101, m_lexer.get_position(),
  4720. exception_message(token_type::name_separator, "object separator")));
  4721. }
  4722. // remember we are now inside an object
  4723. states.push_back(false);
  4724. // parse values
  4725. get_token();
  4726. continue;
  4727. }
  4728. case token_type::begin_array:
  4729. {
  4730. if (JSON_UNLIKELY(not sax->start_array(std::size_t(-1))))
  4731. {
  4732. return false;
  4733. }
  4734. // closing ] -> we are done
  4735. if (get_token() == token_type::end_array)
  4736. {
  4737. if (JSON_UNLIKELY(not sax->end_array()))
  4738. {
  4739. return false;
  4740. }
  4741. break;
  4742. }
  4743. // remember we are now inside an array
  4744. states.push_back(true);
  4745. // parse values (no need to call get_token)
  4746. continue;
  4747. }
  4748. case token_type::value_float:
  4749. {
  4750. const auto res = m_lexer.get_number_float();
  4751. if (JSON_UNLIKELY(not std::isfinite(res)))
  4752. {
  4753. return sax->parse_error(m_lexer.get_position(),
  4754. m_lexer.get_token_string(),
  4755. out_of_range::create(406, "number overflow parsing '" + m_lexer.get_token_string() + "'"));
  4756. }
  4757. else
  4758. {
  4759. if (JSON_UNLIKELY(not sax->number_float(res, m_lexer.get_string())))
  4760. {
  4761. return false;
  4762. }
  4763. break;
  4764. }
  4765. }
  4766. case token_type::literal_false:
  4767. {
  4768. if (JSON_UNLIKELY(not sax->boolean(false)))
  4769. {
  4770. return false;
  4771. }
  4772. break;
  4773. }
  4774. case token_type::literal_null:
  4775. {
  4776. if (JSON_UNLIKELY(not sax->null()))
  4777. {
  4778. return false;
  4779. }
  4780. break;
  4781. }
  4782. case token_type::literal_true:
  4783. {
  4784. if (JSON_UNLIKELY(not sax->boolean(true)))
  4785. {
  4786. return false;
  4787. }
  4788. break;
  4789. }
  4790. case token_type::value_integer:
  4791. {
  4792. if (JSON_UNLIKELY(not sax->number_integer(m_lexer.get_number_integer())))
  4793. {
  4794. return false;
  4795. }
  4796. break;
  4797. }
  4798. case token_type::value_string:
  4799. {
  4800. if (JSON_UNLIKELY(not sax->string(m_lexer.get_string())))
  4801. {
  4802. return false;
  4803. }
  4804. break;
  4805. }
  4806. case token_type::value_unsigned:
  4807. {
  4808. if (JSON_UNLIKELY(not sax->number_unsigned(m_lexer.get_number_unsigned())))
  4809. {
  4810. return false;
  4811. }
  4812. break;
  4813. }
  4814. case token_type::parse_error:
  4815. {
  4816. // using "uninitialized" to avoid "expected" message
  4817. return sax->parse_error(m_lexer.get_position(),
  4818. m_lexer.get_token_string(),
  4819. parse_error::create(101, m_lexer.get_position(),
  4820. exception_message(token_type::uninitialized, "value")));
  4821. }
  4822. default: // the last token was unexpected
  4823. {
  4824. return sax->parse_error(m_lexer.get_position(),
  4825. m_lexer.get_token_string(),
  4826. parse_error::create(101, m_lexer.get_position(),
  4827. exception_message(token_type::literal_or_value, "value")));
  4828. }
  4829. }
  4830. }
  4831. else
  4832. {
  4833. skip_to_state_evaluation = false;
  4834. }
  4835. // we reached this line after we successfully parsed a value
  4836. if (states.empty())
  4837. {
  4838. // empty stack: we reached the end of the hierarchy: done
  4839. return true;
  4840. }
  4841. else
  4842. {
  4843. if (states.back()) // array
  4844. {
  4845. // comma -> next value
  4846. if (get_token() == token_type::value_separator)
  4847. {
  4848. // parse a new value
  4849. get_token();
  4850. continue;
  4851. }
  4852. // closing ]
  4853. if (JSON_LIKELY(last_token == token_type::end_array))
  4854. {
  4855. if (JSON_UNLIKELY(not sax->end_array()))
  4856. {
  4857. return false;
  4858. }
  4859. // We are done with this array. Before we can parse a
  4860. // new value, we need to evaluate the new state first.
  4861. // By setting skip_to_state_evaluation to false, we
  4862. // are effectively jumping to the beginning of this if.
  4863. assert(not states.empty());
  4864. states.pop_back();
  4865. skip_to_state_evaluation = true;
  4866. continue;
  4867. }
  4868. else
  4869. {
  4870. return sax->parse_error(m_lexer.get_position(),
  4871. m_lexer.get_token_string(),
  4872. parse_error::create(101, m_lexer.get_position(),
  4873. exception_message(token_type::end_array, "array")));
  4874. }
  4875. }
  4876. else // object
  4877. {
  4878. // comma -> next value
  4879. if (get_token() == token_type::value_separator)
  4880. {
  4881. // parse key
  4882. if (JSON_UNLIKELY(get_token() != token_type::value_string))
  4883. {
  4884. return sax->parse_error(m_lexer.get_position(),
  4885. m_lexer.get_token_string(),
  4886. parse_error::create(101, m_lexer.get_position(),
  4887. exception_message(token_type::value_string, "object key")));
  4888. }
  4889. else
  4890. {
  4891. if (JSON_UNLIKELY(not sax->key(m_lexer.get_string())))
  4892. {
  4893. return false;
  4894. }
  4895. }
  4896. // parse separator (:)
  4897. if (JSON_UNLIKELY(get_token() != token_type::name_separator))
  4898. {
  4899. return sax->parse_error(m_lexer.get_position(),
  4900. m_lexer.get_token_string(),
  4901. parse_error::create(101, m_lexer.get_position(),
  4902. exception_message(token_type::name_separator, "object separator")));
  4903. }
  4904. // parse values
  4905. get_token();
  4906. continue;
  4907. }
  4908. // closing }
  4909. if (JSON_LIKELY(last_token == token_type::end_object))
  4910. {
  4911. if (JSON_UNLIKELY(not sax->end_object()))
  4912. {
  4913. return false;
  4914. }
  4915. // We are done with this object. Before we can parse a
  4916. // new value, we need to evaluate the new state first.
  4917. // By setting skip_to_state_evaluation to false, we
  4918. // are effectively jumping to the beginning of this if.
  4919. assert(not states.empty());
  4920. states.pop_back();
  4921. skip_to_state_evaluation = true;
  4922. continue;
  4923. }
  4924. else
  4925. {
  4926. return sax->parse_error(m_lexer.get_position(),
  4927. m_lexer.get_token_string(),
  4928. parse_error::create(101, m_lexer.get_position(),
  4929. exception_message(token_type::end_object, "object")));
  4930. }
  4931. }
  4932. }
  4933. }
  4934. }
  4935. /// get next token from lexer
  4936. token_type get_token()
  4937. {
  4938. return (last_token = m_lexer.scan());
  4939. }
  4940. std::string exception_message(const token_type expected, const std::string& context)
  4941. {
  4942. std::string error_msg = "syntax error ";
  4943. if (not context.empty())
  4944. {
  4945. error_msg += "while parsing " + context + " ";
  4946. }
  4947. error_msg += "- ";
  4948. if (last_token == token_type::parse_error)
  4949. {
  4950. error_msg += std::string(m_lexer.get_error_message()) + "; last read: '" +
  4951. m_lexer.get_token_string() + "'";
  4952. }
  4953. else
  4954. {
  4955. error_msg += "unexpected " + std::string(lexer_t::token_type_name(last_token));
  4956. }
  4957. if (expected != token_type::uninitialized)
  4958. {
  4959. error_msg += "; expected " + std::string(lexer_t::token_type_name(expected));
  4960. }
  4961. return error_msg;
  4962. }
  4963. private:
  4964. /// callback function
  4965. const parser_callback_t callback = nullptr;
  4966. /// the type of the last read token
  4967. token_type last_token = token_type::uninitialized;
  4968. /// the lexer
  4969. lexer_t m_lexer;
  4970. /// whether to throw exceptions in case of errors
  4971. const bool allow_exceptions = true;
  4972. };
  4973. } // namespace detail
  4974. } // namespace nlohmann
  4975. // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
  4976. #include <cstddef> // ptrdiff_t
  4977. #include <limits> // numeric_limits
  4978. namespace nlohmann
  4979. {
  4980. namespace detail
  4981. {
  4982. /*
  4983. @brief an iterator for primitive JSON types
  4984. This class models an iterator for primitive JSON types (boolean, number,
  4985. string). It's only purpose is to allow the iterator/const_iterator classes
  4986. to "iterate" over primitive values. Internally, the iterator is modeled by
  4987. a `difference_type` variable. Value begin_value (`0`) models the begin,
  4988. end_value (`1`) models past the end.
  4989. */
  4990. class primitive_iterator_t
  4991. {
  4992. private:
  4993. using difference_type = std::ptrdiff_t;
  4994. static constexpr difference_type begin_value = 0;
  4995. static constexpr difference_type end_value = begin_value + 1;
  4996. /// iterator as signed integer type
  4997. difference_type m_it = (std::numeric_limits<std::ptrdiff_t>::min)();
  4998. public:
  4999. constexpr difference_type get_value() const noexcept
  5000. {
  5001. return m_it;
  5002. }
  5003. /// set iterator to a defined beginning
  5004. void set_begin() noexcept
  5005. {
  5006. m_it = begin_value;
  5007. }
  5008. /// set iterator to a defined past the end
  5009. void set_end() noexcept
  5010. {
  5011. m_it = end_value;
  5012. }
  5013. /// return whether the iterator can be dereferenced
  5014. constexpr bool is_begin() const noexcept
  5015. {
  5016. return m_it == begin_value;
  5017. }
  5018. /// return whether the iterator is at end
  5019. constexpr bool is_end() const noexcept
  5020. {
  5021. return m_it == end_value;
  5022. }
  5023. friend constexpr bool operator==(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
  5024. {
  5025. return lhs.m_it == rhs.m_it;
  5026. }
  5027. friend constexpr bool operator<(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
  5028. {
  5029. return lhs.m_it < rhs.m_it;
  5030. }
  5031. primitive_iterator_t operator+(difference_type n) noexcept
  5032. {
  5033. auto result = *this;
  5034. result += n;
  5035. return result;
  5036. }
  5037. friend constexpr difference_type operator-(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
  5038. {
  5039. return lhs.m_it - rhs.m_it;
  5040. }
  5041. primitive_iterator_t& operator++() noexcept
  5042. {
  5043. ++m_it;
  5044. return *this;
  5045. }
  5046. primitive_iterator_t const operator++(int) noexcept
  5047. {
  5048. auto result = *this;
  5049. ++m_it;
  5050. return result;
  5051. }
  5052. primitive_iterator_t& operator--() noexcept
  5053. {
  5054. --m_it;
  5055. return *this;
  5056. }
  5057. primitive_iterator_t const operator--(int) noexcept
  5058. {
  5059. auto result = *this;
  5060. --m_it;
  5061. return result;
  5062. }
  5063. primitive_iterator_t& operator+=(difference_type n) noexcept
  5064. {
  5065. m_it += n;
  5066. return *this;
  5067. }
  5068. primitive_iterator_t& operator-=(difference_type n) noexcept
  5069. {
  5070. m_it -= n;
  5071. return *this;
  5072. }
  5073. };
  5074. } // namespace detail
  5075. } // namespace nlohmann
  5076. // #include <nlohmann/detail/iterators/internal_iterator.hpp>
  5077. // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
  5078. namespace nlohmann
  5079. {
  5080. namespace detail
  5081. {
  5082. /*!
  5083. @brief an iterator value
  5084. @note This structure could easily be a union, but MSVC currently does not allow
  5085. unions members with complex constructors, see https://github.com/nlohmann/json/pull/105.
  5086. */
  5087. template<typename BasicJsonType> struct internal_iterator
  5088. {
  5089. /// iterator for JSON objects
  5090. typename BasicJsonType::object_t::iterator object_iterator {};
  5091. /// iterator for JSON arrays
  5092. typename BasicJsonType::array_t::iterator array_iterator {};
  5093. /// generic iterator for all other types
  5094. primitive_iterator_t primitive_iterator {};
  5095. };
  5096. } // namespace detail
  5097. } // namespace nlohmann
  5098. // #include <nlohmann/detail/iterators/iter_impl.hpp>
  5099. #include <ciso646> // not
  5100. #include <iterator> // iterator, random_access_iterator_tag, bidirectional_iterator_tag, advance, next
  5101. #include <type_traits> // conditional, is_const, remove_const
  5102. // #include <nlohmann/detail/exceptions.hpp>
  5103. // #include <nlohmann/detail/iterators/internal_iterator.hpp>
  5104. // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
  5105. // #include <nlohmann/detail/macro_scope.hpp>
  5106. // #include <nlohmann/detail/meta/cpp_future.hpp>
  5107. // #include <nlohmann/detail/value_t.hpp>
  5108. namespace nlohmann
  5109. {
  5110. namespace detail
  5111. {
  5112. // forward declare, to be able to friend it later on
  5113. template<typename IteratorType> class iteration_proxy;
  5114. template<typename IteratorType> class iteration_proxy_value;
  5115. /*!
  5116. @brief a template for a bidirectional iterator for the @ref basic_json class
  5117. This class implements a both iterators (iterator and const_iterator) for the
  5118. @ref basic_json class.
  5119. @note An iterator is called *initialized* when a pointer to a JSON value has
  5120. been set (e.g., by a constructor or a copy assignment). If the iterator is
  5121. default-constructed, it is *uninitialized* and most methods are undefined.
  5122. **The library uses assertions to detect calls on uninitialized iterators.**
  5123. @requirement The class satisfies the following concept requirements:
  5124. -
  5125. [BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
  5126. The iterator that can be moved can be moved in both directions (i.e.
  5127. incremented and decremented).
  5128. @since version 1.0.0, simplified in version 2.0.9, change to bidirectional
  5129. iterators in version 3.0.0 (see https://github.com/nlohmann/json/issues/593)
  5130. */
  5131. template<typename BasicJsonType>
  5132. class iter_impl
  5133. {
  5134. /// allow basic_json to access private members
  5135. friend iter_impl<typename std::conditional<std::is_const<BasicJsonType>::value, typename std::remove_const<BasicJsonType>::type, const BasicJsonType>::type>;
  5136. friend BasicJsonType;
  5137. friend iteration_proxy<iter_impl>;
  5138. friend iteration_proxy_value<iter_impl>;
  5139. using object_t = typename BasicJsonType::object_t;
  5140. using array_t = typename BasicJsonType::array_t;
  5141. // make sure BasicJsonType is basic_json or const basic_json
  5142. static_assert(is_basic_json<typename std::remove_const<BasicJsonType>::type>::value,
  5143. "iter_impl only accepts (const) basic_json");
  5144. public:
  5145. /// The std::iterator class template (used as a base class to provide typedefs) is deprecated in C++17.
  5146. /// The C++ Standard has never required user-defined iterators to derive from std::iterator.
  5147. /// A user-defined iterator should provide publicly accessible typedefs named
  5148. /// iterator_category, value_type, difference_type, pointer, and reference.
  5149. /// Note that value_type is required to be non-const, even for constant iterators.
  5150. using iterator_category = std::bidirectional_iterator_tag;
  5151. /// the type of the values when the iterator is dereferenced
  5152. using value_type = typename BasicJsonType::value_type;
  5153. /// a type to represent differences between iterators
  5154. using difference_type = typename BasicJsonType::difference_type;
  5155. /// defines a pointer to the type iterated over (value_type)
  5156. using pointer = typename std::conditional<std::is_const<BasicJsonType>::value,
  5157. typename BasicJsonType::const_pointer,
  5158. typename BasicJsonType::pointer>::type;
  5159. /// defines a reference to the type iterated over (value_type)
  5160. using reference =
  5161. typename std::conditional<std::is_const<BasicJsonType>::value,
  5162. typename BasicJsonType::const_reference,
  5163. typename BasicJsonType::reference>::type;
  5164. /// default constructor
  5165. iter_impl() = default;
  5166. /*!
  5167. @brief constructor for a given JSON instance
  5168. @param[in] object pointer to a JSON object for this iterator
  5169. @pre object != nullptr
  5170. @post The iterator is initialized; i.e. `m_object != nullptr`.
  5171. */
  5172. explicit iter_impl(pointer object) noexcept : m_object(object)
  5173. {
  5174. assert(m_object != nullptr);
  5175. switch (m_object->m_type)
  5176. {
  5177. case value_t::object:
  5178. {
  5179. m_it.object_iterator = typename object_t::iterator();
  5180. break;
  5181. }
  5182. case value_t::array:
  5183. {
  5184. m_it.array_iterator = typename array_t::iterator();
  5185. break;
  5186. }
  5187. default:
  5188. {
  5189. m_it.primitive_iterator = primitive_iterator_t();
  5190. break;
  5191. }
  5192. }
  5193. }
  5194. /*!
  5195. @note The conventional copy constructor and copy assignment are implicitly
  5196. defined. Combined with the following converting constructor and
  5197. assignment, they support: (1) copy from iterator to iterator, (2)
  5198. copy from const iterator to const iterator, and (3) conversion from
  5199. iterator to const iterator. However conversion from const iterator
  5200. to iterator is not defined.
  5201. */
  5202. /*!
  5203. @brief converting constructor
  5204. @param[in] other non-const iterator to copy from
  5205. @note It is not checked whether @a other is initialized.
  5206. */
  5207. iter_impl(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept
  5208. : m_object(other.m_object), m_it(other.m_it) {}
  5209. /*!
  5210. @brief converting assignment
  5211. @param[in,out] other non-const iterator to copy from
  5212. @return const/non-const iterator
  5213. @note It is not checked whether @a other is initialized.
  5214. */
  5215. iter_impl& operator=(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept
  5216. {
  5217. m_object = other.m_object;
  5218. m_it = other.m_it;
  5219. return *this;
  5220. }
  5221. private:
  5222. /*!
  5223. @brief set the iterator to the first value
  5224. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  5225. */
  5226. void set_begin() noexcept
  5227. {
  5228. assert(m_object != nullptr);
  5229. switch (m_object->m_type)
  5230. {
  5231. case value_t::object:
  5232. {
  5233. m_it.object_iterator = m_object->m_value.object->begin();
  5234. break;
  5235. }
  5236. case value_t::array:
  5237. {
  5238. m_it.array_iterator = m_object->m_value.array->begin();
  5239. break;
  5240. }
  5241. case value_t::null:
  5242. {
  5243. // set to end so begin()==end() is true: null is empty
  5244. m_it.primitive_iterator.set_end();
  5245. break;
  5246. }
  5247. default:
  5248. {
  5249. m_it.primitive_iterator.set_begin();
  5250. break;
  5251. }
  5252. }
  5253. }
  5254. /*!
  5255. @brief set the iterator past the last value
  5256. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  5257. */
  5258. void set_end() noexcept
  5259. {
  5260. assert(m_object != nullptr);
  5261. switch (m_object->m_type)
  5262. {
  5263. case value_t::object:
  5264. {
  5265. m_it.object_iterator = m_object->m_value.object->end();
  5266. break;
  5267. }
  5268. case value_t::array:
  5269. {
  5270. m_it.array_iterator = m_object->m_value.array->end();
  5271. break;
  5272. }
  5273. default:
  5274. {
  5275. m_it.primitive_iterator.set_end();
  5276. break;
  5277. }
  5278. }
  5279. }
  5280. public:
  5281. /*!
  5282. @brief return a reference to the value pointed to by the iterator
  5283. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  5284. */
  5285. reference operator*() const
  5286. {
  5287. assert(m_object != nullptr);
  5288. switch (m_object->m_type)
  5289. {
  5290. case value_t::object:
  5291. {
  5292. assert(m_it.object_iterator != m_object->m_value.object->end());
  5293. return m_it.object_iterator->second;
  5294. }
  5295. case value_t::array:
  5296. {
  5297. assert(m_it.array_iterator != m_object->m_value.array->end());
  5298. return *m_it.array_iterator;
  5299. }
  5300. case value_t::null:
  5301. JSON_THROW(invalid_iterator::create(214, "cannot get value"));
  5302. default:
  5303. {
  5304. if (JSON_LIKELY(m_it.primitive_iterator.is_begin()))
  5305. {
  5306. return *m_object;
  5307. }
  5308. JSON_THROW(invalid_iterator::create(214, "cannot get value"));
  5309. }
  5310. }
  5311. }
  5312. /*!
  5313. @brief dereference the iterator
  5314. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  5315. */
  5316. pointer operator->() const
  5317. {
  5318. assert(m_object != nullptr);
  5319. switch (m_object->m_type)
  5320. {
  5321. case value_t::object:
  5322. {
  5323. assert(m_it.object_iterator != m_object->m_value.object->end());
  5324. return &(m_it.object_iterator->second);
  5325. }
  5326. case value_t::array:
  5327. {
  5328. assert(m_it.array_iterator != m_object->m_value.array->end());
  5329. return &*m_it.array_iterator;
  5330. }
  5331. default:
  5332. {
  5333. if (JSON_LIKELY(m_it.primitive_iterator.is_begin()))
  5334. {
  5335. return m_object;
  5336. }
  5337. JSON_THROW(invalid_iterator::create(214, "cannot get value"));
  5338. }
  5339. }
  5340. }
  5341. /*!
  5342. @brief post-increment (it++)
  5343. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  5344. */
  5345. iter_impl const operator++(int)
  5346. {
  5347. auto result = *this;
  5348. ++(*this);
  5349. return result;
  5350. }
  5351. /*!
  5352. @brief pre-increment (++it)
  5353. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  5354. */
  5355. iter_impl& operator++()
  5356. {
  5357. assert(m_object != nullptr);
  5358. switch (m_object->m_type)
  5359. {
  5360. case value_t::object:
  5361. {
  5362. std::advance(m_it.object_iterator, 1);
  5363. break;
  5364. }
  5365. case value_t::array:
  5366. {
  5367. std::advance(m_it.array_iterator, 1);
  5368. break;
  5369. }
  5370. default:
  5371. {
  5372. ++m_it.primitive_iterator;
  5373. break;
  5374. }
  5375. }
  5376. return *this;
  5377. }
  5378. /*!
  5379. @brief post-decrement (it--)
  5380. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  5381. */
  5382. iter_impl const operator--(int)
  5383. {
  5384. auto result = *this;
  5385. --(*this);
  5386. return result;
  5387. }
  5388. /*!
  5389. @brief pre-decrement (--it)
  5390. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  5391. */
  5392. iter_impl& operator--()
  5393. {
  5394. assert(m_object != nullptr);
  5395. switch (m_object->m_type)
  5396. {
  5397. case value_t::object:
  5398. {
  5399. std::advance(m_it.object_iterator, -1);
  5400. break;
  5401. }
  5402. case value_t::array:
  5403. {
  5404. std::advance(m_it.array_iterator, -1);
  5405. break;
  5406. }
  5407. default:
  5408. {
  5409. --m_it.primitive_iterator;
  5410. break;
  5411. }
  5412. }
  5413. return *this;
  5414. }
  5415. /*!
  5416. @brief comparison: equal
  5417. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  5418. */
  5419. bool operator==(const iter_impl& other) const
  5420. {
  5421. // if objects are not the same, the comparison is undefined
  5422. if (JSON_UNLIKELY(m_object != other.m_object))
  5423. {
  5424. JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers"));
  5425. }
  5426. assert(m_object != nullptr);
  5427. switch (m_object->m_type)
  5428. {
  5429. case value_t::object:
  5430. return (m_it.object_iterator == other.m_it.object_iterator);
  5431. case value_t::array:
  5432. return (m_it.array_iterator == other.m_it.array_iterator);
  5433. default:
  5434. return (m_it.primitive_iterator == other.m_it.primitive_iterator);
  5435. }
  5436. }
  5437. /*!
  5438. @brief comparison: not equal
  5439. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  5440. */
  5441. bool operator!=(const iter_impl& other) const
  5442. {
  5443. return not operator==(other);
  5444. }
  5445. /*!
  5446. @brief comparison: smaller
  5447. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  5448. */
  5449. bool operator<(const iter_impl& other) const
  5450. {
  5451. // if objects are not the same, the comparison is undefined
  5452. if (JSON_UNLIKELY(m_object != other.m_object))
  5453. {
  5454. JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers"));
  5455. }
  5456. assert(m_object != nullptr);
  5457. switch (m_object->m_type)
  5458. {
  5459. case value_t::object:
  5460. JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators"));
  5461. case value_t::array:
  5462. return (m_it.array_iterator < other.m_it.array_iterator);
  5463. default:
  5464. return (m_it.primitive_iterator < other.m_it.primitive_iterator);
  5465. }
  5466. }
  5467. /*!
  5468. @brief comparison: less than or equal
  5469. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  5470. */
  5471. bool operator<=(const iter_impl& other) const
  5472. {
  5473. return not other.operator < (*this);
  5474. }
  5475. /*!
  5476. @brief comparison: greater than
  5477. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  5478. */
  5479. bool operator>(const iter_impl& other) const
  5480. {
  5481. return not operator<=(other);
  5482. }
  5483. /*!
  5484. @brief comparison: greater than or equal
  5485. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  5486. */
  5487. bool operator>=(const iter_impl& other) const
  5488. {
  5489. return not operator<(other);
  5490. }
  5491. /*!
  5492. @brief add to iterator
  5493. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  5494. */
  5495. iter_impl& operator+=(difference_type i)
  5496. {
  5497. assert(m_object != nullptr);
  5498. switch (m_object->m_type)
  5499. {
  5500. case value_t::object:
  5501. JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators"));
  5502. case value_t::array:
  5503. {
  5504. std::advance(m_it.array_iterator, i);
  5505. break;
  5506. }
  5507. default:
  5508. {
  5509. m_it.primitive_iterator += i;
  5510. break;
  5511. }
  5512. }
  5513. return *this;
  5514. }
  5515. /*!
  5516. @brief subtract from iterator
  5517. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  5518. */
  5519. iter_impl& operator-=(difference_type i)
  5520. {
  5521. return operator+=(-i);
  5522. }
  5523. /*!
  5524. @brief add to iterator
  5525. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  5526. */
  5527. iter_impl operator+(difference_type i) const
  5528. {
  5529. auto result = *this;
  5530. result += i;
  5531. return result;
  5532. }
  5533. /*!
  5534. @brief addition of distance and iterator
  5535. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  5536. */
  5537. friend iter_impl operator+(difference_type i, const iter_impl& it)
  5538. {
  5539. auto result = it;
  5540. result += i;
  5541. return result;
  5542. }
  5543. /*!
  5544. @brief subtract from iterator
  5545. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  5546. */
  5547. iter_impl operator-(difference_type i) const
  5548. {
  5549. auto result = *this;
  5550. result -= i;
  5551. return result;
  5552. }
  5553. /*!
  5554. @brief return difference
  5555. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  5556. */
  5557. difference_type operator-(const iter_impl& other) const
  5558. {
  5559. assert(m_object != nullptr);
  5560. switch (m_object->m_type)
  5561. {
  5562. case value_t::object:
  5563. JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators"));
  5564. case value_t::array:
  5565. return m_it.array_iterator - other.m_it.array_iterator;
  5566. default:
  5567. return m_it.primitive_iterator - other.m_it.primitive_iterator;
  5568. }
  5569. }
  5570. /*!
  5571. @brief access to successor
  5572. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  5573. */
  5574. reference operator[](difference_type n) const
  5575. {
  5576. assert(m_object != nullptr);
  5577. switch (m_object->m_type)
  5578. {
  5579. case value_t::object:
  5580. JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators"));
  5581. case value_t::array:
  5582. return *std::next(m_it.array_iterator, n);
  5583. case value_t::null:
  5584. JSON_THROW(invalid_iterator::create(214, "cannot get value"));
  5585. default:
  5586. {
  5587. if (JSON_LIKELY(m_it.primitive_iterator.get_value() == -n))
  5588. {
  5589. return *m_object;
  5590. }
  5591. JSON_THROW(invalid_iterator::create(214, "cannot get value"));
  5592. }
  5593. }
  5594. }
  5595. /*!
  5596. @brief return the key of an object iterator
  5597. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  5598. */
  5599. const typename object_t::key_type& key() const
  5600. {
  5601. assert(m_object != nullptr);
  5602. if (JSON_LIKELY(m_object->is_object()))
  5603. {
  5604. return m_it.object_iterator->first;
  5605. }
  5606. JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators"));
  5607. }
  5608. /*!
  5609. @brief return the value of an iterator
  5610. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  5611. */
  5612. reference value() const
  5613. {
  5614. return operator*();
  5615. }
  5616. private:
  5617. /// associated JSON instance
  5618. pointer m_object = nullptr;
  5619. /// the actual iterator of the associated instance
  5620. internal_iterator<typename std::remove_const<BasicJsonType>::type> m_it;
  5621. };
  5622. } // namespace detail
  5623. } // namespace nlohmann
  5624. // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
  5625. // #include <nlohmann/detail/iterators/json_reverse_iterator.hpp>
  5626. #include <cstddef> // ptrdiff_t
  5627. #include <iterator> // reverse_iterator
  5628. #include <utility> // declval
  5629. namespace nlohmann
  5630. {
  5631. namespace detail
  5632. {
  5633. //////////////////////
  5634. // reverse_iterator //
  5635. //////////////////////
  5636. /*!
  5637. @brief a template for a reverse iterator class
  5638. @tparam Base the base iterator type to reverse. Valid types are @ref
  5639. iterator (to create @ref reverse_iterator) and @ref const_iterator (to
  5640. create @ref const_reverse_iterator).
  5641. @requirement The class satisfies the following concept requirements:
  5642. -
  5643. [BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
  5644. The iterator that can be moved can be moved in both directions (i.e.
  5645. incremented and decremented).
  5646. - [OutputIterator](https://en.cppreference.com/w/cpp/named_req/OutputIterator):
  5647. It is possible to write to the pointed-to element (only if @a Base is
  5648. @ref iterator).
  5649. @since version 1.0.0
  5650. */
  5651. template<typename Base>
  5652. class json_reverse_iterator : public std::reverse_iterator<Base>
  5653. {
  5654. public:
  5655. using difference_type = std::ptrdiff_t;
  5656. /// shortcut to the reverse iterator adapter
  5657. using base_iterator = std::reverse_iterator<Base>;
  5658. /// the reference type for the pointed-to element
  5659. using reference = typename Base::reference;
  5660. /// create reverse iterator from iterator
  5661. explicit json_reverse_iterator(const typename base_iterator::iterator_type& it) noexcept
  5662. : base_iterator(it) {}
  5663. /// create reverse iterator from base class
  5664. explicit json_reverse_iterator(const base_iterator& it) noexcept : base_iterator(it) {}
  5665. /// post-increment (it++)
  5666. json_reverse_iterator const operator++(int)
  5667. {
  5668. return static_cast<json_reverse_iterator>(base_iterator::operator++(1));
  5669. }
  5670. /// pre-increment (++it)
  5671. json_reverse_iterator& operator++()
  5672. {
  5673. return static_cast<json_reverse_iterator&>(base_iterator::operator++());
  5674. }
  5675. /// post-decrement (it--)
  5676. json_reverse_iterator const operator--(int)
  5677. {
  5678. return static_cast<json_reverse_iterator>(base_iterator::operator--(1));
  5679. }
  5680. /// pre-decrement (--it)
  5681. json_reverse_iterator& operator--()
  5682. {
  5683. return static_cast<json_reverse_iterator&>(base_iterator::operator--());
  5684. }
  5685. /// add to iterator
  5686. json_reverse_iterator& operator+=(difference_type i)
  5687. {
  5688. return static_cast<json_reverse_iterator&>(base_iterator::operator+=(i));
  5689. }
  5690. /// add to iterator
  5691. json_reverse_iterator operator+(difference_type i) const
  5692. {
  5693. return static_cast<json_reverse_iterator>(base_iterator::operator+(i));
  5694. }
  5695. /// subtract from iterator
  5696. json_reverse_iterator operator-(difference_type i) const
  5697. {
  5698. return static_cast<json_reverse_iterator>(base_iterator::operator-(i));
  5699. }
  5700. /// return difference
  5701. difference_type operator-(const json_reverse_iterator& other) const
  5702. {
  5703. return base_iterator(*this) - base_iterator(other);
  5704. }
  5705. /// access to successor
  5706. reference operator[](difference_type n) const
  5707. {
  5708. return *(this->operator+(n));
  5709. }
  5710. /// return the key of an object iterator
  5711. auto key() const -> decltype(std::declval<Base>().key())
  5712. {
  5713. auto it = --this->base();
  5714. return it.key();
  5715. }
  5716. /// return the value of an iterator
  5717. reference value() const
  5718. {
  5719. auto it = --this->base();
  5720. return it.operator * ();
  5721. }
  5722. };
  5723. } // namespace detail
  5724. } // namespace nlohmann
  5725. // #include <nlohmann/detail/output/output_adapters.hpp>
  5726. #include <algorithm> // copy
  5727. #include <cstddef> // size_t
  5728. #include <ios> // streamsize
  5729. #include <iterator> // back_inserter
  5730. #include <memory> // shared_ptr, make_shared
  5731. #include <ostream> // basic_ostream
  5732. #include <string> // basic_string
  5733. #include <vector> // vector
  5734. namespace nlohmann
  5735. {
  5736. namespace detail
  5737. {
  5738. /// abstract output adapter interface
  5739. template<typename CharType> struct output_adapter_protocol
  5740. {
  5741. virtual void write_character(CharType c) = 0;
  5742. virtual void write_characters(const CharType* s, std::size_t length) = 0;
  5743. virtual ~output_adapter_protocol() = default;
  5744. };
  5745. /// a type to simplify interfaces
  5746. template<typename CharType>
  5747. using output_adapter_t = std::shared_ptr<output_adapter_protocol<CharType>>;
  5748. /// output adapter for byte vectors
  5749. template<typename CharType>
  5750. class output_vector_adapter : public output_adapter_protocol<CharType>
  5751. {
  5752. public:
  5753. explicit output_vector_adapter(std::vector<CharType>& vec) noexcept
  5754. : v(vec)
  5755. {}
  5756. void write_character(CharType c) override
  5757. {
  5758. v.push_back(c);
  5759. }
  5760. void write_characters(const CharType* s, std::size_t length) override
  5761. {
  5762. std::copy(s, s + length, std::back_inserter(v));
  5763. }
  5764. private:
  5765. std::vector<CharType>& v;
  5766. };
  5767. /// output adapter for output streams
  5768. template<typename CharType>
  5769. class output_stream_adapter : public output_adapter_protocol<CharType>
  5770. {
  5771. public:
  5772. explicit output_stream_adapter(std::basic_ostream<CharType>& s) noexcept
  5773. : stream(s)
  5774. {}
  5775. void write_character(CharType c) override
  5776. {
  5777. stream.put(c);
  5778. }
  5779. void write_characters(const CharType* s, std::size_t length) override
  5780. {
  5781. stream.write(s, static_cast<std::streamsize>(length));
  5782. }
  5783. private:
  5784. std::basic_ostream<CharType>& stream;
  5785. };
  5786. /// output adapter for basic_string
  5787. template<typename CharType, typename StringType = std::basic_string<CharType>>
  5788. class output_string_adapter : public output_adapter_protocol<CharType>
  5789. {
  5790. public:
  5791. explicit output_string_adapter(StringType& s) noexcept
  5792. : str(s)
  5793. {}
  5794. void write_character(CharType c) override
  5795. {
  5796. str.push_back(c);
  5797. }
  5798. void write_characters(const CharType* s, std::size_t length) override
  5799. {
  5800. str.append(s, length);
  5801. }
  5802. private:
  5803. StringType& str;
  5804. };
  5805. template<typename CharType, typename StringType = std::basic_string<CharType>>
  5806. class output_adapter
  5807. {
  5808. public:
  5809. output_adapter(std::vector<CharType>& vec)
  5810. : oa(std::make_shared<output_vector_adapter<CharType>>(vec)) {}
  5811. output_adapter(std::basic_ostream<CharType>& s)
  5812. : oa(std::make_shared<output_stream_adapter<CharType>>(s)) {}
  5813. output_adapter(StringType& s)
  5814. : oa(std::make_shared<output_string_adapter<CharType, StringType>>(s)) {}
  5815. operator output_adapter_t<CharType>()
  5816. {
  5817. return oa;
  5818. }
  5819. private:
  5820. output_adapter_t<CharType> oa = nullptr;
  5821. };
  5822. } // namespace detail
  5823. } // namespace nlohmann
  5824. // #include <nlohmann/detail/input/binary_reader.hpp>
  5825. #include <algorithm> // generate_n
  5826. #include <array> // array
  5827. #include <cassert> // assert
  5828. #include <cmath> // ldexp
  5829. #include <cstddef> // size_t
  5830. #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
  5831. #include <cstdio> // snprintf
  5832. #include <cstring> // memcpy
  5833. #include <iterator> // back_inserter
  5834. #include <limits> // numeric_limits
  5835. #include <string> // char_traits, string
  5836. #include <utility> // make_pair, move
  5837. // #include <nlohmann/detail/input/input_adapters.hpp>
  5838. // #include <nlohmann/detail/input/json_sax.hpp>
  5839. // #include <nlohmann/detail/exceptions.hpp>
  5840. // #include <nlohmann/detail/macro_scope.hpp>
  5841. // #include <nlohmann/detail/meta/is_sax.hpp>
  5842. // #include <nlohmann/detail/value_t.hpp>
  5843. namespace nlohmann
  5844. {
  5845. namespace detail
  5846. {
  5847. ///////////////////
  5848. // binary reader //
  5849. ///////////////////
  5850. /*!
  5851. @brief deserialization of CBOR, MessagePack, and UBJSON values
  5852. */
  5853. template<typename BasicJsonType, typename SAX = json_sax_dom_parser<BasicJsonType>>
  5854. class binary_reader
  5855. {
  5856. using number_integer_t = typename BasicJsonType::number_integer_t;
  5857. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  5858. using number_float_t = typename BasicJsonType::number_float_t;
  5859. using string_t = typename BasicJsonType::string_t;
  5860. using json_sax_t = SAX;
  5861. public:
  5862. /*!
  5863. @brief create a binary reader
  5864. @param[in] adapter input adapter to read from
  5865. */
  5866. explicit binary_reader(input_adapter_t adapter) : ia(std::move(adapter))
  5867. {
  5868. (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
  5869. assert(ia);
  5870. }
  5871. /*!
  5872. @param[in] format the binary format to parse
  5873. @param[in] sax_ a SAX event processor
  5874. @param[in] strict whether to expect the input to be consumed completed
  5875. @return
  5876. */
  5877. bool sax_parse(const input_format_t format,
  5878. json_sax_t* sax_,
  5879. const bool strict = true)
  5880. {
  5881. sax = sax_;
  5882. bool result = false;
  5883. switch (format)
  5884. {
  5885. case input_format_t::bson:
  5886. result = parse_bson_internal();
  5887. break;
  5888. case input_format_t::cbor:
  5889. result = parse_cbor_internal();
  5890. break;
  5891. case input_format_t::msgpack:
  5892. result = parse_msgpack_internal();
  5893. break;
  5894. case input_format_t::ubjson:
  5895. result = parse_ubjson_internal();
  5896. break;
  5897. // LCOV_EXCL_START
  5898. default:
  5899. assert(false);
  5900. // LCOV_EXCL_STOP
  5901. }
  5902. // strict mode: next byte must be EOF
  5903. if (result and strict)
  5904. {
  5905. if (format == input_format_t::ubjson)
  5906. {
  5907. get_ignore_noop();
  5908. }
  5909. else
  5910. {
  5911. get();
  5912. }
  5913. if (JSON_UNLIKELY(current != std::char_traits<char>::eof()))
  5914. {
  5915. return sax->parse_error(chars_read, get_token_string(),
  5916. parse_error::create(110, chars_read, exception_message(format, "expected end of input; last byte: 0x" + get_token_string(), "value")));
  5917. }
  5918. }
  5919. return result;
  5920. }
  5921. /*!
  5922. @brief determine system byte order
  5923. @return true if and only if system's byte order is little endian
  5924. @note from http://stackoverflow.com/a/1001328/266378
  5925. */
  5926. static constexpr bool little_endianess(int num = 1) noexcept
  5927. {
  5928. return (*reinterpret_cast<char*>(&num) == 1);
  5929. }
  5930. private:
  5931. //////////
  5932. // BSON //
  5933. //////////
  5934. /*!
  5935. @brief Reads in a BSON-object and passes it to the SAX-parser.
  5936. @return whether a valid BSON-value was passed to the SAX parser
  5937. */
  5938. bool parse_bson_internal()
  5939. {
  5940. std::int32_t document_size;
  5941. get_number<std::int32_t, true>(input_format_t::bson, document_size);
  5942. if (JSON_UNLIKELY(not sax->start_object(std::size_t(-1))))
  5943. {
  5944. return false;
  5945. }
  5946. if (JSON_UNLIKELY(not parse_bson_element_list(/*is_array*/false)))
  5947. {
  5948. return false;
  5949. }
  5950. return sax->end_object();
  5951. }
  5952. /*!
  5953. @brief Parses a C-style string from the BSON input.
  5954. @param[in, out] result A reference to the string variable where the read
  5955. string is to be stored.
  5956. @return `true` if the \x00-byte indicating the end of the string was
  5957. encountered before the EOF; false` indicates an unexpected EOF.
  5958. */
  5959. bool get_bson_cstr(string_t& result)
  5960. {
  5961. auto out = std::back_inserter(result);
  5962. while (true)
  5963. {
  5964. get();
  5965. if (JSON_UNLIKELY(not unexpect_eof(input_format_t::bson, "cstring")))
  5966. {
  5967. return false;
  5968. }
  5969. if (current == 0x00)
  5970. {
  5971. return true;
  5972. }
  5973. *out++ = static_cast<char>(current);
  5974. }
  5975. return true;
  5976. }
  5977. /*!
  5978. @brief Parses a zero-terminated string of length @a len from the BSON
  5979. input.
  5980. @param[in] len The length (including the zero-byte at the end) of the
  5981. string to be read.
  5982. @param[in, out] result A reference to the string variable where the read
  5983. string is to be stored.
  5984. @tparam NumberType The type of the length @a len
  5985. @pre len >= 1
  5986. @return `true` if the string was successfully parsed
  5987. */
  5988. template<typename NumberType>
  5989. bool get_bson_string(const NumberType len, string_t& result)
  5990. {
  5991. if (JSON_UNLIKELY(len < 1))
  5992. {
  5993. auto last_token = get_token_string();
  5994. 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")));
  5995. }
  5996. return get_string(input_format_t::bson, len - static_cast<NumberType>(1), result) and get() != std::char_traits<char>::eof();
  5997. }
  5998. /*!
  5999. @brief Read a BSON document element of the given @a element_type.
  6000. @param[in] element_type The BSON element type, c.f. http://bsonspec.org/spec.html
  6001. @param[in] element_type_parse_position The position in the input stream,
  6002. where the `element_type` was read.
  6003. @warning Not all BSON element types are supported yet. An unsupported
  6004. @a element_type will give rise to a parse_error.114:
  6005. Unsupported BSON record type 0x...
  6006. @return whether a valid BSON-object/array was passed to the SAX parser
  6007. */
  6008. bool parse_bson_element_internal(const int element_type,
  6009. const std::size_t element_type_parse_position)
  6010. {
  6011. switch (element_type)
  6012. {
  6013. case 0x01: // double
  6014. {
  6015. double number;
  6016. return get_number<double, true>(input_format_t::bson, number) and sax->number_float(static_cast<number_float_t>(number), "");
  6017. }
  6018. case 0x02: // string
  6019. {
  6020. std::int32_t len;
  6021. string_t value;
  6022. return get_number<std::int32_t, true>(input_format_t::bson, len) and get_bson_string(len, value) and sax->string(value);
  6023. }
  6024. case 0x03: // object
  6025. {
  6026. return parse_bson_internal();
  6027. }
  6028. case 0x04: // array
  6029. {
  6030. return parse_bson_array();
  6031. }
  6032. case 0x08: // boolean
  6033. {
  6034. return sax->boolean(get() != 0);
  6035. }
  6036. case 0x0A: // null
  6037. {
  6038. return sax->null();
  6039. }
  6040. case 0x10: // int32
  6041. {
  6042. std::int32_t value;
  6043. return get_number<std::int32_t, true>(input_format_t::bson, value) and sax->number_integer(value);
  6044. }
  6045. case 0x12: // int64
  6046. {
  6047. std::int64_t value;
  6048. return get_number<std::int64_t, true>(input_format_t::bson, value) and sax->number_integer(value);
  6049. }
  6050. default: // anything else not supported (yet)
  6051. {
  6052. char cr[3];
  6053. (std::snprintf)(cr, sizeof(cr), "%.2hhX", static_cast<unsigned char>(element_type));
  6054. return sax->parse_error(element_type_parse_position, std::string(cr), parse_error::create(114, element_type_parse_position, "Unsupported BSON record type 0x" + std::string(cr)));
  6055. }
  6056. }
  6057. }
  6058. /*!
  6059. @brief Read a BSON element list (as specified in the BSON-spec)
  6060. The same binary layout is used for objects and arrays, hence it must be
  6061. indicated with the argument @a is_array which one is expected
  6062. (true --> array, false --> object).
  6063. @param[in] is_array Determines if the element list being read is to be
  6064. treated as an object (@a is_array == false), or as an
  6065. array (@a is_array == true).
  6066. @return whether a valid BSON-object/array was passed to the SAX parser
  6067. */
  6068. bool parse_bson_element_list(const bool is_array)
  6069. {
  6070. string_t key;
  6071. while (int element_type = get())
  6072. {
  6073. if (JSON_UNLIKELY(not unexpect_eof(input_format_t::bson, "element list")))
  6074. {
  6075. return false;
  6076. }
  6077. const std::size_t element_type_parse_position = chars_read;
  6078. if (JSON_UNLIKELY(not get_bson_cstr(key)))
  6079. {
  6080. return false;
  6081. }
  6082. if (not is_array)
  6083. {
  6084. if (not sax->key(key))
  6085. {
  6086. return false;
  6087. }
  6088. }
  6089. if (JSON_UNLIKELY(not parse_bson_element_internal(element_type, element_type_parse_position)))
  6090. {
  6091. return false;
  6092. }
  6093. // get_bson_cstr only appends
  6094. key.clear();
  6095. }
  6096. return true;
  6097. }
  6098. /*!
  6099. @brief Reads an array from the BSON input and passes it to the SAX-parser.
  6100. @return whether a valid BSON-array was passed to the SAX parser
  6101. */
  6102. bool parse_bson_array()
  6103. {
  6104. std::int32_t document_size;
  6105. get_number<std::int32_t, true>(input_format_t::bson, document_size);
  6106. if (JSON_UNLIKELY(not sax->start_array(std::size_t(-1))))
  6107. {
  6108. return false;
  6109. }
  6110. if (JSON_UNLIKELY(not parse_bson_element_list(/*is_array*/true)))
  6111. {
  6112. return false;
  6113. }
  6114. return sax->end_array();
  6115. }
  6116. //////////
  6117. // CBOR //
  6118. //////////
  6119. /*!
  6120. @param[in] get_char whether a new character should be retrieved from the
  6121. input (true, default) or whether the last read
  6122. character should be considered instead
  6123. @return whether a valid CBOR value was passed to the SAX parser
  6124. */
  6125. bool parse_cbor_internal(const bool get_char = true)
  6126. {
  6127. switch (get_char ? get() : current)
  6128. {
  6129. // EOF
  6130. case std::char_traits<char>::eof():
  6131. return unexpect_eof(input_format_t::cbor, "value");
  6132. // Integer 0x00..0x17 (0..23)
  6133. case 0x00:
  6134. case 0x01:
  6135. case 0x02:
  6136. case 0x03:
  6137. case 0x04:
  6138. case 0x05:
  6139. case 0x06:
  6140. case 0x07:
  6141. case 0x08:
  6142. case 0x09:
  6143. case 0x0A:
  6144. case 0x0B:
  6145. case 0x0C:
  6146. case 0x0D:
  6147. case 0x0E:
  6148. case 0x0F:
  6149. case 0x10:
  6150. case 0x11:
  6151. case 0x12:
  6152. case 0x13:
  6153. case 0x14:
  6154. case 0x15:
  6155. case 0x16:
  6156. case 0x17:
  6157. return sax->number_unsigned(static_cast<number_unsigned_t>(current));
  6158. case 0x18: // Unsigned integer (one-byte uint8_t follows)
  6159. {
  6160. uint8_t number;
  6161. return get_number(input_format_t::cbor, number) and sax->number_unsigned(number);
  6162. }
  6163. case 0x19: // Unsigned integer (two-byte uint16_t follows)
  6164. {
  6165. uint16_t number;
  6166. return get_number(input_format_t::cbor, number) and sax->number_unsigned(number);
  6167. }
  6168. case 0x1A: // Unsigned integer (four-byte uint32_t follows)
  6169. {
  6170. uint32_t number;
  6171. return get_number(input_format_t::cbor, number) and sax->number_unsigned(number);
  6172. }
  6173. case 0x1B: // Unsigned integer (eight-byte uint64_t follows)
  6174. {
  6175. uint64_t number;
  6176. return get_number(input_format_t::cbor, number) and sax->number_unsigned(number);
  6177. }
  6178. // Negative integer -1-0x00..-1-0x17 (-1..-24)
  6179. case 0x20:
  6180. case 0x21:
  6181. case 0x22:
  6182. case 0x23:
  6183. case 0x24:
  6184. case 0x25:
  6185. case 0x26:
  6186. case 0x27:
  6187. case 0x28:
  6188. case 0x29:
  6189. case 0x2A:
  6190. case 0x2B:
  6191. case 0x2C:
  6192. case 0x2D:
  6193. case 0x2E:
  6194. case 0x2F:
  6195. case 0x30:
  6196. case 0x31:
  6197. case 0x32:
  6198. case 0x33:
  6199. case 0x34:
  6200. case 0x35:
  6201. case 0x36:
  6202. case 0x37:
  6203. return sax->number_integer(static_cast<int8_t>(0x20 - 1 - current));
  6204. case 0x38: // Negative integer (one-byte uint8_t follows)
  6205. {
  6206. uint8_t number;
  6207. return get_number(input_format_t::cbor, number) and sax->number_integer(static_cast<number_integer_t>(-1) - number);
  6208. }
  6209. case 0x39: // Negative integer -1-n (two-byte uint16_t follows)
  6210. {
  6211. uint16_t number;
  6212. return get_number(input_format_t::cbor, number) and sax->number_integer(static_cast<number_integer_t>(-1) - number);
  6213. }
  6214. case 0x3A: // Negative integer -1-n (four-byte uint32_t follows)
  6215. {
  6216. uint32_t number;
  6217. return get_number(input_format_t::cbor, number) and sax->number_integer(static_cast<number_integer_t>(-1) - number);
  6218. }
  6219. case 0x3B: // Negative integer -1-n (eight-byte uint64_t follows)
  6220. {
  6221. uint64_t number;
  6222. return get_number(input_format_t::cbor, number) and sax->number_integer(static_cast<number_integer_t>(-1)
  6223. - static_cast<number_integer_t>(number));
  6224. }
  6225. // UTF-8 string (0x00..0x17 bytes follow)
  6226. case 0x60:
  6227. case 0x61:
  6228. case 0x62:
  6229. case 0x63:
  6230. case 0x64:
  6231. case 0x65:
  6232. case 0x66:
  6233. case 0x67:
  6234. case 0x68:
  6235. case 0x69:
  6236. case 0x6A:
  6237. case 0x6B:
  6238. case 0x6C:
  6239. case 0x6D:
  6240. case 0x6E:
  6241. case 0x6F:
  6242. case 0x70:
  6243. case 0x71:
  6244. case 0x72:
  6245. case 0x73:
  6246. case 0x74:
  6247. case 0x75:
  6248. case 0x76:
  6249. case 0x77:
  6250. case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
  6251. case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
  6252. case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
  6253. case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
  6254. case 0x7F: // UTF-8 string (indefinite length)
  6255. {
  6256. string_t s;
  6257. return get_cbor_string(s) and sax->string(s);
  6258. }
  6259. // array (0x00..0x17 data items follow)
  6260. case 0x80:
  6261. case 0x81:
  6262. case 0x82:
  6263. case 0x83:
  6264. case 0x84:
  6265. case 0x85:
  6266. case 0x86:
  6267. case 0x87:
  6268. case 0x88:
  6269. case 0x89:
  6270. case 0x8A:
  6271. case 0x8B:
  6272. case 0x8C:
  6273. case 0x8D:
  6274. case 0x8E:
  6275. case 0x8F:
  6276. case 0x90:
  6277. case 0x91:
  6278. case 0x92:
  6279. case 0x93:
  6280. case 0x94:
  6281. case 0x95:
  6282. case 0x96:
  6283. case 0x97:
  6284. return get_cbor_array(static_cast<std::size_t>(current & 0x1F));
  6285. case 0x98: // array (one-byte uint8_t for n follows)
  6286. {
  6287. uint8_t len;
  6288. return get_number(input_format_t::cbor, len) and get_cbor_array(static_cast<std::size_t>(len));
  6289. }
  6290. case 0x99: // array (two-byte uint16_t for n follow)
  6291. {
  6292. uint16_t len;
  6293. return get_number(input_format_t::cbor, len) and get_cbor_array(static_cast<std::size_t>(len));
  6294. }
  6295. case 0x9A: // array (four-byte uint32_t for n follow)
  6296. {
  6297. uint32_t len;
  6298. return get_number(input_format_t::cbor, len) and get_cbor_array(static_cast<std::size_t>(len));
  6299. }
  6300. case 0x9B: // array (eight-byte uint64_t for n follow)
  6301. {
  6302. uint64_t len;
  6303. return get_number(input_format_t::cbor, len) and get_cbor_array(static_cast<std::size_t>(len));
  6304. }
  6305. case 0x9F: // array (indefinite length)
  6306. return get_cbor_array(std::size_t(-1));
  6307. // map (0x00..0x17 pairs of data items follow)
  6308. case 0xA0:
  6309. case 0xA1:
  6310. case 0xA2:
  6311. case 0xA3:
  6312. case 0xA4:
  6313. case 0xA5:
  6314. case 0xA6:
  6315. case 0xA7:
  6316. case 0xA8:
  6317. case 0xA9:
  6318. case 0xAA:
  6319. case 0xAB:
  6320. case 0xAC:
  6321. case 0xAD:
  6322. case 0xAE:
  6323. case 0xAF:
  6324. case 0xB0:
  6325. case 0xB1:
  6326. case 0xB2:
  6327. case 0xB3:
  6328. case 0xB4:
  6329. case 0xB5:
  6330. case 0xB6:
  6331. case 0xB7:
  6332. return get_cbor_object(static_cast<std::size_t>(current & 0x1F));
  6333. case 0xB8: // map (one-byte uint8_t for n follows)
  6334. {
  6335. uint8_t len;
  6336. return get_number(input_format_t::cbor, len) and get_cbor_object(static_cast<std::size_t>(len));
  6337. }
  6338. case 0xB9: // map (two-byte uint16_t for n follow)
  6339. {
  6340. uint16_t len;
  6341. return get_number(input_format_t::cbor, len) and get_cbor_object(static_cast<std::size_t>(len));
  6342. }
  6343. case 0xBA: // map (four-byte uint32_t for n follow)
  6344. {
  6345. uint32_t len;
  6346. return get_number(input_format_t::cbor, len) and get_cbor_object(static_cast<std::size_t>(len));
  6347. }
  6348. case 0xBB: // map (eight-byte uint64_t for n follow)
  6349. {
  6350. uint64_t len;
  6351. return get_number(input_format_t::cbor, len) and get_cbor_object(static_cast<std::size_t>(len));
  6352. }
  6353. case 0xBF: // map (indefinite length)
  6354. return get_cbor_object(std::size_t(-1));
  6355. case 0xF4: // false
  6356. return sax->boolean(false);
  6357. case 0xF5: // true
  6358. return sax->boolean(true);
  6359. case 0xF6: // null
  6360. return sax->null();
  6361. case 0xF9: // Half-Precision Float (two-byte IEEE 754)
  6362. {
  6363. const int byte1_raw = get();
  6364. if (JSON_UNLIKELY(not unexpect_eof(input_format_t::cbor, "number")))
  6365. {
  6366. return false;
  6367. }
  6368. const int byte2_raw = get();
  6369. if (JSON_UNLIKELY(not unexpect_eof(input_format_t::cbor, "number")))
  6370. {
  6371. return false;
  6372. }
  6373. const auto byte1 = static_cast<unsigned char>(byte1_raw);
  6374. const auto byte2 = static_cast<unsigned char>(byte2_raw);
  6375. // code from RFC 7049, Appendix D, Figure 3:
  6376. // As half-precision floating-point numbers were only added
  6377. // to IEEE 754 in 2008, today's programming platforms often
  6378. // still only have limited support for them. It is very
  6379. // easy to include at least decoding support for them even
  6380. // without such support. An example of a small decoder for
  6381. // half-precision floating-point numbers in the C language
  6382. // is shown in Fig. 3.
  6383. const int half = (byte1 << 8) + byte2;
  6384. const double val = [&half]
  6385. {
  6386. const int exp = (half >> 10) & 0x1F;
  6387. const int mant = half & 0x3FF;
  6388. assert(0 <= exp and exp <= 32);
  6389. assert(0 <= mant and mant <= 1024);
  6390. switch (exp)
  6391. {
  6392. case 0:
  6393. return std::ldexp(mant, -24);
  6394. case 31:
  6395. return (mant == 0)
  6396. ? std::numeric_limits<double>::infinity()
  6397. : std::numeric_limits<double>::quiet_NaN();
  6398. default:
  6399. return std::ldexp(mant + 1024, exp - 25);
  6400. }
  6401. }();
  6402. return sax->number_float((half & 0x8000) != 0
  6403. ? static_cast<number_float_t>(-val)
  6404. : static_cast<number_float_t>(val), "");
  6405. }
  6406. case 0xFA: // Single-Precision Float (four-byte IEEE 754)
  6407. {
  6408. float number;
  6409. return get_number(input_format_t::cbor, number) and sax->number_float(static_cast<number_float_t>(number), "");
  6410. }
  6411. case 0xFB: // Double-Precision Float (eight-byte IEEE 754)
  6412. {
  6413. double number;
  6414. return get_number(input_format_t::cbor, number) and sax->number_float(static_cast<number_float_t>(number), "");
  6415. }
  6416. default: // anything else (0xFF is handled inside the other types)
  6417. {
  6418. auto last_token = get_token_string();
  6419. 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")));
  6420. }
  6421. }
  6422. }
  6423. /*!
  6424. @brief reads a CBOR string
  6425. This function first reads starting bytes to determine the expected
  6426. string length and then copies this number of bytes into a string.
  6427. Additionally, CBOR's strings with indefinite lengths are supported.
  6428. @param[out] result created string
  6429. @return whether string creation completed
  6430. */
  6431. bool get_cbor_string(string_t& result)
  6432. {
  6433. if (JSON_UNLIKELY(not unexpect_eof(input_format_t::cbor, "string")))
  6434. {
  6435. return false;
  6436. }
  6437. switch (current)
  6438. {
  6439. // UTF-8 string (0x00..0x17 bytes follow)
  6440. case 0x60:
  6441. case 0x61:
  6442. case 0x62:
  6443. case 0x63:
  6444. case 0x64:
  6445. case 0x65:
  6446. case 0x66:
  6447. case 0x67:
  6448. case 0x68:
  6449. case 0x69:
  6450. case 0x6A:
  6451. case 0x6B:
  6452. case 0x6C:
  6453. case 0x6D:
  6454. case 0x6E:
  6455. case 0x6F:
  6456. case 0x70:
  6457. case 0x71:
  6458. case 0x72:
  6459. case 0x73:
  6460. case 0x74:
  6461. case 0x75:
  6462. case 0x76:
  6463. case 0x77:
  6464. {
  6465. return get_string(input_format_t::cbor, current & 0x1F, result);
  6466. }
  6467. case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
  6468. {
  6469. uint8_t len;
  6470. return get_number(input_format_t::cbor, len) and get_string(input_format_t::cbor, len, result);
  6471. }
  6472. case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
  6473. {
  6474. uint16_t len;
  6475. return get_number(input_format_t::cbor, len) and get_string(input_format_t::cbor, len, result);
  6476. }
  6477. case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
  6478. {
  6479. uint32_t len;
  6480. return get_number(input_format_t::cbor, len) and get_string(input_format_t::cbor, len, result);
  6481. }
  6482. case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
  6483. {
  6484. uint64_t len;
  6485. return get_number(input_format_t::cbor, len) and get_string(input_format_t::cbor, len, result);
  6486. }
  6487. case 0x7F: // UTF-8 string (indefinite length)
  6488. {
  6489. while (get() != 0xFF)
  6490. {
  6491. string_t chunk;
  6492. if (not get_cbor_string(chunk))
  6493. {
  6494. return false;
  6495. }
  6496. result.append(chunk);
  6497. }
  6498. return true;
  6499. }
  6500. default:
  6501. {
  6502. auto last_token = get_token_string();
  6503. 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")));
  6504. }
  6505. }
  6506. }
  6507. /*!
  6508. @param[in] len the length of the array or std::size_t(-1) for an
  6509. array of indefinite size
  6510. @return whether array creation completed
  6511. */
  6512. bool get_cbor_array(const std::size_t len)
  6513. {
  6514. if (JSON_UNLIKELY(not sax->start_array(len)))
  6515. {
  6516. return false;
  6517. }
  6518. if (len != std::size_t(-1))
  6519. {
  6520. for (std::size_t i = 0; i < len; ++i)
  6521. {
  6522. if (JSON_UNLIKELY(not parse_cbor_internal()))
  6523. {
  6524. return false;
  6525. }
  6526. }
  6527. }
  6528. else
  6529. {
  6530. while (get() != 0xFF)
  6531. {
  6532. if (JSON_UNLIKELY(not parse_cbor_internal(false)))
  6533. {
  6534. return false;
  6535. }
  6536. }
  6537. }
  6538. return sax->end_array();
  6539. }
  6540. /*!
  6541. @param[in] len the length of the object or std::size_t(-1) for an
  6542. object of indefinite size
  6543. @return whether object creation completed
  6544. */
  6545. bool get_cbor_object(const std::size_t len)
  6546. {
  6547. if (not JSON_UNLIKELY(sax->start_object(len)))
  6548. {
  6549. return false;
  6550. }
  6551. string_t key;
  6552. if (len != std::size_t(-1))
  6553. {
  6554. for (std::size_t i = 0; i < len; ++i)
  6555. {
  6556. get();
  6557. if (JSON_UNLIKELY(not get_cbor_string(key) or not sax->key(key)))
  6558. {
  6559. return false;
  6560. }
  6561. if (JSON_UNLIKELY(not parse_cbor_internal()))
  6562. {
  6563. return false;
  6564. }
  6565. key.clear();
  6566. }
  6567. }
  6568. else
  6569. {
  6570. while (get() != 0xFF)
  6571. {
  6572. if (JSON_UNLIKELY(not get_cbor_string(key) or not sax->key(key)))
  6573. {
  6574. return false;
  6575. }
  6576. if (JSON_UNLIKELY(not parse_cbor_internal()))
  6577. {
  6578. return false;
  6579. }
  6580. key.clear();
  6581. }
  6582. }
  6583. return sax->end_object();
  6584. }
  6585. /////////////
  6586. // MsgPack //
  6587. /////////////
  6588. /*!
  6589. @return whether a valid MessagePack value was passed to the SAX parser
  6590. */
  6591. bool parse_msgpack_internal()
  6592. {
  6593. switch (get())
  6594. {
  6595. // EOF
  6596. case std::char_traits<char>::eof():
  6597. return unexpect_eof(input_format_t::msgpack, "value");
  6598. // positive fixint
  6599. case 0x00:
  6600. case 0x01:
  6601. case 0x02:
  6602. case 0x03:
  6603. case 0x04:
  6604. case 0x05:
  6605. case 0x06:
  6606. case 0x07:
  6607. case 0x08:
  6608. case 0x09:
  6609. case 0x0A:
  6610. case 0x0B:
  6611. case 0x0C:
  6612. case 0x0D:
  6613. case 0x0E:
  6614. case 0x0F:
  6615. case 0x10:
  6616. case 0x11:
  6617. case 0x12:
  6618. case 0x13:
  6619. case 0x14:
  6620. case 0x15:
  6621. case 0x16:
  6622. case 0x17:
  6623. case 0x18:
  6624. case 0x19:
  6625. case 0x1A:
  6626. case 0x1B:
  6627. case 0x1C:
  6628. case 0x1D:
  6629. case 0x1E:
  6630. case 0x1F:
  6631. case 0x20:
  6632. case 0x21:
  6633. case 0x22:
  6634. case 0x23:
  6635. case 0x24:
  6636. case 0x25:
  6637. case 0x26:
  6638. case 0x27:
  6639. case 0x28:
  6640. case 0x29:
  6641. case 0x2A:
  6642. case 0x2B:
  6643. case 0x2C:
  6644. case 0x2D:
  6645. case 0x2E:
  6646. case 0x2F:
  6647. case 0x30:
  6648. case 0x31:
  6649. case 0x32:
  6650. case 0x33:
  6651. case 0x34:
  6652. case 0x35:
  6653. case 0x36:
  6654. case 0x37:
  6655. case 0x38:
  6656. case 0x39:
  6657. case 0x3A:
  6658. case 0x3B:
  6659. case 0x3C:
  6660. case 0x3D:
  6661. case 0x3E:
  6662. case 0x3F:
  6663. case 0x40:
  6664. case 0x41:
  6665. case 0x42:
  6666. case 0x43:
  6667. case 0x44:
  6668. case 0x45:
  6669. case 0x46:
  6670. case 0x47:
  6671. case 0x48:
  6672. case 0x49:
  6673. case 0x4A:
  6674. case 0x4B:
  6675. case 0x4C:
  6676. case 0x4D:
  6677. case 0x4E:
  6678. case 0x4F:
  6679. case 0x50:
  6680. case 0x51:
  6681. case 0x52:
  6682. case 0x53:
  6683. case 0x54:
  6684. case 0x55:
  6685. case 0x56:
  6686. case 0x57:
  6687. case 0x58:
  6688. case 0x59:
  6689. case 0x5A:
  6690. case 0x5B:
  6691. case 0x5C:
  6692. case 0x5D:
  6693. case 0x5E:
  6694. case 0x5F:
  6695. case 0x60:
  6696. case 0x61:
  6697. case 0x62:
  6698. case 0x63:
  6699. case 0x64:
  6700. case 0x65:
  6701. case 0x66:
  6702. case 0x67:
  6703. case 0x68:
  6704. case 0x69:
  6705. case 0x6A:
  6706. case 0x6B:
  6707. case 0x6C:
  6708. case 0x6D:
  6709. case 0x6E:
  6710. case 0x6F:
  6711. case 0x70:
  6712. case 0x71:
  6713. case 0x72:
  6714. case 0x73:
  6715. case 0x74:
  6716. case 0x75:
  6717. case 0x76:
  6718. case 0x77:
  6719. case 0x78:
  6720. case 0x79:
  6721. case 0x7A:
  6722. case 0x7B:
  6723. case 0x7C:
  6724. case 0x7D:
  6725. case 0x7E:
  6726. case 0x7F:
  6727. return sax->number_unsigned(static_cast<number_unsigned_t>(current));
  6728. // fixmap
  6729. case 0x80:
  6730. case 0x81:
  6731. case 0x82:
  6732. case 0x83:
  6733. case 0x84:
  6734. case 0x85:
  6735. case 0x86:
  6736. case 0x87:
  6737. case 0x88:
  6738. case 0x89:
  6739. case 0x8A:
  6740. case 0x8B:
  6741. case 0x8C:
  6742. case 0x8D:
  6743. case 0x8E:
  6744. case 0x8F:
  6745. return get_msgpack_object(static_cast<std::size_t>(current & 0x0F));
  6746. // fixarray
  6747. case 0x90:
  6748. case 0x91:
  6749. case 0x92:
  6750. case 0x93:
  6751. case 0x94:
  6752. case 0x95:
  6753. case 0x96:
  6754. case 0x97:
  6755. case 0x98:
  6756. case 0x99:
  6757. case 0x9A:
  6758. case 0x9B:
  6759. case 0x9C:
  6760. case 0x9D:
  6761. case 0x9E:
  6762. case 0x9F:
  6763. return get_msgpack_array(static_cast<std::size_t>(current & 0x0F));
  6764. // fixstr
  6765. case 0xA0:
  6766. case 0xA1:
  6767. case 0xA2:
  6768. case 0xA3:
  6769. case 0xA4:
  6770. case 0xA5:
  6771. case 0xA6:
  6772. case 0xA7:
  6773. case 0xA8:
  6774. case 0xA9:
  6775. case 0xAA:
  6776. case 0xAB:
  6777. case 0xAC:
  6778. case 0xAD:
  6779. case 0xAE:
  6780. case 0xAF:
  6781. case 0xB0:
  6782. case 0xB1:
  6783. case 0xB2:
  6784. case 0xB3:
  6785. case 0xB4:
  6786. case 0xB5:
  6787. case 0xB6:
  6788. case 0xB7:
  6789. case 0xB8:
  6790. case 0xB9:
  6791. case 0xBA:
  6792. case 0xBB:
  6793. case 0xBC:
  6794. case 0xBD:
  6795. case 0xBE:
  6796. case 0xBF:
  6797. {
  6798. string_t s;
  6799. return get_msgpack_string(s) and sax->string(s);
  6800. }
  6801. case 0xC0: // nil
  6802. return sax->null();
  6803. case 0xC2: // false
  6804. return sax->boolean(false);
  6805. case 0xC3: // true
  6806. return sax->boolean(true);
  6807. case 0xCA: // float 32
  6808. {
  6809. float number;
  6810. return get_number(input_format_t::msgpack, number) and sax->number_float(static_cast<number_float_t>(number), "");
  6811. }
  6812. case 0xCB: // float 64
  6813. {
  6814. double number;
  6815. return get_number(input_format_t::msgpack, number) and sax->number_float(static_cast<number_float_t>(number), "");
  6816. }
  6817. case 0xCC: // uint 8
  6818. {
  6819. uint8_t number;
  6820. return get_number(input_format_t::msgpack, number) and sax->number_unsigned(number);
  6821. }
  6822. case 0xCD: // uint 16
  6823. {
  6824. uint16_t number;
  6825. return get_number(input_format_t::msgpack, number) and sax->number_unsigned(number);
  6826. }
  6827. case 0xCE: // uint 32
  6828. {
  6829. uint32_t number;
  6830. return get_number(input_format_t::msgpack, number) and sax->number_unsigned(number);
  6831. }
  6832. case 0xCF: // uint 64
  6833. {
  6834. uint64_t number;
  6835. return get_number(input_format_t::msgpack, number) and sax->number_unsigned(number);
  6836. }
  6837. case 0xD0: // int 8
  6838. {
  6839. int8_t number;
  6840. return get_number(input_format_t::msgpack, number) and sax->number_integer(number);
  6841. }
  6842. case 0xD1: // int 16
  6843. {
  6844. int16_t number;
  6845. return get_number(input_format_t::msgpack, number) and sax->number_integer(number);
  6846. }
  6847. case 0xD2: // int 32
  6848. {
  6849. int32_t number;
  6850. return get_number(input_format_t::msgpack, number) and sax->number_integer(number);
  6851. }
  6852. case 0xD3: // int 64
  6853. {
  6854. int64_t number;
  6855. return get_number(input_format_t::msgpack, number) and sax->number_integer(number);
  6856. }
  6857. case 0xD9: // str 8
  6858. case 0xDA: // str 16
  6859. case 0xDB: // str 32
  6860. {
  6861. string_t s;
  6862. return get_msgpack_string(s) and sax->string(s);
  6863. }
  6864. case 0xDC: // array 16
  6865. {
  6866. uint16_t len;
  6867. return get_number(input_format_t::msgpack, len) and get_msgpack_array(static_cast<std::size_t>(len));
  6868. }
  6869. case 0xDD: // array 32
  6870. {
  6871. uint32_t len;
  6872. return get_number(input_format_t::msgpack, len) and get_msgpack_array(static_cast<std::size_t>(len));
  6873. }
  6874. case 0xDE: // map 16
  6875. {
  6876. uint16_t len;
  6877. return get_number(input_format_t::msgpack, len) and get_msgpack_object(static_cast<std::size_t>(len));
  6878. }
  6879. case 0xDF: // map 32
  6880. {
  6881. uint32_t len;
  6882. return get_number(input_format_t::msgpack, len) and get_msgpack_object(static_cast<std::size_t>(len));
  6883. }
  6884. // negative fixint
  6885. case 0xE0:
  6886. case 0xE1:
  6887. case 0xE2:
  6888. case 0xE3:
  6889. case 0xE4:
  6890. case 0xE5:
  6891. case 0xE6:
  6892. case 0xE7:
  6893. case 0xE8:
  6894. case 0xE9:
  6895. case 0xEA:
  6896. case 0xEB:
  6897. case 0xEC:
  6898. case 0xED:
  6899. case 0xEE:
  6900. case 0xEF:
  6901. case 0xF0:
  6902. case 0xF1:
  6903. case 0xF2:
  6904. case 0xF3:
  6905. case 0xF4:
  6906. case 0xF5:
  6907. case 0xF6:
  6908. case 0xF7:
  6909. case 0xF8:
  6910. case 0xF9:
  6911. case 0xFA:
  6912. case 0xFB:
  6913. case 0xFC:
  6914. case 0xFD:
  6915. case 0xFE:
  6916. case 0xFF:
  6917. return sax->number_integer(static_cast<int8_t>(current));
  6918. default: // anything else
  6919. {
  6920. auto last_token = get_token_string();
  6921. 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")));
  6922. }
  6923. }
  6924. }
  6925. /*!
  6926. @brief reads a MessagePack string
  6927. This function first reads starting bytes to determine the expected
  6928. string length and then copies this number of bytes into a string.
  6929. @param[out] result created string
  6930. @return whether string creation completed
  6931. */
  6932. bool get_msgpack_string(string_t& result)
  6933. {
  6934. if (JSON_UNLIKELY(not unexpect_eof(input_format_t::msgpack, "string")))
  6935. {
  6936. return false;
  6937. }
  6938. switch (current)
  6939. {
  6940. // fixstr
  6941. case 0xA0:
  6942. case 0xA1:
  6943. case 0xA2:
  6944. case 0xA3:
  6945. case 0xA4:
  6946. case 0xA5:
  6947. case 0xA6:
  6948. case 0xA7:
  6949. case 0xA8:
  6950. case 0xA9:
  6951. case 0xAA:
  6952. case 0xAB:
  6953. case 0xAC:
  6954. case 0xAD:
  6955. case 0xAE:
  6956. case 0xAF:
  6957. case 0xB0:
  6958. case 0xB1:
  6959. case 0xB2:
  6960. case 0xB3:
  6961. case 0xB4:
  6962. case 0xB5:
  6963. case 0xB6:
  6964. case 0xB7:
  6965. case 0xB8:
  6966. case 0xB9:
  6967. case 0xBA:
  6968. case 0xBB:
  6969. case 0xBC:
  6970. case 0xBD:
  6971. case 0xBE:
  6972. case 0xBF:
  6973. {
  6974. return get_string(input_format_t::msgpack, current & 0x1F, result);
  6975. }
  6976. case 0xD9: // str 8
  6977. {
  6978. uint8_t len;
  6979. return get_number(input_format_t::msgpack, len) and get_string(input_format_t::msgpack, len, result);
  6980. }
  6981. case 0xDA: // str 16
  6982. {
  6983. uint16_t len;
  6984. return get_number(input_format_t::msgpack, len) and get_string(input_format_t::msgpack, len, result);
  6985. }
  6986. case 0xDB: // str 32
  6987. {
  6988. uint32_t len;
  6989. return get_number(input_format_t::msgpack, len) and get_string(input_format_t::msgpack, len, result);
  6990. }
  6991. default:
  6992. {
  6993. auto last_token = get_token_string();
  6994. 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")));
  6995. }
  6996. }
  6997. }
  6998. /*!
  6999. @param[in] len the length of the array
  7000. @return whether array creation completed
  7001. */
  7002. bool get_msgpack_array(const std::size_t len)
  7003. {
  7004. if (JSON_UNLIKELY(not sax->start_array(len)))
  7005. {
  7006. return false;
  7007. }
  7008. for (std::size_t i = 0; i < len; ++i)
  7009. {
  7010. if (JSON_UNLIKELY(not parse_msgpack_internal()))
  7011. {
  7012. return false;
  7013. }
  7014. }
  7015. return sax->end_array();
  7016. }
  7017. /*!
  7018. @param[in] len the length of the object
  7019. @return whether object creation completed
  7020. */
  7021. bool get_msgpack_object(const std::size_t len)
  7022. {
  7023. if (JSON_UNLIKELY(not sax->start_object(len)))
  7024. {
  7025. return false;
  7026. }
  7027. string_t key;
  7028. for (std::size_t i = 0; i < len; ++i)
  7029. {
  7030. get();
  7031. if (JSON_UNLIKELY(not get_msgpack_string(key) or not sax->key(key)))
  7032. {
  7033. return false;
  7034. }
  7035. if (JSON_UNLIKELY(not parse_msgpack_internal()))
  7036. {
  7037. return false;
  7038. }
  7039. key.clear();
  7040. }
  7041. return sax->end_object();
  7042. }
  7043. ////////////
  7044. // UBJSON //
  7045. ////////////
  7046. /*!
  7047. @param[in] get_char whether a new character should be retrieved from the
  7048. input (true, default) or whether the last read
  7049. character should be considered instead
  7050. @return whether a valid UBJSON value was passed to the SAX parser
  7051. */
  7052. bool parse_ubjson_internal(const bool get_char = true)
  7053. {
  7054. return get_ubjson_value(get_char ? get_ignore_noop() : current);
  7055. }
  7056. /*!
  7057. @brief reads a UBJSON string
  7058. This function is either called after reading the 'S' byte explicitly
  7059. indicating a string, or in case of an object key where the 'S' byte can be
  7060. left out.
  7061. @param[out] result created string
  7062. @param[in] get_char whether a new character should be retrieved from the
  7063. input (true, default) or whether the last read
  7064. character should be considered instead
  7065. @return whether string creation completed
  7066. */
  7067. bool get_ubjson_string(string_t& result, const bool get_char = true)
  7068. {
  7069. if (get_char)
  7070. {
  7071. get(); // TODO: may we ignore N here?
  7072. }
  7073. if (JSON_UNLIKELY(not unexpect_eof(input_format_t::ubjson, "value")))
  7074. {
  7075. return false;
  7076. }
  7077. switch (current)
  7078. {
  7079. case 'U':
  7080. {
  7081. uint8_t len;
  7082. return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result);
  7083. }
  7084. case 'i':
  7085. {
  7086. int8_t len;
  7087. return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result);
  7088. }
  7089. case 'I':
  7090. {
  7091. int16_t len;
  7092. return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result);
  7093. }
  7094. case 'l':
  7095. {
  7096. int32_t len;
  7097. return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result);
  7098. }
  7099. case 'L':
  7100. {
  7101. int64_t len;
  7102. return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result);
  7103. }
  7104. default:
  7105. auto last_token = get_token_string();
  7106. 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")));
  7107. }
  7108. }
  7109. /*!
  7110. @param[out] result determined size
  7111. @return whether size determination completed
  7112. */
  7113. bool get_ubjson_size_value(std::size_t& result)
  7114. {
  7115. switch (get_ignore_noop())
  7116. {
  7117. case 'U':
  7118. {
  7119. uint8_t number;
  7120. if (JSON_UNLIKELY(not get_number(input_format_t::ubjson, number)))
  7121. {
  7122. return false;
  7123. }
  7124. result = static_cast<std::size_t>(number);
  7125. return true;
  7126. }
  7127. case 'i':
  7128. {
  7129. int8_t number;
  7130. if (JSON_UNLIKELY(not get_number(input_format_t::ubjson, number)))
  7131. {
  7132. return false;
  7133. }
  7134. result = static_cast<std::size_t>(number);
  7135. return true;
  7136. }
  7137. case 'I':
  7138. {
  7139. int16_t number;
  7140. if (JSON_UNLIKELY(not get_number(input_format_t::ubjson, number)))
  7141. {
  7142. return false;
  7143. }
  7144. result = static_cast<std::size_t>(number);
  7145. return true;
  7146. }
  7147. case 'l':
  7148. {
  7149. int32_t number;
  7150. if (JSON_UNLIKELY(not get_number(input_format_t::ubjson, number)))
  7151. {
  7152. return false;
  7153. }
  7154. result = static_cast<std::size_t>(number);
  7155. return true;
  7156. }
  7157. case 'L':
  7158. {
  7159. int64_t number;
  7160. if (JSON_UNLIKELY(not get_number(input_format_t::ubjson, number)))
  7161. {
  7162. return false;
  7163. }
  7164. result = static_cast<std::size_t>(number);
  7165. return true;
  7166. }
  7167. default:
  7168. {
  7169. auto last_token = get_token_string();
  7170. 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")));
  7171. }
  7172. }
  7173. }
  7174. /*!
  7175. @brief determine the type and size for a container
  7176. In the optimized UBJSON format, a type and a size can be provided to allow
  7177. for a more compact representation.
  7178. @param[out] result pair of the size and the type
  7179. @return whether pair creation completed
  7180. */
  7181. bool get_ubjson_size_type(std::pair<std::size_t, int>& result)
  7182. {
  7183. result.first = string_t::npos; // size
  7184. result.second = 0; // type
  7185. get_ignore_noop();
  7186. if (current == '$')
  7187. {
  7188. result.second = get(); // must not ignore 'N', because 'N' maybe the type
  7189. if (JSON_UNLIKELY(not unexpect_eof(input_format_t::ubjson, "type")))
  7190. {
  7191. return false;
  7192. }
  7193. get_ignore_noop();
  7194. if (JSON_UNLIKELY(current != '#'))
  7195. {
  7196. if (JSON_UNLIKELY(not unexpect_eof(input_format_t::ubjson, "value")))
  7197. {
  7198. return false;
  7199. }
  7200. auto last_token = get_token_string();
  7201. 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")));
  7202. }
  7203. return get_ubjson_size_value(result.first);
  7204. }
  7205. else if (current == '#')
  7206. {
  7207. return get_ubjson_size_value(result.first);
  7208. }
  7209. return true;
  7210. }
  7211. /*!
  7212. @param prefix the previously read or set type prefix
  7213. @return whether value creation completed
  7214. */
  7215. bool get_ubjson_value(const int prefix)
  7216. {
  7217. switch (prefix)
  7218. {
  7219. case std::char_traits<char>::eof(): // EOF
  7220. return unexpect_eof(input_format_t::ubjson, "value");
  7221. case 'T': // true
  7222. return sax->boolean(true);
  7223. case 'F': // false
  7224. return sax->boolean(false);
  7225. case 'Z': // null
  7226. return sax->null();
  7227. case 'U':
  7228. {
  7229. uint8_t number;
  7230. return get_number(input_format_t::ubjson, number) and sax->number_unsigned(number);
  7231. }
  7232. case 'i':
  7233. {
  7234. int8_t number;
  7235. return get_number(input_format_t::ubjson, number) and sax->number_integer(number);
  7236. }
  7237. case 'I':
  7238. {
  7239. int16_t number;
  7240. return get_number(input_format_t::ubjson, number) and sax->number_integer(number);
  7241. }
  7242. case 'l':
  7243. {
  7244. int32_t number;
  7245. return get_number(input_format_t::ubjson, number) and sax->number_integer(number);
  7246. }
  7247. case 'L':
  7248. {
  7249. int64_t number;
  7250. return get_number(input_format_t::ubjson, number) and sax->number_integer(number);
  7251. }
  7252. case 'd':
  7253. {
  7254. float number;
  7255. return get_number(input_format_t::ubjson, number) and sax->number_float(static_cast<number_float_t>(number), "");
  7256. }
  7257. case 'D':
  7258. {
  7259. double number;
  7260. return get_number(input_format_t::ubjson, number) and sax->number_float(static_cast<number_float_t>(number), "");
  7261. }
  7262. case 'C': // char
  7263. {
  7264. get();
  7265. if (JSON_UNLIKELY(not unexpect_eof(input_format_t::ubjson, "char")))
  7266. {
  7267. return false;
  7268. }
  7269. if (JSON_UNLIKELY(current > 127))
  7270. {
  7271. auto last_token = get_token_string();
  7272. 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")));
  7273. }
  7274. string_t s(1, static_cast<char>(current));
  7275. return sax->string(s);
  7276. }
  7277. case 'S': // string
  7278. {
  7279. string_t s;
  7280. return get_ubjson_string(s) and sax->string(s);
  7281. }
  7282. case '[': // array
  7283. return get_ubjson_array();
  7284. case '{': // object
  7285. return get_ubjson_object();
  7286. default: // anything else
  7287. {
  7288. auto last_token = get_token_string();
  7289. 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")));
  7290. }
  7291. }
  7292. }
  7293. /*!
  7294. @return whether array creation completed
  7295. */
  7296. bool get_ubjson_array()
  7297. {
  7298. std::pair<std::size_t, int> size_and_type;
  7299. if (JSON_UNLIKELY(not get_ubjson_size_type(size_and_type)))
  7300. {
  7301. return false;
  7302. }
  7303. if (size_and_type.first != string_t::npos)
  7304. {
  7305. if (JSON_UNLIKELY(not sax->start_array(size_and_type.first)))
  7306. {
  7307. return false;
  7308. }
  7309. if (size_and_type.second != 0)
  7310. {
  7311. if (size_and_type.second != 'N')
  7312. {
  7313. for (std::size_t i = 0; i < size_and_type.first; ++i)
  7314. {
  7315. if (JSON_UNLIKELY(not get_ubjson_value(size_and_type.second)))
  7316. {
  7317. return false;
  7318. }
  7319. }
  7320. }
  7321. }
  7322. else
  7323. {
  7324. for (std::size_t i = 0; i < size_and_type.first; ++i)
  7325. {
  7326. if (JSON_UNLIKELY(not parse_ubjson_internal()))
  7327. {
  7328. return false;
  7329. }
  7330. }
  7331. }
  7332. }
  7333. else
  7334. {
  7335. if (JSON_UNLIKELY(not sax->start_array(std::size_t(-1))))
  7336. {
  7337. return false;
  7338. }
  7339. while (current != ']')
  7340. {
  7341. if (JSON_UNLIKELY(not parse_ubjson_internal(false)))
  7342. {
  7343. return false;
  7344. }
  7345. get_ignore_noop();
  7346. }
  7347. }
  7348. return sax->end_array();
  7349. }
  7350. /*!
  7351. @return whether object creation completed
  7352. */
  7353. bool get_ubjson_object()
  7354. {
  7355. std::pair<std::size_t, int> size_and_type;
  7356. if (JSON_UNLIKELY(not get_ubjson_size_type(size_and_type)))
  7357. {
  7358. return false;
  7359. }
  7360. string_t key;
  7361. if (size_and_type.first != string_t::npos)
  7362. {
  7363. if (JSON_UNLIKELY(not sax->start_object(size_and_type.first)))
  7364. {
  7365. return false;
  7366. }
  7367. if (size_and_type.second != 0)
  7368. {
  7369. for (std::size_t i = 0; i < size_and_type.first; ++i)
  7370. {
  7371. if (JSON_UNLIKELY(not get_ubjson_string(key) or not sax->key(key)))
  7372. {
  7373. return false;
  7374. }
  7375. if (JSON_UNLIKELY(not get_ubjson_value(size_and_type.second)))
  7376. {
  7377. return false;
  7378. }
  7379. key.clear();
  7380. }
  7381. }
  7382. else
  7383. {
  7384. for (std::size_t i = 0; i < size_and_type.first; ++i)
  7385. {
  7386. if (JSON_UNLIKELY(not get_ubjson_string(key) or not sax->key(key)))
  7387. {
  7388. return false;
  7389. }
  7390. if (JSON_UNLIKELY(not parse_ubjson_internal()))
  7391. {
  7392. return false;
  7393. }
  7394. key.clear();
  7395. }
  7396. }
  7397. }
  7398. else
  7399. {
  7400. if (JSON_UNLIKELY(not sax->start_object(std::size_t(-1))))
  7401. {
  7402. return false;
  7403. }
  7404. while (current != '}')
  7405. {
  7406. if (JSON_UNLIKELY(not get_ubjson_string(key, false) or not sax->key(key)))
  7407. {
  7408. return false;
  7409. }
  7410. if (JSON_UNLIKELY(not parse_ubjson_internal()))
  7411. {
  7412. return false;
  7413. }
  7414. get_ignore_noop();
  7415. key.clear();
  7416. }
  7417. }
  7418. return sax->end_object();
  7419. }
  7420. ///////////////////////
  7421. // Utility functions //
  7422. ///////////////////////
  7423. /*!
  7424. @brief get next character from the input
  7425. This function provides the interface to the used input adapter. It does
  7426. not throw in case the input reached EOF, but returns a -'ve valued
  7427. `std::char_traits<char>::eof()` in that case.
  7428. @return character read from the input
  7429. */
  7430. int get()
  7431. {
  7432. ++chars_read;
  7433. return (current = ia->get_character());
  7434. }
  7435. /*!
  7436. @return character read from the input after ignoring all 'N' entries
  7437. */
  7438. int get_ignore_noop()
  7439. {
  7440. do
  7441. {
  7442. get();
  7443. }
  7444. while (current == 'N');
  7445. return current;
  7446. }
  7447. /*
  7448. @brief read a number from the input
  7449. @tparam NumberType the type of the number
  7450. @param[in] format the current format (for diagnostics)
  7451. @param[out] result number of type @a NumberType
  7452. @return whether conversion completed
  7453. @note This function needs to respect the system's endianess, because
  7454. bytes in CBOR, MessagePack, and UBJSON are stored in network order
  7455. (big endian) and therefore need reordering on little endian systems.
  7456. */
  7457. template<typename NumberType, bool InputIsLittleEndian = false>
  7458. bool get_number(const input_format_t format, NumberType& result)
  7459. {
  7460. // step 1: read input into array with system's byte order
  7461. std::array<uint8_t, sizeof(NumberType)> vec;
  7462. for (std::size_t i = 0; i < sizeof(NumberType); ++i)
  7463. {
  7464. get();
  7465. if (JSON_UNLIKELY(not unexpect_eof(format, "number")))
  7466. {
  7467. return false;
  7468. }
  7469. // reverse byte order prior to conversion if necessary
  7470. if (is_little_endian && !InputIsLittleEndian)
  7471. {
  7472. vec[sizeof(NumberType) - i - 1] = static_cast<uint8_t>(current);
  7473. }
  7474. else
  7475. {
  7476. vec[i] = static_cast<uint8_t>(current); // LCOV_EXCL_LINE
  7477. }
  7478. }
  7479. // step 2: convert array into number of type T and return
  7480. std::memcpy(&result, vec.data(), sizeof(NumberType));
  7481. return true;
  7482. }
  7483. /*!
  7484. @brief create a string by reading characters from the input
  7485. @tparam NumberType the type of the number
  7486. @param[in] format the current format (for diagnostics)
  7487. @param[in] len number of characters to read
  7488. @param[out] result string created by reading @a len bytes
  7489. @return whether string creation completed
  7490. @note We can not reserve @a len bytes for the result, because @a len
  7491. may be too large. Usually, @ref unexpect_eof() detects the end of
  7492. the input before we run out of string memory.
  7493. */
  7494. template<typename NumberType>
  7495. bool get_string(const input_format_t format,
  7496. const NumberType len,
  7497. string_t& result)
  7498. {
  7499. bool success = true;
  7500. std::generate_n(std::back_inserter(result), len, [this, &success, &format]()
  7501. {
  7502. get();
  7503. if (JSON_UNLIKELY(not unexpect_eof(format, "string")))
  7504. {
  7505. success = false;
  7506. }
  7507. return static_cast<char>(current);
  7508. });
  7509. return success;
  7510. }
  7511. /*!
  7512. @param[in] format the current format (for diagnostics)
  7513. @param[in] context further context information (for diagnostics)
  7514. @return whether the last read character is not EOF
  7515. */
  7516. bool unexpect_eof(const input_format_t format, const char* context) const
  7517. {
  7518. if (JSON_UNLIKELY(current == std::char_traits<char>::eof()))
  7519. {
  7520. return sax->parse_error(chars_read, "<end of file>",
  7521. parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context)));
  7522. }
  7523. return true;
  7524. }
  7525. /*!
  7526. @return a string representation of the last read byte
  7527. */
  7528. std::string get_token_string() const
  7529. {
  7530. char cr[3];
  7531. (std::snprintf)(cr, 3, "%.2hhX", static_cast<unsigned char>(current));
  7532. return std::string{cr};
  7533. }
  7534. /*!
  7535. @param[in] format the current format
  7536. @param[in] detail a detailed error message
  7537. @param[in] context further contect information
  7538. @return a message string to use in the parse_error exceptions
  7539. */
  7540. std::string exception_message(const input_format_t format,
  7541. const std::string& detail,
  7542. const std::string& context) const
  7543. {
  7544. std::string error_msg = "syntax error while parsing ";
  7545. switch (format)
  7546. {
  7547. case input_format_t::cbor:
  7548. error_msg += "CBOR";
  7549. break;
  7550. case input_format_t::msgpack:
  7551. error_msg += "MessagePack";
  7552. break;
  7553. case input_format_t::ubjson:
  7554. error_msg += "UBJSON";
  7555. break;
  7556. case input_format_t::bson:
  7557. error_msg += "BSON";
  7558. break;
  7559. // LCOV_EXCL_START
  7560. default:
  7561. assert(false);
  7562. // LCOV_EXCL_STOP
  7563. }
  7564. return error_msg + " " + context + ": " + detail;
  7565. }
  7566. private:
  7567. /// input adapter
  7568. input_adapter_t ia = nullptr;
  7569. /// the current character
  7570. int current = std::char_traits<char>::eof();
  7571. /// the number of characters read
  7572. std::size_t chars_read = 0;
  7573. /// whether we can assume little endianess
  7574. const bool is_little_endian = little_endianess();
  7575. /// the SAX parser
  7576. json_sax_t* sax = nullptr;
  7577. };
  7578. } // namespace detail
  7579. } // namespace nlohmann
  7580. // #include <nlohmann/detail/output/binary_writer.hpp>
  7581. #include <algorithm> // reverse
  7582. #include <array> // array
  7583. #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
  7584. #include <cstring> // memcpy
  7585. #include <limits> // numeric_limits
  7586. // #include <nlohmann/detail/input/binary_reader.hpp>
  7587. // #include <nlohmann/detail/output/output_adapters.hpp>
  7588. namespace nlohmann
  7589. {
  7590. namespace detail
  7591. {
  7592. ///////////////////
  7593. // binary writer //
  7594. ///////////////////
  7595. /*!
  7596. @brief serialization to CBOR and MessagePack values
  7597. */
  7598. template<typename BasicJsonType, typename CharType>
  7599. class binary_writer
  7600. {
  7601. using string_t = typename BasicJsonType::string_t;
  7602. public:
  7603. /*!
  7604. @brief create a binary writer
  7605. @param[in] adapter output adapter to write to
  7606. */
  7607. explicit binary_writer(output_adapter_t<CharType> adapter) : oa(adapter)
  7608. {
  7609. assert(oa);
  7610. }
  7611. /*!
  7612. @param[in] j JSON value to serialize
  7613. @pre j.type() == value_t::object
  7614. */
  7615. void write_bson(const BasicJsonType& j)
  7616. {
  7617. switch (j.type())
  7618. {
  7619. case value_t::object:
  7620. {
  7621. write_bson_object(*j.m_value.object);
  7622. break;
  7623. }
  7624. default:
  7625. {
  7626. JSON_THROW(type_error::create(317, "to serialize to BSON, top-level type must be object, but is " + std::string(j.type_name())));
  7627. }
  7628. }
  7629. }
  7630. /*!
  7631. @param[in] j JSON value to serialize
  7632. */
  7633. void write_cbor(const BasicJsonType& j)
  7634. {
  7635. switch (j.type())
  7636. {
  7637. case value_t::null:
  7638. {
  7639. oa->write_character(to_char_type(0xF6));
  7640. break;
  7641. }
  7642. case value_t::boolean:
  7643. {
  7644. oa->write_character(j.m_value.boolean
  7645. ? to_char_type(0xF5)
  7646. : to_char_type(0xF4));
  7647. break;
  7648. }
  7649. case value_t::number_integer:
  7650. {
  7651. if (j.m_value.number_integer >= 0)
  7652. {
  7653. // CBOR does not differentiate between positive signed
  7654. // integers and unsigned integers. Therefore, we used the
  7655. // code from the value_t::number_unsigned case here.
  7656. if (j.m_value.number_integer <= 0x17)
  7657. {
  7658. write_number(static_cast<uint8_t>(j.m_value.number_integer));
  7659. }
  7660. else if (j.m_value.number_integer <= (std::numeric_limits<uint8_t>::max)())
  7661. {
  7662. oa->write_character(to_char_type(0x18));
  7663. write_number(static_cast<uint8_t>(j.m_value.number_integer));
  7664. }
  7665. else if (j.m_value.number_integer <= (std::numeric_limits<uint16_t>::max)())
  7666. {
  7667. oa->write_character(to_char_type(0x19));
  7668. write_number(static_cast<uint16_t>(j.m_value.number_integer));
  7669. }
  7670. else if (j.m_value.number_integer <= (std::numeric_limits<uint32_t>::max)())
  7671. {
  7672. oa->write_character(to_char_type(0x1A));
  7673. write_number(static_cast<uint32_t>(j.m_value.number_integer));
  7674. }
  7675. else
  7676. {
  7677. oa->write_character(to_char_type(0x1B));
  7678. write_number(static_cast<uint64_t>(j.m_value.number_integer));
  7679. }
  7680. }
  7681. else
  7682. {
  7683. // The conversions below encode the sign in the first
  7684. // byte, and the value is converted to a positive number.
  7685. const auto positive_number = -1 - j.m_value.number_integer;
  7686. if (j.m_value.number_integer >= -24)
  7687. {
  7688. write_number(static_cast<uint8_t>(0x20 + positive_number));
  7689. }
  7690. else if (positive_number <= (std::numeric_limits<uint8_t>::max)())
  7691. {
  7692. oa->write_character(to_char_type(0x38));
  7693. write_number(static_cast<uint8_t>(positive_number));
  7694. }
  7695. else if (positive_number <= (std::numeric_limits<uint16_t>::max)())
  7696. {
  7697. oa->write_character(to_char_type(0x39));
  7698. write_number(static_cast<uint16_t>(positive_number));
  7699. }
  7700. else if (positive_number <= (std::numeric_limits<uint32_t>::max)())
  7701. {
  7702. oa->write_character(to_char_type(0x3A));
  7703. write_number(static_cast<uint32_t>(positive_number));
  7704. }
  7705. else
  7706. {
  7707. oa->write_character(to_char_type(0x3B));
  7708. write_number(static_cast<uint64_t>(positive_number));
  7709. }
  7710. }
  7711. break;
  7712. }
  7713. case value_t::number_unsigned:
  7714. {
  7715. if (j.m_value.number_unsigned <= 0x17)
  7716. {
  7717. write_number(static_cast<uint8_t>(j.m_value.number_unsigned));
  7718. }
  7719. else if (j.m_value.number_unsigned <= (std::numeric_limits<uint8_t>::max)())
  7720. {
  7721. oa->write_character(to_char_type(0x18));
  7722. write_number(static_cast<uint8_t>(j.m_value.number_unsigned));
  7723. }
  7724. else if (j.m_value.number_unsigned <= (std::numeric_limits<uint16_t>::max)())
  7725. {
  7726. oa->write_character(to_char_type(0x19));
  7727. write_number(static_cast<uint16_t>(j.m_value.number_unsigned));
  7728. }
  7729. else if (j.m_value.number_unsigned <= (std::numeric_limits<uint32_t>::max)())
  7730. {
  7731. oa->write_character(to_char_type(0x1A));
  7732. write_number(static_cast<uint32_t>(j.m_value.number_unsigned));
  7733. }
  7734. else
  7735. {
  7736. oa->write_character(to_char_type(0x1B));
  7737. write_number(static_cast<uint64_t>(j.m_value.number_unsigned));
  7738. }
  7739. break;
  7740. }
  7741. case value_t::number_float:
  7742. {
  7743. oa->write_character(get_cbor_float_prefix(j.m_value.number_float));
  7744. write_number(j.m_value.number_float);
  7745. break;
  7746. }
  7747. case value_t::string:
  7748. {
  7749. // step 1: write control byte and the string length
  7750. const auto N = j.m_value.string->size();
  7751. if (N <= 0x17)
  7752. {
  7753. write_number(static_cast<uint8_t>(0x60 + N));
  7754. }
  7755. else if (N <= (std::numeric_limits<uint8_t>::max)())
  7756. {
  7757. oa->write_character(to_char_type(0x78));
  7758. write_number(static_cast<uint8_t>(N));
  7759. }
  7760. else if (N <= (std::numeric_limits<uint16_t>::max)())
  7761. {
  7762. oa->write_character(to_char_type(0x79));
  7763. write_number(static_cast<uint16_t>(N));
  7764. }
  7765. else if (N <= (std::numeric_limits<uint32_t>::max)())
  7766. {
  7767. oa->write_character(to_char_type(0x7A));
  7768. write_number(static_cast<uint32_t>(N));
  7769. }
  7770. // LCOV_EXCL_START
  7771. else if (N <= (std::numeric_limits<uint64_t>::max)())
  7772. {
  7773. oa->write_character(to_char_type(0x7B));
  7774. write_number(static_cast<uint64_t>(N));
  7775. }
  7776. // LCOV_EXCL_STOP
  7777. // step 2: write the string
  7778. oa->write_characters(
  7779. reinterpret_cast<const CharType*>(j.m_value.string->c_str()),
  7780. j.m_value.string->size());
  7781. break;
  7782. }
  7783. case value_t::array:
  7784. {
  7785. // step 1: write control byte and the array size
  7786. const auto N = j.m_value.array->size();
  7787. if (N <= 0x17)
  7788. {
  7789. write_number(static_cast<uint8_t>(0x80 + N));
  7790. }
  7791. else if (N <= (std::numeric_limits<uint8_t>::max)())
  7792. {
  7793. oa->write_character(to_char_type(0x98));
  7794. write_number(static_cast<uint8_t>(N));
  7795. }
  7796. else if (N <= (std::numeric_limits<uint16_t>::max)())
  7797. {
  7798. oa->write_character(to_char_type(0x99));
  7799. write_number(static_cast<uint16_t>(N));
  7800. }
  7801. else if (N <= (std::numeric_limits<uint32_t>::max)())
  7802. {
  7803. oa->write_character(to_char_type(0x9A));
  7804. write_number(static_cast<uint32_t>(N));
  7805. }
  7806. // LCOV_EXCL_START
  7807. else if (N <= (std::numeric_limits<uint64_t>::max)())
  7808. {
  7809. oa->write_character(to_char_type(0x9B));
  7810. write_number(static_cast<uint64_t>(N));
  7811. }
  7812. // LCOV_EXCL_STOP
  7813. // step 2: write each element
  7814. for (const auto& el : *j.m_value.array)
  7815. {
  7816. write_cbor(el);
  7817. }
  7818. break;
  7819. }
  7820. case value_t::object:
  7821. {
  7822. // step 1: write control byte and the object size
  7823. const auto N = j.m_value.object->size();
  7824. if (N <= 0x17)
  7825. {
  7826. write_number(static_cast<uint8_t>(0xA0 + N));
  7827. }
  7828. else if (N <= (std::numeric_limits<uint8_t>::max)())
  7829. {
  7830. oa->write_character(to_char_type(0xB8));
  7831. write_number(static_cast<uint8_t>(N));
  7832. }
  7833. else if (N <= (std::numeric_limits<uint16_t>::max)())
  7834. {
  7835. oa->write_character(to_char_type(0xB9));
  7836. write_number(static_cast<uint16_t>(N));
  7837. }
  7838. else if (N <= (std::numeric_limits<uint32_t>::max)())
  7839. {
  7840. oa->write_character(to_char_type(0xBA));
  7841. write_number(static_cast<uint32_t>(N));
  7842. }
  7843. // LCOV_EXCL_START
  7844. else if (N <= (std::numeric_limits<uint64_t>::max)())
  7845. {
  7846. oa->write_character(to_char_type(0xBB));
  7847. write_number(static_cast<uint64_t>(N));
  7848. }
  7849. // LCOV_EXCL_STOP
  7850. // step 2: write each element
  7851. for (const auto& el : *j.m_value.object)
  7852. {
  7853. write_cbor(el.first);
  7854. write_cbor(el.second);
  7855. }
  7856. break;
  7857. }
  7858. default:
  7859. break;
  7860. }
  7861. }
  7862. /*!
  7863. @param[in] j JSON value to serialize
  7864. */
  7865. void write_msgpack(const BasicJsonType& j)
  7866. {
  7867. switch (j.type())
  7868. {
  7869. case value_t::null: // nil
  7870. {
  7871. oa->write_character(to_char_type(0xC0));
  7872. break;
  7873. }
  7874. case value_t::boolean: // true and false
  7875. {
  7876. oa->write_character(j.m_value.boolean
  7877. ? to_char_type(0xC3)
  7878. : to_char_type(0xC2));
  7879. break;
  7880. }
  7881. case value_t::number_integer:
  7882. {
  7883. if (j.m_value.number_integer >= 0)
  7884. {
  7885. // MessagePack does not differentiate between positive
  7886. // signed integers and unsigned integers. Therefore, we used
  7887. // the code from the value_t::number_unsigned case here.
  7888. if (j.m_value.number_unsigned < 128)
  7889. {
  7890. // positive fixnum
  7891. write_number(static_cast<uint8_t>(j.m_value.number_integer));
  7892. }
  7893. else if (j.m_value.number_unsigned <= (std::numeric_limits<uint8_t>::max)())
  7894. {
  7895. // uint 8
  7896. oa->write_character(to_char_type(0xCC));
  7897. write_number(static_cast<uint8_t>(j.m_value.number_integer));
  7898. }
  7899. else if (j.m_value.number_unsigned <= (std::numeric_limits<uint16_t>::max)())
  7900. {
  7901. // uint 16
  7902. oa->write_character(to_char_type(0xCD));
  7903. write_number(static_cast<uint16_t>(j.m_value.number_integer));
  7904. }
  7905. else if (j.m_value.number_unsigned <= (std::numeric_limits<uint32_t>::max)())
  7906. {
  7907. // uint 32
  7908. oa->write_character(to_char_type(0xCE));
  7909. write_number(static_cast<uint32_t>(j.m_value.number_integer));
  7910. }
  7911. else if (j.m_value.number_unsigned <= (std::numeric_limits<uint64_t>::max)())
  7912. {
  7913. // uint 64
  7914. oa->write_character(to_char_type(0xCF));
  7915. write_number(static_cast<uint64_t>(j.m_value.number_integer));
  7916. }
  7917. }
  7918. else
  7919. {
  7920. if (j.m_value.number_integer >= -32)
  7921. {
  7922. // negative fixnum
  7923. write_number(static_cast<int8_t>(j.m_value.number_integer));
  7924. }
  7925. else if (j.m_value.number_integer >= (std::numeric_limits<int8_t>::min)() and
  7926. j.m_value.number_integer <= (std::numeric_limits<int8_t>::max)())
  7927. {
  7928. // int 8
  7929. oa->write_character(to_char_type(0xD0));
  7930. write_number(static_cast<int8_t>(j.m_value.number_integer));
  7931. }
  7932. else if (j.m_value.number_integer >= (std::numeric_limits<int16_t>::min)() and
  7933. j.m_value.number_integer <= (std::numeric_limits<int16_t>::max)())
  7934. {
  7935. // int 16
  7936. oa->write_character(to_char_type(0xD1));
  7937. write_number(static_cast<int16_t>(j.m_value.number_integer));
  7938. }
  7939. else if (j.m_value.number_integer >= (std::numeric_limits<int32_t>::min)() and
  7940. j.m_value.number_integer <= (std::numeric_limits<int32_t>::max)())
  7941. {
  7942. // int 32
  7943. oa->write_character(to_char_type(0xD2));
  7944. write_number(static_cast<int32_t>(j.m_value.number_integer));
  7945. }
  7946. else if (j.m_value.number_integer >= (std::numeric_limits<int64_t>::min)() and
  7947. j.m_value.number_integer <= (std::numeric_limits<int64_t>::max)())
  7948. {
  7949. // int 64
  7950. oa->write_character(to_char_type(0xD3));
  7951. write_number(static_cast<int64_t>(j.m_value.number_integer));
  7952. }
  7953. }
  7954. break;
  7955. }
  7956. case value_t::number_unsigned:
  7957. {
  7958. if (j.m_value.number_unsigned < 128)
  7959. {
  7960. // positive fixnum
  7961. write_number(static_cast<uint8_t>(j.m_value.number_integer));
  7962. }
  7963. else if (j.m_value.number_unsigned <= (std::numeric_limits<uint8_t>::max)())
  7964. {
  7965. // uint 8
  7966. oa->write_character(to_char_type(0xCC));
  7967. write_number(static_cast<uint8_t>(j.m_value.number_integer));
  7968. }
  7969. else if (j.m_value.number_unsigned <= (std::numeric_limits<uint16_t>::max)())
  7970. {
  7971. // uint 16
  7972. oa->write_character(to_char_type(0xCD));
  7973. write_number(static_cast<uint16_t>(j.m_value.number_integer));
  7974. }
  7975. else if (j.m_value.number_unsigned <= (std::numeric_limits<uint32_t>::max)())
  7976. {
  7977. // uint 32
  7978. oa->write_character(to_char_type(0xCE));
  7979. write_number(static_cast<uint32_t>(j.m_value.number_integer));
  7980. }
  7981. else if (j.m_value.number_unsigned <= (std::numeric_limits<uint64_t>::max)())
  7982. {
  7983. // uint 64
  7984. oa->write_character(to_char_type(0xCF));
  7985. write_number(static_cast<uint64_t>(j.m_value.number_integer));
  7986. }
  7987. break;
  7988. }
  7989. case value_t::number_float:
  7990. {
  7991. oa->write_character(get_msgpack_float_prefix(j.m_value.number_float));
  7992. write_number(j.m_value.number_float);
  7993. break;
  7994. }
  7995. case value_t::string:
  7996. {
  7997. // step 1: write control byte and the string length
  7998. const auto N = j.m_value.string->size();
  7999. if (N <= 31)
  8000. {
  8001. // fixstr
  8002. write_number(static_cast<uint8_t>(0xA0 | N));
  8003. }
  8004. else if (N <= (std::numeric_limits<uint8_t>::max)())
  8005. {
  8006. // str 8
  8007. oa->write_character(to_char_type(0xD9));
  8008. write_number(static_cast<uint8_t>(N));
  8009. }
  8010. else if (N <= (std::numeric_limits<uint16_t>::max)())
  8011. {
  8012. // str 16
  8013. oa->write_character(to_char_type(0xDA));
  8014. write_number(static_cast<uint16_t>(N));
  8015. }
  8016. else if (N <= (std::numeric_limits<uint32_t>::max)())
  8017. {
  8018. // str 32
  8019. oa->write_character(to_char_type(0xDB));
  8020. write_number(static_cast<uint32_t>(N));
  8021. }
  8022. // step 2: write the string
  8023. oa->write_characters(
  8024. reinterpret_cast<const CharType*>(j.m_value.string->c_str()),
  8025. j.m_value.string->size());
  8026. break;
  8027. }
  8028. case value_t::array:
  8029. {
  8030. // step 1: write control byte and the array size
  8031. const auto N = j.m_value.array->size();
  8032. if (N <= 15)
  8033. {
  8034. // fixarray
  8035. write_number(static_cast<uint8_t>(0x90 | N));
  8036. }
  8037. else if (N <= (std::numeric_limits<uint16_t>::max)())
  8038. {
  8039. // array 16
  8040. oa->write_character(to_char_type(0xDC));
  8041. write_number(static_cast<uint16_t>(N));
  8042. }
  8043. else if (N <= (std::numeric_limits<uint32_t>::max)())
  8044. {
  8045. // array 32
  8046. oa->write_character(to_char_type(0xDD));
  8047. write_number(static_cast<uint32_t>(N));
  8048. }
  8049. // step 2: write each element
  8050. for (const auto& el : *j.m_value.array)
  8051. {
  8052. write_msgpack(el);
  8053. }
  8054. break;
  8055. }
  8056. case value_t::object:
  8057. {
  8058. // step 1: write control byte and the object size
  8059. const auto N = j.m_value.object->size();
  8060. if (N <= 15)
  8061. {
  8062. // fixmap
  8063. write_number(static_cast<uint8_t>(0x80 | (N & 0xF)));
  8064. }
  8065. else if (N <= (std::numeric_limits<uint16_t>::max)())
  8066. {
  8067. // map 16
  8068. oa->write_character(to_char_type(0xDE));
  8069. write_number(static_cast<uint16_t>(N));
  8070. }
  8071. else if (N <= (std::numeric_limits<uint32_t>::max)())
  8072. {
  8073. // map 32
  8074. oa->write_character(to_char_type(0xDF));
  8075. write_number(static_cast<uint32_t>(N));
  8076. }
  8077. // step 2: write each element
  8078. for (const auto& el : *j.m_value.object)
  8079. {
  8080. write_msgpack(el.first);
  8081. write_msgpack(el.second);
  8082. }
  8083. break;
  8084. }
  8085. default:
  8086. break;
  8087. }
  8088. }
  8089. /*!
  8090. @param[in] j JSON value to serialize
  8091. @param[in] use_count whether to use '#' prefixes (optimized format)
  8092. @param[in] use_type whether to use '$' prefixes (optimized format)
  8093. @param[in] add_prefix whether prefixes need to be used for this value
  8094. */
  8095. void write_ubjson(const BasicJsonType& j, const bool use_count,
  8096. const bool use_type, const bool add_prefix = true)
  8097. {
  8098. switch (j.type())
  8099. {
  8100. case value_t::null:
  8101. {
  8102. if (add_prefix)
  8103. {
  8104. oa->write_character(to_char_type('Z'));
  8105. }
  8106. break;
  8107. }
  8108. case value_t::boolean:
  8109. {
  8110. if (add_prefix)
  8111. {
  8112. oa->write_character(j.m_value.boolean
  8113. ? to_char_type('T')
  8114. : to_char_type('F'));
  8115. }
  8116. break;
  8117. }
  8118. case value_t::number_integer:
  8119. {
  8120. write_number_with_ubjson_prefix(j.m_value.number_integer, add_prefix);
  8121. break;
  8122. }
  8123. case value_t::number_unsigned:
  8124. {
  8125. write_number_with_ubjson_prefix(j.m_value.number_unsigned, add_prefix);
  8126. break;
  8127. }
  8128. case value_t::number_float:
  8129. {
  8130. write_number_with_ubjson_prefix(j.m_value.number_float, add_prefix);
  8131. break;
  8132. }
  8133. case value_t::string:
  8134. {
  8135. if (add_prefix)
  8136. {
  8137. oa->write_character(to_char_type('S'));
  8138. }
  8139. write_number_with_ubjson_prefix(j.m_value.string->size(), true);
  8140. oa->write_characters(
  8141. reinterpret_cast<const CharType*>(j.m_value.string->c_str()),
  8142. j.m_value.string->size());
  8143. break;
  8144. }
  8145. case value_t::array:
  8146. {
  8147. if (add_prefix)
  8148. {
  8149. oa->write_character(to_char_type('['));
  8150. }
  8151. bool prefix_required = true;
  8152. if (use_type and not j.m_value.array->empty())
  8153. {
  8154. assert(use_count);
  8155. const CharType first_prefix = ubjson_prefix(j.front());
  8156. const bool same_prefix = std::all_of(j.begin() + 1, j.end(),
  8157. [this, first_prefix](const BasicJsonType & v)
  8158. {
  8159. return ubjson_prefix(v) == first_prefix;
  8160. });
  8161. if (same_prefix)
  8162. {
  8163. prefix_required = false;
  8164. oa->write_character(to_char_type('$'));
  8165. oa->write_character(first_prefix);
  8166. }
  8167. }
  8168. if (use_count)
  8169. {
  8170. oa->write_character(to_char_type('#'));
  8171. write_number_with_ubjson_prefix(j.m_value.array->size(), true);
  8172. }
  8173. for (const auto& el : *j.m_value.array)
  8174. {
  8175. write_ubjson(el, use_count, use_type, prefix_required);
  8176. }
  8177. if (not use_count)
  8178. {
  8179. oa->write_character(to_char_type(']'));
  8180. }
  8181. break;
  8182. }
  8183. case value_t::object:
  8184. {
  8185. if (add_prefix)
  8186. {
  8187. oa->write_character(to_char_type('{'));
  8188. }
  8189. bool prefix_required = true;
  8190. if (use_type and not j.m_value.object->empty())
  8191. {
  8192. assert(use_count);
  8193. const CharType first_prefix = ubjson_prefix(j.front());
  8194. const bool same_prefix = std::all_of(j.begin(), j.end(),
  8195. [this, first_prefix](const BasicJsonType & v)
  8196. {
  8197. return ubjson_prefix(v) == first_prefix;
  8198. });
  8199. if (same_prefix)
  8200. {
  8201. prefix_required = false;
  8202. oa->write_character(to_char_type('$'));
  8203. oa->write_character(first_prefix);
  8204. }
  8205. }
  8206. if (use_count)
  8207. {
  8208. oa->write_character(to_char_type('#'));
  8209. write_number_with_ubjson_prefix(j.m_value.object->size(), true);
  8210. }
  8211. for (const auto& el : *j.m_value.object)
  8212. {
  8213. write_number_with_ubjson_prefix(el.first.size(), true);
  8214. oa->write_characters(
  8215. reinterpret_cast<const CharType*>(el.first.c_str()),
  8216. el.first.size());
  8217. write_ubjson(el.second, use_count, use_type, prefix_required);
  8218. }
  8219. if (not use_count)
  8220. {
  8221. oa->write_character(to_char_type('}'));
  8222. }
  8223. break;
  8224. }
  8225. default:
  8226. break;
  8227. }
  8228. }
  8229. private:
  8230. //////////
  8231. // BSON //
  8232. //////////
  8233. /*!
  8234. @return The size of a BSON document entry header, including the id marker
  8235. and the entry name size (and its null-terminator).
  8236. */
  8237. static std::size_t calc_bson_entry_header_size(const string_t& name)
  8238. {
  8239. const auto it = name.find(static_cast<typename string_t::value_type>(0));
  8240. if (JSON_UNLIKELY(it != BasicJsonType::string_t::npos))
  8241. {
  8242. JSON_THROW(out_of_range::create(409,
  8243. "BSON key cannot contain code point U+0000 (at byte " + std::to_string(it) + ")"));
  8244. }
  8245. return /*id*/ 1ul + name.size() + /*zero-terminator*/1u;
  8246. }
  8247. /*!
  8248. @brief Writes the given @a element_type and @a name to the output adapter
  8249. */
  8250. void write_bson_entry_header(const string_t& name,
  8251. const std::uint8_t element_type)
  8252. {
  8253. oa->write_character(to_char_type(element_type)); // boolean
  8254. oa->write_characters(
  8255. reinterpret_cast<const CharType*>(name.c_str()),
  8256. name.size() + 1u);
  8257. }
  8258. /*!
  8259. @brief Writes a BSON element with key @a name and boolean value @a value
  8260. */
  8261. void write_bson_boolean(const string_t& name,
  8262. const bool value)
  8263. {
  8264. write_bson_entry_header(name, 0x08);
  8265. oa->write_character(value ? to_char_type(0x01) : to_char_type(0x00));
  8266. }
  8267. /*!
  8268. @brief Writes a BSON element with key @a name and double value @a value
  8269. */
  8270. void write_bson_double(const string_t& name,
  8271. const double value)
  8272. {
  8273. write_bson_entry_header(name, 0x01);
  8274. write_number<double, true>(value);
  8275. }
  8276. /*!
  8277. @return The size of the BSON-encoded string in @a value
  8278. */
  8279. static std::size_t calc_bson_string_size(const string_t& value)
  8280. {
  8281. return sizeof(std::int32_t) + value.size() + 1ul;
  8282. }
  8283. /*!
  8284. @brief Writes a BSON element with key @a name and string value @a value
  8285. */
  8286. void write_bson_string(const string_t& name,
  8287. const string_t& value)
  8288. {
  8289. write_bson_entry_header(name, 0x02);
  8290. write_number<std::int32_t, true>(static_cast<std::int32_t>(value.size() + 1ul));
  8291. oa->write_characters(
  8292. reinterpret_cast<const CharType*>(value.c_str()),
  8293. value.size() + 1);
  8294. }
  8295. /*!
  8296. @brief Writes a BSON element with key @a name and null value
  8297. */
  8298. void write_bson_null(const string_t& name)
  8299. {
  8300. write_bson_entry_header(name, 0x0A);
  8301. }
  8302. /*!
  8303. @return The size of the BSON-encoded integer @a value
  8304. */
  8305. static std::size_t calc_bson_integer_size(const std::int64_t value)
  8306. {
  8307. if ((std::numeric_limits<std::int32_t>::min)() <= value and value <= (std::numeric_limits<std::int32_t>::max)())
  8308. {
  8309. return sizeof(std::int32_t);
  8310. }
  8311. else
  8312. {
  8313. return sizeof(std::int64_t);
  8314. }
  8315. }
  8316. /*!
  8317. @brief Writes a BSON element with key @a name and integer @a value
  8318. */
  8319. void write_bson_integer(const string_t& name,
  8320. const std::int64_t value)
  8321. {
  8322. if ((std::numeric_limits<std::int32_t>::min)() <= value and value <= (std::numeric_limits<std::int32_t>::max)())
  8323. {
  8324. write_bson_entry_header(name, 0x10); // int32
  8325. write_number<std::int32_t, true>(static_cast<std::int32_t>(value));
  8326. }
  8327. else
  8328. {
  8329. write_bson_entry_header(name, 0x12); // int64
  8330. write_number<std::int64_t, true>(static_cast<std::int64_t>(value));
  8331. }
  8332. }
  8333. /*!
  8334. @return The size of the BSON-encoded unsigned integer in @a j
  8335. */
  8336. static constexpr std::size_t calc_bson_unsigned_size(const std::uint64_t value) noexcept
  8337. {
  8338. return (value <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  8339. ? sizeof(std::int32_t)
  8340. : sizeof(std::int64_t);
  8341. }
  8342. /*!
  8343. @brief Writes a BSON element with key @a name and unsigned @a value
  8344. */
  8345. void write_bson_unsigned(const string_t& name,
  8346. const std::uint64_t value)
  8347. {
  8348. if (value <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
  8349. {
  8350. write_bson_entry_header(name, 0x10 /* int32 */);
  8351. write_number<std::int32_t, true>(static_cast<std::int32_t>(value));
  8352. }
  8353. else if (value <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
  8354. {
  8355. write_bson_entry_header(name, 0x12 /* int64 */);
  8356. write_number<std::int64_t, true>(static_cast<std::int64_t>(value));
  8357. }
  8358. else
  8359. {
  8360. JSON_THROW(out_of_range::create(407, "integer number " + std::to_string(value) + " cannot be represented by BSON as it does not fit int64"));
  8361. }
  8362. }
  8363. /*!
  8364. @brief Writes a BSON element with key @a name and object @a value
  8365. */
  8366. void write_bson_object_entry(const string_t& name,
  8367. const typename BasicJsonType::object_t& value)
  8368. {
  8369. write_bson_entry_header(name, 0x03); // object
  8370. write_bson_object(value);
  8371. }
  8372. /*!
  8373. @return The size of the BSON-encoded array @a value
  8374. */
  8375. static std::size_t calc_bson_array_size(const typename BasicJsonType::array_t& value)
  8376. {
  8377. std::size_t embedded_document_size = 0ul;
  8378. std::size_t array_index = 0ul;
  8379. for (const auto& el : value)
  8380. {
  8381. embedded_document_size += calc_bson_element_size(std::to_string(array_index++), el);
  8382. }
  8383. return sizeof(std::int32_t) + embedded_document_size + 1ul;
  8384. }
  8385. /*!
  8386. @brief Writes a BSON element with key @a name and array @a value
  8387. */
  8388. void write_bson_array(const string_t& name,
  8389. const typename BasicJsonType::array_t& value)
  8390. {
  8391. write_bson_entry_header(name, 0x04); // array
  8392. write_number<std::int32_t, true>(static_cast<std::int32_t>(calc_bson_array_size(value)));
  8393. std::size_t array_index = 0ul;
  8394. for (const auto& el : value)
  8395. {
  8396. write_bson_element(std::to_string(array_index++), el);
  8397. }
  8398. oa->write_character(to_char_type(0x00));
  8399. }
  8400. /*!
  8401. @brief Calculates the size necessary to serialize the JSON value @a j with its @a name
  8402. @return The calculated size for the BSON document entry for @a j with the given @a name.
  8403. */
  8404. static std::size_t calc_bson_element_size(const string_t& name,
  8405. const BasicJsonType& j)
  8406. {
  8407. const auto header_size = calc_bson_entry_header_size(name);
  8408. switch (j.type())
  8409. {
  8410. case value_t::object:
  8411. return header_size + calc_bson_object_size(*j.m_value.object);
  8412. case value_t::array:
  8413. return header_size + calc_bson_array_size(*j.m_value.array);
  8414. case value_t::boolean:
  8415. return header_size + 1ul;
  8416. case value_t::number_float:
  8417. return header_size + 8ul;
  8418. case value_t::number_integer:
  8419. return header_size + calc_bson_integer_size(j.m_value.number_integer);
  8420. case value_t::number_unsigned:
  8421. return header_size + calc_bson_unsigned_size(j.m_value.number_unsigned);
  8422. case value_t::string:
  8423. return header_size + calc_bson_string_size(*j.m_value.string);
  8424. case value_t::null:
  8425. return header_size + 0ul;
  8426. // LCOV_EXCL_START
  8427. default:
  8428. assert(false);
  8429. return 0ul;
  8430. // LCOV_EXCL_STOP
  8431. };
  8432. }
  8433. /*!
  8434. @brief Serializes the JSON value @a j to BSON and associates it with the
  8435. key @a name.
  8436. @param name The name to associate with the JSON entity @a j within the
  8437. current BSON document
  8438. @return The size of the BSON entry
  8439. */
  8440. void write_bson_element(const string_t& name,
  8441. const BasicJsonType& j)
  8442. {
  8443. switch (j.type())
  8444. {
  8445. case value_t::object:
  8446. return write_bson_object_entry(name, *j.m_value.object);
  8447. case value_t::array:
  8448. return write_bson_array(name, *j.m_value.array);
  8449. case value_t::boolean:
  8450. return write_bson_boolean(name, j.m_value.boolean);
  8451. case value_t::number_float:
  8452. return write_bson_double(name, j.m_value.number_float);
  8453. case value_t::number_integer:
  8454. return write_bson_integer(name, j.m_value.number_integer);
  8455. case value_t::number_unsigned:
  8456. return write_bson_unsigned(name, j.m_value.number_unsigned);
  8457. case value_t::string:
  8458. return write_bson_string(name, *j.m_value.string);
  8459. case value_t::null:
  8460. return write_bson_null(name);
  8461. // LCOV_EXCL_START
  8462. default:
  8463. assert(false);
  8464. return;
  8465. // LCOV_EXCL_STOP
  8466. };
  8467. }
  8468. /*!
  8469. @brief Calculates the size of the BSON serialization of the given
  8470. JSON-object @a j.
  8471. @param[in] j JSON value to serialize
  8472. @pre j.type() == value_t::object
  8473. */
  8474. static std::size_t calc_bson_object_size(const typename BasicJsonType::object_t& value)
  8475. {
  8476. std::size_t document_size = std::accumulate(value.begin(), value.end(), 0ul,
  8477. [](size_t result, const typename BasicJsonType::object_t::value_type & el)
  8478. {
  8479. return result += calc_bson_element_size(el.first, el.second);
  8480. });
  8481. return sizeof(std::int32_t) + document_size + 1ul;
  8482. }
  8483. /*!
  8484. @param[in] j JSON value to serialize
  8485. @pre j.type() == value_t::object
  8486. */
  8487. void write_bson_object(const typename BasicJsonType::object_t& value)
  8488. {
  8489. write_number<std::int32_t, true>(static_cast<std::int32_t>(calc_bson_object_size(value)));
  8490. for (const auto& el : value)
  8491. {
  8492. write_bson_element(el.first, el.second);
  8493. }
  8494. oa->write_character(to_char_type(0x00));
  8495. }
  8496. //////////
  8497. // CBOR //
  8498. //////////
  8499. static constexpr CharType get_cbor_float_prefix(float /*unused*/)
  8500. {
  8501. return to_char_type(0xFA); // Single-Precision Float
  8502. }
  8503. static constexpr CharType get_cbor_float_prefix(double /*unused*/)
  8504. {
  8505. return to_char_type(0xFB); // Double-Precision Float
  8506. }
  8507. /////////////
  8508. // MsgPack //
  8509. /////////////
  8510. static constexpr CharType get_msgpack_float_prefix(float /*unused*/)
  8511. {
  8512. return to_char_type(0xCA); // float 32
  8513. }
  8514. static constexpr CharType get_msgpack_float_prefix(double /*unused*/)
  8515. {
  8516. return to_char_type(0xCB); // float 64
  8517. }
  8518. ////////////
  8519. // UBJSON //
  8520. ////////////
  8521. // UBJSON: write number (floating point)
  8522. template<typename NumberType, typename std::enable_if<
  8523. std::is_floating_point<NumberType>::value, int>::type = 0>
  8524. void write_number_with_ubjson_prefix(const NumberType n,
  8525. const bool add_prefix)
  8526. {
  8527. if (add_prefix)
  8528. {
  8529. oa->write_character(get_ubjson_float_prefix(n));
  8530. }
  8531. write_number(n);
  8532. }
  8533. // UBJSON: write number (unsigned integer)
  8534. template<typename NumberType, typename std::enable_if<
  8535. std::is_unsigned<NumberType>::value, int>::type = 0>
  8536. void write_number_with_ubjson_prefix(const NumberType n,
  8537. const bool add_prefix)
  8538. {
  8539. if (n <= static_cast<uint64_t>((std::numeric_limits<int8_t>::max)()))
  8540. {
  8541. if (add_prefix)
  8542. {
  8543. oa->write_character(to_char_type('i')); // int8
  8544. }
  8545. write_number(static_cast<uint8_t>(n));
  8546. }
  8547. else if (n <= (std::numeric_limits<uint8_t>::max)())
  8548. {
  8549. if (add_prefix)
  8550. {
  8551. oa->write_character(to_char_type('U')); // uint8
  8552. }
  8553. write_number(static_cast<uint8_t>(n));
  8554. }
  8555. else if (n <= static_cast<uint64_t>((std::numeric_limits<int16_t>::max)()))
  8556. {
  8557. if (add_prefix)
  8558. {
  8559. oa->write_character(to_char_type('I')); // int16
  8560. }
  8561. write_number(static_cast<int16_t>(n));
  8562. }
  8563. else if (n <= static_cast<uint64_t>((std::numeric_limits<int32_t>::max)()))
  8564. {
  8565. if (add_prefix)
  8566. {
  8567. oa->write_character(to_char_type('l')); // int32
  8568. }
  8569. write_number(static_cast<int32_t>(n));
  8570. }
  8571. else if (n <= static_cast<uint64_t>((std::numeric_limits<int64_t>::max)()))
  8572. {
  8573. if (add_prefix)
  8574. {
  8575. oa->write_character(to_char_type('L')); // int64
  8576. }
  8577. write_number(static_cast<int64_t>(n));
  8578. }
  8579. else
  8580. {
  8581. JSON_THROW(out_of_range::create(407, "integer number " + std::to_string(n) + " cannot be represented by UBJSON as it does not fit int64"));
  8582. }
  8583. }
  8584. // UBJSON: write number (signed integer)
  8585. template<typename NumberType, typename std::enable_if<
  8586. std::is_signed<NumberType>::value and
  8587. not std::is_floating_point<NumberType>::value, int>::type = 0>
  8588. void write_number_with_ubjson_prefix(const NumberType n,
  8589. const bool add_prefix)
  8590. {
  8591. if ((std::numeric_limits<int8_t>::min)() <= n and n <= (std::numeric_limits<int8_t>::max)())
  8592. {
  8593. if (add_prefix)
  8594. {
  8595. oa->write_character(to_char_type('i')); // int8
  8596. }
  8597. write_number(static_cast<int8_t>(n));
  8598. }
  8599. else if (static_cast<int64_t>((std::numeric_limits<uint8_t>::min)()) <= n and n <= static_cast<int64_t>((std::numeric_limits<uint8_t>::max)()))
  8600. {
  8601. if (add_prefix)
  8602. {
  8603. oa->write_character(to_char_type('U')); // uint8
  8604. }
  8605. write_number(static_cast<uint8_t>(n));
  8606. }
  8607. else if ((std::numeric_limits<int16_t>::min)() <= n and n <= (std::numeric_limits<int16_t>::max)())
  8608. {
  8609. if (add_prefix)
  8610. {
  8611. oa->write_character(to_char_type('I')); // int16
  8612. }
  8613. write_number(static_cast<int16_t>(n));
  8614. }
  8615. else if ((std::numeric_limits<int32_t>::min)() <= n and n <= (std::numeric_limits<int32_t>::max)())
  8616. {
  8617. if (add_prefix)
  8618. {
  8619. oa->write_character(to_char_type('l')); // int32
  8620. }
  8621. write_number(static_cast<int32_t>(n));
  8622. }
  8623. else if ((std::numeric_limits<int64_t>::min)() <= n and n <= (std::numeric_limits<int64_t>::max)())
  8624. {
  8625. if (add_prefix)
  8626. {
  8627. oa->write_character(to_char_type('L')); // int64
  8628. }
  8629. write_number(static_cast<int64_t>(n));
  8630. }
  8631. // LCOV_EXCL_START
  8632. else
  8633. {
  8634. JSON_THROW(out_of_range::create(407, "integer number " + std::to_string(n) + " cannot be represented by UBJSON as it does not fit int64"));
  8635. }
  8636. // LCOV_EXCL_STOP
  8637. }
  8638. /*!
  8639. @brief determine the type prefix of container values
  8640. @note This function does not need to be 100% accurate when it comes to
  8641. integer limits. In case a number exceeds the limits of int64_t,
  8642. this will be detected by a later call to function
  8643. write_number_with_ubjson_prefix. Therefore, we return 'L' for any
  8644. value that does not fit the previous limits.
  8645. */
  8646. CharType ubjson_prefix(const BasicJsonType& j) const noexcept
  8647. {
  8648. switch (j.type())
  8649. {
  8650. case value_t::null:
  8651. return 'Z';
  8652. case value_t::boolean:
  8653. return j.m_value.boolean ? 'T' : 'F';
  8654. case value_t::number_integer:
  8655. {
  8656. if ((std::numeric_limits<int8_t>::min)() <= j.m_value.number_integer and j.m_value.number_integer <= (std::numeric_limits<int8_t>::max)())
  8657. {
  8658. return 'i';
  8659. }
  8660. if ((std::numeric_limits<uint8_t>::min)() <= j.m_value.number_integer and j.m_value.number_integer <= (std::numeric_limits<uint8_t>::max)())
  8661. {
  8662. return 'U';
  8663. }
  8664. if ((std::numeric_limits<int16_t>::min)() <= j.m_value.number_integer and j.m_value.number_integer <= (std::numeric_limits<int16_t>::max)())
  8665. {
  8666. return 'I';
  8667. }
  8668. if ((std::numeric_limits<int32_t>::min)() <= j.m_value.number_integer and j.m_value.number_integer <= (std::numeric_limits<int32_t>::max)())
  8669. {
  8670. return 'l';
  8671. }
  8672. // no check and assume int64_t (see note above)
  8673. return 'L';
  8674. }
  8675. case value_t::number_unsigned:
  8676. {
  8677. if (j.m_value.number_unsigned <= (std::numeric_limits<int8_t>::max)())
  8678. {
  8679. return 'i';
  8680. }
  8681. if (j.m_value.number_unsigned <= (std::numeric_limits<uint8_t>::max)())
  8682. {
  8683. return 'U';
  8684. }
  8685. if (j.m_value.number_unsigned <= (std::numeric_limits<int16_t>::max)())
  8686. {
  8687. return 'I';
  8688. }
  8689. if (j.m_value.number_unsigned <= (std::numeric_limits<int32_t>::max)())
  8690. {
  8691. return 'l';
  8692. }
  8693. // no check and assume int64_t (see note above)
  8694. return 'L';
  8695. }
  8696. case value_t::number_float:
  8697. return get_ubjson_float_prefix(j.m_value.number_float);
  8698. case value_t::string:
  8699. return 'S';
  8700. case value_t::array:
  8701. return '[';
  8702. case value_t::object:
  8703. return '{';
  8704. default: // discarded values
  8705. return 'N';
  8706. }
  8707. }
  8708. static constexpr CharType get_ubjson_float_prefix(float /*unused*/)
  8709. {
  8710. return 'd'; // float 32
  8711. }
  8712. static constexpr CharType get_ubjson_float_prefix(double /*unused*/)
  8713. {
  8714. return 'D'; // float 64
  8715. }
  8716. ///////////////////////
  8717. // Utility functions //
  8718. ///////////////////////
  8719. /*
  8720. @brief write a number to output input
  8721. @param[in] n number of type @a NumberType
  8722. @tparam NumberType the type of the number
  8723. @tparam OutputIsLittleEndian Set to true if output data is
  8724. required to be little endian
  8725. @note This function needs to respect the system's endianess, because bytes
  8726. in CBOR, MessagePack, and UBJSON are stored in network order (big
  8727. endian) and therefore need reordering on little endian systems.
  8728. */
  8729. template<typename NumberType, bool OutputIsLittleEndian = false>
  8730. void write_number(const NumberType n)
  8731. {
  8732. // step 1: write number to array of length NumberType
  8733. std::array<CharType, sizeof(NumberType)> vec;
  8734. std::memcpy(vec.data(), &n, sizeof(NumberType));
  8735. // step 2: write array to output (with possible reordering)
  8736. if (is_little_endian and not OutputIsLittleEndian)
  8737. {
  8738. // reverse byte order prior to conversion if necessary
  8739. std::reverse(vec.begin(), vec.end());
  8740. }
  8741. oa->write_characters(vec.data(), sizeof(NumberType));
  8742. }
  8743. public:
  8744. // The following to_char_type functions are implement the conversion
  8745. // between uint8_t and CharType. In case CharType is not unsigned,
  8746. // such a conversion is required to allow values greater than 128.
  8747. // See <https://github.com/nlohmann/json/issues/1286> for a discussion.
  8748. template < typename C = CharType,
  8749. enable_if_t < std::is_signed<C>::value and std::is_signed<char>::value > * = nullptr >
  8750. static constexpr CharType to_char_type(std::uint8_t x) noexcept
  8751. {
  8752. return *reinterpret_cast<char*>(&x);
  8753. }
  8754. template < typename C = CharType,
  8755. enable_if_t < std::is_signed<C>::value and std::is_unsigned<char>::value > * = nullptr >
  8756. static CharType to_char_type(std::uint8_t x) noexcept
  8757. {
  8758. static_assert(sizeof(std::uint8_t) == sizeof(CharType), "size of CharType must be equal to std::uint8_t");
  8759. static_assert(std::is_pod<CharType>::value, "CharType must be POD");
  8760. CharType result;
  8761. std::memcpy(&result, &x, sizeof(x));
  8762. return result;
  8763. }
  8764. template<typename C = CharType,
  8765. enable_if_t<std::is_unsigned<C>::value>* = nullptr>
  8766. static constexpr CharType to_char_type(std::uint8_t x) noexcept
  8767. {
  8768. return x;
  8769. }
  8770. template < typename InputCharType, typename C = CharType,
  8771. enable_if_t <
  8772. std::is_signed<C>::value and
  8773. std::is_signed<char>::value and
  8774. std::is_same<char, typename std::remove_cv<InputCharType>::type>::value
  8775. > * = nullptr >
  8776. static constexpr CharType to_char_type(InputCharType x) noexcept
  8777. {
  8778. return x;
  8779. }
  8780. private:
  8781. /// whether we can assume little endianess
  8782. const bool is_little_endian = binary_reader<BasicJsonType>::little_endianess();
  8783. /// the output
  8784. output_adapter_t<CharType> oa = nullptr;
  8785. };
  8786. } // namespace detail
  8787. } // namespace nlohmann
  8788. // #include <nlohmann/detail/output/serializer.hpp>
  8789. #include <algorithm> // reverse, remove, fill, find, none_of
  8790. #include <array> // array
  8791. #include <cassert> // assert
  8792. #include <ciso646> // and, or
  8793. #include <clocale> // localeconv, lconv
  8794. #include <cmath> // labs, isfinite, isnan, signbit
  8795. #include <cstddef> // size_t, ptrdiff_t
  8796. #include <cstdint> // uint8_t
  8797. #include <cstdio> // snprintf
  8798. #include <limits> // numeric_limits
  8799. #include <string> // string
  8800. #include <type_traits> // is_same
  8801. // #include <nlohmann/detail/exceptions.hpp>
  8802. // #include <nlohmann/detail/conversions/to_chars.hpp>
  8803. #include <cassert> // assert
  8804. #include <ciso646> // or, and, not
  8805. #include <cmath> // signbit, isfinite
  8806. #include <cstdint> // intN_t, uintN_t
  8807. #include <cstring> // memcpy, memmove
  8808. namespace nlohmann
  8809. {
  8810. namespace detail
  8811. {
  8812. /*!
  8813. @brief implements the Grisu2 algorithm for binary to decimal floating-point
  8814. conversion.
  8815. This implementation is a slightly modified version of the reference
  8816. implementation which may be obtained from
  8817. http://florian.loitsch.com/publications (bench.tar.gz).
  8818. The code is distributed under the MIT license, Copyright (c) 2009 Florian Loitsch.
  8819. For a detailed description of the algorithm see:
  8820. [1] Loitsch, "Printing Floating-Point Numbers Quickly and Accurately with
  8821. Integers", Proceedings of the ACM SIGPLAN 2010 Conference on Programming
  8822. Language Design and Implementation, PLDI 2010
  8823. [2] Burger, Dybvig, "Printing Floating-Point Numbers Quickly and Accurately",
  8824. Proceedings of the ACM SIGPLAN 1996 Conference on Programming Language
  8825. Design and Implementation, PLDI 1996
  8826. */
  8827. namespace dtoa_impl
  8828. {
  8829. template <typename Target, typename Source>
  8830. Target reinterpret_bits(const Source source)
  8831. {
  8832. static_assert(sizeof(Target) == sizeof(Source), "size mismatch");
  8833. Target target;
  8834. std::memcpy(&target, &source, sizeof(Source));
  8835. return target;
  8836. }
  8837. struct diyfp // f * 2^e
  8838. {
  8839. static constexpr int kPrecision = 64; // = q
  8840. uint64_t f = 0;
  8841. int e = 0;
  8842. constexpr diyfp(uint64_t f_, int e_) noexcept : f(f_), e(e_) {}
  8843. /*!
  8844. @brief returns x - y
  8845. @pre x.e == y.e and x.f >= y.f
  8846. */
  8847. static diyfp sub(const diyfp& x, const diyfp& y) noexcept
  8848. {
  8849. assert(x.e == y.e);
  8850. assert(x.f >= y.f);
  8851. return {x.f - y.f, x.e};
  8852. }
  8853. /*!
  8854. @brief returns x * y
  8855. @note The result is rounded. (Only the upper q bits are returned.)
  8856. */
  8857. static diyfp mul(const diyfp& x, const diyfp& y) noexcept
  8858. {
  8859. static_assert(kPrecision == 64, "internal error");
  8860. // Computes:
  8861. // f = round((x.f * y.f) / 2^q)
  8862. // e = x.e + y.e + q
  8863. // Emulate the 64-bit * 64-bit multiplication:
  8864. //
  8865. // p = u * v
  8866. // = (u_lo + 2^32 u_hi) (v_lo + 2^32 v_hi)
  8867. // = (u_lo v_lo ) + 2^32 ((u_lo v_hi ) + (u_hi v_lo )) + 2^64 (u_hi v_hi )
  8868. // = (p0 ) + 2^32 ((p1 ) + (p2 )) + 2^64 (p3 )
  8869. // = (p0_lo + 2^32 p0_hi) + 2^32 ((p1_lo + 2^32 p1_hi) + (p2_lo + 2^32 p2_hi)) + 2^64 (p3 )
  8870. // = (p0_lo ) + 2^32 (p0_hi + p1_lo + p2_lo ) + 2^64 (p1_hi + p2_hi + p3)
  8871. // = (p0_lo ) + 2^32 (Q ) + 2^64 (H )
  8872. // = (p0_lo ) + 2^32 (Q_lo + 2^32 Q_hi ) + 2^64 (H )
  8873. //
  8874. // (Since Q might be larger than 2^32 - 1)
  8875. //
  8876. // = (p0_lo + 2^32 Q_lo) + 2^64 (Q_hi + H)
  8877. //
  8878. // (Q_hi + H does not overflow a 64-bit int)
  8879. //
  8880. // = p_lo + 2^64 p_hi
  8881. const uint64_t u_lo = x.f & 0xFFFFFFFF;
  8882. const uint64_t u_hi = x.f >> 32;
  8883. const uint64_t v_lo = y.f & 0xFFFFFFFF;
  8884. const uint64_t v_hi = y.f >> 32;
  8885. const uint64_t p0 = u_lo * v_lo;
  8886. const uint64_t p1 = u_lo * v_hi;
  8887. const uint64_t p2 = u_hi * v_lo;
  8888. const uint64_t p3 = u_hi * v_hi;
  8889. const uint64_t p0_hi = p0 >> 32;
  8890. const uint64_t p1_lo = p1 & 0xFFFFFFFF;
  8891. const uint64_t p1_hi = p1 >> 32;
  8892. const uint64_t p2_lo = p2 & 0xFFFFFFFF;
  8893. const uint64_t p2_hi = p2 >> 32;
  8894. uint64_t Q = p0_hi + p1_lo + p2_lo;
  8895. // The full product might now be computed as
  8896. //
  8897. // p_hi = p3 + p2_hi + p1_hi + (Q >> 32)
  8898. // p_lo = p0_lo + (Q << 32)
  8899. //
  8900. // But in this particular case here, the full p_lo is not required.
  8901. // Effectively we only need to add the highest bit in p_lo to p_hi (and
  8902. // Q_hi + 1 does not overflow).
  8903. Q += uint64_t{1} << (64 - 32 - 1); // round, ties up
  8904. const uint64_t h = p3 + p2_hi + p1_hi + (Q >> 32);
  8905. return {h, x.e + y.e + 64};
  8906. }
  8907. /*!
  8908. @brief normalize x such that the significand is >= 2^(q-1)
  8909. @pre x.f != 0
  8910. */
  8911. static diyfp normalize(diyfp x) noexcept
  8912. {
  8913. assert(x.f != 0);
  8914. while ((x.f >> 63) == 0)
  8915. {
  8916. x.f <<= 1;
  8917. x.e--;
  8918. }
  8919. return x;
  8920. }
  8921. /*!
  8922. @brief normalize x such that the result has the exponent E
  8923. @pre e >= x.e and the upper e - x.e bits of x.f must be zero.
  8924. */
  8925. static diyfp normalize_to(const diyfp& x, const int target_exponent) noexcept
  8926. {
  8927. const int delta = x.e - target_exponent;
  8928. assert(delta >= 0);
  8929. assert(((x.f << delta) >> delta) == x.f);
  8930. return {x.f << delta, target_exponent};
  8931. }
  8932. };
  8933. struct boundaries
  8934. {
  8935. diyfp w;
  8936. diyfp minus;
  8937. diyfp plus;
  8938. };
  8939. /*!
  8940. Compute the (normalized) diyfp representing the input number 'value' and its
  8941. boundaries.
  8942. @pre value must be finite and positive
  8943. */
  8944. template <typename FloatType>
  8945. boundaries compute_boundaries(FloatType value)
  8946. {
  8947. assert(std::isfinite(value));
  8948. assert(value > 0);
  8949. // Convert the IEEE representation into a diyfp.
  8950. //
  8951. // If v is denormal:
  8952. // value = 0.F * 2^(1 - bias) = ( F) * 2^(1 - bias - (p-1))
  8953. // If v is normalized:
  8954. // value = 1.F * 2^(E - bias) = (2^(p-1) + F) * 2^(E - bias - (p-1))
  8955. static_assert(std::numeric_limits<FloatType>::is_iec559,
  8956. "internal error: dtoa_short requires an IEEE-754 floating-point implementation");
  8957. constexpr int kPrecision = std::numeric_limits<FloatType>::digits; // = p (includes the hidden bit)
  8958. constexpr int kBias = std::numeric_limits<FloatType>::max_exponent - 1 + (kPrecision - 1);
  8959. constexpr int kMinExp = 1 - kBias;
  8960. constexpr uint64_t kHiddenBit = uint64_t{1} << (kPrecision - 1); // = 2^(p-1)
  8961. using bits_type = typename std::conditional< kPrecision == 24, uint32_t, uint64_t >::type;
  8962. const uint64_t bits = reinterpret_bits<bits_type>(value);
  8963. const uint64_t E = bits >> (kPrecision - 1);
  8964. const uint64_t F = bits & (kHiddenBit - 1);
  8965. const bool is_denormal = (E == 0);
  8966. const diyfp v = is_denormal
  8967. ? diyfp(F, kMinExp)
  8968. : diyfp(F + kHiddenBit, static_cast<int>(E) - kBias);
  8969. // Compute the boundaries m- and m+ of the floating-point value
  8970. // v = f * 2^e.
  8971. //
  8972. // Determine v- and v+, the floating-point predecessor and successor if v,
  8973. // respectively.
  8974. //
  8975. // v- = v - 2^e if f != 2^(p-1) or e == e_min (A)
  8976. // = v - 2^(e-1) if f == 2^(p-1) and e > e_min (B)
  8977. //
  8978. // v+ = v + 2^e
  8979. //
  8980. // Let m- = (v- + v) / 2 and m+ = (v + v+) / 2. All real numbers _strictly_
  8981. // between m- and m+ round to v, regardless of how the input rounding
  8982. // algorithm breaks ties.
  8983. //
  8984. // ---+-------------+-------------+-------------+-------------+--- (A)
  8985. // v- m- v m+ v+
  8986. //
  8987. // -----------------+------+------+-------------+-------------+--- (B)
  8988. // v- m- v m+ v+
  8989. const bool lower_boundary_is_closer = (F == 0 and E > 1);
  8990. const diyfp m_plus = diyfp(2 * v.f + 1, v.e - 1);
  8991. const diyfp m_minus = lower_boundary_is_closer
  8992. ? diyfp(4 * v.f - 1, v.e - 2) // (B)
  8993. : diyfp(2 * v.f - 1, v.e - 1); // (A)
  8994. // Determine the normalized w+ = m+.
  8995. const diyfp w_plus = diyfp::normalize(m_plus);
  8996. // Determine w- = m- such that e_(w-) = e_(w+).
  8997. const diyfp w_minus = diyfp::normalize_to(m_minus, w_plus.e);
  8998. return {diyfp::normalize(v), w_minus, w_plus};
  8999. }
  9000. // Given normalized diyfp w, Grisu needs to find a (normalized) cached
  9001. // power-of-ten c, such that the exponent of the product c * w = f * 2^e lies
  9002. // within a certain range [alpha, gamma] (Definition 3.2 from [1])
  9003. //
  9004. // alpha <= e = e_c + e_w + q <= gamma
  9005. //
  9006. // or
  9007. //
  9008. // f_c * f_w * 2^alpha <= f_c 2^(e_c) * f_w 2^(e_w) * 2^q
  9009. // <= f_c * f_w * 2^gamma
  9010. //
  9011. // Since c and w are normalized, i.e. 2^(q-1) <= f < 2^q, this implies
  9012. //
  9013. // 2^(q-1) * 2^(q-1) * 2^alpha <= c * w * 2^q < 2^q * 2^q * 2^gamma
  9014. //
  9015. // or
  9016. //
  9017. // 2^(q - 2 + alpha) <= c * w < 2^(q + gamma)
  9018. //
  9019. // The choice of (alpha,gamma) determines the size of the table and the form of
  9020. // the digit generation procedure. Using (alpha,gamma)=(-60,-32) works out well
  9021. // in practice:
  9022. //
  9023. // The idea is to cut the number c * w = f * 2^e into two parts, which can be
  9024. // processed independently: An integral part p1, and a fractional part p2:
  9025. //
  9026. // f * 2^e = ( (f div 2^-e) * 2^-e + (f mod 2^-e) ) * 2^e
  9027. // = (f div 2^-e) + (f mod 2^-e) * 2^e
  9028. // = p1 + p2 * 2^e
  9029. //
  9030. // The conversion of p1 into decimal form requires a series of divisions and
  9031. // modulos by (a power of) 10. These operations are faster for 32-bit than for
  9032. // 64-bit integers, so p1 should ideally fit into a 32-bit integer. This can be
  9033. // achieved by choosing
  9034. //
  9035. // -e >= 32 or e <= -32 := gamma
  9036. //
  9037. // In order to convert the fractional part
  9038. //
  9039. // p2 * 2^e = p2 / 2^-e = d[-1] / 10^1 + d[-2] / 10^2 + ...
  9040. //
  9041. // into decimal form, the fraction is repeatedly multiplied by 10 and the digits
  9042. // d[-i] are extracted in order:
  9043. //
  9044. // (10 * p2) div 2^-e = d[-1]
  9045. // (10 * p2) mod 2^-e = d[-2] / 10^1 + ...
  9046. //
  9047. // The multiplication by 10 must not overflow. It is sufficient to choose
  9048. //
  9049. // 10 * p2 < 16 * p2 = 2^4 * p2 <= 2^64.
  9050. //
  9051. // Since p2 = f mod 2^-e < 2^-e,
  9052. //
  9053. // -e <= 60 or e >= -60 := alpha
  9054. constexpr int kAlpha = -60;
  9055. constexpr int kGamma = -32;
  9056. struct cached_power // c = f * 2^e ~= 10^k
  9057. {
  9058. uint64_t f;
  9059. int e;
  9060. int k;
  9061. };
  9062. /*!
  9063. For a normalized diyfp w = f * 2^e, this function returns a (normalized) cached
  9064. power-of-ten c = f_c * 2^e_c, such that the exponent of the product w * c
  9065. satisfies (Definition 3.2 from [1])
  9066. alpha <= e_c + e + q <= gamma.
  9067. */
  9068. inline cached_power get_cached_power_for_binary_exponent(int e)
  9069. {
  9070. // Now
  9071. //
  9072. // alpha <= e_c + e + q <= gamma (1)
  9073. // ==> f_c * 2^alpha <= c * 2^e * 2^q
  9074. //
  9075. // and since the c's are normalized, 2^(q-1) <= f_c,
  9076. //
  9077. // ==> 2^(q - 1 + alpha) <= c * 2^(e + q)
  9078. // ==> 2^(alpha - e - 1) <= c
  9079. //
  9080. // If c were an exakt power of ten, i.e. c = 10^k, one may determine k as
  9081. //
  9082. // k = ceil( log_10( 2^(alpha - e - 1) ) )
  9083. // = ceil( (alpha - e - 1) * log_10(2) )
  9084. //
  9085. // From the paper:
  9086. // "In theory the result of the procedure could be wrong since c is rounded,
  9087. // and the computation itself is approximated [...]. In practice, however,
  9088. // this simple function is sufficient."
  9089. //
  9090. // For IEEE double precision floating-point numbers converted into
  9091. // normalized diyfp's w = f * 2^e, with q = 64,
  9092. //
  9093. // e >= -1022 (min IEEE exponent)
  9094. // -52 (p - 1)
  9095. // -52 (p - 1, possibly normalize denormal IEEE numbers)
  9096. // -11 (normalize the diyfp)
  9097. // = -1137
  9098. //
  9099. // and
  9100. //
  9101. // e <= +1023 (max IEEE exponent)
  9102. // -52 (p - 1)
  9103. // -11 (normalize the diyfp)
  9104. // = 960
  9105. //
  9106. // This binary exponent range [-1137,960] results in a decimal exponent
  9107. // range [-307,324]. One does not need to store a cached power for each
  9108. // k in this range. For each such k it suffices to find a cached power
  9109. // such that the exponent of the product lies in [alpha,gamma].
  9110. // This implies that the difference of the decimal exponents of adjacent
  9111. // table entries must be less than or equal to
  9112. //
  9113. // floor( (gamma - alpha) * log_10(2) ) = 8.
  9114. //
  9115. // (A smaller distance gamma-alpha would require a larger table.)
  9116. // NB:
  9117. // Actually this function returns c, such that -60 <= e_c + e + 64 <= -34.
  9118. constexpr int kCachedPowersSize = 79;
  9119. constexpr int kCachedPowersMinDecExp = -300;
  9120. constexpr int kCachedPowersDecStep = 8;
  9121. static constexpr cached_power kCachedPowers[] =
  9122. {
  9123. { 0xAB70FE17C79AC6CA, -1060, -300 },
  9124. { 0xFF77B1FCBEBCDC4F, -1034, -292 },
  9125. { 0xBE5691EF416BD60C, -1007, -284 },
  9126. { 0x8DD01FAD907FFC3C, -980, -276 },
  9127. { 0xD3515C2831559A83, -954, -268 },
  9128. { 0x9D71AC8FADA6C9B5, -927, -260 },
  9129. { 0xEA9C227723EE8BCB, -901, -252 },
  9130. { 0xAECC49914078536D, -874, -244 },
  9131. { 0x823C12795DB6CE57, -847, -236 },
  9132. { 0xC21094364DFB5637, -821, -228 },
  9133. { 0x9096EA6F3848984F, -794, -220 },
  9134. { 0xD77485CB25823AC7, -768, -212 },
  9135. { 0xA086CFCD97BF97F4, -741, -204 },
  9136. { 0xEF340A98172AACE5, -715, -196 },
  9137. { 0xB23867FB2A35B28E, -688, -188 },
  9138. { 0x84C8D4DFD2C63F3B, -661, -180 },
  9139. { 0xC5DD44271AD3CDBA, -635, -172 },
  9140. { 0x936B9FCEBB25C996, -608, -164 },
  9141. { 0xDBAC6C247D62A584, -582, -156 },
  9142. { 0xA3AB66580D5FDAF6, -555, -148 },
  9143. { 0xF3E2F893DEC3F126, -529, -140 },
  9144. { 0xB5B5ADA8AAFF80B8, -502, -132 },
  9145. { 0x87625F056C7C4A8B, -475, -124 },
  9146. { 0xC9BCFF6034C13053, -449, -116 },
  9147. { 0x964E858C91BA2655, -422, -108 },
  9148. { 0xDFF9772470297EBD, -396, -100 },
  9149. { 0xA6DFBD9FB8E5B88F, -369, -92 },
  9150. { 0xF8A95FCF88747D94, -343, -84 },
  9151. { 0xB94470938FA89BCF, -316, -76 },
  9152. { 0x8A08F0F8BF0F156B, -289, -68 },
  9153. { 0xCDB02555653131B6, -263, -60 },
  9154. { 0x993FE2C6D07B7FAC, -236, -52 },
  9155. { 0xE45C10C42A2B3B06, -210, -44 },
  9156. { 0xAA242499697392D3, -183, -36 },
  9157. { 0xFD87B5F28300CA0E, -157, -28 },
  9158. { 0xBCE5086492111AEB, -130, -20 },
  9159. { 0x8CBCCC096F5088CC, -103, -12 },
  9160. { 0xD1B71758E219652C, -77, -4 },
  9161. { 0x9C40000000000000, -50, 4 },
  9162. { 0xE8D4A51000000000, -24, 12 },
  9163. { 0xAD78EBC5AC620000, 3, 20 },
  9164. { 0x813F3978F8940984, 30, 28 },
  9165. { 0xC097CE7BC90715B3, 56, 36 },
  9166. { 0x8F7E32CE7BEA5C70, 83, 44 },
  9167. { 0xD5D238A4ABE98068, 109, 52 },
  9168. { 0x9F4F2726179A2245, 136, 60 },
  9169. { 0xED63A231D4C4FB27, 162, 68 },
  9170. { 0xB0DE65388CC8ADA8, 189, 76 },
  9171. { 0x83C7088E1AAB65DB, 216, 84 },
  9172. { 0xC45D1DF942711D9A, 242, 92 },
  9173. { 0x924D692CA61BE758, 269, 100 },
  9174. { 0xDA01EE641A708DEA, 295, 108 },
  9175. { 0xA26DA3999AEF774A, 322, 116 },
  9176. { 0xF209787BB47D6B85, 348, 124 },
  9177. { 0xB454E4A179DD1877, 375, 132 },
  9178. { 0x865B86925B9BC5C2, 402, 140 },
  9179. { 0xC83553C5C8965D3D, 428, 148 },
  9180. { 0x952AB45CFA97A0B3, 455, 156 },
  9181. { 0xDE469FBD99A05FE3, 481, 164 },
  9182. { 0xA59BC234DB398C25, 508, 172 },
  9183. { 0xF6C69A72A3989F5C, 534, 180 },
  9184. { 0xB7DCBF5354E9BECE, 561, 188 },
  9185. { 0x88FCF317F22241E2, 588, 196 },
  9186. { 0xCC20CE9BD35C78A5, 614, 204 },
  9187. { 0x98165AF37B2153DF, 641, 212 },
  9188. { 0xE2A0B5DC971F303A, 667, 220 },
  9189. { 0xA8D9D1535CE3B396, 694, 228 },
  9190. { 0xFB9B7CD9A4A7443C, 720, 236 },
  9191. { 0xBB764C4CA7A44410, 747, 244 },
  9192. { 0x8BAB8EEFB6409C1A, 774, 252 },
  9193. { 0xD01FEF10A657842C, 800, 260 },
  9194. { 0x9B10A4E5E9913129, 827, 268 },
  9195. { 0xE7109BFBA19C0C9D, 853, 276 },
  9196. { 0xAC2820D9623BF429, 880, 284 },
  9197. { 0x80444B5E7AA7CF85, 907, 292 },
  9198. { 0xBF21E44003ACDD2D, 933, 300 },
  9199. { 0x8E679C2F5E44FF8F, 960, 308 },
  9200. { 0xD433179D9C8CB841, 986, 316 },
  9201. { 0x9E19DB92B4E31BA9, 1013, 324 },
  9202. };
  9203. // This computation gives exactly the same results for k as
  9204. // k = ceil((kAlpha - e - 1) * 0.30102999566398114)
  9205. // for |e| <= 1500, but doesn't require floating-point operations.
  9206. // NB: log_10(2) ~= 78913 / 2^18
  9207. assert(e >= -1500);
  9208. assert(e <= 1500);
  9209. const int f = kAlpha - e - 1;
  9210. const int k = (f * 78913) / (1 << 18) + static_cast<int>(f > 0);
  9211. const int index = (-kCachedPowersMinDecExp + k + (kCachedPowersDecStep - 1)) / kCachedPowersDecStep;
  9212. assert(index >= 0);
  9213. assert(index < kCachedPowersSize);
  9214. static_cast<void>(kCachedPowersSize); // Fix warning.
  9215. const cached_power cached = kCachedPowers[index];
  9216. assert(kAlpha <= cached.e + e + 64);
  9217. assert(kGamma >= cached.e + e + 64);
  9218. return cached;
  9219. }
  9220. /*!
  9221. For n != 0, returns k, such that pow10 := 10^(k-1) <= n < 10^k.
  9222. For n == 0, returns 1 and sets pow10 := 1.
  9223. */
  9224. inline int find_largest_pow10(const uint32_t n, uint32_t& pow10)
  9225. {
  9226. // LCOV_EXCL_START
  9227. if (n >= 1000000000)
  9228. {
  9229. pow10 = 1000000000;
  9230. return 10;
  9231. }
  9232. // LCOV_EXCL_STOP
  9233. else if (n >= 100000000)
  9234. {
  9235. pow10 = 100000000;
  9236. return 9;
  9237. }
  9238. else if (n >= 10000000)
  9239. {
  9240. pow10 = 10000000;
  9241. return 8;
  9242. }
  9243. else if (n >= 1000000)
  9244. {
  9245. pow10 = 1000000;
  9246. return 7;
  9247. }
  9248. else if (n >= 100000)
  9249. {
  9250. pow10 = 100000;
  9251. return 6;
  9252. }
  9253. else if (n >= 10000)
  9254. {
  9255. pow10 = 10000;
  9256. return 5;
  9257. }
  9258. else if (n >= 1000)
  9259. {
  9260. pow10 = 1000;
  9261. return 4;
  9262. }
  9263. else if (n >= 100)
  9264. {
  9265. pow10 = 100;
  9266. return 3;
  9267. }
  9268. else if (n >= 10)
  9269. {
  9270. pow10 = 10;
  9271. return 2;
  9272. }
  9273. else
  9274. {
  9275. pow10 = 1;
  9276. return 1;
  9277. }
  9278. }
  9279. inline void grisu2_round(char* buf, int len, uint64_t dist, uint64_t delta,
  9280. uint64_t rest, uint64_t ten_k)
  9281. {
  9282. assert(len >= 1);
  9283. assert(dist <= delta);
  9284. assert(rest <= delta);
  9285. assert(ten_k > 0);
  9286. // <--------------------------- delta ---->
  9287. // <---- dist --------->
  9288. // --------------[------------------+-------------------]--------------
  9289. // M- w M+
  9290. //
  9291. // ten_k
  9292. // <------>
  9293. // <---- rest ---->
  9294. // --------------[------------------+----+--------------]--------------
  9295. // w V
  9296. // = buf * 10^k
  9297. //
  9298. // ten_k represents a unit-in-the-last-place in the decimal representation
  9299. // stored in buf.
  9300. // Decrement buf by ten_k while this takes buf closer to w.
  9301. // The tests are written in this order to avoid overflow in unsigned
  9302. // integer arithmetic.
  9303. while (rest < dist
  9304. and delta - rest >= ten_k
  9305. and (rest + ten_k < dist or dist - rest > rest + ten_k - dist))
  9306. {
  9307. assert(buf[len - 1] != '0');
  9308. buf[len - 1]--;
  9309. rest += ten_k;
  9310. }
  9311. }
  9312. /*!
  9313. Generates V = buffer * 10^decimal_exponent, such that M- <= V <= M+.
  9314. M- and M+ must be normalized and share the same exponent -60 <= e <= -32.
  9315. */
  9316. inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent,
  9317. diyfp M_minus, diyfp w, diyfp M_plus)
  9318. {
  9319. static_assert(kAlpha >= -60, "internal error");
  9320. static_assert(kGamma <= -32, "internal error");
  9321. // Generates the digits (and the exponent) of a decimal floating-point
  9322. // number V = buffer * 10^decimal_exponent in the range [M-, M+]. The diyfp's
  9323. // w, M- and M+ share the same exponent e, which satisfies alpha <= e <= gamma.
  9324. //
  9325. // <--------------------------- delta ---->
  9326. // <---- dist --------->
  9327. // --------------[------------------+-------------------]--------------
  9328. // M- w M+
  9329. //
  9330. // Grisu2 generates the digits of M+ from left to right and stops as soon as
  9331. // V is in [M-,M+].
  9332. assert(M_plus.e >= kAlpha);
  9333. assert(M_plus.e <= kGamma);
  9334. uint64_t delta = diyfp::sub(M_plus, M_minus).f; // (significand of (M+ - M-), implicit exponent is e)
  9335. uint64_t dist = diyfp::sub(M_plus, w ).f; // (significand of (M+ - w ), implicit exponent is e)
  9336. // Split M+ = f * 2^e into two parts p1 and p2 (note: e < 0):
  9337. //
  9338. // M+ = f * 2^e
  9339. // = ((f div 2^-e) * 2^-e + (f mod 2^-e)) * 2^e
  9340. // = ((p1 ) * 2^-e + (p2 )) * 2^e
  9341. // = p1 + p2 * 2^e
  9342. const diyfp one(uint64_t{1} << -M_plus.e, M_plus.e);
  9343. auto p1 = static_cast<uint32_t>(M_plus.f >> -one.e); // p1 = f div 2^-e (Since -e >= 32, p1 fits into a 32-bit int.)
  9344. uint64_t p2 = M_plus.f & (one.f - 1); // p2 = f mod 2^-e
  9345. // 1)
  9346. //
  9347. // Generate the digits of the integral part p1 = d[n-1]...d[1]d[0]
  9348. assert(p1 > 0);
  9349. uint32_t pow10;
  9350. const int k = find_largest_pow10(p1, pow10);
  9351. // 10^(k-1) <= p1 < 10^k, pow10 = 10^(k-1)
  9352. //
  9353. // p1 = (p1 div 10^(k-1)) * 10^(k-1) + (p1 mod 10^(k-1))
  9354. // = (d[k-1] ) * 10^(k-1) + (p1 mod 10^(k-1))
  9355. //
  9356. // M+ = p1 + p2 * 2^e
  9357. // = d[k-1] * 10^(k-1) + (p1 mod 10^(k-1)) + p2 * 2^e
  9358. // = d[k-1] * 10^(k-1) + ((p1 mod 10^(k-1)) * 2^-e + p2) * 2^e
  9359. // = d[k-1] * 10^(k-1) + ( rest) * 2^e
  9360. //
  9361. // Now generate the digits d[n] of p1 from left to right (n = k-1,...,0)
  9362. //
  9363. // p1 = d[k-1]...d[n] * 10^n + d[n-1]...d[0]
  9364. //
  9365. // but stop as soon as
  9366. //
  9367. // rest * 2^e = (d[n-1]...d[0] * 2^-e + p2) * 2^e <= delta * 2^e
  9368. int n = k;
  9369. while (n > 0)
  9370. {
  9371. // Invariants:
  9372. // M+ = buffer * 10^n + (p1 + p2 * 2^e) (buffer = 0 for n = k)
  9373. // pow10 = 10^(n-1) <= p1 < 10^n
  9374. //
  9375. const uint32_t d = p1 / pow10; // d = p1 div 10^(n-1)
  9376. const uint32_t r = p1 % pow10; // r = p1 mod 10^(n-1)
  9377. //
  9378. // M+ = buffer * 10^n + (d * 10^(n-1) + r) + p2 * 2^e
  9379. // = (buffer * 10 + d) * 10^(n-1) + (r + p2 * 2^e)
  9380. //
  9381. assert(d <= 9);
  9382. buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
  9383. //
  9384. // M+ = buffer * 10^(n-1) + (r + p2 * 2^e)
  9385. //
  9386. p1 = r;
  9387. n--;
  9388. //
  9389. // M+ = buffer * 10^n + (p1 + p2 * 2^e)
  9390. // pow10 = 10^n
  9391. //
  9392. // Now check if enough digits have been generated.
  9393. // Compute
  9394. //
  9395. // p1 + p2 * 2^e = (p1 * 2^-e + p2) * 2^e = rest * 2^e
  9396. //
  9397. // Note:
  9398. // Since rest and delta share the same exponent e, it suffices to
  9399. // compare the significands.
  9400. const uint64_t rest = (uint64_t{p1} << -one.e) + p2;
  9401. if (rest <= delta)
  9402. {
  9403. // V = buffer * 10^n, with M- <= V <= M+.
  9404. decimal_exponent += n;
  9405. // We may now just stop. But instead look if the buffer could be
  9406. // decremented to bring V closer to w.
  9407. //
  9408. // pow10 = 10^n is now 1 ulp in the decimal representation V.
  9409. // The rounding procedure works with diyfp's with an implicit
  9410. // exponent of e.
  9411. //
  9412. // 10^n = (10^n * 2^-e) * 2^e = ulp * 2^e
  9413. //
  9414. const uint64_t ten_n = uint64_t{pow10} << -one.e;
  9415. grisu2_round(buffer, length, dist, delta, rest, ten_n);
  9416. return;
  9417. }
  9418. pow10 /= 10;
  9419. //
  9420. // pow10 = 10^(n-1) <= p1 < 10^n
  9421. // Invariants restored.
  9422. }
  9423. // 2)
  9424. //
  9425. // The digits of the integral part have been generated:
  9426. //
  9427. // M+ = d[k-1]...d[1]d[0] + p2 * 2^e
  9428. // = buffer + p2 * 2^e
  9429. //
  9430. // Now generate the digits of the fractional part p2 * 2^e.
  9431. //
  9432. // Note:
  9433. // No decimal point is generated: the exponent is adjusted instead.
  9434. //
  9435. // p2 actually represents the fraction
  9436. //
  9437. // p2 * 2^e
  9438. // = p2 / 2^-e
  9439. // = d[-1] / 10^1 + d[-2] / 10^2 + ...
  9440. //
  9441. // Now generate the digits d[-m] of p1 from left to right (m = 1,2,...)
  9442. //
  9443. // p2 * 2^e = d[-1]d[-2]...d[-m] * 10^-m
  9444. // + 10^-m * (d[-m-1] / 10^1 + d[-m-2] / 10^2 + ...)
  9445. //
  9446. // using
  9447. //
  9448. // 10^m * p2 = ((10^m * p2) div 2^-e) * 2^-e + ((10^m * p2) mod 2^-e)
  9449. // = ( d) * 2^-e + ( r)
  9450. //
  9451. // or
  9452. // 10^m * p2 * 2^e = d + r * 2^e
  9453. //
  9454. // i.e.
  9455. //
  9456. // M+ = buffer + p2 * 2^e
  9457. // = buffer + 10^-m * (d + r * 2^e)
  9458. // = (buffer * 10^m + d) * 10^-m + 10^-m * r * 2^e
  9459. //
  9460. // and stop as soon as 10^-m * r * 2^e <= delta * 2^e
  9461. assert(p2 > delta);
  9462. int m = 0;
  9463. for (;;)
  9464. {
  9465. // Invariant:
  9466. // M+ = buffer * 10^-m + 10^-m * (d[-m-1] / 10 + d[-m-2] / 10^2 + ...) * 2^e
  9467. // = buffer * 10^-m + 10^-m * (p2 ) * 2^e
  9468. // = buffer * 10^-m + 10^-m * (1/10 * (10 * p2) ) * 2^e
  9469. // = buffer * 10^-m + 10^-m * (1/10 * ((10*p2 div 2^-e) * 2^-e + (10*p2 mod 2^-e)) * 2^e
  9470. //
  9471. assert(p2 <= UINT64_MAX / 10);
  9472. p2 *= 10;
  9473. const uint64_t d = p2 >> -one.e; // d = (10 * p2) div 2^-e
  9474. const uint64_t r = p2 & (one.f - 1); // r = (10 * p2) mod 2^-e
  9475. //
  9476. // M+ = buffer * 10^-m + 10^-m * (1/10 * (d * 2^-e + r) * 2^e
  9477. // = buffer * 10^-m + 10^-m * (1/10 * (d + r * 2^e))
  9478. // = (buffer * 10 + d) * 10^(-m-1) + 10^(-m-1) * r * 2^e
  9479. //
  9480. assert(d <= 9);
  9481. buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
  9482. //
  9483. // M+ = buffer * 10^(-m-1) + 10^(-m-1) * r * 2^e
  9484. //
  9485. p2 = r;
  9486. m++;
  9487. //
  9488. // M+ = buffer * 10^-m + 10^-m * p2 * 2^e
  9489. // Invariant restored.
  9490. // Check if enough digits have been generated.
  9491. //
  9492. // 10^-m * p2 * 2^e <= delta * 2^e
  9493. // p2 * 2^e <= 10^m * delta * 2^e
  9494. // p2 <= 10^m * delta
  9495. delta *= 10;
  9496. dist *= 10;
  9497. if (p2 <= delta)
  9498. {
  9499. break;
  9500. }
  9501. }
  9502. // V = buffer * 10^-m, with M- <= V <= M+.
  9503. decimal_exponent -= m;
  9504. // 1 ulp in the decimal representation is now 10^-m.
  9505. // Since delta and dist are now scaled by 10^m, we need to do the
  9506. // same with ulp in order to keep the units in sync.
  9507. //
  9508. // 10^m * 10^-m = 1 = 2^-e * 2^e = ten_m * 2^e
  9509. //
  9510. const uint64_t ten_m = one.f;
  9511. grisu2_round(buffer, length, dist, delta, p2, ten_m);
  9512. // By construction this algorithm generates the shortest possible decimal
  9513. // number (Loitsch, Theorem 6.2) which rounds back to w.
  9514. // For an input number of precision p, at least
  9515. //
  9516. // N = 1 + ceil(p * log_10(2))
  9517. //
  9518. // decimal digits are sufficient to identify all binary floating-point
  9519. // numbers (Matula, "In-and-Out conversions").
  9520. // This implies that the algorithm does not produce more than N decimal
  9521. // digits.
  9522. //
  9523. // N = 17 for p = 53 (IEEE double precision)
  9524. // N = 9 for p = 24 (IEEE single precision)
  9525. }
  9526. /*!
  9527. v = buf * 10^decimal_exponent
  9528. len is the length of the buffer (number of decimal digits)
  9529. The buffer must be large enough, i.e. >= max_digits10.
  9530. */
  9531. inline void grisu2(char* buf, int& len, int& decimal_exponent,
  9532. diyfp m_minus, diyfp v, diyfp m_plus)
  9533. {
  9534. assert(m_plus.e == m_minus.e);
  9535. assert(m_plus.e == v.e);
  9536. // --------(-----------------------+-----------------------)-------- (A)
  9537. // m- v m+
  9538. //
  9539. // --------------------(-----------+-----------------------)-------- (B)
  9540. // m- v m+
  9541. //
  9542. // First scale v (and m- and m+) such that the exponent is in the range
  9543. // [alpha, gamma].
  9544. const cached_power cached = get_cached_power_for_binary_exponent(m_plus.e);
  9545. const diyfp c_minus_k(cached.f, cached.e); // = c ~= 10^-k
  9546. // The exponent of the products is = v.e + c_minus_k.e + q and is in the range [alpha,gamma]
  9547. const diyfp w = diyfp::mul(v, c_minus_k);
  9548. const diyfp w_minus = diyfp::mul(m_minus, c_minus_k);
  9549. const diyfp w_plus = diyfp::mul(m_plus, c_minus_k);
  9550. // ----(---+---)---------------(---+---)---------------(---+---)----
  9551. // w- w w+
  9552. // = c*m- = c*v = c*m+
  9553. //
  9554. // diyfp::mul rounds its result and c_minus_k is approximated too. w, w- and
  9555. // w+ are now off by a small amount.
  9556. // In fact:
  9557. //
  9558. // w - v * 10^k < 1 ulp
  9559. //
  9560. // To account for this inaccuracy, add resp. subtract 1 ulp.
  9561. //
  9562. // --------+---[---------------(---+---)---------------]---+--------
  9563. // w- M- w M+ w+
  9564. //
  9565. // Now any number in [M-, M+] (bounds included) will round to w when input,
  9566. // regardless of how the input rounding algorithm breaks ties.
  9567. //
  9568. // And digit_gen generates the shortest possible such number in [M-, M+].
  9569. // Note that this does not mean that Grisu2 always generates the shortest
  9570. // possible number in the interval (m-, m+).
  9571. const diyfp M_minus(w_minus.f + 1, w_minus.e);
  9572. const diyfp M_plus (w_plus.f - 1, w_plus.e );
  9573. decimal_exponent = -cached.k; // = -(-k) = k
  9574. grisu2_digit_gen(buf, len, decimal_exponent, M_minus, w, M_plus);
  9575. }
  9576. /*!
  9577. v = buf * 10^decimal_exponent
  9578. len is the length of the buffer (number of decimal digits)
  9579. The buffer must be large enough, i.e. >= max_digits10.
  9580. */
  9581. template <typename FloatType>
  9582. void grisu2(char* buf, int& len, int& decimal_exponent, FloatType value)
  9583. {
  9584. static_assert(diyfp::kPrecision >= std::numeric_limits<FloatType>::digits + 3,
  9585. "internal error: not enough precision");
  9586. assert(std::isfinite(value));
  9587. assert(value > 0);
  9588. // If the neighbors (and boundaries) of 'value' are always computed for double-precision
  9589. // numbers, all float's can be recovered using strtod (and strtof). However, the resulting
  9590. // decimal representations are not exactly "short".
  9591. //
  9592. // The documentation for 'std::to_chars' (https://en.cppreference.com/w/cpp/utility/to_chars)
  9593. // says "value is converted to a string as if by std::sprintf in the default ("C") locale"
  9594. // and since sprintf promotes float's to double's, I think this is exactly what 'std::to_chars'
  9595. // does.
  9596. // On the other hand, the documentation for 'std::to_chars' requires that "parsing the
  9597. // representation using the corresponding std::from_chars function recovers value exactly". That
  9598. // indicates that single precision floating-point numbers should be recovered using
  9599. // 'std::strtof'.
  9600. //
  9601. // NB: If the neighbors are computed for single-precision numbers, there is a single float
  9602. // (7.0385307e-26f) which can't be recovered using strtod. The resulting double precision
  9603. // value is off by 1 ulp.
  9604. #if 0
  9605. const boundaries w = compute_boundaries(static_cast<double>(value));
  9606. #else
  9607. const boundaries w = compute_boundaries(value);
  9608. #endif
  9609. grisu2(buf, len, decimal_exponent, w.minus, w.w, w.plus);
  9610. }
  9611. /*!
  9612. @brief appends a decimal representation of e to buf
  9613. @return a pointer to the element following the exponent.
  9614. @pre -1000 < e < 1000
  9615. */
  9616. inline char* append_exponent(char* buf, int e)
  9617. {
  9618. assert(e > -1000);
  9619. assert(e < 1000);
  9620. if (e < 0)
  9621. {
  9622. e = -e;
  9623. *buf++ = '-';
  9624. }
  9625. else
  9626. {
  9627. *buf++ = '+';
  9628. }
  9629. auto k = static_cast<uint32_t>(e);
  9630. if (k < 10)
  9631. {
  9632. // Always print at least two digits in the exponent.
  9633. // This is for compatibility with printf("%g").
  9634. *buf++ = '0';
  9635. *buf++ = static_cast<char>('0' + k);
  9636. }
  9637. else if (k < 100)
  9638. {
  9639. *buf++ = static_cast<char>('0' + k / 10);
  9640. k %= 10;
  9641. *buf++ = static_cast<char>('0' + k);
  9642. }
  9643. else
  9644. {
  9645. *buf++ = static_cast<char>('0' + k / 100);
  9646. k %= 100;
  9647. *buf++ = static_cast<char>('0' + k / 10);
  9648. k %= 10;
  9649. *buf++ = static_cast<char>('0' + k);
  9650. }
  9651. return buf;
  9652. }
  9653. /*!
  9654. @brief prettify v = buf * 10^decimal_exponent
  9655. If v is in the range [10^min_exp, 10^max_exp) it will be printed in fixed-point
  9656. notation. Otherwise it will be printed in exponential notation.
  9657. @pre min_exp < 0
  9658. @pre max_exp > 0
  9659. */
  9660. inline char* format_buffer(char* buf, int len, int decimal_exponent,
  9661. int min_exp, int max_exp)
  9662. {
  9663. assert(min_exp < 0);
  9664. assert(max_exp > 0);
  9665. const int k = len;
  9666. const int n = len + decimal_exponent;
  9667. // v = buf * 10^(n-k)
  9668. // k is the length of the buffer (number of decimal digits)
  9669. // n is the position of the decimal point relative to the start of the buffer.
  9670. if (k <= n and n <= max_exp)
  9671. {
  9672. // digits[000]
  9673. // len <= max_exp + 2
  9674. std::memset(buf + k, '0', static_cast<size_t>(n - k));
  9675. // Make it look like a floating-point number (#362, #378)
  9676. buf[n + 0] = '.';
  9677. buf[n + 1] = '0';
  9678. return buf + (n + 2);
  9679. }
  9680. if (0 < n and n <= max_exp)
  9681. {
  9682. // dig.its
  9683. // len <= max_digits10 + 1
  9684. assert(k > n);
  9685. std::memmove(buf + (n + 1), buf + n, static_cast<size_t>(k - n));
  9686. buf[n] = '.';
  9687. return buf + (k + 1);
  9688. }
  9689. if (min_exp < n and n <= 0)
  9690. {
  9691. // 0.[000]digits
  9692. // len <= 2 + (-min_exp - 1) + max_digits10
  9693. std::memmove(buf + (2 + -n), buf, static_cast<size_t>(k));
  9694. buf[0] = '0';
  9695. buf[1] = '.';
  9696. std::memset(buf + 2, '0', static_cast<size_t>(-n));
  9697. return buf + (2 + (-n) + k);
  9698. }
  9699. if (k == 1)
  9700. {
  9701. // dE+123
  9702. // len <= 1 + 5
  9703. buf += 1;
  9704. }
  9705. else
  9706. {
  9707. // d.igitsE+123
  9708. // len <= max_digits10 + 1 + 5
  9709. std::memmove(buf + 2, buf + 1, static_cast<size_t>(k - 1));
  9710. buf[1] = '.';
  9711. buf += 1 + k;
  9712. }
  9713. *buf++ = 'e';
  9714. return append_exponent(buf, n - 1);
  9715. }
  9716. } // namespace dtoa_impl
  9717. /*!
  9718. @brief generates a decimal representation of the floating-point number value in [first, last).
  9719. The format of the resulting decimal representation is similar to printf's %g
  9720. format. Returns an iterator pointing past-the-end of the decimal representation.
  9721. @note The input number must be finite, i.e. NaN's and Inf's are not supported.
  9722. @note The buffer must be large enough.
  9723. @note The result is NOT null-terminated.
  9724. */
  9725. template <typename FloatType>
  9726. char* to_chars(char* first, const char* last, FloatType value)
  9727. {
  9728. static_cast<void>(last); // maybe unused - fix warning
  9729. assert(std::isfinite(value));
  9730. // Use signbit(value) instead of (value < 0) since signbit works for -0.
  9731. if (std::signbit(value))
  9732. {
  9733. value = -value;
  9734. *first++ = '-';
  9735. }
  9736. if (value == 0) // +-0
  9737. {
  9738. *first++ = '0';
  9739. // Make it look like a floating-point number (#362, #378)
  9740. *first++ = '.';
  9741. *first++ = '0';
  9742. return first;
  9743. }
  9744. assert(last - first >= std::numeric_limits<FloatType>::max_digits10);
  9745. // Compute v = buffer * 10^decimal_exponent.
  9746. // The decimal digits are stored in the buffer, which needs to be interpreted
  9747. // as an unsigned decimal integer.
  9748. // len is the length of the buffer, i.e. the number of decimal digits.
  9749. int len = 0;
  9750. int decimal_exponent = 0;
  9751. dtoa_impl::grisu2(first, len, decimal_exponent, value);
  9752. assert(len <= std::numeric_limits<FloatType>::max_digits10);
  9753. // Format the buffer like printf("%.*g", prec, value)
  9754. constexpr int kMinExp = -4;
  9755. // Use digits10 here to increase compatibility with version 2.
  9756. constexpr int kMaxExp = std::numeric_limits<FloatType>::digits10;
  9757. assert(last - first >= kMaxExp + 2);
  9758. assert(last - first >= 2 + (-kMinExp - 1) + std::numeric_limits<FloatType>::max_digits10);
  9759. assert(last - first >= std::numeric_limits<FloatType>::max_digits10 + 6);
  9760. return dtoa_impl::format_buffer(first, len, decimal_exponent, kMinExp, kMaxExp);
  9761. }
  9762. } // namespace detail
  9763. } // namespace nlohmann
  9764. // #include <nlohmann/detail/macro_scope.hpp>
  9765. // #include <nlohmann/detail/meta/cpp_future.hpp>
  9766. // #include <nlohmann/detail/output/binary_writer.hpp>
  9767. // #include <nlohmann/detail/output/output_adapters.hpp>
  9768. // #include <nlohmann/detail/value_t.hpp>
  9769. namespace nlohmann
  9770. {
  9771. namespace detail
  9772. {
  9773. ///////////////////
  9774. // serialization //
  9775. ///////////////////
  9776. /// how to treat decoding errors
  9777. enum class error_handler_t
  9778. {
  9779. strict, ///< throw a type_error exception in case of invalid UTF-8
  9780. replace, ///< replace invalid UTF-8 sequences with U+FFFD
  9781. ignore ///< ignore invalid UTF-8 sequences
  9782. };
  9783. template<typename BasicJsonType>
  9784. class serializer
  9785. {
  9786. using string_t = typename BasicJsonType::string_t;
  9787. using number_float_t = typename BasicJsonType::number_float_t;
  9788. using number_integer_t = typename BasicJsonType::number_integer_t;
  9789. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  9790. static constexpr uint8_t UTF8_ACCEPT = 0;
  9791. static constexpr uint8_t UTF8_REJECT = 1;
  9792. public:
  9793. /*!
  9794. @param[in] s output stream to serialize to
  9795. @param[in] ichar indentation character to use
  9796. @param[in] error_handler_ how to react on decoding errors
  9797. */
  9798. serializer(output_adapter_t<char> s, const char ichar,
  9799. error_handler_t error_handler_ = error_handler_t::strict)
  9800. : o(std::move(s))
  9801. , loc(std::localeconv())
  9802. , thousands_sep(loc->thousands_sep == nullptr ? '\0' : * (loc->thousands_sep))
  9803. , decimal_point(loc->decimal_point == nullptr ? '\0' : * (loc->decimal_point))
  9804. , indent_char(ichar)
  9805. , indent_string(512, indent_char)
  9806. , error_handler(error_handler_)
  9807. {}
  9808. // delete because of pointer members
  9809. serializer(const serializer&) = delete;
  9810. serializer& operator=(const serializer&) = delete;
  9811. serializer(serializer&&) = delete;
  9812. serializer& operator=(serializer&&) = delete;
  9813. ~serializer() = default;
  9814. /*!
  9815. @brief internal implementation of the serialization function
  9816. This function is called by the public member function dump and organizes
  9817. the serialization internally. The indentation level is propagated as
  9818. additional parameter. In case of arrays and objects, the function is
  9819. called recursively.
  9820. - strings and object keys are escaped using `escape_string()`
  9821. - integer numbers are converted implicitly via `operator<<`
  9822. - floating-point numbers are converted to a string using `"%g"` format
  9823. @param[in] val value to serialize
  9824. @param[in] pretty_print whether the output shall be pretty-printed
  9825. @param[in] indent_step the indent level
  9826. @param[in] current_indent the current indent level (only used internally)
  9827. */
  9828. void dump(const BasicJsonType& val, const bool pretty_print,
  9829. const bool ensure_ascii,
  9830. const unsigned int indent_step,
  9831. const unsigned int current_indent = 0)
  9832. {
  9833. switch (val.m_type)
  9834. {
  9835. case value_t::object:
  9836. {
  9837. if (val.m_value.object->empty())
  9838. {
  9839. o->write_characters("{}", 2);
  9840. return;
  9841. }
  9842. if (pretty_print)
  9843. {
  9844. o->write_characters("{\n", 2);
  9845. // variable to hold indentation for recursive calls
  9846. const auto new_indent = current_indent + indent_step;
  9847. if (JSON_UNLIKELY(indent_string.size() < new_indent))
  9848. {
  9849. indent_string.resize(indent_string.size() * 2, ' ');
  9850. }
  9851. // first n-1 elements
  9852. auto i = val.m_value.object->cbegin();
  9853. for (std::size_t cnt = 0; cnt < val.m_value.object->size() - 1; ++cnt, ++i)
  9854. {
  9855. o->write_characters(indent_string.c_str(), new_indent);
  9856. o->write_character('\"');
  9857. dump_escaped(i->first, ensure_ascii);
  9858. o->write_characters("\": ", 3);
  9859. dump(i->second, true, ensure_ascii, indent_step, new_indent);
  9860. o->write_characters(",\n", 2);
  9861. }
  9862. // last element
  9863. assert(i != val.m_value.object->cend());
  9864. assert(std::next(i) == val.m_value.object->cend());
  9865. o->write_characters(indent_string.c_str(), new_indent);
  9866. o->write_character('\"');
  9867. dump_escaped(i->first, ensure_ascii);
  9868. o->write_characters("\": ", 3);
  9869. dump(i->second, true, ensure_ascii, indent_step, new_indent);
  9870. o->write_character('\n');
  9871. o->write_characters(indent_string.c_str(), current_indent);
  9872. o->write_character('}');
  9873. }
  9874. else
  9875. {
  9876. o->write_character('{');
  9877. // first n-1 elements
  9878. auto i = val.m_value.object->cbegin();
  9879. for (std::size_t cnt = 0; cnt < val.m_value.object->size() - 1; ++cnt, ++i)
  9880. {
  9881. o->write_character('\"');
  9882. dump_escaped(i->first, ensure_ascii);
  9883. o->write_characters("\":", 2);
  9884. dump(i->second, false, ensure_ascii, indent_step, current_indent);
  9885. o->write_character(',');
  9886. }
  9887. // last element
  9888. assert(i != val.m_value.object->cend());
  9889. assert(std::next(i) == val.m_value.object->cend());
  9890. o->write_character('\"');
  9891. dump_escaped(i->first, ensure_ascii);
  9892. o->write_characters("\":", 2);
  9893. dump(i->second, false, ensure_ascii, indent_step, current_indent);
  9894. o->write_character('}');
  9895. }
  9896. return;
  9897. }
  9898. case value_t::array:
  9899. {
  9900. if (val.m_value.array->empty())
  9901. {
  9902. o->write_characters("[]", 2);
  9903. return;
  9904. }
  9905. if (pretty_print)
  9906. {
  9907. o->write_characters("[\n", 2);
  9908. // variable to hold indentation for recursive calls
  9909. const auto new_indent = current_indent + indent_step;
  9910. if (JSON_UNLIKELY(indent_string.size() < new_indent))
  9911. {
  9912. indent_string.resize(indent_string.size() * 2, ' ');
  9913. }
  9914. // first n-1 elements
  9915. for (auto i = val.m_value.array->cbegin();
  9916. i != val.m_value.array->cend() - 1; ++i)
  9917. {
  9918. o->write_characters(indent_string.c_str(), new_indent);
  9919. dump(*i, true, ensure_ascii, indent_step, new_indent);
  9920. o->write_characters(",\n", 2);
  9921. }
  9922. // last element
  9923. assert(not val.m_value.array->empty());
  9924. o->write_characters(indent_string.c_str(), new_indent);
  9925. dump(val.m_value.array->back(), true, ensure_ascii, indent_step, new_indent);
  9926. o->write_character('\n');
  9927. o->write_characters(indent_string.c_str(), current_indent);
  9928. o->write_character(']');
  9929. }
  9930. else
  9931. {
  9932. o->write_character('[');
  9933. // first n-1 elements
  9934. for (auto i = val.m_value.array->cbegin();
  9935. i != val.m_value.array->cend() - 1; ++i)
  9936. {
  9937. dump(*i, false, ensure_ascii, indent_step, current_indent);
  9938. o->write_character(',');
  9939. }
  9940. // last element
  9941. assert(not val.m_value.array->empty());
  9942. dump(val.m_value.array->back(), false, ensure_ascii, indent_step, current_indent);
  9943. o->write_character(']');
  9944. }
  9945. return;
  9946. }
  9947. case value_t::string:
  9948. {
  9949. o->write_character('\"');
  9950. dump_escaped(*val.m_value.string, ensure_ascii);
  9951. o->write_character('\"');
  9952. return;
  9953. }
  9954. case value_t::boolean:
  9955. {
  9956. if (val.m_value.boolean)
  9957. {
  9958. o->write_characters("true", 4);
  9959. }
  9960. else
  9961. {
  9962. o->write_characters("false", 5);
  9963. }
  9964. return;
  9965. }
  9966. case value_t::number_integer:
  9967. {
  9968. dump_integer(val.m_value.number_integer);
  9969. return;
  9970. }
  9971. case value_t::number_unsigned:
  9972. {
  9973. dump_integer(val.m_value.number_unsigned);
  9974. return;
  9975. }
  9976. case value_t::number_float:
  9977. {
  9978. dump_float(val.m_value.number_float);
  9979. return;
  9980. }
  9981. case value_t::discarded:
  9982. {
  9983. o->write_characters("<discarded>", 11);
  9984. return;
  9985. }
  9986. case value_t::null:
  9987. {
  9988. o->write_characters("null", 4);
  9989. return;
  9990. }
  9991. }
  9992. }
  9993. private:
  9994. /*!
  9995. @brief dump escaped string
  9996. Escape a string by replacing certain special characters by a sequence of an
  9997. escape character (backslash) and another character and other control
  9998. characters by a sequence of "\u" followed by a four-digit hex
  9999. representation. The escaped string is written to output stream @a o.
  10000. @param[in] s the string to escape
  10001. @param[in] ensure_ascii whether to escape non-ASCII characters with
  10002. \uXXXX sequences
  10003. @complexity Linear in the length of string @a s.
  10004. */
  10005. void dump_escaped(const string_t& s, const bool ensure_ascii)
  10006. {
  10007. uint32_t codepoint;
  10008. uint8_t state = UTF8_ACCEPT;
  10009. std::size_t bytes = 0; // number of bytes written to string_buffer
  10010. // number of bytes written at the point of the last valid byte
  10011. std::size_t bytes_after_last_accept = 0;
  10012. std::size_t undumped_chars = 0;
  10013. for (std::size_t i = 0; i < s.size(); ++i)
  10014. {
  10015. const auto byte = static_cast<uint8_t>(s[i]);
  10016. switch (decode(state, codepoint, byte))
  10017. {
  10018. case UTF8_ACCEPT: // decode found a new code point
  10019. {
  10020. switch (codepoint)
  10021. {
  10022. case 0x08: // backspace
  10023. {
  10024. string_buffer[bytes++] = '\\';
  10025. string_buffer[bytes++] = 'b';
  10026. break;
  10027. }
  10028. case 0x09: // horizontal tab
  10029. {
  10030. string_buffer[bytes++] = '\\';
  10031. string_buffer[bytes++] = 't';
  10032. break;
  10033. }
  10034. case 0x0A: // newline
  10035. {
  10036. string_buffer[bytes++] = '\\';
  10037. string_buffer[bytes++] = 'n';
  10038. break;
  10039. }
  10040. case 0x0C: // formfeed
  10041. {
  10042. string_buffer[bytes++] = '\\';
  10043. string_buffer[bytes++] = 'f';
  10044. break;
  10045. }
  10046. case 0x0D: // carriage return
  10047. {
  10048. string_buffer[bytes++] = '\\';
  10049. string_buffer[bytes++] = 'r';
  10050. break;
  10051. }
  10052. case 0x22: // quotation mark
  10053. {
  10054. string_buffer[bytes++] = '\\';
  10055. string_buffer[bytes++] = '\"';
  10056. break;
  10057. }
  10058. case 0x5C: // reverse solidus
  10059. {
  10060. string_buffer[bytes++] = '\\';
  10061. string_buffer[bytes++] = '\\';
  10062. break;
  10063. }
  10064. default:
  10065. {
  10066. // escape control characters (0x00..0x1F) or, if
  10067. // ensure_ascii parameter is used, non-ASCII characters
  10068. if ((codepoint <= 0x1F) or (ensure_ascii and (codepoint >= 0x7F)))
  10069. {
  10070. if (codepoint <= 0xFFFF)
  10071. {
  10072. (std::snprintf)(string_buffer.data() + bytes, 7, "\\u%04x",
  10073. static_cast<uint16_t>(codepoint));
  10074. bytes += 6;
  10075. }
  10076. else
  10077. {
  10078. (std::snprintf)(string_buffer.data() + bytes, 13, "\\u%04x\\u%04x",
  10079. static_cast<uint16_t>(0xD7C0 + (codepoint >> 10)),
  10080. static_cast<uint16_t>(0xDC00 + (codepoint & 0x3FF)));
  10081. bytes += 12;
  10082. }
  10083. }
  10084. else
  10085. {
  10086. // copy byte to buffer (all previous bytes
  10087. // been copied have in default case above)
  10088. string_buffer[bytes++] = s[i];
  10089. }
  10090. break;
  10091. }
  10092. }
  10093. // write buffer and reset index; there must be 13 bytes
  10094. // left, as this is the maximal number of bytes to be
  10095. // written ("\uxxxx\uxxxx\0") for one code point
  10096. if (string_buffer.size() - bytes < 13)
  10097. {
  10098. o->write_characters(string_buffer.data(), bytes);
  10099. bytes = 0;
  10100. }
  10101. // remember the byte position of this accept
  10102. bytes_after_last_accept = bytes;
  10103. undumped_chars = 0;
  10104. break;
  10105. }
  10106. case UTF8_REJECT: // decode found invalid UTF-8 byte
  10107. {
  10108. switch (error_handler)
  10109. {
  10110. case error_handler_t::strict:
  10111. {
  10112. std::string sn(3, '\0');
  10113. (std::snprintf)(&sn[0], sn.size(), "%.2X", byte);
  10114. JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + sn));
  10115. }
  10116. case error_handler_t::ignore:
  10117. case error_handler_t::replace:
  10118. {
  10119. // in case we saw this character the first time, we
  10120. // would like to read it again, because the byte
  10121. // may be OK for itself, but just not OK for the
  10122. // previous sequence
  10123. if (undumped_chars > 0)
  10124. {
  10125. --i;
  10126. }
  10127. // reset length buffer to the last accepted index;
  10128. // thus removing/ignoring the invalid characters
  10129. bytes = bytes_after_last_accept;
  10130. if (error_handler == error_handler_t::replace)
  10131. {
  10132. // add a replacement character
  10133. if (ensure_ascii)
  10134. {
  10135. string_buffer[bytes++] = '\\';
  10136. string_buffer[bytes++] = 'u';
  10137. string_buffer[bytes++] = 'f';
  10138. string_buffer[bytes++] = 'f';
  10139. string_buffer[bytes++] = 'f';
  10140. string_buffer[bytes++] = 'd';
  10141. }
  10142. else
  10143. {
  10144. string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xEF');
  10145. string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xBF');
  10146. string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xBD');
  10147. }
  10148. bytes_after_last_accept = bytes;
  10149. }
  10150. undumped_chars = 0;
  10151. // continue processing the string
  10152. state = UTF8_ACCEPT;
  10153. break;
  10154. }
  10155. }
  10156. break;
  10157. }
  10158. default: // decode found yet incomplete multi-byte code point
  10159. {
  10160. if (not ensure_ascii)
  10161. {
  10162. // code point will not be escaped - copy byte to buffer
  10163. string_buffer[bytes++] = s[i];
  10164. }
  10165. ++undumped_chars;
  10166. break;
  10167. }
  10168. }
  10169. }
  10170. // we finished processing the string
  10171. if (JSON_LIKELY(state == UTF8_ACCEPT))
  10172. {
  10173. // write buffer
  10174. if (bytes > 0)
  10175. {
  10176. o->write_characters(string_buffer.data(), bytes);
  10177. }
  10178. }
  10179. else
  10180. {
  10181. // we finish reading, but do not accept: string was incomplete
  10182. switch (error_handler)
  10183. {
  10184. case error_handler_t::strict:
  10185. {
  10186. std::string sn(3, '\0');
  10187. (std::snprintf)(&sn[0], sn.size(), "%.2X", static_cast<uint8_t>(s.back()));
  10188. JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + sn));
  10189. }
  10190. case error_handler_t::ignore:
  10191. {
  10192. // write all accepted bytes
  10193. o->write_characters(string_buffer.data(), bytes_after_last_accept);
  10194. break;
  10195. }
  10196. case error_handler_t::replace:
  10197. {
  10198. // write all accepted bytes
  10199. o->write_characters(string_buffer.data(), bytes_after_last_accept);
  10200. // add a replacement character
  10201. if (ensure_ascii)
  10202. {
  10203. o->write_characters("\\ufffd", 6);
  10204. }
  10205. else
  10206. {
  10207. o->write_characters("\xEF\xBF\xBD", 3);
  10208. }
  10209. break;
  10210. }
  10211. }
  10212. }
  10213. }
  10214. /*!
  10215. @brief dump an integer
  10216. Dump a given integer to output stream @a o. Works internally with
  10217. @a number_buffer.
  10218. @param[in] x integer number (signed or unsigned) to dump
  10219. @tparam NumberType either @a number_integer_t or @a number_unsigned_t
  10220. */
  10221. template<typename NumberType, detail::enable_if_t<
  10222. std::is_same<NumberType, number_unsigned_t>::value or
  10223. std::is_same<NumberType, number_integer_t>::value,
  10224. int> = 0>
  10225. void dump_integer(NumberType x)
  10226. {
  10227. // special case for "0"
  10228. if (x == 0)
  10229. {
  10230. o->write_character('0');
  10231. return;
  10232. }
  10233. const bool is_negative = std::is_same<NumberType, number_integer_t>::value and not (x >= 0); // see issue #755
  10234. std::size_t i = 0;
  10235. while (x != 0)
  10236. {
  10237. // spare 1 byte for '\0'
  10238. assert(i < number_buffer.size() - 1);
  10239. const auto digit = std::labs(static_cast<long>(x % 10));
  10240. number_buffer[i++] = static_cast<char>('0' + digit);
  10241. x /= 10;
  10242. }
  10243. if (is_negative)
  10244. {
  10245. // make sure there is capacity for the '-'
  10246. assert(i < number_buffer.size() - 2);
  10247. number_buffer[i++] = '-';
  10248. }
  10249. std::reverse(number_buffer.begin(), number_buffer.begin() + i);
  10250. o->write_characters(number_buffer.data(), i);
  10251. }
  10252. /*!
  10253. @brief dump a floating-point number
  10254. Dump a given floating-point number to output stream @a o. Works internally
  10255. with @a number_buffer.
  10256. @param[in] x floating-point number to dump
  10257. */
  10258. void dump_float(number_float_t x)
  10259. {
  10260. // NaN / inf
  10261. if (not std::isfinite(x))
  10262. {
  10263. o->write_characters("null", 4);
  10264. return;
  10265. }
  10266. // If number_float_t is an IEEE-754 single or double precision number,
  10267. // use the Grisu2 algorithm to produce short numbers which are
  10268. // guaranteed to round-trip, using strtof and strtod, resp.
  10269. //
  10270. // NB: The test below works if <long double> == <double>.
  10271. static constexpr bool is_ieee_single_or_double
  10272. = (std::numeric_limits<number_float_t>::is_iec559 and std::numeric_limits<number_float_t>::digits == 24 and std::numeric_limits<number_float_t>::max_exponent == 128) or
  10273. (std::numeric_limits<number_float_t>::is_iec559 and std::numeric_limits<number_float_t>::digits == 53 and std::numeric_limits<number_float_t>::max_exponent == 1024);
  10274. dump_float(x, std::integral_constant<bool, is_ieee_single_or_double>());
  10275. }
  10276. void dump_float(number_float_t x, std::true_type /*is_ieee_single_or_double*/)
  10277. {
  10278. char* begin = number_buffer.data();
  10279. char* end = ::nlohmann::detail::to_chars(begin, begin + number_buffer.size(), x);
  10280. o->write_characters(begin, static_cast<size_t>(end - begin));
  10281. }
  10282. void dump_float(number_float_t x, std::false_type /*is_ieee_single_or_double*/)
  10283. {
  10284. // get number of digits for a float -> text -> float round-trip
  10285. static constexpr auto d = std::numeric_limits<number_float_t>::max_digits10;
  10286. // the actual conversion
  10287. std::ptrdiff_t len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*g", d, x);
  10288. // negative value indicates an error
  10289. assert(len > 0);
  10290. // check if buffer was large enough
  10291. assert(static_cast<std::size_t>(len) < number_buffer.size());
  10292. // erase thousands separator
  10293. if (thousands_sep != '\0')
  10294. {
  10295. const auto end = std::remove(number_buffer.begin(),
  10296. number_buffer.begin() + len, thousands_sep);
  10297. std::fill(end, number_buffer.end(), '\0');
  10298. assert((end - number_buffer.begin()) <= len);
  10299. len = (end - number_buffer.begin());
  10300. }
  10301. // convert decimal point to '.'
  10302. if (decimal_point != '\0' and decimal_point != '.')
  10303. {
  10304. const auto dec_pos = std::find(number_buffer.begin(), number_buffer.end(), decimal_point);
  10305. if (dec_pos != number_buffer.end())
  10306. {
  10307. *dec_pos = '.';
  10308. }
  10309. }
  10310. o->write_characters(number_buffer.data(), static_cast<std::size_t>(len));
  10311. // determine if need to append ".0"
  10312. const bool value_is_int_like =
  10313. std::none_of(number_buffer.begin(), number_buffer.begin() + len + 1,
  10314. [](char c)
  10315. {
  10316. return (c == '.' or c == 'e');
  10317. });
  10318. if (value_is_int_like)
  10319. {
  10320. o->write_characters(".0", 2);
  10321. }
  10322. }
  10323. /*!
  10324. @brief check whether a string is UTF-8 encoded
  10325. The function checks each byte of a string whether it is UTF-8 encoded. The
  10326. result of the check is stored in the @a state parameter. The function must
  10327. be called initially with state 0 (accept). State 1 means the string must
  10328. be rejected, because the current byte is not allowed. If the string is
  10329. completely processed, but the state is non-zero, the string ended
  10330. prematurely; that is, the last byte indicated more bytes should have
  10331. followed.
  10332. @param[in,out] state the state of the decoding
  10333. @param[in,out] codep codepoint (valid only if resulting state is UTF8_ACCEPT)
  10334. @param[in] byte next byte to decode
  10335. @return new state
  10336. @note The function has been edited: a std::array is used.
  10337. @copyright Copyright (c) 2008-2009 Bjoern Hoehrmann <[email protected]>
  10338. @sa http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
  10339. */
  10340. static uint8_t decode(uint8_t& state, uint32_t& codep, const uint8_t byte) noexcept
  10341. {
  10342. static const std::array<uint8_t, 400> utf8d =
  10343. {
  10344. {
  10345. 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
  10346. 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
  10347. 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
  10348. 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
  10349. 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
  10350. 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
  10351. 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
  10352. 0xA, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x4, 0x3, 0x3, // E0..EF
  10353. 0xB, 0x6, 0x6, 0x6, 0x5, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, // F0..FF
  10354. 0x0, 0x1, 0x2, 0x3, 0x5, 0x8, 0x7, 0x1, 0x1, 0x1, 0x4, 0x6, 0x1, 0x1, 0x1, 0x1, // s0..s0
  10355. 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
  10356. 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
  10357. 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
  10358. 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
  10359. }
  10360. };
  10361. const uint8_t type = utf8d[byte];
  10362. codep = (state != UTF8_ACCEPT)
  10363. ? (byte & 0x3fu) | (codep << 6)
  10364. : static_cast<uint32_t>(0xff >> type) & (byte);
  10365. state = utf8d[256u + state * 16u + type];
  10366. return state;
  10367. }
  10368. private:
  10369. /// the output of the serializer
  10370. output_adapter_t<char> o = nullptr;
  10371. /// a (hopefully) large enough character buffer
  10372. std::array<char, 64> number_buffer{{}};
  10373. /// the locale
  10374. const std::lconv* loc = nullptr;
  10375. /// the locale's thousand separator character
  10376. const char thousands_sep = '\0';
  10377. /// the locale's decimal point character
  10378. const char decimal_point = '\0';
  10379. /// string buffer
  10380. std::array<char, 512> string_buffer{{}};
  10381. /// the indentation character
  10382. const char indent_char;
  10383. /// the indentation string
  10384. string_t indent_string;
  10385. /// error_handler how to react on decoding errors
  10386. const error_handler_t error_handler;
  10387. };
  10388. } // namespace detail
  10389. } // namespace nlohmann
  10390. // #include <nlohmann/detail/json_ref.hpp>
  10391. #include <initializer_list>
  10392. #include <utility>
  10393. // #include <nlohmann/detail/meta/type_traits.hpp>
  10394. namespace nlohmann
  10395. {
  10396. namespace detail
  10397. {
  10398. template<typename BasicJsonType>
  10399. class json_ref
  10400. {
  10401. public:
  10402. using value_type = BasicJsonType;
  10403. json_ref(value_type&& value)
  10404. : owned_value(std::move(value)), value_ref(&owned_value), is_rvalue(true)
  10405. {}
  10406. json_ref(const value_type& value)
  10407. : value_ref(const_cast<value_type*>(&value)), is_rvalue(false)
  10408. {}
  10409. json_ref(std::initializer_list<json_ref> init)
  10410. : owned_value(init), value_ref(&owned_value), is_rvalue(true)
  10411. {}
  10412. template <
  10413. class... Args,
  10414. enable_if_t<std::is_constructible<value_type, Args...>::value, int> = 0 >
  10415. json_ref(Args && ... args)
  10416. : owned_value(std::forward<Args>(args)...), value_ref(&owned_value),
  10417. is_rvalue(true) {}
  10418. // class should be movable only
  10419. json_ref(json_ref&&) = default;
  10420. json_ref(const json_ref&) = delete;
  10421. json_ref& operator=(const json_ref&) = delete;
  10422. json_ref& operator=(json_ref&&) = delete;
  10423. ~json_ref() = default;
  10424. value_type moved_or_copied() const
  10425. {
  10426. if (is_rvalue)
  10427. {
  10428. return std::move(*value_ref);
  10429. }
  10430. return *value_ref;
  10431. }
  10432. value_type const& operator*() const
  10433. {
  10434. return *static_cast<value_type const*>(value_ref);
  10435. }
  10436. value_type const* operator->() const
  10437. {
  10438. return static_cast<value_type const*>(value_ref);
  10439. }
  10440. private:
  10441. mutable value_type owned_value = nullptr;
  10442. value_type* value_ref = nullptr;
  10443. const bool is_rvalue;
  10444. };
  10445. } // namespace detail
  10446. } // namespace nlohmann
  10447. // #include <nlohmann/detail/json_pointer.hpp>
  10448. #include <cassert> // assert
  10449. #include <numeric> // accumulate
  10450. #include <string> // string
  10451. #include <vector> // vector
  10452. // #include <nlohmann/detail/macro_scope.hpp>
  10453. // #include <nlohmann/detail/exceptions.hpp>
  10454. // #include <nlohmann/detail/value_t.hpp>
  10455. namespace nlohmann
  10456. {
  10457. template<typename BasicJsonType>
  10458. class json_pointer
  10459. {
  10460. // allow basic_json to access private members
  10461. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  10462. friend class basic_json;
  10463. public:
  10464. /*!
  10465. @brief create JSON pointer
  10466. Create a JSON pointer according to the syntax described in
  10467. [Section 3 of RFC6901](https://tools.ietf.org/html/rfc6901#section-3).
  10468. @param[in] s string representing the JSON pointer; if omitted, the empty
  10469. string is assumed which references the whole JSON value
  10470. @throw parse_error.107 if the given JSON pointer @a s is nonempty and does
  10471. not begin with a slash (`/`); see example below
  10472. @throw parse_error.108 if a tilde (`~`) in the given JSON pointer @a s is
  10473. not followed by `0` (representing `~`) or `1` (representing `/`); see
  10474. example below
  10475. @liveexample{The example shows the construction several valid JSON pointers
  10476. as well as the exceptional behavior.,json_pointer}
  10477. @since version 2.0.0
  10478. */
  10479. explicit json_pointer(const std::string& s = "")
  10480. : reference_tokens(split(s))
  10481. {}
  10482. /*!
  10483. @brief return a string representation of the JSON pointer
  10484. @invariant For each JSON pointer `ptr`, it holds:
  10485. @code {.cpp}
  10486. ptr == json_pointer(ptr.to_string());
  10487. @endcode
  10488. @return a string representation of the JSON pointer
  10489. @liveexample{The example shows the result of `to_string`.,
  10490. json_pointer__to_string}
  10491. @since version 2.0.0
  10492. */
  10493. std::string to_string() const
  10494. {
  10495. return std::accumulate(reference_tokens.begin(), reference_tokens.end(),
  10496. std::string{},
  10497. [](const std::string & a, const std::string & b)
  10498. {
  10499. return a + "/" + escape(b);
  10500. });
  10501. }
  10502. /// @copydoc to_string()
  10503. operator std::string() const
  10504. {
  10505. return to_string();
  10506. }
  10507. /*!
  10508. @param[in] s reference token to be converted into an array index
  10509. @return integer representation of @a s
  10510. @throw out_of_range.404 if string @a s could not be converted to an integer
  10511. */
  10512. static int array_index(const std::string& s)
  10513. {
  10514. std::size_t processed_chars = 0;
  10515. const int res = std::stoi(s, &processed_chars);
  10516. // check if the string was completely read
  10517. if (JSON_UNLIKELY(processed_chars != s.size()))
  10518. {
  10519. JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'"));
  10520. }
  10521. return res;
  10522. }
  10523. private:
  10524. /*!
  10525. @brief remove and return last reference pointer
  10526. @throw out_of_range.405 if JSON pointer has no parent
  10527. */
  10528. std::string pop_back()
  10529. {
  10530. if (JSON_UNLIKELY(is_root()))
  10531. {
  10532. JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent"));
  10533. }
  10534. auto last = reference_tokens.back();
  10535. reference_tokens.pop_back();
  10536. return last;
  10537. }
  10538. /// return whether pointer points to the root document
  10539. bool is_root() const noexcept
  10540. {
  10541. return reference_tokens.empty();
  10542. }
  10543. json_pointer top() const
  10544. {
  10545. if (JSON_UNLIKELY(is_root()))
  10546. {
  10547. JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent"));
  10548. }
  10549. json_pointer result = *this;
  10550. result.reference_tokens = {reference_tokens[0]};
  10551. return result;
  10552. }
  10553. /*!
  10554. @brief create and return a reference to the pointed to value
  10555. @complexity Linear in the number of reference tokens.
  10556. @throw parse_error.109 if array index is not a number
  10557. @throw type_error.313 if value cannot be unflattened
  10558. */
  10559. BasicJsonType& get_and_create(BasicJsonType& j) const
  10560. {
  10561. using size_type = typename BasicJsonType::size_type;
  10562. auto result = &j;
  10563. // in case no reference tokens exist, return a reference to the JSON value
  10564. // j which will be overwritten by a primitive value
  10565. for (const auto& reference_token : reference_tokens)
  10566. {
  10567. switch (result->m_type)
  10568. {
  10569. case detail::value_t::null:
  10570. {
  10571. if (reference_token == "0")
  10572. {
  10573. // start a new array if reference token is 0
  10574. result = &result->operator[](0);
  10575. }
  10576. else
  10577. {
  10578. // start a new object otherwise
  10579. result = &result->operator[](reference_token);
  10580. }
  10581. break;
  10582. }
  10583. case detail::value_t::object:
  10584. {
  10585. // create an entry in the object
  10586. result = &result->operator[](reference_token);
  10587. break;
  10588. }
  10589. case detail::value_t::array:
  10590. {
  10591. // create an entry in the array
  10592. JSON_TRY
  10593. {
  10594. result = &result->operator[](static_cast<size_type>(array_index(reference_token)));
  10595. }
  10596. JSON_CATCH(std::invalid_argument&)
  10597. {
  10598. JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number"));
  10599. }
  10600. break;
  10601. }
  10602. /*
  10603. The following code is only reached if there exists a reference
  10604. token _and_ the current value is primitive. In this case, we have
  10605. an error situation, because primitive values may only occur as
  10606. single value; that is, with an empty list of reference tokens.
  10607. */
  10608. default:
  10609. JSON_THROW(detail::type_error::create(313, "invalid value to unflatten"));
  10610. }
  10611. }
  10612. return *result;
  10613. }
  10614. /*!
  10615. @brief return a reference to the pointed to value
  10616. @note This version does not throw if a value is not present, but tries to
  10617. create nested values instead. For instance, calling this function
  10618. with pointer `"/this/that"` on a null value is equivalent to calling
  10619. `operator[]("this").operator[]("that")` on that value, effectively
  10620. changing the null value to an object.
  10621. @param[in] ptr a JSON value
  10622. @return reference to the JSON value pointed to by the JSON pointer
  10623. @complexity Linear in the length of the JSON pointer.
  10624. @throw parse_error.106 if an array index begins with '0'
  10625. @throw parse_error.109 if an array index was not a number
  10626. @throw out_of_range.404 if the JSON pointer can not be resolved
  10627. */
  10628. BasicJsonType& get_unchecked(BasicJsonType* ptr) const
  10629. {
  10630. using size_type = typename BasicJsonType::size_type;
  10631. for (const auto& reference_token : reference_tokens)
  10632. {
  10633. // convert null values to arrays or objects before continuing
  10634. if (ptr->m_type == detail::value_t::null)
  10635. {
  10636. // check if reference token is a number
  10637. const bool nums =
  10638. std::all_of(reference_token.begin(), reference_token.end(),
  10639. [](const char x)
  10640. {
  10641. return (x >= '0' and x <= '9');
  10642. });
  10643. // change value to array for numbers or "-" or to object otherwise
  10644. *ptr = (nums or reference_token == "-")
  10645. ? detail::value_t::array
  10646. : detail::value_t::object;
  10647. }
  10648. switch (ptr->m_type)
  10649. {
  10650. case detail::value_t::object:
  10651. {
  10652. // use unchecked object access
  10653. ptr = &ptr->operator[](reference_token);
  10654. break;
  10655. }
  10656. case detail::value_t::array:
  10657. {
  10658. // error condition (cf. RFC 6901, Sect. 4)
  10659. if (JSON_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0'))
  10660. {
  10661. JSON_THROW(detail::parse_error::create(106, 0,
  10662. "array index '" + reference_token +
  10663. "' must not begin with '0'"));
  10664. }
  10665. if (reference_token == "-")
  10666. {
  10667. // explicitly treat "-" as index beyond the end
  10668. ptr = &ptr->operator[](ptr->m_value.array->size());
  10669. }
  10670. else
  10671. {
  10672. // convert array index to number; unchecked access
  10673. JSON_TRY
  10674. {
  10675. ptr = &ptr->operator[](
  10676. static_cast<size_type>(array_index(reference_token)));
  10677. }
  10678. JSON_CATCH(std::invalid_argument&)
  10679. {
  10680. JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number"));
  10681. }
  10682. }
  10683. break;
  10684. }
  10685. default:
  10686. JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'"));
  10687. }
  10688. }
  10689. return *ptr;
  10690. }
  10691. /*!
  10692. @throw parse_error.106 if an array index begins with '0'
  10693. @throw parse_error.109 if an array index was not a number
  10694. @throw out_of_range.402 if the array index '-' is used
  10695. @throw out_of_range.404 if the JSON pointer can not be resolved
  10696. */
  10697. BasicJsonType& get_checked(BasicJsonType* ptr) const
  10698. {
  10699. using size_type = typename BasicJsonType::size_type;
  10700. for (const auto& reference_token : reference_tokens)
  10701. {
  10702. switch (ptr->m_type)
  10703. {
  10704. case detail::value_t::object:
  10705. {
  10706. // note: at performs range check
  10707. ptr = &ptr->at(reference_token);
  10708. break;
  10709. }
  10710. case detail::value_t::array:
  10711. {
  10712. if (JSON_UNLIKELY(reference_token == "-"))
  10713. {
  10714. // "-" always fails the range check
  10715. JSON_THROW(detail::out_of_range::create(402,
  10716. "array index '-' (" + std::to_string(ptr->m_value.array->size()) +
  10717. ") is out of range"));
  10718. }
  10719. // error condition (cf. RFC 6901, Sect. 4)
  10720. if (JSON_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0'))
  10721. {
  10722. JSON_THROW(detail::parse_error::create(106, 0,
  10723. "array index '" + reference_token +
  10724. "' must not begin with '0'"));
  10725. }
  10726. // note: at performs range check
  10727. JSON_TRY
  10728. {
  10729. ptr = &ptr->at(static_cast<size_type>(array_index(reference_token)));
  10730. }
  10731. JSON_CATCH(std::invalid_argument&)
  10732. {
  10733. JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number"));
  10734. }
  10735. break;
  10736. }
  10737. default:
  10738. JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'"));
  10739. }
  10740. }
  10741. return *ptr;
  10742. }
  10743. /*!
  10744. @brief return a const reference to the pointed to value
  10745. @param[in] ptr a JSON value
  10746. @return const reference to the JSON value pointed to by the JSON
  10747. pointer
  10748. @throw parse_error.106 if an array index begins with '0'
  10749. @throw parse_error.109 if an array index was not a number
  10750. @throw out_of_range.402 if the array index '-' is used
  10751. @throw out_of_range.404 if the JSON pointer can not be resolved
  10752. */
  10753. const BasicJsonType& get_unchecked(const BasicJsonType* ptr) const
  10754. {
  10755. using size_type = typename BasicJsonType::size_type;
  10756. for (const auto& reference_token : reference_tokens)
  10757. {
  10758. switch (ptr->m_type)
  10759. {
  10760. case detail::value_t::object:
  10761. {
  10762. // use unchecked object access
  10763. ptr = &ptr->operator[](reference_token);
  10764. break;
  10765. }
  10766. case detail::value_t::array:
  10767. {
  10768. if (JSON_UNLIKELY(reference_token == "-"))
  10769. {
  10770. // "-" cannot be used for const access
  10771. JSON_THROW(detail::out_of_range::create(402,
  10772. "array index '-' (" + std::to_string(ptr->m_value.array->size()) +
  10773. ") is out of range"));
  10774. }
  10775. // error condition (cf. RFC 6901, Sect. 4)
  10776. if (JSON_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0'))
  10777. {
  10778. JSON_THROW(detail::parse_error::create(106, 0,
  10779. "array index '" + reference_token +
  10780. "' must not begin with '0'"));
  10781. }
  10782. // use unchecked array access
  10783. JSON_TRY
  10784. {
  10785. ptr = &ptr->operator[](
  10786. static_cast<size_type>(array_index(reference_token)));
  10787. }
  10788. JSON_CATCH(std::invalid_argument&)
  10789. {
  10790. JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number"));
  10791. }
  10792. break;
  10793. }
  10794. default:
  10795. JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'"));
  10796. }
  10797. }
  10798. return *ptr;
  10799. }
  10800. /*!
  10801. @throw parse_error.106 if an array index begins with '0'
  10802. @throw parse_error.109 if an array index was not a number
  10803. @throw out_of_range.402 if the array index '-' is used
  10804. @throw out_of_range.404 if the JSON pointer can not be resolved
  10805. */
  10806. const BasicJsonType& get_checked(const BasicJsonType* ptr) const
  10807. {
  10808. using size_type = typename BasicJsonType::size_type;
  10809. for (const auto& reference_token : reference_tokens)
  10810. {
  10811. switch (ptr->m_type)
  10812. {
  10813. case detail::value_t::object:
  10814. {
  10815. // note: at performs range check
  10816. ptr = &ptr->at(reference_token);
  10817. break;
  10818. }
  10819. case detail::value_t::array:
  10820. {
  10821. if (JSON_UNLIKELY(reference_token == "-"))
  10822. {
  10823. // "-" always fails the range check
  10824. JSON_THROW(detail::out_of_range::create(402,
  10825. "array index '-' (" + std::to_string(ptr->m_value.array->size()) +
  10826. ") is out of range"));
  10827. }
  10828. // error condition (cf. RFC 6901, Sect. 4)
  10829. if (JSON_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0'))
  10830. {
  10831. JSON_THROW(detail::parse_error::create(106, 0,
  10832. "array index '" + reference_token +
  10833. "' must not begin with '0'"));
  10834. }
  10835. // note: at performs range check
  10836. JSON_TRY
  10837. {
  10838. ptr = &ptr->at(static_cast<size_type>(array_index(reference_token)));
  10839. }
  10840. JSON_CATCH(std::invalid_argument&)
  10841. {
  10842. JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number"));
  10843. }
  10844. break;
  10845. }
  10846. default:
  10847. JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'"));
  10848. }
  10849. }
  10850. return *ptr;
  10851. }
  10852. /*!
  10853. @brief split the string input to reference tokens
  10854. @note This function is only called by the json_pointer constructor.
  10855. All exceptions below are documented there.
  10856. @throw parse_error.107 if the pointer is not empty or begins with '/'
  10857. @throw parse_error.108 if character '~' is not followed by '0' or '1'
  10858. */
  10859. static std::vector<std::string> split(const std::string& reference_string)
  10860. {
  10861. std::vector<std::string> result;
  10862. // special case: empty reference string -> no reference tokens
  10863. if (reference_string.empty())
  10864. {
  10865. return result;
  10866. }
  10867. // check if nonempty reference string begins with slash
  10868. if (JSON_UNLIKELY(reference_string[0] != '/'))
  10869. {
  10870. JSON_THROW(detail::parse_error::create(107, 1,
  10871. "JSON pointer must be empty or begin with '/' - was: '" +
  10872. reference_string + "'"));
  10873. }
  10874. // extract the reference tokens:
  10875. // - slash: position of the last read slash (or end of string)
  10876. // - start: position after the previous slash
  10877. for (
  10878. // search for the first slash after the first character
  10879. std::size_t slash = reference_string.find_first_of('/', 1),
  10880. // set the beginning of the first reference token
  10881. start = 1;
  10882. // we can stop if start == 0 (if slash == std::string::npos)
  10883. start != 0;
  10884. // set the beginning of the next reference token
  10885. // (will eventually be 0 if slash == std::string::npos)
  10886. start = (slash == std::string::npos) ? 0 : slash + 1,
  10887. // find next slash
  10888. slash = reference_string.find_first_of('/', start))
  10889. {
  10890. // use the text between the beginning of the reference token
  10891. // (start) and the last slash (slash).
  10892. auto reference_token = reference_string.substr(start, slash - start);
  10893. // check reference tokens are properly escaped
  10894. for (std::size_t pos = reference_token.find_first_of('~');
  10895. pos != std::string::npos;
  10896. pos = reference_token.find_first_of('~', pos + 1))
  10897. {
  10898. assert(reference_token[pos] == '~');
  10899. // ~ must be followed by 0 or 1
  10900. if (JSON_UNLIKELY(pos == reference_token.size() - 1 or
  10901. (reference_token[pos + 1] != '0' and
  10902. reference_token[pos + 1] != '1')))
  10903. {
  10904. JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'"));
  10905. }
  10906. }
  10907. // finally, store the reference token
  10908. unescape(reference_token);
  10909. result.push_back(reference_token);
  10910. }
  10911. return result;
  10912. }
  10913. /*!
  10914. @brief replace all occurrences of a substring by another string
  10915. @param[in,out] s the string to manipulate; changed so that all
  10916. occurrences of @a f are replaced with @a t
  10917. @param[in] f the substring to replace with @a t
  10918. @param[in] t the string to replace @a f
  10919. @pre The search string @a f must not be empty. **This precondition is
  10920. enforced with an assertion.**
  10921. @since version 2.0.0
  10922. */
  10923. static void replace_substring(std::string& s, const std::string& f,
  10924. const std::string& t)
  10925. {
  10926. assert(not f.empty());
  10927. for (auto pos = s.find(f); // find first occurrence of f
  10928. pos != std::string::npos; // make sure f was found
  10929. s.replace(pos, f.size(), t), // replace with t, and
  10930. pos = s.find(f, pos + t.size())) // find next occurrence of f
  10931. {}
  10932. }
  10933. /// escape "~" to "~0" and "/" to "~1"
  10934. static std::string escape(std::string s)
  10935. {
  10936. replace_substring(s, "~", "~0");
  10937. replace_substring(s, "/", "~1");
  10938. return s;
  10939. }
  10940. /// unescape "~1" to tilde and "~0" to slash (order is important!)
  10941. static void unescape(std::string& s)
  10942. {
  10943. replace_substring(s, "~1", "/");
  10944. replace_substring(s, "~0", "~");
  10945. }
  10946. /*!
  10947. @param[in] reference_string the reference string to the current value
  10948. @param[in] value the value to consider
  10949. @param[in,out] result the result object to insert values to
  10950. @note Empty objects or arrays are flattened to `null`.
  10951. */
  10952. static void flatten(const std::string& reference_string,
  10953. const BasicJsonType& value,
  10954. BasicJsonType& result)
  10955. {
  10956. switch (value.m_type)
  10957. {
  10958. case detail::value_t::array:
  10959. {
  10960. if (value.m_value.array->empty())
  10961. {
  10962. // flatten empty array as null
  10963. result[reference_string] = nullptr;
  10964. }
  10965. else
  10966. {
  10967. // iterate array and use index as reference string
  10968. for (std::size_t i = 0; i < value.m_value.array->size(); ++i)
  10969. {
  10970. flatten(reference_string + "/" + std::to_string(i),
  10971. value.m_value.array->operator[](i), result);
  10972. }
  10973. }
  10974. break;
  10975. }
  10976. case detail::value_t::object:
  10977. {
  10978. if (value.m_value.object->empty())
  10979. {
  10980. // flatten empty object as null
  10981. result[reference_string] = nullptr;
  10982. }
  10983. else
  10984. {
  10985. // iterate object and use keys as reference string
  10986. for (const auto& element : *value.m_value.object)
  10987. {
  10988. flatten(reference_string + "/" + escape(element.first), element.second, result);
  10989. }
  10990. }
  10991. break;
  10992. }
  10993. default:
  10994. {
  10995. // add primitive value with its reference string
  10996. result[reference_string] = value;
  10997. break;
  10998. }
  10999. }
  11000. }
  11001. /*!
  11002. @param[in] value flattened JSON
  11003. @return unflattened JSON
  11004. @throw parse_error.109 if array index is not a number
  11005. @throw type_error.314 if value is not an object
  11006. @throw type_error.315 if object values are not primitive
  11007. @throw type_error.313 if value cannot be unflattened
  11008. */
  11009. static BasicJsonType
  11010. unflatten(const BasicJsonType& value)
  11011. {
  11012. if (JSON_UNLIKELY(not value.is_object()))
  11013. {
  11014. JSON_THROW(detail::type_error::create(314, "only objects can be unflattened"));
  11015. }
  11016. BasicJsonType result;
  11017. // iterate the JSON object values
  11018. for (const auto& element : *value.m_value.object)
  11019. {
  11020. if (JSON_UNLIKELY(not element.second.is_primitive()))
  11021. {
  11022. JSON_THROW(detail::type_error::create(315, "values in object must be primitive"));
  11023. }
  11024. // assign value to reference pointed to by JSON pointer; Note that if
  11025. // the JSON pointer is "" (i.e., points to the whole value), function
  11026. // get_and_create returns a reference to result itself. An assignment
  11027. // will then create a primitive value.
  11028. json_pointer(element.first).get_and_create(result) = element.second;
  11029. }
  11030. return result;
  11031. }
  11032. friend bool operator==(json_pointer const& lhs,
  11033. json_pointer const& rhs) noexcept
  11034. {
  11035. return (lhs.reference_tokens == rhs.reference_tokens);
  11036. }
  11037. friend bool operator!=(json_pointer const& lhs,
  11038. json_pointer const& rhs) noexcept
  11039. {
  11040. return not (lhs == rhs);
  11041. }
  11042. /// the reference tokens
  11043. std::vector<std::string> reference_tokens;
  11044. };
  11045. } // namespace nlohmann
  11046. // #include <nlohmann/adl_serializer.hpp>
  11047. #include <utility>
  11048. // #include <nlohmann/detail/conversions/from_json.hpp>
  11049. // #include <nlohmann/detail/conversions/to_json.hpp>
  11050. namespace nlohmann
  11051. {
  11052. template<typename, typename>
  11053. struct adl_serializer
  11054. {
  11055. /*!
  11056. @brief convert a JSON value to any value type
  11057. This function is usually called by the `get()` function of the
  11058. @ref basic_json class (either explicit or via conversion operators).
  11059. @param[in] j JSON value to read from
  11060. @param[in,out] val value to write to
  11061. */
  11062. template<typename BasicJsonType, typename ValueType>
  11063. static auto from_json(BasicJsonType&& j, ValueType& val) noexcept(
  11064. noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), val)))
  11065. -> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), val), void())
  11066. {
  11067. ::nlohmann::from_json(std::forward<BasicJsonType>(j), val);
  11068. }
  11069. /*!
  11070. @brief convert any value type to a JSON value
  11071. This function is usually called by the constructors of the @ref basic_json
  11072. class.
  11073. @param[in,out] j JSON value to write to
  11074. @param[in] val value to read from
  11075. */
  11076. template <typename BasicJsonType, typename ValueType>
  11077. static auto to_json(BasicJsonType& j, ValueType&& val) noexcept(
  11078. noexcept(::nlohmann::to_json(j, std::forward<ValueType>(val))))
  11079. -> decltype(::nlohmann::to_json(j, std::forward<ValueType>(val)), void())
  11080. {
  11081. ::nlohmann::to_json(j, std::forward<ValueType>(val));
  11082. }
  11083. };
  11084. } // namespace nlohmann
  11085. /*!
  11086. @brief namespace for Niels Lohmann
  11087. @see https://github.com/nlohmann
  11088. @since version 1.0.0
  11089. */
  11090. namespace nlohmann
  11091. {
  11092. /*!
  11093. @brief a class to store JSON values
  11094. @tparam ObjectType type for JSON objects (`std::map` by default; will be used
  11095. in @ref object_t)
  11096. @tparam ArrayType type for JSON arrays (`std::vector` by default; will be used
  11097. in @ref array_t)
  11098. @tparam StringType type for JSON strings and object keys (`std::string` by
  11099. default; will be used in @ref string_t)
  11100. @tparam BooleanType type for JSON booleans (`bool` by default; will be used
  11101. in @ref boolean_t)
  11102. @tparam NumberIntegerType type for JSON integer numbers (`int64_t` by
  11103. default; will be used in @ref number_integer_t)
  11104. @tparam NumberUnsignedType type for JSON unsigned integer numbers (@c
  11105. `uint64_t` by default; will be used in @ref number_unsigned_t)
  11106. @tparam NumberFloatType type for JSON floating-point numbers (`double` by
  11107. default; will be used in @ref number_float_t)
  11108. @tparam AllocatorType type of the allocator to use (`std::allocator` by
  11109. default)
  11110. @tparam JSONSerializer the serializer to resolve internal calls to `to_json()`
  11111. and `from_json()` (@ref adl_serializer by default)
  11112. @requirement The class satisfies the following concept requirements:
  11113. - Basic
  11114. - [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible):
  11115. JSON values can be default constructed. The result will be a JSON null
  11116. value.
  11117. - [MoveConstructible](https://en.cppreference.com/w/cpp/named_req/MoveConstructible):
  11118. A JSON value can be constructed from an rvalue argument.
  11119. - [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible):
  11120. A JSON value can be copy-constructed from an lvalue expression.
  11121. - [MoveAssignable](https://en.cppreference.com/w/cpp/named_req/MoveAssignable):
  11122. A JSON value van be assigned from an rvalue argument.
  11123. - [CopyAssignable](https://en.cppreference.com/w/cpp/named_req/CopyAssignable):
  11124. A JSON value can be copy-assigned from an lvalue expression.
  11125. - [Destructible](https://en.cppreference.com/w/cpp/named_req/Destructible):
  11126. JSON values can be destructed.
  11127. - Layout
  11128. - [StandardLayoutType](https://en.cppreference.com/w/cpp/named_req/StandardLayoutType):
  11129. JSON values have
  11130. [standard layout](https://en.cppreference.com/w/cpp/language/data_members#Standard_layout):
  11131. All non-static data members are private and standard layout types, the
  11132. class has no virtual functions or (virtual) base classes.
  11133. - Library-wide
  11134. - [EqualityComparable](https://en.cppreference.com/w/cpp/named_req/EqualityComparable):
  11135. JSON values can be compared with `==`, see @ref
  11136. operator==(const_reference,const_reference).
  11137. - [LessThanComparable](https://en.cppreference.com/w/cpp/named_req/LessThanComparable):
  11138. JSON values can be compared with `<`, see @ref
  11139. operator<(const_reference,const_reference).
  11140. - [Swappable](https://en.cppreference.com/w/cpp/named_req/Swappable):
  11141. Any JSON lvalue or rvalue of can be swapped with any lvalue or rvalue of
  11142. other compatible types, using unqualified function call @ref swap().
  11143. - [NullablePointer](https://en.cppreference.com/w/cpp/named_req/NullablePointer):
  11144. JSON values can be compared against `std::nullptr_t` objects which are used
  11145. to model the `null` value.
  11146. - Container
  11147. - [Container](https://en.cppreference.com/w/cpp/named_req/Container):
  11148. JSON values can be used like STL containers and provide iterator access.
  11149. - [ReversibleContainer](https://en.cppreference.com/w/cpp/named_req/ReversibleContainer);
  11150. JSON values can be used like STL containers and provide reverse iterator
  11151. access.
  11152. @invariant The member variables @a m_value and @a m_type have the following
  11153. relationship:
  11154. - If `m_type == value_t::object`, then `m_value.object != nullptr`.
  11155. - If `m_type == value_t::array`, then `m_value.array != nullptr`.
  11156. - If `m_type == value_t::string`, then `m_value.string != nullptr`.
  11157. The invariants are checked by member function assert_invariant().
  11158. @internal
  11159. @note ObjectType trick from http://stackoverflow.com/a/9860911
  11160. @endinternal
  11161. @see [RFC 7159: The JavaScript Object Notation (JSON) Data Interchange
  11162. Format](http://rfc7159.net/rfc7159)
  11163. @since version 1.0.0
  11164. @nosubgrouping
  11165. */
  11166. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  11167. class basic_json
  11168. {
  11169. private:
  11170. template<detail::value_t> friend struct detail::external_constructor;
  11171. friend ::nlohmann::json_pointer<basic_json>;
  11172. friend ::nlohmann::detail::parser<basic_json>;
  11173. friend ::nlohmann::detail::serializer<basic_json>;
  11174. template<typename BasicJsonType>
  11175. friend class ::nlohmann::detail::iter_impl;
  11176. template<typename BasicJsonType, typename CharType>
  11177. friend class ::nlohmann::detail::binary_writer;
  11178. template<typename BasicJsonType, typename SAX>
  11179. friend class ::nlohmann::detail::binary_reader;
  11180. template<typename BasicJsonType>
  11181. friend class ::nlohmann::detail::json_sax_dom_parser;
  11182. template<typename BasicJsonType>
  11183. friend class ::nlohmann::detail::json_sax_dom_callback_parser;
  11184. /// workaround type for MSVC
  11185. using basic_json_t = NLOHMANN_BASIC_JSON_TPL;
  11186. // convenience aliases for types residing in namespace detail;
  11187. using lexer = ::nlohmann::detail::lexer<basic_json>;
  11188. using parser = ::nlohmann::detail::parser<basic_json>;
  11189. using primitive_iterator_t = ::nlohmann::detail::primitive_iterator_t;
  11190. template<typename BasicJsonType>
  11191. using internal_iterator = ::nlohmann::detail::internal_iterator<BasicJsonType>;
  11192. template<typename BasicJsonType>
  11193. using iter_impl = ::nlohmann::detail::iter_impl<BasicJsonType>;
  11194. template<typename Iterator>
  11195. using iteration_proxy = ::nlohmann::detail::iteration_proxy<Iterator>;
  11196. template<typename Base> using json_reverse_iterator = ::nlohmann::detail::json_reverse_iterator<Base>;
  11197. template<typename CharType>
  11198. using output_adapter_t = ::nlohmann::detail::output_adapter_t<CharType>;
  11199. using binary_reader = ::nlohmann::detail::binary_reader<basic_json>;
  11200. template<typename CharType> using binary_writer = ::nlohmann::detail::binary_writer<basic_json, CharType>;
  11201. using serializer = ::nlohmann::detail::serializer<basic_json>;
  11202. public:
  11203. using value_t = detail::value_t;
  11204. /// JSON Pointer, see @ref nlohmann::json_pointer
  11205. using json_pointer = ::nlohmann::json_pointer<basic_json>;
  11206. template<typename T, typename SFINAE>
  11207. using json_serializer = JSONSerializer<T, SFINAE>;
  11208. /// how to treat decoding errors
  11209. using error_handler_t = detail::error_handler_t;
  11210. /// helper type for initializer lists of basic_json values
  11211. using initializer_list_t = std::initializer_list<detail::json_ref<basic_json>>;
  11212. using input_format_t = detail::input_format_t;
  11213. /// SAX interface type, see @ref nlohmann::json_sax
  11214. using json_sax_t = json_sax<basic_json>;
  11215. ////////////////
  11216. // exceptions //
  11217. ////////////////
  11218. /// @name exceptions
  11219. /// Classes to implement user-defined exceptions.
  11220. /// @{
  11221. /// @copydoc detail::exception
  11222. using exception = detail::exception;
  11223. /// @copydoc detail::parse_error
  11224. using parse_error = detail::parse_error;
  11225. /// @copydoc detail::invalid_iterator
  11226. using invalid_iterator = detail::invalid_iterator;
  11227. /// @copydoc detail::type_error
  11228. using type_error = detail::type_error;
  11229. /// @copydoc detail::out_of_range
  11230. using out_of_range = detail::out_of_range;
  11231. /// @copydoc detail::other_error
  11232. using other_error = detail::other_error;
  11233. /// @}
  11234. /////////////////////
  11235. // container types //
  11236. /////////////////////
  11237. /// @name container types
  11238. /// The canonic container types to use @ref basic_json like any other STL
  11239. /// container.
  11240. /// @{
  11241. /// the type of elements in a basic_json container
  11242. using value_type = basic_json;
  11243. /// the type of an element reference
  11244. using reference = value_type&;
  11245. /// the type of an element const reference
  11246. using const_reference = const value_type&;
  11247. /// a type to represent differences between iterators
  11248. using difference_type = std::ptrdiff_t;
  11249. /// a type to represent container sizes
  11250. using size_type = std::size_t;
  11251. /// the allocator type
  11252. using allocator_type = AllocatorType<basic_json>;
  11253. /// the type of an element pointer
  11254. using pointer = typename std::allocator_traits<allocator_type>::pointer;
  11255. /// the type of an element const pointer
  11256. using const_pointer = typename std::allocator_traits<allocator_type>::const_pointer;
  11257. /// an iterator for a basic_json container
  11258. using iterator = iter_impl<basic_json>;
  11259. /// a const iterator for a basic_json container
  11260. using const_iterator = iter_impl<const basic_json>;
  11261. /// a reverse iterator for a basic_json container
  11262. using reverse_iterator = json_reverse_iterator<typename basic_json::iterator>;
  11263. /// a const reverse iterator for a basic_json container
  11264. using const_reverse_iterator = json_reverse_iterator<typename basic_json::const_iterator>;
  11265. /// @}
  11266. /*!
  11267. @brief returns the allocator associated with the container
  11268. */
  11269. static allocator_type get_allocator()
  11270. {
  11271. return allocator_type();
  11272. }
  11273. /*!
  11274. @brief returns version information on the library
  11275. This function returns a JSON object with information about the library,
  11276. including the version number and information on the platform and compiler.
  11277. @return JSON object holding version information
  11278. key | description
  11279. ----------- | ---------------
  11280. `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).
  11281. `copyright` | The copyright line for the library as string.
  11282. `name` | The name of the library as string.
  11283. `platform` | The used platform as string. Possible values are `win32`, `linux`, `apple`, `unix`, and `unknown`.
  11284. `url` | The URL of the project as string.
  11285. `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).
  11286. @liveexample{The following code shows an example output of the `meta()`
  11287. function.,meta}
  11288. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  11289. changes to any JSON value.
  11290. @complexity Constant.
  11291. @since 2.1.0
  11292. */
  11293. static basic_json meta()
  11294. {
  11295. basic_json result;
  11296. result["copyright"] = "(C) 2013-2017 Niels Lohmann";
  11297. result["name"] = "JSON for Modern C++";
  11298. result["url"] = "https://github.com/nlohmann/json";
  11299. result["version"]["string"] =
  11300. std::to_string(NLOHMANN_JSON_VERSION_MAJOR) + "." +
  11301. std::to_string(NLOHMANN_JSON_VERSION_MINOR) + "." +
  11302. std::to_string(NLOHMANN_JSON_VERSION_PATCH);
  11303. result["version"]["major"] = NLOHMANN_JSON_VERSION_MAJOR;
  11304. result["version"]["minor"] = NLOHMANN_JSON_VERSION_MINOR;
  11305. result["version"]["patch"] = NLOHMANN_JSON_VERSION_PATCH;
  11306. #ifdef _WIN32
  11307. result["platform"] = "win32";
  11308. #elif defined __linux__
  11309. result["platform"] = "linux";
  11310. #elif defined __APPLE__
  11311. result["platform"] = "apple";
  11312. #elif defined __unix__
  11313. result["platform"] = "unix";
  11314. #else
  11315. result["platform"] = "unknown";
  11316. #endif
  11317. #if defined(__ICC) || defined(__INTEL_COMPILER)
  11318. result["compiler"] = {{"family", "icc"}, {"version", __INTEL_COMPILER}};
  11319. #elif defined(__clang__)
  11320. result["compiler"] = {{"family", "clang"}, {"version", __clang_version__}};
  11321. #elif defined(__GNUC__) || defined(__GNUG__)
  11322. result["compiler"] = {{"family", "gcc"}, {"version", std::to_string(__GNUC__) + "." + std::to_string(__GNUC_MINOR__) + "." + std::to_string(__GNUC_PATCHLEVEL__)}};
  11323. #elif defined(__HP_cc) || defined(__HP_aCC)
  11324. result["compiler"] = "hp"
  11325. #elif defined(__IBMCPP__)
  11326. result["compiler"] = {{"family", "ilecpp"}, {"version", __IBMCPP__}};
  11327. #elif defined(_MSC_VER)
  11328. result["compiler"] = {{"family", "msvc"}, {"version", _MSC_VER}};
  11329. #elif defined(__PGI)
  11330. result["compiler"] = {{"family", "pgcpp"}, {"version", __PGI}};
  11331. #elif defined(__SUNPRO_CC)
  11332. result["compiler"] = {{"family", "sunpro"}, {"version", __SUNPRO_CC}};
  11333. #else
  11334. result["compiler"] = {{"family", "unknown"}, {"version", "unknown"}};
  11335. #endif
  11336. #ifdef __cplusplus
  11337. result["compiler"]["c++"] = std::to_string(__cplusplus);
  11338. #else
  11339. result["compiler"]["c++"] = "unknown";
  11340. #endif
  11341. return result;
  11342. }
  11343. ///////////////////////////
  11344. // JSON value data types //
  11345. ///////////////////////////
  11346. /// @name JSON value data types
  11347. /// The data types to store a JSON value. These types are derived from
  11348. /// the template arguments passed to class @ref basic_json.
  11349. /// @{
  11350. #if defined(JSON_HAS_CPP_14)
  11351. // Use transparent comparator if possible, combined with perfect forwarding
  11352. // on find() and count() calls prevents unnecessary string construction.
  11353. using object_comparator_t = std::less<>;
  11354. #else
  11355. using object_comparator_t = std::less<StringType>;
  11356. #endif
  11357. /*!
  11358. @brief a type for an object
  11359. [RFC 7159](http://rfc7159.net/rfc7159) describes JSON objects as follows:
  11360. > An object is an unordered collection of zero or more name/value pairs,
  11361. > where a name is a string and a value is a string, number, boolean, null,
  11362. > object, or array.
  11363. To store objects in C++, a type is defined by the template parameters
  11364. described below.
  11365. @tparam ObjectType the container to store objects (e.g., `std::map` or
  11366. `std::unordered_map`)
  11367. @tparam StringType the type of the keys or names (e.g., `std::string`).
  11368. The comparison function `std::less<StringType>` is used to order elements
  11369. inside the container.
  11370. @tparam AllocatorType the allocator to use for objects (e.g.,
  11371. `std::allocator`)
  11372. #### Default type
  11373. With the default values for @a ObjectType (`std::map`), @a StringType
  11374. (`std::string`), and @a AllocatorType (`std::allocator`), the default
  11375. value for @a object_t is:
  11376. @code {.cpp}
  11377. std::map<
  11378. std::string, // key_type
  11379. basic_json, // value_type
  11380. std::less<std::string>, // key_compare
  11381. std::allocator<std::pair<const std::string, basic_json>> // allocator_type
  11382. >
  11383. @endcode
  11384. #### Behavior
  11385. The choice of @a object_t influences the behavior of the JSON class. With
  11386. the default type, objects have the following behavior:
  11387. - When all names are unique, objects will be interoperable in the sense
  11388. that all software implementations receiving that object will agree on
  11389. the name-value mappings.
  11390. - When the names within an object are not unique, it is unspecified which
  11391. one of the values for a given key will be chosen. For instance,
  11392. `{"key": 2, "key": 1}` could be equal to either `{"key": 1}` or
  11393. `{"key": 2}`.
  11394. - Internally, name/value pairs are stored in lexicographical order of the
  11395. names. Objects will also be serialized (see @ref dump) in this order.
  11396. For instance, `{"b": 1, "a": 2}` and `{"a": 2, "b": 1}` will be stored
  11397. and serialized as `{"a": 2, "b": 1}`.
  11398. - When comparing objects, the order of the name/value pairs is irrelevant.
  11399. This makes objects interoperable in the sense that they will not be
  11400. affected by these differences. For instance, `{"b": 1, "a": 2}` and
  11401. `{"a": 2, "b": 1}` will be treated as equal.
  11402. #### Limits
  11403. [RFC 7159](http://rfc7159.net/rfc7159) specifies:
  11404. > An implementation may set limits on the maximum depth of nesting.
  11405. In this class, the object's limit of nesting is not explicitly constrained.
  11406. However, a maximum depth of nesting may be introduced by the compiler or
  11407. runtime environment. A theoretical limit can be queried by calling the
  11408. @ref max_size function of a JSON object.
  11409. #### Storage
  11410. Objects are stored as pointers in a @ref basic_json type. That is, for any
  11411. access to object values, a pointer of type `object_t*` must be
  11412. dereferenced.
  11413. @sa @ref array_t -- type for an array value
  11414. @since version 1.0.0
  11415. @note The order name/value pairs are added to the object is *not*
  11416. preserved by the library. Therefore, iterating an object may return
  11417. name/value pairs in a different order than they were originally stored. In
  11418. fact, keys will be traversed in alphabetical order as `std::map` with
  11419. `std::less` is used by default. Please note this behavior conforms to [RFC
  11420. 7159](http://rfc7159.net/rfc7159), because any order implements the
  11421. specified "unordered" nature of JSON objects.
  11422. */
  11423. using object_t = ObjectType<StringType,
  11424. basic_json,
  11425. object_comparator_t,
  11426. AllocatorType<std::pair<const StringType,
  11427. basic_json>>>;
  11428. /*!
  11429. @brief a type for an array
  11430. [RFC 7159](http://rfc7159.net/rfc7159) describes JSON arrays as follows:
  11431. > An array is an ordered sequence of zero or more values.
  11432. To store objects in C++, a type is defined by the template parameters
  11433. explained below.
  11434. @tparam ArrayType container type to store arrays (e.g., `std::vector` or
  11435. `std::list`)
  11436. @tparam AllocatorType allocator to use for arrays (e.g., `std::allocator`)
  11437. #### Default type
  11438. With the default values for @a ArrayType (`std::vector`) and @a
  11439. AllocatorType (`std::allocator`), the default value for @a array_t is:
  11440. @code {.cpp}
  11441. std::vector<
  11442. basic_json, // value_type
  11443. std::allocator<basic_json> // allocator_type
  11444. >
  11445. @endcode
  11446. #### Limits
  11447. [RFC 7159](http://rfc7159.net/rfc7159) specifies:
  11448. > An implementation may set limits on the maximum depth of nesting.
  11449. In this class, the array's limit of nesting is not explicitly constrained.
  11450. However, a maximum depth of nesting may be introduced by the compiler or
  11451. runtime environment. A theoretical limit can be queried by calling the
  11452. @ref max_size function of a JSON array.
  11453. #### Storage
  11454. Arrays are stored as pointers in a @ref basic_json type. That is, for any
  11455. access to array values, a pointer of type `array_t*` must be dereferenced.
  11456. @sa @ref object_t -- type for an object value
  11457. @since version 1.0.0
  11458. */
  11459. using array_t = ArrayType<basic_json, AllocatorType<basic_json>>;
  11460. /*!
  11461. @brief a type for a string
  11462. [RFC 7159](http://rfc7159.net/rfc7159) describes JSON strings as follows:
  11463. > A string is a sequence of zero or more Unicode characters.
  11464. To store objects in C++, a type is defined by the template parameter
  11465. described below. Unicode values are split by the JSON class into
  11466. byte-sized characters during deserialization.
  11467. @tparam StringType the container to store strings (e.g., `std::string`).
  11468. Note this container is used for keys/names in objects, see @ref object_t.
  11469. #### Default type
  11470. With the default values for @a StringType (`std::string`), the default
  11471. value for @a string_t is:
  11472. @code {.cpp}
  11473. std::string
  11474. @endcode
  11475. #### Encoding
  11476. Strings are stored in UTF-8 encoding. Therefore, functions like
  11477. `std::string::size()` or `std::string::length()` return the number of
  11478. bytes in the string rather than the number of characters or glyphs.
  11479. #### String comparison
  11480. [RFC 7159](http://rfc7159.net/rfc7159) states:
  11481. > Software implementations are typically required to test names of object
  11482. > members for equality. Implementations that transform the textual
  11483. > representation into sequences of Unicode code units and then perform the
  11484. > comparison numerically, code unit by code unit, are interoperable in the
  11485. > sense that implementations will agree in all cases on equality or
  11486. > inequality of two strings. For example, implementations that compare
  11487. > strings with escaped characters unconverted may incorrectly find that
  11488. > `"a\\b"` and `"a\u005Cb"` are not equal.
  11489. This implementation is interoperable as it does compare strings code unit
  11490. by code unit.
  11491. #### Storage
  11492. String values are stored as pointers in a @ref basic_json type. That is,
  11493. for any access to string values, a pointer of type `string_t*` must be
  11494. dereferenced.
  11495. @since version 1.0.0
  11496. */
  11497. using string_t = StringType;
  11498. /*!
  11499. @brief a type for a boolean
  11500. [RFC 7159](http://rfc7159.net/rfc7159) implicitly describes a boolean as a
  11501. type which differentiates the two literals `true` and `false`.
  11502. To store objects in C++, a type is defined by the template parameter @a
  11503. BooleanType which chooses the type to use.
  11504. #### Default type
  11505. With the default values for @a BooleanType (`bool`), the default value for
  11506. @a boolean_t is:
  11507. @code {.cpp}
  11508. bool
  11509. @endcode
  11510. #### Storage
  11511. Boolean values are stored directly inside a @ref basic_json type.
  11512. @since version 1.0.0
  11513. */
  11514. using boolean_t = BooleanType;
  11515. /*!
  11516. @brief a type for a number (integer)
  11517. [RFC 7159](http://rfc7159.net/rfc7159) describes numbers as follows:
  11518. > The representation of numbers is similar to that used in most
  11519. > programming languages. A number is represented in base 10 using decimal
  11520. > digits. It contains an integer component that may be prefixed with an
  11521. > optional minus sign, which may be followed by a fraction part and/or an
  11522. > exponent part. Leading zeros are not allowed. (...) Numeric values that
  11523. > cannot be represented in the grammar below (such as Infinity and NaN)
  11524. > are not permitted.
  11525. This description includes both integer and floating-point numbers.
  11526. However, C++ allows more precise storage if it is known whether the number
  11527. is a signed integer, an unsigned integer or a floating-point number.
  11528. Therefore, three different types, @ref number_integer_t, @ref
  11529. number_unsigned_t and @ref number_float_t are used.
  11530. To store integer numbers in C++, a type is defined by the template
  11531. parameter @a NumberIntegerType which chooses the type to use.
  11532. #### Default type
  11533. With the default values for @a NumberIntegerType (`int64_t`), the default
  11534. value for @a number_integer_t is:
  11535. @code {.cpp}
  11536. int64_t
  11537. @endcode
  11538. #### Default behavior
  11539. - The restrictions about leading zeros is not enforced in C++. Instead,
  11540. leading zeros in integer literals lead to an interpretation as octal
  11541. number. Internally, the value will be stored as decimal number. For
  11542. instance, the C++ integer literal `010` will be serialized to `8`.
  11543. During deserialization, leading zeros yield an error.
  11544. - Not-a-number (NaN) values will be serialized to `null`.
  11545. #### Limits
  11546. [RFC 7159](http://rfc7159.net/rfc7159) specifies:
  11547. > An implementation may set limits on the range and precision of numbers.
  11548. When the default type is used, the maximal integer number that can be
  11549. stored is `9223372036854775807` (INT64_MAX) and the minimal integer number
  11550. that can be stored is `-9223372036854775808` (INT64_MIN). Integer numbers
  11551. that are out of range will yield over/underflow when used in a
  11552. constructor. During deserialization, too large or small integer numbers
  11553. will be automatically be stored as @ref number_unsigned_t or @ref
  11554. number_float_t.
  11555. [RFC 7159](http://rfc7159.net/rfc7159) further states:
  11556. > Note that when such software is used, numbers that are integers and are
  11557. > in the range \f$[-2^{53}+1, 2^{53}-1]\f$ are interoperable in the sense
  11558. > that implementations will agree exactly on their numeric values.
  11559. As this range is a subrange of the exactly supported range [INT64_MIN,
  11560. INT64_MAX], this class's integer type is interoperable.
  11561. #### Storage
  11562. Integer number values are stored directly inside a @ref basic_json type.
  11563. @sa @ref number_float_t -- type for number values (floating-point)
  11564. @sa @ref number_unsigned_t -- type for number values (unsigned integer)
  11565. @since version 1.0.0
  11566. */
  11567. using number_integer_t = NumberIntegerType;
  11568. /*!
  11569. @brief a type for a number (unsigned)
  11570. [RFC 7159](http://rfc7159.net/rfc7159) describes numbers as follows:
  11571. > The representation of numbers is similar to that used in most
  11572. > programming languages. A number is represented in base 10 using decimal
  11573. > digits. It contains an integer component that may be prefixed with an
  11574. > optional minus sign, which may be followed by a fraction part and/or an
  11575. > exponent part. Leading zeros are not allowed. (...) Numeric values that
  11576. > cannot be represented in the grammar below (such as Infinity and NaN)
  11577. > are not permitted.
  11578. This description includes both integer and floating-point numbers.
  11579. However, C++ allows more precise storage if it is known whether the number
  11580. is a signed integer, an unsigned integer or a floating-point number.
  11581. Therefore, three different types, @ref number_integer_t, @ref
  11582. number_unsigned_t and @ref number_float_t are used.
  11583. To store unsigned integer numbers in C++, a type is defined by the
  11584. template parameter @a NumberUnsignedType which chooses the type to use.
  11585. #### Default type
  11586. With the default values for @a NumberUnsignedType (`uint64_t`), the
  11587. default value for @a number_unsigned_t is:
  11588. @code {.cpp}
  11589. uint64_t
  11590. @endcode
  11591. #### Default behavior
  11592. - The restrictions about leading zeros is not enforced in C++. Instead,
  11593. leading zeros in integer literals lead to an interpretation as octal
  11594. number. Internally, the value will be stored as decimal number. For
  11595. instance, the C++ integer literal `010` will be serialized to `8`.
  11596. During deserialization, leading zeros yield an error.
  11597. - Not-a-number (NaN) values will be serialized to `null`.
  11598. #### Limits
  11599. [RFC 7159](http://rfc7159.net/rfc7159) specifies:
  11600. > An implementation may set limits on the range and precision of numbers.
  11601. When the default type is used, the maximal integer number that can be
  11602. stored is `18446744073709551615` (UINT64_MAX) and the minimal integer
  11603. number that can be stored is `0`. Integer numbers that are out of range
  11604. will yield over/underflow when used in a constructor. During
  11605. deserialization, too large or small integer numbers will be automatically
  11606. be stored as @ref number_integer_t or @ref number_float_t.
  11607. [RFC 7159](http://rfc7159.net/rfc7159) further states:
  11608. > Note that when such software is used, numbers that are integers and are
  11609. > in the range \f$[-2^{53}+1, 2^{53}-1]\f$ are interoperable in the sense
  11610. > that implementations will agree exactly on their numeric values.
  11611. As this range is a subrange (when considered in conjunction with the
  11612. number_integer_t type) of the exactly supported range [0, UINT64_MAX],
  11613. this class's integer type is interoperable.
  11614. #### Storage
  11615. Integer number values are stored directly inside a @ref basic_json type.
  11616. @sa @ref number_float_t -- type for number values (floating-point)
  11617. @sa @ref number_integer_t -- type for number values (integer)
  11618. @since version 2.0.0
  11619. */
  11620. using number_unsigned_t = NumberUnsignedType;
  11621. /*!
  11622. @brief a type for a number (floating-point)
  11623. [RFC 7159](http://rfc7159.net/rfc7159) describes numbers as follows:
  11624. > The representation of numbers is similar to that used in most
  11625. > programming languages. A number is represented in base 10 using decimal
  11626. > digits. It contains an integer component that may be prefixed with an
  11627. > optional minus sign, which may be followed by a fraction part and/or an
  11628. > exponent part. Leading zeros are not allowed. (...) Numeric values that
  11629. > cannot be represented in the grammar below (such as Infinity and NaN)
  11630. > are not permitted.
  11631. This description includes both integer and floating-point numbers.
  11632. However, C++ allows more precise storage if it is known whether the number
  11633. is a signed integer, an unsigned integer or a floating-point number.
  11634. Therefore, three different types, @ref number_integer_t, @ref
  11635. number_unsigned_t and @ref number_float_t are used.
  11636. To store floating-point numbers in C++, a type is defined by the template
  11637. parameter @a NumberFloatType which chooses the type to use.
  11638. #### Default type
  11639. With the default values for @a NumberFloatType (`double`), the default
  11640. value for @a number_float_t is:
  11641. @code {.cpp}
  11642. double
  11643. @endcode
  11644. #### Default behavior
  11645. - The restrictions about leading zeros is not enforced in C++. Instead,
  11646. leading zeros in floating-point literals will be ignored. Internally,
  11647. the value will be stored as decimal number. For instance, the C++
  11648. floating-point literal `01.2` will be serialized to `1.2`. During
  11649. deserialization, leading zeros yield an error.
  11650. - Not-a-number (NaN) values will be serialized to `null`.
  11651. #### Limits
  11652. [RFC 7159](http://rfc7159.net/rfc7159) states:
  11653. > This specification allows implementations to set limits on the range and
  11654. > precision of numbers accepted. Since software that implements IEEE
  11655. > 754-2008 binary64 (double precision) numbers is generally available and
  11656. > widely used, good interoperability can be achieved by implementations
  11657. > that expect no more precision or range than these provide, in the sense
  11658. > that implementations will approximate JSON numbers within the expected
  11659. > precision.
  11660. This implementation does exactly follow this approach, as it uses double
  11661. precision floating-point numbers. Note values smaller than
  11662. `-1.79769313486232e+308` and values greater than `1.79769313486232e+308`
  11663. will be stored as NaN internally and be serialized to `null`.
  11664. #### Storage
  11665. Floating-point number values are stored directly inside a @ref basic_json
  11666. type.
  11667. @sa @ref number_integer_t -- type for number values (integer)
  11668. @sa @ref number_unsigned_t -- type for number values (unsigned integer)
  11669. @since version 1.0.0
  11670. */
  11671. using number_float_t = NumberFloatType;
  11672. /// @}
  11673. private:
  11674. /// helper for exception-safe object creation
  11675. template<typename T, typename... Args>
  11676. static T* create(Args&& ... args)
  11677. {
  11678. AllocatorType<T> alloc;
  11679. using AllocatorTraits = std::allocator_traits<AllocatorType<T>>;
  11680. auto deleter = [&](T * object)
  11681. {
  11682. AllocatorTraits::deallocate(alloc, object, 1);
  11683. };
  11684. std::unique_ptr<T, decltype(deleter)> object(AllocatorTraits::allocate(alloc, 1), deleter);
  11685. AllocatorTraits::construct(alloc, object.get(), std::forward<Args>(args)...);
  11686. assert(object != nullptr);
  11687. return object.release();
  11688. }
  11689. ////////////////////////
  11690. // JSON value storage //
  11691. ////////////////////////
  11692. /*!
  11693. @brief a JSON value
  11694. The actual storage for a JSON value of the @ref basic_json class. This
  11695. union combines the different storage types for the JSON value types
  11696. defined in @ref value_t.
  11697. JSON type | value_t type | used type
  11698. --------- | --------------- | ------------------------
  11699. object | object | pointer to @ref object_t
  11700. array | array | pointer to @ref array_t
  11701. string | string | pointer to @ref string_t
  11702. boolean | boolean | @ref boolean_t
  11703. number | number_integer | @ref number_integer_t
  11704. number | number_unsigned | @ref number_unsigned_t
  11705. number | number_float | @ref number_float_t
  11706. null | null | *no value is stored*
  11707. @note Variable-length types (objects, arrays, and strings) are stored as
  11708. pointers. The size of the union should not exceed 64 bits if the default
  11709. value types are used.
  11710. @since version 1.0.0
  11711. */
  11712. union json_value
  11713. {
  11714. /// object (stored with pointer to save storage)
  11715. object_t* object;
  11716. /// array (stored with pointer to save storage)
  11717. array_t* array;
  11718. /// string (stored with pointer to save storage)
  11719. string_t* string;
  11720. /// boolean
  11721. boolean_t boolean;
  11722. /// number (integer)
  11723. number_integer_t number_integer;
  11724. /// number (unsigned integer)
  11725. number_unsigned_t number_unsigned;
  11726. /// number (floating-point)
  11727. number_float_t number_float;
  11728. /// default constructor (for null values)
  11729. json_value() = default;
  11730. /// constructor for booleans
  11731. json_value(boolean_t v) noexcept : boolean(v) {}
  11732. /// constructor for numbers (integer)
  11733. json_value(number_integer_t v) noexcept : number_integer(v) {}
  11734. /// constructor for numbers (unsigned)
  11735. json_value(number_unsigned_t v) noexcept : number_unsigned(v) {}
  11736. /// constructor for numbers (floating-point)
  11737. json_value(number_float_t v) noexcept : number_float(v) {}
  11738. /// constructor for empty values of a given type
  11739. json_value(value_t t)
  11740. {
  11741. switch (t)
  11742. {
  11743. case value_t::object:
  11744. {
  11745. object = create<object_t>();
  11746. break;
  11747. }
  11748. case value_t::array:
  11749. {
  11750. array = create<array_t>();
  11751. break;
  11752. }
  11753. case value_t::string:
  11754. {
  11755. string = create<string_t>("");
  11756. break;
  11757. }
  11758. case value_t::boolean:
  11759. {
  11760. boolean = boolean_t(false);
  11761. break;
  11762. }
  11763. case value_t::number_integer:
  11764. {
  11765. number_integer = number_integer_t(0);
  11766. break;
  11767. }
  11768. case value_t::number_unsigned:
  11769. {
  11770. number_unsigned = number_unsigned_t(0);
  11771. break;
  11772. }
  11773. case value_t::number_float:
  11774. {
  11775. number_float = number_float_t(0.0);
  11776. break;
  11777. }
  11778. case value_t::null:
  11779. {
  11780. object = nullptr; // silence warning, see #821
  11781. break;
  11782. }
  11783. default:
  11784. {
  11785. object = nullptr; // silence warning, see #821
  11786. if (JSON_UNLIKELY(t == value_t::null))
  11787. {
  11788. JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.5.0")); // LCOV_EXCL_LINE
  11789. }
  11790. break;
  11791. }
  11792. }
  11793. }
  11794. /// constructor for strings
  11795. json_value(const string_t& value)
  11796. {
  11797. string = create<string_t>(value);
  11798. }
  11799. /// constructor for rvalue strings
  11800. json_value(string_t&& value)
  11801. {
  11802. string = create<string_t>(std::move(value));
  11803. }
  11804. /// constructor for objects
  11805. json_value(const object_t& value)
  11806. {
  11807. object = create<object_t>(value);
  11808. }
  11809. /// constructor for rvalue objects
  11810. json_value(object_t&& value)
  11811. {
  11812. object = create<object_t>(std::move(value));
  11813. }
  11814. /// constructor for arrays
  11815. json_value(const array_t& value)
  11816. {
  11817. array = create<array_t>(value);
  11818. }
  11819. /// constructor for rvalue arrays
  11820. json_value(array_t&& value)
  11821. {
  11822. array = create<array_t>(std::move(value));
  11823. }
  11824. void destroy(value_t t) noexcept
  11825. {
  11826. switch (t)
  11827. {
  11828. case value_t::object:
  11829. {
  11830. AllocatorType<object_t> alloc;
  11831. std::allocator_traits<decltype(alloc)>::destroy(alloc, object);
  11832. std::allocator_traits<decltype(alloc)>::deallocate(alloc, object, 1);
  11833. break;
  11834. }
  11835. case value_t::array:
  11836. {
  11837. AllocatorType<array_t> alloc;
  11838. std::allocator_traits<decltype(alloc)>::destroy(alloc, array);
  11839. std::allocator_traits<decltype(alloc)>::deallocate(alloc, array, 1);
  11840. break;
  11841. }
  11842. case value_t::string:
  11843. {
  11844. AllocatorType<string_t> alloc;
  11845. std::allocator_traits<decltype(alloc)>::destroy(alloc, string);
  11846. std::allocator_traits<decltype(alloc)>::deallocate(alloc, string, 1);
  11847. break;
  11848. }
  11849. default:
  11850. {
  11851. break;
  11852. }
  11853. }
  11854. }
  11855. };
  11856. /*!
  11857. @brief checks the class invariants
  11858. This function asserts the class invariants. It needs to be called at the
  11859. end of every constructor to make sure that created objects respect the
  11860. invariant. Furthermore, it has to be called each time the type of a JSON
  11861. value is changed, because the invariant expresses a relationship between
  11862. @a m_type and @a m_value.
  11863. */
  11864. void assert_invariant() const noexcept
  11865. {
  11866. assert(m_type != value_t::object or m_value.object != nullptr);
  11867. assert(m_type != value_t::array or m_value.array != nullptr);
  11868. assert(m_type != value_t::string or m_value.string != nullptr);
  11869. }
  11870. public:
  11871. //////////////////////////
  11872. // JSON parser callback //
  11873. //////////////////////////
  11874. /*!
  11875. @brief parser event types
  11876. The parser callback distinguishes the following events:
  11877. - `object_start`: the parser read `{` and started to process a JSON object
  11878. - `key`: the parser read a key of a value in an object
  11879. - `object_end`: the parser read `}` and finished processing a JSON object
  11880. - `array_start`: the parser read `[` and started to process a JSON array
  11881. - `array_end`: the parser read `]` and finished processing a JSON array
  11882. - `value`: the parser finished reading a JSON value
  11883. @image html callback_events.png "Example when certain parse events are triggered"
  11884. @sa @ref parser_callback_t for more information and examples
  11885. */
  11886. using parse_event_t = typename parser::parse_event_t;
  11887. /*!
  11888. @brief per-element parser callback type
  11889. With a parser callback function, the result of parsing a JSON text can be
  11890. influenced. When passed to @ref parse, it is called on certain events
  11891. (passed as @ref parse_event_t via parameter @a event) with a set recursion
  11892. depth @a depth and context JSON value @a parsed. The return value of the
  11893. callback function is a boolean indicating whether the element that emitted
  11894. the callback shall be kept or not.
  11895. We distinguish six scenarios (determined by the event type) in which the
  11896. callback function can be called. The following table describes the values
  11897. of the parameters @a depth, @a event, and @a parsed.
  11898. parameter @a event | description | parameter @a depth | parameter @a parsed
  11899. ------------------ | ----------- | ------------------ | -------------------
  11900. 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
  11901. 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
  11902. 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
  11903. 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
  11904. 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
  11905. parse_event_t::value | the parser finished reading a JSON value | depth of the value | the parsed JSON value
  11906. @image html callback_events.png "Example when certain parse events are triggered"
  11907. Discarding a value (i.e., returning `false`) has different effects
  11908. depending on the context in which function was called:
  11909. - Discarded values in structured types are skipped. That is, the parser
  11910. will behave as if the discarded value was never read.
  11911. - In case a value outside a structured type is skipped, it is replaced
  11912. with `null`. This case happens if the top-level element is skipped.
  11913. @param[in] depth the depth of the recursion during parsing
  11914. @param[in] event an event of type parse_event_t indicating the context in
  11915. the callback function has been called
  11916. @param[in,out] parsed the current intermediate parse result; note that
  11917. writing to this value has no effect for parse_event_t::key events
  11918. @return Whether the JSON value which called the function during parsing
  11919. should be kept (`true`) or not (`false`). In the latter case, it is either
  11920. skipped completely or replaced by an empty discarded object.
  11921. @sa @ref parse for examples
  11922. @since version 1.0.0
  11923. */
  11924. using parser_callback_t = typename parser::parser_callback_t;
  11925. //////////////////
  11926. // constructors //
  11927. //////////////////
  11928. /// @name constructors and destructors
  11929. /// Constructors of class @ref basic_json, copy/move constructor, copy
  11930. /// assignment, static functions creating objects, and the destructor.
  11931. /// @{
  11932. /*!
  11933. @brief create an empty value with a given type
  11934. Create an empty JSON value with a given type. The value will be default
  11935. initialized with an empty value which depends on the type:
  11936. Value type | initial value
  11937. ----------- | -------------
  11938. null | `null`
  11939. boolean | `false`
  11940. string | `""`
  11941. number | `0`
  11942. object | `{}`
  11943. array | `[]`
  11944. @param[in] v the type of the value to create
  11945. @complexity Constant.
  11946. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  11947. changes to any JSON value.
  11948. @liveexample{The following code shows the constructor for different @ref
  11949. value_t values,basic_json__value_t}
  11950. @sa @ref clear() -- restores the postcondition of this constructor
  11951. @since version 1.0.0
  11952. */
  11953. basic_json(const value_t v)
  11954. : m_type(v), m_value(v)
  11955. {
  11956. assert_invariant();
  11957. }
  11958. /*!
  11959. @brief create a null object
  11960. Create a `null` JSON value. It either takes a null pointer as parameter
  11961. (explicitly creating `null`) or no parameter (implicitly creating `null`).
  11962. The passed null pointer itself is not read -- it is only used to choose
  11963. the right constructor.
  11964. @complexity Constant.
  11965. @exceptionsafety No-throw guarantee: this constructor never throws
  11966. exceptions.
  11967. @liveexample{The following code shows the constructor with and without a
  11968. null pointer parameter.,basic_json__nullptr_t}
  11969. @since version 1.0.0
  11970. */
  11971. basic_json(std::nullptr_t = nullptr) noexcept
  11972. : basic_json(value_t::null)
  11973. {
  11974. assert_invariant();
  11975. }
  11976. /*!
  11977. @brief create a JSON value
  11978. This is a "catch all" constructor for all compatible JSON types; that is,
  11979. types for which a `to_json()` method exists. The constructor forwards the
  11980. parameter @a val to that method (to `json_serializer<U>::to_json` method
  11981. with `U = uncvref_t<CompatibleType>`, to be exact).
  11982. Template type @a CompatibleType includes, but is not limited to, the
  11983. following types:
  11984. - **arrays**: @ref array_t and all kinds of compatible containers such as
  11985. `std::vector`, `std::deque`, `std::list`, `std::forward_list`,
  11986. `std::array`, `std::valarray`, `std::set`, `std::unordered_set`,
  11987. `std::multiset`, and `std::unordered_multiset` with a `value_type` from
  11988. which a @ref basic_json value can be constructed.
  11989. - **objects**: @ref object_t and all kinds of compatible associative
  11990. containers such as `std::map`, `std::unordered_map`, `std::multimap`,
  11991. and `std::unordered_multimap` with a `key_type` compatible to
  11992. @ref string_t and a `value_type` from which a @ref basic_json value can
  11993. be constructed.
  11994. - **strings**: @ref string_t, string literals, and all compatible string
  11995. containers can be used.
  11996. - **numbers**: @ref number_integer_t, @ref number_unsigned_t,
  11997. @ref number_float_t, and all convertible number types such as `int`,
  11998. `size_t`, `int64_t`, `float` or `double` can be used.
  11999. - **boolean**: @ref boolean_t / `bool` can be used.
  12000. See the examples below.
  12001. @tparam CompatibleType a type such that:
  12002. - @a CompatibleType is not derived from `std::istream`,
  12003. - @a CompatibleType is not @ref basic_json (to avoid hijacking copy/move
  12004. constructors),
  12005. - @a CompatibleType is not a different @ref basic_json type (i.e. with different template arguments)
  12006. - @a CompatibleType is not a @ref basic_json nested type (e.g.,
  12007. @ref json_pointer, @ref iterator, etc ...)
  12008. - @ref @ref json_serializer<U> has a
  12009. `to_json(basic_json_t&, CompatibleType&&)` method
  12010. @tparam U = `uncvref_t<CompatibleType>`
  12011. @param[in] val the value to be forwarded to the respective constructor
  12012. @complexity Usually linear in the size of the passed @a val, also
  12013. depending on the implementation of the called `to_json()`
  12014. method.
  12015. @exceptionsafety Depends on the called constructor. For types directly
  12016. supported by the library (i.e., all types for which no `to_json()` function
  12017. was provided), strong guarantee holds: if an exception is thrown, there are
  12018. no changes to any JSON value.
  12019. @liveexample{The following code shows the constructor with several
  12020. compatible types.,basic_json__CompatibleType}
  12021. @since version 2.1.0
  12022. */
  12023. template <typename CompatibleType,
  12024. typename U = detail::uncvref_t<CompatibleType>,
  12025. detail::enable_if_t<
  12026. not detail::is_basic_json<U>::value and detail::is_compatible_type<basic_json_t, U>::value, int> = 0>
  12027. basic_json(CompatibleType && val) noexcept(noexcept(
  12028. JSONSerializer<U>::to_json(std::declval<basic_json_t&>(),
  12029. std::forward<CompatibleType>(val))))
  12030. {
  12031. JSONSerializer<U>::to_json(*this, std::forward<CompatibleType>(val));
  12032. assert_invariant();
  12033. }
  12034. /*!
  12035. @brief create a JSON value from an existing one
  12036. This is a constructor for existing @ref basic_json types.
  12037. It does not hijack copy/move constructors, since the parameter has different
  12038. template arguments than the current ones.
  12039. The constructor tries to convert the internal @ref m_value of the parameter.
  12040. @tparam BasicJsonType a type such that:
  12041. - @a BasicJsonType is a @ref basic_json type.
  12042. - @a BasicJsonType has different template arguments than @ref basic_json_t.
  12043. @param[in] val the @ref basic_json value to be converted.
  12044. @complexity Usually linear in the size of the passed @a val, also
  12045. depending on the implementation of the called `to_json()`
  12046. method.
  12047. @exceptionsafety Depends on the called constructor. For types directly
  12048. supported by the library (i.e., all types for which no `to_json()` function
  12049. was provided), strong guarantee holds: if an exception is thrown, there are
  12050. no changes to any JSON value.
  12051. @since version 3.2.0
  12052. */
  12053. template <typename BasicJsonType,
  12054. detail::enable_if_t<
  12055. detail::is_basic_json<BasicJsonType>::value and not std::is_same<basic_json, BasicJsonType>::value, int> = 0>
  12056. basic_json(const BasicJsonType& val)
  12057. {
  12058. using other_boolean_t = typename BasicJsonType::boolean_t;
  12059. using other_number_float_t = typename BasicJsonType::number_float_t;
  12060. using other_number_integer_t = typename BasicJsonType::number_integer_t;
  12061. using other_number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  12062. using other_string_t = typename BasicJsonType::string_t;
  12063. using other_object_t = typename BasicJsonType::object_t;
  12064. using other_array_t = typename BasicJsonType::array_t;
  12065. switch (val.type())
  12066. {
  12067. case value_t::boolean:
  12068. JSONSerializer<other_boolean_t>::to_json(*this, val.template get<other_boolean_t>());
  12069. break;
  12070. case value_t::number_float:
  12071. JSONSerializer<other_number_float_t>::to_json(*this, val.template get<other_number_float_t>());
  12072. break;
  12073. case value_t::number_integer:
  12074. JSONSerializer<other_number_integer_t>::to_json(*this, val.template get<other_number_integer_t>());
  12075. break;
  12076. case value_t::number_unsigned:
  12077. JSONSerializer<other_number_unsigned_t>::to_json(*this, val.template get<other_number_unsigned_t>());
  12078. break;
  12079. case value_t::string:
  12080. JSONSerializer<other_string_t>::to_json(*this, val.template get_ref<const other_string_t&>());
  12081. break;
  12082. case value_t::object:
  12083. JSONSerializer<other_object_t>::to_json(*this, val.template get_ref<const other_object_t&>());
  12084. break;
  12085. case value_t::array:
  12086. JSONSerializer<other_array_t>::to_json(*this, val.template get_ref<const other_array_t&>());
  12087. break;
  12088. case value_t::null:
  12089. *this = nullptr;
  12090. break;
  12091. case value_t::discarded:
  12092. m_type = value_t::discarded;
  12093. break;
  12094. }
  12095. assert_invariant();
  12096. }
  12097. /*!
  12098. @brief create a container (array or object) from an initializer list
  12099. Creates a JSON value of type array or object from the passed initializer
  12100. list @a init. In case @a type_deduction is `true` (default), the type of
  12101. the JSON value to be created is deducted from the initializer list @a init
  12102. according to the following rules:
  12103. 1. If the list is empty, an empty JSON object value `{}` is created.
  12104. 2. If the list consists of pairs whose first element is a string, a JSON
  12105. object value is created where the first elements of the pairs are
  12106. treated as keys and the second elements are as values.
  12107. 3. In all other cases, an array is created.
  12108. The rules aim to create the best fit between a C++ initializer list and
  12109. JSON values. The rationale is as follows:
  12110. 1. The empty initializer list is written as `{}` which is exactly an empty
  12111. JSON object.
  12112. 2. C++ has no way of describing mapped types other than to list a list of
  12113. pairs. As JSON requires that keys must be of type string, rule 2 is the
  12114. weakest constraint one can pose on initializer lists to interpret them
  12115. as an object.
  12116. 3. In all other cases, the initializer list could not be interpreted as
  12117. JSON object type, so interpreting it as JSON array type is safe.
  12118. With the rules described above, the following JSON values cannot be
  12119. expressed by an initializer list:
  12120. - the empty array (`[]`): use @ref array(initializer_list_t)
  12121. with an empty initializer list in this case
  12122. - arrays whose elements satisfy rule 2: use @ref
  12123. array(initializer_list_t) with the same initializer list
  12124. in this case
  12125. @note When used without parentheses around an empty initializer list, @ref
  12126. basic_json() is called instead of this function, yielding the JSON null
  12127. value.
  12128. @param[in] init initializer list with JSON values
  12129. @param[in] type_deduction internal parameter; when set to `true`, the type
  12130. of the JSON value is deducted from the initializer list @a init; when set
  12131. to `false`, the type provided via @a manual_type is forced. This mode is
  12132. used by the functions @ref array(initializer_list_t) and
  12133. @ref object(initializer_list_t).
  12134. @param[in] manual_type internal parameter; when @a type_deduction is set
  12135. to `false`, the created JSON value will use the provided type (only @ref
  12136. value_t::array and @ref value_t::object are valid); when @a type_deduction
  12137. is set to `true`, this parameter has no effect
  12138. @throw type_error.301 if @a type_deduction is `false`, @a manual_type is
  12139. `value_t::object`, but @a init contains an element which is not a pair
  12140. whose first element is a string. In this case, the constructor could not
  12141. create an object. If @a type_deduction would have be `true`, an array
  12142. would have been created. See @ref object(initializer_list_t)
  12143. for an example.
  12144. @complexity Linear in the size of the initializer list @a init.
  12145. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  12146. changes to any JSON value.
  12147. @liveexample{The example below shows how JSON values are created from
  12148. initializer lists.,basic_json__list_init_t}
  12149. @sa @ref array(initializer_list_t) -- create a JSON array
  12150. value from an initializer list
  12151. @sa @ref object(initializer_list_t) -- create a JSON object
  12152. value from an initializer list
  12153. @since version 1.0.0
  12154. */
  12155. basic_json(initializer_list_t init,
  12156. bool type_deduction = true,
  12157. value_t manual_type = value_t::array)
  12158. {
  12159. // check if each element is an array with two elements whose first
  12160. // element is a string
  12161. bool is_an_object = std::all_of(init.begin(), init.end(),
  12162. [](const detail::json_ref<basic_json>& element_ref)
  12163. {
  12164. return (element_ref->is_array() and element_ref->size() == 2 and (*element_ref)[0].is_string());
  12165. });
  12166. // adjust type if type deduction is not wanted
  12167. if (not type_deduction)
  12168. {
  12169. // if array is wanted, do not create an object though possible
  12170. if (manual_type == value_t::array)
  12171. {
  12172. is_an_object = false;
  12173. }
  12174. // if object is wanted but impossible, throw an exception
  12175. if (JSON_UNLIKELY(manual_type == value_t::object and not is_an_object))
  12176. {
  12177. JSON_THROW(type_error::create(301, "cannot create object from initializer list"));
  12178. }
  12179. }
  12180. if (is_an_object)
  12181. {
  12182. // the initializer list is a list of pairs -> create object
  12183. m_type = value_t::object;
  12184. m_value = value_t::object;
  12185. std::for_each(init.begin(), init.end(), [this](const detail::json_ref<basic_json>& element_ref)
  12186. {
  12187. auto element = element_ref.moved_or_copied();
  12188. m_value.object->emplace(
  12189. std::move(*((*element.m_value.array)[0].m_value.string)),
  12190. std::move((*element.m_value.array)[1]));
  12191. });
  12192. }
  12193. else
  12194. {
  12195. // the initializer list describes an array -> create array
  12196. m_type = value_t::array;
  12197. m_value.array = create<array_t>(init.begin(), init.end());
  12198. }
  12199. assert_invariant();
  12200. }
  12201. /*!
  12202. @brief explicitly create an array from an initializer list
  12203. Creates a JSON array value from a given initializer list. That is, given a
  12204. list of values `a, b, c`, creates the JSON value `[a, b, c]`. If the
  12205. initializer list is empty, the empty array `[]` is created.
  12206. @note This function is only needed to express two edge cases that cannot
  12207. be realized with the initializer list constructor (@ref
  12208. basic_json(initializer_list_t, bool, value_t)). These cases
  12209. are:
  12210. 1. creating an array whose elements are all pairs whose first element is a
  12211. string -- in this case, the initializer list constructor would create an
  12212. object, taking the first elements as keys
  12213. 2. creating an empty array -- passing the empty initializer list to the
  12214. initializer list constructor yields an empty object
  12215. @param[in] init initializer list with JSON values to create an array from
  12216. (optional)
  12217. @return JSON array value
  12218. @complexity Linear in the size of @a init.
  12219. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  12220. changes to any JSON value.
  12221. @liveexample{The following code shows an example for the `array`
  12222. function.,array}
  12223. @sa @ref basic_json(initializer_list_t, bool, value_t) --
  12224. create a JSON value from an initializer list
  12225. @sa @ref object(initializer_list_t) -- create a JSON object
  12226. value from an initializer list
  12227. @since version 1.0.0
  12228. */
  12229. static basic_json array(initializer_list_t init = {})
  12230. {
  12231. return basic_json(init, false, value_t::array);
  12232. }
  12233. /*!
  12234. @brief explicitly create an object from an initializer list
  12235. Creates a JSON object value from a given initializer list. The initializer
  12236. lists elements must be pairs, and their first elements must be strings. If
  12237. the initializer list is empty, the empty object `{}` is created.
  12238. @note This function is only added for symmetry reasons. In contrast to the
  12239. related function @ref array(initializer_list_t), there are
  12240. no cases which can only be expressed by this function. That is, any
  12241. initializer list @a init can also be passed to the initializer list
  12242. constructor @ref basic_json(initializer_list_t, bool, value_t).
  12243. @param[in] init initializer list to create an object from (optional)
  12244. @return JSON object value
  12245. @throw type_error.301 if @a init is not a list of pairs whose first
  12246. elements are strings. In this case, no object can be created. When such a
  12247. value is passed to @ref basic_json(initializer_list_t, bool, value_t),
  12248. an array would have been created from the passed initializer list @a init.
  12249. See example below.
  12250. @complexity Linear in the size of @a init.
  12251. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  12252. changes to any JSON value.
  12253. @liveexample{The following code shows an example for the `object`
  12254. function.,object}
  12255. @sa @ref basic_json(initializer_list_t, bool, value_t) --
  12256. create a JSON value from an initializer list
  12257. @sa @ref array(initializer_list_t) -- create a JSON array
  12258. value from an initializer list
  12259. @since version 1.0.0
  12260. */
  12261. static basic_json object(initializer_list_t init = {})
  12262. {
  12263. return basic_json(init, false, value_t::object);
  12264. }
  12265. /*!
  12266. @brief construct an array with count copies of given value
  12267. Constructs a JSON array value by creating @a cnt copies of a passed value.
  12268. In case @a cnt is `0`, an empty array is created.
  12269. @param[in] cnt the number of JSON copies of @a val to create
  12270. @param[in] val the JSON value to copy
  12271. @post `std::distance(begin(),end()) == cnt` holds.
  12272. @complexity Linear in @a cnt.
  12273. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  12274. changes to any JSON value.
  12275. @liveexample{The following code shows examples for the @ref
  12276. basic_json(size_type\, const basic_json&)
  12277. constructor.,basic_json__size_type_basic_json}
  12278. @since version 1.0.0
  12279. */
  12280. basic_json(size_type cnt, const basic_json& val)
  12281. : m_type(value_t::array)
  12282. {
  12283. m_value.array = create<array_t>(cnt, val);
  12284. assert_invariant();
  12285. }
  12286. /*!
  12287. @brief construct a JSON container given an iterator range
  12288. Constructs the JSON value with the contents of the range `[first, last)`.
  12289. The semantics depends on the different types a JSON value can have:
  12290. - In case of a null type, invalid_iterator.206 is thrown.
  12291. - In case of other primitive types (number, boolean, or string), @a first
  12292. must be `begin()` and @a last must be `end()`. In this case, the value is
  12293. copied. Otherwise, invalid_iterator.204 is thrown.
  12294. - In case of structured types (array, object), the constructor behaves as
  12295. similar versions for `std::vector` or `std::map`; that is, a JSON array
  12296. or object is constructed from the values in the range.
  12297. @tparam InputIT an input iterator type (@ref iterator or @ref
  12298. const_iterator)
  12299. @param[in] first begin of the range to copy from (included)
  12300. @param[in] last end of the range to copy from (excluded)
  12301. @pre Iterators @a first and @a last must be initialized. **This
  12302. precondition is enforced with an assertion (see warning).** If
  12303. assertions are switched off, a violation of this precondition yields
  12304. undefined behavior.
  12305. @pre Range `[first, last)` is valid. Usually, this precondition cannot be
  12306. checked efficiently. Only certain edge cases are detected; see the
  12307. description of the exceptions below. A violation of this precondition
  12308. yields undefined behavior.
  12309. @warning A precondition is enforced with a runtime assertion that will
  12310. result in calling `std::abort` if this precondition is not met.
  12311. Assertions can be disabled by defining `NDEBUG` at compile time.
  12312. See https://en.cppreference.com/w/cpp/error/assert for more
  12313. information.
  12314. @throw invalid_iterator.201 if iterators @a first and @a last are not
  12315. compatible (i.e., do not belong to the same JSON value). In this case,
  12316. the range `[first, last)` is undefined.
  12317. @throw invalid_iterator.204 if iterators @a first and @a last belong to a
  12318. primitive type (number, boolean, or string), but @a first does not point
  12319. to the first element any more. In this case, the range `[first, last)` is
  12320. undefined. See example code below.
  12321. @throw invalid_iterator.206 if iterators @a first and @a last belong to a
  12322. null value. In this case, the range `[first, last)` is undefined.
  12323. @complexity Linear in distance between @a first and @a last.
  12324. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  12325. changes to any JSON value.
  12326. @liveexample{The example below shows several ways to create JSON values by
  12327. specifying a subrange with iterators.,basic_json__InputIt_InputIt}
  12328. @since version 1.0.0
  12329. */
  12330. template<class InputIT, typename std::enable_if<
  12331. std::is_same<InputIT, typename basic_json_t::iterator>::value or
  12332. std::is_same<InputIT, typename basic_json_t::const_iterator>::value, int>::type = 0>
  12333. basic_json(InputIT first, InputIT last)
  12334. {
  12335. assert(first.m_object != nullptr);
  12336. assert(last.m_object != nullptr);
  12337. // make sure iterator fits the current value
  12338. if (JSON_UNLIKELY(first.m_object != last.m_object))
  12339. {
  12340. JSON_THROW(invalid_iterator::create(201, "iterators are not compatible"));
  12341. }
  12342. // copy type from first iterator
  12343. m_type = first.m_object->m_type;
  12344. // check if iterator range is complete for primitive values
  12345. switch (m_type)
  12346. {
  12347. case value_t::boolean:
  12348. case value_t::number_float:
  12349. case value_t::number_integer:
  12350. case value_t::number_unsigned:
  12351. case value_t::string:
  12352. {
  12353. if (JSON_UNLIKELY(not first.m_it.primitive_iterator.is_begin()
  12354. or not last.m_it.primitive_iterator.is_end()))
  12355. {
  12356. JSON_THROW(invalid_iterator::create(204, "iterators out of range"));
  12357. }
  12358. break;
  12359. }
  12360. default:
  12361. break;
  12362. }
  12363. switch (m_type)
  12364. {
  12365. case value_t::number_integer:
  12366. {
  12367. m_value.number_integer = first.m_object->m_value.number_integer;
  12368. break;
  12369. }
  12370. case value_t::number_unsigned:
  12371. {
  12372. m_value.number_unsigned = first.m_object->m_value.number_unsigned;
  12373. break;
  12374. }
  12375. case value_t::number_float:
  12376. {
  12377. m_value.number_float = first.m_object->m_value.number_float;
  12378. break;
  12379. }
  12380. case value_t::boolean:
  12381. {
  12382. m_value.boolean = first.m_object->m_value.boolean;
  12383. break;
  12384. }
  12385. case value_t::string:
  12386. {
  12387. m_value = *first.m_object->m_value.string;
  12388. break;
  12389. }
  12390. case value_t::object:
  12391. {
  12392. m_value.object = create<object_t>(first.m_it.object_iterator,
  12393. last.m_it.object_iterator);
  12394. break;
  12395. }
  12396. case value_t::array:
  12397. {
  12398. m_value.array = create<array_t>(first.m_it.array_iterator,
  12399. last.m_it.array_iterator);
  12400. break;
  12401. }
  12402. default:
  12403. JSON_THROW(invalid_iterator::create(206, "cannot construct with iterators from " +
  12404. std::string(first.m_object->type_name())));
  12405. }
  12406. assert_invariant();
  12407. }
  12408. ///////////////////////////////////////
  12409. // other constructors and destructor //
  12410. ///////////////////////////////////////
  12411. /// @private
  12412. basic_json(const detail::json_ref<basic_json>& ref)
  12413. : basic_json(ref.moved_or_copied())
  12414. {}
  12415. /*!
  12416. @brief copy constructor
  12417. Creates a copy of a given JSON value.
  12418. @param[in] other the JSON value to copy
  12419. @post `*this == other`
  12420. @complexity Linear in the size of @a other.
  12421. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  12422. changes to any JSON value.
  12423. @requirement This function helps `basic_json` satisfying the
  12424. [Container](https://en.cppreference.com/w/cpp/named_req/Container)
  12425. requirements:
  12426. - The complexity is linear.
  12427. - As postcondition, it holds: `other == basic_json(other)`.
  12428. @liveexample{The following code shows an example for the copy
  12429. constructor.,basic_json__basic_json}
  12430. @since version 1.0.0
  12431. */
  12432. basic_json(const basic_json& other)
  12433. : m_type(other.m_type)
  12434. {
  12435. // check of passed value is valid
  12436. other.assert_invariant();
  12437. switch (m_type)
  12438. {
  12439. case value_t::object:
  12440. {
  12441. m_value = *other.m_value.object;
  12442. break;
  12443. }
  12444. case value_t::array:
  12445. {
  12446. m_value = *other.m_value.array;
  12447. break;
  12448. }
  12449. case value_t::string:
  12450. {
  12451. m_value = *other.m_value.string;
  12452. break;
  12453. }
  12454. case value_t::boolean:
  12455. {
  12456. m_value = other.m_value.boolean;
  12457. break;
  12458. }
  12459. case value_t::number_integer:
  12460. {
  12461. m_value = other.m_value.number_integer;
  12462. break;
  12463. }
  12464. case value_t::number_unsigned:
  12465. {
  12466. m_value = other.m_value.number_unsigned;
  12467. break;
  12468. }
  12469. case value_t::number_float:
  12470. {
  12471. m_value = other.m_value.number_float;
  12472. break;
  12473. }
  12474. default:
  12475. break;
  12476. }
  12477. assert_invariant();
  12478. }
  12479. /*!
  12480. @brief move constructor
  12481. Move constructor. Constructs a JSON value with the contents of the given
  12482. value @a other using move semantics. It "steals" the resources from @a
  12483. other and leaves it as JSON null value.
  12484. @param[in,out] other value to move to this object
  12485. @post `*this` has the same value as @a other before the call.
  12486. @post @a other is a JSON null value.
  12487. @complexity Constant.
  12488. @exceptionsafety No-throw guarantee: this constructor never throws
  12489. exceptions.
  12490. @requirement This function helps `basic_json` satisfying the
  12491. [MoveConstructible](https://en.cppreference.com/w/cpp/named_req/MoveConstructible)
  12492. requirements.
  12493. @liveexample{The code below shows the move constructor explicitly called
  12494. via std::move.,basic_json__moveconstructor}
  12495. @since version 1.0.0
  12496. */
  12497. basic_json(basic_json&& other) noexcept
  12498. : m_type(std::move(other.m_type)),
  12499. m_value(std::move(other.m_value))
  12500. {
  12501. // check that passed value is valid
  12502. other.assert_invariant();
  12503. // invalidate payload
  12504. other.m_type = value_t::null;
  12505. other.m_value = {};
  12506. assert_invariant();
  12507. }
  12508. /*!
  12509. @brief copy assignment
  12510. Copy assignment operator. Copies a JSON value via the "copy and swap"
  12511. strategy: It is expressed in terms of the copy constructor, destructor,
  12512. and the `swap()` member function.
  12513. @param[in] other value to copy from
  12514. @complexity Linear.
  12515. @requirement This function helps `basic_json` satisfying the
  12516. [Container](https://en.cppreference.com/w/cpp/named_req/Container)
  12517. requirements:
  12518. - The complexity is linear.
  12519. @liveexample{The code below shows and example for the copy assignment. It
  12520. creates a copy of value `a` which is then swapped with `b`. Finally\, the
  12521. copy of `a` (which is the null value after the swap) is
  12522. destroyed.,basic_json__copyassignment}
  12523. @since version 1.0.0
  12524. */
  12525. basic_json& operator=(basic_json other) noexcept (
  12526. std::is_nothrow_move_constructible<value_t>::value and
  12527. std::is_nothrow_move_assignable<value_t>::value and
  12528. std::is_nothrow_move_constructible<json_value>::value and
  12529. std::is_nothrow_move_assignable<json_value>::value
  12530. )
  12531. {
  12532. // check that passed value is valid
  12533. other.assert_invariant();
  12534. using std::swap;
  12535. swap(m_type, other.m_type);
  12536. swap(m_value, other.m_value);
  12537. assert_invariant();
  12538. return *this;
  12539. }
  12540. /*!
  12541. @brief destructor
  12542. Destroys the JSON value and frees all allocated memory.
  12543. @complexity Linear.
  12544. @requirement This function helps `basic_json` satisfying the
  12545. [Container](https://en.cppreference.com/w/cpp/named_req/Container)
  12546. requirements:
  12547. - The complexity is linear.
  12548. - All stored elements are destroyed and all memory is freed.
  12549. @since version 1.0.0
  12550. */
  12551. ~basic_json() noexcept
  12552. {
  12553. assert_invariant();
  12554. m_value.destroy(m_type);
  12555. }
  12556. /// @}
  12557. public:
  12558. ///////////////////////
  12559. // object inspection //
  12560. ///////////////////////
  12561. /// @name object inspection
  12562. /// Functions to inspect the type of a JSON value.
  12563. /// @{
  12564. /*!
  12565. @brief serialization
  12566. Serialization function for JSON values. The function tries to mimic
  12567. Python's `json.dumps()` function, and currently supports its @a indent
  12568. and @a ensure_ascii parameters.
  12569. @param[in] indent If indent is nonnegative, then array elements and object
  12570. members will be pretty-printed with that indent level. An indent level of
  12571. `0` will only insert newlines. `-1` (the default) selects the most compact
  12572. representation.
  12573. @param[in] indent_char The character to use for indentation if @a indent is
  12574. greater than `0`. The default is ` ` (space).
  12575. @param[in] ensure_ascii If @a ensure_ascii is true, all non-ASCII characters
  12576. in the output are escaped with `\uXXXX` sequences, and the result consists
  12577. of ASCII characters only.
  12578. @param[in] error_handler how to react on decoding errors; there are three
  12579. possible values: `strict` (throws and exception in case a decoding error
  12580. occurs; default), `replace` (replace invalid UTF-8 sequences with U+FFFD),
  12581. and `ignore` (ignore invalid UTF-8 sequences during serialization).
  12582. @return string containing the serialization of the JSON value
  12583. @throw type_error.316 if a string stored inside the JSON value is not
  12584. UTF-8 encoded
  12585. @complexity Linear.
  12586. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  12587. changes in the JSON value.
  12588. @liveexample{The following example shows the effect of different @a indent\,
  12589. @a indent_char\, and @a ensure_ascii parameters to the result of the
  12590. serialization.,dump}
  12591. @see https://docs.python.org/2/library/json.html#json.dump
  12592. @since version 1.0.0; indentation character @a indent_char, option
  12593. @a ensure_ascii and exceptions added in version 3.0.0; error
  12594. handlers added in version 3.4.0.
  12595. */
  12596. string_t dump(const int indent = -1,
  12597. const char indent_char = ' ',
  12598. const bool ensure_ascii = false,
  12599. const error_handler_t error_handler = error_handler_t::strict) const
  12600. {
  12601. string_t result;
  12602. serializer s(detail::output_adapter<char, string_t>(result), indent_char, error_handler);
  12603. if (indent >= 0)
  12604. {
  12605. s.dump(*this, true, ensure_ascii, static_cast<unsigned int>(indent));
  12606. }
  12607. else
  12608. {
  12609. s.dump(*this, false, ensure_ascii, 0);
  12610. }
  12611. return result;
  12612. }
  12613. /*!
  12614. @brief return the type of the JSON value (explicit)
  12615. Return the type of the JSON value as a value from the @ref value_t
  12616. enumeration.
  12617. @return the type of the JSON value
  12618. Value type | return value
  12619. ------------------------- | -------------------------
  12620. null | value_t::null
  12621. boolean | value_t::boolean
  12622. string | value_t::string
  12623. number (integer) | value_t::number_integer
  12624. number (unsigned integer) | value_t::number_unsigned
  12625. number (floating-point) | value_t::number_float
  12626. object | value_t::object
  12627. array | value_t::array
  12628. discarded | value_t::discarded
  12629. @complexity Constant.
  12630. @exceptionsafety No-throw guarantee: this member function never throws
  12631. exceptions.
  12632. @liveexample{The following code exemplifies `type()` for all JSON
  12633. types.,type}
  12634. @sa @ref operator value_t() -- return the type of the JSON value (implicit)
  12635. @sa @ref type_name() -- return the type as string
  12636. @since version 1.0.0
  12637. */
  12638. constexpr value_t type() const noexcept
  12639. {
  12640. return m_type;
  12641. }
  12642. /*!
  12643. @brief return whether type is primitive
  12644. This function returns true if and only if the JSON type is primitive
  12645. (string, number, boolean, or null).
  12646. @return `true` if type is primitive (string, number, boolean, or null),
  12647. `false` otherwise.
  12648. @complexity Constant.
  12649. @exceptionsafety No-throw guarantee: this member function never throws
  12650. exceptions.
  12651. @liveexample{The following code exemplifies `is_primitive()` for all JSON
  12652. types.,is_primitive}
  12653. @sa @ref is_structured() -- returns whether JSON value is structured
  12654. @sa @ref is_null() -- returns whether JSON value is `null`
  12655. @sa @ref is_string() -- returns whether JSON value is a string
  12656. @sa @ref is_boolean() -- returns whether JSON value is a boolean
  12657. @sa @ref is_number() -- returns whether JSON value is a number
  12658. @since version 1.0.0
  12659. */
  12660. constexpr bool is_primitive() const noexcept
  12661. {
  12662. return is_null() or is_string() or is_boolean() or is_number();
  12663. }
  12664. /*!
  12665. @brief return whether type is structured
  12666. This function returns true if and only if the JSON type is structured
  12667. (array or object).
  12668. @return `true` if type is structured (array or object), `false` otherwise.
  12669. @complexity Constant.
  12670. @exceptionsafety No-throw guarantee: this member function never throws
  12671. exceptions.
  12672. @liveexample{The following code exemplifies `is_structured()` for all JSON
  12673. types.,is_structured}
  12674. @sa @ref is_primitive() -- returns whether value is primitive
  12675. @sa @ref is_array() -- returns whether value is an array
  12676. @sa @ref is_object() -- returns whether value is an object
  12677. @since version 1.0.0
  12678. */
  12679. constexpr bool is_structured() const noexcept
  12680. {
  12681. return is_array() or is_object();
  12682. }
  12683. /*!
  12684. @brief return whether value is null
  12685. This function returns true if and only if the JSON value is null.
  12686. @return `true` if type is null, `false` otherwise.
  12687. @complexity Constant.
  12688. @exceptionsafety No-throw guarantee: this member function never throws
  12689. exceptions.
  12690. @liveexample{The following code exemplifies `is_null()` for all JSON
  12691. types.,is_null}
  12692. @since version 1.0.0
  12693. */
  12694. constexpr bool is_null() const noexcept
  12695. {
  12696. return (m_type == value_t::null);
  12697. }
  12698. /*!
  12699. @brief return whether value is a boolean
  12700. This function returns true if and only if the JSON value is a boolean.
  12701. @return `true` if type is boolean, `false` otherwise.
  12702. @complexity Constant.
  12703. @exceptionsafety No-throw guarantee: this member function never throws
  12704. exceptions.
  12705. @liveexample{The following code exemplifies `is_boolean()` for all JSON
  12706. types.,is_boolean}
  12707. @since version 1.0.0
  12708. */
  12709. constexpr bool is_boolean() const noexcept
  12710. {
  12711. return (m_type == value_t::boolean);
  12712. }
  12713. /*!
  12714. @brief return whether value is a number
  12715. This function returns true if and only if the JSON value is a number. This
  12716. includes both integer (signed and unsigned) and floating-point values.
  12717. @return `true` if type is number (regardless whether integer, unsigned
  12718. integer or floating-type), `false` otherwise.
  12719. @complexity Constant.
  12720. @exceptionsafety No-throw guarantee: this member function never throws
  12721. exceptions.
  12722. @liveexample{The following code exemplifies `is_number()` for all JSON
  12723. types.,is_number}
  12724. @sa @ref is_number_integer() -- check if value is an integer or unsigned
  12725. integer number
  12726. @sa @ref is_number_unsigned() -- check if value is an unsigned integer
  12727. number
  12728. @sa @ref is_number_float() -- check if value is a floating-point number
  12729. @since version 1.0.0
  12730. */
  12731. constexpr bool is_number() const noexcept
  12732. {
  12733. return is_number_integer() or is_number_float();
  12734. }
  12735. /*!
  12736. @brief return whether value is an integer number
  12737. This function returns true if and only if the JSON value is a signed or
  12738. unsigned integer number. This excludes floating-point values.
  12739. @return `true` if type is an integer or unsigned integer number, `false`
  12740. otherwise.
  12741. @complexity Constant.
  12742. @exceptionsafety No-throw guarantee: this member function never throws
  12743. exceptions.
  12744. @liveexample{The following code exemplifies `is_number_integer()` for all
  12745. JSON types.,is_number_integer}
  12746. @sa @ref is_number() -- check if value is a number
  12747. @sa @ref is_number_unsigned() -- check if value is an unsigned integer
  12748. number
  12749. @sa @ref is_number_float() -- check if value is a floating-point number
  12750. @since version 1.0.0
  12751. */
  12752. constexpr bool is_number_integer() const noexcept
  12753. {
  12754. return (m_type == value_t::number_integer or m_type == value_t::number_unsigned);
  12755. }
  12756. /*!
  12757. @brief return whether value is an unsigned integer number
  12758. This function returns true if and only if the JSON value is an unsigned
  12759. integer number. This excludes floating-point and signed integer values.
  12760. @return `true` if type is an unsigned integer number, `false` otherwise.
  12761. @complexity Constant.
  12762. @exceptionsafety No-throw guarantee: this member function never throws
  12763. exceptions.
  12764. @liveexample{The following code exemplifies `is_number_unsigned()` for all
  12765. JSON types.,is_number_unsigned}
  12766. @sa @ref is_number() -- check if value is a number
  12767. @sa @ref is_number_integer() -- check if value is an integer or unsigned
  12768. integer number
  12769. @sa @ref is_number_float() -- check if value is a floating-point number
  12770. @since version 2.0.0
  12771. */
  12772. constexpr bool is_number_unsigned() const noexcept
  12773. {
  12774. return (m_type == value_t::number_unsigned);
  12775. }
  12776. /*!
  12777. @brief return whether value is a floating-point number
  12778. This function returns true if and only if the JSON value is a
  12779. floating-point number. This excludes signed and unsigned integer values.
  12780. @return `true` if type is a floating-point number, `false` otherwise.
  12781. @complexity Constant.
  12782. @exceptionsafety No-throw guarantee: this member function never throws
  12783. exceptions.
  12784. @liveexample{The following code exemplifies `is_number_float()` for all
  12785. JSON types.,is_number_float}
  12786. @sa @ref is_number() -- check if value is number
  12787. @sa @ref is_number_integer() -- check if value is an integer number
  12788. @sa @ref is_number_unsigned() -- check if value is an unsigned integer
  12789. number
  12790. @since version 1.0.0
  12791. */
  12792. constexpr bool is_number_float() const noexcept
  12793. {
  12794. return (m_type == value_t::number_float);
  12795. }
  12796. /*!
  12797. @brief return whether value is an object
  12798. This function returns true if and only if the JSON value is an object.
  12799. @return `true` if type is object, `false` otherwise.
  12800. @complexity Constant.
  12801. @exceptionsafety No-throw guarantee: this member function never throws
  12802. exceptions.
  12803. @liveexample{The following code exemplifies `is_object()` for all JSON
  12804. types.,is_object}
  12805. @since version 1.0.0
  12806. */
  12807. constexpr bool is_object() const noexcept
  12808. {
  12809. return (m_type == value_t::object);
  12810. }
  12811. /*!
  12812. @brief return whether value is an array
  12813. This function returns true if and only if the JSON value is an array.
  12814. @return `true` if type is array, `false` otherwise.
  12815. @complexity Constant.
  12816. @exceptionsafety No-throw guarantee: this member function never throws
  12817. exceptions.
  12818. @liveexample{The following code exemplifies `is_array()` for all JSON
  12819. types.,is_array}
  12820. @since version 1.0.0
  12821. */
  12822. constexpr bool is_array() const noexcept
  12823. {
  12824. return (m_type == value_t::array);
  12825. }
  12826. /*!
  12827. @brief return whether value is a string
  12828. This function returns true if and only if the JSON value is a string.
  12829. @return `true` if type is string, `false` otherwise.
  12830. @complexity Constant.
  12831. @exceptionsafety No-throw guarantee: this member function never throws
  12832. exceptions.
  12833. @liveexample{The following code exemplifies `is_string()` for all JSON
  12834. types.,is_string}
  12835. @since version 1.0.0
  12836. */
  12837. constexpr bool is_string() const noexcept
  12838. {
  12839. return (m_type == value_t::string);
  12840. }
  12841. /*!
  12842. @brief return whether value is discarded
  12843. This function returns true if and only if the JSON value was discarded
  12844. during parsing with a callback function (see @ref parser_callback_t).
  12845. @note This function will always be `false` for JSON values after parsing.
  12846. That is, discarded values can only occur during parsing, but will be
  12847. removed when inside a structured value or replaced by null in other cases.
  12848. @return `true` if type is discarded, `false` otherwise.
  12849. @complexity Constant.
  12850. @exceptionsafety No-throw guarantee: this member function never throws
  12851. exceptions.
  12852. @liveexample{The following code exemplifies `is_discarded()` for all JSON
  12853. types.,is_discarded}
  12854. @since version 1.0.0
  12855. */
  12856. constexpr bool is_discarded() const noexcept
  12857. {
  12858. return (m_type == value_t::discarded);
  12859. }
  12860. /*!
  12861. @brief return the type of the JSON value (implicit)
  12862. Implicitly return the type of the JSON value as a value from the @ref
  12863. value_t enumeration.
  12864. @return the type of the JSON value
  12865. @complexity Constant.
  12866. @exceptionsafety No-throw guarantee: this member function never throws
  12867. exceptions.
  12868. @liveexample{The following code exemplifies the @ref value_t operator for
  12869. all JSON types.,operator__value_t}
  12870. @sa @ref type() -- return the type of the JSON value (explicit)
  12871. @sa @ref type_name() -- return the type as string
  12872. @since version 1.0.0
  12873. */
  12874. constexpr operator value_t() const noexcept
  12875. {
  12876. return m_type;
  12877. }
  12878. /// @}
  12879. private:
  12880. //////////////////
  12881. // value access //
  12882. //////////////////
  12883. /// get a boolean (explicit)
  12884. boolean_t get_impl(boolean_t* /*unused*/) const
  12885. {
  12886. if (JSON_LIKELY(is_boolean()))
  12887. {
  12888. return m_value.boolean;
  12889. }
  12890. JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(type_name())));
  12891. }
  12892. /// get a pointer to the value (object)
  12893. object_t* get_impl_ptr(object_t* /*unused*/) noexcept
  12894. {
  12895. return is_object() ? m_value.object : nullptr;
  12896. }
  12897. /// get a pointer to the value (object)
  12898. constexpr const object_t* get_impl_ptr(const object_t* /*unused*/) const noexcept
  12899. {
  12900. return is_object() ? m_value.object : nullptr;
  12901. }
  12902. /// get a pointer to the value (array)
  12903. array_t* get_impl_ptr(array_t* /*unused*/) noexcept
  12904. {
  12905. return is_array() ? m_value.array : nullptr;
  12906. }
  12907. /// get a pointer to the value (array)
  12908. constexpr const array_t* get_impl_ptr(const array_t* /*unused*/) const noexcept
  12909. {
  12910. return is_array() ? m_value.array : nullptr;
  12911. }
  12912. /// get a pointer to the value (string)
  12913. string_t* get_impl_ptr(string_t* /*unused*/) noexcept
  12914. {
  12915. return is_string() ? m_value.string : nullptr;
  12916. }
  12917. /// get a pointer to the value (string)
  12918. constexpr const string_t* get_impl_ptr(const string_t* /*unused*/) const noexcept
  12919. {
  12920. return is_string() ? m_value.string : nullptr;
  12921. }
  12922. /// get a pointer to the value (boolean)
  12923. boolean_t* get_impl_ptr(boolean_t* /*unused*/) noexcept
  12924. {
  12925. return is_boolean() ? &m_value.boolean : nullptr;
  12926. }
  12927. /// get a pointer to the value (boolean)
  12928. constexpr const boolean_t* get_impl_ptr(const boolean_t* /*unused*/) const noexcept
  12929. {
  12930. return is_boolean() ? &m_value.boolean : nullptr;
  12931. }
  12932. /// get a pointer to the value (integer number)
  12933. number_integer_t* get_impl_ptr(number_integer_t* /*unused*/) noexcept
  12934. {
  12935. return is_number_integer() ? &m_value.number_integer : nullptr;
  12936. }
  12937. /// get a pointer to the value (integer number)
  12938. constexpr const number_integer_t* get_impl_ptr(const number_integer_t* /*unused*/) const noexcept
  12939. {
  12940. return is_number_integer() ? &m_value.number_integer : nullptr;
  12941. }
  12942. /// get a pointer to the value (unsigned number)
  12943. number_unsigned_t* get_impl_ptr(number_unsigned_t* /*unused*/) noexcept
  12944. {
  12945. return is_number_unsigned() ? &m_value.number_unsigned : nullptr;
  12946. }
  12947. /// get a pointer to the value (unsigned number)
  12948. constexpr const number_unsigned_t* get_impl_ptr(const number_unsigned_t* /*unused*/) const noexcept
  12949. {
  12950. return is_number_unsigned() ? &m_value.number_unsigned : nullptr;
  12951. }
  12952. /// get a pointer to the value (floating-point number)
  12953. number_float_t* get_impl_ptr(number_float_t* /*unused*/) noexcept
  12954. {
  12955. return is_number_float() ? &m_value.number_float : nullptr;
  12956. }
  12957. /// get a pointer to the value (floating-point number)
  12958. constexpr const number_float_t* get_impl_ptr(const number_float_t* /*unused*/) const noexcept
  12959. {
  12960. return is_number_float() ? &m_value.number_float : nullptr;
  12961. }
  12962. /*!
  12963. @brief helper function to implement get_ref()
  12964. This function helps to implement get_ref() without code duplication for
  12965. const and non-const overloads
  12966. @tparam ThisType will be deduced as `basic_json` or `const basic_json`
  12967. @throw type_error.303 if ReferenceType does not match underlying value
  12968. type of the current JSON
  12969. */
  12970. template<typename ReferenceType, typename ThisType>
  12971. static ReferenceType get_ref_impl(ThisType& obj)
  12972. {
  12973. // delegate the call to get_ptr<>()
  12974. auto ptr = obj.template get_ptr<typename std::add_pointer<ReferenceType>::type>();
  12975. if (JSON_LIKELY(ptr != nullptr))
  12976. {
  12977. return *ptr;
  12978. }
  12979. JSON_THROW(type_error::create(303, "incompatible ReferenceType for get_ref, actual type is " + std::string(obj.type_name())));
  12980. }
  12981. public:
  12982. /// @name value access
  12983. /// Direct access to the stored value of a JSON value.
  12984. /// @{
  12985. /*!
  12986. @brief get special-case overload
  12987. This overloads avoids a lot of template boilerplate, it can be seen as the
  12988. identity method
  12989. @tparam BasicJsonType == @ref basic_json
  12990. @return a copy of *this
  12991. @complexity Constant.
  12992. @since version 2.1.0
  12993. */
  12994. template<typename BasicJsonType, detail::enable_if_t<
  12995. std::is_same<typename std::remove_const<BasicJsonType>::type, basic_json_t>::value,
  12996. int> = 0>
  12997. basic_json get() const
  12998. {
  12999. return *this;
  13000. }
  13001. /*!
  13002. @brief get special-case overload
  13003. This overloads converts the current @ref basic_json in a different
  13004. @ref basic_json type
  13005. @tparam BasicJsonType == @ref basic_json
  13006. @return a copy of *this, converted into @tparam BasicJsonType
  13007. @complexity Depending on the implementation of the called `from_json()`
  13008. method.
  13009. @since version 3.2.0
  13010. */
  13011. template<typename BasicJsonType, detail::enable_if_t<
  13012. not std::is_same<BasicJsonType, basic_json>::value and
  13013. detail::is_basic_json<BasicJsonType>::value, int> = 0>
  13014. BasicJsonType get() const
  13015. {
  13016. return *this;
  13017. }
  13018. /*!
  13019. @brief get a value (explicit)
  13020. Explicit type conversion between the JSON value and a compatible value
  13021. which is [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)
  13022. and [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible).
  13023. The value is converted by calling the @ref json_serializer<ValueType>
  13024. `from_json()` method.
  13025. The function is equivalent to executing
  13026. @code {.cpp}
  13027. ValueType ret;
  13028. JSONSerializer<ValueType>::from_json(*this, ret);
  13029. return ret;
  13030. @endcode
  13031. This overloads is chosen if:
  13032. - @a ValueType is not @ref basic_json,
  13033. - @ref json_serializer<ValueType> has a `from_json()` method of the form
  13034. `void from_json(const basic_json&, ValueType&)`, and
  13035. - @ref json_serializer<ValueType> does not have a `from_json()` method of
  13036. the form `ValueType from_json(const basic_json&)`
  13037. @tparam ValueTypeCV the provided value type
  13038. @tparam ValueType the returned value type
  13039. @return copy of the JSON value, converted to @a ValueType
  13040. @throw what @ref json_serializer<ValueType> `from_json()` method throws
  13041. @liveexample{The example below shows several conversions from JSON values
  13042. to other types. There a few things to note: (1) Floating-point numbers can
  13043. be converted to integers\, (2) A JSON array can be converted to a standard
  13044. `std::vector<short>`\, (3) A JSON object can be converted to C++
  13045. associative containers such as `std::unordered_map<std::string\,
  13046. json>`.,get__ValueType_const}
  13047. @since version 2.1.0
  13048. */
  13049. template<typename ValueTypeCV, typename ValueType = detail::uncvref_t<ValueTypeCV>,
  13050. detail::enable_if_t <
  13051. not detail::is_basic_json<ValueType>::value and
  13052. detail::has_from_json<basic_json_t, ValueType>::value and
  13053. not detail::has_non_default_from_json<basic_json_t, ValueType>::value,
  13054. int> = 0>
  13055. ValueType get() const noexcept(noexcept(
  13056. JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), std::declval<ValueType&>())))
  13057. {
  13058. // we cannot static_assert on ValueTypeCV being non-const, because
  13059. // there is support for get<const basic_json_t>(), which is why we
  13060. // still need the uncvref
  13061. static_assert(not std::is_reference<ValueTypeCV>::value,
  13062. "get() cannot be used with reference types, you might want to use get_ref()");
  13063. static_assert(std::is_default_constructible<ValueType>::value,
  13064. "types must be DefaultConstructible when used with get()");
  13065. ValueType ret;
  13066. JSONSerializer<ValueType>::from_json(*this, ret);
  13067. return ret;
  13068. }
  13069. /*!
  13070. @brief get a value (explicit); special case
  13071. Explicit type conversion between the JSON value and a compatible value
  13072. which is **not** [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)
  13073. and **not** [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible).
  13074. The value is converted by calling the @ref json_serializer<ValueType>
  13075. `from_json()` method.
  13076. The function is equivalent to executing
  13077. @code {.cpp}
  13078. return JSONSerializer<ValueTypeCV>::from_json(*this);
  13079. @endcode
  13080. This overloads is chosen if:
  13081. - @a ValueType is not @ref basic_json and
  13082. - @ref json_serializer<ValueType> has a `from_json()` method of the form
  13083. `ValueType from_json(const basic_json&)`
  13084. @note If @ref json_serializer<ValueType> has both overloads of
  13085. `from_json()`, this one is chosen.
  13086. @tparam ValueTypeCV the provided value type
  13087. @tparam ValueType the returned value type
  13088. @return copy of the JSON value, converted to @a ValueType
  13089. @throw what @ref json_serializer<ValueType> `from_json()` method throws
  13090. @since version 2.1.0
  13091. */
  13092. template<typename ValueTypeCV, typename ValueType = detail::uncvref_t<ValueTypeCV>,
  13093. detail::enable_if_t<not std::is_same<basic_json_t, ValueType>::value and
  13094. detail::has_non_default_from_json<basic_json_t, ValueType>::value,
  13095. int> = 0>
  13096. ValueType get() const noexcept(noexcept(
  13097. JSONSerializer<ValueTypeCV>::from_json(std::declval<const basic_json_t&>())))
  13098. {
  13099. static_assert(not std::is_reference<ValueTypeCV>::value,
  13100. "get() cannot be used with reference types, you might want to use get_ref()");
  13101. return JSONSerializer<ValueTypeCV>::from_json(*this);
  13102. }
  13103. /*!
  13104. @brief get a value (explicit)
  13105. Explicit type conversion between the JSON value and a compatible value.
  13106. The value is filled into the input parameter by calling the @ref json_serializer<ValueType>
  13107. `from_json()` method.
  13108. The function is equivalent to executing
  13109. @code {.cpp}
  13110. ValueType v;
  13111. JSONSerializer<ValueType>::from_json(*this, v);
  13112. @endcode
  13113. This overloads is chosen if:
  13114. - @a ValueType is not @ref basic_json,
  13115. - @ref json_serializer<ValueType> has a `from_json()` method of the form
  13116. `void from_json(const basic_json&, ValueType&)`, and
  13117. @tparam ValueType the input parameter type.
  13118. @return the input parameter, allowing chaining calls.
  13119. @throw what @ref json_serializer<ValueType> `from_json()` method throws
  13120. @liveexample{The example below shows several conversions from JSON values
  13121. to other types. There a few things to note: (1) Floating-point numbers can
  13122. be converted to integers\, (2) A JSON array can be converted to a standard
  13123. `std::vector<short>`\, (3) A JSON object can be converted to C++
  13124. associative containers such as `std::unordered_map<std::string\,
  13125. json>`.,get_to}
  13126. @since version 3.3.0
  13127. */
  13128. template<typename ValueType,
  13129. detail::enable_if_t <
  13130. not detail::is_basic_json<ValueType>::value and
  13131. detail::has_from_json<basic_json_t, ValueType>::value,
  13132. int> = 0>
  13133. ValueType & get_to(ValueType& v) const noexcept(noexcept(
  13134. JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), v)))
  13135. {
  13136. JSONSerializer<ValueType>::from_json(*this, v);
  13137. return v;
  13138. }
  13139. /*!
  13140. @brief get a pointer value (implicit)
  13141. Implicit pointer access to the internally stored JSON value. No copies are
  13142. made.
  13143. @warning Writing data to the pointee of the result yields an undefined
  13144. state.
  13145. @tparam PointerType pointer type; must be a pointer to @ref array_t, @ref
  13146. object_t, @ref string_t, @ref boolean_t, @ref number_integer_t,
  13147. @ref number_unsigned_t, or @ref number_float_t. Enforced by a static
  13148. assertion.
  13149. @return pointer to the internally stored JSON value if the requested
  13150. pointer type @a PointerType fits to the JSON value; `nullptr` otherwise
  13151. @complexity Constant.
  13152. @liveexample{The example below shows how pointers to internal values of a
  13153. JSON value can be requested. Note that no type conversions are made and a
  13154. `nullptr` is returned if the value and the requested pointer type does not
  13155. match.,get_ptr}
  13156. @since version 1.0.0
  13157. */
  13158. template<typename PointerType, typename std::enable_if<
  13159. std::is_pointer<PointerType>::value, int>::type = 0>
  13160. auto get_ptr() noexcept -> decltype(std::declval<basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
  13161. {
  13162. // delegate the call to get_impl_ptr<>()
  13163. return get_impl_ptr(static_cast<PointerType>(nullptr));
  13164. }
  13165. /*!
  13166. @brief get a pointer value (implicit)
  13167. @copydoc get_ptr()
  13168. */
  13169. template<typename PointerType, typename std::enable_if<
  13170. std::is_pointer<PointerType>::value and
  13171. std::is_const<typename std::remove_pointer<PointerType>::type>::value, int>::type = 0>
  13172. constexpr auto get_ptr() const noexcept -> decltype(std::declval<const basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
  13173. {
  13174. // delegate the call to get_impl_ptr<>() const
  13175. return get_impl_ptr(static_cast<PointerType>(nullptr));
  13176. }
  13177. /*!
  13178. @brief get a pointer value (explicit)
  13179. Explicit pointer access to the internally stored JSON value. No copies are
  13180. made.
  13181. @warning The pointer becomes invalid if the underlying JSON object
  13182. changes.
  13183. @tparam PointerType pointer type; must be a pointer to @ref array_t, @ref
  13184. object_t, @ref string_t, @ref boolean_t, @ref number_integer_t,
  13185. @ref number_unsigned_t, or @ref number_float_t.
  13186. @return pointer to the internally stored JSON value if the requested
  13187. pointer type @a PointerType fits to the JSON value; `nullptr` otherwise
  13188. @complexity Constant.
  13189. @liveexample{The example below shows how pointers to internal values of a
  13190. JSON value can be requested. Note that no type conversions are made and a
  13191. `nullptr` is returned if the value and the requested pointer type does not
  13192. match.,get__PointerType}
  13193. @sa @ref get_ptr() for explicit pointer-member access
  13194. @since version 1.0.0
  13195. */
  13196. template<typename PointerType, typename std::enable_if<
  13197. std::is_pointer<PointerType>::value, int>::type = 0>
  13198. auto get() noexcept -> decltype(std::declval<basic_json_t&>().template get_ptr<PointerType>())
  13199. {
  13200. // delegate the call to get_ptr
  13201. return get_ptr<PointerType>();
  13202. }
  13203. /*!
  13204. @brief get a pointer value (explicit)
  13205. @copydoc get()
  13206. */
  13207. template<typename PointerType, typename std::enable_if<
  13208. std::is_pointer<PointerType>::value, int>::type = 0>
  13209. constexpr auto get() const noexcept -> decltype(std::declval<const basic_json_t&>().template get_ptr<PointerType>())
  13210. {
  13211. // delegate the call to get_ptr
  13212. return get_ptr<PointerType>();
  13213. }
  13214. /*!
  13215. @brief get a reference value (implicit)
  13216. Implicit reference access to the internally stored JSON value. No copies
  13217. are made.
  13218. @warning Writing data to the referee of the result yields an undefined
  13219. state.
  13220. @tparam ReferenceType reference type; must be a reference to @ref array_t,
  13221. @ref object_t, @ref string_t, @ref boolean_t, @ref number_integer_t, or
  13222. @ref number_float_t. Enforced by static assertion.
  13223. @return reference to the internally stored JSON value if the requested
  13224. reference type @a ReferenceType fits to the JSON value; throws
  13225. type_error.303 otherwise
  13226. @throw type_error.303 in case passed type @a ReferenceType is incompatible
  13227. with the stored JSON value; see example below
  13228. @complexity Constant.
  13229. @liveexample{The example shows several calls to `get_ref()`.,get_ref}
  13230. @since version 1.1.0
  13231. */
  13232. template<typename ReferenceType, typename std::enable_if<
  13233. std::is_reference<ReferenceType>::value, int>::type = 0>
  13234. ReferenceType get_ref()
  13235. {
  13236. // delegate call to get_ref_impl
  13237. return get_ref_impl<ReferenceType>(*this);
  13238. }
  13239. /*!
  13240. @brief get a reference value (implicit)
  13241. @copydoc get_ref()
  13242. */
  13243. template<typename ReferenceType, typename std::enable_if<
  13244. std::is_reference<ReferenceType>::value and
  13245. std::is_const<typename std::remove_reference<ReferenceType>::type>::value, int>::type = 0>
  13246. ReferenceType get_ref() const
  13247. {
  13248. // delegate call to get_ref_impl
  13249. return get_ref_impl<ReferenceType>(*this);
  13250. }
  13251. /*!
  13252. @brief get a value (implicit)
  13253. Implicit type conversion between the JSON value and a compatible value.
  13254. The call is realized by calling @ref get() const.
  13255. @tparam ValueType non-pointer type compatible to the JSON value, for
  13256. instance `int` for JSON integer numbers, `bool` for JSON booleans, or
  13257. `std::vector` types for JSON arrays. The character type of @ref string_t
  13258. as well as an initializer list of this type is excluded to avoid
  13259. ambiguities as these types implicitly convert to `std::string`.
  13260. @return copy of the JSON value, converted to type @a ValueType
  13261. @throw type_error.302 in case passed type @a ValueType is incompatible
  13262. to the JSON value type (e.g., the JSON value is of type boolean, but a
  13263. string is requested); see example below
  13264. @complexity Linear in the size of the JSON value.
  13265. @liveexample{The example below shows several conversions from JSON values
  13266. to other types. There a few things to note: (1) Floating-point numbers can
  13267. be converted to integers\, (2) A JSON array can be converted to a standard
  13268. `std::vector<short>`\, (3) A JSON object can be converted to C++
  13269. associative containers such as `std::unordered_map<std::string\,
  13270. json>`.,operator__ValueType}
  13271. @since version 1.0.0
  13272. */
  13273. template < typename ValueType, typename std::enable_if <
  13274. not std::is_pointer<ValueType>::value and
  13275. not std::is_same<ValueType, detail::json_ref<basic_json>>::value and
  13276. not std::is_same<ValueType, typename string_t::value_type>::value and
  13277. not detail::is_basic_json<ValueType>::value
  13278. #ifndef _MSC_VER // fix for issue #167 operator<< ambiguity under VS2015
  13279. and not std::is_same<ValueType, std::initializer_list<typename string_t::value_type>>::value
  13280. #if defined(JSON_HAS_CPP_17) && defined(_MSC_VER) and _MSC_VER <= 1914
  13281. and not std::is_same<ValueType, typename std::string_view>::value
  13282. #endif
  13283. #endif
  13284. and detail::is_detected<detail::get_template_function, const basic_json_t&, ValueType>::value
  13285. , int >::type = 0 >
  13286. operator ValueType() const
  13287. {
  13288. // delegate the call to get<>() const
  13289. return get<ValueType>();
  13290. }
  13291. /// @}
  13292. ////////////////////
  13293. // element access //
  13294. ////////////////////
  13295. /// @name element access
  13296. /// Access to the JSON value.
  13297. /// @{
  13298. /*!
  13299. @brief access specified array element with bounds checking
  13300. Returns a reference to the element at specified location @a idx, with
  13301. bounds checking.
  13302. @param[in] idx index of the element to access
  13303. @return reference to the element at index @a idx
  13304. @throw type_error.304 if the JSON value is not an array; in this case,
  13305. calling `at` with an index makes no sense. See example below.
  13306. @throw out_of_range.401 if the index @a idx is out of range of the array;
  13307. that is, `idx >= size()`. See example below.
  13308. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  13309. changes in the JSON value.
  13310. @complexity Constant.
  13311. @since version 1.0.0
  13312. @liveexample{The example below shows how array elements can be read and
  13313. written using `at()`. It also demonstrates the different exceptions that
  13314. can be thrown.,at__size_type}
  13315. */
  13316. reference at(size_type idx)
  13317. {
  13318. // at only works for arrays
  13319. if (JSON_LIKELY(is_array()))
  13320. {
  13321. JSON_TRY
  13322. {
  13323. return m_value.array->at(idx);
  13324. }
  13325. JSON_CATCH (std::out_of_range&)
  13326. {
  13327. // create better exception explanation
  13328. JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range"));
  13329. }
  13330. }
  13331. else
  13332. {
  13333. JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name())));
  13334. }
  13335. }
  13336. /*!
  13337. @brief access specified array element with bounds checking
  13338. Returns a const reference to the element at specified location @a idx,
  13339. with bounds checking.
  13340. @param[in] idx index of the element to access
  13341. @return const reference to the element at index @a idx
  13342. @throw type_error.304 if the JSON value is not an array; in this case,
  13343. calling `at` with an index makes no sense. See example below.
  13344. @throw out_of_range.401 if the index @a idx is out of range of the array;
  13345. that is, `idx >= size()`. See example below.
  13346. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  13347. changes in the JSON value.
  13348. @complexity Constant.
  13349. @since version 1.0.0
  13350. @liveexample{The example below shows how array elements can be read using
  13351. `at()`. It also demonstrates the different exceptions that can be thrown.,
  13352. at__size_type_const}
  13353. */
  13354. const_reference at(size_type idx) const
  13355. {
  13356. // at only works for arrays
  13357. if (JSON_LIKELY(is_array()))
  13358. {
  13359. JSON_TRY
  13360. {
  13361. return m_value.array->at(idx);
  13362. }
  13363. JSON_CATCH (std::out_of_range&)
  13364. {
  13365. // create better exception explanation
  13366. JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range"));
  13367. }
  13368. }
  13369. else
  13370. {
  13371. JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name())));
  13372. }
  13373. }
  13374. /*!
  13375. @brief access specified object element with bounds checking
  13376. Returns a reference to the element at with specified key @a key, with
  13377. bounds checking.
  13378. @param[in] key key of the element to access
  13379. @return reference to the element at key @a key
  13380. @throw type_error.304 if the JSON value is not an object; in this case,
  13381. calling `at` with a key makes no sense. See example below.
  13382. @throw out_of_range.403 if the key @a key is is not stored in the object;
  13383. that is, `find(key) == end()`. See example below.
  13384. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  13385. changes in the JSON value.
  13386. @complexity Logarithmic in the size of the container.
  13387. @sa @ref operator[](const typename object_t::key_type&) for unchecked
  13388. access by reference
  13389. @sa @ref value() for access by value with a default value
  13390. @since version 1.0.0
  13391. @liveexample{The example below shows how object elements can be read and
  13392. written using `at()`. It also demonstrates the different exceptions that
  13393. can be thrown.,at__object_t_key_type}
  13394. */
  13395. reference at(const typename object_t::key_type& key)
  13396. {
  13397. // at only works for objects
  13398. if (JSON_LIKELY(is_object()))
  13399. {
  13400. JSON_TRY
  13401. {
  13402. return m_value.object->at(key);
  13403. }
  13404. JSON_CATCH (std::out_of_range&)
  13405. {
  13406. // create better exception explanation
  13407. JSON_THROW(out_of_range::create(403, "key '" + key + "' not found"));
  13408. }
  13409. }
  13410. else
  13411. {
  13412. JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name())));
  13413. }
  13414. }
  13415. /*!
  13416. @brief access specified object element with bounds checking
  13417. Returns a const reference to the element at with specified key @a key,
  13418. with bounds checking.
  13419. @param[in] key key of the element to access
  13420. @return const reference to the element at key @a key
  13421. @throw type_error.304 if the JSON value is not an object; in this case,
  13422. calling `at` with a key makes no sense. See example below.
  13423. @throw out_of_range.403 if the key @a key is is not stored in the object;
  13424. that is, `find(key) == end()`. See example below.
  13425. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  13426. changes in the JSON value.
  13427. @complexity Logarithmic in the size of the container.
  13428. @sa @ref operator[](const typename object_t::key_type&) for unchecked
  13429. access by reference
  13430. @sa @ref value() for access by value with a default value
  13431. @since version 1.0.0
  13432. @liveexample{The example below shows how object elements can be read using
  13433. `at()`. It also demonstrates the different exceptions that can be thrown.,
  13434. at__object_t_key_type_const}
  13435. */
  13436. const_reference at(const typename object_t::key_type& key) const
  13437. {
  13438. // at only works for objects
  13439. if (JSON_LIKELY(is_object()))
  13440. {
  13441. JSON_TRY
  13442. {
  13443. return m_value.object->at(key);
  13444. }
  13445. JSON_CATCH (std::out_of_range&)
  13446. {
  13447. // create better exception explanation
  13448. JSON_THROW(out_of_range::create(403, "key '" + key + "' not found"));
  13449. }
  13450. }
  13451. else
  13452. {
  13453. JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name())));
  13454. }
  13455. }
  13456. /*!
  13457. @brief access specified array element
  13458. Returns a reference to the element at specified location @a idx.
  13459. @note If @a idx is beyond the range of the array (i.e., `idx >= size()`),
  13460. then the array is silently filled up with `null` values to make `idx` a
  13461. valid reference to the last stored element.
  13462. @param[in] idx index of the element to access
  13463. @return reference to the element at index @a idx
  13464. @throw type_error.305 if the JSON value is not an array or null; in that
  13465. cases, using the [] operator with an index makes no sense.
  13466. @complexity Constant if @a idx is in the range of the array. Otherwise
  13467. linear in `idx - size()`.
  13468. @liveexample{The example below shows how array elements can be read and
  13469. written using `[]` operator. Note the addition of `null`
  13470. values.,operatorarray__size_type}
  13471. @since version 1.0.0
  13472. */
  13473. reference operator[](size_type idx)
  13474. {
  13475. // implicitly convert null value to an empty array
  13476. if (is_null())
  13477. {
  13478. m_type = value_t::array;
  13479. m_value.array = create<array_t>();
  13480. assert_invariant();
  13481. }
  13482. // operator[] only works for arrays
  13483. if (JSON_LIKELY(is_array()))
  13484. {
  13485. // fill up array with null values if given idx is outside range
  13486. if (idx >= m_value.array->size())
  13487. {
  13488. m_value.array->insert(m_value.array->end(),
  13489. idx - m_value.array->size() + 1,
  13490. basic_json());
  13491. }
  13492. return m_value.array->operator[](idx);
  13493. }
  13494. JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name())));
  13495. }
  13496. /*!
  13497. @brief access specified array element
  13498. Returns a const reference to the element at specified location @a idx.
  13499. @param[in] idx index of the element to access
  13500. @return const reference to the element at index @a idx
  13501. @throw type_error.305 if the JSON value is not an array; in that case,
  13502. using the [] operator with an index makes no sense.
  13503. @complexity Constant.
  13504. @liveexample{The example below shows how array elements can be read using
  13505. the `[]` operator.,operatorarray__size_type_const}
  13506. @since version 1.0.0
  13507. */
  13508. const_reference operator[](size_type idx) const
  13509. {
  13510. // const operator[] only works for arrays
  13511. if (JSON_LIKELY(is_array()))
  13512. {
  13513. return m_value.array->operator[](idx);
  13514. }
  13515. JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name())));
  13516. }
  13517. /*!
  13518. @brief access specified object element
  13519. Returns a reference to the element at with specified key @a key.
  13520. @note If @a key is not found in the object, then it is silently added to
  13521. the object and filled with a `null` value to make `key` a valid reference.
  13522. In case the value was `null` before, it is converted to an object.
  13523. @param[in] key key of the element to access
  13524. @return reference to the element at key @a key
  13525. @throw type_error.305 if the JSON value is not an object or null; in that
  13526. cases, using the [] operator with a key makes no sense.
  13527. @complexity Logarithmic in the size of the container.
  13528. @liveexample{The example below shows how object elements can be read and
  13529. written using the `[]` operator.,operatorarray__key_type}
  13530. @sa @ref at(const typename object_t::key_type&) for access by reference
  13531. with range checking
  13532. @sa @ref value() for access by value with a default value
  13533. @since version 1.0.0
  13534. */
  13535. reference operator[](const typename object_t::key_type& key)
  13536. {
  13537. // implicitly convert null value to an empty object
  13538. if (is_null())
  13539. {
  13540. m_type = value_t::object;
  13541. m_value.object = create<object_t>();
  13542. assert_invariant();
  13543. }
  13544. // operator[] only works for objects
  13545. if (JSON_LIKELY(is_object()))
  13546. {
  13547. return m_value.object->operator[](key);
  13548. }
  13549. JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name())));
  13550. }
  13551. /*!
  13552. @brief read-only access specified object element
  13553. Returns a const reference to the element at with specified key @a key. No
  13554. bounds checking is performed.
  13555. @warning If the element with key @a key does not exist, the behavior is
  13556. undefined.
  13557. @param[in] key key of the element to access
  13558. @return const reference to the element at key @a key
  13559. @pre The element with key @a key must exist. **This precondition is
  13560. enforced with an assertion.**
  13561. @throw type_error.305 if the JSON value is not an object; in that case,
  13562. using the [] operator with a key makes no sense.
  13563. @complexity Logarithmic in the size of the container.
  13564. @liveexample{The example below shows how object elements can be read using
  13565. the `[]` operator.,operatorarray__key_type_const}
  13566. @sa @ref at(const typename object_t::key_type&) for access by reference
  13567. with range checking
  13568. @sa @ref value() for access by value with a default value
  13569. @since version 1.0.0
  13570. */
  13571. const_reference operator[](const typename object_t::key_type& key) const
  13572. {
  13573. // const operator[] only works for objects
  13574. if (JSON_LIKELY(is_object()))
  13575. {
  13576. assert(m_value.object->find(key) != m_value.object->end());
  13577. return m_value.object->find(key)->second;
  13578. }
  13579. JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name())));
  13580. }
  13581. /*!
  13582. @brief access specified object element
  13583. Returns a reference to the element at with specified key @a key.
  13584. @note If @a key is not found in the object, then it is silently added to
  13585. the object and filled with a `null` value to make `key` a valid reference.
  13586. In case the value was `null` before, it is converted to an object.
  13587. @param[in] key key of the element to access
  13588. @return reference to the element at key @a key
  13589. @throw type_error.305 if the JSON value is not an object or null; in that
  13590. cases, using the [] operator with a key makes no sense.
  13591. @complexity Logarithmic in the size of the container.
  13592. @liveexample{The example below shows how object elements can be read and
  13593. written using the `[]` operator.,operatorarray__key_type}
  13594. @sa @ref at(const typename object_t::key_type&) for access by reference
  13595. with range checking
  13596. @sa @ref value() for access by value with a default value
  13597. @since version 1.1.0
  13598. */
  13599. template<typename T>
  13600. reference operator[](T* key)
  13601. {
  13602. // implicitly convert null to object
  13603. if (is_null())
  13604. {
  13605. m_type = value_t::object;
  13606. m_value = value_t::object;
  13607. assert_invariant();
  13608. }
  13609. // at only works for objects
  13610. if (JSON_LIKELY(is_object()))
  13611. {
  13612. return m_value.object->operator[](key);
  13613. }
  13614. JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name())));
  13615. }
  13616. /*!
  13617. @brief read-only access specified object element
  13618. Returns a const reference to the element at with specified key @a key. No
  13619. bounds checking is performed.
  13620. @warning If the element with key @a key does not exist, the behavior is
  13621. undefined.
  13622. @param[in] key key of the element to access
  13623. @return const reference to the element at key @a key
  13624. @pre The element with key @a key must exist. **This precondition is
  13625. enforced with an assertion.**
  13626. @throw type_error.305 if the JSON value is not an object; in that case,
  13627. using the [] operator with a key makes no sense.
  13628. @complexity Logarithmic in the size of the container.
  13629. @liveexample{The example below shows how object elements can be read using
  13630. the `[]` operator.,operatorarray__key_type_const}
  13631. @sa @ref at(const typename object_t::key_type&) for access by reference
  13632. with range checking
  13633. @sa @ref value() for access by value with a default value
  13634. @since version 1.1.0
  13635. */
  13636. template<typename T>
  13637. const_reference operator[](T* key) const
  13638. {
  13639. // at only works for objects
  13640. if (JSON_LIKELY(is_object()))
  13641. {
  13642. assert(m_value.object->find(key) != m_value.object->end());
  13643. return m_value.object->find(key)->second;
  13644. }
  13645. JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name())));
  13646. }
  13647. /*!
  13648. @brief access specified object element with default value
  13649. Returns either a copy of an object's element at the specified key @a key
  13650. or a given default value if no element with key @a key exists.
  13651. The function is basically equivalent to executing
  13652. @code {.cpp}
  13653. try {
  13654. return at(key);
  13655. } catch(out_of_range) {
  13656. return default_value;
  13657. }
  13658. @endcode
  13659. @note Unlike @ref at(const typename object_t::key_type&), this function
  13660. does not throw if the given key @a key was not found.
  13661. @note Unlike @ref operator[](const typename object_t::key_type& key), this
  13662. function does not implicitly add an element to the position defined by @a
  13663. key. This function is furthermore also applicable to const objects.
  13664. @param[in] key key of the element to access
  13665. @param[in] default_value the value to return if @a key is not found
  13666. @tparam ValueType type compatible to JSON values, for instance `int` for
  13667. JSON integer numbers, `bool` for JSON booleans, or `std::vector` types for
  13668. JSON arrays. Note the type of the expected value at @a key and the default
  13669. value @a default_value must be compatible.
  13670. @return copy of the element at key @a key or @a default_value if @a key
  13671. is not found
  13672. @throw type_error.306 if the JSON value is not an object; in that case,
  13673. using `value()` with a key makes no sense.
  13674. @complexity Logarithmic in the size of the container.
  13675. @liveexample{The example below shows how object elements can be queried
  13676. with a default value.,basic_json__value}
  13677. @sa @ref at(const typename object_t::key_type&) for access by reference
  13678. with range checking
  13679. @sa @ref operator[](const typename object_t::key_type&) for unchecked
  13680. access by reference
  13681. @since version 1.0.0
  13682. */
  13683. template<class ValueType, typename std::enable_if<
  13684. std::is_convertible<basic_json_t, ValueType>::value, int>::type = 0>
  13685. ValueType value(const typename object_t::key_type& key, const ValueType& default_value) const
  13686. {
  13687. // at only works for objects
  13688. if (JSON_LIKELY(is_object()))
  13689. {
  13690. // if key is found, return value and given default value otherwise
  13691. const auto it = find(key);
  13692. if (it != end())
  13693. {
  13694. return *it;
  13695. }
  13696. return default_value;
  13697. }
  13698. JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name())));
  13699. }
  13700. /*!
  13701. @brief overload for a default value of type const char*
  13702. @copydoc basic_json::value(const typename object_t::key_type&, const ValueType&) const
  13703. */
  13704. string_t value(const typename object_t::key_type& key, const char* default_value) const
  13705. {
  13706. return value(key, string_t(default_value));
  13707. }
  13708. /*!
  13709. @brief access specified object element via JSON Pointer with default value
  13710. Returns either a copy of an object's element at the specified key @a key
  13711. or a given default value if no element with key @a key exists.
  13712. The function is basically equivalent to executing
  13713. @code {.cpp}
  13714. try {
  13715. return at(ptr);
  13716. } catch(out_of_range) {
  13717. return default_value;
  13718. }
  13719. @endcode
  13720. @note Unlike @ref at(const json_pointer&), this function does not throw
  13721. if the given key @a key was not found.
  13722. @param[in] ptr a JSON pointer to the element to access
  13723. @param[in] default_value the value to return if @a ptr found no value
  13724. @tparam ValueType type compatible to JSON values, for instance `int` for
  13725. JSON integer numbers, `bool` for JSON booleans, or `std::vector` types for
  13726. JSON arrays. Note the type of the expected value at @a key and the default
  13727. value @a default_value must be compatible.
  13728. @return copy of the element at key @a key or @a default_value if @a key
  13729. is not found
  13730. @throw type_error.306 if the JSON value is not an object; in that case,
  13731. using `value()` with a key makes no sense.
  13732. @complexity Logarithmic in the size of the container.
  13733. @liveexample{The example below shows how object elements can be queried
  13734. with a default value.,basic_json__value_ptr}
  13735. @sa @ref operator[](const json_pointer&) for unchecked access by reference
  13736. @since version 2.0.2
  13737. */
  13738. template<class ValueType, typename std::enable_if<
  13739. std::is_convertible<basic_json_t, ValueType>::value, int>::type = 0>
  13740. ValueType value(const json_pointer& ptr, const ValueType& default_value) const
  13741. {
  13742. // at only works for objects
  13743. if (JSON_LIKELY(is_object()))
  13744. {
  13745. // if pointer resolves a value, return it or use default value
  13746. JSON_TRY
  13747. {
  13748. return ptr.get_checked(this);
  13749. }
  13750. JSON_INTERNAL_CATCH (out_of_range&)
  13751. {
  13752. return default_value;
  13753. }
  13754. }
  13755. JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name())));
  13756. }
  13757. /*!
  13758. @brief overload for a default value of type const char*
  13759. @copydoc basic_json::value(const json_pointer&, ValueType) const
  13760. */
  13761. string_t value(const json_pointer& ptr, const char* default_value) const
  13762. {
  13763. return value(ptr, string_t(default_value));
  13764. }
  13765. /*!
  13766. @brief access the first element
  13767. Returns a reference to the first element in the container. For a JSON
  13768. container `c`, the expression `c.front()` is equivalent to `*c.begin()`.
  13769. @return In case of a structured type (array or object), a reference to the
  13770. first element is returned. In case of number, string, or boolean values, a
  13771. reference to the value is returned.
  13772. @complexity Constant.
  13773. @pre The JSON value must not be `null` (would throw `std::out_of_range`)
  13774. or an empty array or object (undefined behavior, **guarded by
  13775. assertions**).
  13776. @post The JSON value remains unchanged.
  13777. @throw invalid_iterator.214 when called on `null` value
  13778. @liveexample{The following code shows an example for `front()`.,front}
  13779. @sa @ref back() -- access the last element
  13780. @since version 1.0.0
  13781. */
  13782. reference front()
  13783. {
  13784. return *begin();
  13785. }
  13786. /*!
  13787. @copydoc basic_json::front()
  13788. */
  13789. const_reference front() const
  13790. {
  13791. return *cbegin();
  13792. }
  13793. /*!
  13794. @brief access the last element
  13795. Returns a reference to the last element in the container. For a JSON
  13796. container `c`, the expression `c.back()` is equivalent to
  13797. @code {.cpp}
  13798. auto tmp = c.end();
  13799. --tmp;
  13800. return *tmp;
  13801. @endcode
  13802. @return In case of a structured type (array or object), a reference to the
  13803. last element is returned. In case of number, string, or boolean values, a
  13804. reference to the value is returned.
  13805. @complexity Constant.
  13806. @pre The JSON value must not be `null` (would throw `std::out_of_range`)
  13807. or an empty array or object (undefined behavior, **guarded by
  13808. assertions**).
  13809. @post The JSON value remains unchanged.
  13810. @throw invalid_iterator.214 when called on a `null` value. See example
  13811. below.
  13812. @liveexample{The following code shows an example for `back()`.,back}
  13813. @sa @ref front() -- access the first element
  13814. @since version 1.0.0
  13815. */
  13816. reference back()
  13817. {
  13818. auto tmp = end();
  13819. --tmp;
  13820. return *tmp;
  13821. }
  13822. /*!
  13823. @copydoc basic_json::back()
  13824. */
  13825. const_reference back() const
  13826. {
  13827. auto tmp = cend();
  13828. --tmp;
  13829. return *tmp;
  13830. }
  13831. /*!
  13832. @brief remove element given an iterator
  13833. Removes the element specified by iterator @a pos. The iterator @a pos must
  13834. be valid and dereferenceable. Thus the `end()` iterator (which is valid,
  13835. but is not dereferenceable) cannot be used as a value for @a pos.
  13836. If called on a primitive type other than `null`, the resulting JSON value
  13837. will be `null`.
  13838. @param[in] pos iterator to the element to remove
  13839. @return Iterator following the last removed element. If the iterator @a
  13840. pos refers to the last element, the `end()` iterator is returned.
  13841. @tparam IteratorType an @ref iterator or @ref const_iterator
  13842. @post Invalidates iterators and references at or after the point of the
  13843. erase, including the `end()` iterator.
  13844. @throw type_error.307 if called on a `null` value; example: `"cannot use
  13845. erase() with null"`
  13846. @throw invalid_iterator.202 if called on an iterator which does not belong
  13847. to the current JSON value; example: `"iterator does not fit current
  13848. value"`
  13849. @throw invalid_iterator.205 if called on a primitive type with invalid
  13850. iterator (i.e., any iterator which is not `begin()`); example: `"iterator
  13851. out of range"`
  13852. @complexity The complexity depends on the type:
  13853. - objects: amortized constant
  13854. - arrays: linear in distance between @a pos and the end of the container
  13855. - strings: linear in the length of the string
  13856. - other types: constant
  13857. @liveexample{The example shows the result of `erase()` for different JSON
  13858. types.,erase__IteratorType}
  13859. @sa @ref erase(IteratorType, IteratorType) -- removes the elements in
  13860. the given range
  13861. @sa @ref erase(const typename object_t::key_type&) -- removes the element
  13862. from an object at the given key
  13863. @sa @ref erase(const size_type) -- removes the element from an array at
  13864. the given index
  13865. @since version 1.0.0
  13866. */
  13867. template<class IteratorType, typename std::enable_if<
  13868. std::is_same<IteratorType, typename basic_json_t::iterator>::value or
  13869. std::is_same<IteratorType, typename basic_json_t::const_iterator>::value, int>::type
  13870. = 0>
  13871. IteratorType erase(IteratorType pos)
  13872. {
  13873. // make sure iterator fits the current value
  13874. if (JSON_UNLIKELY(this != pos.m_object))
  13875. {
  13876. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value"));
  13877. }
  13878. IteratorType result = end();
  13879. switch (m_type)
  13880. {
  13881. case value_t::boolean:
  13882. case value_t::number_float:
  13883. case value_t::number_integer:
  13884. case value_t::number_unsigned:
  13885. case value_t::string:
  13886. {
  13887. if (JSON_UNLIKELY(not pos.m_it.primitive_iterator.is_begin()))
  13888. {
  13889. JSON_THROW(invalid_iterator::create(205, "iterator out of range"));
  13890. }
  13891. if (is_string())
  13892. {
  13893. AllocatorType<string_t> alloc;
  13894. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.string);
  13895. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.string, 1);
  13896. m_value.string = nullptr;
  13897. }
  13898. m_type = value_t::null;
  13899. assert_invariant();
  13900. break;
  13901. }
  13902. case value_t::object:
  13903. {
  13904. result.m_it.object_iterator = m_value.object->erase(pos.m_it.object_iterator);
  13905. break;
  13906. }
  13907. case value_t::array:
  13908. {
  13909. result.m_it.array_iterator = m_value.array->erase(pos.m_it.array_iterator);
  13910. break;
  13911. }
  13912. default:
  13913. JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name())));
  13914. }
  13915. return result;
  13916. }
  13917. /*!
  13918. @brief remove elements given an iterator range
  13919. Removes the element specified by the range `[first; last)`. The iterator
  13920. @a first does not need to be dereferenceable if `first == last`: erasing
  13921. an empty range is a no-op.
  13922. If called on a primitive type other than `null`, the resulting JSON value
  13923. will be `null`.
  13924. @param[in] first iterator to the beginning of the range to remove
  13925. @param[in] last iterator past the end of the range to remove
  13926. @return Iterator following the last removed element. If the iterator @a
  13927. second refers to the last element, the `end()` iterator is returned.
  13928. @tparam IteratorType an @ref iterator or @ref const_iterator
  13929. @post Invalidates iterators and references at or after the point of the
  13930. erase, including the `end()` iterator.
  13931. @throw type_error.307 if called on a `null` value; example: `"cannot use
  13932. erase() with null"`
  13933. @throw invalid_iterator.203 if called on iterators which does not belong
  13934. to the current JSON value; example: `"iterators do not fit current value"`
  13935. @throw invalid_iterator.204 if called on a primitive type with invalid
  13936. iterators (i.e., if `first != begin()` and `last != end()`); example:
  13937. `"iterators out of range"`
  13938. @complexity The complexity depends on the type:
  13939. - objects: `log(size()) + std::distance(first, last)`
  13940. - arrays: linear in the distance between @a first and @a last, plus linear
  13941. in the distance between @a last and end of the container
  13942. - strings: linear in the length of the string
  13943. - other types: constant
  13944. @liveexample{The example shows the result of `erase()` for different JSON
  13945. types.,erase__IteratorType_IteratorType}
  13946. @sa @ref erase(IteratorType) -- removes the element at a given position
  13947. @sa @ref erase(const typename object_t::key_type&) -- removes the element
  13948. from an object at the given key
  13949. @sa @ref erase(const size_type) -- removes the element from an array at
  13950. the given index
  13951. @since version 1.0.0
  13952. */
  13953. template<class IteratorType, typename std::enable_if<
  13954. std::is_same<IteratorType, typename basic_json_t::iterator>::value or
  13955. std::is_same<IteratorType, typename basic_json_t::const_iterator>::value, int>::type
  13956. = 0>
  13957. IteratorType erase(IteratorType first, IteratorType last)
  13958. {
  13959. // make sure iterator fits the current value
  13960. if (JSON_UNLIKELY(this != first.m_object or this != last.m_object))
  13961. {
  13962. JSON_THROW(invalid_iterator::create(203, "iterators do not fit current value"));
  13963. }
  13964. IteratorType result = end();
  13965. switch (m_type)
  13966. {
  13967. case value_t::boolean:
  13968. case value_t::number_float:
  13969. case value_t::number_integer:
  13970. case value_t::number_unsigned:
  13971. case value_t::string:
  13972. {
  13973. if (JSON_LIKELY(not first.m_it.primitive_iterator.is_begin()
  13974. or not last.m_it.primitive_iterator.is_end()))
  13975. {
  13976. JSON_THROW(invalid_iterator::create(204, "iterators out of range"));
  13977. }
  13978. if (is_string())
  13979. {
  13980. AllocatorType<string_t> alloc;
  13981. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.string);
  13982. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.string, 1);
  13983. m_value.string = nullptr;
  13984. }
  13985. m_type = value_t::null;
  13986. assert_invariant();
  13987. break;
  13988. }
  13989. case value_t::object:
  13990. {
  13991. result.m_it.object_iterator = m_value.object->erase(first.m_it.object_iterator,
  13992. last.m_it.object_iterator);
  13993. break;
  13994. }
  13995. case value_t::array:
  13996. {
  13997. result.m_it.array_iterator = m_value.array->erase(first.m_it.array_iterator,
  13998. last.m_it.array_iterator);
  13999. break;
  14000. }
  14001. default:
  14002. JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name())));
  14003. }
  14004. return result;
  14005. }
  14006. /*!
  14007. @brief remove element from a JSON object given a key
  14008. Removes elements from a JSON object with the key value @a key.
  14009. @param[in] key value of the elements to remove
  14010. @return Number of elements removed. If @a ObjectType is the default
  14011. `std::map` type, the return value will always be `0` (@a key was not
  14012. found) or `1` (@a key was found).
  14013. @post References and iterators to the erased elements are invalidated.
  14014. Other references and iterators are not affected.
  14015. @throw type_error.307 when called on a type other than JSON object;
  14016. example: `"cannot use erase() with null"`
  14017. @complexity `log(size()) + count(key)`
  14018. @liveexample{The example shows the effect of `erase()`.,erase__key_type}
  14019. @sa @ref erase(IteratorType) -- removes the element at a given position
  14020. @sa @ref erase(IteratorType, IteratorType) -- removes the elements in
  14021. the given range
  14022. @sa @ref erase(const size_type) -- removes the element from an array at
  14023. the given index
  14024. @since version 1.0.0
  14025. */
  14026. size_type erase(const typename object_t::key_type& key)
  14027. {
  14028. // this erase only works for objects
  14029. if (JSON_LIKELY(is_object()))
  14030. {
  14031. return m_value.object->erase(key);
  14032. }
  14033. JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name())));
  14034. }
  14035. /*!
  14036. @brief remove element from a JSON array given an index
  14037. Removes element from a JSON array at the index @a idx.
  14038. @param[in] idx index of the element to remove
  14039. @throw type_error.307 when called on a type other than JSON object;
  14040. example: `"cannot use erase() with null"`
  14041. @throw out_of_range.401 when `idx >= size()`; example: `"array index 17
  14042. is out of range"`
  14043. @complexity Linear in distance between @a idx and the end of the container.
  14044. @liveexample{The example shows the effect of `erase()`.,erase__size_type}
  14045. @sa @ref erase(IteratorType) -- removes the element at a given position
  14046. @sa @ref erase(IteratorType, IteratorType) -- removes the elements in
  14047. the given range
  14048. @sa @ref erase(const typename object_t::key_type&) -- removes the element
  14049. from an object at the given key
  14050. @since version 1.0.0
  14051. */
  14052. void erase(const size_type idx)
  14053. {
  14054. // this erase only works for arrays
  14055. if (JSON_LIKELY(is_array()))
  14056. {
  14057. if (JSON_UNLIKELY(idx >= size()))
  14058. {
  14059. JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range"));
  14060. }
  14061. m_value.array->erase(m_value.array->begin() + static_cast<difference_type>(idx));
  14062. }
  14063. else
  14064. {
  14065. JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name())));
  14066. }
  14067. }
  14068. /// @}
  14069. ////////////
  14070. // lookup //
  14071. ////////////
  14072. /// @name lookup
  14073. /// @{
  14074. /*!
  14075. @brief find an element in a JSON object
  14076. Finds an element in a JSON object with key equivalent to @a key. If the
  14077. element is not found or the JSON value is not an object, end() is
  14078. returned.
  14079. @note This method always returns @ref end() when executed on a JSON type
  14080. that is not an object.
  14081. @param[in] key key value of the element to search for.
  14082. @return Iterator to an element with key equivalent to @a key. If no such
  14083. element is found or the JSON value is not an object, past-the-end (see
  14084. @ref end()) iterator is returned.
  14085. @complexity Logarithmic in the size of the JSON object.
  14086. @liveexample{The example shows how `find()` is used.,find__key_type}
  14087. @since version 1.0.0
  14088. */
  14089. template<typename KeyT>
  14090. iterator find(KeyT&& key)
  14091. {
  14092. auto result = end();
  14093. if (is_object())
  14094. {
  14095. result.m_it.object_iterator = m_value.object->find(std::forward<KeyT>(key));
  14096. }
  14097. return result;
  14098. }
  14099. /*!
  14100. @brief find an element in a JSON object
  14101. @copydoc find(KeyT&&)
  14102. */
  14103. template<typename KeyT>
  14104. const_iterator find(KeyT&& key) const
  14105. {
  14106. auto result = cend();
  14107. if (is_object())
  14108. {
  14109. result.m_it.object_iterator = m_value.object->find(std::forward<KeyT>(key));
  14110. }
  14111. return result;
  14112. }
  14113. /*!
  14114. @brief returns the number of occurrences of a key in a JSON object
  14115. Returns the number of elements with key @a key. If ObjectType is the
  14116. default `std::map` type, the return value will always be `0` (@a key was
  14117. not found) or `1` (@a key was found).
  14118. @note This method always returns `0` when executed on a JSON type that is
  14119. not an object.
  14120. @param[in] key key value of the element to count
  14121. @return Number of elements with key @a key. If the JSON value is not an
  14122. object, the return value will be `0`.
  14123. @complexity Logarithmic in the size of the JSON object.
  14124. @liveexample{The example shows how `count()` is used.,count}
  14125. @since version 1.0.0
  14126. */
  14127. template<typename KeyT>
  14128. size_type count(KeyT&& key) const
  14129. {
  14130. // return 0 for all nonobject types
  14131. return is_object() ? m_value.object->count(std::forward<KeyT>(key)) : 0;
  14132. }
  14133. /// @}
  14134. ///////////////
  14135. // iterators //
  14136. ///////////////
  14137. /// @name iterators
  14138. /// @{
  14139. /*!
  14140. @brief returns an iterator to the first element
  14141. Returns an iterator to the first element.
  14142. @image html range-begin-end.svg "Illustration from cppreference.com"
  14143. @return iterator to the first element
  14144. @complexity Constant.
  14145. @requirement This function helps `basic_json` satisfying the
  14146. [Container](https://en.cppreference.com/w/cpp/named_req/Container)
  14147. requirements:
  14148. - The complexity is constant.
  14149. @liveexample{The following code shows an example for `begin()`.,begin}
  14150. @sa @ref cbegin() -- returns a const iterator to the beginning
  14151. @sa @ref end() -- returns an iterator to the end
  14152. @sa @ref cend() -- returns a const iterator to the end
  14153. @since version 1.0.0
  14154. */
  14155. iterator begin() noexcept
  14156. {
  14157. iterator result(this);
  14158. result.set_begin();
  14159. return result;
  14160. }
  14161. /*!
  14162. @copydoc basic_json::cbegin()
  14163. */
  14164. const_iterator begin() const noexcept
  14165. {
  14166. return cbegin();
  14167. }
  14168. /*!
  14169. @brief returns a const iterator to the first element
  14170. Returns a const iterator to the first element.
  14171. @image html range-begin-end.svg "Illustration from cppreference.com"
  14172. @return const iterator to the first element
  14173. @complexity Constant.
  14174. @requirement This function helps `basic_json` satisfying the
  14175. [Container](https://en.cppreference.com/w/cpp/named_req/Container)
  14176. requirements:
  14177. - The complexity is constant.
  14178. - Has the semantics of `const_cast<const basic_json&>(*this).begin()`.
  14179. @liveexample{The following code shows an example for `cbegin()`.,cbegin}
  14180. @sa @ref begin() -- returns an iterator to the beginning
  14181. @sa @ref end() -- returns an iterator to the end
  14182. @sa @ref cend() -- returns a const iterator to the end
  14183. @since version 1.0.0
  14184. */
  14185. const_iterator cbegin() const noexcept
  14186. {
  14187. const_iterator result(this);
  14188. result.set_begin();
  14189. return result;
  14190. }
  14191. /*!
  14192. @brief returns an iterator to one past the last element
  14193. Returns an iterator to one past the last element.
  14194. @image html range-begin-end.svg "Illustration from cppreference.com"
  14195. @return iterator one past the last element
  14196. @complexity Constant.
  14197. @requirement This function helps `basic_json` satisfying the
  14198. [Container](https://en.cppreference.com/w/cpp/named_req/Container)
  14199. requirements:
  14200. - The complexity is constant.
  14201. @liveexample{The following code shows an example for `end()`.,end}
  14202. @sa @ref cend() -- returns a const iterator to the end
  14203. @sa @ref begin() -- returns an iterator to the beginning
  14204. @sa @ref cbegin() -- returns a const iterator to the beginning
  14205. @since version 1.0.0
  14206. */
  14207. iterator end() noexcept
  14208. {
  14209. iterator result(this);
  14210. result.set_end();
  14211. return result;
  14212. }
  14213. /*!
  14214. @copydoc basic_json::cend()
  14215. */
  14216. const_iterator end() const noexcept
  14217. {
  14218. return cend();
  14219. }
  14220. /*!
  14221. @brief returns a const iterator to one past the last element
  14222. Returns a const iterator to one past the last element.
  14223. @image html range-begin-end.svg "Illustration from cppreference.com"
  14224. @return const iterator one past the last element
  14225. @complexity Constant.
  14226. @requirement This function helps `basic_json` satisfying the
  14227. [Container](https://en.cppreference.com/w/cpp/named_req/Container)
  14228. requirements:
  14229. - The complexity is constant.
  14230. - Has the semantics of `const_cast<const basic_json&>(*this).end()`.
  14231. @liveexample{The following code shows an example for `cend()`.,cend}
  14232. @sa @ref end() -- returns an iterator to the end
  14233. @sa @ref begin() -- returns an iterator to the beginning
  14234. @sa @ref cbegin() -- returns a const iterator to the beginning
  14235. @since version 1.0.0
  14236. */
  14237. const_iterator cend() const noexcept
  14238. {
  14239. const_iterator result(this);
  14240. result.set_end();
  14241. return result;
  14242. }
  14243. /*!
  14244. @brief returns an iterator to the reverse-beginning
  14245. Returns an iterator to the reverse-beginning; that is, the last element.
  14246. @image html range-rbegin-rend.svg "Illustration from cppreference.com"
  14247. @complexity Constant.
  14248. @requirement This function helps `basic_json` satisfying the
  14249. [ReversibleContainer](https://en.cppreference.com/w/cpp/named_req/ReversibleContainer)
  14250. requirements:
  14251. - The complexity is constant.
  14252. - Has the semantics of `reverse_iterator(end())`.
  14253. @liveexample{The following code shows an example for `rbegin()`.,rbegin}
  14254. @sa @ref crbegin() -- returns a const reverse iterator to the beginning
  14255. @sa @ref rend() -- returns a reverse iterator to the end
  14256. @sa @ref crend() -- returns a const reverse iterator to the end
  14257. @since version 1.0.0
  14258. */
  14259. reverse_iterator rbegin() noexcept
  14260. {
  14261. return reverse_iterator(end());
  14262. }
  14263. /*!
  14264. @copydoc basic_json::crbegin()
  14265. */
  14266. const_reverse_iterator rbegin() const noexcept
  14267. {
  14268. return crbegin();
  14269. }
  14270. /*!
  14271. @brief returns an iterator to the reverse-end
  14272. Returns an iterator to the reverse-end; that is, one before the first
  14273. element.
  14274. @image html range-rbegin-rend.svg "Illustration from cppreference.com"
  14275. @complexity Constant.
  14276. @requirement This function helps `basic_json` satisfying the
  14277. [ReversibleContainer](https://en.cppreference.com/w/cpp/named_req/ReversibleContainer)
  14278. requirements:
  14279. - The complexity is constant.
  14280. - Has the semantics of `reverse_iterator(begin())`.
  14281. @liveexample{The following code shows an example for `rend()`.,rend}
  14282. @sa @ref crend() -- returns a const reverse iterator to the end
  14283. @sa @ref rbegin() -- returns a reverse iterator to the beginning
  14284. @sa @ref crbegin() -- returns a const reverse iterator to the beginning
  14285. @since version 1.0.0
  14286. */
  14287. reverse_iterator rend() noexcept
  14288. {
  14289. return reverse_iterator(begin());
  14290. }
  14291. /*!
  14292. @copydoc basic_json::crend()
  14293. */
  14294. const_reverse_iterator rend() const noexcept
  14295. {
  14296. return crend();
  14297. }
  14298. /*!
  14299. @brief returns a const reverse iterator to the last element
  14300. Returns a const iterator to the reverse-beginning; that is, the last
  14301. element.
  14302. @image html range-rbegin-rend.svg "Illustration from cppreference.com"
  14303. @complexity Constant.
  14304. @requirement This function helps `basic_json` satisfying the
  14305. [ReversibleContainer](https://en.cppreference.com/w/cpp/named_req/ReversibleContainer)
  14306. requirements:
  14307. - The complexity is constant.
  14308. - Has the semantics of `const_cast<const basic_json&>(*this).rbegin()`.
  14309. @liveexample{The following code shows an example for `crbegin()`.,crbegin}
  14310. @sa @ref rbegin() -- returns a reverse iterator to the beginning
  14311. @sa @ref rend() -- returns a reverse iterator to the end
  14312. @sa @ref crend() -- returns a const reverse iterator to the end
  14313. @since version 1.0.0
  14314. */
  14315. const_reverse_iterator crbegin() const noexcept
  14316. {
  14317. return const_reverse_iterator(cend());
  14318. }
  14319. /*!
  14320. @brief returns a const reverse iterator to one before the first
  14321. Returns a const reverse iterator to the reverse-end; that is, one before
  14322. the first element.
  14323. @image html range-rbegin-rend.svg "Illustration from cppreference.com"
  14324. @complexity Constant.
  14325. @requirement This function helps `basic_json` satisfying the
  14326. [ReversibleContainer](https://en.cppreference.com/w/cpp/named_req/ReversibleContainer)
  14327. requirements:
  14328. - The complexity is constant.
  14329. - Has the semantics of `const_cast<const basic_json&>(*this).rend()`.
  14330. @liveexample{The following code shows an example for `crend()`.,crend}
  14331. @sa @ref rend() -- returns a reverse iterator to the end
  14332. @sa @ref rbegin() -- returns a reverse iterator to the beginning
  14333. @sa @ref crbegin() -- returns a const reverse iterator to the beginning
  14334. @since version 1.0.0
  14335. */
  14336. const_reverse_iterator crend() const noexcept
  14337. {
  14338. return const_reverse_iterator(cbegin());
  14339. }
  14340. public:
  14341. /*!
  14342. @brief wrapper to access iterator member functions in range-based for
  14343. This function allows to access @ref iterator::key() and @ref
  14344. iterator::value() during range-based for loops. In these loops, a
  14345. reference to the JSON values is returned, so there is no access to the
  14346. underlying iterator.
  14347. For loop without iterator_wrapper:
  14348. @code{cpp}
  14349. for (auto it = j_object.begin(); it != j_object.end(); ++it)
  14350. {
  14351. std::cout << "key: " << it.key() << ", value:" << it.value() << '\n';
  14352. }
  14353. @endcode
  14354. Range-based for loop without iterator proxy:
  14355. @code{cpp}
  14356. for (auto it : j_object)
  14357. {
  14358. // "it" is of type json::reference and has no key() member
  14359. std::cout << "value: " << it << '\n';
  14360. }
  14361. @endcode
  14362. Range-based for loop with iterator proxy:
  14363. @code{cpp}
  14364. for (auto it : json::iterator_wrapper(j_object))
  14365. {
  14366. std::cout << "key: " << it.key() << ", value:" << it.value() << '\n';
  14367. }
  14368. @endcode
  14369. @note When iterating over an array, `key()` will return the index of the
  14370. element as string (see example).
  14371. @param[in] ref reference to a JSON value
  14372. @return iteration proxy object wrapping @a ref with an interface to use in
  14373. range-based for loops
  14374. @liveexample{The following code shows how the wrapper is used,iterator_wrapper}
  14375. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  14376. changes in the JSON value.
  14377. @complexity Constant.
  14378. @note The name of this function is not yet final and may change in the
  14379. future.
  14380. @deprecated This stream operator is deprecated and will be removed in
  14381. future 4.0.0 of the library. Please use @ref items() instead;
  14382. that is, replace `json::iterator_wrapper(j)` with `j.items()`.
  14383. */
  14384. JSON_DEPRECATED
  14385. static iteration_proxy<iterator> iterator_wrapper(reference ref) noexcept
  14386. {
  14387. return ref.items();
  14388. }
  14389. /*!
  14390. @copydoc iterator_wrapper(reference)
  14391. */
  14392. JSON_DEPRECATED
  14393. static iteration_proxy<const_iterator> iterator_wrapper(const_reference ref) noexcept
  14394. {
  14395. return ref.items();
  14396. }
  14397. /*!
  14398. @brief helper to access iterator member functions in range-based for
  14399. This function allows to access @ref iterator::key() and @ref
  14400. iterator::value() during range-based for loops. In these loops, a
  14401. reference to the JSON values is returned, so there is no access to the
  14402. underlying iterator.
  14403. For loop without `items()` function:
  14404. @code{cpp}
  14405. for (auto it = j_object.begin(); it != j_object.end(); ++it)
  14406. {
  14407. std::cout << "key: " << it.key() << ", value:" << it.value() << '\n';
  14408. }
  14409. @endcode
  14410. Range-based for loop without `items()` function:
  14411. @code{cpp}
  14412. for (auto it : j_object)
  14413. {
  14414. // "it" is of type json::reference and has no key() member
  14415. std::cout << "value: " << it << '\n';
  14416. }
  14417. @endcode
  14418. Range-based for loop with `items()` function:
  14419. @code{cpp}
  14420. for (auto& el : j_object.items())
  14421. {
  14422. std::cout << "key: " << el.key() << ", value:" << el.value() << '\n';
  14423. }
  14424. @endcode
  14425. The `items()` function also allows to use
  14426. [structured bindings](https://en.cppreference.com/w/cpp/language/structured_binding)
  14427. (C++17):
  14428. @code{cpp}
  14429. for (auto& [key, val] : j_object.items())
  14430. {
  14431. std::cout << "key: " << key << ", value:" << val << '\n';
  14432. }
  14433. @endcode
  14434. @note When iterating over an array, `key()` will return the index of the
  14435. element as string (see example). For primitive types (e.g., numbers),
  14436. `key()` returns an empty string.
  14437. @return iteration proxy object wrapping @a ref with an interface to use in
  14438. range-based for loops
  14439. @liveexample{The following code shows how the function is used.,items}
  14440. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  14441. changes in the JSON value.
  14442. @complexity Constant.
  14443. @since version 3.1.0, structured bindings support since 3.5.0.
  14444. */
  14445. iteration_proxy<iterator> items() noexcept
  14446. {
  14447. return iteration_proxy<iterator>(*this);
  14448. }
  14449. /*!
  14450. @copydoc items()
  14451. */
  14452. iteration_proxy<const_iterator> items() const noexcept
  14453. {
  14454. return iteration_proxy<const_iterator>(*this);
  14455. }
  14456. /// @}
  14457. //////////////
  14458. // capacity //
  14459. //////////////
  14460. /// @name capacity
  14461. /// @{
  14462. /*!
  14463. @brief checks whether the container is empty.
  14464. Checks if a JSON value has no elements (i.e. whether its @ref size is `0`).
  14465. @return The return value depends on the different types and is
  14466. defined as follows:
  14467. Value type | return value
  14468. ----------- | -------------
  14469. null | `true`
  14470. boolean | `false`
  14471. string | `false`
  14472. number | `false`
  14473. object | result of function `object_t::empty()`
  14474. array | result of function `array_t::empty()`
  14475. @liveexample{The following code uses `empty()` to check if a JSON
  14476. object contains any elements.,empty}
  14477. @complexity Constant, as long as @ref array_t and @ref object_t satisfy
  14478. the Container concept; that is, their `empty()` functions have constant
  14479. complexity.
  14480. @iterators No changes.
  14481. @exceptionsafety No-throw guarantee: this function never throws exceptions.
  14482. @note This function does not return whether a string stored as JSON value
  14483. is empty - it returns whether the JSON container itself is empty which is
  14484. false in the case of a string.
  14485. @requirement This function helps `basic_json` satisfying the
  14486. [Container](https://en.cppreference.com/w/cpp/named_req/Container)
  14487. requirements:
  14488. - The complexity is constant.
  14489. - Has the semantics of `begin() == end()`.
  14490. @sa @ref size() -- returns the number of elements
  14491. @since version 1.0.0
  14492. */
  14493. bool empty() const noexcept
  14494. {
  14495. switch (m_type)
  14496. {
  14497. case value_t::null:
  14498. {
  14499. // null values are empty
  14500. return true;
  14501. }
  14502. case value_t::array:
  14503. {
  14504. // delegate call to array_t::empty()
  14505. return m_value.array->empty();
  14506. }
  14507. case value_t::object:
  14508. {
  14509. // delegate call to object_t::empty()
  14510. return m_value.object->empty();
  14511. }
  14512. default:
  14513. {
  14514. // all other types are nonempty
  14515. return false;
  14516. }
  14517. }
  14518. }
  14519. /*!
  14520. @brief returns the number of elements
  14521. Returns the number of elements in a JSON value.
  14522. @return The return value depends on the different types and is
  14523. defined as follows:
  14524. Value type | return value
  14525. ----------- | -------------
  14526. null | `0`
  14527. boolean | `1`
  14528. string | `1`
  14529. number | `1`
  14530. object | result of function object_t::size()
  14531. array | result of function array_t::size()
  14532. @liveexample{The following code calls `size()` on the different value
  14533. types.,size}
  14534. @complexity Constant, as long as @ref array_t and @ref object_t satisfy
  14535. the Container concept; that is, their size() functions have constant
  14536. complexity.
  14537. @iterators No changes.
  14538. @exceptionsafety No-throw guarantee: this function never throws exceptions.
  14539. @note This function does not return the length of a string stored as JSON
  14540. value - it returns the number of elements in the JSON value which is 1 in
  14541. the case of a string.
  14542. @requirement This function helps `basic_json` satisfying the
  14543. [Container](https://en.cppreference.com/w/cpp/named_req/Container)
  14544. requirements:
  14545. - The complexity is constant.
  14546. - Has the semantics of `std::distance(begin(), end())`.
  14547. @sa @ref empty() -- checks whether the container is empty
  14548. @sa @ref max_size() -- returns the maximal number of elements
  14549. @since version 1.0.0
  14550. */
  14551. size_type size() const noexcept
  14552. {
  14553. switch (m_type)
  14554. {
  14555. case value_t::null:
  14556. {
  14557. // null values are empty
  14558. return 0;
  14559. }
  14560. case value_t::array:
  14561. {
  14562. // delegate call to array_t::size()
  14563. return m_value.array->size();
  14564. }
  14565. case value_t::object:
  14566. {
  14567. // delegate call to object_t::size()
  14568. return m_value.object->size();
  14569. }
  14570. default:
  14571. {
  14572. // all other types have size 1
  14573. return 1;
  14574. }
  14575. }
  14576. }
  14577. /*!
  14578. @brief returns the maximum possible number of elements
  14579. Returns the maximum number of elements a JSON value is able to hold due to
  14580. system or library implementation limitations, i.e. `std::distance(begin(),
  14581. end())` for the JSON value.
  14582. @return The return value depends on the different types and is
  14583. defined as follows:
  14584. Value type | return value
  14585. ----------- | -------------
  14586. null | `0` (same as `size()`)
  14587. boolean | `1` (same as `size()`)
  14588. string | `1` (same as `size()`)
  14589. number | `1` (same as `size()`)
  14590. object | result of function `object_t::max_size()`
  14591. array | result of function `array_t::max_size()`
  14592. @liveexample{The following code calls `max_size()` on the different value
  14593. types. Note the output is implementation specific.,max_size}
  14594. @complexity Constant, as long as @ref array_t and @ref object_t satisfy
  14595. the Container concept; that is, their `max_size()` functions have constant
  14596. complexity.
  14597. @iterators No changes.
  14598. @exceptionsafety No-throw guarantee: this function never throws exceptions.
  14599. @requirement This function helps `basic_json` satisfying the
  14600. [Container](https://en.cppreference.com/w/cpp/named_req/Container)
  14601. requirements:
  14602. - The complexity is constant.
  14603. - Has the semantics of returning `b.size()` where `b` is the largest
  14604. possible JSON value.
  14605. @sa @ref size() -- returns the number of elements
  14606. @since version 1.0.0
  14607. */
  14608. size_type max_size() const noexcept
  14609. {
  14610. switch (m_type)
  14611. {
  14612. case value_t::array:
  14613. {
  14614. // delegate call to array_t::max_size()
  14615. return m_value.array->max_size();
  14616. }
  14617. case value_t::object:
  14618. {
  14619. // delegate call to object_t::max_size()
  14620. return m_value.object->max_size();
  14621. }
  14622. default:
  14623. {
  14624. // all other types have max_size() == size()
  14625. return size();
  14626. }
  14627. }
  14628. }
  14629. /// @}
  14630. ///////////////
  14631. // modifiers //
  14632. ///////////////
  14633. /// @name modifiers
  14634. /// @{
  14635. /*!
  14636. @brief clears the contents
  14637. Clears the content of a JSON value and resets it to the default value as
  14638. if @ref basic_json(value_t) would have been called with the current value
  14639. type from @ref type():
  14640. Value type | initial value
  14641. ----------- | -------------
  14642. null | `null`
  14643. boolean | `false`
  14644. string | `""`
  14645. number | `0`
  14646. object | `{}`
  14647. array | `[]`
  14648. @post Has the same effect as calling
  14649. @code {.cpp}
  14650. *this = basic_json(type());
  14651. @endcode
  14652. @liveexample{The example below shows the effect of `clear()` to different
  14653. JSON types.,clear}
  14654. @complexity Linear in the size of the JSON value.
  14655. @iterators All iterators, pointers and references related to this container
  14656. are invalidated.
  14657. @exceptionsafety No-throw guarantee: this function never throws exceptions.
  14658. @sa @ref basic_json(value_t) -- constructor that creates an object with the
  14659. same value than calling `clear()`
  14660. @since version 1.0.0
  14661. */
  14662. void clear() noexcept
  14663. {
  14664. switch (m_type)
  14665. {
  14666. case value_t::number_integer:
  14667. {
  14668. m_value.number_integer = 0;
  14669. break;
  14670. }
  14671. case value_t::number_unsigned:
  14672. {
  14673. m_value.number_unsigned = 0;
  14674. break;
  14675. }
  14676. case value_t::number_float:
  14677. {
  14678. m_value.number_float = 0.0;
  14679. break;
  14680. }
  14681. case value_t::boolean:
  14682. {
  14683. m_value.boolean = false;
  14684. break;
  14685. }
  14686. case value_t::string:
  14687. {
  14688. m_value.string->clear();
  14689. break;
  14690. }
  14691. case value_t::array:
  14692. {
  14693. m_value.array->clear();
  14694. break;
  14695. }
  14696. case value_t::object:
  14697. {
  14698. m_value.object->clear();
  14699. break;
  14700. }
  14701. default:
  14702. break;
  14703. }
  14704. }
  14705. /*!
  14706. @brief add an object to an array
  14707. Appends the given element @a val to the end of the JSON value. If the
  14708. function is called on a JSON null value, an empty array is created before
  14709. appending @a val.
  14710. @param[in] val the value to add to the JSON array
  14711. @throw type_error.308 when called on a type other than JSON array or
  14712. null; example: `"cannot use push_back() with number"`
  14713. @complexity Amortized constant.
  14714. @liveexample{The example shows how `push_back()` and `+=` can be used to
  14715. add elements to a JSON array. Note how the `null` value was silently
  14716. converted to a JSON array.,push_back}
  14717. @since version 1.0.0
  14718. */
  14719. void push_back(basic_json&& val)
  14720. {
  14721. // push_back only works for null objects or arrays
  14722. if (JSON_UNLIKELY(not(is_null() or is_array())))
  14723. {
  14724. JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name())));
  14725. }
  14726. // transform null object into an array
  14727. if (is_null())
  14728. {
  14729. m_type = value_t::array;
  14730. m_value = value_t::array;
  14731. assert_invariant();
  14732. }
  14733. // add element to array (move semantics)
  14734. m_value.array->push_back(std::move(val));
  14735. // invalidate object
  14736. val.m_type = value_t::null;
  14737. }
  14738. /*!
  14739. @brief add an object to an array
  14740. @copydoc push_back(basic_json&&)
  14741. */
  14742. reference operator+=(basic_json&& val)
  14743. {
  14744. push_back(std::move(val));
  14745. return *this;
  14746. }
  14747. /*!
  14748. @brief add an object to an array
  14749. @copydoc push_back(basic_json&&)
  14750. */
  14751. void push_back(const basic_json& val)
  14752. {
  14753. // push_back only works for null objects or arrays
  14754. if (JSON_UNLIKELY(not(is_null() or is_array())))
  14755. {
  14756. JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name())));
  14757. }
  14758. // transform null object into an array
  14759. if (is_null())
  14760. {
  14761. m_type = value_t::array;
  14762. m_value = value_t::array;
  14763. assert_invariant();
  14764. }
  14765. // add element to array
  14766. m_value.array->push_back(val);
  14767. }
  14768. /*!
  14769. @brief add an object to an array
  14770. @copydoc push_back(basic_json&&)
  14771. */
  14772. reference operator+=(const basic_json& val)
  14773. {
  14774. push_back(val);
  14775. return *this;
  14776. }
  14777. /*!
  14778. @brief add an object to an object
  14779. Inserts the given element @a val to the JSON object. If the function is
  14780. called on a JSON null value, an empty object is created before inserting
  14781. @a val.
  14782. @param[in] val the value to add to the JSON object
  14783. @throw type_error.308 when called on a type other than JSON object or
  14784. null; example: `"cannot use push_back() with number"`
  14785. @complexity Logarithmic in the size of the container, O(log(`size()`)).
  14786. @liveexample{The example shows how `push_back()` and `+=` can be used to
  14787. add elements to a JSON object. Note how the `null` value was silently
  14788. converted to a JSON object.,push_back__object_t__value}
  14789. @since version 1.0.0
  14790. */
  14791. void push_back(const typename object_t::value_type& val)
  14792. {
  14793. // push_back only works for null objects or objects
  14794. if (JSON_UNLIKELY(not(is_null() or is_object())))
  14795. {
  14796. JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name())));
  14797. }
  14798. // transform null object into an object
  14799. if (is_null())
  14800. {
  14801. m_type = value_t::object;
  14802. m_value = value_t::object;
  14803. assert_invariant();
  14804. }
  14805. // add element to array
  14806. m_value.object->insert(val);
  14807. }
  14808. /*!
  14809. @brief add an object to an object
  14810. @copydoc push_back(const typename object_t::value_type&)
  14811. */
  14812. reference operator+=(const typename object_t::value_type& val)
  14813. {
  14814. push_back(val);
  14815. return *this;
  14816. }
  14817. /*!
  14818. @brief add an object to an object
  14819. This function allows to use `push_back` with an initializer list. In case
  14820. 1. the current value is an object,
  14821. 2. the initializer list @a init contains only two elements, and
  14822. 3. the first element of @a init is a string,
  14823. @a init is converted into an object element and added using
  14824. @ref push_back(const typename object_t::value_type&). Otherwise, @a init
  14825. is converted to a JSON value and added using @ref push_back(basic_json&&).
  14826. @param[in] init an initializer list
  14827. @complexity Linear in the size of the initializer list @a init.
  14828. @note This function is required to resolve an ambiguous overload error,
  14829. because pairs like `{"key", "value"}` can be both interpreted as
  14830. `object_t::value_type` or `std::initializer_list<basic_json>`, see
  14831. https://github.com/nlohmann/json/issues/235 for more information.
  14832. @liveexample{The example shows how initializer lists are treated as
  14833. objects when possible.,push_back__initializer_list}
  14834. */
  14835. void push_back(initializer_list_t init)
  14836. {
  14837. if (is_object() and init.size() == 2 and (*init.begin())->is_string())
  14838. {
  14839. basic_json&& key = init.begin()->moved_or_copied();
  14840. push_back(typename object_t::value_type(
  14841. std::move(key.get_ref<string_t&>()), (init.begin() + 1)->moved_or_copied()));
  14842. }
  14843. else
  14844. {
  14845. push_back(basic_json(init));
  14846. }
  14847. }
  14848. /*!
  14849. @brief add an object to an object
  14850. @copydoc push_back(initializer_list_t)
  14851. */
  14852. reference operator+=(initializer_list_t init)
  14853. {
  14854. push_back(init);
  14855. return *this;
  14856. }
  14857. /*!
  14858. @brief add an object to an array
  14859. Creates a JSON value from the passed parameters @a args to the end of the
  14860. JSON value. If the function is called on a JSON null value, an empty array
  14861. is created before appending the value created from @a args.
  14862. @param[in] args arguments to forward to a constructor of @ref basic_json
  14863. @tparam Args compatible types to create a @ref basic_json object
  14864. @throw type_error.311 when called on a type other than JSON array or
  14865. null; example: `"cannot use emplace_back() with number"`
  14866. @complexity Amortized constant.
  14867. @liveexample{The example shows how `push_back()` can be used to add
  14868. elements to a JSON array. Note how the `null` value was silently converted
  14869. to a JSON array.,emplace_back}
  14870. @since version 2.0.8
  14871. */
  14872. template<class... Args>
  14873. void emplace_back(Args&& ... args)
  14874. {
  14875. // emplace_back only works for null objects or arrays
  14876. if (JSON_UNLIKELY(not(is_null() or is_array())))
  14877. {
  14878. JSON_THROW(type_error::create(311, "cannot use emplace_back() with " + std::string(type_name())));
  14879. }
  14880. // transform null object into an array
  14881. if (is_null())
  14882. {
  14883. m_type = value_t::array;
  14884. m_value = value_t::array;
  14885. assert_invariant();
  14886. }
  14887. // add element to array (perfect forwarding)
  14888. m_value.array->emplace_back(std::forward<Args>(args)...);
  14889. }
  14890. /*!
  14891. @brief add an object to an object if key does not exist
  14892. Inserts a new element into a JSON object constructed in-place with the
  14893. given @a args if there is no element with the key in the container. If the
  14894. function is called on a JSON null value, an empty object is created before
  14895. appending the value created from @a args.
  14896. @param[in] args arguments to forward to a constructor of @ref basic_json
  14897. @tparam Args compatible types to create a @ref basic_json object
  14898. @return a pair consisting of an iterator to the inserted element, or the
  14899. already-existing element if no insertion happened, and a bool
  14900. denoting whether the insertion took place.
  14901. @throw type_error.311 when called on a type other than JSON object or
  14902. null; example: `"cannot use emplace() with number"`
  14903. @complexity Logarithmic in the size of the container, O(log(`size()`)).
  14904. @liveexample{The example shows how `emplace()` can be used to add elements
  14905. to a JSON object. Note how the `null` value was silently converted to a
  14906. JSON object. Further note how no value is added if there was already one
  14907. value stored with the same key.,emplace}
  14908. @since version 2.0.8
  14909. */
  14910. template<class... Args>
  14911. std::pair<iterator, bool> emplace(Args&& ... args)
  14912. {
  14913. // emplace only works for null objects or arrays
  14914. if (JSON_UNLIKELY(not(is_null() or is_object())))
  14915. {
  14916. JSON_THROW(type_error::create(311, "cannot use emplace() with " + std::string(type_name())));
  14917. }
  14918. // transform null object into an object
  14919. if (is_null())
  14920. {
  14921. m_type = value_t::object;
  14922. m_value = value_t::object;
  14923. assert_invariant();
  14924. }
  14925. // add element to array (perfect forwarding)
  14926. auto res = m_value.object->emplace(std::forward<Args>(args)...);
  14927. // create result iterator and set iterator to the result of emplace
  14928. auto it = begin();
  14929. it.m_it.object_iterator = res.first;
  14930. // return pair of iterator and boolean
  14931. return {it, res.second};
  14932. }
  14933. /// Helper for insertion of an iterator
  14934. /// @note: This uses std::distance to support GCC 4.8,
  14935. /// see https://github.com/nlohmann/json/pull/1257
  14936. template<typename... Args>
  14937. iterator insert_iterator(const_iterator pos, Args&& ... args)
  14938. {
  14939. iterator result(this);
  14940. assert(m_value.array != nullptr);
  14941. auto insert_pos = std::distance(m_value.array->begin(), pos.m_it.array_iterator);
  14942. m_value.array->insert(pos.m_it.array_iterator, std::forward<Args>(args)...);
  14943. result.m_it.array_iterator = m_value.array->begin() + insert_pos;
  14944. // This could have been written as:
  14945. // result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, cnt, val);
  14946. // but the return value of insert is missing in GCC 4.8, so it is written this way instead.
  14947. return result;
  14948. }
  14949. /*!
  14950. @brief inserts element
  14951. Inserts element @a val before iterator @a pos.
  14952. @param[in] pos iterator before which the content will be inserted; may be
  14953. the end() iterator
  14954. @param[in] val element to insert
  14955. @return iterator pointing to the inserted @a val.
  14956. @throw type_error.309 if called on JSON values other than arrays;
  14957. example: `"cannot use insert() with string"`
  14958. @throw invalid_iterator.202 if @a pos is not an iterator of *this;
  14959. example: `"iterator does not fit current value"`
  14960. @complexity Constant plus linear in the distance between @a pos and end of
  14961. the container.
  14962. @liveexample{The example shows how `insert()` is used.,insert}
  14963. @since version 1.0.0
  14964. */
  14965. iterator insert(const_iterator pos, const basic_json& val)
  14966. {
  14967. // insert only works for arrays
  14968. if (JSON_LIKELY(is_array()))
  14969. {
  14970. // check if iterator pos fits to this JSON value
  14971. if (JSON_UNLIKELY(pos.m_object != this))
  14972. {
  14973. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value"));
  14974. }
  14975. // insert to array and return iterator
  14976. return insert_iterator(pos, val);
  14977. }
  14978. JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name())));
  14979. }
  14980. /*!
  14981. @brief inserts element
  14982. @copydoc insert(const_iterator, const basic_json&)
  14983. */
  14984. iterator insert(const_iterator pos, basic_json&& val)
  14985. {
  14986. return insert(pos, val);
  14987. }
  14988. /*!
  14989. @brief inserts elements
  14990. Inserts @a cnt copies of @a val before iterator @a pos.
  14991. @param[in] pos iterator before which the content will be inserted; may be
  14992. the end() iterator
  14993. @param[in] cnt number of copies of @a val to insert
  14994. @param[in] val element to insert
  14995. @return iterator pointing to the first element inserted, or @a pos if
  14996. `cnt==0`
  14997. @throw type_error.309 if called on JSON values other than arrays; example:
  14998. `"cannot use insert() with string"`
  14999. @throw invalid_iterator.202 if @a pos is not an iterator of *this;
  15000. example: `"iterator does not fit current value"`
  15001. @complexity Linear in @a cnt plus linear in the distance between @a pos
  15002. and end of the container.
  15003. @liveexample{The example shows how `insert()` is used.,insert__count}
  15004. @since version 1.0.0
  15005. */
  15006. iterator insert(const_iterator pos, size_type cnt, const basic_json& val)
  15007. {
  15008. // insert only works for arrays
  15009. if (JSON_LIKELY(is_array()))
  15010. {
  15011. // check if iterator pos fits to this JSON value
  15012. if (JSON_UNLIKELY(pos.m_object != this))
  15013. {
  15014. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value"));
  15015. }
  15016. // insert to array and return iterator
  15017. return insert_iterator(pos, cnt, val);
  15018. }
  15019. JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name())));
  15020. }
  15021. /*!
  15022. @brief inserts elements
  15023. Inserts elements from range `[first, last)` before iterator @a pos.
  15024. @param[in] pos iterator before which the content will be inserted; may be
  15025. the end() iterator
  15026. @param[in] first begin of the range of elements to insert
  15027. @param[in] last end of the range of elements to insert
  15028. @throw type_error.309 if called on JSON values other than arrays; example:
  15029. `"cannot use insert() with string"`
  15030. @throw invalid_iterator.202 if @a pos is not an iterator of *this;
  15031. example: `"iterator does not fit current value"`
  15032. @throw invalid_iterator.210 if @a first and @a last do not belong to the
  15033. same JSON value; example: `"iterators do not fit"`
  15034. @throw invalid_iterator.211 if @a first or @a last are iterators into
  15035. container for which insert is called; example: `"passed iterators may not
  15036. belong to container"`
  15037. @return iterator pointing to the first element inserted, or @a pos if
  15038. `first==last`
  15039. @complexity Linear in `std::distance(first, last)` plus linear in the
  15040. distance between @a pos and end of the container.
  15041. @liveexample{The example shows how `insert()` is used.,insert__range}
  15042. @since version 1.0.0
  15043. */
  15044. iterator insert(const_iterator pos, const_iterator first, const_iterator last)
  15045. {
  15046. // insert only works for arrays
  15047. if (JSON_UNLIKELY(not is_array()))
  15048. {
  15049. JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name())));
  15050. }
  15051. // check if iterator pos fits to this JSON value
  15052. if (JSON_UNLIKELY(pos.m_object != this))
  15053. {
  15054. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value"));
  15055. }
  15056. // check if range iterators belong to the same JSON object
  15057. if (JSON_UNLIKELY(first.m_object != last.m_object))
  15058. {
  15059. JSON_THROW(invalid_iterator::create(210, "iterators do not fit"));
  15060. }
  15061. if (JSON_UNLIKELY(first.m_object == this))
  15062. {
  15063. JSON_THROW(invalid_iterator::create(211, "passed iterators may not belong to container"));
  15064. }
  15065. // insert to array and return iterator
  15066. return insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator);
  15067. }
  15068. /*!
  15069. @brief inserts elements
  15070. Inserts elements from initializer list @a ilist before iterator @a pos.
  15071. @param[in] pos iterator before which the content will be inserted; may be
  15072. the end() iterator
  15073. @param[in] ilist initializer list to insert the values from
  15074. @throw type_error.309 if called on JSON values other than arrays; example:
  15075. `"cannot use insert() with string"`
  15076. @throw invalid_iterator.202 if @a pos is not an iterator of *this;
  15077. example: `"iterator does not fit current value"`
  15078. @return iterator pointing to the first element inserted, or @a pos if
  15079. `ilist` is empty
  15080. @complexity Linear in `ilist.size()` plus linear in the distance between
  15081. @a pos and end of the container.
  15082. @liveexample{The example shows how `insert()` is used.,insert__ilist}
  15083. @since version 1.0.0
  15084. */
  15085. iterator insert(const_iterator pos, initializer_list_t ilist)
  15086. {
  15087. // insert only works for arrays
  15088. if (JSON_UNLIKELY(not is_array()))
  15089. {
  15090. JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name())));
  15091. }
  15092. // check if iterator pos fits to this JSON value
  15093. if (JSON_UNLIKELY(pos.m_object != this))
  15094. {
  15095. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value"));
  15096. }
  15097. // insert to array and return iterator
  15098. return insert_iterator(pos, ilist.begin(), ilist.end());
  15099. }
  15100. /*!
  15101. @brief inserts elements
  15102. Inserts elements from range `[first, last)`.
  15103. @param[in] first begin of the range of elements to insert
  15104. @param[in] last end of the range of elements to insert
  15105. @throw type_error.309 if called on JSON values other than objects; example:
  15106. `"cannot use insert() with string"`
  15107. @throw invalid_iterator.202 if iterator @a first or @a last does does not
  15108. point to an object; example: `"iterators first and last must point to
  15109. objects"`
  15110. @throw invalid_iterator.210 if @a first and @a last do not belong to the
  15111. same JSON value; example: `"iterators do not fit"`
  15112. @complexity Logarithmic: `O(N*log(size() + N))`, where `N` is the number
  15113. of elements to insert.
  15114. @liveexample{The example shows how `insert()` is used.,insert__range_object}
  15115. @since version 3.0.0
  15116. */
  15117. void insert(const_iterator first, const_iterator last)
  15118. {
  15119. // insert only works for objects
  15120. if (JSON_UNLIKELY(not is_object()))
  15121. {
  15122. JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name())));
  15123. }
  15124. // check if range iterators belong to the same JSON object
  15125. if (JSON_UNLIKELY(first.m_object != last.m_object))
  15126. {
  15127. JSON_THROW(invalid_iterator::create(210, "iterators do not fit"));
  15128. }
  15129. // passed iterators must belong to objects
  15130. if (JSON_UNLIKELY(not first.m_object->is_object()))
  15131. {
  15132. JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects"));
  15133. }
  15134. m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator);
  15135. }
  15136. /*!
  15137. @brief updates a JSON object from another object, overwriting existing keys
  15138. Inserts all values from JSON object @a j and overwrites existing keys.
  15139. @param[in] j JSON object to read values from
  15140. @throw type_error.312 if called on JSON values other than objects; example:
  15141. `"cannot use update() with string"`
  15142. @complexity O(N*log(size() + N)), where N is the number of elements to
  15143. insert.
  15144. @liveexample{The example shows how `update()` is used.,update}
  15145. @sa https://docs.python.org/3.6/library/stdtypes.html#dict.update
  15146. @since version 3.0.0
  15147. */
  15148. void update(const_reference j)
  15149. {
  15150. // implicitly convert null value to an empty object
  15151. if (is_null())
  15152. {
  15153. m_type = value_t::object;
  15154. m_value.object = create<object_t>();
  15155. assert_invariant();
  15156. }
  15157. if (JSON_UNLIKELY(not is_object()))
  15158. {
  15159. JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name())));
  15160. }
  15161. if (JSON_UNLIKELY(not j.is_object()))
  15162. {
  15163. JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(j.type_name())));
  15164. }
  15165. for (auto it = j.cbegin(); it != j.cend(); ++it)
  15166. {
  15167. m_value.object->operator[](it.key()) = it.value();
  15168. }
  15169. }
  15170. /*!
  15171. @brief updates a JSON object from another object, overwriting existing keys
  15172. Inserts all values from from range `[first, last)` and overwrites existing
  15173. keys.
  15174. @param[in] first begin of the range of elements to insert
  15175. @param[in] last end of the range of elements to insert
  15176. @throw type_error.312 if called on JSON values other than objects; example:
  15177. `"cannot use update() with string"`
  15178. @throw invalid_iterator.202 if iterator @a first or @a last does does not
  15179. point to an object; example: `"iterators first and last must point to
  15180. objects"`
  15181. @throw invalid_iterator.210 if @a first and @a last do not belong to the
  15182. same JSON value; example: `"iterators do not fit"`
  15183. @complexity O(N*log(size() + N)), where N is the number of elements to
  15184. insert.
  15185. @liveexample{The example shows how `update()` is used__range.,update}
  15186. @sa https://docs.python.org/3.6/library/stdtypes.html#dict.update
  15187. @since version 3.0.0
  15188. */
  15189. void update(const_iterator first, const_iterator last)
  15190. {
  15191. // implicitly convert null value to an empty object
  15192. if (is_null())
  15193. {
  15194. m_type = value_t::object;
  15195. m_value.object = create<object_t>();
  15196. assert_invariant();
  15197. }
  15198. if (JSON_UNLIKELY(not is_object()))
  15199. {
  15200. JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name())));
  15201. }
  15202. // check if range iterators belong to the same JSON object
  15203. if (JSON_UNLIKELY(first.m_object != last.m_object))
  15204. {
  15205. JSON_THROW(invalid_iterator::create(210, "iterators do not fit"));
  15206. }
  15207. // passed iterators must belong to objects
  15208. if (JSON_UNLIKELY(not first.m_object->is_object()
  15209. or not last.m_object->is_object()))
  15210. {
  15211. JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects"));
  15212. }
  15213. for (auto it = first; it != last; ++it)
  15214. {
  15215. m_value.object->operator[](it.key()) = it.value();
  15216. }
  15217. }
  15218. /*!
  15219. @brief exchanges the values
  15220. Exchanges the contents of the JSON value with those of @a other. Does not
  15221. invoke any move, copy, or swap operations on individual elements. All
  15222. iterators and references remain valid. The past-the-end iterator is
  15223. invalidated.
  15224. @param[in,out] other JSON value to exchange the contents with
  15225. @complexity Constant.
  15226. @liveexample{The example below shows how JSON values can be swapped with
  15227. `swap()`.,swap__reference}
  15228. @since version 1.0.0
  15229. */
  15230. void swap(reference other) noexcept (
  15231. std::is_nothrow_move_constructible<value_t>::value and
  15232. std::is_nothrow_move_assignable<value_t>::value and
  15233. std::is_nothrow_move_constructible<json_value>::value and
  15234. std::is_nothrow_move_assignable<json_value>::value
  15235. )
  15236. {
  15237. std::swap(m_type, other.m_type);
  15238. std::swap(m_value, other.m_value);
  15239. assert_invariant();
  15240. }
  15241. /*!
  15242. @brief exchanges the values
  15243. Exchanges the contents of a JSON array with those of @a other. Does not
  15244. invoke any move, copy, or swap operations on individual elements. All
  15245. iterators and references remain valid. The past-the-end iterator is
  15246. invalidated.
  15247. @param[in,out] other array to exchange the contents with
  15248. @throw type_error.310 when JSON value is not an array; example: `"cannot
  15249. use swap() with string"`
  15250. @complexity Constant.
  15251. @liveexample{The example below shows how arrays can be swapped with
  15252. `swap()`.,swap__array_t}
  15253. @since version 1.0.0
  15254. */
  15255. void swap(array_t& other)
  15256. {
  15257. // swap only works for arrays
  15258. if (JSON_LIKELY(is_array()))
  15259. {
  15260. std::swap(*(m_value.array), other);
  15261. }
  15262. else
  15263. {
  15264. JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name())));
  15265. }
  15266. }
  15267. /*!
  15268. @brief exchanges the values
  15269. Exchanges the contents of a JSON object with those of @a other. Does not
  15270. invoke any move, copy, or swap operations on individual elements. All
  15271. iterators and references remain valid. The past-the-end iterator is
  15272. invalidated.
  15273. @param[in,out] other object to exchange the contents with
  15274. @throw type_error.310 when JSON value is not an object; example:
  15275. `"cannot use swap() with string"`
  15276. @complexity Constant.
  15277. @liveexample{The example below shows how objects can be swapped with
  15278. `swap()`.,swap__object_t}
  15279. @since version 1.0.0
  15280. */
  15281. void swap(object_t& other)
  15282. {
  15283. // swap only works for objects
  15284. if (JSON_LIKELY(is_object()))
  15285. {
  15286. std::swap(*(m_value.object), other);
  15287. }
  15288. else
  15289. {
  15290. JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name())));
  15291. }
  15292. }
  15293. /*!
  15294. @brief exchanges the values
  15295. Exchanges the contents of a JSON string with those of @a other. Does not
  15296. invoke any move, copy, or swap operations on individual elements. All
  15297. iterators and references remain valid. The past-the-end iterator is
  15298. invalidated.
  15299. @param[in,out] other string to exchange the contents with
  15300. @throw type_error.310 when JSON value is not a string; example: `"cannot
  15301. use swap() with boolean"`
  15302. @complexity Constant.
  15303. @liveexample{The example below shows how strings can be swapped with
  15304. `swap()`.,swap__string_t}
  15305. @since version 1.0.0
  15306. */
  15307. void swap(string_t& other)
  15308. {
  15309. // swap only works for strings
  15310. if (JSON_LIKELY(is_string()))
  15311. {
  15312. std::swap(*(m_value.string), other);
  15313. }
  15314. else
  15315. {
  15316. JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name())));
  15317. }
  15318. }
  15319. /// @}
  15320. public:
  15321. //////////////////////////////////////////
  15322. // lexicographical comparison operators //
  15323. //////////////////////////////////////////
  15324. /// @name lexicographical comparison operators
  15325. /// @{
  15326. /*!
  15327. @brief comparison: equal
  15328. Compares two JSON values for equality according to the following rules:
  15329. - Two JSON values are equal if (1) they are from the same type and (2)
  15330. their stored values are the same according to their respective
  15331. `operator==`.
  15332. - Integer and floating-point numbers are automatically converted before
  15333. comparison. Note than two NaN values are always treated as unequal.
  15334. - Two JSON null values are equal.
  15335. @note Floating-point inside JSON values numbers are compared with
  15336. `json::number_float_t::operator==` which is `double::operator==` by
  15337. default. To compare floating-point while respecting an epsilon, an alternative
  15338. [comparison function](https://github.com/mariokonrad/marnav/blob/master/src/marnav/math/floatingpoint.hpp#L34-#L39)
  15339. could be used, for instance
  15340. @code {.cpp}
  15341. template<typename T, typename = typename std::enable_if<std::is_floating_point<T>::value, T>::type>
  15342. inline bool is_same(T a, T b, T epsilon = std::numeric_limits<T>::epsilon()) noexcept
  15343. {
  15344. return std::abs(a - b) <= epsilon;
  15345. }
  15346. @endcode
  15347. @note NaN values never compare equal to themselves or to other NaN values.
  15348. @param[in] lhs first JSON value to consider
  15349. @param[in] rhs second JSON value to consider
  15350. @return whether the values @a lhs and @a rhs are equal
  15351. @exceptionsafety No-throw guarantee: this function never throws exceptions.
  15352. @complexity Linear.
  15353. @liveexample{The example demonstrates comparing several JSON
  15354. types.,operator__equal}
  15355. @since version 1.0.0
  15356. */
  15357. friend bool operator==(const_reference lhs, const_reference rhs) noexcept
  15358. {
  15359. const auto lhs_type = lhs.type();
  15360. const auto rhs_type = rhs.type();
  15361. if (lhs_type == rhs_type)
  15362. {
  15363. switch (lhs_type)
  15364. {
  15365. case value_t::array:
  15366. return (*lhs.m_value.array == *rhs.m_value.array);
  15367. case value_t::object:
  15368. return (*lhs.m_value.object == *rhs.m_value.object);
  15369. case value_t::null:
  15370. return true;
  15371. case value_t::string:
  15372. return (*lhs.m_value.string == *rhs.m_value.string);
  15373. case value_t::boolean:
  15374. return (lhs.m_value.boolean == rhs.m_value.boolean);
  15375. case value_t::number_integer:
  15376. return (lhs.m_value.number_integer == rhs.m_value.number_integer);
  15377. case value_t::number_unsigned:
  15378. return (lhs.m_value.number_unsigned == rhs.m_value.number_unsigned);
  15379. case value_t::number_float:
  15380. return (lhs.m_value.number_float == rhs.m_value.number_float);
  15381. default:
  15382. return false;
  15383. }
  15384. }
  15385. else if (lhs_type == value_t::number_integer and rhs_type == value_t::number_float)
  15386. {
  15387. return (static_cast<number_float_t>(lhs.m_value.number_integer) == rhs.m_value.number_float);
  15388. }
  15389. else if (lhs_type == value_t::number_float and rhs_type == value_t::number_integer)
  15390. {
  15391. return (lhs.m_value.number_float == static_cast<number_float_t>(rhs.m_value.number_integer));
  15392. }
  15393. else if (lhs_type == value_t::number_unsigned and rhs_type == value_t::number_float)
  15394. {
  15395. return (static_cast<number_float_t>(lhs.m_value.number_unsigned) == rhs.m_value.number_float);
  15396. }
  15397. else if (lhs_type == value_t::number_float and rhs_type == value_t::number_unsigned)
  15398. {
  15399. return (lhs.m_value.number_float == static_cast<number_float_t>(rhs.m_value.number_unsigned));
  15400. }
  15401. else if (lhs_type == value_t::number_unsigned and rhs_type == value_t::number_integer)
  15402. {
  15403. return (static_cast<number_integer_t>(lhs.m_value.number_unsigned) == rhs.m_value.number_integer);
  15404. }
  15405. else if (lhs_type == value_t::number_integer and rhs_type == value_t::number_unsigned)
  15406. {
  15407. return (lhs.m_value.number_integer == static_cast<number_integer_t>(rhs.m_value.number_unsigned));
  15408. }
  15409. return false;
  15410. }
  15411. /*!
  15412. @brief comparison: equal
  15413. @copydoc operator==(const_reference, const_reference)
  15414. */
  15415. template<typename ScalarType, typename std::enable_if<
  15416. std::is_scalar<ScalarType>::value, int>::type = 0>
  15417. friend bool operator==(const_reference lhs, const ScalarType rhs) noexcept
  15418. {
  15419. return (lhs == basic_json(rhs));
  15420. }
  15421. /*!
  15422. @brief comparison: equal
  15423. @copydoc operator==(const_reference, const_reference)
  15424. */
  15425. template<typename ScalarType, typename std::enable_if<
  15426. std::is_scalar<ScalarType>::value, int>::type = 0>
  15427. friend bool operator==(const ScalarType lhs, const_reference rhs) noexcept
  15428. {
  15429. return (basic_json(lhs) == rhs);
  15430. }
  15431. /*!
  15432. @brief comparison: not equal
  15433. Compares two JSON values for inequality by calculating `not (lhs == rhs)`.
  15434. @param[in] lhs first JSON value to consider
  15435. @param[in] rhs second JSON value to consider
  15436. @return whether the values @a lhs and @a rhs are not equal
  15437. @complexity Linear.
  15438. @exceptionsafety No-throw guarantee: this function never throws exceptions.
  15439. @liveexample{The example demonstrates comparing several JSON
  15440. types.,operator__notequal}
  15441. @since version 1.0.0
  15442. */
  15443. friend bool operator!=(const_reference lhs, const_reference rhs) noexcept
  15444. {
  15445. return not (lhs == rhs);
  15446. }
  15447. /*!
  15448. @brief comparison: not equal
  15449. @copydoc operator!=(const_reference, const_reference)
  15450. */
  15451. template<typename ScalarType, typename std::enable_if<
  15452. std::is_scalar<ScalarType>::value, int>::type = 0>
  15453. friend bool operator!=(const_reference lhs, const ScalarType rhs) noexcept
  15454. {
  15455. return (lhs != basic_json(rhs));
  15456. }
  15457. /*!
  15458. @brief comparison: not equal
  15459. @copydoc operator!=(const_reference, const_reference)
  15460. */
  15461. template<typename ScalarType, typename std::enable_if<
  15462. std::is_scalar<ScalarType>::value, int>::type = 0>
  15463. friend bool operator!=(const ScalarType lhs, const_reference rhs) noexcept
  15464. {
  15465. return (basic_json(lhs) != rhs);
  15466. }
  15467. /*!
  15468. @brief comparison: less than
  15469. Compares whether one JSON value @a lhs is less than another JSON value @a
  15470. rhs according to the following rules:
  15471. - If @a lhs and @a rhs have the same type, the values are compared using
  15472. the default `<` operator.
  15473. - Integer and floating-point numbers are automatically converted before
  15474. comparison
  15475. - In case @a lhs and @a rhs have different types, the values are ignored
  15476. and the order of the types is considered, see
  15477. @ref operator<(const value_t, const value_t).
  15478. @param[in] lhs first JSON value to consider
  15479. @param[in] rhs second JSON value to consider
  15480. @return whether @a lhs is less than @a rhs
  15481. @complexity Linear.
  15482. @exceptionsafety No-throw guarantee: this function never throws exceptions.
  15483. @liveexample{The example demonstrates comparing several JSON
  15484. types.,operator__less}
  15485. @since version 1.0.0
  15486. */
  15487. friend bool operator<(const_reference lhs, const_reference rhs) noexcept
  15488. {
  15489. const auto lhs_type = lhs.type();
  15490. const auto rhs_type = rhs.type();
  15491. if (lhs_type == rhs_type)
  15492. {
  15493. switch (lhs_type)
  15494. {
  15495. case value_t::array:
  15496. return (*lhs.m_value.array) < (*rhs.m_value.array);
  15497. case value_t::object:
  15498. return *lhs.m_value.object < *rhs.m_value.object;
  15499. case value_t::null:
  15500. return false;
  15501. case value_t::string:
  15502. return *lhs.m_value.string < *rhs.m_value.string;
  15503. case value_t::boolean:
  15504. return lhs.m_value.boolean < rhs.m_value.boolean;
  15505. case value_t::number_integer:
  15506. return lhs.m_value.number_integer < rhs.m_value.number_integer;
  15507. case value_t::number_unsigned:
  15508. return lhs.m_value.number_unsigned < rhs.m_value.number_unsigned;
  15509. case value_t::number_float:
  15510. return lhs.m_value.number_float < rhs.m_value.number_float;
  15511. default:
  15512. return false;
  15513. }
  15514. }
  15515. else if (lhs_type == value_t::number_integer and rhs_type == value_t::number_float)
  15516. {
  15517. return static_cast<number_float_t>(lhs.m_value.number_integer) < rhs.m_value.number_float;
  15518. }
  15519. else if (lhs_type == value_t::number_float and rhs_type == value_t::number_integer)
  15520. {
  15521. return lhs.m_value.number_float < static_cast<number_float_t>(rhs.m_value.number_integer);
  15522. }
  15523. else if (lhs_type == value_t::number_unsigned and rhs_type == value_t::number_float)
  15524. {
  15525. return static_cast<number_float_t>(lhs.m_value.number_unsigned) < rhs.m_value.number_float;
  15526. }
  15527. else if (lhs_type == value_t::number_float and rhs_type == value_t::number_unsigned)
  15528. {
  15529. return lhs.m_value.number_float < static_cast<number_float_t>(rhs.m_value.number_unsigned);
  15530. }
  15531. else if (lhs_type == value_t::number_integer and rhs_type == value_t::number_unsigned)
  15532. {
  15533. return lhs.m_value.number_integer < static_cast<number_integer_t>(rhs.m_value.number_unsigned);
  15534. }
  15535. else if (lhs_type == value_t::number_unsigned and rhs_type == value_t::number_integer)
  15536. {
  15537. return static_cast<number_integer_t>(lhs.m_value.number_unsigned) < rhs.m_value.number_integer;
  15538. }
  15539. // We only reach this line if we cannot compare values. In that case,
  15540. // we compare types. Note we have to call the operator explicitly,
  15541. // because MSVC has problems otherwise.
  15542. return operator<(lhs_type, rhs_type);
  15543. }
  15544. /*!
  15545. @brief comparison: less than
  15546. @copydoc operator<(const_reference, const_reference)
  15547. */
  15548. template<typename ScalarType, typename std::enable_if<
  15549. std::is_scalar<ScalarType>::value, int>::type = 0>
  15550. friend bool operator<(const_reference lhs, const ScalarType rhs) noexcept
  15551. {
  15552. return (lhs < basic_json(rhs));
  15553. }
  15554. /*!
  15555. @brief comparison: less than
  15556. @copydoc operator<(const_reference, const_reference)
  15557. */
  15558. template<typename ScalarType, typename std::enable_if<
  15559. std::is_scalar<ScalarType>::value, int>::type = 0>
  15560. friend bool operator<(const ScalarType lhs, const_reference rhs) noexcept
  15561. {
  15562. return (basic_json(lhs) < rhs);
  15563. }
  15564. /*!
  15565. @brief comparison: less than or equal
  15566. Compares whether one JSON value @a lhs is less than or equal to another
  15567. JSON value by calculating `not (rhs < lhs)`.
  15568. @param[in] lhs first JSON value to consider
  15569. @param[in] rhs second JSON value to consider
  15570. @return whether @a lhs is less than or equal to @a rhs
  15571. @complexity Linear.
  15572. @exceptionsafety No-throw guarantee: this function never throws exceptions.
  15573. @liveexample{The example demonstrates comparing several JSON
  15574. types.,operator__greater}
  15575. @since version 1.0.0
  15576. */
  15577. friend bool operator<=(const_reference lhs, const_reference rhs) noexcept
  15578. {
  15579. return not (rhs < lhs);
  15580. }
  15581. /*!
  15582. @brief comparison: less than or equal
  15583. @copydoc operator<=(const_reference, const_reference)
  15584. */
  15585. template<typename ScalarType, typename std::enable_if<
  15586. std::is_scalar<ScalarType>::value, int>::type = 0>
  15587. friend bool operator<=(const_reference lhs, const ScalarType rhs) noexcept
  15588. {
  15589. return (lhs <= basic_json(rhs));
  15590. }
  15591. /*!
  15592. @brief comparison: less than or equal
  15593. @copydoc operator<=(const_reference, const_reference)
  15594. */
  15595. template<typename ScalarType, typename std::enable_if<
  15596. std::is_scalar<ScalarType>::value, int>::type = 0>
  15597. friend bool operator<=(const ScalarType lhs, const_reference rhs) noexcept
  15598. {
  15599. return (basic_json(lhs) <= rhs);
  15600. }
  15601. /*!
  15602. @brief comparison: greater than
  15603. Compares whether one JSON value @a lhs is greater than another
  15604. JSON value by calculating `not (lhs <= rhs)`.
  15605. @param[in] lhs first JSON value to consider
  15606. @param[in] rhs second JSON value to consider
  15607. @return whether @a lhs is greater than to @a rhs
  15608. @complexity Linear.
  15609. @exceptionsafety No-throw guarantee: this function never throws exceptions.
  15610. @liveexample{The example demonstrates comparing several JSON
  15611. types.,operator__lessequal}
  15612. @since version 1.0.0
  15613. */
  15614. friend bool operator>(const_reference lhs, const_reference rhs) noexcept
  15615. {
  15616. return not (lhs <= rhs);
  15617. }
  15618. /*!
  15619. @brief comparison: greater than
  15620. @copydoc operator>(const_reference, const_reference)
  15621. */
  15622. template<typename ScalarType, typename std::enable_if<
  15623. std::is_scalar<ScalarType>::value, int>::type = 0>
  15624. friend bool operator>(const_reference lhs, const ScalarType rhs) noexcept
  15625. {
  15626. return (lhs > basic_json(rhs));
  15627. }
  15628. /*!
  15629. @brief comparison: greater than
  15630. @copydoc operator>(const_reference, const_reference)
  15631. */
  15632. template<typename ScalarType, typename std::enable_if<
  15633. std::is_scalar<ScalarType>::value, int>::type = 0>
  15634. friend bool operator>(const ScalarType lhs, const_reference rhs) noexcept
  15635. {
  15636. return (basic_json(lhs) > rhs);
  15637. }
  15638. /*!
  15639. @brief comparison: greater than or equal
  15640. Compares whether one JSON value @a lhs is greater than or equal to another
  15641. JSON value by calculating `not (lhs < rhs)`.
  15642. @param[in] lhs first JSON value to consider
  15643. @param[in] rhs second JSON value to consider
  15644. @return whether @a lhs is greater than or equal to @a rhs
  15645. @complexity Linear.
  15646. @exceptionsafety No-throw guarantee: this function never throws exceptions.
  15647. @liveexample{The example demonstrates comparing several JSON
  15648. types.,operator__greaterequal}
  15649. @since version 1.0.0
  15650. */
  15651. friend bool operator>=(const_reference lhs, const_reference rhs) noexcept
  15652. {
  15653. return not (lhs < rhs);
  15654. }
  15655. /*!
  15656. @brief comparison: greater than or equal
  15657. @copydoc operator>=(const_reference, const_reference)
  15658. */
  15659. template<typename ScalarType, typename std::enable_if<
  15660. std::is_scalar<ScalarType>::value, int>::type = 0>
  15661. friend bool operator>=(const_reference lhs, const ScalarType rhs) noexcept
  15662. {
  15663. return (lhs >= basic_json(rhs));
  15664. }
  15665. /*!
  15666. @brief comparison: greater than or equal
  15667. @copydoc operator>=(const_reference, const_reference)
  15668. */
  15669. template<typename ScalarType, typename std::enable_if<
  15670. std::is_scalar<ScalarType>::value, int>::type = 0>
  15671. friend bool operator>=(const ScalarType lhs, const_reference rhs) noexcept
  15672. {
  15673. return (basic_json(lhs) >= rhs);
  15674. }
  15675. /// @}
  15676. ///////////////////
  15677. // serialization //
  15678. ///////////////////
  15679. /// @name serialization
  15680. /// @{
  15681. /*!
  15682. @brief serialize to stream
  15683. Serialize the given JSON value @a j to the output stream @a o. The JSON
  15684. value will be serialized using the @ref dump member function.
  15685. - The indentation of the output can be controlled with the member variable
  15686. `width` of the output stream @a o. For instance, using the manipulator
  15687. `std::setw(4)` on @a o sets the indentation level to `4` and the
  15688. serialization result is the same as calling `dump(4)`.
  15689. - The indentation character can be controlled with the member variable
  15690. `fill` of the output stream @a o. For instance, the manipulator
  15691. `std::setfill('\\t')` sets indentation to use a tab character rather than
  15692. the default space character.
  15693. @param[in,out] o stream to serialize to
  15694. @param[in] j JSON value to serialize
  15695. @return the stream @a o
  15696. @throw type_error.316 if a string stored inside the JSON value is not
  15697. UTF-8 encoded
  15698. @complexity Linear.
  15699. @liveexample{The example below shows the serialization with different
  15700. parameters to `width` to adjust the indentation level.,operator_serialize}
  15701. @since version 1.0.0; indentation character added in version 3.0.0
  15702. */
  15703. friend std::ostream& operator<<(std::ostream& o, const basic_json& j)
  15704. {
  15705. // read width member and use it as indentation parameter if nonzero
  15706. const bool pretty_print = (o.width() > 0);
  15707. const auto indentation = (pretty_print ? o.width() : 0);
  15708. // reset width to 0 for subsequent calls to this stream
  15709. o.width(0);
  15710. // do the actual serialization
  15711. serializer s(detail::output_adapter<char>(o), o.fill());
  15712. s.dump(j, pretty_print, false, static_cast<unsigned int>(indentation));
  15713. return o;
  15714. }
  15715. /*!
  15716. @brief serialize to stream
  15717. @deprecated This stream operator is deprecated and will be removed in
  15718. future 4.0.0 of the library. Please use
  15719. @ref operator<<(std::ostream&, const basic_json&)
  15720. instead; that is, replace calls like `j >> o;` with `o << j;`.
  15721. @since version 1.0.0; deprecated since version 3.0.0
  15722. */
  15723. JSON_DEPRECATED
  15724. friend std::ostream& operator>>(const basic_json& j, std::ostream& o)
  15725. {
  15726. return o << j;
  15727. }
  15728. /// @}
  15729. /////////////////////
  15730. // deserialization //
  15731. /////////////////////
  15732. /// @name deserialization
  15733. /// @{
  15734. /*!
  15735. @brief deserialize from a compatible input
  15736. This function reads from a compatible input. Examples are:
  15737. - an array of 1-byte values
  15738. - strings with character/literal type with size of 1 byte
  15739. - input streams
  15740. - container with contiguous storage of 1-byte values. Compatible container
  15741. types include `std::vector`, `std::string`, `std::array`,
  15742. `std::valarray`, and `std::initializer_list`. Furthermore, C-style
  15743. arrays can be used with `std::begin()`/`std::end()`. User-defined
  15744. containers can be used as long as they implement random-access iterators
  15745. and a contiguous storage.
  15746. @pre Each element of the container has a size of 1 byte. Violating this
  15747. precondition yields undefined behavior. **This precondition is enforced
  15748. with a static assertion.**
  15749. @pre The container storage is contiguous. Violating this precondition
  15750. yields undefined behavior. **This precondition is enforced with an
  15751. assertion.**
  15752. @pre Each element of the container has a size of 1 byte. Violating this
  15753. precondition yields undefined behavior. **This precondition is enforced
  15754. with a static assertion.**
  15755. @warning There is no way to enforce all preconditions at compile-time. If
  15756. the function is called with a noncompliant container and with
  15757. assertions switched off, the behavior is undefined and will most
  15758. likely yield segmentation violation.
  15759. @param[in] i input to read from
  15760. @param[in] cb a parser callback function of type @ref parser_callback_t
  15761. which is used to control the deserialization by filtering unwanted values
  15762. (optional)
  15763. @param[in] allow_exceptions whether to throw exceptions in case of a
  15764. parse error (optional, true by default)
  15765. @return result of the deserialization
  15766. @throw parse_error.101 if a parse error occurs; example: `""unexpected end
  15767. of input; expected string literal""`
  15768. @throw parse_error.102 if to_unicode fails or surrogate error
  15769. @throw parse_error.103 if to_unicode fails
  15770. @complexity Linear in the length of the input. The parser is a predictive
  15771. LL(1) parser. The complexity can be higher if the parser callback function
  15772. @a cb has a super-linear complexity.
  15773. @note A UTF-8 byte order mark is silently ignored.
  15774. @liveexample{The example below demonstrates the `parse()` function reading
  15775. from an array.,parse__array__parser_callback_t}
  15776. @liveexample{The example below demonstrates the `parse()` function with
  15777. and without callback function.,parse__string__parser_callback_t}
  15778. @liveexample{The example below demonstrates the `parse()` function with
  15779. and without callback function.,parse__istream__parser_callback_t}
  15780. @liveexample{The example below demonstrates the `parse()` function reading
  15781. from a contiguous container.,parse__contiguouscontainer__parser_callback_t}
  15782. @since version 2.0.3 (contiguous containers)
  15783. */
  15784. static basic_json parse(detail::input_adapter&& i,
  15785. const parser_callback_t cb = nullptr,
  15786. const bool allow_exceptions = true)
  15787. {
  15788. basic_json result;
  15789. parser(i, cb, allow_exceptions).parse(true, result);
  15790. return result;
  15791. }
  15792. static bool accept(detail::input_adapter&& i)
  15793. {
  15794. return parser(i).accept(true);
  15795. }
  15796. /*!
  15797. @brief generate SAX events
  15798. The SAX event lister must follow the interface of @ref json_sax.
  15799. This function reads from a compatible input. Examples are:
  15800. - an array of 1-byte values
  15801. - strings with character/literal type with size of 1 byte
  15802. - input streams
  15803. - container with contiguous storage of 1-byte values. Compatible container
  15804. types include `std::vector`, `std::string`, `std::array`,
  15805. `std::valarray`, and `std::initializer_list`. Furthermore, C-style
  15806. arrays can be used with `std::begin()`/`std::end()`. User-defined
  15807. containers can be used as long as they implement random-access iterators
  15808. and a contiguous storage.
  15809. @pre Each element of the container has a size of 1 byte. Violating this
  15810. precondition yields undefined behavior. **This precondition is enforced
  15811. with a static assertion.**
  15812. @pre The container storage is contiguous. Violating this precondition
  15813. yields undefined behavior. **This precondition is enforced with an
  15814. assertion.**
  15815. @pre Each element of the container has a size of 1 byte. Violating this
  15816. precondition yields undefined behavior. **This precondition is enforced
  15817. with a static assertion.**
  15818. @warning There is no way to enforce all preconditions at compile-time. If
  15819. the function is called with a noncompliant container and with
  15820. assertions switched off, the behavior is undefined and will most
  15821. likely yield segmentation violation.
  15822. @param[in] i input to read from
  15823. @param[in,out] sax SAX event listener
  15824. @param[in] format the format to parse (JSON, CBOR, MessagePack, or UBJSON)
  15825. @param[in] strict whether the input has to be consumed completely
  15826. @return return value of the last processed SAX event
  15827. @throw parse_error.101 if a parse error occurs; example: `""unexpected end
  15828. of input; expected string literal""`
  15829. @throw parse_error.102 if to_unicode fails or surrogate error
  15830. @throw parse_error.103 if to_unicode fails
  15831. @complexity Linear in the length of the input. The parser is a predictive
  15832. LL(1) parser. The complexity can be higher if the SAX consumer @a sax has
  15833. a super-linear complexity.
  15834. @note A UTF-8 byte order mark is silently ignored.
  15835. @liveexample{The example below demonstrates the `sax_parse()` function
  15836. reading from string and processing the events with a user-defined SAX
  15837. event consumer.,sax_parse}
  15838. @since version 3.2.0
  15839. */
  15840. template <typename SAX>
  15841. static bool sax_parse(detail::input_adapter&& i, SAX* sax,
  15842. input_format_t format = input_format_t::json,
  15843. const bool strict = true)
  15844. {
  15845. assert(sax);
  15846. switch (format)
  15847. {
  15848. case input_format_t::json:
  15849. return parser(std::move(i)).sax_parse(sax, strict);
  15850. default:
  15851. return detail::binary_reader<basic_json, SAX>(std::move(i)).sax_parse(format, sax, strict);
  15852. }
  15853. }
  15854. /*!
  15855. @brief deserialize from an iterator range with contiguous storage
  15856. This function reads from an iterator range of a container with contiguous
  15857. storage of 1-byte values. Compatible container types include
  15858. `std::vector`, `std::string`, `std::array`, `std::valarray`, and
  15859. `std::initializer_list`. Furthermore, C-style arrays can be used with
  15860. `std::begin()`/`std::end()`. User-defined containers can be used as long
  15861. as they implement random-access iterators and a contiguous storage.
  15862. @pre The iterator range is contiguous. Violating this precondition yields
  15863. undefined behavior. **This precondition is enforced with an assertion.**
  15864. @pre Each element in the range has a size of 1 byte. Violating this
  15865. precondition yields undefined behavior. **This precondition is enforced
  15866. with a static assertion.**
  15867. @warning There is no way to enforce all preconditions at compile-time. If
  15868. the function is called with noncompliant iterators and with
  15869. assertions switched off, the behavior is undefined and will most
  15870. likely yield segmentation violation.
  15871. @tparam IteratorType iterator of container with contiguous storage
  15872. @param[in] first begin of the range to parse (included)
  15873. @param[in] last end of the range to parse (excluded)
  15874. @param[in] cb a parser callback function of type @ref parser_callback_t
  15875. which is used to control the deserialization by filtering unwanted values
  15876. (optional)
  15877. @param[in] allow_exceptions whether to throw exceptions in case of a
  15878. parse error (optional, true by default)
  15879. @return result of the deserialization
  15880. @throw parse_error.101 in case of an unexpected token
  15881. @throw parse_error.102 if to_unicode fails or surrogate error
  15882. @throw parse_error.103 if to_unicode fails
  15883. @complexity Linear in the length of the input. The parser is a predictive
  15884. LL(1) parser. The complexity can be higher if the parser callback function
  15885. @a cb has a super-linear complexity.
  15886. @note A UTF-8 byte order mark is silently ignored.
  15887. @liveexample{The example below demonstrates the `parse()` function reading
  15888. from an iterator range.,parse__iteratortype__parser_callback_t}
  15889. @since version 2.0.3
  15890. */
  15891. template<class IteratorType, typename std::enable_if<
  15892. std::is_base_of<
  15893. std::random_access_iterator_tag,
  15894. typename std::iterator_traits<IteratorType>::iterator_category>::value, int>::type = 0>
  15895. static basic_json parse(IteratorType first, IteratorType last,
  15896. const parser_callback_t cb = nullptr,
  15897. const bool allow_exceptions = true)
  15898. {
  15899. basic_json result;
  15900. parser(detail::input_adapter(first, last), cb, allow_exceptions).parse(true, result);
  15901. return result;
  15902. }
  15903. template<class IteratorType, typename std::enable_if<
  15904. std::is_base_of<
  15905. std::random_access_iterator_tag,
  15906. typename std::iterator_traits<IteratorType>::iterator_category>::value, int>::type = 0>
  15907. static bool accept(IteratorType first, IteratorType last)
  15908. {
  15909. return parser(detail::input_adapter(first, last)).accept(true);
  15910. }
  15911. template<class IteratorType, class SAX, typename std::enable_if<
  15912. std::is_base_of<
  15913. std::random_access_iterator_tag,
  15914. typename std::iterator_traits<IteratorType>::iterator_category>::value, int>::type = 0>
  15915. static bool sax_parse(IteratorType first, IteratorType last, SAX* sax)
  15916. {
  15917. return parser(detail::input_adapter(first, last)).sax_parse(sax);
  15918. }
  15919. /*!
  15920. @brief deserialize from stream
  15921. @deprecated This stream operator is deprecated and will be removed in
  15922. version 4.0.0 of the library. Please use
  15923. @ref operator>>(std::istream&, basic_json&)
  15924. instead; that is, replace calls like `j << i;` with `i >> j;`.
  15925. @since version 1.0.0; deprecated since version 3.0.0
  15926. */
  15927. JSON_DEPRECATED
  15928. friend std::istream& operator<<(basic_json& j, std::istream& i)
  15929. {
  15930. return operator>>(i, j);
  15931. }
  15932. /*!
  15933. @brief deserialize from stream
  15934. Deserializes an input stream to a JSON value.
  15935. @param[in,out] i input stream to read a serialized JSON value from
  15936. @param[in,out] j JSON value to write the deserialized input to
  15937. @throw parse_error.101 in case of an unexpected token
  15938. @throw parse_error.102 if to_unicode fails or surrogate error
  15939. @throw parse_error.103 if to_unicode fails
  15940. @complexity Linear in the length of the input. The parser is a predictive
  15941. LL(1) parser.
  15942. @note A UTF-8 byte order mark is silently ignored.
  15943. @liveexample{The example below shows how a JSON value is constructed by
  15944. reading a serialization from a stream.,operator_deserialize}
  15945. @sa parse(std::istream&, const parser_callback_t) for a variant with a
  15946. parser callback function to filter values while parsing
  15947. @since version 1.0.0
  15948. */
  15949. friend std::istream& operator>>(std::istream& i, basic_json& j)
  15950. {
  15951. parser(detail::input_adapter(i)).parse(false, j);
  15952. return i;
  15953. }
  15954. /// @}
  15955. ///////////////////////////
  15956. // convenience functions //
  15957. ///////////////////////////
  15958. /*!
  15959. @brief return the type as string
  15960. Returns the type name as string to be used in error messages - usually to
  15961. indicate that a function was called on a wrong JSON type.
  15962. @return a string representation of a the @a m_type member:
  15963. Value type | return value
  15964. ----------- | -------------
  15965. null | `"null"`
  15966. boolean | `"boolean"`
  15967. string | `"string"`
  15968. number | `"number"` (for all number types)
  15969. object | `"object"`
  15970. array | `"array"`
  15971. discarded | `"discarded"`
  15972. @exceptionsafety No-throw guarantee: this function never throws exceptions.
  15973. @complexity Constant.
  15974. @liveexample{The following code exemplifies `type_name()` for all JSON
  15975. types.,type_name}
  15976. @sa @ref type() -- return the type of the JSON value
  15977. @sa @ref operator value_t() -- return the type of the JSON value (implicit)
  15978. @since version 1.0.0, public since 2.1.0, `const char*` and `noexcept`
  15979. since 3.0.0
  15980. */
  15981. const char* type_name() const noexcept
  15982. {
  15983. {
  15984. switch (m_type)
  15985. {
  15986. case value_t::null:
  15987. return "null";
  15988. case value_t::object:
  15989. return "object";
  15990. case value_t::array:
  15991. return "array";
  15992. case value_t::string:
  15993. return "string";
  15994. case value_t::boolean:
  15995. return "boolean";
  15996. case value_t::discarded:
  15997. return "discarded";
  15998. default:
  15999. return "number";
  16000. }
  16001. }
  16002. }
  16003. private:
  16004. //////////////////////
  16005. // member variables //
  16006. //////////////////////
  16007. /// the type of the current element
  16008. value_t m_type = value_t::null;
  16009. /// the value of the current element
  16010. json_value m_value = {};
  16011. //////////////////////////////////////////
  16012. // binary serialization/deserialization //
  16013. //////////////////////////////////////////
  16014. /// @name binary serialization/deserialization support
  16015. /// @{
  16016. public:
  16017. /*!
  16018. @brief create a CBOR serialization of a given JSON value
  16019. Serializes a given JSON value @a j to a byte vector using the CBOR (Concise
  16020. Binary Object Representation) serialization format. CBOR is a binary
  16021. serialization format which aims to be more compact than JSON itself, yet
  16022. more efficient to parse.
  16023. The library uses the following mapping from JSON values types to
  16024. CBOR types according to the CBOR specification (RFC 7049):
  16025. JSON value type | value/range | CBOR type | first byte
  16026. --------------- | ------------------------------------------ | ---------------------------------- | ---------------
  16027. null | `null` | Null | 0xF6
  16028. boolean | `true` | True | 0xF5
  16029. boolean | `false` | False | 0xF4
  16030. number_integer | -9223372036854775808..-2147483649 | Negative integer (8 bytes follow) | 0x3B
  16031. number_integer | -2147483648..-32769 | Negative integer (4 bytes follow) | 0x3A
  16032. number_integer | -32768..-129 | Negative integer (2 bytes follow) | 0x39
  16033. number_integer | -128..-25 | Negative integer (1 byte follow) | 0x38
  16034. number_integer | -24..-1 | Negative integer | 0x20..0x37
  16035. number_integer | 0..23 | Integer | 0x00..0x17
  16036. number_integer | 24..255 | Unsigned integer (1 byte follow) | 0x18
  16037. number_integer | 256..65535 | Unsigned integer (2 bytes follow) | 0x19
  16038. number_integer | 65536..4294967295 | Unsigned integer (4 bytes follow) | 0x1A
  16039. number_integer | 4294967296..18446744073709551615 | Unsigned integer (8 bytes follow) | 0x1B
  16040. number_unsigned | 0..23 | Integer | 0x00..0x17
  16041. number_unsigned | 24..255 | Unsigned integer (1 byte follow) | 0x18
  16042. number_unsigned | 256..65535 | Unsigned integer (2 bytes follow) | 0x19
  16043. number_unsigned | 65536..4294967295 | Unsigned integer (4 bytes follow) | 0x1A
  16044. number_unsigned | 4294967296..18446744073709551615 | Unsigned integer (8 bytes follow) | 0x1B
  16045. number_float | *any value* | Double-Precision Float | 0xFB
  16046. string | *length*: 0..23 | UTF-8 string | 0x60..0x77
  16047. string | *length*: 23..255 | UTF-8 string (1 byte follow) | 0x78
  16048. string | *length*: 256..65535 | UTF-8 string (2 bytes follow) | 0x79
  16049. string | *length*: 65536..4294967295 | UTF-8 string (4 bytes follow) | 0x7A
  16050. string | *length*: 4294967296..18446744073709551615 | UTF-8 string (8 bytes follow) | 0x7B
  16051. array | *size*: 0..23 | array | 0x80..0x97
  16052. array | *size*: 23..255 | array (1 byte follow) | 0x98
  16053. array | *size*: 256..65535 | array (2 bytes follow) | 0x99
  16054. array | *size*: 65536..4294967295 | array (4 bytes follow) | 0x9A
  16055. array | *size*: 4294967296..18446744073709551615 | array (8 bytes follow) | 0x9B
  16056. object | *size*: 0..23 | map | 0xA0..0xB7
  16057. object | *size*: 23..255 | map (1 byte follow) | 0xB8
  16058. object | *size*: 256..65535 | map (2 bytes follow) | 0xB9
  16059. object | *size*: 65536..4294967295 | map (4 bytes follow) | 0xBA
  16060. object | *size*: 4294967296..18446744073709551615 | map (8 bytes follow) | 0xBB
  16061. @note The mapping is **complete** in the sense that any JSON value type
  16062. can be converted to a CBOR value.
  16063. @note If NaN or Infinity are stored inside a JSON number, they are
  16064. serialized properly. This behavior differs from the @ref dump()
  16065. function which serializes NaN or Infinity to `null`.
  16066. @note The following CBOR types are not used in the conversion:
  16067. - byte strings (0x40..0x5F)
  16068. - UTF-8 strings terminated by "break" (0x7F)
  16069. - arrays terminated by "break" (0x9F)
  16070. - maps terminated by "break" (0xBF)
  16071. - date/time (0xC0..0xC1)
  16072. - bignum (0xC2..0xC3)
  16073. - decimal fraction (0xC4)
  16074. - bigfloat (0xC5)
  16075. - tagged items (0xC6..0xD4, 0xD8..0xDB)
  16076. - expected conversions (0xD5..0xD7)
  16077. - simple values (0xE0..0xF3, 0xF8)
  16078. - undefined (0xF7)
  16079. - half and single-precision floats (0xF9-0xFA)
  16080. - break (0xFF)
  16081. @param[in] j JSON value to serialize
  16082. @return MessagePack serialization as byte vector
  16083. @complexity Linear in the size of the JSON value @a j.
  16084. @liveexample{The example shows the serialization of a JSON value to a byte
  16085. vector in CBOR format.,to_cbor}
  16086. @sa http://cbor.io
  16087. @sa @ref from_cbor(detail::input_adapter&&, const bool, const bool) for the
  16088. analogous deserialization
  16089. @sa @ref to_msgpack(const basic_json&) for the related MessagePack format
  16090. @sa @ref to_ubjson(const basic_json&, const bool, const bool) for the
  16091. related UBJSON format
  16092. @since version 2.0.9
  16093. */
  16094. static std::vector<uint8_t> to_cbor(const basic_json& j)
  16095. {
  16096. std::vector<uint8_t> result;
  16097. to_cbor(j, result);
  16098. return result;
  16099. }
  16100. static void to_cbor(const basic_json& j, detail::output_adapter<uint8_t> o)
  16101. {
  16102. binary_writer<uint8_t>(o).write_cbor(j);
  16103. }
  16104. static void to_cbor(const basic_json& j, detail::output_adapter<char> o)
  16105. {
  16106. binary_writer<char>(o).write_cbor(j);
  16107. }
  16108. /*!
  16109. @brief create a MessagePack serialization of a given JSON value
  16110. Serializes a given JSON value @a j to a byte vector using the MessagePack
  16111. serialization format. MessagePack is a binary serialization format which
  16112. aims to be more compact than JSON itself, yet more efficient to parse.
  16113. The library uses the following mapping from JSON values types to
  16114. MessagePack types according to the MessagePack specification:
  16115. JSON value type | value/range | MessagePack type | first byte
  16116. --------------- | --------------------------------- | ---------------- | ----------
  16117. null | `null` | nil | 0xC0
  16118. boolean | `true` | true | 0xC3
  16119. boolean | `false` | false | 0xC2
  16120. number_integer | -9223372036854775808..-2147483649 | int64 | 0xD3
  16121. number_integer | -2147483648..-32769 | int32 | 0xD2
  16122. number_integer | -32768..-129 | int16 | 0xD1
  16123. number_integer | -128..-33 | int8 | 0xD0
  16124. number_integer | -32..-1 | negative fixint | 0xE0..0xFF
  16125. number_integer | 0..127 | positive fixint | 0x00..0x7F
  16126. number_integer | 128..255 | uint 8 | 0xCC
  16127. number_integer | 256..65535 | uint 16 | 0xCD
  16128. number_integer | 65536..4294967295 | uint 32 | 0xCE
  16129. number_integer | 4294967296..18446744073709551615 | uint 64 | 0xCF
  16130. number_unsigned | 0..127 | positive fixint | 0x00..0x7F
  16131. number_unsigned | 128..255 | uint 8 | 0xCC
  16132. number_unsigned | 256..65535 | uint 16 | 0xCD
  16133. number_unsigned | 65536..4294967295 | uint 32 | 0xCE
  16134. number_unsigned | 4294967296..18446744073709551615 | uint 64 | 0xCF
  16135. number_float | *any value* | float 64 | 0xCB
  16136. string | *length*: 0..31 | fixstr | 0xA0..0xBF
  16137. string | *length*: 32..255 | str 8 | 0xD9
  16138. string | *length*: 256..65535 | str 16 | 0xDA
  16139. string | *length*: 65536..4294967295 | str 32 | 0xDB
  16140. array | *size*: 0..15 | fixarray | 0x90..0x9F
  16141. array | *size*: 16..65535 | array 16 | 0xDC
  16142. array | *size*: 65536..4294967295 | array 32 | 0xDD
  16143. object | *size*: 0..15 | fix map | 0x80..0x8F
  16144. object | *size*: 16..65535 | map 16 | 0xDE
  16145. object | *size*: 65536..4294967295 | map 32 | 0xDF
  16146. @note The mapping is **complete** in the sense that any JSON value type
  16147. can be converted to a MessagePack value.
  16148. @note The following values can **not** be converted to a MessagePack value:
  16149. - strings with more than 4294967295 bytes
  16150. - arrays with more than 4294967295 elements
  16151. - objects with more than 4294967295 elements
  16152. @note The following MessagePack types are not used in the conversion:
  16153. - bin 8 - bin 32 (0xC4..0xC6)
  16154. - ext 8 - ext 32 (0xC7..0xC9)
  16155. - float 32 (0xCA)
  16156. - fixext 1 - fixext 16 (0xD4..0xD8)
  16157. @note Any MessagePack output created @ref to_msgpack can be successfully
  16158. parsed by @ref from_msgpack.
  16159. @note If NaN or Infinity are stored inside a JSON number, they are
  16160. serialized properly. This behavior differs from the @ref dump()
  16161. function which serializes NaN or Infinity to `null`.
  16162. @param[in] j JSON value to serialize
  16163. @return MessagePack serialization as byte vector
  16164. @complexity Linear in the size of the JSON value @a j.
  16165. @liveexample{The example shows the serialization of a JSON value to a byte
  16166. vector in MessagePack format.,to_msgpack}
  16167. @sa http://msgpack.org
  16168. @sa @ref from_msgpack for the analogous deserialization
  16169. @sa @ref to_cbor(const basic_json& for the related CBOR format
  16170. @sa @ref to_ubjson(const basic_json&, const bool, const bool) for the
  16171. related UBJSON format
  16172. @since version 2.0.9
  16173. */
  16174. static std::vector<uint8_t> to_msgpack(const basic_json& j)
  16175. {
  16176. std::vector<uint8_t> result;
  16177. to_msgpack(j, result);
  16178. return result;
  16179. }
  16180. static void to_msgpack(const basic_json& j, detail::output_adapter<uint8_t> o)
  16181. {
  16182. binary_writer<uint8_t>(o).write_msgpack(j);
  16183. }
  16184. static void to_msgpack(const basic_json& j, detail::output_adapter<char> o)
  16185. {
  16186. binary_writer<char>(o).write_msgpack(j);
  16187. }
  16188. /*!
  16189. @brief create a UBJSON serialization of a given JSON value
  16190. Serializes a given JSON value @a j to a byte vector using the UBJSON
  16191. (Universal Binary JSON) serialization format. UBJSON aims to be more compact
  16192. than JSON itself, yet more efficient to parse.
  16193. The library uses the following mapping from JSON values types to
  16194. UBJSON types according to the UBJSON specification:
  16195. JSON value type | value/range | UBJSON type | marker
  16196. --------------- | --------------------------------- | ----------- | ------
  16197. null | `null` | null | `Z`
  16198. boolean | `true` | true | `T`
  16199. boolean | `false` | false | `F`
  16200. number_integer | -9223372036854775808..-2147483649 | int64 | `L`
  16201. number_integer | -2147483648..-32769 | int32 | `l`
  16202. number_integer | -32768..-129 | int16 | `I`
  16203. number_integer | -128..127 | int8 | `i`
  16204. number_integer | 128..255 | uint8 | `U`
  16205. number_integer | 256..32767 | int16 | `I`
  16206. number_integer | 32768..2147483647 | int32 | `l`
  16207. number_integer | 2147483648..9223372036854775807 | int64 | `L`
  16208. number_unsigned | 0..127 | int8 | `i`
  16209. number_unsigned | 128..255 | uint8 | `U`
  16210. number_unsigned | 256..32767 | int16 | `I`
  16211. number_unsigned | 32768..2147483647 | int32 | `l`
  16212. number_unsigned | 2147483648..9223372036854775807 | int64 | `L`
  16213. number_float | *any value* | float64 | `D`
  16214. string | *with shortest length indicator* | string | `S`
  16215. array | *see notes on optimized format* | array | `[`
  16216. object | *see notes on optimized format* | map | `{`
  16217. @note The mapping is **complete** in the sense that any JSON value type
  16218. can be converted to a UBJSON value.
  16219. @note The following values can **not** be converted to a UBJSON value:
  16220. - strings with more than 9223372036854775807 bytes (theoretical)
  16221. - unsigned integer numbers above 9223372036854775807
  16222. @note The following markers are not used in the conversion:
  16223. - `Z`: no-op values are not created.
  16224. - `C`: single-byte strings are serialized with `S` markers.
  16225. @note Any UBJSON output created @ref to_ubjson can be successfully parsed
  16226. by @ref from_ubjson.
  16227. @note If NaN or Infinity are stored inside a JSON number, they are
  16228. serialized properly. This behavior differs from the @ref dump()
  16229. function which serializes NaN or Infinity to `null`.
  16230. @note The optimized formats for containers are supported: Parameter
  16231. @a use_size adds size information to the beginning of a container and
  16232. removes the closing marker. Parameter @a use_type further checks
  16233. whether all elements of a container have the same type and adds the
  16234. type marker to the beginning of the container. The @a use_type
  16235. parameter must only be used together with @a use_size = true. Note
  16236. that @a use_size = true alone may result in larger representations -
  16237. the benefit of this parameter is that the receiving side is
  16238. immediately informed on the number of elements of the container.
  16239. @param[in] j JSON value to serialize
  16240. @param[in] use_size whether to add size annotations to container types
  16241. @param[in] use_type whether to add type annotations to container types
  16242. (must be combined with @a use_size = true)
  16243. @return UBJSON serialization as byte vector
  16244. @complexity Linear in the size of the JSON value @a j.
  16245. @liveexample{The example shows the serialization of a JSON value to a byte
  16246. vector in UBJSON format.,to_ubjson}
  16247. @sa http://ubjson.org
  16248. @sa @ref from_ubjson(detail::input_adapter&&, const bool, const bool) for the
  16249. analogous deserialization
  16250. @sa @ref to_cbor(const basic_json& for the related CBOR format
  16251. @sa @ref to_msgpack(const basic_json&) for the related MessagePack format
  16252. @since version 3.1.0
  16253. */
  16254. static std::vector<uint8_t> to_ubjson(const basic_json& j,
  16255. const bool use_size = false,
  16256. const bool use_type = false)
  16257. {
  16258. std::vector<uint8_t> result;
  16259. to_ubjson(j, result, use_size, use_type);
  16260. return result;
  16261. }
  16262. static void to_ubjson(const basic_json& j, detail::output_adapter<uint8_t> o,
  16263. const bool use_size = false, const bool use_type = false)
  16264. {
  16265. binary_writer<uint8_t>(o).write_ubjson(j, use_size, use_type);
  16266. }
  16267. static void to_ubjson(const basic_json& j, detail::output_adapter<char> o,
  16268. const bool use_size = false, const bool use_type = false)
  16269. {
  16270. binary_writer<char>(o).write_ubjson(j, use_size, use_type);
  16271. }
  16272. /*!
  16273. @brief Serializes the given JSON object `j` to BSON and returns a vector
  16274. containing the corresponding BSON-representation.
  16275. BSON (Binary JSON) is a binary format in which zero or more ordered key/value pairs are
  16276. stored as a single entity (a so-called document).
  16277. The library uses the following mapping from JSON values types to BSON types:
  16278. JSON value type | value/range | BSON type | marker
  16279. --------------- | --------------------------------- | ----------- | ------
  16280. null | `null` | null | 0x0A
  16281. boolean | `true`, `false` | boolean | 0x08
  16282. number_integer | -9223372036854775808..-2147483649 | int64 | 0x12
  16283. number_integer | -2147483648..2147483647 | int32 | 0x10
  16284. number_integer | 2147483648..9223372036854775807 | int64 | 0x12
  16285. number_unsigned | 0..2147483647 | int32 | 0x10
  16286. number_unsigned | 2147483648..9223372036854775807 | int64 | 0x12
  16287. number_unsigned | 9223372036854775808..18446744073709551615| -- | --
  16288. number_float | *any value* | double | 0x01
  16289. string | *any value* | string | 0x02
  16290. array | *any value* | document | 0x04
  16291. object | *any value* | document | 0x03
  16292. @warning The mapping is **incomplete**, since only JSON-objects (and things
  16293. contained therein) can be serialized to BSON.
  16294. Also, integers larger than 9223372036854775807 cannot be serialized to BSON,
  16295. and the keys may not contain U+0000, since they are serialized a
  16296. zero-terminated c-strings.
  16297. @throw out_of_range.407 if `j.is_number_unsigned() && j.get<std::uint64_t>() > 9223372036854775807`
  16298. @throw out_of_range.409 if a key in `j` contains a NULL (U+0000)
  16299. @throw type_error.317 if `!j.is_object()`
  16300. @pre The input `j` is required to be an object: `j.is_object() == true`.
  16301. @note Any BSON output created via @ref to_bson can be successfully parsed
  16302. by @ref from_bson.
  16303. @param[in] j JSON value to serialize
  16304. @return BSON serialization as byte vector
  16305. @complexity Linear in the size of the JSON value @a j.
  16306. @liveexample{The example shows the serialization of a JSON value to a byte
  16307. vector in BSON format.,to_bson}
  16308. @sa http://bsonspec.org/spec.html
  16309. @sa @ref from_bson(detail::input_adapter&&, const bool strict) for the
  16310. analogous deserialization
  16311. @sa @ref to_ubjson(const basic_json&, const bool, const bool) for the
  16312. related UBJSON format
  16313. @sa @ref to_cbor(const basic_json&) for the related CBOR format
  16314. @sa @ref to_msgpack(const basic_json&) for the related MessagePack format
  16315. */
  16316. static std::vector<uint8_t> to_bson(const basic_json& j)
  16317. {
  16318. std::vector<uint8_t> result;
  16319. to_bson(j, result);
  16320. return result;
  16321. }
  16322. /*!
  16323. @brief Serializes the given JSON object `j` to BSON and forwards the
  16324. corresponding BSON-representation to the given output_adapter `o`.
  16325. @param j The JSON object to convert to BSON.
  16326. @param o The output adapter that receives the binary BSON representation.
  16327. @pre The input `j` shall be an object: `j.is_object() == true`
  16328. @sa @ref to_bson(const basic_json&)
  16329. */
  16330. static void to_bson(const basic_json& j, detail::output_adapter<uint8_t> o)
  16331. {
  16332. binary_writer<uint8_t>(o).write_bson(j);
  16333. }
  16334. /*!
  16335. @copydoc to_bson(const basic_json&, detail::output_adapter<uint8_t>)
  16336. */
  16337. static void to_bson(const basic_json& j, detail::output_adapter<char> o)
  16338. {
  16339. binary_writer<char>(o).write_bson(j);
  16340. }
  16341. /*!
  16342. @brief create a JSON value from an input in CBOR format
  16343. Deserializes a given input @a i to a JSON value using the CBOR (Concise
  16344. Binary Object Representation) serialization format.
  16345. The library maps CBOR types to JSON value types as follows:
  16346. CBOR type | JSON value type | first byte
  16347. ---------------------- | --------------- | ----------
  16348. Integer | number_unsigned | 0x00..0x17
  16349. Unsigned integer | number_unsigned | 0x18
  16350. Unsigned integer | number_unsigned | 0x19
  16351. Unsigned integer | number_unsigned | 0x1A
  16352. Unsigned integer | number_unsigned | 0x1B
  16353. Negative integer | number_integer | 0x20..0x37
  16354. Negative integer | number_integer | 0x38
  16355. Negative integer | number_integer | 0x39
  16356. Negative integer | number_integer | 0x3A
  16357. Negative integer | number_integer | 0x3B
  16358. Negative integer | number_integer | 0x40..0x57
  16359. UTF-8 string | string | 0x60..0x77
  16360. UTF-8 string | string | 0x78
  16361. UTF-8 string | string | 0x79
  16362. UTF-8 string | string | 0x7A
  16363. UTF-8 string | string | 0x7B
  16364. UTF-8 string | string | 0x7F
  16365. array | array | 0x80..0x97
  16366. array | array | 0x98
  16367. array | array | 0x99
  16368. array | array | 0x9A
  16369. array | array | 0x9B
  16370. array | array | 0x9F
  16371. map | object | 0xA0..0xB7
  16372. map | object | 0xB8
  16373. map | object | 0xB9
  16374. map | object | 0xBA
  16375. map | object | 0xBB
  16376. map | object | 0xBF
  16377. False | `false` | 0xF4
  16378. True | `true` | 0xF5
  16379. Null | `null` | 0xF6
  16380. Half-Precision Float | number_float | 0xF9
  16381. Single-Precision Float | number_float | 0xFA
  16382. Double-Precision Float | number_float | 0xFB
  16383. @warning The mapping is **incomplete** in the sense that not all CBOR
  16384. types can be converted to a JSON value. The following CBOR types
  16385. are not supported and will yield parse errors (parse_error.112):
  16386. - byte strings (0x40..0x5F)
  16387. - date/time (0xC0..0xC1)
  16388. - bignum (0xC2..0xC3)
  16389. - decimal fraction (0xC4)
  16390. - bigfloat (0xC5)
  16391. - tagged items (0xC6..0xD4, 0xD8..0xDB)
  16392. - expected conversions (0xD5..0xD7)
  16393. - simple values (0xE0..0xF3, 0xF8)
  16394. - undefined (0xF7)
  16395. @warning CBOR allows map keys of any type, whereas JSON only allows
  16396. strings as keys in object values. Therefore, CBOR maps with keys
  16397. other than UTF-8 strings are rejected (parse_error.113).
  16398. @note Any CBOR output created @ref to_cbor can be successfully parsed by
  16399. @ref from_cbor.
  16400. @param[in] i an input in CBOR format convertible to an input adapter
  16401. @param[in] strict whether to expect the input to be consumed until EOF
  16402. (true by default)
  16403. @param[in] allow_exceptions whether to throw exceptions in case of a
  16404. parse error (optional, true by default)
  16405. @return deserialized JSON value
  16406. @throw parse_error.110 if the given input ends prematurely or the end of
  16407. file was not reached when @a strict was set to true
  16408. @throw parse_error.112 if unsupported features from CBOR were
  16409. used in the given input @a v or if the input is not valid CBOR
  16410. @throw parse_error.113 if a string was expected as map key, but not found
  16411. @complexity Linear in the size of the input @a i.
  16412. @liveexample{The example shows the deserialization of a byte vector in CBOR
  16413. format to a JSON value.,from_cbor}
  16414. @sa http://cbor.io
  16415. @sa @ref to_cbor(const basic_json&) for the analogous serialization
  16416. @sa @ref from_msgpack(detail::input_adapter&&, const bool, const bool) for the
  16417. related MessagePack format
  16418. @sa @ref from_ubjson(detail::input_adapter&&, const bool, const bool) for the
  16419. related UBJSON format
  16420. @since version 2.0.9; parameter @a start_index since 2.1.1; changed to
  16421. consume input adapters, removed start_index parameter, and added
  16422. @a strict parameter since 3.0.0; added @a allow_exceptions parameter
  16423. since 3.2.0
  16424. */
  16425. static basic_json from_cbor(detail::input_adapter&& i,
  16426. const bool strict = true,
  16427. const bool allow_exceptions = true)
  16428. {
  16429. basic_json result;
  16430. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  16431. const bool res = binary_reader(detail::input_adapter(i)).sax_parse(input_format_t::cbor, &sdp, strict);
  16432. return res ? result : basic_json(value_t::discarded);
  16433. }
  16434. /*!
  16435. @copydoc from_cbor(detail::input_adapter&&, const bool, const bool)
  16436. */
  16437. template<typename A1, typename A2,
  16438. detail::enable_if_t<std::is_constructible<detail::input_adapter, A1, A2>::value, int> = 0>
  16439. static basic_json from_cbor(A1 && a1, A2 && a2,
  16440. const bool strict = true,
  16441. const bool allow_exceptions = true)
  16442. {
  16443. basic_json result;
  16444. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  16445. const bool res = binary_reader(detail::input_adapter(std::forward<A1>(a1), std::forward<A2>(a2))).sax_parse(input_format_t::cbor, &sdp, strict);
  16446. return res ? result : basic_json(value_t::discarded);
  16447. }
  16448. /*!
  16449. @brief create a JSON value from an input in MessagePack format
  16450. Deserializes a given input @a i to a JSON value using the MessagePack
  16451. serialization format.
  16452. The library maps MessagePack types to JSON value types as follows:
  16453. MessagePack type | JSON value type | first byte
  16454. ---------------- | --------------- | ----------
  16455. positive fixint | number_unsigned | 0x00..0x7F
  16456. fixmap | object | 0x80..0x8F
  16457. fixarray | array | 0x90..0x9F
  16458. fixstr | string | 0xA0..0xBF
  16459. nil | `null` | 0xC0
  16460. false | `false` | 0xC2
  16461. true | `true` | 0xC3
  16462. float 32 | number_float | 0xCA
  16463. float 64 | number_float | 0xCB
  16464. uint 8 | number_unsigned | 0xCC
  16465. uint 16 | number_unsigned | 0xCD
  16466. uint 32 | number_unsigned | 0xCE
  16467. uint 64 | number_unsigned | 0xCF
  16468. int 8 | number_integer | 0xD0
  16469. int 16 | number_integer | 0xD1
  16470. int 32 | number_integer | 0xD2
  16471. int 64 | number_integer | 0xD3
  16472. str 8 | string | 0xD9
  16473. str 16 | string | 0xDA
  16474. str 32 | string | 0xDB
  16475. array 16 | array | 0xDC
  16476. array 32 | array | 0xDD
  16477. map 16 | object | 0xDE
  16478. map 32 | object | 0xDF
  16479. negative fixint | number_integer | 0xE0-0xFF
  16480. @warning The mapping is **incomplete** in the sense that not all
  16481. MessagePack types can be converted to a JSON value. The following
  16482. MessagePack types are not supported and will yield parse errors:
  16483. - bin 8 - bin 32 (0xC4..0xC6)
  16484. - ext 8 - ext 32 (0xC7..0xC9)
  16485. - fixext 1 - fixext 16 (0xD4..0xD8)
  16486. @note Any MessagePack output created @ref to_msgpack can be successfully
  16487. parsed by @ref from_msgpack.
  16488. @param[in] i an input in MessagePack format convertible to an input
  16489. adapter
  16490. @param[in] strict whether to expect the input to be consumed until EOF
  16491. (true by default)
  16492. @param[in] allow_exceptions whether to throw exceptions in case of a
  16493. parse error (optional, true by default)
  16494. @return deserialized JSON value
  16495. @throw parse_error.110 if the given input ends prematurely or the end of
  16496. file was not reached when @a strict was set to true
  16497. @throw parse_error.112 if unsupported features from MessagePack were
  16498. used in the given input @a i or if the input is not valid MessagePack
  16499. @throw parse_error.113 if a string was expected as map key, but not found
  16500. @complexity Linear in the size of the input @a i.
  16501. @liveexample{The example shows the deserialization of a byte vector in
  16502. MessagePack format to a JSON value.,from_msgpack}
  16503. @sa http://msgpack.org
  16504. @sa @ref to_msgpack(const basic_json&) for the analogous serialization
  16505. @sa @ref from_cbor(detail::input_adapter&&, const bool, const bool) for the
  16506. related CBOR format
  16507. @sa @ref from_ubjson(detail::input_adapter&&, const bool, const bool) for
  16508. the related UBJSON format
  16509. @sa @ref from_bson(detail::input_adapter&&, const bool, const bool) for
  16510. the related BSON format
  16511. @since version 2.0.9; parameter @a start_index since 2.1.1; changed to
  16512. consume input adapters, removed start_index parameter, and added
  16513. @a strict parameter since 3.0.0; added @a allow_exceptions parameter
  16514. since 3.2.0
  16515. */
  16516. static basic_json from_msgpack(detail::input_adapter&& i,
  16517. const bool strict = true,
  16518. const bool allow_exceptions = true)
  16519. {
  16520. basic_json result;
  16521. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  16522. const bool res = binary_reader(detail::input_adapter(i)).sax_parse(input_format_t::msgpack, &sdp, strict);
  16523. return res ? result : basic_json(value_t::discarded);
  16524. }
  16525. /*!
  16526. @copydoc from_msgpack(detail::input_adapter&&, const bool, const bool)
  16527. */
  16528. template<typename A1, typename A2,
  16529. detail::enable_if_t<std::is_constructible<detail::input_adapter, A1, A2>::value, int> = 0>
  16530. static basic_json from_msgpack(A1 && a1, A2 && a2,
  16531. const bool strict = true,
  16532. const bool allow_exceptions = true)
  16533. {
  16534. basic_json result;
  16535. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  16536. const bool res = binary_reader(detail::input_adapter(std::forward<A1>(a1), std::forward<A2>(a2))).sax_parse(input_format_t::msgpack, &sdp, strict);
  16537. return res ? result : basic_json(value_t::discarded);
  16538. }
  16539. /*!
  16540. @brief create a JSON value from an input in UBJSON format
  16541. Deserializes a given input @a i to a JSON value using the UBJSON (Universal
  16542. Binary JSON) serialization format.
  16543. The library maps UBJSON types to JSON value types as follows:
  16544. UBJSON type | JSON value type | marker
  16545. ----------- | --------------------------------------- | ------
  16546. no-op | *no value, next value is read* | `N`
  16547. null | `null` | `Z`
  16548. false | `false` | `F`
  16549. true | `true` | `T`
  16550. float32 | number_float | `d`
  16551. float64 | number_float | `D`
  16552. uint8 | number_unsigned | `U`
  16553. int8 | number_integer | `i`
  16554. int16 | number_integer | `I`
  16555. int32 | number_integer | `l`
  16556. int64 | number_integer | `L`
  16557. string | string | `S`
  16558. char | string | `C`
  16559. array | array (optimized values are supported) | `[`
  16560. object | object (optimized values are supported) | `{`
  16561. @note The mapping is **complete** in the sense that any UBJSON value can
  16562. be converted to a JSON value.
  16563. @param[in] i an input in UBJSON format convertible to an input adapter
  16564. @param[in] strict whether to expect the input to be consumed until EOF
  16565. (true by default)
  16566. @param[in] allow_exceptions whether to throw exceptions in case of a
  16567. parse error (optional, true by default)
  16568. @return deserialized JSON value
  16569. @throw parse_error.110 if the given input ends prematurely or the end of
  16570. file was not reached when @a strict was set to true
  16571. @throw parse_error.112 if a parse error occurs
  16572. @throw parse_error.113 if a string could not be parsed successfully
  16573. @complexity Linear in the size of the input @a i.
  16574. @liveexample{The example shows the deserialization of a byte vector in
  16575. UBJSON format to a JSON value.,from_ubjson}
  16576. @sa http://ubjson.org
  16577. @sa @ref to_ubjson(const basic_json&, const bool, const bool) for the
  16578. analogous serialization
  16579. @sa @ref from_cbor(detail::input_adapter&&, const bool, const bool) for the
  16580. related CBOR format
  16581. @sa @ref from_msgpack(detail::input_adapter&&, const bool, const bool) for
  16582. the related MessagePack format
  16583. @sa @ref from_bson(detail::input_adapter&&, const bool, const bool) for
  16584. the related BSON format
  16585. @since version 3.1.0; added @a allow_exceptions parameter since 3.2.0
  16586. */
  16587. static basic_json from_ubjson(detail::input_adapter&& i,
  16588. const bool strict = true,
  16589. const bool allow_exceptions = true)
  16590. {
  16591. basic_json result;
  16592. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  16593. const bool res = binary_reader(detail::input_adapter(i)).sax_parse(input_format_t::ubjson, &sdp, strict);
  16594. return res ? result : basic_json(value_t::discarded);
  16595. }
  16596. /*!
  16597. @copydoc from_ubjson(detail::input_adapter&&, const bool, const bool)
  16598. */
  16599. template<typename A1, typename A2,
  16600. detail::enable_if_t<std::is_constructible<detail::input_adapter, A1, A2>::value, int> = 0>
  16601. static basic_json from_ubjson(A1 && a1, A2 && a2,
  16602. const bool strict = true,
  16603. const bool allow_exceptions = true)
  16604. {
  16605. basic_json result;
  16606. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  16607. const bool res = binary_reader(detail::input_adapter(std::forward<A1>(a1), std::forward<A2>(a2))).sax_parse(input_format_t::ubjson, &sdp, strict);
  16608. return res ? result : basic_json(value_t::discarded);
  16609. }
  16610. /*!
  16611. @brief Create a JSON value from an input in BSON format
  16612. Deserializes a given input @a i to a JSON value using the BSON (Binary JSON)
  16613. serialization format.
  16614. The library maps BSON record types to JSON value types as follows:
  16615. BSON type | BSON marker byte | JSON value type
  16616. --------------- | ---------------- | ---------------------------
  16617. double | 0x01 | number_float
  16618. string | 0x02 | string
  16619. document | 0x03 | object
  16620. array | 0x04 | array
  16621. binary | 0x05 | still unsupported
  16622. undefined | 0x06 | still unsupported
  16623. ObjectId | 0x07 | still unsupported
  16624. boolean | 0x08 | boolean
  16625. UTC Date-Time | 0x09 | still unsupported
  16626. null | 0x0A | null
  16627. Regular Expr. | 0x0B | still unsupported
  16628. DB Pointer | 0x0C | still unsupported
  16629. JavaScript Code | 0x0D | still unsupported
  16630. Symbol | 0x0E | still unsupported
  16631. JavaScript Code | 0x0F | still unsupported
  16632. int32 | 0x10 | number_integer
  16633. Timestamp | 0x11 | still unsupported
  16634. 128-bit decimal float | 0x13 | still unsupported
  16635. Max Key | 0x7F | still unsupported
  16636. Min Key | 0xFF | still unsupported
  16637. @warning The mapping is **incomplete**. The unsupported mappings
  16638. are indicated in the table above.
  16639. @param[in] i an input in BSON format convertible to an input adapter
  16640. @param[in] strict whether to expect the input to be consumed until EOF
  16641. (true by default)
  16642. @param[in] allow_exceptions whether to throw exceptions in case of a
  16643. parse error (optional, true by default)
  16644. @return deserialized JSON value
  16645. @throw parse_error.114 if an unsupported BSON record type is encountered
  16646. @complexity Linear in the size of the input @a i.
  16647. @liveexample{The example shows the deserialization of a byte vector in
  16648. BSON format to a JSON value.,from_bson}
  16649. @sa http://bsonspec.org/spec.html
  16650. @sa @ref to_bson(const basic_json&) for the analogous serialization
  16651. @sa @ref from_cbor(detail::input_adapter&&, const bool, const bool) for the
  16652. related CBOR format
  16653. @sa @ref from_msgpack(detail::input_adapter&&, const bool, const bool) for
  16654. the related MessagePack format
  16655. @sa @ref from_ubjson(detail::input_adapter&&, const bool, const bool) for the
  16656. related UBJSON format
  16657. */
  16658. static basic_json from_bson(detail::input_adapter&& i,
  16659. const bool strict = true,
  16660. const bool allow_exceptions = true)
  16661. {
  16662. basic_json result;
  16663. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  16664. const bool res = binary_reader(detail::input_adapter(i)).sax_parse(input_format_t::bson, &sdp, strict);
  16665. return res ? result : basic_json(value_t::discarded);
  16666. }
  16667. /*!
  16668. @copydoc from_bson(detail::input_adapter&&, const bool, const bool)
  16669. */
  16670. template<typename A1, typename A2,
  16671. detail::enable_if_t<std::is_constructible<detail::input_adapter, A1, A2>::value, int> = 0>
  16672. static basic_json from_bson(A1 && a1, A2 && a2,
  16673. const bool strict = true,
  16674. const bool allow_exceptions = true)
  16675. {
  16676. basic_json result;
  16677. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  16678. const bool res = binary_reader(detail::input_adapter(std::forward<A1>(a1), std::forward<A2>(a2))).sax_parse(input_format_t::bson, &sdp, strict);
  16679. return res ? result : basic_json(value_t::discarded);
  16680. }
  16681. /// @}
  16682. //////////////////////////
  16683. // JSON Pointer support //
  16684. //////////////////////////
  16685. /// @name JSON Pointer functions
  16686. /// @{
  16687. /*!
  16688. @brief access specified element via JSON Pointer
  16689. Uses a JSON pointer to retrieve a reference to the respective JSON value.
  16690. No bound checking is performed. Similar to @ref operator[](const typename
  16691. object_t::key_type&), `null` values are created in arrays and objects if
  16692. necessary.
  16693. In particular:
  16694. - If the JSON pointer points to an object key that does not exist, it
  16695. is created an filled with a `null` value before a reference to it
  16696. is returned.
  16697. - If the JSON pointer points to an array index that does not exist, it
  16698. is created an filled with a `null` value before a reference to it
  16699. is returned. All indices between the current maximum and the given
  16700. index are also filled with `null`.
  16701. - The special value `-` is treated as a synonym for the index past the
  16702. end.
  16703. @param[in] ptr a JSON pointer
  16704. @return reference to the element pointed to by @a ptr
  16705. @complexity Constant.
  16706. @throw parse_error.106 if an array index begins with '0'
  16707. @throw parse_error.109 if an array index was not a number
  16708. @throw out_of_range.404 if the JSON pointer can not be resolved
  16709. @liveexample{The behavior is shown in the example.,operatorjson_pointer}
  16710. @since version 2.0.0
  16711. */
  16712. reference operator[](const json_pointer& ptr)
  16713. {
  16714. return ptr.get_unchecked(this);
  16715. }
  16716. /*!
  16717. @brief access specified element via JSON Pointer
  16718. Uses a JSON pointer to retrieve a reference to the respective JSON value.
  16719. No bound checking is performed. The function does not change the JSON
  16720. value; no `null` values are created. In particular, the the special value
  16721. `-` yields an exception.
  16722. @param[in] ptr JSON pointer to the desired element
  16723. @return const reference to the element pointed to by @a ptr
  16724. @complexity Constant.
  16725. @throw parse_error.106 if an array index begins with '0'
  16726. @throw parse_error.109 if an array index was not a number
  16727. @throw out_of_range.402 if the array index '-' is used
  16728. @throw out_of_range.404 if the JSON pointer can not be resolved
  16729. @liveexample{The behavior is shown in the example.,operatorjson_pointer_const}
  16730. @since version 2.0.0
  16731. */
  16732. const_reference operator[](const json_pointer& ptr) const
  16733. {
  16734. return ptr.get_unchecked(this);
  16735. }
  16736. /*!
  16737. @brief access specified element via JSON Pointer
  16738. Returns a reference to the element at with specified JSON pointer @a ptr,
  16739. with bounds checking.
  16740. @param[in] ptr JSON pointer to the desired element
  16741. @return reference to the element pointed to by @a ptr
  16742. @throw parse_error.106 if an array index in the passed JSON pointer @a ptr
  16743. begins with '0'. See example below.
  16744. @throw parse_error.109 if an array index in the passed JSON pointer @a ptr
  16745. is not a number. See example below.
  16746. @throw out_of_range.401 if an array index in the passed JSON pointer @a ptr
  16747. is out of range. See example below.
  16748. @throw out_of_range.402 if the array index '-' is used in the passed JSON
  16749. pointer @a ptr. As `at` provides checked access (and no elements are
  16750. implicitly inserted), the index '-' is always invalid. See example below.
  16751. @throw out_of_range.403 if the JSON pointer describes a key of an object
  16752. which cannot be found. See example below.
  16753. @throw out_of_range.404 if the JSON pointer @a ptr can not be resolved.
  16754. See example below.
  16755. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  16756. changes in the JSON value.
  16757. @complexity Constant.
  16758. @since version 2.0.0
  16759. @liveexample{The behavior is shown in the example.,at_json_pointer}
  16760. */
  16761. reference at(const json_pointer& ptr)
  16762. {
  16763. return ptr.get_checked(this);
  16764. }
  16765. /*!
  16766. @brief access specified element via JSON Pointer
  16767. Returns a const reference to the element at with specified JSON pointer @a
  16768. ptr, with bounds checking.
  16769. @param[in] ptr JSON pointer to the desired element
  16770. @return reference to the element pointed to by @a ptr
  16771. @throw parse_error.106 if an array index in the passed JSON pointer @a ptr
  16772. begins with '0'. See example below.
  16773. @throw parse_error.109 if an array index in the passed JSON pointer @a ptr
  16774. is not a number. See example below.
  16775. @throw out_of_range.401 if an array index in the passed JSON pointer @a ptr
  16776. is out of range. See example below.
  16777. @throw out_of_range.402 if the array index '-' is used in the passed JSON
  16778. pointer @a ptr. As `at` provides checked access (and no elements are
  16779. implicitly inserted), the index '-' is always invalid. See example below.
  16780. @throw out_of_range.403 if the JSON pointer describes a key of an object
  16781. which cannot be found. See example below.
  16782. @throw out_of_range.404 if the JSON pointer @a ptr can not be resolved.
  16783. See example below.
  16784. @exceptionsafety Strong guarantee: if an exception is thrown, there are no
  16785. changes in the JSON value.
  16786. @complexity Constant.
  16787. @since version 2.0.0
  16788. @liveexample{The behavior is shown in the example.,at_json_pointer_const}
  16789. */
  16790. const_reference at(const json_pointer& ptr) const
  16791. {
  16792. return ptr.get_checked(this);
  16793. }
  16794. /*!
  16795. @brief return flattened JSON value
  16796. The function creates a JSON object whose keys are JSON pointers (see [RFC
  16797. 6901](https://tools.ietf.org/html/rfc6901)) and whose values are all
  16798. primitive. The original JSON value can be restored using the @ref
  16799. unflatten() function.
  16800. @return an object that maps JSON pointers to primitive values
  16801. @note Empty objects and arrays are flattened to `null` and will not be
  16802. reconstructed correctly by the @ref unflatten() function.
  16803. @complexity Linear in the size the JSON value.
  16804. @liveexample{The following code shows how a JSON object is flattened to an
  16805. object whose keys consist of JSON pointers.,flatten}
  16806. @sa @ref unflatten() for the reverse function
  16807. @since version 2.0.0
  16808. */
  16809. basic_json flatten() const
  16810. {
  16811. basic_json result(value_t::object);
  16812. json_pointer::flatten("", *this, result);
  16813. return result;
  16814. }
  16815. /*!
  16816. @brief unflatten a previously flattened JSON value
  16817. The function restores the arbitrary nesting of a JSON value that has been
  16818. flattened before using the @ref flatten() function. The JSON value must
  16819. meet certain constraints:
  16820. 1. The value must be an object.
  16821. 2. The keys must be JSON pointers (see
  16822. [RFC 6901](https://tools.ietf.org/html/rfc6901))
  16823. 3. The mapped values must be primitive JSON types.
  16824. @return the original JSON from a flattened version
  16825. @note Empty objects and arrays are flattened by @ref flatten() to `null`
  16826. values and can not unflattened to their original type. Apart from
  16827. this example, for a JSON value `j`, the following is always true:
  16828. `j == j.flatten().unflatten()`.
  16829. @complexity Linear in the size the JSON value.
  16830. @throw type_error.314 if value is not an object
  16831. @throw type_error.315 if object values are not primitive
  16832. @liveexample{The following code shows how a flattened JSON object is
  16833. unflattened into the original nested JSON object.,unflatten}
  16834. @sa @ref flatten() for the reverse function
  16835. @since version 2.0.0
  16836. */
  16837. basic_json unflatten() const
  16838. {
  16839. return json_pointer::unflatten(*this);
  16840. }
  16841. /// @}
  16842. //////////////////////////
  16843. // JSON Patch functions //
  16844. //////////////////////////
  16845. /// @name JSON Patch functions
  16846. /// @{
  16847. /*!
  16848. @brief applies a JSON patch
  16849. [JSON Patch](http://jsonpatch.com) defines a JSON document structure for
  16850. expressing a sequence of operations to apply to a JSON) document. With
  16851. this function, a JSON Patch is applied to the current JSON value by
  16852. executing all operations from the patch.
  16853. @param[in] json_patch JSON patch document
  16854. @return patched document
  16855. @note The application of a patch is atomic: Either all operations succeed
  16856. and the patched document is returned or an exception is thrown. In
  16857. any case, the original value is not changed: the patch is applied
  16858. to a copy of the value.
  16859. @throw parse_error.104 if the JSON patch does not consist of an array of
  16860. objects
  16861. @throw parse_error.105 if the JSON patch is malformed (e.g., mandatory
  16862. attributes are missing); example: `"operation add must have member path"`
  16863. @throw out_of_range.401 if an array index is out of range.
  16864. @throw out_of_range.403 if a JSON pointer inside the patch could not be
  16865. resolved successfully in the current JSON value; example: `"key baz not
  16866. found"`
  16867. @throw out_of_range.405 if JSON pointer has no parent ("add", "remove",
  16868. "move")
  16869. @throw other_error.501 if "test" operation was unsuccessful
  16870. @complexity Linear in the size of the JSON value and the length of the
  16871. JSON patch. As usually only a fraction of the JSON value is affected by
  16872. the patch, the complexity can usually be neglected.
  16873. @liveexample{The following code shows how a JSON patch is applied to a
  16874. value.,patch}
  16875. @sa @ref diff -- create a JSON patch by comparing two JSON values
  16876. @sa [RFC 6902 (JSON Patch)](https://tools.ietf.org/html/rfc6902)
  16877. @sa [RFC 6901 (JSON Pointer)](https://tools.ietf.org/html/rfc6901)
  16878. @since version 2.0.0
  16879. */
  16880. basic_json patch(const basic_json& json_patch) const
  16881. {
  16882. // make a working copy to apply the patch to
  16883. basic_json result = *this;
  16884. // the valid JSON Patch operations
  16885. enum class patch_operations {add, remove, replace, move, copy, test, invalid};
  16886. const auto get_op = [](const std::string & op)
  16887. {
  16888. if (op == "add")
  16889. {
  16890. return patch_operations::add;
  16891. }
  16892. if (op == "remove")
  16893. {
  16894. return patch_operations::remove;
  16895. }
  16896. if (op == "replace")
  16897. {
  16898. return patch_operations::replace;
  16899. }
  16900. if (op == "move")
  16901. {
  16902. return patch_operations::move;
  16903. }
  16904. if (op == "copy")
  16905. {
  16906. return patch_operations::copy;
  16907. }
  16908. if (op == "test")
  16909. {
  16910. return patch_operations::test;
  16911. }
  16912. return patch_operations::invalid;
  16913. };
  16914. // wrapper for "add" operation; add value at ptr
  16915. const auto operation_add = [&result](json_pointer & ptr, basic_json val)
  16916. {
  16917. // adding to the root of the target document means replacing it
  16918. if (ptr.is_root())
  16919. {
  16920. result = val;
  16921. }
  16922. else
  16923. {
  16924. // make sure the top element of the pointer exists
  16925. json_pointer top_pointer = ptr.top();
  16926. if (top_pointer != ptr)
  16927. {
  16928. result.at(top_pointer);
  16929. }
  16930. // get reference to parent of JSON pointer ptr
  16931. const auto last_path = ptr.pop_back();
  16932. basic_json& parent = result[ptr];
  16933. switch (parent.m_type)
  16934. {
  16935. case value_t::null:
  16936. case value_t::object:
  16937. {
  16938. // use operator[] to add value
  16939. parent[last_path] = val;
  16940. break;
  16941. }
  16942. case value_t::array:
  16943. {
  16944. if (last_path == "-")
  16945. {
  16946. // special case: append to back
  16947. parent.push_back(val);
  16948. }
  16949. else
  16950. {
  16951. const auto idx = json_pointer::array_index(last_path);
  16952. if (JSON_UNLIKELY(static_cast<size_type>(idx) > parent.size()))
  16953. {
  16954. // avoid undefined behavior
  16955. JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range"));
  16956. }
  16957. // default case: insert add offset
  16958. parent.insert(parent.begin() + static_cast<difference_type>(idx), val);
  16959. }
  16960. break;
  16961. }
  16962. // LCOV_EXCL_START
  16963. default:
  16964. {
  16965. // if there exists a parent it cannot be primitive
  16966. assert(false);
  16967. }
  16968. // LCOV_EXCL_STOP
  16969. }
  16970. }
  16971. };
  16972. // wrapper for "remove" operation; remove value at ptr
  16973. const auto operation_remove = [&result](json_pointer & ptr)
  16974. {
  16975. // get reference to parent of JSON pointer ptr
  16976. const auto last_path = ptr.pop_back();
  16977. basic_json& parent = result.at(ptr);
  16978. // remove child
  16979. if (parent.is_object())
  16980. {
  16981. // perform range check
  16982. auto it = parent.find(last_path);
  16983. if (JSON_LIKELY(it != parent.end()))
  16984. {
  16985. parent.erase(it);
  16986. }
  16987. else
  16988. {
  16989. JSON_THROW(out_of_range::create(403, "key '" + last_path + "' not found"));
  16990. }
  16991. }
  16992. else if (parent.is_array())
  16993. {
  16994. // note erase performs range check
  16995. parent.erase(static_cast<size_type>(json_pointer::array_index(last_path)));
  16996. }
  16997. };
  16998. // type check: top level value must be an array
  16999. if (JSON_UNLIKELY(not json_patch.is_array()))
  17000. {
  17001. JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects"));
  17002. }
  17003. // iterate and apply the operations
  17004. for (const auto& val : json_patch)
  17005. {
  17006. // wrapper to get a value for an operation
  17007. const auto get_value = [&val](const std::string & op,
  17008. const std::string & member,
  17009. bool string_type) -> basic_json &
  17010. {
  17011. // find value
  17012. auto it = val.m_value.object->find(member);
  17013. // context-sensitive error message
  17014. const auto error_msg = (op == "op") ? "operation" : "operation '" + op + "'";
  17015. // check if desired value is present
  17016. if (JSON_UNLIKELY(it == val.m_value.object->end()))
  17017. {
  17018. JSON_THROW(parse_error::create(105, 0, error_msg + " must have member '" + member + "'"));
  17019. }
  17020. // check if result is of type string
  17021. if (JSON_UNLIKELY(string_type and not it->second.is_string()))
  17022. {
  17023. JSON_THROW(parse_error::create(105, 0, error_msg + " must have string member '" + member + "'"));
  17024. }
  17025. // no error: return value
  17026. return it->second;
  17027. };
  17028. // type check: every element of the array must be an object
  17029. if (JSON_UNLIKELY(not val.is_object()))
  17030. {
  17031. JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects"));
  17032. }
  17033. // collect mandatory members
  17034. const std::string op = get_value("op", "op", true);
  17035. const std::string path = get_value(op, "path", true);
  17036. json_pointer ptr(path);
  17037. switch (get_op(op))
  17038. {
  17039. case patch_operations::add:
  17040. {
  17041. operation_add(ptr, get_value("add", "value", false));
  17042. break;
  17043. }
  17044. case patch_operations::remove:
  17045. {
  17046. operation_remove(ptr);
  17047. break;
  17048. }
  17049. case patch_operations::replace:
  17050. {
  17051. // the "path" location must exist - use at()
  17052. result.at(ptr) = get_value("replace", "value", false);
  17053. break;
  17054. }
  17055. case patch_operations::move:
  17056. {
  17057. const std::string from_path = get_value("move", "from", true);
  17058. json_pointer from_ptr(from_path);
  17059. // the "from" location must exist - use at()
  17060. basic_json v = result.at(from_ptr);
  17061. // The move operation is functionally identical to a
  17062. // "remove" operation on the "from" location, followed
  17063. // immediately by an "add" operation at the target
  17064. // location with the value that was just removed.
  17065. operation_remove(from_ptr);
  17066. operation_add(ptr, v);
  17067. break;
  17068. }
  17069. case patch_operations::copy:
  17070. {
  17071. const std::string from_path = get_value("copy", "from", true);
  17072. const json_pointer from_ptr(from_path);
  17073. // the "from" location must exist - use at()
  17074. basic_json v = result.at(from_ptr);
  17075. // The copy is functionally identical to an "add"
  17076. // operation at the target location using the value
  17077. // specified in the "from" member.
  17078. operation_add(ptr, v);
  17079. break;
  17080. }
  17081. case patch_operations::test:
  17082. {
  17083. bool success = false;
  17084. JSON_TRY
  17085. {
  17086. // check if "value" matches the one at "path"
  17087. // the "path" location must exist - use at()
  17088. success = (result.at(ptr) == get_value("test", "value", false));
  17089. }
  17090. JSON_INTERNAL_CATCH (out_of_range&)
  17091. {
  17092. // ignore out of range errors: success remains false
  17093. }
  17094. // throw an exception if test fails
  17095. if (JSON_UNLIKELY(not success))
  17096. {
  17097. JSON_THROW(other_error::create(501, "unsuccessful: " + val.dump()));
  17098. }
  17099. break;
  17100. }
  17101. case patch_operations::invalid:
  17102. {
  17103. // op must be "add", "remove", "replace", "move", "copy", or
  17104. // "test"
  17105. JSON_THROW(parse_error::create(105, 0, "operation value '" + op + "' is invalid"));
  17106. }
  17107. }
  17108. }
  17109. return result;
  17110. }
  17111. /*!
  17112. @brief creates a diff as a JSON patch
  17113. Creates a [JSON Patch](http://jsonpatch.com) so that value @a source can
  17114. be changed into the value @a target by calling @ref patch function.
  17115. @invariant For two JSON values @a source and @a target, the following code
  17116. yields always `true`:
  17117. @code {.cpp}
  17118. source.patch(diff(source, target)) == target;
  17119. @endcode
  17120. @note Currently, only `remove`, `add`, and `replace` operations are
  17121. generated.
  17122. @param[in] source JSON value to compare from
  17123. @param[in] target JSON value to compare against
  17124. @param[in] path helper value to create JSON pointers
  17125. @return a JSON patch to convert the @a source to @a target
  17126. @complexity Linear in the lengths of @a source and @a target.
  17127. @liveexample{The following code shows how a JSON patch is created as a
  17128. diff for two JSON values.,diff}
  17129. @sa @ref patch -- apply a JSON patch
  17130. @sa @ref merge_patch -- apply a JSON Merge Patch
  17131. @sa [RFC 6902 (JSON Patch)](https://tools.ietf.org/html/rfc6902)
  17132. @since version 2.0.0
  17133. */
  17134. static basic_json diff(const basic_json& source, const basic_json& target,
  17135. const std::string& path = "")
  17136. {
  17137. // the patch
  17138. basic_json result(value_t::array);
  17139. // if the values are the same, return empty patch
  17140. if (source == target)
  17141. {
  17142. return result;
  17143. }
  17144. if (source.type() != target.type())
  17145. {
  17146. // different types: replace value
  17147. result.push_back(
  17148. {
  17149. {"op", "replace"}, {"path", path}, {"value", target}
  17150. });
  17151. }
  17152. else
  17153. {
  17154. switch (source.type())
  17155. {
  17156. case value_t::array:
  17157. {
  17158. // first pass: traverse common elements
  17159. std::size_t i = 0;
  17160. while (i < source.size() and i < target.size())
  17161. {
  17162. // recursive call to compare array values at index i
  17163. auto temp_diff = diff(source[i], target[i], path + "/" + std::to_string(i));
  17164. result.insert(result.end(), temp_diff.begin(), temp_diff.end());
  17165. ++i;
  17166. }
  17167. // i now reached the end of at least one array
  17168. // in a second pass, traverse the remaining elements
  17169. // remove my remaining elements
  17170. const auto end_index = static_cast<difference_type>(result.size());
  17171. while (i < source.size())
  17172. {
  17173. // add operations in reverse order to avoid invalid
  17174. // indices
  17175. result.insert(result.begin() + end_index, object(
  17176. {
  17177. {"op", "remove"},
  17178. {"path", path + "/" + std::to_string(i)}
  17179. }));
  17180. ++i;
  17181. }
  17182. // add other remaining elements
  17183. while (i < target.size())
  17184. {
  17185. result.push_back(
  17186. {
  17187. {"op", "add"},
  17188. {"path", path + "/" + std::to_string(i)},
  17189. {"value", target[i]}
  17190. });
  17191. ++i;
  17192. }
  17193. break;
  17194. }
  17195. case value_t::object:
  17196. {
  17197. // first pass: traverse this object's elements
  17198. for (auto it = source.cbegin(); it != source.cend(); ++it)
  17199. {
  17200. // escape the key name to be used in a JSON patch
  17201. const auto key = json_pointer::escape(it.key());
  17202. if (target.find(it.key()) != target.end())
  17203. {
  17204. // recursive call to compare object values at key it
  17205. auto temp_diff = diff(it.value(), target[it.key()], path + "/" + key);
  17206. result.insert(result.end(), temp_diff.begin(), temp_diff.end());
  17207. }
  17208. else
  17209. {
  17210. // found a key that is not in o -> remove it
  17211. result.push_back(object(
  17212. {
  17213. {"op", "remove"}, {"path", path + "/" + key}
  17214. }));
  17215. }
  17216. }
  17217. // second pass: traverse other object's elements
  17218. for (auto it = target.cbegin(); it != target.cend(); ++it)
  17219. {
  17220. if (source.find(it.key()) == source.end())
  17221. {
  17222. // found a key that is not in this -> add it
  17223. const auto key = json_pointer::escape(it.key());
  17224. result.push_back(
  17225. {
  17226. {"op", "add"}, {"path", path + "/" + key},
  17227. {"value", it.value()}
  17228. });
  17229. }
  17230. }
  17231. break;
  17232. }
  17233. default:
  17234. {
  17235. // both primitive type: replace value
  17236. result.push_back(
  17237. {
  17238. {"op", "replace"}, {"path", path}, {"value", target}
  17239. });
  17240. break;
  17241. }
  17242. }
  17243. }
  17244. return result;
  17245. }
  17246. /// @}
  17247. ////////////////////////////////
  17248. // JSON Merge Patch functions //
  17249. ////////////////////////////////
  17250. /// @name JSON Merge Patch functions
  17251. /// @{
  17252. /*!
  17253. @brief applies a JSON Merge Patch
  17254. The merge patch format is primarily intended for use with the HTTP PATCH
  17255. method as a means of describing a set of modifications to a target
  17256. resource's content. This function applies a merge patch to the current
  17257. JSON value.
  17258. The function implements the following algorithm from Section 2 of
  17259. [RFC 7396 (JSON Merge Patch)](https://tools.ietf.org/html/rfc7396):
  17260. ```
  17261. define MergePatch(Target, Patch):
  17262. if Patch is an Object:
  17263. if Target is not an Object:
  17264. Target = {} // Ignore the contents and set it to an empty Object
  17265. for each Name/Value pair in Patch:
  17266. if Value is null:
  17267. if Name exists in Target:
  17268. remove the Name/Value pair from Target
  17269. else:
  17270. Target[Name] = MergePatch(Target[Name], Value)
  17271. return Target
  17272. else:
  17273. return Patch
  17274. ```
  17275. Thereby, `Target` is the current object; that is, the patch is applied to
  17276. the current value.
  17277. @param[in] apply_patch the patch to apply
  17278. @complexity Linear in the lengths of @a patch.
  17279. @liveexample{The following code shows how a JSON Merge Patch is applied to
  17280. a JSON document.,merge_patch}
  17281. @sa @ref patch -- apply a JSON patch
  17282. @sa [RFC 7396 (JSON Merge Patch)](https://tools.ietf.org/html/rfc7396)
  17283. @since version 3.0.0
  17284. */
  17285. void merge_patch(const basic_json& apply_patch)
  17286. {
  17287. if (apply_patch.is_object())
  17288. {
  17289. if (not is_object())
  17290. {
  17291. *this = object();
  17292. }
  17293. for (auto it = apply_patch.begin(); it != apply_patch.end(); ++it)
  17294. {
  17295. if (it.value().is_null())
  17296. {
  17297. erase(it.key());
  17298. }
  17299. else
  17300. {
  17301. operator[](it.key()).merge_patch(it.value());
  17302. }
  17303. }
  17304. }
  17305. else
  17306. {
  17307. *this = apply_patch;
  17308. }
  17309. }
  17310. /// @}
  17311. };
  17312. } // namespace nlohmann
  17313. ///////////////////////
  17314. // nonmember support //
  17315. ///////////////////////
  17316. // specialization of std::swap, and std::hash
  17317. namespace std
  17318. {
  17319. /// hash value for JSON objects
  17320. template<>
  17321. struct hash<nlohmann::json>
  17322. {
  17323. /*!
  17324. @brief return a hash value for a JSON object
  17325. @since version 1.0.0
  17326. */
  17327. std::size_t operator()(const nlohmann::json& j) const
  17328. {
  17329. // a naive hashing via the string representation
  17330. const auto& h = hash<nlohmann::json::string_t>();
  17331. return h(j.dump());
  17332. }
  17333. };
  17334. /// specialization for std::less<value_t>
  17335. /// @note: do not remove the space after '<',
  17336. /// see https://github.com/nlohmann/json/pull/679
  17337. template<>
  17338. struct less< ::nlohmann::detail::value_t>
  17339. {
  17340. /*!
  17341. @brief compare two value_t enum values
  17342. @since version 3.0.0
  17343. */
  17344. bool operator()(nlohmann::detail::value_t lhs,
  17345. nlohmann::detail::value_t rhs) const noexcept
  17346. {
  17347. return nlohmann::detail::operator<(lhs, rhs);
  17348. }
  17349. };
  17350. /*!
  17351. @brief exchanges the values of two JSON objects
  17352. @since version 1.0.0
  17353. */
  17354. template<>
  17355. inline void swap<nlohmann::json>(nlohmann::json& j1, nlohmann::json& j2) noexcept(
  17356. is_nothrow_move_constructible<nlohmann::json>::value and
  17357. is_nothrow_move_assignable<nlohmann::json>::value
  17358. )
  17359. {
  17360. j1.swap(j2);
  17361. }
  17362. } // namespace std
  17363. /*!
  17364. @brief user-defined string literal for JSON values
  17365. This operator implements a user-defined string literal for JSON objects. It
  17366. can be used by adding `"_json"` to a string literal and returns a JSON object
  17367. if no parse error occurred.
  17368. @param[in] s a string representation of a JSON object
  17369. @param[in] n the length of string @a s
  17370. @return a JSON object
  17371. @since version 1.0.0
  17372. */
  17373. inline nlohmann::json operator "" _json(const char* s, std::size_t n)
  17374. {
  17375. return nlohmann::json::parse(s, s + n);
  17376. }
  17377. /*!
  17378. @brief user-defined string literal for JSON pointer
  17379. This operator implements a user-defined string literal for JSON Pointers. It
  17380. can be used by adding `"_json_pointer"` to a string literal and returns a JSON pointer
  17381. object if no parse error occurred.
  17382. @param[in] s a string representation of a JSON Pointer
  17383. @param[in] n the length of string @a s
  17384. @return a JSON pointer object
  17385. @since version 2.0.0
  17386. */
  17387. inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std::size_t n)
  17388. {
  17389. return nlohmann::json::json_pointer(std::string(s, n));
  17390. }
  17391. // #include <nlohmann/detail/macro_unscope.hpp>
  17392. // restore GCC/clang diagnostic settings
  17393. #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
  17394. #pragma GCC diagnostic pop
  17395. #endif
  17396. #if defined(__clang__)
  17397. #pragma GCC diagnostic pop
  17398. #endif
  17399. // clean up
  17400. #undef JSON_INTERNAL_CATCH
  17401. #undef JSON_CATCH
  17402. #undef JSON_THROW
  17403. #undef JSON_TRY
  17404. #undef JSON_LIKELY
  17405. #undef JSON_UNLIKELY
  17406. #undef JSON_DEPRECATED
  17407. #undef JSON_HAS_CPP_14
  17408. #undef JSON_HAS_CPP_17
  17409. #undef NLOHMANN_BASIC_JSON_TPL_DECLARATION
  17410. #undef NLOHMANN_BASIC_JSON_TPL
  17411. #endif
  17412. #pragma endregion
  17413. ////////////////////////////////////////////////
  17414. //stb_image_write.h
  17415. ////////////////////////////////////////////////
  17416. #pragma region stb_image_write
  17417. /* stb_image_write - v1.11 - public domain - http://nothings.org/stb/stb_image_write.h
  17418. writes out PNG/BMP/TGA/JPEG/HDR images to C stdio - Sean Barrett 2010-2015
  17419. no warranty implied; use at your own risk
  17420. Before #including,
  17421. #define STB_IMAGE_WRITE_IMPLEMENTATION
  17422. in the file that you want to have the implementation.
  17423. Will probably not work correctly with strict-aliasing optimizations.
  17424. If using a modern Microsoft Compiler, non-safe versions of CRT calls may cause
  17425. compilation warnings or even errors. To avoid this, also before #including,
  17426. #define STBI_MSC_SECURE_CRT
  17427. ABOUT:
  17428. This header file is a library for writing images to C stdio or a callback.
  17429. The PNG output is not optimal; it is 20-50% larger than the file
  17430. written by a decent optimizing implementation; though providing a custom
  17431. zlib compress function (see STBIW_ZLIB_COMPRESS) can mitigate that.
  17432. This library is designed for source code compactness and simplicity,
  17433. not optimal image file size or run-time performance.
  17434. BUILDING:
  17435. You can #define STBIW_ASSERT(x) before the #include to avoid using assert.h.
  17436. You can #define STBIW_MALLOC(), STBIW_REALLOC(), and STBIW_FREE() to replace
  17437. malloc,realloc,free.
  17438. You can #define STBIW_MEMMOVE() to replace memmove()
  17439. You can #define STBIW_ZLIB_COMPRESS to use a custom zlib-style compress function
  17440. for PNG compression (instead of the builtin one), it must have the following signature:
  17441. unsigned char * my_compress(unsigned char *data, int data_len, int *out_len, int quality);
  17442. The returned data will be freed with STBIW_FREE() (free() by default),
  17443. so it must be heap allocated with STBIW_MALLOC() (malloc() by default),
  17444. UNICODE:
  17445. If compiling for Windows and you wish to use Unicode filenames, compile
  17446. with
  17447. #define STBIW_WINDOWS_UTF8
  17448. and pass utf8-encoded filenames. Call stbiw_convert_wchar_to_utf8 to convert
  17449. Windows wchar_t filenames to utf8.
  17450. USAGE:
  17451. There are five functions, one for each image file format:
  17452. int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);
  17453. int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data);
  17454. int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data);
  17455. int stbi_write_jpg(char const *filename, int w, int h, int comp, const void *data, int quality);
  17456. int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data);
  17457. void stbi_flip_vertically_on_write(int flag); // flag is non-zero to flip data vertically
  17458. There are also five equivalent functions that use an arbitrary write function. You are
  17459. expected to open/close your file-equivalent before and after calling these:
  17460. int stbi_write_png_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data, int stride_in_bytes);
  17461. int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);
  17462. int stbi_write_tga_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);
  17463. int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const float *data);
  17464. int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality);
  17465. where the callback is:
  17466. void stbi_write_func(void *context, void *data, int size);
  17467. You can configure it with these global variables:
  17468. int stbi_write_tga_with_rle; // defaults to true; set to 0 to disable RLE
  17469. int stbi_write_png_compression_level; // defaults to 8; set to higher for more compression
  17470. int stbi_write_force_png_filter; // defaults to -1; set to 0..5 to force a filter mode
  17471. You can define STBI_WRITE_NO_STDIO to disable the file variant of these
  17472. functions, so the library will not use stdio.h at all. However, this will
  17473. also disable HDR writing, because it requires stdio for formatted output.
  17474. Each function returns 0 on failure and non-0 on success.
  17475. The functions create an image file defined by the parameters. The image
  17476. is a rectangle of pixels stored from left-to-right, top-to-bottom.
  17477. Each pixel contains 'comp' channels of data stored interleaved with 8-bits
  17478. per channel, in the following order: 1=Y, 2=YA, 3=RGB, 4=RGBA. (Y is
  17479. monochrome color.) The rectangle is 'w' pixels wide and 'h' pixels tall.
  17480. The *data pointer points to the first byte of the top-left-most pixel.
  17481. For PNG, "stride_in_bytes" is the distance in bytes from the first byte of
  17482. a row of pixels to the first byte of the next row of pixels.
  17483. PNG creates output files with the same number of components as the input.
  17484. The BMP format expands Y to RGB in the file format and does not
  17485. output alpha.
  17486. PNG supports writing rectangles of data even when the bytes storing rows of
  17487. data are not consecutive in memory (e.g. sub-rectangles of a larger image),
  17488. by supplying the stride between the beginning of adjacent rows. The other
  17489. formats do not. (Thus you cannot write a native-format BMP through the BMP
  17490. writer, both because it is in BGR order and because it may have padding
  17491. at the end of the line.)
  17492. PNG allows you to set the deflate compression level by setting the global
  17493. variable 'stbi_write_png_compression_level' (it defaults to 8).
  17494. HDR expects linear float data. Since the format is always 32-bit rgb(e)
  17495. data, alpha (if provided) is discarded, and for monochrome data it is
  17496. replicated across all three channels.
  17497. TGA supports RLE or non-RLE compressed data. To use non-RLE-compressed
  17498. data, set the global variable 'stbi_write_tga_with_rle' to 0.
  17499. JPEG does ignore alpha channels in input data; quality is between 1 and 100.
  17500. Higher quality looks better but results in a bigger image.
  17501. JPEG baseline (no JPEG progressive).
  17502. CREDITS:
  17503. Sean Barrett - PNG/BMP/TGA
  17504. Baldur Karlsson - HDR
  17505. Jean-Sebastien Guay - TGA monochrome
  17506. Tim Kelsey - misc enhancements
  17507. Alan Hickman - TGA RLE
  17508. Emmanuel Julien - initial file IO callback implementation
  17509. Jon Olick - original jo_jpeg.cpp code
  17510. Daniel Gibson - integrate JPEG, allow external zlib
  17511. Aarni Koskela - allow choosing PNG filter
  17512. bugfixes:
  17513. github:Chribba
  17514. Guillaume Chereau
  17515. github:jry2
  17516. github:romigrou
  17517. Sergio Gonzalez
  17518. Jonas Karlsson
  17519. Filip Wasil
  17520. Thatcher Ulrich
  17521. github:poppolopoppo
  17522. Patrick Boettcher
  17523. github:xeekworx
  17524. Cap Petschulat
  17525. Simon Rodriguez
  17526. Ivan Tikhonov
  17527. github:ignotion
  17528. Adam Schackart
  17529. LICENSE
  17530. See end of file for license information.
  17531. */
  17532. #ifndef INCLUDE_STB_IMAGE_WRITE_H
  17533. #define INCLUDE_STB_IMAGE_WRITE_H
  17534. #include <stdlib.h>
  17535. // if STB_IMAGE_WRITE_STATIC causes problems, try defining STBIWDEF to 'inline' or 'static inline'
  17536. #ifndef STBIWDEF
  17537. #ifdef STB_IMAGE_WRITE_STATIC
  17538. #define STBIWDEF static
  17539. #else
  17540. #ifdef __cplusplus
  17541. #define STBIWDEF extern "C"
  17542. #else
  17543. #define STBIWDEF extern
  17544. #endif
  17545. #endif
  17546. #endif
  17547. #ifndef STB_IMAGE_WRITE_STATIC // C++ forbids static forward declarations
  17548. extern int stbi_write_tga_with_rle;
  17549. extern int stbi_write_png_compression_level;
  17550. extern int stbi_write_force_png_filter;
  17551. #endif
  17552. #ifndef STBI_WRITE_NO_STDIO
  17553. STBIWDEF int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);
  17554. STBIWDEF int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data);
  17555. STBIWDEF int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data);
  17556. STBIWDEF int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data);
  17557. STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality);
  17558. #ifdef STBI_WINDOWS_UTF8
  17559. STBIWDEF int stbiw_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t *input);
  17560. #endif
  17561. #endif
  17562. typedef void stbi_write_func(void *context, void *data, int size);
  17563. STBIWDEF int stbi_write_png_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data, int stride_in_bytes);
  17564. STBIWDEF int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);
  17565. STBIWDEF int stbi_write_tga_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);
  17566. STBIWDEF int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const float *data);
  17567. STBIWDEF int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality);
  17568. STBIWDEF void stbi_flip_vertically_on_write(int flip_boolean);
  17569. #endif//INCLUDE_STB_IMAGE_WRITE_H
  17570. #ifdef STB_IMAGE_WRITE_IMPLEMENTATION
  17571. #ifdef _WIN32
  17572. #ifndef _CRT_SECURE_NO_WARNINGS
  17573. #define _CRT_SECURE_NO_WARNINGS
  17574. #endif
  17575. #ifndef _CRT_NONSTDC_NO_DEPRECATE
  17576. #define _CRT_NONSTDC_NO_DEPRECATE
  17577. #endif
  17578. #endif
  17579. #ifndef STBI_WRITE_NO_STDIO
  17580. #include <stdio.h>
  17581. #endif // STBI_WRITE_NO_STDIO
  17582. #include <stdarg.h>
  17583. #include <stdlib.h>
  17584. #include <string.h>
  17585. #include <math.h>
  17586. #if defined(STBIW_MALLOC) && defined(STBIW_FREE) && (defined(STBIW_REALLOC) || defined(STBIW_REALLOC_SIZED))
  17587. // ok
  17588. #elif !defined(STBIW_MALLOC) && !defined(STBIW_FREE) && !defined(STBIW_REALLOC) && !defined(STBIW_REALLOC_SIZED)
  17589. // ok
  17590. #else
  17591. #error "Must define all or none of STBIW_MALLOC, STBIW_FREE, and STBIW_REALLOC (or STBIW_REALLOC_SIZED)."
  17592. #endif
  17593. #ifndef STBIW_MALLOC
  17594. #define STBIW_MALLOC(sz) malloc(sz)
  17595. #define STBIW_REALLOC(p,newsz) realloc(p,newsz)
  17596. #define STBIW_FREE(p) free(p)
  17597. #endif
  17598. #ifndef STBIW_REALLOC_SIZED
  17599. #define STBIW_REALLOC_SIZED(p,oldsz,newsz) STBIW_REALLOC(p,newsz)
  17600. #endif
  17601. #ifndef STBIW_MEMMOVE
  17602. #define STBIW_MEMMOVE(a,b,sz) memmove(a,b,sz)
  17603. #endif
  17604. #ifndef STBIW_ASSERT
  17605. #include <assert.h>
  17606. #define STBIW_ASSERT(x) assert(x)
  17607. #endif
  17608. #define STBIW_UCHAR(x) (unsigned char) ((x) & 0xff)
  17609. #ifdef STB_IMAGE_WRITE_STATIC
  17610. static int stbi__flip_vertically_on_write = 0;
  17611. static int stbi_write_png_compression_level = 8;
  17612. static int stbi_write_tga_with_rle = 1;
  17613. static int stbi_write_force_png_filter = -1;
  17614. #else
  17615. int stbi_write_png_compression_level = 8;
  17616. int stbi__flip_vertically_on_write = 0;
  17617. int stbi_write_tga_with_rle = 1;
  17618. int stbi_write_force_png_filter = -1;
  17619. #endif
  17620. STBIWDEF void stbi_flip_vertically_on_write(int flag)
  17621. {
  17622. stbi__flip_vertically_on_write = flag;
  17623. }
  17624. typedef struct
  17625. {
  17626. stbi_write_func *func;
  17627. void *context;
  17628. } stbi__write_context;
  17629. // initialize a callback-based context
  17630. static void stbi__start_write_callbacks(stbi__write_context *s, stbi_write_func *c, void *context)
  17631. {
  17632. s->func = c;
  17633. s->context = context;
  17634. }
  17635. #ifndef STBI_WRITE_NO_STDIO
  17636. static void stbi__stdio_write(void *context, void *data, int size)
  17637. {
  17638. fwrite(data, 1, size, (FILE *)context);
  17639. }
  17640. #if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8)
  17641. #ifdef __cplusplus
  17642. #define STBIW_EXTERN extern "C"
  17643. #else
  17644. #define STBIW_EXTERN extern
  17645. #endif
  17646. STBIW_EXTERN __declspec(dllimport) int __stdcall MultiByteToWideChar(unsigned int cp, unsigned long flags, const char *str, int cbmb, wchar_t *widestr, int cchwide);
  17647. STBIW_EXTERN __declspec(dllimport) int __stdcall WideCharToMultiByte(unsigned int cp, unsigned long flags, const wchar_t *widestr, int cchwide, char *str, int cbmb, const char *defchar, int *used_default);
  17648. STBIWDEF int stbiw_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t *input)
  17649. {
  17650. return WideCharToMultiByte(65001 /* UTF8 */, 0, input, -1, buffer, bufferlen, NULL, NULL);
  17651. }
  17652. #endif
  17653. static FILE *stbiw__fopen(char const *filename, char const *mode)
  17654. {
  17655. FILE *f;
  17656. #if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8)
  17657. wchar_t wMode[64];
  17658. wchar_t wFilename[1024];
  17659. if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, filename, -1, wFilename, sizeof(wFilename)))
  17660. return 0;
  17661. if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, mode, -1, wMode, sizeof(wMode)))
  17662. return 0;
  17663. #if _MSC_VER >= 1400
  17664. if (0 != _wfopen_s(&f, wFilename, wMode))
  17665. f = 0;
  17666. #else
  17667. f = _wfopen(wFilename, wMode);
  17668. #endif
  17669. #elif defined(_MSC_VER) && _MSC_VER >= 1400
  17670. if (0 != fopen_s(&f, filename, mode))
  17671. f = 0;
  17672. #else
  17673. f = fopen(filename, mode);
  17674. #endif
  17675. return f;
  17676. }
  17677. static int stbi__start_write_file(stbi__write_context *s, const char *filename)
  17678. {
  17679. FILE *f = stbiw__fopen(filename, "wb");
  17680. stbi__start_write_callbacks(s, stbi__stdio_write, (void *)f);
  17681. return f != NULL;
  17682. }
  17683. static void stbi__end_write_file(stbi__write_context *s)
  17684. {
  17685. fclose((FILE *)s->context);
  17686. }
  17687. #endif // !STBI_WRITE_NO_STDIO
  17688. typedef unsigned int stbiw_uint32;
  17689. typedef int stb_image_write_test[sizeof(stbiw_uint32) == 4 ? 1 : -1];
  17690. static void stbiw__writefv(stbi__write_context *s, const char *fmt, va_list v)
  17691. {
  17692. while (*fmt)
  17693. {
  17694. switch (*fmt++)
  17695. {
  17696. case ' ': break;
  17697. case '1':
  17698. {
  17699. unsigned char x = STBIW_UCHAR(va_arg(v, int));
  17700. s->func(s->context, &x, 1);
  17701. break;
  17702. }
  17703. case '2':
  17704. {
  17705. int x = va_arg(v, int);
  17706. unsigned char b[2];
  17707. b[0] = STBIW_UCHAR(x);
  17708. b[1] = STBIW_UCHAR(x >> 8);
  17709. s->func(s->context, b, 2);
  17710. break;
  17711. }
  17712. case '4':
  17713. {
  17714. stbiw_uint32 x = va_arg(v, int);
  17715. unsigned char b[4];
  17716. b[0] = STBIW_UCHAR(x);
  17717. b[1] = STBIW_UCHAR(x >> 8);
  17718. b[2] = STBIW_UCHAR(x >> 16);
  17719. b[3] = STBIW_UCHAR(x >> 24);
  17720. s->func(s->context, b, 4);
  17721. break;
  17722. }
  17723. default:
  17724. STBIW_ASSERT(0);
  17725. return;
  17726. }
  17727. }
  17728. }
  17729. static void stbiw__writef(stbi__write_context *s, const char *fmt, ...)
  17730. {
  17731. va_list v;
  17732. va_start(v, fmt);
  17733. stbiw__writefv(s, fmt, v);
  17734. va_end(v);
  17735. }
  17736. static void stbiw__putc(stbi__write_context *s, unsigned char c)
  17737. {
  17738. s->func(s->context, &c, 1);
  17739. }
  17740. static void stbiw__write3(stbi__write_context *s, unsigned char a, unsigned char b, unsigned char c)
  17741. {
  17742. unsigned char arr[3];
  17743. arr[0] = a, arr[1] = b, arr[2] = c;
  17744. s->func(s->context, arr, 3);
  17745. }
  17746. static void stbiw__write_pixel(stbi__write_context *s, int rgb_dir, int comp, int write_alpha, int expand_mono, unsigned char *d)
  17747. {
  17748. unsigned char bg[3] = { 255, 0, 255 }, px[3];
  17749. int k;
  17750. if (write_alpha < 0)
  17751. s->func(s->context, &d[comp - 1], 1);
  17752. switch (comp)
  17753. {
  17754. case 2: // 2 pixels = mono + alpha, alpha is written separately, so same as 1-channel case
  17755. case 1:
  17756. if (expand_mono)
  17757. stbiw__write3(s, d[0], d[0], d[0]); // monochrome bmp
  17758. else
  17759. s->func(s->context, d, 1); // monochrome TGA
  17760. break;
  17761. case 4:
  17762. if (!write_alpha)
  17763. {
  17764. // composite against pink background
  17765. for (k = 0; k < 3; ++k)
  17766. px[k] = bg[k] + ((d[k] - bg[k]) * d[3]) / 255;
  17767. stbiw__write3(s, px[1 - rgb_dir], px[1], px[1 + rgb_dir]);
  17768. break;
  17769. }
  17770. /* FALLTHROUGH */
  17771. case 3:
  17772. stbiw__write3(s, d[1 - rgb_dir], d[1], d[1 + rgb_dir]);
  17773. break;
  17774. }
  17775. if (write_alpha > 0)
  17776. s->func(s->context, &d[comp - 1], 1);
  17777. }
  17778. static void stbiw__write_pixels(stbi__write_context *s, int rgb_dir, int vdir, int x, int y, int comp, void *data, int write_alpha, int scanline_pad, int expand_mono)
  17779. {
  17780. stbiw_uint32 zero = 0;
  17781. int i, j, j_end;
  17782. if (y <= 0)
  17783. return;
  17784. if (stbi__flip_vertically_on_write)
  17785. vdir *= -1;
  17786. if (vdir < 0)
  17787. j_end = -1, j = y - 1;
  17788. else
  17789. j_end = y, j = 0;
  17790. for (; j != j_end; j += vdir)
  17791. {
  17792. for (i = 0; i < x; ++i)
  17793. {
  17794. unsigned char *d = (unsigned char *)data + (j * x + i) * comp;
  17795. stbiw__write_pixel(s, rgb_dir, comp, write_alpha, expand_mono, d);
  17796. }
  17797. s->func(s->context, &zero, scanline_pad);
  17798. }
  17799. }
  17800. static int stbiw__outfile(stbi__write_context *s, int rgb_dir, int vdir, int x, int y, int comp, int expand_mono, void *data, int alpha, int pad, const char *fmt, ...)
  17801. {
  17802. if (y < 0 || x < 0)
  17803. {
  17804. return 0;
  17805. }
  17806. else
  17807. {
  17808. va_list v;
  17809. va_start(v, fmt);
  17810. stbiw__writefv(s, fmt, v);
  17811. va_end(v);
  17812. stbiw__write_pixels(s, rgb_dir, vdir, x, y, comp, data, alpha, pad, expand_mono);
  17813. return 1;
  17814. }
  17815. }
  17816. static int stbi_write_bmp_core(stbi__write_context *s, int x, int y, int comp, const void *data)
  17817. {
  17818. int pad = (-x * 3) & 3;
  17819. return stbiw__outfile(s, -1, -1, x, y, comp, 1, (void *)data, 0, pad,
  17820. "11 4 22 4" "4 44 22 444444",
  17821. 'B', 'M', 14 + 40 + (x * 3 + pad) * y, 0, 0, 14 + 40, // file header
  17822. 40, x, y, 1, 24, 0, 0, 0, 0, 0, 0); // bitmap header
  17823. }
  17824. STBIWDEF int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data)
  17825. {
  17826. stbi__write_context s;
  17827. stbi__start_write_callbacks(&s, func, context);
  17828. return stbi_write_bmp_core(&s, x, y, comp, data);
  17829. }
  17830. #ifndef STBI_WRITE_NO_STDIO
  17831. STBIWDEF int stbi_write_bmp(char const *filename, int x, int y, int comp, const void *data)
  17832. {
  17833. stbi__write_context s;
  17834. if (stbi__start_write_file(&s, filename))
  17835. {
  17836. int r = stbi_write_bmp_core(&s, x, y, comp, data);
  17837. stbi__end_write_file(&s);
  17838. return r;
  17839. }
  17840. else
  17841. return 0;
  17842. }
  17843. #endif //!STBI_WRITE_NO_STDIO
  17844. static int stbi_write_tga_core(stbi__write_context *s, int x, int y, int comp, void *data)
  17845. {
  17846. int has_alpha = (comp == 2 || comp == 4);
  17847. int colorbytes = has_alpha ? comp - 1 : comp;
  17848. int format = colorbytes < 2 ? 3 : 2; // 3 color channels (RGB/RGBA) = 2, 1 color channel (Y/YA) = 3
  17849. if (y < 0 || x < 0)
  17850. return 0;
  17851. if (!stbi_write_tga_with_rle)
  17852. {
  17853. return stbiw__outfile(s, -1, -1, x, y, comp, 0, (void *)data, has_alpha, 0,
  17854. "111 221 2222 11", 0, 0, format, 0, 0, 0, 0, 0, x, y, (colorbytes + has_alpha) * 8, has_alpha * 8);
  17855. }
  17856. else
  17857. {
  17858. int i, j, k;
  17859. int jend, jdir;
  17860. stbiw__writef(s, "111 221 2222 11", 0, 0, format + 8, 0, 0, 0, 0, 0, x, y, (colorbytes + has_alpha) * 8, has_alpha * 8);
  17861. if (stbi__flip_vertically_on_write)
  17862. {
  17863. j = 0;
  17864. jend = y;
  17865. jdir = 1;
  17866. }
  17867. else
  17868. {
  17869. j = y - 1;
  17870. jend = -1;
  17871. jdir = -1;
  17872. }
  17873. for (; j != jend; j += jdir)
  17874. {
  17875. unsigned char *row = (unsigned char *)data + j * x * comp;
  17876. int len;
  17877. for (i = 0; i < x; i += len)
  17878. {
  17879. unsigned char *begin = row + i * comp;
  17880. int diff = 1;
  17881. len = 1;
  17882. if (i < x - 1)
  17883. {
  17884. ++len;
  17885. diff = memcmp(begin, row + (i + 1) * comp, comp);
  17886. if (diff)
  17887. {
  17888. const unsigned char *prev = begin;
  17889. for (k = i + 2; k < x && len < 128; ++k)
  17890. {
  17891. if (memcmp(prev, row + k * comp, comp))
  17892. {
  17893. prev += comp;
  17894. ++len;
  17895. }
  17896. else
  17897. {
  17898. --len;
  17899. break;
  17900. }
  17901. }
  17902. }
  17903. else
  17904. {
  17905. for (k = i + 2; k < x && len < 128; ++k)
  17906. {
  17907. if (!memcmp(begin, row + k * comp, comp))
  17908. {
  17909. ++len;
  17910. }
  17911. else
  17912. {
  17913. break;
  17914. }
  17915. }
  17916. }
  17917. }
  17918. if (diff)
  17919. {
  17920. unsigned char header = STBIW_UCHAR(len - 1);
  17921. s->func(s->context, &header, 1);
  17922. for (k = 0; k < len; ++k)
  17923. {
  17924. stbiw__write_pixel(s, -1, comp, has_alpha, 0, begin + k * comp);
  17925. }
  17926. }
  17927. else
  17928. {
  17929. unsigned char header = STBIW_UCHAR(len - 129);
  17930. s->func(s->context, &header, 1);
  17931. stbiw__write_pixel(s, -1, comp, has_alpha, 0, begin);
  17932. }
  17933. }
  17934. }
  17935. }
  17936. return 1;
  17937. }
  17938. STBIWDEF int stbi_write_tga_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data)
  17939. {
  17940. stbi__write_context s;
  17941. stbi__start_write_callbacks(&s, func, context);
  17942. return stbi_write_tga_core(&s, x, y, comp, (void *)data);
  17943. }
  17944. #ifndef STBI_WRITE_NO_STDIO
  17945. STBIWDEF int stbi_write_tga(char const *filename, int x, int y, int comp, const void *data)
  17946. {
  17947. stbi__write_context s;
  17948. if (stbi__start_write_file(&s, filename))
  17949. {
  17950. int r = stbi_write_tga_core(&s, x, y, comp, (void *)data);
  17951. stbi__end_write_file(&s);
  17952. return r;
  17953. }
  17954. else
  17955. return 0;
  17956. }
  17957. #endif
  17958. // *************************************************************************************************
  17959. // Radiance RGBE HDR writer
  17960. // by Baldur Karlsson
  17961. #define stbiw__max(a, b) ((a) > (b) ? (a) : (b))
  17962. static void stbiw__linear_to_rgbe(unsigned char *rgbe, float *linear)
  17963. {
  17964. int exponent;
  17965. float maxcomp = stbiw__max(linear[0], stbiw__max(linear[1], linear[2]));
  17966. if (maxcomp < 1e-32f)
  17967. {
  17968. rgbe[0] = rgbe[1] = rgbe[2] = rgbe[3] = 0;
  17969. }
  17970. else
  17971. {
  17972. float normalize = (float)frexp(maxcomp, &exponent) * 256.0f / maxcomp;
  17973. rgbe[0] = (unsigned char)(linear[0] * normalize);
  17974. rgbe[1] = (unsigned char)(linear[1] * normalize);
  17975. rgbe[2] = (unsigned char)(linear[2] * normalize);
  17976. rgbe[3] = (unsigned char)(exponent + 128);
  17977. }
  17978. }
  17979. static void stbiw__write_run_data(stbi__write_context *s, int length, unsigned char databyte)
  17980. {
  17981. unsigned char lengthbyte = STBIW_UCHAR(length + 128);
  17982. STBIW_ASSERT(length + 128 <= 255);
  17983. s->func(s->context, &lengthbyte, 1);
  17984. s->func(s->context, &databyte, 1);
  17985. }
  17986. static void stbiw__write_dump_data(stbi__write_context *s, int length, unsigned char *data)
  17987. {
  17988. unsigned char lengthbyte = STBIW_UCHAR(length);
  17989. STBIW_ASSERT(length <= 128); // inconsistent with spec but consistent with official code
  17990. s->func(s->context, &lengthbyte, 1);
  17991. s->func(s->context, data, length);
  17992. }
  17993. static void stbiw__write_hdr_scanline(stbi__write_context *s, int width, int ncomp, unsigned char *scratch, float *scanline)
  17994. {
  17995. unsigned char scanlineheader[4] = { 2, 2, 0, 0 };
  17996. unsigned char rgbe[4];
  17997. float linear[3];
  17998. int x;
  17999. scanlineheader[2] = (width & 0xff00) >> 8;
  18000. scanlineheader[3] = (width & 0x00ff);
  18001. /* skip RLE for images too small or large */
  18002. if (width < 8 || width >= 32768)
  18003. {
  18004. for (x = 0; x < width; x++)
  18005. {
  18006. switch (ncomp)
  18007. {
  18008. case 4: /* fallthrough */
  18009. case 3: linear[2] = scanline[x * ncomp + 2];
  18010. linear[1] = scanline[x * ncomp + 1];
  18011. linear[0] = scanline[x * ncomp + 0];
  18012. break;
  18013. default:
  18014. linear[0] = linear[1] = linear[2] = scanline[x * ncomp + 0];
  18015. break;
  18016. }
  18017. stbiw__linear_to_rgbe(rgbe, linear);
  18018. s->func(s->context, rgbe, 4);
  18019. }
  18020. }
  18021. else
  18022. {
  18023. int c, r;
  18024. /* encode into scratch buffer */
  18025. for (x = 0; x < width; x++)
  18026. {
  18027. switch (ncomp)
  18028. {
  18029. case 4: /* fallthrough */
  18030. case 3: linear[2] = scanline[x * ncomp + 2];
  18031. linear[1] = scanline[x * ncomp + 1];
  18032. linear[0] = scanline[x * ncomp + 0];
  18033. break;
  18034. default:
  18035. linear[0] = linear[1] = linear[2] = scanline[x * ncomp + 0];
  18036. break;
  18037. }
  18038. stbiw__linear_to_rgbe(rgbe, linear);
  18039. scratch[x + width * 0] = rgbe[0];
  18040. scratch[x + width * 1] = rgbe[1];
  18041. scratch[x + width * 2] = rgbe[2];
  18042. scratch[x + width * 3] = rgbe[3];
  18043. }
  18044. s->func(s->context, scanlineheader, 4);
  18045. /* RLE each component separately */
  18046. for (c = 0; c < 4; c++)
  18047. {
  18048. unsigned char *comp = &scratch[width * c];
  18049. x = 0;
  18050. while (x < width)
  18051. {
  18052. // find first run
  18053. r = x;
  18054. while (r + 2 < width)
  18055. {
  18056. if (comp[r] == comp[r + 1] && comp[r] == comp[r + 2])
  18057. break;
  18058. ++r;
  18059. }
  18060. if (r + 2 >= width)
  18061. r = width;
  18062. // dump up to first run
  18063. while (x < r)
  18064. {
  18065. int len = r - x;
  18066. if (len > 128) len = 128;
  18067. stbiw__write_dump_data(s, len, &comp[x]);
  18068. x += len;
  18069. }
  18070. // if there's a run, output it
  18071. if (r + 2 < width)
  18072. { // same test as what we break out of in search loop, so only true if we break'd
  18073. // find next byte after run
  18074. while (r < width && comp[r] == comp[x])
  18075. ++r;
  18076. // output run up to r
  18077. while (x < r)
  18078. {
  18079. int len = r - x;
  18080. if (len > 127) len = 127;
  18081. stbiw__write_run_data(s, len, comp[x]);
  18082. x += len;
  18083. }
  18084. }
  18085. }
  18086. }
  18087. }
  18088. }
  18089. static int stbi_write_hdr_core(stbi__write_context *s, int x, int y, int comp, float *data)
  18090. {
  18091. if (y <= 0 || x <= 0 || data == NULL)
  18092. return 0;
  18093. else
  18094. {
  18095. // Each component is stored separately. Allocate scratch space for full output scanline.
  18096. unsigned char *scratch = (unsigned char *)STBIW_MALLOC(x * 4);
  18097. int i, len;
  18098. char buffer[128];
  18099. char header[] = "#?RADIANCE\n# Written by stb_image_write.h\nFORMAT=32-bit_rle_rgbe\n";
  18100. s->func(s->context, header, sizeof(header) - 1);
  18101. #ifdef STBI_MSC_SECURE_CRT
  18102. len = sprintf_s(buffer, "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x);
  18103. #else
  18104. len = sprintf(buffer, "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x);
  18105. #endif
  18106. s->func(s->context, buffer, len);
  18107. for (i = 0; i < y; i++)
  18108. stbiw__write_hdr_scanline(s, x, comp, scratch, data + comp * x * (stbi__flip_vertically_on_write ? y - 1 - i : i));
  18109. STBIW_FREE(scratch);
  18110. return 1;
  18111. }
  18112. }
  18113. STBIWDEF int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const float *data)
  18114. {
  18115. stbi__write_context s;
  18116. stbi__start_write_callbacks(&s, func, context);
  18117. return stbi_write_hdr_core(&s, x, y, comp, (float *)data);
  18118. }
  18119. #ifndef STBI_WRITE_NO_STDIO
  18120. STBIWDEF int stbi_write_hdr(char const *filename, int x, int y, int comp, const float *data)
  18121. {
  18122. stbi__write_context s;
  18123. if (stbi__start_write_file(&s, filename))
  18124. {
  18125. int r = stbi_write_hdr_core(&s, x, y, comp, (float *)data);
  18126. stbi__end_write_file(&s);
  18127. return r;
  18128. }
  18129. else
  18130. return 0;
  18131. }
  18132. #endif // STBI_WRITE_NO_STDIO
  18133. //////////////////////////////////////////////////////////////////////////////
  18134. //
  18135. // PNG writer
  18136. //
  18137. #ifndef STBIW_ZLIB_COMPRESS
  18138. // stretchy buffer; stbiw__sbpush() == vector<>::push_back() -- stbiw__sbcount() == vector<>::size()
  18139. #define stbiw__sbraw(a) ((int *) (a) - 2)
  18140. #define stbiw__sbm(a) stbiw__sbraw(a)[0]
  18141. #define stbiw__sbn(a) stbiw__sbraw(a)[1]
  18142. #define stbiw__sbneedgrow(a,n) ((a)==0 || stbiw__sbn(a)+n >= stbiw__sbm(a))
  18143. #define stbiw__sbmaybegrow(a,n) (stbiw__sbneedgrow(a,(n)) ? stbiw__sbgrow(a,n) : 0)
  18144. #define stbiw__sbgrow(a,n) stbiw__sbgrowf((void **) &(a), (n), sizeof(*(a)))
  18145. #define stbiw__sbpush(a, v) (stbiw__sbmaybegrow(a,1), (a)[stbiw__sbn(a)++] = (v))
  18146. #define stbiw__sbcount(a) ((a) ? stbiw__sbn(a) : 0)
  18147. #define stbiw__sbfree(a) ((a) ? STBIW_FREE(stbiw__sbraw(a)),0 : 0)
  18148. static void *stbiw__sbgrowf(void **arr, int increment, int itemsize)
  18149. {
  18150. int m = *arr ? 2 * stbiw__sbm(*arr) + increment : increment + 1;
  18151. void *p = STBIW_REALLOC_SIZED(*arr ? stbiw__sbraw(*arr) : 0, *arr ? (stbiw__sbm(*arr) * itemsize + sizeof(int) * 2) : 0, itemsize * m + sizeof(int) * 2);
  18152. STBIW_ASSERT(p);
  18153. if (p)
  18154. {
  18155. if (!*arr) ((int *)p)[1] = 0;
  18156. *arr = (void *)((int *)p + 2);
  18157. stbiw__sbm(*arr) = m;
  18158. }
  18159. return *arr;
  18160. }
  18161. static unsigned char *stbiw__zlib_flushf(unsigned char *data, unsigned int *bitbuffer, int *bitcount)
  18162. {
  18163. while (*bitcount >= 8)
  18164. {
  18165. stbiw__sbpush(data, STBIW_UCHAR(*bitbuffer));
  18166. *bitbuffer >>= 8;
  18167. *bitcount -= 8;
  18168. }
  18169. return data;
  18170. }
  18171. static int stbiw__zlib_bitrev(int code, int codebits)
  18172. {
  18173. int res = 0;
  18174. while (codebits--)
  18175. {
  18176. res = (res << 1) | (code & 1);
  18177. code >>= 1;
  18178. }
  18179. return res;
  18180. }
  18181. static unsigned int stbiw__zlib_countm(unsigned char *a, unsigned char *b, int limit)
  18182. {
  18183. int i;
  18184. for (i = 0; i < limit && i < 258; ++i)
  18185. if (a[i] != b[i]) break;
  18186. return i;
  18187. }
  18188. static unsigned int stbiw__zhash(unsigned char *data)
  18189. {
  18190. stbiw_uint32 hash = data[0] + (data[1] << 8) + (data[2] << 16);
  18191. hash ^= hash << 3;
  18192. hash += hash >> 5;
  18193. hash ^= hash << 4;
  18194. hash += hash >> 17;
  18195. hash ^= hash << 25;
  18196. hash += hash >> 6;
  18197. return hash;
  18198. }
  18199. #define stbiw__zlib_flush() (out = stbiw__zlib_flushf(out, &bitbuf, &bitcount))
  18200. #define stbiw__zlib_add(code,codebits) \
  18201. (bitbuf |= (code) << bitcount, bitcount += (codebits), stbiw__zlib_flush())
  18202. #define stbiw__zlib_huffa(b,c) stbiw__zlib_add(stbiw__zlib_bitrev(b,c),c)
  18203. // default huffman tables
  18204. #define stbiw__zlib_huff1(n) stbiw__zlib_huffa(0x30 + (n), 8)
  18205. #define stbiw__zlib_huff2(n) stbiw__zlib_huffa(0x190 + (n)-144, 9)
  18206. #define stbiw__zlib_huff3(n) stbiw__zlib_huffa(0 + (n)-256,7)
  18207. #define stbiw__zlib_huff4(n) stbiw__zlib_huffa(0xc0 + (n)-280,8)
  18208. #define stbiw__zlib_huff(n) ((n) <= 143 ? stbiw__zlib_huff1(n) : (n) <= 255 ? stbiw__zlib_huff2(n) : (n) <= 279 ? stbiw__zlib_huff3(n) : stbiw__zlib_huff4(n))
  18209. #define stbiw__zlib_huffb(n) ((n) <= 143 ? stbiw__zlib_huff1(n) : stbiw__zlib_huff2(n))
  18210. #define stbiw__ZHASH 16384
  18211. #endif // STBIW_ZLIB_COMPRESS
  18212. STBIWDEF unsigned char *stbi_zlib_compress(unsigned char *data, int data_len, int *out_len, int quality)
  18213. {
  18214. #ifdef STBIW_ZLIB_COMPRESS
  18215. // user provided a zlib compress implementation, use that
  18216. return STBIW_ZLIB_COMPRESS(data, data_len, out_len, quality);
  18217. #else // use builtin
  18218. static unsigned short lengthc[] = { 3,4,5,6,7,8,9,10,11,13,15,17,19,23,27,31,35,43,51,59,67,83,99,115,131,163,195,227,258, 259 };
  18219. static unsigned char lengtheb[] = { 0,0,0,0,0,0,0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0 };
  18220. static unsigned short distc[] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577, 32768 };
  18221. static unsigned char disteb[] = { 0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13 };
  18222. unsigned int bitbuf = 0;
  18223. int i, j, bitcount = 0;
  18224. unsigned char *out = NULL;
  18225. unsigned char ***hash_table = (unsigned char ***)STBIW_MALLOC(stbiw__ZHASH * sizeof(char **));
  18226. if (hash_table == NULL)
  18227. return NULL;
  18228. if (quality < 5) quality = 5;
  18229. stbiw__sbpush(out, 0x78); // DEFLATE 32K window
  18230. stbiw__sbpush(out, 0x5e); // FLEVEL = 1
  18231. stbiw__zlib_add(1, 1); // BFINAL = 1
  18232. stbiw__zlib_add(1, 2); // BTYPE = 1 -- fixed huffman
  18233. for (i = 0; i < stbiw__ZHASH; ++i)
  18234. hash_table[i] = NULL;
  18235. i = 0;
  18236. while (i < data_len - 3)
  18237. {
  18238. // hash next 3 bytes of data to be compressed
  18239. int h = stbiw__zhash(data + i) & (stbiw__ZHASH - 1), best = 3;
  18240. unsigned char *bestloc = 0;
  18241. unsigned char **hlist = hash_table[h];
  18242. int n = stbiw__sbcount(hlist);
  18243. for (j = 0; j < n; ++j)
  18244. {
  18245. if (hlist[j] - data > i - 32768)
  18246. { // if entry lies within window
  18247. int d = stbiw__zlib_countm(hlist[j], data + i, data_len - i);
  18248. if (d >= best) best = d, bestloc = hlist[j];
  18249. }
  18250. }
  18251. // when hash table entry is too long, delete half the entries
  18252. if (hash_table[h] && stbiw__sbn(hash_table[h]) == 2 * quality)
  18253. {
  18254. STBIW_MEMMOVE(hash_table[h], hash_table[h] + quality, sizeof(hash_table[h][0]) * quality);
  18255. stbiw__sbn(hash_table[h]) = quality;
  18256. }
  18257. stbiw__sbpush(hash_table[h], data + i);
  18258. if (bestloc)
  18259. {
  18260. // "lazy matching" - check match at *next* byte, and if it's better, do cur byte as literal
  18261. h = stbiw__zhash(data + i + 1) & (stbiw__ZHASH - 1);
  18262. hlist = hash_table[h];
  18263. n = stbiw__sbcount(hlist);
  18264. for (j = 0; j < n; ++j)
  18265. {
  18266. if (hlist[j] - data > i - 32767)
  18267. {
  18268. int e = stbiw__zlib_countm(hlist[j], data + i + 1, data_len - i - 1);
  18269. if (e > best)
  18270. { // if next match is better, bail on current match
  18271. bestloc = NULL;
  18272. break;
  18273. }
  18274. }
  18275. }
  18276. }
  18277. if (bestloc)
  18278. {
  18279. int d = (int)(data + i - bestloc); // distance back
  18280. STBIW_ASSERT(d <= 32767 && best <= 258);
  18281. for (j = 0; best > lengthc[j + 1] - 1; ++j);
  18282. stbiw__zlib_huff(j + 257);
  18283. if (lengtheb[j]) stbiw__zlib_add(best - lengthc[j], lengtheb[j]);
  18284. for (j = 0; d > distc[j + 1] - 1; ++j);
  18285. stbiw__zlib_add(stbiw__zlib_bitrev(j, 5), 5);
  18286. if (disteb[j]) stbiw__zlib_add(d - distc[j], disteb[j]);
  18287. i += best;
  18288. }
  18289. else
  18290. {
  18291. stbiw__zlib_huffb(data[i]);
  18292. ++i;
  18293. }
  18294. }
  18295. // write out final bytes
  18296. for (; i < data_len; ++i)
  18297. stbiw__zlib_huffb(data[i]);
  18298. stbiw__zlib_huff(256); // end of block
  18299. // pad with 0 bits to byte boundary
  18300. while (bitcount)
  18301. stbiw__zlib_add(0, 1);
  18302. for (i = 0; i < stbiw__ZHASH; ++i)
  18303. (void)stbiw__sbfree(hash_table[i]);
  18304. STBIW_FREE(hash_table);
  18305. {
  18306. // compute adler32 on input
  18307. unsigned int s1 = 1, s2 = 0;
  18308. int blocklen = (int)(data_len % 5552);
  18309. j = 0;
  18310. while (j < data_len)
  18311. {
  18312. for (i = 0; i < blocklen; ++i) s1 += data[j + i], s2 += s1;
  18313. s1 %= 65521, s2 %= 65521;
  18314. j += blocklen;
  18315. blocklen = 5552;
  18316. }
  18317. stbiw__sbpush(out, STBIW_UCHAR(s2 >> 8));
  18318. stbiw__sbpush(out, STBIW_UCHAR(s2));
  18319. stbiw__sbpush(out, STBIW_UCHAR(s1 >> 8));
  18320. stbiw__sbpush(out, STBIW_UCHAR(s1));
  18321. }
  18322. *out_len = stbiw__sbn(out);
  18323. // make returned pointer freeable
  18324. STBIW_MEMMOVE(stbiw__sbraw(out), out, *out_len);
  18325. return (unsigned char *)stbiw__sbraw(out);
  18326. #endif // STBIW_ZLIB_COMPRESS
  18327. }
  18328. static unsigned int stbiw__crc32(unsigned char *buffer, int len)
  18329. {
  18330. #ifdef STBIW_CRC32
  18331. return STBIW_CRC32(buffer, len);
  18332. #else
  18333. static unsigned int crc_table[256] =
  18334. {
  18335. 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
  18336. 0x0eDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91,
  18337. 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
  18338. 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5,
  18339. 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B,
  18340. 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
  18341. 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F,
  18342. 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D,
  18343. 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
  18344. 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01,
  18345. 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457,
  18346. 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
  18347. 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB,
  18348. 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9,
  18349. 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
  18350. 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD,
  18351. 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683,
  18352. 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
  18353. 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7,
  18354. 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5,
  18355. 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
  18356. 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79,
  18357. 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F,
  18358. 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
  18359. 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713,
  18360. 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21,
  18361. 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
  18362. 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45,
  18363. 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB,
  18364. 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
  18365. 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF,
  18366. 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D
  18367. };
  18368. unsigned int crc = ~0u;
  18369. int i;
  18370. for (i = 0; i < len; ++i)
  18371. crc = (crc >> 8) ^ crc_table[buffer[i] ^ (crc & 0xff)];
  18372. return ~crc;
  18373. #endif
  18374. }
  18375. #define stbiw__wpng4(o,a,b,c,d) ((o)[0]=STBIW_UCHAR(a),(o)[1]=STBIW_UCHAR(b),(o)[2]=STBIW_UCHAR(c),(o)[3]=STBIW_UCHAR(d),(o)+=4)
  18376. #define stbiw__wp32(data,v) stbiw__wpng4(data, (v)>>24,(v)>>16,(v)>>8,(v));
  18377. #define stbiw__wptag(data,s) stbiw__wpng4(data, s[0],s[1],s[2],s[3])
  18378. static void stbiw__wpcrc(unsigned char **data, int len)
  18379. {
  18380. unsigned int crc = stbiw__crc32(*data - len - 4, len + 4);
  18381. stbiw__wp32(*data, crc);
  18382. }
  18383. static unsigned char stbiw__paeth(int a, int b, int c)
  18384. {
  18385. int p = a + b - c, pa = abs(p - a), pb = abs(p - b), pc = abs(p - c);
  18386. if (pa <= pb && pa <= pc) return STBIW_UCHAR(a);
  18387. if (pb <= pc) return STBIW_UCHAR(b);
  18388. return STBIW_UCHAR(c);
  18389. }
  18390. // @OPTIMIZE: provide an option that always forces left-predict or paeth predict
  18391. static void stbiw__encode_png_line(unsigned char *pixels, int stride_bytes, int width, int height, int y, int n, int filter_type, signed char *line_buffer)
  18392. {
  18393. static int mapping[] = { 0,1,2,3,4 };
  18394. static int firstmap[] = { 0,1,0,5,6 };
  18395. int *mymap = (y != 0) ? mapping : firstmap;
  18396. int i;
  18397. int type = mymap[filter_type];
  18398. unsigned char *z = pixels + stride_bytes * (stbi__flip_vertically_on_write ? height - 1 - y : y);
  18399. int signed_stride = stbi__flip_vertically_on_write ? -stride_bytes : stride_bytes;
  18400. if (type == 0)
  18401. {
  18402. memcpy(line_buffer, z, width * n);
  18403. return;
  18404. }
  18405. // first loop isn't optimized since it's just one pixel
  18406. for (i = 0; i < n; ++i)
  18407. {
  18408. switch (type)
  18409. {
  18410. case 1: line_buffer[i] = z[i]; break;
  18411. case 2: line_buffer[i] = z[i] - z[i - signed_stride]; break;
  18412. case 3: line_buffer[i] = z[i] - (z[i - signed_stride] >> 1); break;
  18413. case 4: line_buffer[i] = (signed char)(z[i] - stbiw__paeth(0, z[i - signed_stride], 0)); break;
  18414. case 5: line_buffer[i] = z[i]; break;
  18415. case 6: line_buffer[i] = z[i]; break;
  18416. }
  18417. }
  18418. switch (type)
  18419. {
  18420. case 1: for (i = n; i < width * n; ++i) line_buffer[i] = z[i] - z[i - n]; break;
  18421. case 2: for (i = n; i < width * n; ++i) line_buffer[i] = z[i] - z[i - signed_stride]; break;
  18422. case 3: for (i = n; i < width * n; ++i) line_buffer[i] = z[i] - ((z[i - n] + z[i - signed_stride]) >> 1); break;
  18423. case 4: for (i = n; i < width * n; ++i) line_buffer[i] = z[i] - stbiw__paeth(z[i - n], z[i - signed_stride], z[i - signed_stride - n]); break;
  18424. case 5: for (i = n; i < width * n; ++i) line_buffer[i] = z[i] - (z[i - n] >> 1); break;
  18425. case 6: for (i = n; i < width * n; ++i) line_buffer[i] = z[i] - stbiw__paeth(z[i - n], 0, 0); break;
  18426. }
  18427. }
  18428. STBIWDEF unsigned char *stbi_write_png_to_mem(const unsigned char *pixels, int stride_bytes, int x, int y, int n, int *out_len)
  18429. {
  18430. int force_filter = stbi_write_force_png_filter;
  18431. int ctype[5] = { -1, 0, 4, 2, 6 };
  18432. unsigned char sig[8] = { 137,80,78,71,13,10,26,10 };
  18433. unsigned char *out, *o, *filt, *zlib;
  18434. signed char *line_buffer;
  18435. int j, zlen;
  18436. if (stride_bytes == 0)
  18437. stride_bytes = x * n;
  18438. if (force_filter >= 5)
  18439. {
  18440. force_filter = -1;
  18441. }
  18442. filt = (unsigned char *)STBIW_MALLOC((x * n + 1) * y); if (!filt) return 0;
  18443. line_buffer = (signed char *)STBIW_MALLOC(x * n); if (!line_buffer) { STBIW_FREE(filt); return 0; }
  18444. for (j = 0; j < y; ++j)
  18445. {
  18446. int filter_type;
  18447. if (force_filter > -1)
  18448. {
  18449. filter_type = force_filter;
  18450. stbiw__encode_png_line((unsigned char *)(pixels), stride_bytes, x, y, j, n, force_filter, line_buffer);
  18451. }
  18452. else
  18453. { // Estimate the best filter by running through all of them:
  18454. int best_filter = 0, best_filter_val = 0x7fffffff, est, i;
  18455. for (filter_type = 0; filter_type < 5; filter_type++)
  18456. {
  18457. stbiw__encode_png_line((unsigned char *)(pixels), stride_bytes, x, y, j, n, filter_type, line_buffer);
  18458. // Estimate the entropy of the line using this filter; the less, the better.
  18459. est = 0;
  18460. for (i = 0; i < x * n; ++i)
  18461. {
  18462. est += abs((signed char)line_buffer[i]);
  18463. }
  18464. if (est < best_filter_val)
  18465. {
  18466. best_filter_val = est;
  18467. best_filter = filter_type;
  18468. }
  18469. }
  18470. if (filter_type != best_filter)
  18471. { // If the last iteration already got us the best filter, don't redo it
  18472. stbiw__encode_png_line((unsigned char *)(pixels), stride_bytes, x, y, j, n, best_filter, line_buffer);
  18473. filter_type = best_filter;
  18474. }
  18475. }
  18476. // when we get here, filter_type contains the filter type, and line_buffer contains the data
  18477. filt[j * (x * n + 1)] = (unsigned char)filter_type;
  18478. STBIW_MEMMOVE(filt + j * (x * n + 1) + 1, line_buffer, x * n);
  18479. }
  18480. STBIW_FREE(line_buffer);
  18481. zlib = stbi_zlib_compress(filt, y * (x * n + 1), &zlen, stbi_write_png_compression_level);
  18482. STBIW_FREE(filt);
  18483. if (!zlib) return 0;
  18484. // each tag requires 12 bytes of overhead
  18485. out = (unsigned char *)STBIW_MALLOC(8 + 12 + 13 + 12 + zlen + 12);
  18486. if (!out) return 0;
  18487. *out_len = 8 + 12 + 13 + 12 + zlen + 12;
  18488. o = out;
  18489. STBIW_MEMMOVE(o, sig, 8); o += 8;
  18490. stbiw__wp32(o, 13); // header length
  18491. stbiw__wptag(o, "IHDR");
  18492. stbiw__wp32(o, x);
  18493. stbiw__wp32(o, y);
  18494. *o++ = 8;
  18495. *o++ = STBIW_UCHAR(ctype[n]);
  18496. *o++ = 0;
  18497. *o++ = 0;
  18498. *o++ = 0;
  18499. stbiw__wpcrc(&o, 13);
  18500. stbiw__wp32(o, zlen);
  18501. stbiw__wptag(o, "IDAT");
  18502. STBIW_MEMMOVE(o, zlib, zlen);
  18503. o += zlen;
  18504. STBIW_FREE(zlib);
  18505. stbiw__wpcrc(&o, zlen);
  18506. stbiw__wp32(o, 0);
  18507. stbiw__wptag(o, "IEND");
  18508. stbiw__wpcrc(&o, 0);
  18509. STBIW_ASSERT(o == out + *out_len);
  18510. return out;
  18511. }
  18512. #ifndef STBI_WRITE_NO_STDIO
  18513. STBIWDEF int stbi_write_png(char const *filename, int x, int y, int comp, const void *data, int stride_bytes)
  18514. {
  18515. FILE *f;
  18516. int len;
  18517. unsigned char *png = stbi_write_png_to_mem((const unsigned char *)data, stride_bytes, x, y, comp, &len);
  18518. if (png == NULL) return 0;
  18519. f = stbiw__fopen(filename, "wb");
  18520. if (!f) { STBIW_FREE(png); return 0; }
  18521. fwrite(png, 1, len, f);
  18522. fclose(f);
  18523. STBIW_FREE(png);
  18524. return 1;
  18525. }
  18526. #endif
  18527. STBIWDEF int stbi_write_png_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int stride_bytes)
  18528. {
  18529. int len;
  18530. unsigned char *png = stbi_write_png_to_mem((const unsigned char *)data, stride_bytes, x, y, comp, &len);
  18531. if (png == NULL) return 0;
  18532. func(context, png, len);
  18533. STBIW_FREE(png);
  18534. return 1;
  18535. }
  18536. /* ***************************************************************************
  18537. *
  18538. * JPEG writer
  18539. *
  18540. * This is based on Jon Olick's jo_jpeg.cpp:
  18541. * public domain Simple, Minimalistic JPEG writer - http://www.jonolick.com/code.html
  18542. */
  18543. static const unsigned char stbiw__jpg_ZigZag[] = { 0,1,5,6,14,15,27,28,2,4,7,13,16,26,29,42,3,8,12,17,25,30,41,43,9,11,18,
  18544. 24,31,40,44,53,10,19,23,32,39,45,52,54,20,22,33,38,46,51,55,60,21,34,37,47,50,56,59,61,35,36,48,49,57,58,62,63 };
  18545. static void stbiw__jpg_writeBits(stbi__write_context *s, int *bitBufP, int *bitCntP, const unsigned short *bs)
  18546. {
  18547. int bitBuf = *bitBufP, bitCnt = *bitCntP;
  18548. bitCnt += bs[1];
  18549. bitBuf |= bs[0] << (24 - bitCnt);
  18550. while (bitCnt >= 8)
  18551. {
  18552. unsigned char c = (bitBuf >> 16) & 255;
  18553. stbiw__putc(s, c);
  18554. if (c == 255)
  18555. {
  18556. stbiw__putc(s, 0);
  18557. }
  18558. bitBuf <<= 8;
  18559. bitCnt -= 8;
  18560. }
  18561. *bitBufP = bitBuf;
  18562. *bitCntP = bitCnt;
  18563. }
  18564. static void stbiw__jpg_DCT(float *d0p, float *d1p, float *d2p, float *d3p, float *d4p, float *d5p, float *d6p, float *d7p)
  18565. {
  18566. float d0 = *d0p, d1 = *d1p, d2 = *d2p, d3 = *d3p, d4 = *d4p, d5 = *d5p, d6 = *d6p, d7 = *d7p;
  18567. float z1, z2, z3, z4, z5, z11, z13;
  18568. float tmp0 = d0 + d7;
  18569. float tmp7 = d0 - d7;
  18570. float tmp1 = d1 + d6;
  18571. float tmp6 = d1 - d6;
  18572. float tmp2 = d2 + d5;
  18573. float tmp5 = d2 - d5;
  18574. float tmp3 = d3 + d4;
  18575. float tmp4 = d3 - d4;
  18576. // Even part
  18577. float tmp10 = tmp0 + tmp3; // phase 2
  18578. float tmp13 = tmp0 - tmp3;
  18579. float tmp11 = tmp1 + tmp2;
  18580. float tmp12 = tmp1 - tmp2;
  18581. d0 = tmp10 + tmp11; // phase 3
  18582. d4 = tmp10 - tmp11;
  18583. z1 = (tmp12 + tmp13) * 0.707106781f; // c4
  18584. d2 = tmp13 + z1; // phase 5
  18585. d6 = tmp13 - z1;
  18586. // Odd part
  18587. tmp10 = tmp4 + tmp5; // phase 2
  18588. tmp11 = tmp5 + tmp6;
  18589. tmp12 = tmp6 + tmp7;
  18590. // The rotator is modified from fig 4-8 to avoid extra negations.
  18591. z5 = (tmp10 - tmp12) * 0.382683433f; // c6
  18592. z2 = tmp10 * 0.541196100f + z5; // c2-c6
  18593. z4 = tmp12 * 1.306562965f + z5; // c2+c6
  18594. z3 = tmp11 * 0.707106781f; // c4
  18595. z11 = tmp7 + z3; // phase 5
  18596. z13 = tmp7 - z3;
  18597. *d5p = z13 + z2; // phase 6
  18598. *d3p = z13 - z2;
  18599. *d1p = z11 + z4;
  18600. *d7p = z11 - z4;
  18601. *d0p = d0; *d2p = d2; *d4p = d4; *d6p = d6;
  18602. }
  18603. static void stbiw__jpg_calcBits(int val, unsigned short bits[2])
  18604. {
  18605. int tmp1 = val < 0 ? -val : val;
  18606. val = val < 0 ? val - 1 : val;
  18607. bits[1] = 1;
  18608. while (tmp1 >>= 1)
  18609. {
  18610. ++bits[1];
  18611. }
  18612. bits[0] = val & ((1 << bits[1]) - 1);
  18613. }
  18614. static int stbiw__jpg_processDU(stbi__write_context *s, int *bitBuf, int *bitCnt, float *CDU, float *fdtbl, int DC, const unsigned short HTDC[256][2], const unsigned short HTAC[256][2])
  18615. {
  18616. const unsigned short EOB[2] = { HTAC[0x00][0], HTAC[0x00][1] };
  18617. const unsigned short M16zeroes[2] = { HTAC[0xF0][0], HTAC[0xF0][1] };
  18618. int dataOff, i, diff, end0pos;
  18619. int DU[64];
  18620. // DCT rows
  18621. for (dataOff = 0; dataOff < 64; dataOff += 8)
  18622. {
  18623. stbiw__jpg_DCT(&CDU[dataOff], &CDU[dataOff + 1], &CDU[dataOff + 2], &CDU[dataOff + 3], &CDU[dataOff + 4], &CDU[dataOff + 5], &CDU[dataOff + 6], &CDU[dataOff + 7]);
  18624. }
  18625. // DCT columns
  18626. for (dataOff = 0; dataOff < 8; ++dataOff)
  18627. {
  18628. stbiw__jpg_DCT(&CDU[dataOff], &CDU[dataOff + 8], &CDU[dataOff + 16], &CDU[dataOff + 24], &CDU[dataOff + 32], &CDU[dataOff + 40], &CDU[dataOff + 48], &CDU[dataOff + 56]);
  18629. }
  18630. // Quantize/descale/zigzag the coefficients
  18631. for (i = 0; i < 64; ++i)
  18632. {
  18633. float v = CDU[i] * fdtbl[i];
  18634. // DU[stbiw__jpg_ZigZag[i]] = (int)(v < 0 ? ceilf(v - 0.5f) : floorf(v + 0.5f));
  18635. // ceilf() and floorf() are C99, not C89, but I /think/ they're not needed here anyway?
  18636. DU[stbiw__jpg_ZigZag[i]] = (int)(v < 0 ? v - 0.5f : v + 0.5f);
  18637. }
  18638. // Encode DC
  18639. diff = DU[0] - DC;
  18640. if (diff == 0)
  18641. {
  18642. stbiw__jpg_writeBits(s, bitBuf, bitCnt, HTDC[0]);
  18643. }
  18644. else
  18645. {
  18646. unsigned short bits[2];
  18647. stbiw__jpg_calcBits(diff, bits);
  18648. stbiw__jpg_writeBits(s, bitBuf, bitCnt, HTDC[bits[1]]);
  18649. stbiw__jpg_writeBits(s, bitBuf, bitCnt, bits);
  18650. }
  18651. // Encode ACs
  18652. end0pos = 63;
  18653. for (; (end0pos > 0) && (DU[end0pos] == 0); --end0pos)
  18654. {
  18655. }
  18656. // end0pos = first element in reverse order !=0
  18657. if (end0pos == 0)
  18658. {
  18659. stbiw__jpg_writeBits(s, bitBuf, bitCnt, EOB);
  18660. return DU[0];
  18661. }
  18662. for (i = 1; i <= end0pos; ++i)
  18663. {
  18664. int startpos = i;
  18665. int nrzeroes;
  18666. unsigned short bits[2];
  18667. for (; DU[i] == 0 && i <= end0pos; ++i)
  18668. {
  18669. }
  18670. nrzeroes = i - startpos;
  18671. if (nrzeroes >= 16)
  18672. {
  18673. int lng = nrzeroes >> 4;
  18674. int nrmarker;
  18675. for (nrmarker = 1; nrmarker <= lng; ++nrmarker)
  18676. stbiw__jpg_writeBits(s, bitBuf, bitCnt, M16zeroes);
  18677. nrzeroes &= 15;
  18678. }
  18679. stbiw__jpg_calcBits(DU[i], bits);
  18680. stbiw__jpg_writeBits(s, bitBuf, bitCnt, HTAC[(nrzeroes << 4) + bits[1]]);
  18681. stbiw__jpg_writeBits(s, bitBuf, bitCnt, bits);
  18682. }
  18683. if (end0pos != 63)
  18684. {
  18685. stbiw__jpg_writeBits(s, bitBuf, bitCnt, EOB);
  18686. }
  18687. return DU[0];
  18688. }
  18689. static int stbi_write_jpg_core(stbi__write_context *s, int width, int height, int comp, const void *data, int quality)
  18690. {
  18691. // Constants that don't pollute global namespace
  18692. static const unsigned char std_dc_luminance_nrcodes[] = { 0,0,1,5,1,1,1,1,1,1,0,0,0,0,0,0,0 };
  18693. static const unsigned char std_dc_luminance_values[] = { 0,1,2,3,4,5,6,7,8,9,10,11 };
  18694. static const unsigned char std_ac_luminance_nrcodes[] = { 0,0,2,1,3,3,2,4,3,5,5,4,4,0,0,1,0x7d };
  18695. static const unsigned char std_ac_luminance_values[] = {
  18696. 0x01,0x02,0x03,0x00,0x04,0x11,0x05,0x12,0x21,0x31,0x41,0x06,0x13,0x51,0x61,0x07,0x22,0x71,0x14,0x32,0x81,0x91,0xa1,0x08,
  18697. 0x23,0x42,0xb1,0xc1,0x15,0x52,0xd1,0xf0,0x24,0x33,0x62,0x72,0x82,0x09,0x0a,0x16,0x17,0x18,0x19,0x1a,0x25,0x26,0x27,0x28,
  18698. 0x29,0x2a,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x53,0x54,0x55,0x56,0x57,0x58,0x59,
  18699. 0x5a,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x83,0x84,0x85,0x86,0x87,0x88,0x89,
  18700. 0x8a,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xb2,0xb3,0xb4,0xb5,0xb6,
  18701. 0xb7,0xb8,0xb9,0xba,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xe1,0xe2,
  18702. 0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa
  18703. };
  18704. static const unsigned char std_dc_chrominance_nrcodes[] = { 0,0,3,1,1,1,1,1,1,1,1,1,0,0,0,0,0 };
  18705. static const unsigned char std_dc_chrominance_values[] = { 0,1,2,3,4,5,6,7,8,9,10,11 };
  18706. static const unsigned char std_ac_chrominance_nrcodes[] = { 0,0,2,1,2,4,4,3,4,7,5,4,4,0,1,2,0x77 };
  18707. static const unsigned char std_ac_chrominance_values[] = {
  18708. 0x00,0x01,0x02,0x03,0x11,0x04,0x05,0x21,0x31,0x06,0x12,0x41,0x51,0x07,0x61,0x71,0x13,0x22,0x32,0x81,0x08,0x14,0x42,0x91,
  18709. 0xa1,0xb1,0xc1,0x09,0x23,0x33,0x52,0xf0,0x15,0x62,0x72,0xd1,0x0a,0x16,0x24,0x34,0xe1,0x25,0xf1,0x17,0x18,0x19,0x1a,0x26,
  18710. 0x27,0x28,0x29,0x2a,0x35,0x36,0x37,0x38,0x39,0x3a,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x53,0x54,0x55,0x56,0x57,0x58,
  18711. 0x59,0x5a,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x82,0x83,0x84,0x85,0x86,0x87,
  18712. 0x88,0x89,0x8a,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xb2,0xb3,0xb4,
  18713. 0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,
  18714. 0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa
  18715. };
  18716. // Huffman tables
  18717. static const unsigned short YDC_HT[256][2] = { {0,2},{2,3},{3,3},{4,3},{5,3},{6,3},{14,4},{30,5},{62,6},{126,7},{254,8},{510,9} };
  18718. static const unsigned short UVDC_HT[256][2] = { {0,2},{1,2},{2,2},{6,3},{14,4},{30,5},{62,6},{126,7},{254,8},{510,9},{1022,10},{2046,11} };
  18719. static const unsigned short YAC_HT[256][2] = {
  18720. {10,4},{0,2},{1,2},{4,3},{11,4},{26,5},{120,7},{248,8},{1014,10},{65410,16},{65411,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
  18721. {12,4},{27,5},{121,7},{502,9},{2038,11},{65412,16},{65413,16},{65414,16},{65415,16},{65416,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
  18722. {28,5},{249,8},{1015,10},{4084,12},{65417,16},{65418,16},{65419,16},{65420,16},{65421,16},{65422,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
  18723. {58,6},{503,9},{4085,12},{65423,16},{65424,16},{65425,16},{65426,16},{65427,16},{65428,16},{65429,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
  18724. {59,6},{1016,10},{65430,16},{65431,16},{65432,16},{65433,16},{65434,16},{65435,16},{65436,16},{65437,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
  18725. {122,7},{2039,11},{65438,16},{65439,16},{65440,16},{65441,16},{65442,16},{65443,16},{65444,16},{65445,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
  18726. {123,7},{4086,12},{65446,16},{65447,16},{65448,16},{65449,16},{65450,16},{65451,16},{65452,16},{65453,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
  18727. {250,8},{4087,12},{65454,16},{65455,16},{65456,16},{65457,16},{65458,16},{65459,16},{65460,16},{65461,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
  18728. {504,9},{32704,15},{65462,16},{65463,16},{65464,16},{65465,16},{65466,16},{65467,16},{65468,16},{65469,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
  18729. {505,9},{65470,16},{65471,16},{65472,16},{65473,16},{65474,16},{65475,16},{65476,16},{65477,16},{65478,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
  18730. {506,9},{65479,16},{65480,16},{65481,16},{65482,16},{65483,16},{65484,16},{65485,16},{65486,16},{65487,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
  18731. {1017,10},{65488,16},{65489,16},{65490,16},{65491,16},{65492,16},{65493,16},{65494,16},{65495,16},{65496,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
  18732. {1018,10},{65497,16},{65498,16},{65499,16},{65500,16},{65501,16},{65502,16},{65503,16},{65504,16},{65505,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
  18733. {2040,11},{65506,16},{65507,16},{65508,16},{65509,16},{65510,16},{65511,16},{65512,16},{65513,16},{65514,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
  18734. {65515,16},{65516,16},{65517,16},{65518,16},{65519,16},{65520,16},{65521,16},{65522,16},{65523,16},{65524,16},{0,0},{0,0},{0,0},{0,0},{0,0},
  18735. {2041,11},{65525,16},{65526,16},{65527,16},{65528,16},{65529,16},{65530,16},{65531,16},{65532,16},{65533,16},{65534,16},{0,0},{0,0},{0,0},{0,0},{0,0}
  18736. };
  18737. static const unsigned short UVAC_HT[256][2] = {
  18738. {0,2},{1,2},{4,3},{10,4},{24,5},{25,5},{56,6},{120,7},{500,9},{1014,10},{4084,12},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
  18739. {11,4},{57,6},{246,8},{501,9},{2038,11},{4085,12},{65416,16},{65417,16},{65418,16},{65419,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
  18740. {26,5},{247,8},{1015,10},{4086,12},{32706,15},{65420,16},{65421,16},{65422,16},{65423,16},{65424,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
  18741. {27,5},{248,8},{1016,10},{4087,12},{65425,16},{65426,16},{65427,16},{65428,16},{65429,16},{65430,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
  18742. {58,6},{502,9},{65431,16},{65432,16},{65433,16},{65434,16},{65435,16},{65436,16},{65437,16},{65438,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
  18743. {59,6},{1017,10},{65439,16},{65440,16},{65441,16},{65442,16},{65443,16},{65444,16},{65445,16},{65446,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
  18744. {121,7},{2039,11},{65447,16},{65448,16},{65449,16},{65450,16},{65451,16},{65452,16},{65453,16},{65454,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
  18745. {122,7},{2040,11},{65455,16},{65456,16},{65457,16},{65458,16},{65459,16},{65460,16},{65461,16},{65462,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
  18746. {249,8},{65463,16},{65464,16},{65465,16},{65466,16},{65467,16},{65468,16},{65469,16},{65470,16},{65471,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
  18747. {503,9},{65472,16},{65473,16},{65474,16},{65475,16},{65476,16},{65477,16},{65478,16},{65479,16},{65480,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
  18748. {504,9},{65481,16},{65482,16},{65483,16},{65484,16},{65485,16},{65486,16},{65487,16},{65488,16},{65489,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
  18749. {505,9},{65490,16},{65491,16},{65492,16},{65493,16},{65494,16},{65495,16},{65496,16},{65497,16},{65498,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
  18750. {506,9},{65499,16},{65500,16},{65501,16},{65502,16},{65503,16},{65504,16},{65505,16},{65506,16},{65507,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
  18751. {2041,11},{65508,16},{65509,16},{65510,16},{65511,16},{65512,16},{65513,16},{65514,16},{65515,16},{65516,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
  18752. {16352,14},{65517,16},{65518,16},{65519,16},{65520,16},{65521,16},{65522,16},{65523,16},{65524,16},{65525,16},{0,0},{0,0},{0,0},{0,0},{0,0},
  18753. {1018,10},{32707,15},{65526,16},{65527,16},{65528,16},{65529,16},{65530,16},{65531,16},{65532,16},{65533,16},{65534,16},{0,0},{0,0},{0,0},{0,0},{0,0}
  18754. };
  18755. static const int YQT[] = { 16,11,10,16,24,40,51,61,12,12,14,19,26,58,60,55,14,13,16,24,40,57,69,56,14,17,22,29,51,87,80,62,18,22,
  18756. 37,56,68,109,103,77,24,35,55,64,81,104,113,92,49,64,78,87,103,121,120,101,72,92,95,98,112,100,103,99 };
  18757. static const int UVQT[] = { 17,18,24,47,99,99,99,99,18,21,26,66,99,99,99,99,24,26,56,99,99,99,99,99,47,66,99,99,99,99,99,99,
  18758. 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99 };
  18759. static const float aasf[] = { 1.0f * 2.828427125f, 1.387039845f * 2.828427125f, 1.306562965f * 2.828427125f, 1.175875602f * 2.828427125f,
  18760. 1.0f * 2.828427125f, 0.785694958f * 2.828427125f, 0.541196100f * 2.828427125f, 0.275899379f * 2.828427125f };
  18761. int row, col, i, k;
  18762. float fdtbl_Y[64], fdtbl_UV[64];
  18763. unsigned char YTable[64], UVTable[64];
  18764. if (!data || !width || !height || comp > 4 || comp < 1)
  18765. {
  18766. return 0;
  18767. }
  18768. quality = quality ? quality : 90;
  18769. quality = quality < 1 ? 1 : quality > 100 ? 100 : quality;
  18770. quality = quality < 50 ? 5000 / quality : 200 - quality * 2;
  18771. for (i = 0; i < 64; ++i)
  18772. {
  18773. int uvti, yti = (YQT[i] * quality + 50) / 100;
  18774. YTable[stbiw__jpg_ZigZag[i]] = (unsigned char)(yti < 1 ? 1 : yti > 255 ? 255 : yti);
  18775. uvti = (UVQT[i] * quality + 50) / 100;
  18776. UVTable[stbiw__jpg_ZigZag[i]] = (unsigned char)(uvti < 1 ? 1 : uvti > 255 ? 255 : uvti);
  18777. }
  18778. for (row = 0, k = 0; row < 8; ++row)
  18779. {
  18780. for (col = 0; col < 8; ++col, ++k)
  18781. {
  18782. fdtbl_Y[k] = 1 / (YTable[stbiw__jpg_ZigZag[k]] * aasf[row] * aasf[col]);
  18783. fdtbl_UV[k] = 1 / (UVTable[stbiw__jpg_ZigZag[k]] * aasf[row] * aasf[col]);
  18784. }
  18785. }
  18786. // Write Headers
  18787. {
  18788. static const unsigned char head0[] = { 0xFF,0xD8,0xFF,0xE0,0,0x10,'J','F','I','F',0,1,1,0,0,1,0,1,0,0,0xFF,0xDB,0,0x84,0 };
  18789. static const unsigned char head2[] = { 0xFF,0xDA,0,0xC,3,1,0,2,0x11,3,0x11,0,0x3F,0 };
  18790. const unsigned char head1[] = { 0xFF,0xC0,0,0x11,8,(unsigned char)(height >> 8),STBIW_UCHAR(height),(unsigned char)(width >> 8),STBIW_UCHAR(width),
  18791. 3,1,0x11,0,2,0x11,1,3,0x11,1,0xFF,0xC4,0x01,0xA2,0 };
  18792. s->func(s->context, (void *)head0, sizeof(head0));
  18793. s->func(s->context, (void *)YTable, sizeof(YTable));
  18794. stbiw__putc(s, 1);
  18795. s->func(s->context, UVTable, sizeof(UVTable));
  18796. s->func(s->context, (void *)head1, sizeof(head1));
  18797. s->func(s->context, (void *)(std_dc_luminance_nrcodes + 1), sizeof(std_dc_luminance_nrcodes) - 1);
  18798. s->func(s->context, (void *)std_dc_luminance_values, sizeof(std_dc_luminance_values));
  18799. stbiw__putc(s, 0x10); // HTYACinfo
  18800. s->func(s->context, (void *)(std_ac_luminance_nrcodes + 1), sizeof(std_ac_luminance_nrcodes) - 1);
  18801. s->func(s->context, (void *)std_ac_luminance_values, sizeof(std_ac_luminance_values));
  18802. stbiw__putc(s, 1); // HTUDCinfo
  18803. s->func(s->context, (void *)(std_dc_chrominance_nrcodes + 1), sizeof(std_dc_chrominance_nrcodes) - 1);
  18804. s->func(s->context, (void *)std_dc_chrominance_values, sizeof(std_dc_chrominance_values));
  18805. stbiw__putc(s, 0x11); // HTUACinfo
  18806. s->func(s->context, (void *)(std_ac_chrominance_nrcodes + 1), sizeof(std_ac_chrominance_nrcodes) - 1);
  18807. s->func(s->context, (void *)std_ac_chrominance_values, sizeof(std_ac_chrominance_values));
  18808. s->func(s->context, (void *)head2, sizeof(head2));
  18809. }
  18810. // Encode 8x8 macroblocks
  18811. {
  18812. static const unsigned short fillBits[] = { 0x7F, 7 };
  18813. const unsigned char *imageData = (const unsigned char *)data;
  18814. int DCY = 0, DCU = 0, DCV = 0;
  18815. int bitBuf = 0, bitCnt = 0;
  18816. // comp == 2 is grey+alpha (alpha is ignored)
  18817. int ofsG = comp > 2 ? 1 : 0, ofsB = comp > 2 ? 2 : 0;
  18818. int x, y, pos;
  18819. for (y = 0; y < height; y += 8)
  18820. {
  18821. for (x = 0; x < width; x += 8)
  18822. {
  18823. float YDU[64], UDU[64], VDU[64];
  18824. for (row = y, pos = 0; row < y + 8; ++row)
  18825. {
  18826. // row >= height => use last input row
  18827. int clamped_row = (row < height) ? row : height - 1;
  18828. int base_p = (stbi__flip_vertically_on_write ? (height - 1 - clamped_row) : clamped_row) * width * comp;
  18829. for (col = x; col < x + 8; ++col, ++pos)
  18830. {
  18831. float r, g, b;
  18832. // if col >= width => use pixel from last input column
  18833. int p = base_p + ((col < width) ? col : (width - 1)) * comp;
  18834. r = imageData[p + 0];
  18835. g = imageData[p + ofsG];
  18836. b = imageData[p + ofsB];
  18837. YDU[pos] = +0.29900f * r + 0.58700f * g + 0.11400f * b - 128;
  18838. UDU[pos] = -0.16874f * r - 0.33126f * g + 0.50000f * b;
  18839. VDU[pos] = +0.50000f * r - 0.41869f * g - 0.08131f * b;
  18840. }
  18841. }
  18842. DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, YDU, fdtbl_Y, DCY, YDC_HT, YAC_HT);
  18843. DCU = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, UDU, fdtbl_UV, DCU, UVDC_HT, UVAC_HT);
  18844. DCV = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, VDU, fdtbl_UV, DCV, UVDC_HT, UVAC_HT);
  18845. }
  18846. }
  18847. // Do the bit alignment of the EOI marker
  18848. stbiw__jpg_writeBits(s, &bitBuf, &bitCnt, fillBits);
  18849. }
  18850. // EOI
  18851. stbiw__putc(s, 0xFF);
  18852. stbiw__putc(s, 0xD9);
  18853. return 1;
  18854. }
  18855. STBIWDEF int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality)
  18856. {
  18857. stbi__write_context s;
  18858. stbi__start_write_callbacks(&s, func, context);
  18859. return stbi_write_jpg_core(&s, x, y, comp, (void *)data, quality);
  18860. }
  18861. #ifndef STBI_WRITE_NO_STDIO
  18862. STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality)
  18863. {
  18864. stbi__write_context s;
  18865. if (stbi__start_write_file(&s, filename))
  18866. {
  18867. int r = stbi_write_jpg_core(&s, x, y, comp, data, quality);
  18868. stbi__end_write_file(&s);
  18869. return r;
  18870. }
  18871. else
  18872. return 0;
  18873. }
  18874. #endif
  18875. #endif // STB_IMAGE_WRITE_IMPLEMENTATION
  18876. /* Revision history
  18877. 1.10 (2019-02-07)
  18878. support utf8 filenames in Windows; fix warnings and platform ifdefs
  18879. 1.09 (2018-02-11)
  18880. fix typo in zlib quality API, improve STB_I_W_STATIC in C++
  18881. 1.08 (2018-01-29)
  18882. add stbi__flip_vertically_on_write, external zlib, zlib quality, choose PNG filter
  18883. 1.07 (2017-07-24)
  18884. doc fix
  18885. 1.06 (2017-07-23)
  18886. writing JPEG (using Jon Olick's code)
  18887. 1.05 ???
  18888. 1.04 (2017-03-03)
  18889. monochrome BMP expansion
  18890. 1.03 ???
  18891. 1.02 (2016-04-02)
  18892. avoid allocating large structures on the stack
  18893. 1.01 (2016-01-16)
  18894. STBIW_REALLOC_SIZED: support allocators with no realloc support
  18895. avoid race-condition in crc initialization
  18896. minor compile issues
  18897. 1.00 (2015-09-14)
  18898. installable file IO function
  18899. 0.99 (2015-09-13)
  18900. warning fixes; TGA rle support
  18901. 0.98 (2015-04-08)
  18902. added STBIW_MALLOC, STBIW_ASSERT etc
  18903. 0.97 (2015-01-18)
  18904. fixed HDR asserts, rewrote HDR rle logic
  18905. 0.96 (2015-01-17)
  18906. add HDR output
  18907. fix monochrome BMP
  18908. 0.95 (2014-08-17)
  18909. add monochrome TGA output
  18910. 0.94 (2014-05-31)
  18911. rename private functions to avoid conflicts with stb_image.h
  18912. 0.93 (2014-05-27)
  18913. warning fixes
  18914. 0.92 (2010-08-01)
  18915. casts to unsigned char to fix warnings
  18916. 0.91 (2010-07-17)
  18917. first public release
  18918. 0.90 first internal release
  18919. */
  18920. /*
  18921. ------------------------------------------------------------------------------
  18922. This software is available under 2 licenses -- choose whichever you prefer.
  18923. ------------------------------------------------------------------------------
  18924. ALTERNATIVE A - MIT License
  18925. Copyright (c) 2017 Sean Barrett
  18926. Permission is hereby granted, free of charge, to any person obtaining a copy of
  18927. this software and associated documentation files (the "Software"), to deal in
  18928. the Software without restriction, including without limitation the rights to
  18929. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  18930. of the Software, and to permit persons to whom the Software is furnished to do
  18931. so, subject to the following conditions:
  18932. The above copyright notice and this permission notice shall be included in all
  18933. copies or substantial portions of the Software.
  18934. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18935. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18936. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18937. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18938. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18939. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18940. SOFTWARE.
  18941. ------------------------------------------------------------------------------
  18942. ALTERNATIVE B - Public Domain (www.unlicense.org)
  18943. This is free and unencumbered software released into the public domain.
  18944. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
  18945. software, either in source code form or as a compiled binary, for any purpose,
  18946. commercial or non-commercial, and by any means.
  18947. In jurisdictions that recognize copyright laws, the author or authors of this
  18948. software dedicate any and all copyright interest in the software to the public
  18949. domain. We make this dedication for the benefit of the public at large and to
  18950. the detriment of our heirs and successors. We intend this dedication to be an
  18951. overt act of relinquishment in perpetuity of all present and future rights to
  18952. this software under copyright law.
  18953. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18954. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18955. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18956. AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  18957. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18958. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18959. ------------------------------------------------------------------------------
  18960. */
  18961. #pragma endregion
  18962. ////////////////////////////////////////////////
  18963. //tiny_gltf.h
  18964. ////////////////////////////////////////////////
  18965. #pragma region tiny_gltf
  18966. //
  18967. // Header-only tiny glTF 2.0 loader and serializer.
  18968. //
  18969. //
  18970. // The MIT License (MIT)
  18971. //
  18972. // Copyright (c) 2015 - 2020 Syoyo Fujita, Aurélien Chatelain and many
  18973. // contributors.
  18974. //
  18975. // Permission is hereby granted, free of charge, to any person obtaining a copy
  18976. // of this software and associated documentation files (the "Software"), to deal
  18977. // in the Software without restriction, including without limitation the rights
  18978. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  18979. // copies of the Software, and to permit persons to whom the Software is
  18980. // furnished to do so, subject to the following conditions:
  18981. //
  18982. // The above copyright notice and this permission notice shall be included in
  18983. // all copies or substantial portions of the Software.
  18984. //
  18985. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18986. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18987. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18988. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18989. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18990. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18991. // THE SOFTWARE.
  18992. // Version:
  18993. // - v2.5.0 Add SetPreserveImageChannels() option to load image data as is.
  18994. // - v2.4.3 Fix null object output when when material has all default parameters.
  18995. // - v2.4.2 Decode percent-encoded URI.
  18996. // - v2.4.1 Fix some glTF object class does not have `extensions` and/or
  18997. // `extras` property.
  18998. // - v2.4.0 Experimental RapidJSON and C++14 support(Thanks to @jrkoone).
  18999. // - v2.3.1 Set default value of minFilter and magFilter in Sampler to -1.
  19000. // - v2.3.0 Modified Material representation according to glTF 2.0 schema
  19001. // (and introduced TextureInfo class)
  19002. // Change the behavior of `Value::IsNumber`. It return true either the
  19003. // value is int or real.
  19004. // - v2.2.0 Add loading 16bit PNG support. Add Sparse accessor support(Thanks
  19005. // to @Ybalrid)
  19006. // - v2.1.0 Add draco compression.
  19007. // - v2.0.1 Add comparsion feature(Thanks to @Selmar).
  19008. // - v2.0.0 glTF 2.0!.
  19009. //
  19010. // Tiny glTF loader is using following third party libraries:
  19011. //
  19012. // - jsonhpp: C++ JSON library.
  19013. // - base64: base64 decode/encode library.
  19014. // - stb_image: Image loading library.
  19015. //
  19016. #ifndef TINY_GLTF_H_
  19017. #define TINY_GLTF_H_
  19018. #define TINYGLTF_USE_CPP14
  19019. #include <array>
  19020. #include <cassert>
  19021. #include <cmath> // std::fabs
  19022. #include <cstdint>
  19023. #include <cstdlib>
  19024. #include <cstring>
  19025. #include <limits>
  19026. #include <map>
  19027. #include <string>
  19028. #include <vector>
  19029. #ifndef TINYGLTF_USE_CPP14
  19030. #include <functional>
  19031. #endif
  19032. #ifdef __ANDROID__
  19033. #ifdef TINYGLTF_ANDROID_LOAD_FROM_ASSETS
  19034. #include <android/asset_manager.h>
  19035. #endif
  19036. #endif
  19037. #ifdef __GNUC__
  19038. #if (__GNUC__ < 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ <= 8))
  19039. #define TINYGLTF_NOEXCEPT
  19040. #else
  19041. #define TINYGLTF_NOEXCEPT noexcept
  19042. #endif
  19043. #else
  19044. #define TINYGLTF_NOEXCEPT noexcept
  19045. #endif
  19046. #define DEFAULT_METHODS(x) \
  19047. ~x() = default; \
  19048. x(const x &) = default; \
  19049. x(x &&) TINYGLTF_NOEXCEPT = default; \
  19050. x &operator=(const x &) = default; \
  19051. x &operator=(x &&) TINYGLTF_NOEXCEPT = default;
  19052. namespace tinygltf
  19053. {
  19054. #define TINYGLTF_MODE_POINTS (0)
  19055. #define TINYGLTF_MODE_LINE (1)
  19056. #define TINYGLTF_MODE_LINE_LOOP (2)
  19057. #define TINYGLTF_MODE_LINE_STRIP (3)
  19058. #define TINYGLTF_MODE_TRIANGLES (4)
  19059. #define TINYGLTF_MODE_TRIANGLE_STRIP (5)
  19060. #define TINYGLTF_MODE_TRIANGLE_FAN (6)
  19061. #define TINYGLTF_COMPONENT_TYPE_BYTE (5120)
  19062. #define TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE (5121)
  19063. #define TINYGLTF_COMPONENT_TYPE_SHORT (5122)
  19064. #define TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT (5123)
  19065. #define TINYGLTF_COMPONENT_TYPE_INT (5124)
  19066. #define TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT (5125)
  19067. #define TINYGLTF_COMPONENT_TYPE_FLOAT (5126)
  19068. #define TINYGLTF_COMPONENT_TYPE_DOUBLE (5130)
  19069. #define TINYGLTF_TEXTURE_FILTER_NEAREST (9728)
  19070. #define TINYGLTF_TEXTURE_FILTER_LINEAR (9729)
  19071. #define TINYGLTF_TEXTURE_FILTER_NEAREST_MIPMAP_NEAREST (9984)
  19072. #define TINYGLTF_TEXTURE_FILTER_LINEAR_MIPMAP_NEAREST (9985)
  19073. #define TINYGLTF_TEXTURE_FILTER_NEAREST_MIPMAP_LINEAR (9986)
  19074. #define TINYGLTF_TEXTURE_FILTER_LINEAR_MIPMAP_LINEAR (9987)
  19075. #define TINYGLTF_TEXTURE_WRAP_REPEAT (10497)
  19076. #define TINYGLTF_TEXTURE_WRAP_CLAMP_TO_EDGE (33071)
  19077. #define TINYGLTF_TEXTURE_WRAP_MIRRORED_REPEAT (33648)
  19078. // Redeclarations of the above for technique.parameters.
  19079. #define TINYGLTF_PARAMETER_TYPE_BYTE (5120)
  19080. #define TINYGLTF_PARAMETER_TYPE_UNSIGNED_BYTE (5121)
  19081. #define TINYGLTF_PARAMETER_TYPE_SHORT (5122)
  19082. #define TINYGLTF_PARAMETER_TYPE_UNSIGNED_SHORT (5123)
  19083. #define TINYGLTF_PARAMETER_TYPE_INT (5124)
  19084. #define TINYGLTF_PARAMETER_TYPE_UNSIGNED_INT (5125)
  19085. #define TINYGLTF_PARAMETER_TYPE_FLOAT (5126)
  19086. #define TINYGLTF_PARAMETER_TYPE_FLOAT_VEC2 (35664)
  19087. #define TINYGLTF_PARAMETER_TYPE_FLOAT_VEC3 (35665)
  19088. #define TINYGLTF_PARAMETER_TYPE_FLOAT_VEC4 (35666)
  19089. #define TINYGLTF_PARAMETER_TYPE_INT_VEC2 (35667)
  19090. #define TINYGLTF_PARAMETER_TYPE_INT_VEC3 (35668)
  19091. #define TINYGLTF_PARAMETER_TYPE_INT_VEC4 (35669)
  19092. #define TINYGLTF_PARAMETER_TYPE_BOOL (35670)
  19093. #define TINYGLTF_PARAMETER_TYPE_BOOL_VEC2 (35671)
  19094. #define TINYGLTF_PARAMETER_TYPE_BOOL_VEC3 (35672)
  19095. #define TINYGLTF_PARAMETER_TYPE_BOOL_VEC4 (35673)
  19096. #define TINYGLTF_PARAMETER_TYPE_FLOAT_MAT2 (35674)
  19097. #define TINYGLTF_PARAMETER_TYPE_FLOAT_MAT3 (35675)
  19098. #define TINYGLTF_PARAMETER_TYPE_FLOAT_MAT4 (35676)
  19099. #define TINYGLTF_PARAMETER_TYPE_SAMPLER_2D (35678)
  19100. // End parameter types
  19101. #define TINYGLTF_TYPE_VEC2 (2)
  19102. #define TINYGLTF_TYPE_VEC3 (3)
  19103. #define TINYGLTF_TYPE_VEC4 (4)
  19104. #define TINYGLTF_TYPE_MAT2 (32 + 2)
  19105. #define TINYGLTF_TYPE_MAT3 (32 + 3)
  19106. #define TINYGLTF_TYPE_MAT4 (32 + 4)
  19107. #define TINYGLTF_TYPE_SCALAR (64 + 1)
  19108. #define TINYGLTF_TYPE_VECTOR (64 + 4)
  19109. #define TINYGLTF_TYPE_MATRIX (64 + 16)
  19110. #define TINYGLTF_IMAGE_FORMAT_JPEG (0)
  19111. #define TINYGLTF_IMAGE_FORMAT_PNG (1)
  19112. #define TINYGLTF_IMAGE_FORMAT_BMP (2)
  19113. #define TINYGLTF_IMAGE_FORMAT_GIF (3)
  19114. #define TINYGLTF_TEXTURE_FORMAT_ALPHA (6406)
  19115. #define TINYGLTF_TEXTURE_FORMAT_RGB (6407)
  19116. #define TINYGLTF_TEXTURE_FORMAT_RGBA (6408)
  19117. #define TINYGLTF_TEXTURE_FORMAT_LUMINANCE (6409)
  19118. #define TINYGLTF_TEXTURE_FORMAT_LUMINANCE_ALPHA (6410)
  19119. #define TINYGLTF_TEXTURE_TARGET_TEXTURE2D (3553)
  19120. #define TINYGLTF_TEXTURE_TYPE_UNSIGNED_BYTE (5121)
  19121. #define TINYGLTF_TARGET_ARRAY_BUFFER (34962)
  19122. #define TINYGLTF_TARGET_ELEMENT_ARRAY_BUFFER (34963)
  19123. #define TINYGLTF_SHADER_TYPE_VERTEX_SHADER (35633)
  19124. #define TINYGLTF_SHADER_TYPE_FRAGMENT_SHADER (35632)
  19125. #define TINYGLTF_DOUBLE_EPS (1.e-12)
  19126. #define TINYGLTF_DOUBLE_EQUAL(a, b) (std::fabs((b) - (a)) < TINYGLTF_DOUBLE_EPS)
  19127. #ifdef __ANDROID__
  19128. #ifdef TINYGLTF_ANDROID_LOAD_FROM_ASSETS
  19129. AAssetManager *asset_manager = nullptr;
  19130. #endif
  19131. #endif
  19132. typedef enum
  19133. {
  19134. NULL_TYPE = 0,
  19135. REAL_TYPE = 1,
  19136. INT_TYPE = 2,
  19137. BOOL_TYPE = 3,
  19138. STRING_TYPE = 4,
  19139. ARRAY_TYPE = 5,
  19140. BINARY_TYPE = 6,
  19141. OBJECT_TYPE = 7
  19142. } Type;
  19143. static inline int32_t GetComponentSizeInBytes(uint32_t componentType)
  19144. {
  19145. if (componentType == TINYGLTF_COMPONENT_TYPE_BYTE)
  19146. {
  19147. return 1;
  19148. }
  19149. else if (componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE)
  19150. {
  19151. return 1;
  19152. }
  19153. else if (componentType == TINYGLTF_COMPONENT_TYPE_SHORT)
  19154. {
  19155. return 2;
  19156. }
  19157. else if (componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT)
  19158. {
  19159. return 2;
  19160. }
  19161. else if (componentType == TINYGLTF_COMPONENT_TYPE_INT)
  19162. {
  19163. return 4;
  19164. }
  19165. else if (componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT)
  19166. {
  19167. return 4;
  19168. }
  19169. else if (componentType == TINYGLTF_COMPONENT_TYPE_FLOAT)
  19170. {
  19171. return 4;
  19172. }
  19173. else if (componentType == TINYGLTF_COMPONENT_TYPE_DOUBLE)
  19174. {
  19175. return 8;
  19176. }
  19177. else
  19178. {
  19179. // Unknown componenty type
  19180. return -1;
  19181. }
  19182. }
  19183. static inline int32_t GetNumComponentsInType(uint32_t ty)
  19184. {
  19185. if (ty == TINYGLTF_TYPE_SCALAR)
  19186. {
  19187. return 1;
  19188. }
  19189. else if (ty == TINYGLTF_TYPE_VEC2)
  19190. {
  19191. return 2;
  19192. }
  19193. else if (ty == TINYGLTF_TYPE_VEC3)
  19194. {
  19195. return 3;
  19196. }
  19197. else if (ty == TINYGLTF_TYPE_VEC4)
  19198. {
  19199. return 4;
  19200. }
  19201. else if (ty == TINYGLTF_TYPE_MAT2)
  19202. {
  19203. return 4;
  19204. }
  19205. else if (ty == TINYGLTF_TYPE_MAT3)
  19206. {
  19207. return 9;
  19208. }
  19209. else if (ty == TINYGLTF_TYPE_MAT4)
  19210. {
  19211. return 16;
  19212. }
  19213. else
  19214. {
  19215. // Unknown componenty type
  19216. return -1;
  19217. }
  19218. }
  19219. // TODO(syoyo): Move these functions to TinyGLTF class
  19220. bool IsDataURI(const std::string &in);
  19221. bool DecodeDataURI(std::vector<unsigned char> *out, std::string &mime_type,
  19222. const std::string &in, size_t reqBytes, bool checkSize);
  19223. #ifdef __clang__
  19224. #pragma clang diagnostic push
  19225. // Suppress warning for : static Value null_value
  19226. // https://stackoverflow.com/questions/15708411/how-to-deal-with-global-constructor-warning-in-clang
  19227. #pragma clang diagnostic ignored "-Wexit-time-destructors"
  19228. #pragma clang diagnostic ignored "-Wpadded"
  19229. #endif
  19230. // Simple class to represent JSON object
  19231. class Value
  19232. {
  19233. public:
  19234. typedef std::vector<Value> Array;
  19235. typedef std::map<std::string, Value> Object;
  19236. Value()
  19237. : type_(NULL_TYPE),
  19238. int_value_(0),
  19239. real_value_(0.0),
  19240. boolean_value_(false)
  19241. {
  19242. }
  19243. explicit Value(bool b) : type_(BOOL_TYPE) { boolean_value_ = b; }
  19244. explicit Value(int i) : type_(INT_TYPE)
  19245. {
  19246. int_value_ = i;
  19247. real_value_ = i;
  19248. }
  19249. explicit Value(double n) : type_(REAL_TYPE) { real_value_ = n; }
  19250. explicit Value(const std::string &s) : type_(STRING_TYPE)
  19251. {
  19252. string_value_ = s;
  19253. }
  19254. explicit Value(std::string &&s)
  19255. : type_(STRING_TYPE), string_value_(std::move(s))
  19256. {
  19257. }
  19258. explicit Value(const unsigned char *p, size_t n) : type_(BINARY_TYPE)
  19259. {
  19260. binary_value_.resize(n);
  19261. memcpy(binary_value_.data(), p, n);
  19262. }
  19263. explicit Value(std::vector<unsigned char> &&v) noexcept
  19264. : type_(BINARY_TYPE),
  19265. binary_value_(std::move(v))
  19266. {
  19267. }
  19268. explicit Value(const Array &a) : type_(ARRAY_TYPE) { array_value_ = a; }
  19269. explicit Value(Array &&a) noexcept : type_(ARRAY_TYPE),
  19270. array_value_(std::move(a))
  19271. {
  19272. }
  19273. explicit Value(const Object &o) : type_(OBJECT_TYPE) { object_value_ = o; }
  19274. explicit Value(Object &&o) noexcept : type_(OBJECT_TYPE),
  19275. object_value_(std::move(o))
  19276. {
  19277. }
  19278. DEFAULT_METHODS(Value)
  19279. char Type() const { return static_cast<const char>(type_); }
  19280. bool IsBool() const { return (type_ == BOOL_TYPE); }
  19281. bool IsInt() const { return (type_ == INT_TYPE); }
  19282. bool IsNumber() const { return (type_ == REAL_TYPE) || (type_ == INT_TYPE); }
  19283. bool IsReal() const { return (type_ == REAL_TYPE); }
  19284. bool IsString() const { return (type_ == STRING_TYPE); }
  19285. bool IsBinary() const { return (type_ == BINARY_TYPE); }
  19286. bool IsArray() const { return (type_ == ARRAY_TYPE); }
  19287. bool IsObject() const { return (type_ == OBJECT_TYPE); }
  19288. // Use this function if you want to have number value as double.
  19289. double GetNumberAsDouble() const
  19290. {
  19291. if (type_ == INT_TYPE)
  19292. {
  19293. return double(int_value_);
  19294. }
  19295. else
  19296. {
  19297. return real_value_;
  19298. }
  19299. }
  19300. // Use this function if you want to have number value as int.
  19301. // TODO(syoyo): Support int value larger than 32 bits
  19302. int GetNumberAsInt() const
  19303. {
  19304. if (type_ == REAL_TYPE)
  19305. {
  19306. return int(real_value_);
  19307. }
  19308. else
  19309. {
  19310. return int_value_;
  19311. }
  19312. }
  19313. // Accessor
  19314. template <typename T>
  19315. const T &Get() const;
  19316. template <typename T>
  19317. T &Get();
  19318. // Lookup value from an array
  19319. const Value &Get(int idx) const
  19320. {
  19321. static Value null_value;
  19322. assert(IsArray());
  19323. assert(idx >= 0);
  19324. return (static_cast<size_t>(idx) < array_value_.size())
  19325. ? array_value_[static_cast<size_t>(idx)]
  19326. : null_value;
  19327. }
  19328. // Lookup value from a key-value pair
  19329. const Value &Get(const std::string &key) const
  19330. {
  19331. static Value null_value;
  19332. assert(IsObject());
  19333. Object::const_iterator it = object_value_.find(key);
  19334. return (it != object_value_.end()) ? it->second : null_value;
  19335. }
  19336. size_t ArrayLen() const
  19337. {
  19338. if (!IsArray()) return 0;
  19339. return array_value_.size();
  19340. }
  19341. // Valid only for object type.
  19342. bool Has(const std::string &key) const
  19343. {
  19344. if (!IsObject()) return false;
  19345. Object::const_iterator it = object_value_.find(key);
  19346. return (it != object_value_.end()) ? true : false;
  19347. }
  19348. // List keys
  19349. std::vector<std::string> Keys() const
  19350. {
  19351. std::vector<std::string> keys;
  19352. if (!IsObject()) return keys; // empty
  19353. for (Object::const_iterator it = object_value_.begin();
  19354. it != object_value_.end(); ++it)
  19355. {
  19356. keys.push_back(it->first);
  19357. }
  19358. return keys;
  19359. }
  19360. size_t Size() const { return (IsArray() ? ArrayLen() : Keys().size()); }
  19361. bool operator==(const tinygltf::Value &other) const;
  19362. protected:
  19363. int type_ = NULL_TYPE;
  19364. int int_value_ = 0;
  19365. double real_value_ = 0.0;
  19366. std::string string_value_;
  19367. std::vector<unsigned char> binary_value_;
  19368. Array array_value_;
  19369. Object object_value_;
  19370. bool boolean_value_ = false;
  19371. };
  19372. #ifdef __clang__
  19373. #pragma clang diagnostic pop
  19374. #endif
  19375. #define TINYGLTF_VALUE_GET(ctype, var) \
  19376. template <> \
  19377. inline const ctype &Value::Get<ctype>() const { \
  19378. return var; \
  19379. } \
  19380. template <> \
  19381. inline ctype &Value::Get<ctype>() { \
  19382. return var; \
  19383. }
  19384. TINYGLTF_VALUE_GET(bool, boolean_value_)
  19385. TINYGLTF_VALUE_GET(double, real_value_)
  19386. TINYGLTF_VALUE_GET(int, int_value_)
  19387. TINYGLTF_VALUE_GET(std::string, string_value_)
  19388. TINYGLTF_VALUE_GET(std::vector<unsigned char>, binary_value_)
  19389. TINYGLTF_VALUE_GET(Value::Array, array_value_)
  19390. TINYGLTF_VALUE_GET(Value::Object, object_value_)
  19391. #undef TINYGLTF_VALUE_GET
  19392. #ifdef __clang__
  19393. #pragma clang diagnostic push
  19394. #pragma clang diagnostic ignored "-Wc++98-compat"
  19395. #pragma clang diagnostic ignored "-Wpadded"
  19396. #endif
  19397. /// Agregate object for representing a color
  19398. using ColorValue = std::array<double, 4>;
  19399. // === legacy interface ====
  19400. // TODO(syoyo): Deprecate `Parameter` class.
  19401. struct Parameter
  19402. {
  19403. bool bool_value = false;
  19404. bool has_number_value = false;
  19405. std::string string_value;
  19406. std::vector<double> number_array;
  19407. std::map<std::string, double> json_double_value;
  19408. double number_value = 0.0;
  19409. // context sensitive methods. depending the type of the Parameter you are
  19410. // accessing, these are either valid or not
  19411. // If this parameter represent a texture map in a material, will return the
  19412. // texture index
  19413. /// Return the index of a texture if this Parameter is a texture map.
  19414. /// Returned value is only valid if the parameter represent a texture from a
  19415. /// material
  19416. int TextureIndex() const
  19417. {
  19418. const auto it = json_double_value.find("index");
  19419. if (it != std::end(json_double_value))
  19420. {
  19421. return int(it->second);
  19422. }
  19423. return -1;
  19424. }
  19425. /// Return the index of a texture coordinate set if this Parameter is a
  19426. /// texture map. Returned value is only valid if the parameter represent a
  19427. /// texture from a material
  19428. int TextureTexCoord() const
  19429. {
  19430. const auto it = json_double_value.find("texCoord");
  19431. if (it != std::end(json_double_value))
  19432. {
  19433. return int(it->second);
  19434. }
  19435. // As per the spec, if texCoord is ommited, this parameter is 0
  19436. return 0;
  19437. }
  19438. /// Return the scale of a texture if this Parameter is a normal texture map.
  19439. /// Returned value is only valid if the parameter represent a normal texture
  19440. /// from a material
  19441. double TextureScale() const
  19442. {
  19443. const auto it = json_double_value.find("scale");
  19444. if (it != std::end(json_double_value))
  19445. {
  19446. return it->second;
  19447. }
  19448. // As per the spec, if scale is ommited, this paramter is 1
  19449. return 1;
  19450. }
  19451. /// Return the strength of a texture if this Parameter is a an occlusion map.
  19452. /// Returned value is only valid if the parameter represent an occlusion map
  19453. /// from a material
  19454. double TextureStrength() const
  19455. {
  19456. const auto it = json_double_value.find("strength");
  19457. if (it != std::end(json_double_value))
  19458. {
  19459. return it->second;
  19460. }
  19461. // As per the spec, if strenghth is ommited, this parameter is 1
  19462. return 1;
  19463. }
  19464. /// Material factor, like the roughness or metalness of a material
  19465. /// Returned value is only valid if the parameter represent a texture from a
  19466. /// material
  19467. double Factor() const { return number_value; }
  19468. /// Return the color of a material
  19469. /// Returned value is only valid if the parameter represent a texture from a
  19470. /// material
  19471. ColorValue ColorFactor() const
  19472. {
  19473. return {
  19474. {// this agregate intialize the std::array object, and uses C++11 RVO.
  19475. number_array[0], number_array[1], number_array[2],
  19476. (number_array.size() > 3 ? number_array[3] : 1.0)} };
  19477. }
  19478. Parameter() = default;
  19479. DEFAULT_METHODS(Parameter)
  19480. bool operator==(const Parameter &) const;
  19481. };
  19482. #ifdef __clang__
  19483. #pragma clang diagnostic pop
  19484. #endif
  19485. #ifdef __clang__
  19486. #pragma clang diagnostic push
  19487. #pragma clang diagnostic ignored "-Wpadded"
  19488. #endif
  19489. typedef std::map<std::string, Parameter> ParameterMap;
  19490. typedef std::map<std::string, Value> ExtensionMap;
  19491. struct AnimationChannel
  19492. {
  19493. int sampler; // required
  19494. int target_node; // required (index of the node to target)
  19495. std::string target_path; // required in ["translation", "rotation", "scale",
  19496. // "weights"]
  19497. Value extras;
  19498. ExtensionMap extensions;
  19499. ExtensionMap target_extensions;
  19500. // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled.
  19501. std::string extras_json_string;
  19502. std::string extensions_json_string;
  19503. std::string target_extensions_json_string;
  19504. AnimationChannel() : sampler(-1), target_node(-1) {}
  19505. DEFAULT_METHODS(AnimationChannel)
  19506. bool operator==(const AnimationChannel &) const;
  19507. };
  19508. struct AnimationSampler
  19509. {
  19510. int input; // required
  19511. int output; // required
  19512. std::string interpolation; // "LINEAR", "STEP","CUBICSPLINE" or user defined
  19513. // string. default "LINEAR"
  19514. Value extras;
  19515. ExtensionMap extensions;
  19516. // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled.
  19517. std::string extras_json_string;
  19518. std::string extensions_json_string;
  19519. AnimationSampler() : input(-1), output(-1), interpolation("LINEAR") {}
  19520. DEFAULT_METHODS(AnimationSampler)
  19521. bool operator==(const AnimationSampler &) const;
  19522. };
  19523. struct Animation
  19524. {
  19525. std::string name;
  19526. std::vector<AnimationChannel> channels;
  19527. std::vector<AnimationSampler> samplers;
  19528. Value extras;
  19529. ExtensionMap extensions;
  19530. // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled.
  19531. std::string extras_json_string;
  19532. std::string extensions_json_string;
  19533. Animation() = default;
  19534. DEFAULT_METHODS(Animation)
  19535. bool operator==(const Animation &) const;
  19536. };
  19537. struct Skin
  19538. {
  19539. std::string name;
  19540. int inverseBindMatrices; // required here but not in the spec
  19541. int skeleton; // The index of the node used as a skeleton root
  19542. std::vector<int> joints; // Indices of skeleton nodes
  19543. Value extras;
  19544. ExtensionMap extensions;
  19545. // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled.
  19546. std::string extras_json_string;
  19547. std::string extensions_json_string;
  19548. Skin()
  19549. {
  19550. inverseBindMatrices = -1;
  19551. skeleton = -1;
  19552. }
  19553. DEFAULT_METHODS(Skin)
  19554. bool operator==(const Skin &) const;
  19555. };
  19556. struct Sampler
  19557. {
  19558. std::string name;
  19559. // glTF 2.0 spec does not define default value for `minFilter` and
  19560. // `magFilter`. Set -1 in TinyGLTF(issue #186)
  19561. int minFilter =
  19562. -1; // optional. -1 = no filter defined. ["NEAREST", "LINEAR",
  19563. // "NEAREST_MIPMAP_LINEAR", "LINEAR_MIPMAP_NEAREST",
  19564. // "NEAREST_MIPMAP_LINEAR", "LINEAR_MIPMAP_LINEAR"]
  19565. int magFilter =
  19566. -1; // optional. -1 = no filter defined. ["NEAREST", "LINEAR"]
  19567. int wrapS =
  19568. TINYGLTF_TEXTURE_WRAP_REPEAT; // ["CLAMP_TO_EDGE", "MIRRORED_REPEAT",
  19569. // "REPEAT"], default "REPEAT"
  19570. int wrapT =
  19571. TINYGLTF_TEXTURE_WRAP_REPEAT; // ["CLAMP_TO_EDGE", "MIRRORED_REPEAT",
  19572. // "REPEAT"], default "REPEAT"
  19573. int wrapR = TINYGLTF_TEXTURE_WRAP_REPEAT; // TinyGLTF extension
  19574. Value extras;
  19575. ExtensionMap extensions;
  19576. // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled.
  19577. std::string extras_json_string;
  19578. std::string extensions_json_string;
  19579. Sampler()
  19580. : minFilter(-1),
  19581. magFilter(-1),
  19582. wrapS(TINYGLTF_TEXTURE_WRAP_REPEAT),
  19583. wrapT(TINYGLTF_TEXTURE_WRAP_REPEAT),
  19584. wrapR(TINYGLTF_TEXTURE_WRAP_REPEAT)
  19585. {
  19586. }
  19587. DEFAULT_METHODS(Sampler)
  19588. bool operator==(const Sampler &) const;
  19589. };
  19590. struct Image
  19591. {
  19592. std::string name;
  19593. int width;
  19594. int height;
  19595. int component;
  19596. int bits; // bit depth per channel. 8(byte), 16 or 32.
  19597. int pixel_type; // pixel type(TINYGLTF_COMPONENT_TYPE_***). usually
  19598. // UBYTE(bits = 8) or USHORT(bits = 16)
  19599. std::vector<unsigned char> image;
  19600. int bufferView; // (required if no uri)
  19601. std::string mimeType; // (required if no uri) ["image/jpeg", "image/png",
  19602. // "image/bmp", "image/gif"]
  19603. std::string uri; // (required if no mimeType) uri is not decoded(e.g.
  19604. // whitespace may be represented as %20)
  19605. Value extras;
  19606. ExtensionMap extensions;
  19607. // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled.
  19608. std::string extras_json_string;
  19609. std::string extensions_json_string;
  19610. // When this flag is true, data is stored to `image` in as-is format(e.g. jpeg
  19611. // compressed for "image/jpeg" mime) This feature is good if you use custom
  19612. // image loader function. (e.g. delayed decoding of images for faster glTF
  19613. // parsing) Default parser for Image does not provide as-is loading feature at
  19614. // the moment. (You can manipulate this by providing your own LoadImageData
  19615. // function)
  19616. bool as_is;
  19617. Image() : as_is(false)
  19618. {
  19619. bufferView = -1;
  19620. width = -1;
  19621. height = -1;
  19622. component = -1;
  19623. bits = -1;
  19624. pixel_type = -1;
  19625. }
  19626. DEFAULT_METHODS(Image)
  19627. bool operator==(const Image &) const;
  19628. };
  19629. struct Texture
  19630. {
  19631. std::string name;
  19632. int sampler;
  19633. int source;
  19634. Value extras;
  19635. ExtensionMap extensions;
  19636. // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled.
  19637. std::string extras_json_string;
  19638. std::string extensions_json_string;
  19639. Texture() : sampler(-1), source(-1) {}
  19640. DEFAULT_METHODS(Texture)
  19641. bool operator==(const Texture &) const;
  19642. };
  19643. struct TextureInfo
  19644. {
  19645. int index = -1; // required.
  19646. int texCoord; // The set index of texture's TEXCOORD attribute used for
  19647. // texture coordinate mapping.
  19648. Value extras;
  19649. ExtensionMap extensions;
  19650. // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled.
  19651. std::string extras_json_string;
  19652. std::string extensions_json_string;
  19653. TextureInfo() : index(-1), texCoord(0) {}
  19654. DEFAULT_METHODS(TextureInfo)
  19655. bool operator==(const TextureInfo &) const;
  19656. };
  19657. struct NormalTextureInfo
  19658. {
  19659. int index = -1; // required
  19660. int texCoord; // The set index of texture's TEXCOORD attribute used for
  19661. // texture coordinate mapping.
  19662. double scale; // scaledNormal = normalize((<sampled normal texture value>
  19663. // * 2.0 - 1.0) * vec3(<normal scale>, <normal scale>, 1.0))
  19664. Value extras;
  19665. ExtensionMap extensions;
  19666. // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled.
  19667. std::string extras_json_string;
  19668. std::string extensions_json_string;
  19669. NormalTextureInfo() : index(-1), texCoord(0), scale(1.0) {}
  19670. DEFAULT_METHODS(NormalTextureInfo)
  19671. bool operator==(const NormalTextureInfo &) const;
  19672. };
  19673. struct OcclusionTextureInfo
  19674. {
  19675. int index = -1; // required
  19676. int texCoord; // The set index of texture's TEXCOORD attribute used for
  19677. // texture coordinate mapping.
  19678. double strength; // occludedColor = lerp(color, color * <sampled occlusion
  19679. // texture value>, <occlusion strength>)
  19680. Value extras;
  19681. ExtensionMap extensions;
  19682. // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled.
  19683. std::string extras_json_string;
  19684. std::string extensions_json_string;
  19685. OcclusionTextureInfo() : index(-1), texCoord(0), strength(1.0) {}
  19686. DEFAULT_METHODS(OcclusionTextureInfo)
  19687. bool operator==(const OcclusionTextureInfo &) const;
  19688. };
  19689. // pbrMetallicRoughness class defined in glTF 2.0 spec.
  19690. struct PbrMetallicRoughness
  19691. {
  19692. std::vector<double> baseColorFactor; // len = 4. default [1,1,1,1]
  19693. TextureInfo baseColorTexture;
  19694. double metallicFactor; // default 0
  19695. double roughnessFactor; // default 0.5
  19696. TextureInfo metallicRoughnessTexture;
  19697. Value extras;
  19698. ExtensionMap extensions;
  19699. // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled.
  19700. std::string extras_json_string;
  19701. std::string extensions_json_string;
  19702. PbrMetallicRoughness()
  19703. : baseColorFactor(std::vector<double>{1.0, 1.0, 1.0, 1.0}),
  19704. metallicFactor(0),
  19705. roughnessFactor(0.5)
  19706. {
  19707. }
  19708. DEFAULT_METHODS(PbrMetallicRoughness)
  19709. bool operator==(const PbrMetallicRoughness &) const;
  19710. };
  19711. // Each extension should be stored in a ParameterMap.
  19712. // members not in the values could be included in the ParameterMap
  19713. // to keep a single material model
  19714. struct Material
  19715. {
  19716. std::string name;
  19717. std::vector<double> emissiveFactor; // length 3. default [0, 0, 0]
  19718. std::string alphaMode; // default "OPAQUE"
  19719. double alphaCutoff; // default 0.5
  19720. bool doubleSided; // default false;
  19721. PbrMetallicRoughness pbrMetallicRoughness;
  19722. NormalTextureInfo normalTexture;
  19723. OcclusionTextureInfo occlusionTexture;
  19724. TextureInfo emissiveTexture;
  19725. // For backward compatibility
  19726. // TODO(syoyo): Remove `values` and `additionalValues` in the next release.
  19727. ParameterMap values;
  19728. ParameterMap additionalValues;
  19729. ExtensionMap extensions;
  19730. Value extras;
  19731. // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled.
  19732. std::string extras_json_string;
  19733. std::string extensions_json_string;
  19734. Material() : alphaMode("OPAQUE"), alphaCutoff(0.5), doubleSided(false) {}
  19735. DEFAULT_METHODS(Material)
  19736. bool operator==(const Material &) const;
  19737. };
  19738. struct BufferView
  19739. {
  19740. std::string name;
  19741. int buffer{ -1 }; // Required
  19742. size_t byteOffset{ 0 }; // minimum 0, default 0
  19743. size_t byteLength{ 0 }; // required, minimum 1. 0 = invalid
  19744. size_t byteStride{ 0 }; // minimum 4, maximum 252 (multiple of 4), default 0 =
  19745. // understood to be tightly packed
  19746. int target{ 0 }; // ["ARRAY_BUFFER", "ELEMENT_ARRAY_BUFFER"] for vertex indices
  19747. // or atttribs. Could be 0 for other data
  19748. Value extras;
  19749. ExtensionMap extensions;
  19750. // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled.
  19751. std::string extras_json_string;
  19752. std::string extensions_json_string;
  19753. bool dracoDecoded{ false }; // Flag indicating this has been draco decoded
  19754. BufferView()
  19755. : buffer(-1),
  19756. byteOffset(0),
  19757. byteLength(0),
  19758. byteStride(0),
  19759. target(0),
  19760. dracoDecoded(false)
  19761. {
  19762. }
  19763. DEFAULT_METHODS(BufferView)
  19764. bool operator==(const BufferView &) const;
  19765. };
  19766. struct Accessor
  19767. {
  19768. int bufferView; // optional in spec but required here since sparse accessor
  19769. // are not supported
  19770. std::string name;
  19771. size_t byteOffset;
  19772. bool normalized; // optional.
  19773. int componentType; // (required) One of TINYGLTF_COMPONENT_TYPE_***
  19774. size_t count; // required
  19775. int type; // (required) One of TINYGLTF_TYPE_*** ..
  19776. Value extras;
  19777. ExtensionMap extensions;
  19778. // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled.
  19779. std::string extras_json_string;
  19780. std::string extensions_json_string;
  19781. std::vector<double> minValues; // optional
  19782. std::vector<double> maxValues; // optional
  19783. struct
  19784. {
  19785. int count;
  19786. bool isSparse;
  19787. struct
  19788. {
  19789. int byteOffset;
  19790. int bufferView;
  19791. int componentType; // a TINYGLTF_COMPONENT_TYPE_ value
  19792. } indices;
  19793. struct
  19794. {
  19795. int bufferView;
  19796. int byteOffset;
  19797. } values;
  19798. } sparse;
  19799. ///
  19800. /// Utility function to compute byteStride for a given bufferView object.
  19801. /// Returns -1 upon invalid glTF value or parameter configuration.
  19802. ///
  19803. int ByteStride(const BufferView &bufferViewObject) const
  19804. {
  19805. if (bufferViewObject.byteStride == 0)
  19806. {
  19807. // Assume data is tightly packed.
  19808. int componentSizeInBytes =
  19809. GetComponentSizeInBytes(static_cast<uint32_t>(componentType));
  19810. if (componentSizeInBytes <= 0)
  19811. {
  19812. return -1;
  19813. }
  19814. int numComponents = GetNumComponentsInType(static_cast<uint32_t>(type));
  19815. if (numComponents <= 0)
  19816. {
  19817. return -1;
  19818. }
  19819. return componentSizeInBytes * numComponents;
  19820. }
  19821. else
  19822. {
  19823. // Check if byteStride is a mulple of the size of the accessor's component
  19824. // type.
  19825. int componentSizeInBytes =
  19826. GetComponentSizeInBytes(static_cast<uint32_t>(componentType));
  19827. if (componentSizeInBytes <= 0)
  19828. {
  19829. return -1;
  19830. }
  19831. if ((bufferViewObject.byteStride % uint32_t(componentSizeInBytes)) != 0)
  19832. {
  19833. return -1;
  19834. }
  19835. return static_cast<int>(bufferViewObject.byteStride);
  19836. }
  19837. // unreachable return 0;
  19838. }
  19839. Accessor()
  19840. : bufferView(-1),
  19841. byteOffset(0),
  19842. normalized(false),
  19843. componentType(-1),
  19844. count(0),
  19845. type(-1)
  19846. {
  19847. sparse.isSparse = false;
  19848. }
  19849. DEFAULT_METHODS(Accessor)
  19850. bool operator==(const tinygltf::Accessor &) const;
  19851. };
  19852. struct PerspectiveCamera
  19853. {
  19854. double aspectRatio; // min > 0
  19855. double yfov; // required. min > 0
  19856. double zfar; // min > 0
  19857. double znear; // required. min > 0
  19858. PerspectiveCamera()
  19859. : aspectRatio(0.0),
  19860. yfov(0.0),
  19861. zfar(0.0) // 0 = use infinite projecton matrix
  19862. ,
  19863. znear(0.0)
  19864. {
  19865. }
  19866. DEFAULT_METHODS(PerspectiveCamera)
  19867. bool operator==(const PerspectiveCamera &) const;
  19868. ExtensionMap extensions;
  19869. Value extras;
  19870. // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled.
  19871. std::string extras_json_string;
  19872. std::string extensions_json_string;
  19873. };
  19874. struct OrthographicCamera
  19875. {
  19876. double xmag; // required. must not be zero.
  19877. double ymag; // required. must not be zero.
  19878. double zfar; // required. `zfar` must be greater than `znear`.
  19879. double znear; // required
  19880. OrthographicCamera() : xmag(0.0), ymag(0.0), zfar(0.0), znear(0.0) {}
  19881. DEFAULT_METHODS(OrthographicCamera)
  19882. bool operator==(const OrthographicCamera &) const;
  19883. ExtensionMap extensions;
  19884. Value extras;
  19885. // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled.
  19886. std::string extras_json_string;
  19887. std::string extensions_json_string;
  19888. };
  19889. struct Camera
  19890. {
  19891. std::string type; // required. "perspective" or "orthographic"
  19892. std::string name;
  19893. PerspectiveCamera perspective;
  19894. OrthographicCamera orthographic;
  19895. Camera() {}
  19896. DEFAULT_METHODS(Camera)
  19897. bool operator==(const Camera &) const;
  19898. ExtensionMap extensions;
  19899. Value extras;
  19900. // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled.
  19901. std::string extras_json_string;
  19902. std::string extensions_json_string;
  19903. };
  19904. struct Primitive
  19905. {
  19906. std::map<std::string, int> attributes; // (required) A dictionary object of
  19907. // integer, where each integer
  19908. // is the index of the accessor
  19909. // containing an attribute.
  19910. int material; // The index of the material to apply to this primitive
  19911. // when rendering.
  19912. int indices; // The index of the accessor that contains the indices.
  19913. int mode; // one of TINYGLTF_MODE_***
  19914. std::vector<std::map<std::string, int> > targets; // array of morph targets,
  19915. // where each target is a dict with attribues in ["POSITION, "NORMAL",
  19916. // "TANGENT"] pointing
  19917. // to their corresponding accessors
  19918. ExtensionMap extensions;
  19919. Value extras;
  19920. // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled.
  19921. std::string extras_json_string;
  19922. std::string extensions_json_string;
  19923. Primitive()
  19924. {
  19925. material = -1;
  19926. indices = -1;
  19927. mode = -1;
  19928. }
  19929. DEFAULT_METHODS(Primitive)
  19930. bool operator==(const Primitive &) const;
  19931. };
  19932. struct Mesh
  19933. {
  19934. std::string name;
  19935. std::vector<Primitive> primitives;
  19936. std::vector<double> weights; // weights to be applied to the Morph Targets
  19937. ExtensionMap extensions;
  19938. Value extras;
  19939. // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled.
  19940. std::string extras_json_string;
  19941. std::string extensions_json_string;
  19942. Mesh() = default;
  19943. DEFAULT_METHODS(Mesh)
  19944. bool operator==(const Mesh &) const;
  19945. };
  19946. class Node
  19947. {
  19948. public:
  19949. Node() : camera(-1), skin(-1), mesh(-1) {}
  19950. DEFAULT_METHODS(Node)
  19951. bool operator==(const Node &) const;
  19952. int camera; // the index of the camera referenced by this node
  19953. std::string name;
  19954. int skin;
  19955. int mesh;
  19956. std::vector<int> children;
  19957. std::vector<double> rotation; // length must be 0 or 4
  19958. std::vector<double> scale; // length must be 0 or 3
  19959. std::vector<double> translation; // length must be 0 or 3
  19960. std::vector<double> matrix; // length must be 0 or 16
  19961. std::vector<double> weights; // The weights of the instantiated Morph Target
  19962. ExtensionMap extensions;
  19963. Value extras;
  19964. // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled.
  19965. std::string extras_json_string;
  19966. std::string extensions_json_string;
  19967. };
  19968. struct Buffer
  19969. {
  19970. std::string name;
  19971. std::vector<unsigned char> data;
  19972. std::string
  19973. uri; // considered as required here but not in the spec (need to clarify)
  19974. // uri is not decoded(e.g. whitespace may be represented as %20)
  19975. Value extras;
  19976. ExtensionMap extensions;
  19977. // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled.
  19978. std::string extras_json_string;
  19979. std::string extensions_json_string;
  19980. Buffer() = default;
  19981. DEFAULT_METHODS(Buffer)
  19982. bool operator==(const Buffer &) const;
  19983. };
  19984. struct Asset
  19985. {
  19986. std::string version; // required
  19987. std::string generator;
  19988. std::string minVersion;
  19989. std::string copyright;
  19990. ExtensionMap extensions;
  19991. Value extras;
  19992. // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled.
  19993. std::string extras_json_string;
  19994. std::string extensions_json_string;
  19995. Asset() = default;
  19996. DEFAULT_METHODS(Asset)
  19997. bool operator==(const Asset &) const;
  19998. };
  19999. struct Scene
  20000. {
  20001. std::string name;
  20002. std::vector<int> nodes;
  20003. ExtensionMap extensions;
  20004. Value extras;
  20005. // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled.
  20006. std::string extras_json_string;
  20007. std::string extensions_json_string;
  20008. Scene() = default;
  20009. DEFAULT_METHODS(Scene)
  20010. bool operator==(const Scene &) const;
  20011. };
  20012. struct SpotLight
  20013. {
  20014. double innerConeAngle;
  20015. double outerConeAngle;
  20016. SpotLight() : innerConeAngle(0.0), outerConeAngle(0.7853981634) {}
  20017. DEFAULT_METHODS(SpotLight)
  20018. bool operator==(const SpotLight &) const;
  20019. ExtensionMap extensions;
  20020. Value extras;
  20021. // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled.
  20022. std::string extras_json_string;
  20023. std::string extensions_json_string;
  20024. };
  20025. struct Light
  20026. {
  20027. std::string name;
  20028. std::vector<double> color;
  20029. double intensity{ 1.0 };
  20030. std::string type;
  20031. double range{ 0.0 }; // 0.0 = inifinite
  20032. SpotLight spot;
  20033. Light() : intensity(1.0), range(0.0) {}
  20034. DEFAULT_METHODS(Light)
  20035. bool operator==(const Light &) const;
  20036. ExtensionMap extensions;
  20037. Value extras;
  20038. // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled.
  20039. std::string extras_json_string;
  20040. std::string extensions_json_string;
  20041. };
  20042. class Model
  20043. {
  20044. public:
  20045. Model() = default;
  20046. DEFAULT_METHODS(Model)
  20047. bool operator==(const Model &) const;
  20048. std::vector<Accessor> accessors;
  20049. std::vector<Animation> animations;
  20050. std::vector<Buffer> buffers;
  20051. std::vector<BufferView> bufferViews;
  20052. std::vector<Material> materials;
  20053. std::vector<Mesh> meshes;
  20054. std::vector<Node> nodes;
  20055. std::vector<Texture> textures;
  20056. std::vector<Image> images;
  20057. std::vector<Skin> skins;
  20058. std::vector<Sampler> samplers;
  20059. std::vector<Camera> cameras;
  20060. std::vector<Scene> scenes;
  20061. std::vector<Light> lights;
  20062. int defaultScene = -1;
  20063. std::vector<std::string> extensionsUsed;
  20064. std::vector<std::string> extensionsRequired;
  20065. Asset asset;
  20066. Value extras;
  20067. ExtensionMap extensions;
  20068. // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled.
  20069. std::string extras_json_string;
  20070. std::string extensions_json_string;
  20071. };
  20072. enum SectionCheck
  20073. {
  20074. NO_REQUIRE = 0x00,
  20075. REQUIRE_VERSION = 0x01,
  20076. REQUIRE_SCENE = 0x02,
  20077. REQUIRE_SCENES = 0x04,
  20078. REQUIRE_NODES = 0x08,
  20079. REQUIRE_ACCESSORS = 0x10,
  20080. REQUIRE_BUFFERS = 0x20,
  20081. REQUIRE_BUFFER_VIEWS = 0x40,
  20082. REQUIRE_ALL = 0x7f
  20083. };
  20084. ///
  20085. /// LoadImageDataFunction type. Signature for custom image loading callbacks.
  20086. ///
  20087. typedef bool (*LoadImageDataFunction)(Image *, const int, std::string *,
  20088. std::string *, int, int,
  20089. const unsigned char *, int, void *user_pointer);
  20090. ///
  20091. /// WriteImageDataFunction type. Signature for custom image writing callbacks.
  20092. ///
  20093. typedef bool (*WriteImageDataFunction)(const std::string *, const std::string *,
  20094. Image *, bool, void *);
  20095. #ifndef TINYGLTF_NO_STB_IMAGE
  20096. // Declaration of default image loader callback
  20097. bool LoadImageData(Image *image, const int image_idx, std::string *err,
  20098. std::string *warn, int req_width, int req_height,
  20099. const unsigned char *bytes, int size, void *);
  20100. #endif
  20101. #ifndef TINYGLTF_NO_STB_IMAGE_WRITE
  20102. // Declaration of default image writer callback
  20103. bool WriteImageData(const std::string *basepath, const std::string *filename,
  20104. Image *image, bool embedImages, void *);
  20105. #endif
  20106. ///
  20107. /// FilExistsFunction type. Signature for custom filesystem callbacks.
  20108. ///
  20109. typedef bool (*FileExistsFunction)(const std::string &abs_filename, void *);
  20110. ///
  20111. /// ExpandFilePathFunction type. Signature for custom filesystem callbacks.
  20112. ///
  20113. typedef std::string(*ExpandFilePathFunction)(const std::string &, void *);
  20114. ///
  20115. /// ReadWholeFileFunction type. Signature for custom filesystem callbacks.
  20116. ///
  20117. typedef bool (*ReadWholeFileFunction)(std::vector<unsigned char> *,
  20118. std::string *, const std::string &,
  20119. void *);
  20120. ///
  20121. /// WriteWholeFileFunction type. Signature for custom filesystem callbacks.
  20122. ///
  20123. typedef bool (*WriteWholeFileFunction)(std::string *, const std::string &,
  20124. const std::vector<unsigned char> &,
  20125. void *);
  20126. ///
  20127. /// A structure containing all required filesystem callbacks and a pointer to
  20128. /// their user data.
  20129. ///
  20130. struct FsCallbacks
  20131. {
  20132. FileExistsFunction FileExists;
  20133. ExpandFilePathFunction ExpandFilePath;
  20134. ReadWholeFileFunction ReadWholeFile;
  20135. WriteWholeFileFunction WriteWholeFile;
  20136. void *user_data; // An argument that is passed to all fs callbacks
  20137. };
  20138. #ifndef TINYGLTF_NO_FS
  20139. // Declaration of default filesystem callbacks
  20140. bool FileExists(const std::string &abs_filename, void *);
  20141. ///
  20142. /// Expand file path(e.g. `~` to home directory on posix, `%APPDATA%` to
  20143. /// `C:\Users\tinygltf\AppData`)
  20144. ///
  20145. /// @param[in] filepath File path string. Assume UTF-8
  20146. /// @param[in] userdata User data. Set to `nullptr` if you don't need it.
  20147. ///
  20148. std::string ExpandFilePath(const std::string &filepath, void *userdata);
  20149. bool ReadWholeFile(std::vector<unsigned char> *out, std::string *err,
  20150. const std::string &filepath, void *);
  20151. bool WriteWholeFile(std::string *err, const std::string &filepath,
  20152. const std::vector<unsigned char> &contents, void *);
  20153. #endif
  20154. ///
  20155. /// glTF Parser/Serialier context.
  20156. ///
  20157. class TinyGLTF
  20158. {
  20159. public:
  20160. #ifdef __clang__
  20161. #pragma clang diagnostic push
  20162. #pragma clang diagnostic ignored "-Wc++98-compat"
  20163. #endif
  20164. TinyGLTF() : bin_data_(nullptr), bin_size_(0), is_binary_(false) {}
  20165. #ifdef __clang__
  20166. #pragma clang diagnostic pop
  20167. #endif
  20168. ~TinyGLTF() {}
  20169. ///
  20170. /// Loads glTF ASCII asset from a file.
  20171. /// Set warning message to `warn` for example it fails to load asserts.
  20172. /// Returns false and set error string to `err` if there's an error.
  20173. ///
  20174. bool LoadASCIIFromFile(Model *model, std::string *err, std::string *warn,
  20175. const std::string &filename,
  20176. unsigned int check_sections = REQUIRE_VERSION);
  20177. ///
  20178. /// Loads glTF ASCII asset from string(memory).
  20179. /// `length` = strlen(str);
  20180. /// Set warning message to `warn` for example it fails to load asserts.
  20181. /// Returns false and set error string to `err` if there's an error.
  20182. ///
  20183. bool LoadASCIIFromString(Model *model, std::string *err, std::string *warn,
  20184. const char *str, const unsigned int length,
  20185. const std::string &base_dir,
  20186. unsigned int check_sections = REQUIRE_VERSION);
  20187. ///
  20188. /// Loads glTF binary asset from a file.
  20189. /// Set warning message to `warn` for example it fails to load asserts.
  20190. /// Returns false and set error string to `err` if there's an error.
  20191. ///
  20192. bool LoadBinaryFromFile(Model *model, std::string *err, std::string *warn,
  20193. const std::string &filename,
  20194. unsigned int check_sections = REQUIRE_VERSION);
  20195. ///
  20196. /// Loads glTF binary asset from memory.
  20197. /// `length` = strlen(str);
  20198. /// Set warning message to `warn` for example it fails to load asserts.
  20199. /// Returns false and set error string to `err` if there's an error.
  20200. ///
  20201. bool LoadBinaryFromMemory(Model *model, std::string *err, std::string *warn,
  20202. const unsigned char *bytes,
  20203. const unsigned int length,
  20204. const std::string &base_dir = "",
  20205. unsigned int check_sections = REQUIRE_VERSION);
  20206. ///
  20207. /// Write glTF to stream, buffers and images will be embeded
  20208. ///
  20209. bool WriteGltfSceneToStream(Model *model, std::ostream &stream,
  20210. bool prettyPrint, bool writeBinary);
  20211. ///
  20212. /// Write glTF to file.
  20213. ///
  20214. bool WriteGltfSceneToFile(Model *model, const std::string &filename,
  20215. bool embedImages, bool embedBuffers,
  20216. bool prettyPrint, bool writeBinary);
  20217. ///
  20218. /// Set callback to use for loading image data
  20219. ///
  20220. void SetImageLoader(LoadImageDataFunction LoadImageData, void *user_data);
  20221. ///
  20222. /// Unset(remove) callback of loading image data
  20223. ///
  20224. void RemoveImageLoader();
  20225. ///
  20226. /// Set callback to use for writing image data
  20227. ///
  20228. void SetImageWriter(WriteImageDataFunction WriteImageData, void *user_data);
  20229. ///
  20230. /// Set callbacks to use for filesystem (fs) access and their user data
  20231. ///
  20232. void SetFsCallbacks(FsCallbacks callbacks);
  20233. ///
  20234. /// Set serializing default values(default = false).
  20235. /// When true, default values are force serialized to .glTF.
  20236. /// This may be helpfull if you want to serialize a full description of glTF
  20237. /// data.
  20238. ///
  20239. /// TODO(LTE): Supply parsing option as function arguments to
  20240. /// `LoadASCIIFromFile()` and others, not by a class method
  20241. ///
  20242. void SetSerializeDefaultValues(const bool enabled)
  20243. {
  20244. serialize_default_values_ = enabled;
  20245. }
  20246. bool GetSerializeDefaultValues() const { return serialize_default_values_; }
  20247. ///
  20248. /// Store original JSON string for `extras` and `extensions`.
  20249. /// This feature will be useful when the user want to reconstruct custom data
  20250. /// structure from JSON string.
  20251. ///
  20252. void SetStoreOriginalJSONForExtrasAndExtensions(const bool enabled)
  20253. {
  20254. store_original_json_for_extras_and_extensions_ = enabled;
  20255. }
  20256. bool GetStoreOriginalJSONForExtrasAndExtensions() const
  20257. {
  20258. return store_original_json_for_extras_and_extensions_;
  20259. }
  20260. ///
  20261. /// Specify whether preserve image channales when loading images or not.
  20262. /// (Not effective when the user suppy their own LoadImageData callbacks)
  20263. ///
  20264. void SetPreserveImageChannels(bool onoff)
  20265. {
  20266. preserve_image_channels_ = onoff;
  20267. }
  20268. bool GetPreserveImageChannels() const
  20269. {
  20270. return preserve_image_channels_;
  20271. }
  20272. private:
  20273. ///
  20274. /// Loads glTF asset from string(memory).
  20275. /// `length` = strlen(str);
  20276. /// Set warning message to `warn` for example it fails to load asserts
  20277. /// Returns false and set error string to `err` if there's an error.
  20278. ///
  20279. bool LoadFromString(Model *model, std::string *err, std::string *warn,
  20280. const char *str, const unsigned int length,
  20281. const std::string &base_dir, unsigned int check_sections);
  20282. const unsigned char *bin_data_ = nullptr;
  20283. size_t bin_size_ = 0;
  20284. bool is_binary_ = false;
  20285. bool serialize_default_values_ = false; ///< Serialize default values?
  20286. bool store_original_json_for_extras_and_extensions_ = false;
  20287. bool preserve_image_channels_ = false; /// Default false(expand channels to RGBA) for backward compatibility.
  20288. FsCallbacks fs = {
  20289. #ifndef TINYGLTF_NO_FS
  20290. & tinygltf::FileExists, &tinygltf::ExpandFilePath,
  20291. &tinygltf::ReadWholeFile, &tinygltf::WriteWholeFile,
  20292. nullptr // Fs callback user data
  20293. #else
  20294. nullptr, nullptr, nullptr, nullptr,
  20295. nullptr // Fs callback user data
  20296. #endif
  20297. };
  20298. LoadImageDataFunction LoadImageData =
  20299. #ifndef TINYGLTF_NO_STB_IMAGE
  20300. & tinygltf::LoadImageData;
  20301. #else
  20302. nullptr;
  20303. #endif
  20304. void *load_image_user_data_{ nullptr };
  20305. bool user_image_loader_{ false };
  20306. WriteImageDataFunction WriteImageData =
  20307. #ifndef TINYGLTF_NO_STB_IMAGE_WRITE
  20308. & tinygltf::WriteImageData;
  20309. #else
  20310. nullptr;
  20311. #endif
  20312. void *write_image_user_data_{ nullptr };
  20313. };
  20314. #ifdef __clang__
  20315. #pragma clang diagnostic pop // -Wpadded
  20316. #endif
  20317. } // namespace tinygltf
  20318. #endif // TINY_GLTF_H_
  20319. #if defined(TINYGLTF_IMPLEMENTATION) || defined(__INTELLISENSE__)
  20320. #include <algorithm>
  20321. //#include <cassert>
  20322. #ifndef TINYGLTF_NO_FS
  20323. #include <cstdio>
  20324. #endif
  20325. #include <sstream>
  20326. #ifdef __clang__
  20327. // Disable some warnings for external files.
  20328. #pragma clang diagnostic push
  20329. #pragma clang diagnostic ignored "-Wfloat-equal"
  20330. #pragma clang diagnostic ignored "-Wexit-time-destructors"
  20331. #pragma clang diagnostic ignored "-Wconversion"
  20332. #pragma clang diagnostic ignored "-Wold-style-cast"
  20333. #pragma clang diagnostic ignored "-Wglobal-constructors"
  20334. #if __has_warning("-Wreserved-id-macro")
  20335. #pragma clang diagnostic ignored "-Wreserved-id-macro"
  20336. #endif
  20337. #pragma clang diagnostic ignored "-Wdisabled-macro-expansion"
  20338. #pragma clang diagnostic ignored "-Wpadded"
  20339. #pragma clang diagnostic ignored "-Wc++98-compat"
  20340. #pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
  20341. #pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
  20342. #pragma clang diagnostic ignored "-Wswitch-enum"
  20343. #pragma clang diagnostic ignored "-Wimplicit-fallthrough"
  20344. #pragma clang diagnostic ignored "-Wweak-vtables"
  20345. #pragma clang diagnostic ignored "-Wcovered-switch-default"
  20346. #if __has_warning("-Wdouble-promotion")
  20347. #pragma clang diagnostic ignored "-Wdouble-promotion"
  20348. #endif
  20349. #if __has_warning("-Wcomma")
  20350. #pragma clang diagnostic ignored "-Wcomma"
  20351. #endif
  20352. #if __has_warning("-Wzero-as-null-pointer-constant")
  20353. #pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant"
  20354. #endif
  20355. #if __has_warning("-Wcast-qual")
  20356. #pragma clang diagnostic ignored "-Wcast-qual"
  20357. #endif
  20358. #if __has_warning("-Wmissing-variable-declarations")
  20359. #pragma clang diagnostic ignored "-Wmissing-variable-declarations"
  20360. #endif
  20361. #if __has_warning("-Wmissing-prototypes")
  20362. #pragma clang diagnostic ignored "-Wmissing-prototypes"
  20363. #endif
  20364. #if __has_warning("-Wcast-align")
  20365. #pragma clang diagnostic ignored "-Wcast-align"
  20366. #endif
  20367. #if __has_warning("-Wnewline-eof")
  20368. #pragma clang diagnostic ignored "-Wnewline-eof"
  20369. #endif
  20370. #if __has_warning("-Wunused-parameter")
  20371. #pragma clang diagnostic ignored "-Wunused-parameter"
  20372. #endif
  20373. #if __has_warning("-Wmismatched-tags")
  20374. #pragma clang diagnostic ignored "-Wmismatched-tags"
  20375. #endif
  20376. #if __has_warning("-Wextra-semi-stmt")
  20377. #pragma clang diagnostic ignored "-Wextra-semi-stmt"
  20378. #endif
  20379. #endif
  20380. // Disable GCC warnigs
  20381. #ifdef __GNUC__
  20382. #pragma GCC diagnostic push
  20383. #pragma GCC diagnostic ignored "-Wtype-limits"
  20384. #endif // __GNUC__
  20385. #ifndef TINYGLTF_NO_INCLUDE_JSON
  20386. #ifndef TINYGLTF_USE_RAPIDJSON
  20387. #else
  20388. #include "document.h"
  20389. #include "prettywriter.h"
  20390. #include "rapidjson.h"
  20391. #include "stringbuffer.h"
  20392. #include "writer.h"
  20393. #endif
  20394. #endif
  20395. #ifdef TINYGLTF_ENABLE_DRACO
  20396. #include "draco/compression/decode.h"
  20397. #include "draco/core/decoder_buffer.h"
  20398. #endif
  20399. #ifndef TINYGLTF_NO_STB_IMAGE
  20400. #ifndef TINYGLTF_NO_INCLUDE_STB_IMAGE
  20401. #include "stb_image.h"
  20402. #endif
  20403. #endif
  20404. #ifndef TINYGLTF_NO_STB_IMAGE_WRITE
  20405. #ifndef TINYGLTF_NO_INCLUDE_STB_IMAGE_WRITE
  20406. #endif
  20407. #endif
  20408. #ifdef __clang__
  20409. #pragma clang diagnostic pop
  20410. #endif
  20411. #ifdef __GNUC__
  20412. #pragma GCC diagnostic pop
  20413. #endif
  20414. #ifdef _WIN32
  20415. // issue 143.
  20416. // Define NOMINMAX to avoid min/max defines,
  20417. // but undef it after included windows.h
  20418. #ifndef NOMINMAX
  20419. #define TINYGLTF_INTERNAL_NOMINMAX
  20420. #define NOMINMAX
  20421. #endif
  20422. #ifndef WIN32_LEAN_AND_MEAN
  20423. #define WIN32_LEAN_AND_MEAN
  20424. #define TINYGLTF_INTERNAL_WIN32_LEAN_AND_MEAN
  20425. #endif
  20426. #include <windows.h> // include API for expanding a file path
  20427. #ifdef TINYGLTF_INTERNAL_WIN32_LEAN_AND_MEAN
  20428. #undef WIN32_LEAN_AND_MEAN
  20429. #endif
  20430. #if defined(TINYGLTF_INTERNAL_NOMINMAX)
  20431. #undef NOMINMAX
  20432. #endif
  20433. #if defined(__GLIBCXX__) // mingw
  20434. #include <fcntl.h> // _O_RDONLY
  20435. #include <ext/stdio_filebuf.h> // fstream (all sorts of IO stuff) + stdio_filebuf (=streambuf)
  20436. #endif
  20437. #elif !defined(__ANDROID__)
  20438. #include <wordexp.h>
  20439. #endif
  20440. #if defined(__sparcv9)
  20441. // Big endian
  20442. #else
  20443. #if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || MINIZ_X86_OR_X64_CPU
  20444. #define TINYGLTF_LITTLE_ENDIAN 1
  20445. #endif
  20446. #endif
  20447. namespace
  20448. {
  20449. #ifdef TINYGLTF_USE_RAPIDJSON
  20450. #ifdef TINYGLTF_USE_RAPIDJSON_CRTALLOCATOR
  20451. // This uses the RapidJSON CRTAllocator. It is thread safe and multiple
  20452. // documents may be active at once.
  20453. using json =
  20454. rapidjson::GenericValue<rapidjson::UTF8<>, rapidjson::CrtAllocator>;
  20455. using json_const_iterator = json::ConstMemberIterator;
  20456. using json_const_array_iterator = json const *;
  20457. using JsonDocument =
  20458. rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator>;
  20459. rapidjson::CrtAllocator s_CrtAllocator; // stateless and thread safe
  20460. rapidjson::CrtAllocator &GetAllocator() { return s_CrtAllocator; }
  20461. #else
  20462. // This uses the default RapidJSON MemoryPoolAllocator. It is very fast, but
  20463. // not thread safe. Only a single JsonDocument may be active at any one time,
  20464. // meaning only a single gltf load/save can be active any one time.
  20465. using json = rapidjson::Value;
  20466. using json_const_iterator = json::ConstMemberIterator;
  20467. using json_const_array_iterator = json const *;
  20468. rapidjson::Document *s_pActiveDocument = nullptr;
  20469. rapidjson::Document::AllocatorType &GetAllocator()
  20470. {
  20471. assert(s_pActiveDocument); // Root json node must be JsonDocument type
  20472. return s_pActiveDocument->GetAllocator();
  20473. }
  20474. #ifdef __clang__
  20475. #pragma clang diagnostic push
  20476. // Suppress JsonDocument(JsonDocument &&rhs) noexcept
  20477. #pragma clang diagnostic ignored "-Wunused-member-function"
  20478. #endif
  20479. struct JsonDocument : public rapidjson::Document
  20480. {
  20481. JsonDocument()
  20482. {
  20483. assert(s_pActiveDocument ==
  20484. nullptr); // When using default allocator, only one document can be
  20485. // active at a time, if you need multiple active at once,
  20486. // define TINYGLTF_USE_RAPIDJSON_CRTALLOCATOR
  20487. s_pActiveDocument = this;
  20488. }
  20489. JsonDocument(const JsonDocument &) = delete;
  20490. JsonDocument(JsonDocument &&rhs) noexcept
  20491. : rapidjson::Document(std::move(rhs))
  20492. {
  20493. s_pActiveDocument = this;
  20494. rhs.isNil = true;
  20495. }
  20496. ~JsonDocument()
  20497. {
  20498. if (!isNil)
  20499. {
  20500. s_pActiveDocument = nullptr;
  20501. }
  20502. }
  20503. private:
  20504. bool isNil = false;
  20505. };
  20506. #ifdef __clang__
  20507. #pragma clang diagnostic pop
  20508. #endif
  20509. #endif // TINYGLTF_USE_RAPIDJSON_CRTALLOCATOR
  20510. #else
  20511. using nlohmann::json;
  20512. using json_const_iterator = json::const_iterator;
  20513. using json_const_array_iterator = json_const_iterator;
  20514. using JsonDocument = json;
  20515. #endif
  20516. void JsonParse(JsonDocument &doc, const char *str, size_t length,
  20517. bool throwExc = false)
  20518. {
  20519. #ifdef TINYGLTF_USE_RAPIDJSON
  20520. (void)throwExc;
  20521. doc.Parse(str, length);
  20522. #else
  20523. doc = json::parse(str, str + length, nullptr, throwExc);
  20524. #endif
  20525. }
  20526. } // namespace
  20527. #ifdef __APPLE__
  20528. #include "TargetConditionals.h"
  20529. #endif
  20530. #ifdef __clang__
  20531. #pragma clang diagnostic push
  20532. #pragma clang diagnostic ignored "-Wc++98-compat"
  20533. #endif
  20534. namespace tinygltf
  20535. {
  20536. ///
  20537. /// Internal LoadImageDataOption struct.
  20538. /// This struct is passed through `user_pointer` in LoadImageData.
  20539. /// The struct is not passed when the user supply their own LoadImageData callbacks.
  20540. ///
  20541. struct LoadImageDataOption
  20542. {
  20543. // true: preserve image channels(e.g. load as RGB image if the image has RGB channels)
  20544. // default `false`(channels are expanded to RGBA for backward compatiblity).
  20545. bool preserve_channels{ false };
  20546. };
  20547. // Equals function for Value, for recursivity
  20548. static bool Equals(const tinygltf::Value &one, const tinygltf::Value &other)
  20549. {
  20550. if (one.Type() != other.Type()) return false;
  20551. switch (one.Type())
  20552. {
  20553. case NULL_TYPE:
  20554. return true;
  20555. case BOOL_TYPE:
  20556. return one.Get<bool>() == other.Get<bool>();
  20557. case REAL_TYPE:
  20558. return TINYGLTF_DOUBLE_EQUAL(one.Get<double>(), other.Get<double>());
  20559. case INT_TYPE:
  20560. return one.Get<int>() == other.Get<int>();
  20561. case OBJECT_TYPE:
  20562. {
  20563. auto oneObj = one.Get<tinygltf::Value::Object>();
  20564. auto otherObj = other.Get<tinygltf::Value::Object>();
  20565. if (oneObj.size() != otherObj.size()) return false;
  20566. for (auto &it : oneObj)
  20567. {
  20568. auto otherIt = otherObj.find(it.first);
  20569. if (otherIt == otherObj.end()) return false;
  20570. if (!Equals(it.second, otherIt->second)) return false;
  20571. }
  20572. return true;
  20573. }
  20574. case ARRAY_TYPE:
  20575. {
  20576. if (one.Size() != other.Size()) return false;
  20577. for (int i = 0; i < int(one.Size()); ++i)
  20578. if (!Equals(one.Get(i), other.Get(i))) return false;
  20579. return true;
  20580. }
  20581. case STRING_TYPE:
  20582. return one.Get<std::string>() == other.Get<std::string>();
  20583. case BINARY_TYPE:
  20584. return one.Get<std::vector<unsigned char> >() ==
  20585. other.Get<std::vector<unsigned char> >();
  20586. default:
  20587. {
  20588. // unhandled type
  20589. return false;
  20590. }
  20591. }
  20592. }
  20593. // Equals function for std::vector<double> using TINYGLTF_DOUBLE_EPSILON
  20594. static bool Equals(const std::vector<double> &one,
  20595. const std::vector<double> &other)
  20596. {
  20597. if (one.size() != other.size()) return false;
  20598. for (int i = 0; i < int(one.size()); ++i)
  20599. {
  20600. if (!TINYGLTF_DOUBLE_EQUAL(one[size_t(i)], other[size_t(i)])) return false;
  20601. }
  20602. return true;
  20603. }
  20604. bool Accessor::operator==(const Accessor &other) const
  20605. {
  20606. return this->bufferView == other.bufferView &&
  20607. this->byteOffset == other.byteOffset &&
  20608. this->componentType == other.componentType &&
  20609. this->count == other.count && this->extensions == other.extensions &&
  20610. this->extras == other.extras &&
  20611. Equals(this->maxValues, other.maxValues) &&
  20612. Equals(this->minValues, other.minValues) && this->name == other.name &&
  20613. this->normalized == other.normalized && this->type == other.type;
  20614. }
  20615. bool Animation::operator==(const Animation &other) const
  20616. {
  20617. return this->channels == other.channels &&
  20618. this->extensions == other.extensions && this->extras == other.extras &&
  20619. this->name == other.name && this->samplers == other.samplers;
  20620. }
  20621. bool AnimationChannel::operator==(const AnimationChannel &other) const
  20622. {
  20623. return this->extensions == other.extensions && this->extras == other.extras &&
  20624. this->target_node == other.target_node &&
  20625. this->target_path == other.target_path &&
  20626. this->sampler == other.sampler;
  20627. }
  20628. bool AnimationSampler::operator==(const AnimationSampler &other) const
  20629. {
  20630. return this->extras == other.extras && this->extensions == other.extensions &&
  20631. this->input == other.input &&
  20632. this->interpolation == other.interpolation &&
  20633. this->output == other.output;
  20634. }
  20635. bool Asset::operator==(const Asset &other) const
  20636. {
  20637. return this->copyright == other.copyright &&
  20638. this->extensions == other.extensions && this->extras == other.extras &&
  20639. this->generator == other.generator &&
  20640. this->minVersion == other.minVersion && this->version == other.version;
  20641. }
  20642. bool Buffer::operator==(const Buffer &other) const
  20643. {
  20644. return this->data == other.data && this->extensions == other.extensions &&
  20645. this->extras == other.extras && this->name == other.name &&
  20646. this->uri == other.uri;
  20647. }
  20648. bool BufferView::operator==(const BufferView &other) const
  20649. {
  20650. return this->buffer == other.buffer && this->byteLength == other.byteLength &&
  20651. this->byteOffset == other.byteOffset &&
  20652. this->byteStride == other.byteStride && this->name == other.name &&
  20653. this->target == other.target && this->extensions == other.extensions &&
  20654. this->extras == other.extras &&
  20655. this->dracoDecoded == other.dracoDecoded;
  20656. }
  20657. bool Camera::operator==(const Camera &other) const
  20658. {
  20659. return this->name == other.name && this->extensions == other.extensions &&
  20660. this->extras == other.extras &&
  20661. this->orthographic == other.orthographic &&
  20662. this->perspective == other.perspective && this->type == other.type;
  20663. }
  20664. bool Image::operator==(const Image &other) const
  20665. {
  20666. return this->bufferView == other.bufferView &&
  20667. this->component == other.component &&
  20668. this->extensions == other.extensions && this->extras == other.extras &&
  20669. this->height == other.height && this->image == other.image &&
  20670. this->mimeType == other.mimeType && this->name == other.name &&
  20671. this->uri == other.uri && this->width == other.width;
  20672. }
  20673. bool Light::operator==(const Light &other) const
  20674. {
  20675. return Equals(this->color, other.color) && this->name == other.name &&
  20676. this->type == other.type;
  20677. }
  20678. bool Material::operator==(const Material &other) const
  20679. {
  20680. return (this->pbrMetallicRoughness == other.pbrMetallicRoughness) &&
  20681. (this->normalTexture == other.normalTexture) &&
  20682. (this->occlusionTexture == other.occlusionTexture) &&
  20683. (this->emissiveTexture == other.emissiveTexture) &&
  20684. Equals(this->emissiveFactor, other.emissiveFactor) &&
  20685. (this->alphaMode == other.alphaMode) &&
  20686. TINYGLTF_DOUBLE_EQUAL(this->alphaCutoff, other.alphaCutoff) &&
  20687. (this->doubleSided == other.doubleSided) &&
  20688. (this->extensions == other.extensions) &&
  20689. (this->extras == other.extras) && (this->values == other.values) &&
  20690. (this->additionalValues == other.additionalValues) &&
  20691. (this->name == other.name);
  20692. }
  20693. bool Mesh::operator==(const Mesh &other) const
  20694. {
  20695. return this->extensions == other.extensions && this->extras == other.extras &&
  20696. this->name == other.name && Equals(this->weights, other.weights) &&
  20697. this->primitives == other.primitives;
  20698. }
  20699. bool Model::operator==(const Model &other) const
  20700. {
  20701. return this->accessors == other.accessors &&
  20702. this->animations == other.animations && this->asset == other.asset &&
  20703. this->buffers == other.buffers &&
  20704. this->bufferViews == other.bufferViews &&
  20705. this->cameras == other.cameras &&
  20706. this->defaultScene == other.defaultScene &&
  20707. this->extensions == other.extensions &&
  20708. this->extensionsRequired == other.extensionsRequired &&
  20709. this->extensionsUsed == other.extensionsUsed &&
  20710. this->extras == other.extras && this->images == other.images &&
  20711. this->lights == other.lights && this->materials == other.materials &&
  20712. this->meshes == other.meshes && this->nodes == other.nodes &&
  20713. this->samplers == other.samplers && this->scenes == other.scenes &&
  20714. this->skins == other.skins && this->textures == other.textures;
  20715. }
  20716. bool Node::operator==(const Node &other) const
  20717. {
  20718. return this->camera == other.camera && this->children == other.children &&
  20719. this->extensions == other.extensions && this->extras == other.extras &&
  20720. Equals(this->matrix, other.matrix) && this->mesh == other.mesh &&
  20721. this->name == other.name && Equals(this->rotation, other.rotation) &&
  20722. Equals(this->scale, other.scale) && this->skin == other.skin &&
  20723. Equals(this->translation, other.translation) &&
  20724. Equals(this->weights, other.weights);
  20725. }
  20726. bool SpotLight::operator==(const SpotLight &other) const
  20727. {
  20728. return this->extensions == other.extensions && this->extras == other.extras &&
  20729. TINYGLTF_DOUBLE_EQUAL(this->innerConeAngle, other.innerConeAngle) &&
  20730. TINYGLTF_DOUBLE_EQUAL(this->outerConeAngle, other.outerConeAngle);
  20731. }
  20732. bool OrthographicCamera::operator==(const OrthographicCamera &other) const
  20733. {
  20734. return this->extensions == other.extensions && this->extras == other.extras &&
  20735. TINYGLTF_DOUBLE_EQUAL(this->xmag, other.xmag) &&
  20736. TINYGLTF_DOUBLE_EQUAL(this->ymag, other.ymag) &&
  20737. TINYGLTF_DOUBLE_EQUAL(this->zfar, other.zfar) &&
  20738. TINYGLTF_DOUBLE_EQUAL(this->znear, other.znear);
  20739. }
  20740. bool Parameter::operator==(const Parameter &other) const
  20741. {
  20742. if (this->bool_value != other.bool_value ||
  20743. this->has_number_value != other.has_number_value)
  20744. return false;
  20745. if (!TINYGLTF_DOUBLE_EQUAL(this->number_value, other.number_value))
  20746. return false;
  20747. if (this->json_double_value.size() != other.json_double_value.size())
  20748. return false;
  20749. for (auto &it : this->json_double_value)
  20750. {
  20751. auto otherIt = other.json_double_value.find(it.first);
  20752. if (otherIt == other.json_double_value.end()) return false;
  20753. if (!TINYGLTF_DOUBLE_EQUAL(it.second, otherIt->second)) return false;
  20754. }
  20755. if (!Equals(this->number_array, other.number_array)) return false;
  20756. if (this->string_value != other.string_value) return false;
  20757. return true;
  20758. }
  20759. bool PerspectiveCamera::operator==(const PerspectiveCamera &other) const
  20760. {
  20761. return TINYGLTF_DOUBLE_EQUAL(this->aspectRatio, other.aspectRatio) &&
  20762. this->extensions == other.extensions && this->extras == other.extras &&
  20763. TINYGLTF_DOUBLE_EQUAL(this->yfov, other.yfov) &&
  20764. TINYGLTF_DOUBLE_EQUAL(this->zfar, other.zfar) &&
  20765. TINYGLTF_DOUBLE_EQUAL(this->znear, other.znear);
  20766. }
  20767. bool Primitive::operator==(const Primitive &other) const
  20768. {
  20769. return this->attributes == other.attributes && this->extras == other.extras &&
  20770. this->indices == other.indices && this->material == other.material &&
  20771. this->mode == other.mode && this->targets == other.targets;
  20772. }
  20773. bool Sampler::operator==(const Sampler &other) const
  20774. {
  20775. return this->extensions == other.extensions && this->extras == other.extras &&
  20776. this->magFilter == other.magFilter &&
  20777. this->minFilter == other.minFilter && this->name == other.name &&
  20778. this->wrapR == other.wrapR && this->wrapS == other.wrapS &&
  20779. this->wrapT == other.wrapT;
  20780. }
  20781. bool Scene::operator==(const Scene &other) const
  20782. {
  20783. return this->extensions == other.extensions && this->extras == other.extras &&
  20784. this->name == other.name && this->nodes == other.nodes;
  20785. }
  20786. bool Skin::operator==(const Skin &other) const
  20787. {
  20788. return this->extensions == other.extensions && this->extras == other.extras &&
  20789. this->inverseBindMatrices == other.inverseBindMatrices &&
  20790. this->joints == other.joints && this->name == other.name &&
  20791. this->skeleton == other.skeleton;
  20792. }
  20793. bool Texture::operator==(const Texture &other) const
  20794. {
  20795. return this->extensions == other.extensions && this->extras == other.extras &&
  20796. this->name == other.name && this->sampler == other.sampler &&
  20797. this->source == other.source;
  20798. }
  20799. bool TextureInfo::operator==(const TextureInfo &other) const
  20800. {
  20801. return this->extensions == other.extensions && this->extras == other.extras &&
  20802. this->index == other.index && this->texCoord == other.texCoord;
  20803. }
  20804. bool NormalTextureInfo::operator==(const NormalTextureInfo &other) const
  20805. {
  20806. return this->extensions == other.extensions && this->extras == other.extras &&
  20807. this->index == other.index && this->texCoord == other.texCoord &&
  20808. TINYGLTF_DOUBLE_EQUAL(this->scale, other.scale);
  20809. }
  20810. bool OcclusionTextureInfo::operator==(const OcclusionTextureInfo &other) const
  20811. {
  20812. return this->extensions == other.extensions && this->extras == other.extras &&
  20813. this->index == other.index && this->texCoord == other.texCoord &&
  20814. TINYGLTF_DOUBLE_EQUAL(this->strength, other.strength);
  20815. }
  20816. bool PbrMetallicRoughness::operator==(const PbrMetallicRoughness &other) const
  20817. {
  20818. return this->extensions == other.extensions && this->extras == other.extras &&
  20819. (this->baseColorTexture == other.baseColorTexture) &&
  20820. (this->metallicRoughnessTexture == other.metallicRoughnessTexture) &&
  20821. Equals(this->baseColorFactor, other.baseColorFactor) &&
  20822. TINYGLTF_DOUBLE_EQUAL(this->metallicFactor, other.metallicFactor) &&
  20823. TINYGLTF_DOUBLE_EQUAL(this->roughnessFactor, other.roughnessFactor);
  20824. }
  20825. bool Value::operator==(const Value &other) const
  20826. {
  20827. return Equals(*this, other);
  20828. }
  20829. static void swap4(unsigned int *val)
  20830. {
  20831. #ifdef TINYGLTF_LITTLE_ENDIAN
  20832. (void)val;
  20833. #else
  20834. unsigned int tmp = *val;
  20835. unsigned char *dst = reinterpret_cast<unsigned char *>(val);
  20836. unsigned char *src = reinterpret_cast<unsigned char *>(&tmp);
  20837. dst[0] = src[3];
  20838. dst[1] = src[2];
  20839. dst[2] = src[1];
  20840. dst[3] = src[0];
  20841. #endif
  20842. }
  20843. static std::string JoinPath(const std::string &path0,
  20844. const std::string &path1)
  20845. {
  20846. if (path0.empty())
  20847. {
  20848. return path1;
  20849. }
  20850. else
  20851. {
  20852. // check '/'
  20853. char lastChar = *path0.rbegin();
  20854. if (lastChar != '/')
  20855. {
  20856. return path0 + std::string("/") + path1;
  20857. }
  20858. else
  20859. {
  20860. return path0 + path1;
  20861. }
  20862. }
  20863. }
  20864. static std::string FindFile(const std::vector<std::string> &paths,
  20865. const std::string &filepath, FsCallbacks *fs)
  20866. {
  20867. if (fs == nullptr || fs->ExpandFilePath == nullptr ||
  20868. fs->FileExists == nullptr)
  20869. {
  20870. // Error, fs callback[s] missing
  20871. return std::string();
  20872. }
  20873. for (size_t i = 0; i < paths.size(); i++)
  20874. {
  20875. std::string absPath =
  20876. fs->ExpandFilePath(JoinPath(paths[i], filepath), fs->user_data);
  20877. if (fs->FileExists(absPath, fs->user_data))
  20878. {
  20879. return absPath;
  20880. }
  20881. }
  20882. return std::string();
  20883. }
  20884. static std::string GetFilePathExtension(const std::string &FileName)
  20885. {
  20886. if (FileName.find_last_of(".") != std::string::npos)
  20887. return FileName.substr(FileName.find_last_of(".") + 1);
  20888. return "";
  20889. }
  20890. static std::string GetBaseDir(const std::string &filepath)
  20891. {
  20892. if (filepath.find_last_of("/\\") != std::string::npos)
  20893. return filepath.substr(0, filepath.find_last_of("/\\"));
  20894. return "";
  20895. }
  20896. // https://stackoverflow.com/questions/8520560/get-a-file-name-from-a-path
  20897. static std::string GetBaseFilename(const std::string &filepath)
  20898. {
  20899. return filepath.substr(filepath.find_last_of("/\\") + 1);
  20900. }
  20901. std::string base64_encode(unsigned char const *, unsigned int len);
  20902. std::string base64_decode(std::string const &s);
  20903. /*
  20904. base64.cpp and base64.h
  20905. Copyright (C) 2004-2008 René Nyffenegger
  20906. This source code is provided 'as-is', without any express or implied
  20907. warranty. In no event will the author be held liable for any damages
  20908. arising from the use of this software.
  20909. Permission is granted to anyone to use this software for any purpose,
  20910. including commercial applications, and to alter it and redistribute it
  20911. freely, subject to the following restrictions:
  20912. 1. The origin of this source code must not be misrepresented; you must not
  20913. claim that you wrote the original source code. If you use this source code
  20914. in a product, an acknowledgment in the product documentation would be
  20915. appreciated but is not required.
  20916. 2. Altered source versions must be plainly marked as such, and must not be
  20917. misrepresented as being the original source code.
  20918. 3. This notice may not be removed or altered from any source distribution.
  20919. René Nyffenegger [email protected]
  20920. */
  20921. #ifdef __clang__
  20922. #pragma clang diagnostic push
  20923. #pragma clang diagnostic ignored "-Wsign-conversion"
  20924. #pragma clang diagnostic ignored "-Wconversion"
  20925. #endif
  20926. static inline bool is_base64(unsigned char c)
  20927. {
  20928. return (isalnum(c) || (c == '+') || (c == '/'));
  20929. }
  20930. std::string base64_encode(unsigned char const *bytes_to_encode,
  20931. unsigned int in_len)
  20932. {
  20933. std::string ret;
  20934. int i = 0;
  20935. int j = 0;
  20936. unsigned char char_array_3[3];
  20937. unsigned char char_array_4[4];
  20938. const char *base64_chars =
  20939. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  20940. "abcdefghijklmnopqrstuvwxyz"
  20941. "0123456789+/";
  20942. while (in_len--)
  20943. {
  20944. char_array_3[i++] = *(bytes_to_encode++);
  20945. if (i == 3)
  20946. {
  20947. char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
  20948. char_array_4[1] =
  20949. ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
  20950. char_array_4[2] =
  20951. ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
  20952. char_array_4[3] = char_array_3[2] & 0x3f;
  20953. for (i = 0; (i < 4); i++) ret += base64_chars[char_array_4[i]];
  20954. i = 0;
  20955. }
  20956. }
  20957. if (i)
  20958. {
  20959. for (j = i; j < 3; j++) char_array_3[j] = '\0';
  20960. char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
  20961. char_array_4[1] =
  20962. ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
  20963. char_array_4[2] =
  20964. ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
  20965. for (j = 0; (j < i + 1); j++) ret += base64_chars[char_array_4[j]];
  20966. while ((i++ < 3)) ret += '=';
  20967. }
  20968. return ret;
  20969. }
  20970. std::string base64_decode(std::string const &encoded_string)
  20971. {
  20972. int in_len = static_cast<int>(encoded_string.size());
  20973. int i = 0;
  20974. int j = 0;
  20975. int in_ = 0;
  20976. unsigned char char_array_4[4], char_array_3[3];
  20977. std::string ret;
  20978. const std::string base64_chars =
  20979. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  20980. "abcdefghijklmnopqrstuvwxyz"
  20981. "0123456789+/";
  20982. while (in_len-- && (encoded_string[in_] != '=') &&
  20983. is_base64(encoded_string[in_]))
  20984. {
  20985. char_array_4[i++] = encoded_string[in_];
  20986. in_++;
  20987. if (i == 4)
  20988. {
  20989. for (i = 0; i < 4; i++)
  20990. char_array_4[i] =
  20991. static_cast<unsigned char>(base64_chars.find(char_array_4[i]));
  20992. char_array_3[0] =
  20993. (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  20994. char_array_3[1] =
  20995. ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  20996. char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  20997. for (i = 0; (i < 3); i++) ret += char_array_3[i];
  20998. i = 0;
  20999. }
  21000. }
  21001. if (i)
  21002. {
  21003. for (j = i; j < 4; j++) char_array_4[j] = 0;
  21004. for (j = 0; j < 4; j++)
  21005. char_array_4[j] =
  21006. static_cast<unsigned char>(base64_chars.find(char_array_4[j]));
  21007. char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  21008. char_array_3[1] =
  21009. ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  21010. char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  21011. for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
  21012. }
  21013. return ret;
  21014. }
  21015. #ifdef __clang__
  21016. #pragma clang diagnostic pop
  21017. #endif
  21018. // https://github.com/syoyo/tinygltf/issues/228
  21019. // TODO(syoyo): Use uriparser https://uriparser.github.io/ for stricter Uri
  21020. // decoding?
  21021. //
  21022. // https://stackoverflow.com/questions/18307429/encode-decode-url-in-c
  21023. // http://dlib.net/dlib/server/server_http.cpp.html
  21024. // --- dlib beign ------------------------------------------------------------
  21025. // Copyright (C) 2003 Davis E. King ([email protected])
  21026. // License: Boost Software License See LICENSE.txt for the full license.
  21027. namespace dlib
  21028. {
  21029. #if 0
  21030. inline unsigned char to_hex(unsigned char x)
  21031. {
  21032. return x + (x > 9 ? ('A' - 10) : '0');
  21033. }
  21034. const std::string urlencode(const std::string &s)
  21035. {
  21036. std::ostringstream os;
  21037. for (std::string::const_iterator ci = s.begin(); ci != s.end(); ++ci)
  21038. {
  21039. if ((*ci >= 'a' && *ci <= 'z') ||
  21040. (*ci >= 'A' && *ci <= 'Z') ||
  21041. (*ci >= '0' && *ci <= '9'))
  21042. { // allowed
  21043. os << *ci;
  21044. }
  21045. else if (*ci == ' ')
  21046. {
  21047. os << '+';
  21048. }
  21049. else
  21050. {
  21051. os << '%' << to_hex(static_cast<unsigned char>(*ci >> 4)) << to_hex(static_cast<unsigned char>(*ci % 16));
  21052. }
  21053. }
  21054. return os.str();
  21055. }
  21056. #endif
  21057. inline unsigned char from_hex(unsigned char ch)
  21058. {
  21059. if (ch <= '9' && ch >= '0')
  21060. ch -= '0';
  21061. else if (ch <= 'f' && ch >= 'a')
  21062. ch -= 'a' - 10;
  21063. else if (ch <= 'F' && ch >= 'A')
  21064. ch -= 'A' - 10;
  21065. else
  21066. ch = 0;
  21067. return ch;
  21068. }
  21069. static const std::string urldecode(const std::string &str)
  21070. {
  21071. using namespace std;
  21072. string result;
  21073. string::size_type i;
  21074. for (i = 0; i < str.size(); ++i)
  21075. {
  21076. if (str[i] == '+')
  21077. {
  21078. result += ' ';
  21079. }
  21080. else if (str[i] == '%' && str.size() > i + 2)
  21081. {
  21082. const unsigned char ch1 =
  21083. from_hex(static_cast<unsigned char>(str[i + 1]));
  21084. const unsigned char ch2 =
  21085. from_hex(static_cast<unsigned char>(str[i + 2]));
  21086. const unsigned char ch = static_cast<unsigned char>((ch1 << 4) | ch2);
  21087. result += static_cast<char>(ch);
  21088. i += 2;
  21089. }
  21090. else
  21091. {
  21092. result += str[i];
  21093. }
  21094. }
  21095. return result;
  21096. }
  21097. } // namespace dlib
  21098. // --- dlib end --------------------------------------------------------------
  21099. static bool LoadExternalFile(std::vector<unsigned char> *out, std::string *err,
  21100. std::string *warn, const std::string &filename,
  21101. const std::string &basedir, bool required,
  21102. size_t reqBytes, bool checkSize, FsCallbacks *fs)
  21103. {
  21104. if (fs == nullptr || fs->FileExists == nullptr ||
  21105. fs->ExpandFilePath == nullptr || fs->ReadWholeFile == nullptr)
  21106. {
  21107. // This is a developer error, assert() ?
  21108. if (err)
  21109. {
  21110. (*err) += "FS callback[s] not set\n";
  21111. }
  21112. return false;
  21113. }
  21114. std::string *failMsgOut = required ? err : warn;
  21115. out->clear();
  21116. std::vector<std::string> paths;
  21117. paths.push_back(basedir);
  21118. paths.push_back(".");
  21119. std::string filepath = FindFile(paths, filename, fs);
  21120. if (filepath.empty() || filename.empty())
  21121. {
  21122. if (failMsgOut)
  21123. {
  21124. (*failMsgOut) += "File not found : " + filename + "\n";
  21125. }
  21126. return false;
  21127. }
  21128. std::vector<unsigned char> buf;
  21129. std::string fileReadErr;
  21130. bool fileRead =
  21131. fs->ReadWholeFile(&buf, &fileReadErr, filepath, fs->user_data);
  21132. if (!fileRead)
  21133. {
  21134. if (failMsgOut)
  21135. {
  21136. (*failMsgOut) +=
  21137. "File read error : " + filepath + " : " + fileReadErr + "\n";
  21138. }
  21139. return false;
  21140. }
  21141. size_t sz = buf.size();
  21142. if (sz == 0)
  21143. {
  21144. if (failMsgOut)
  21145. {
  21146. (*failMsgOut) += "File is empty : " + filepath + "\n";
  21147. }
  21148. return false;
  21149. }
  21150. if (checkSize)
  21151. {
  21152. if (reqBytes == sz)
  21153. {
  21154. out->swap(buf);
  21155. return true;
  21156. }
  21157. else
  21158. {
  21159. std::stringstream ss;
  21160. ss << "File size mismatch : " << filepath << ", requestedBytes "
  21161. << reqBytes << ", but got " << sz << std::endl;
  21162. if (failMsgOut)
  21163. {
  21164. (*failMsgOut) += ss.str();
  21165. }
  21166. return false;
  21167. }
  21168. }
  21169. out->swap(buf);
  21170. return true;
  21171. }
  21172. void TinyGLTF::SetImageLoader(LoadImageDataFunction func, void *user_data)
  21173. {
  21174. LoadImageData = func;
  21175. load_image_user_data_ = user_data;
  21176. user_image_loader_ = true;
  21177. }
  21178. void TinyGLTF::RemoveImageLoader()
  21179. {
  21180. LoadImageData =
  21181. #ifndef TINYGLTF_NO_STB_IMAGE
  21182. & tinygltf::LoadImageData;
  21183. #else
  21184. nullptr;
  21185. #endif
  21186. load_image_user_data_ = nullptr;
  21187. user_image_loader_ = false;
  21188. }
  21189. #ifndef TINYGLTF_NO_STB_IMAGE
  21190. bool LoadImageData(Image *image, const int image_idx, std::string *err,
  21191. std::string *warn, int req_width, int req_height,
  21192. const unsigned char *bytes, int size, void *user_data)
  21193. {
  21194. (void)warn;
  21195. LoadImageDataOption option;
  21196. if (user_data)
  21197. {
  21198. option = *reinterpret_cast<LoadImageDataOption *>(user_data);
  21199. }
  21200. int w = 0, h = 0, comp = 0, req_comp = 0;
  21201. unsigned char *data = nullptr;
  21202. // preserve_channels true: Use channels stored in the image file.
  21203. // false: force 32-bit textures for common Vulkan compatibility. It appears that
  21204. // some GPU drivers do not support 24-bit images for Vulkan
  21205. req_comp = option.preserve_channels ? 0 : 4;
  21206. int bits = 8;
  21207. int pixel_type = TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE;
  21208. // It is possible that the image we want to load is a 16bit per channel image
  21209. // We are going to attempt to load it as 16bit per channel, and if it worked,
  21210. // set the image data accodingly. We are casting the returned pointer into
  21211. // unsigned char, because we are representing "bytes". But we are updating
  21212. // the Image metadata to signal that this image uses 2 bytes (16bits) per
  21213. // channel:
  21214. if (stbi_is_16_bit_from_memory(bytes, size))
  21215. {
  21216. data = reinterpret_cast<unsigned char *>(
  21217. stbi_load_16_from_memory(bytes, size, &w, &h, &comp, req_comp));
  21218. if (data)
  21219. {
  21220. bits = 16;
  21221. pixel_type = TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT;
  21222. }
  21223. }
  21224. // at this point, if data is still NULL, it means that the image wasn't
  21225. // 16bit per channel, we are going to load it as a normal 8bit per channel
  21226. // mage as we used to do:
  21227. // if image cannot be decoded, ignore parsing and keep it by its path
  21228. // don't break in this case
  21229. // FIXME we should only enter this function if the image is embedded. If
  21230. // image->uri references
  21231. // an image file, it should be left as it is. Image loading should not be
  21232. // mandatory (to support other formats)
  21233. if (!data) data = stbi_load_from_memory(bytes, size, &w, &h, &comp, req_comp);
  21234. if (!data)
  21235. {
  21236. // NOTE: you can use `warn` instead of `err`
  21237. if (err)
  21238. {
  21239. (*err) +=
  21240. "Unknown image format. STB cannot decode image data for image[" +
  21241. std::to_string(image_idx) + "] name = \"" + image->name + "\".\n";
  21242. }
  21243. return false;
  21244. }
  21245. if ((w < 1) || (h < 1))
  21246. {
  21247. stbi_image_free(data);
  21248. if (err)
  21249. {
  21250. (*err) += "Invalid image data for image[" + std::to_string(image_idx) +
  21251. "] name = \"" + image->name + "\"\n";
  21252. }
  21253. return false;
  21254. }
  21255. if (req_width > 0)
  21256. {
  21257. if (req_width != w)
  21258. {
  21259. stbi_image_free(data);
  21260. if (err)
  21261. {
  21262. (*err) += "Image width mismatch for image[" +
  21263. std::to_string(image_idx) + "] name = \"" + image->name +
  21264. "\"\n";
  21265. }
  21266. return false;
  21267. }
  21268. }
  21269. if (req_height > 0)
  21270. {
  21271. if (req_height != h)
  21272. {
  21273. stbi_image_free(data);
  21274. if (err)
  21275. {
  21276. (*err) += "Image height mismatch. for image[" +
  21277. std::to_string(image_idx) + "] name = \"" + image->name +
  21278. "\"\n";
  21279. }
  21280. return false;
  21281. }
  21282. }
  21283. if (req_comp != 0)
  21284. {
  21285. // loaded data has `req_comp` channels(components)
  21286. comp = req_comp;
  21287. }
  21288. image->width = w;
  21289. image->height = h;
  21290. image->component = comp;
  21291. image->bits = bits;
  21292. image->pixel_type = pixel_type;
  21293. image->image.resize(static_cast<size_t>(w * h * comp) * size_t(bits / 8));
  21294. std::copy(data, data + w * h * comp * (bits / 8), image->image.begin());
  21295. stbi_image_free(data);
  21296. return true;
  21297. }
  21298. #endif
  21299. void TinyGLTF::SetImageWriter(WriteImageDataFunction func, void *user_data)
  21300. {
  21301. WriteImageData = func;
  21302. write_image_user_data_ = user_data;
  21303. }
  21304. #ifndef TINYGLTF_NO_STB_IMAGE_WRITE
  21305. static void WriteToMemory_stbi(void *context, void *data, int size)
  21306. {
  21307. std::vector<unsigned char> *buffer =
  21308. reinterpret_cast<std::vector<unsigned char> *>(context);
  21309. unsigned char *pData = reinterpret_cast<unsigned char *>(data);
  21310. buffer->insert(buffer->end(), pData, pData + size);
  21311. }
  21312. bool WriteImageData(const std::string *basepath, const std::string *filename,
  21313. Image *image, bool embedImages, void *fsPtr)
  21314. {
  21315. const std::string ext = GetFilePathExtension(*filename);
  21316. // Write image to temporary buffer
  21317. std::string header;
  21318. std::vector<unsigned char> data;
  21319. if (ext == "png")
  21320. {
  21321. if ((image->bits != 8) ||
  21322. (image->pixel_type != TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE))
  21323. {
  21324. // Unsupported pixel format
  21325. return false;
  21326. }
  21327. if (!stbi_write_png_to_func(WriteToMemory_stbi, &data, image->width,
  21328. image->height, image->component,
  21329. &image->image[0], 0))
  21330. {
  21331. return false;
  21332. }
  21333. header = "data:image/png;base64,";
  21334. }
  21335. else if (ext == "jpg")
  21336. {
  21337. if (!stbi_write_jpg_to_func(WriteToMemory_stbi, &data, image->width,
  21338. image->height, image->component,
  21339. &image->image[0], 100))
  21340. {
  21341. return false;
  21342. }
  21343. header = "data:image/jpeg;base64,";
  21344. }
  21345. else if (ext == "bmp")
  21346. {
  21347. if (!stbi_write_bmp_to_func(WriteToMemory_stbi, &data, image->width,
  21348. image->height, image->component,
  21349. &image->image[0]))
  21350. {
  21351. return false;
  21352. }
  21353. header = "data:image/bmp;base64,";
  21354. }
  21355. else if (!embedImages)
  21356. {
  21357. // Error: can't output requested format to file
  21358. return false;
  21359. }
  21360. if (embedImages)
  21361. {
  21362. // Embed base64-encoded image into URI
  21363. if (data.size())
  21364. {
  21365. image->uri =
  21366. header +
  21367. base64_encode(&data[0], static_cast<unsigned int>(data.size()));
  21368. }
  21369. else
  21370. {
  21371. // Throw error?
  21372. }
  21373. }
  21374. else
  21375. {
  21376. // Write image to disc
  21377. FsCallbacks *fs = reinterpret_cast<FsCallbacks *>(fsPtr);
  21378. if ((fs != nullptr) && (fs->WriteWholeFile != nullptr))
  21379. {
  21380. const std::string imagefilepath = JoinPath(*basepath, *filename);
  21381. std::string writeError;
  21382. if (!fs->WriteWholeFile(&writeError, imagefilepath, data,
  21383. fs->user_data))
  21384. {
  21385. // Could not write image file to disc; Throw error ?
  21386. return false;
  21387. }
  21388. }
  21389. else
  21390. {
  21391. // Throw error?
  21392. }
  21393. image->uri = *filename;
  21394. }
  21395. return true;
  21396. }
  21397. #endif
  21398. void TinyGLTF::SetFsCallbacks(FsCallbacks callbacks) { fs = callbacks; }
  21399. #ifdef _WIN32
  21400. static inline std::wstring UTF8ToWchar(const std::string &str)
  21401. {
  21402. int wstr_size =
  21403. MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), nullptr, 0);
  21404. std::wstring wstr(wstr_size, 0);
  21405. MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), &wstr[0],
  21406. (int)wstr.size());
  21407. return wstr;
  21408. }
  21409. static inline std::string WcharToUTF8(const std::wstring &wstr)
  21410. {
  21411. int str_size = WideCharToMultiByte(CP_UTF8, 0, wstr.data(), (int)wstr.size(),
  21412. nullptr, 0, NULL, NULL);
  21413. std::string str(str_size, 0);
  21414. WideCharToMultiByte(CP_UTF8, 0, wstr.data(), (int)wstr.size(), &str[0],
  21415. (int)str.size(), NULL, NULL);
  21416. return str;
  21417. }
  21418. #endif
  21419. #ifndef TINYGLTF_NO_FS
  21420. // Default implementations of filesystem functions
  21421. bool FileExists(const std::string &abs_filename, void *)
  21422. {
  21423. bool ret;
  21424. #ifdef TINYGLTF_ANDROID_LOAD_FROM_ASSETS
  21425. if (asset_manager)
  21426. {
  21427. AAsset *asset = AAssetManager_open(asset_manager, abs_filename.c_str(),
  21428. AASSET_MODE_STREAMING);
  21429. if (!asset)
  21430. {
  21431. return false;
  21432. }
  21433. AAsset_close(asset);
  21434. ret = true;
  21435. }
  21436. else
  21437. {
  21438. return false;
  21439. }
  21440. #else
  21441. #ifdef _WIN32
  21442. #if defined(_MSC_VER) || defined(__GLIBCXX__)
  21443. FILE *fp = nullptr;
  21444. errno_t err = _wfopen_s(&fp, UTF8ToWchar(abs_filename).c_str(), L"rb");
  21445. if (err != 0)
  21446. {
  21447. return false;
  21448. }
  21449. #else
  21450. FILE *fp = nullptr;
  21451. errno_t err = fopen_s(&fp, abs_filename.c_str(), "rb");
  21452. if (err != 0)
  21453. {
  21454. return false;
  21455. }
  21456. #endif
  21457. #else
  21458. FILE *fp = fopen(abs_filename.c_str(), "rb");
  21459. #endif
  21460. if (fp)
  21461. {
  21462. ret = true;
  21463. fclose(fp);
  21464. }
  21465. else
  21466. {
  21467. ret = false;
  21468. }
  21469. #endif
  21470. return ret;
  21471. }
  21472. std::string ExpandFilePath(const std::string &filepath, void *)
  21473. {
  21474. #ifdef _WIN32
  21475. // Assume input `filepath` is encoded in UTF-8
  21476. std::wstring wfilepath = UTF8ToWchar(filepath);
  21477. DWORD wlen = ExpandEnvironmentStringsW(wfilepath.c_str(), nullptr, 0);
  21478. wchar_t *wstr = new wchar_t[wlen];
  21479. ExpandEnvironmentStringsW(wfilepath.c_str(), wstr, wlen);
  21480. std::wstring ws(wstr);
  21481. delete[] wstr;
  21482. return WcharToUTF8(ws);
  21483. #else
  21484. #if defined(TARGET_OS_IPHONE) || defined(TARGET_IPHONE_SIMULATOR) || \
  21485. defined(__ANDROID__) || defined(__EMSCRIPTEN__)
  21486. // no expansion
  21487. std::string s = filepath;
  21488. #else
  21489. std::string s;
  21490. wordexp_t p;
  21491. if (filepath.empty())
  21492. {
  21493. return "";
  21494. }
  21495. // Quote the string to keep any spaces in filepath intact.
  21496. std::string quoted_path = "\"" + filepath + "\"";
  21497. // char** w;
  21498. int ret = wordexp(quoted_path.c_str(), &p, 0);
  21499. if (ret)
  21500. {
  21501. // err
  21502. s = filepath;
  21503. return s;
  21504. }
  21505. // Use first element only.
  21506. if (p.we_wordv)
  21507. {
  21508. s = std::string(p.we_wordv[0]);
  21509. wordfree(&p);
  21510. }
  21511. else
  21512. {
  21513. s = filepath;
  21514. }
  21515. #endif
  21516. return s;
  21517. #endif
  21518. }
  21519. //removed default write
  21520. bool ReadWholeFile(std::vector<unsigned char> *out, std::string *err,
  21521. const std::string &filepath, void *)
  21522. {
  21523. assert(0);
  21524. /*
  21525. #ifdef TINYGLTF_ANDROID_LOAD_FROM_ASSETS
  21526. if (asset_manager)
  21527. {
  21528. AAsset *asset = AAssetManager_open(asset_manager, filepath.c_str(),
  21529. AASSET_MODE_STREAMING);
  21530. if (!asset)
  21531. {
  21532. if (err)
  21533. {
  21534. (*err) += "File open error : " + filepath + "\n";
  21535. }
  21536. return false;
  21537. }
  21538. size_t size = AAsset_getLength(asset);
  21539. if (size == 0)
  21540. {
  21541. if (err)
  21542. {
  21543. (*err) += "Invalid file size : " + filepath +
  21544. " (does the path point to a directory?)";
  21545. }
  21546. return false;
  21547. }
  21548. out->resize(size);
  21549. AAsset_read(asset, reinterpret_cast<char *>(&out->at(0)), size);
  21550. AAsset_close(asset);
  21551. return true;
  21552. }
  21553. else
  21554. {
  21555. if (err)
  21556. {
  21557. (*err) += "No asset manager specified : " + filepath + "\n";
  21558. }
  21559. return false;
  21560. }
  21561. #else
  21562. #ifdef _WIN32
  21563. #if defined(__GLIBCXX__) // mingw
  21564. int file_descriptor =
  21565. _wopen(UTF8ToWchar(filepath).c_str(), _O_RDONLY | _O_BINARY);
  21566. __gnu_cxx::stdio_filebuf<char> wfile_buf(file_descriptor, std::ios_base::in);
  21567. std::istream f(&wfile_buf);
  21568. #elif defined(_MSC_VER) || defined(_LIBCPP_VERSION)
  21569. // For libcxx, assume _LIBCPP_HAS_OPEN_WITH_WCHAR is defined to accept
  21570. // `wchar_t *`
  21571. std::ifstream f(UTF8ToWchar(filepath).c_str(), std::ifstream::binary);
  21572. #else
  21573. // Unknown compiler/runtime
  21574. std::ifstream f(filepath.c_str(), std::ifstream::binary);
  21575. #endif
  21576. #else
  21577. std::ifstream f(filepath.c_str(), std::ifstream::binary);
  21578. #endif
  21579. if (!f)
  21580. {
  21581. if (err)
  21582. {
  21583. (*err) += "File open error : " + filepath + "\n";
  21584. }
  21585. return false;
  21586. }
  21587. f.seekg(0, f.end);
  21588. size_t sz = static_cast<size_t>(f.tellg());
  21589. f.seekg(0, f.beg);
  21590. if (int64_t(sz) < 0)
  21591. {
  21592. if (err)
  21593. {
  21594. (*err) += "Invalid file size : " + filepath +
  21595. " (does the path point to a directory?)";
  21596. }
  21597. return false;
  21598. }
  21599. else if (sz == 0)
  21600. {
  21601. if (err)
  21602. {
  21603. (*err) += "File is empty : " + filepath + "\n";
  21604. }
  21605. return false;
  21606. }
  21607. out->resize(sz);
  21608. f.read(reinterpret_cast<char *>(&out->at(0)),
  21609. static_cast<std::streamsize>(sz));
  21610. return true;
  21611. #endif
  21612. */
  21613. return false;
  21614. }
  21615. //removed writing data
  21616. bool WriteWholeFile(std::string *err, const std::string &filepath,
  21617. const std::vector<unsigned char> &contents, void *)
  21618. {
  21619. assert(0);
  21620. /*
  21621. #ifdef _WIN32
  21622. #if defined(__GLIBCXX__) // mingw
  21623. int file_descriptor = _wopen(UTF8ToWchar(filepath).c_str(),
  21624. _O_CREAT | _O_WRONLY | _O_TRUNC | _O_BINARY);
  21625. __gnu_cxx::stdio_filebuf<char> wfile_buf(
  21626. file_descriptor, std::ios_base::out | std::ios_base::binary);
  21627. std::ostream f(&wfile_buf);
  21628. #elif defined(_MSC_VER)
  21629. std::ofstream f(UTF8ToWchar(filepath).c_str(), std::ofstream::binary);
  21630. #else // clang?
  21631. std::ofstream f(filepath.c_str(), std::ofstream::binary);
  21632. #endif
  21633. #else
  21634. std::ofstream f(filepath.c_str(), std::ofstream::binary);
  21635. #endif
  21636. if (!f)
  21637. {
  21638. if (err)
  21639. {
  21640. (*err) += "File open error for writing : " + filepath + "\n";
  21641. }
  21642. return false;
  21643. }
  21644. f.write(reinterpret_cast<const char *>(&contents.at(0)),
  21645. static_cast<std::streamsize>(contents.size()));
  21646. if (!f)
  21647. {
  21648. if (err)
  21649. {
  21650. (*err) += "File write error: " + filepath + "\n";
  21651. }
  21652. return false;
  21653. }
  21654. */
  21655. return true;
  21656. }
  21657. #endif // TINYGLTF_NO_FS
  21658. static std::string MimeToExt(const std::string &mimeType)
  21659. {
  21660. if (mimeType == "image/jpeg")
  21661. {
  21662. return "jpg";
  21663. }
  21664. else if (mimeType == "image/png")
  21665. {
  21666. return "png";
  21667. }
  21668. else if (mimeType == "image/bmp")
  21669. {
  21670. return "bmp";
  21671. }
  21672. else if (mimeType == "image/gif")
  21673. {
  21674. return "gif";
  21675. }
  21676. return "";
  21677. }
  21678. static void UpdateImageObject(Image &image, std::string &baseDir, int index,
  21679. bool embedImages,
  21680. WriteImageDataFunction *WriteImageData = nullptr,
  21681. void *user_data = nullptr)
  21682. {
  21683. std::string filename;
  21684. std::string ext;
  21685. // If image has uri, use it it as a filename
  21686. if (image.uri.size())
  21687. {
  21688. filename = GetBaseFilename(image.uri);
  21689. ext = GetFilePathExtension(filename);
  21690. }
  21691. else if (image.bufferView != -1)
  21692. {
  21693. // If there's no URI and the data exists in a buffer,
  21694. // don't change properties or write images
  21695. }
  21696. else if (image.name.size())
  21697. {
  21698. ext = MimeToExt(image.mimeType);
  21699. // Otherwise use name as filename
  21700. filename = image.name + "." + ext;
  21701. }
  21702. else
  21703. {
  21704. ext = MimeToExt(image.mimeType);
  21705. // Fallback to index of image as filename
  21706. filename = std::to_string(index) + "." + ext;
  21707. }
  21708. // If callback is set, modify image data object
  21709. if (*WriteImageData != nullptr && !filename.empty())
  21710. {
  21711. std::string uri;
  21712. (*WriteImageData)(&baseDir, &filename, &image, embedImages, user_data);
  21713. }
  21714. }
  21715. bool IsDataURI(const std::string &in)
  21716. {
  21717. std::string header = "data:application/octet-stream;base64,";
  21718. if (in.find(header) == 0)
  21719. {
  21720. return true;
  21721. }
  21722. header = "data:image/jpeg;base64,";
  21723. if (in.find(header) == 0)
  21724. {
  21725. return true;
  21726. }
  21727. header = "data:image/png;base64,";
  21728. if (in.find(header) == 0)
  21729. {
  21730. return true;
  21731. }
  21732. header = "data:image/bmp;base64,";
  21733. if (in.find(header) == 0)
  21734. {
  21735. return true;
  21736. }
  21737. header = "data:image/gif;base64,";
  21738. if (in.find(header) == 0)
  21739. {
  21740. return true;
  21741. }
  21742. header = "data:text/plain;base64,";
  21743. if (in.find(header) == 0)
  21744. {
  21745. return true;
  21746. }
  21747. header = "data:application/gltf-buffer;base64,";
  21748. if (in.find(header) == 0)
  21749. {
  21750. return true;
  21751. }
  21752. return false;
  21753. }
  21754. bool DecodeDataURI(std::vector<unsigned char> *out, std::string &mime_type,
  21755. const std::string &in, size_t reqBytes, bool checkSize)
  21756. {
  21757. std::string header = "data:application/octet-stream;base64,";
  21758. std::string data;
  21759. if (in.find(header) == 0)
  21760. {
  21761. data = base64_decode(in.substr(header.size())); // cut mime string.
  21762. }
  21763. if (data.empty())
  21764. {
  21765. header = "data:image/jpeg;base64,";
  21766. if (in.find(header) == 0)
  21767. {
  21768. mime_type = "image/jpeg";
  21769. data = base64_decode(in.substr(header.size())); // cut mime string.
  21770. }
  21771. }
  21772. if (data.empty())
  21773. {
  21774. header = "data:image/png;base64,";
  21775. if (in.find(header) == 0)
  21776. {
  21777. mime_type = "image/png";
  21778. data = base64_decode(in.substr(header.size())); // cut mime string.
  21779. }
  21780. }
  21781. if (data.empty())
  21782. {
  21783. header = "data:image/bmp;base64,";
  21784. if (in.find(header) == 0)
  21785. {
  21786. mime_type = "image/bmp";
  21787. data = base64_decode(in.substr(header.size())); // cut mime string.
  21788. }
  21789. }
  21790. if (data.empty())
  21791. {
  21792. header = "data:image/gif;base64,";
  21793. if (in.find(header) == 0)
  21794. {
  21795. mime_type = "image/gif";
  21796. data = base64_decode(in.substr(header.size())); // cut mime string.
  21797. }
  21798. }
  21799. if (data.empty())
  21800. {
  21801. header = "data:text/plain;base64,";
  21802. if (in.find(header) == 0)
  21803. {
  21804. mime_type = "text/plain";
  21805. data = base64_decode(in.substr(header.size()));
  21806. }
  21807. }
  21808. if (data.empty())
  21809. {
  21810. header = "data:application/gltf-buffer;base64,";
  21811. if (in.find(header) == 0)
  21812. {
  21813. data = base64_decode(in.substr(header.size()));
  21814. }
  21815. }
  21816. // TODO(syoyo): Allow empty buffer? #229
  21817. if (data.empty())
  21818. {
  21819. return false;
  21820. }
  21821. if (checkSize)
  21822. {
  21823. if (data.size() != reqBytes)
  21824. {
  21825. return false;
  21826. }
  21827. out->resize(reqBytes);
  21828. }
  21829. else
  21830. {
  21831. out->resize(data.size());
  21832. }
  21833. std::copy(data.begin(), data.end(), out->begin());
  21834. return true;
  21835. }
  21836. namespace
  21837. {
  21838. bool GetInt(const json &o, int &val)
  21839. {
  21840. #ifdef TINYGLTF_USE_RAPIDJSON
  21841. if (!o.IsDouble())
  21842. {
  21843. if (o.IsInt())
  21844. {
  21845. val = o.GetInt();
  21846. return true;
  21847. }
  21848. else if (o.IsUint())
  21849. {
  21850. val = static_cast<int>(o.GetUint());
  21851. return true;
  21852. }
  21853. else if (o.IsInt64())
  21854. {
  21855. val = static_cast<int>(o.GetInt64());
  21856. return true;
  21857. }
  21858. else if (o.IsUint64())
  21859. {
  21860. val = static_cast<int>(o.GetUint64());
  21861. return true;
  21862. }
  21863. }
  21864. return false;
  21865. #else
  21866. auto type = o.type();
  21867. if ((type == json::value_t::number_integer) ||
  21868. (type == json::value_t::number_unsigned))
  21869. {
  21870. val = static_cast<int>(o.get<int64_t>());
  21871. return true;
  21872. }
  21873. return false;
  21874. #endif
  21875. }
  21876. #ifdef TINYGLTF_USE_RAPIDJSON
  21877. bool GetDouble(const json &o, double &val)
  21878. {
  21879. if (o.IsDouble())
  21880. {
  21881. val = o.GetDouble();
  21882. return true;
  21883. }
  21884. return false;
  21885. }
  21886. #endif
  21887. bool GetNumber(const json &o, double &val)
  21888. {
  21889. #ifdef TINYGLTF_USE_RAPIDJSON
  21890. if (o.IsNumber())
  21891. {
  21892. val = o.GetDouble();
  21893. return true;
  21894. }
  21895. return false;
  21896. #else
  21897. if (o.is_number())
  21898. {
  21899. val = o.get<double>();
  21900. return true;
  21901. }
  21902. return false;
  21903. #endif
  21904. }
  21905. bool GetString(const json &o, std::string &val)
  21906. {
  21907. #ifdef TINYGLTF_USE_RAPIDJSON
  21908. if (o.IsString())
  21909. {
  21910. val = o.GetString();
  21911. return true;
  21912. }
  21913. return false;
  21914. #else
  21915. if (o.type() == json::value_t::string)
  21916. {
  21917. val = o.get<std::string>();
  21918. return true;
  21919. }
  21920. return false;
  21921. #endif
  21922. }
  21923. bool IsArray(const json &o)
  21924. {
  21925. #ifdef TINYGLTF_USE_RAPIDJSON
  21926. return o.IsArray();
  21927. #else
  21928. return o.is_array();
  21929. #endif
  21930. }
  21931. json_const_array_iterator ArrayBegin(const json &o)
  21932. {
  21933. #ifdef TINYGLTF_USE_RAPIDJSON
  21934. return o.Begin();
  21935. #else
  21936. return o.begin();
  21937. #endif
  21938. }
  21939. json_const_array_iterator ArrayEnd(const json &o)
  21940. {
  21941. #ifdef TINYGLTF_USE_RAPIDJSON
  21942. return o.End();
  21943. #else
  21944. return o.end();
  21945. #endif
  21946. }
  21947. bool IsObject(const json &o)
  21948. {
  21949. #ifdef TINYGLTF_USE_RAPIDJSON
  21950. return o.IsObject();
  21951. #else
  21952. return o.is_object();
  21953. #endif
  21954. }
  21955. json_const_iterator ObjectBegin(const json &o)
  21956. {
  21957. #ifdef TINYGLTF_USE_RAPIDJSON
  21958. return o.MemberBegin();
  21959. #else
  21960. return o.begin();
  21961. #endif
  21962. }
  21963. json_const_iterator ObjectEnd(const json &o)
  21964. {
  21965. #ifdef TINYGLTF_USE_RAPIDJSON
  21966. return o.MemberEnd();
  21967. #else
  21968. return o.end();
  21969. #endif
  21970. }
  21971. // Making this a const char* results in a pointer to a temporary when
  21972. // TINYGLTF_USE_RAPIDJSON is off.
  21973. std::string GetKey(json_const_iterator &it)
  21974. {
  21975. #ifdef TINYGLTF_USE_RAPIDJSON
  21976. return it->name.GetString();
  21977. #else
  21978. return it.key().c_str();
  21979. #endif
  21980. }
  21981. bool FindMember(const json &o, const char *member, json_const_iterator &it)
  21982. {
  21983. #ifdef TINYGLTF_USE_RAPIDJSON
  21984. if (!o.IsObject())
  21985. {
  21986. return false;
  21987. }
  21988. it = o.FindMember(member);
  21989. return it != o.MemberEnd();
  21990. #else
  21991. it = o.find(member);
  21992. return it != o.end();
  21993. #endif
  21994. }
  21995. const json &GetValue(json_const_iterator &it)
  21996. {
  21997. #ifdef TINYGLTF_USE_RAPIDJSON
  21998. return it->value;
  21999. #else
  22000. return it.value();
  22001. #endif
  22002. }
  22003. std::string JsonToString(const json &o, int spacing = -1)
  22004. {
  22005. #ifdef TINYGLTF_USE_RAPIDJSON
  22006. using namespace rapidjson;
  22007. StringBuffer buffer;
  22008. if (spacing == -1)
  22009. {
  22010. Writer<StringBuffer> writer(buffer);
  22011. o.Accept(writer);
  22012. }
  22013. else
  22014. {
  22015. PrettyWriter<StringBuffer> writer(buffer);
  22016. writer.SetIndent(' ', uint32_t(spacing));
  22017. o.Accept(writer);
  22018. }
  22019. return buffer.GetString();
  22020. #else
  22021. return o.dump(spacing);
  22022. #endif
  22023. }
  22024. } // namespace
  22025. static bool ParseJsonAsValue(Value *ret, const json &o)
  22026. {
  22027. Value val{};
  22028. #ifdef TINYGLTF_USE_RAPIDJSON
  22029. using rapidjson::Type;
  22030. switch (o.GetType())
  22031. {
  22032. case Type::kObjectType:
  22033. {
  22034. Value::Object value_object;
  22035. for (auto it = o.MemberBegin(); it != o.MemberEnd(); ++it)
  22036. {
  22037. Value entry;
  22038. ParseJsonAsValue(&entry, it->value);
  22039. if (entry.Type() != NULL_TYPE)
  22040. value_object.emplace(GetKey(it), std::move(entry));
  22041. }
  22042. if (value_object.size() > 0) val = Value(std::move(value_object));
  22043. } break;
  22044. case Type::kArrayType:
  22045. {
  22046. Value::Array value_array;
  22047. value_array.reserve(o.Size());
  22048. for (auto it = o.Begin(); it != o.End(); ++it)
  22049. {
  22050. Value entry;
  22051. ParseJsonAsValue(&entry, *it);
  22052. if (entry.Type() != NULL_TYPE)
  22053. value_array.emplace_back(std::move(entry));
  22054. }
  22055. if (value_array.size() > 0) val = Value(std::move(value_array));
  22056. } break;
  22057. case Type::kStringType:
  22058. val = Value(std::string(o.GetString()));
  22059. break;
  22060. case Type::kFalseType:
  22061. case Type::kTrueType:
  22062. val = Value(o.GetBool());
  22063. break;
  22064. case Type::kNumberType:
  22065. if (!o.IsDouble())
  22066. {
  22067. int i = 0;
  22068. GetInt(o, i);
  22069. val = Value(i);
  22070. }
  22071. else
  22072. {
  22073. double d = 0.0;
  22074. GetDouble(o, d);
  22075. val = Value(d);
  22076. }
  22077. break;
  22078. case Type::kNullType:
  22079. break;
  22080. // all types are covered, so no `case default`
  22081. }
  22082. #else
  22083. switch (o.type())
  22084. {
  22085. case json::value_t::object:
  22086. {
  22087. Value::Object value_object;
  22088. for (auto it = o.begin(); it != o.end(); it++)
  22089. {
  22090. Value entry;
  22091. ParseJsonAsValue(&entry, it.value());
  22092. if (entry.Type() != NULL_TYPE)
  22093. value_object.emplace(it.key(), std::move(entry));
  22094. }
  22095. if (value_object.size() > 0) val = Value(std::move(value_object));
  22096. } break;
  22097. case json::value_t::array:
  22098. {
  22099. Value::Array value_array;
  22100. value_array.reserve(o.size());
  22101. for (auto it = o.begin(); it != o.end(); it++)
  22102. {
  22103. Value entry;
  22104. ParseJsonAsValue(&entry, it.value());
  22105. if (entry.Type() != NULL_TYPE)
  22106. value_array.emplace_back(std::move(entry));
  22107. }
  22108. if (value_array.size() > 0) val = Value(std::move(value_array));
  22109. } break;
  22110. case json::value_t::string:
  22111. val = Value(o.get<std::string>());
  22112. break;
  22113. case json::value_t::boolean:
  22114. val = Value(o.get<bool>());
  22115. break;
  22116. case json::value_t::number_integer:
  22117. case json::value_t::number_unsigned:
  22118. val = Value(static_cast<int>(o.get<int64_t>()));
  22119. break;
  22120. case json::value_t::number_float:
  22121. val = Value(o.get<double>());
  22122. break;
  22123. case json::value_t::null:
  22124. case json::value_t::discarded:
  22125. // default:
  22126. break;
  22127. }
  22128. #endif
  22129. if (ret) *ret = std::move(val);
  22130. return val.Type() != NULL_TYPE;
  22131. }
  22132. static bool ParseExtrasProperty(Value *ret, const json &o)
  22133. {
  22134. json_const_iterator it;
  22135. if (!FindMember(o, "extras", it))
  22136. {
  22137. return false;
  22138. }
  22139. return ParseJsonAsValue(ret, GetValue(it));
  22140. }
  22141. static bool ParseBooleanProperty(bool *ret, std::string *err, const json &o,
  22142. const std::string &property,
  22143. const bool required,
  22144. const std::string &parent_node = "")
  22145. {
  22146. json_const_iterator it;
  22147. if (!FindMember(o, property.c_str(), it))
  22148. {
  22149. if (required)
  22150. {
  22151. if (err)
  22152. {
  22153. (*err) += "'" + property + "' property is missing";
  22154. if (!parent_node.empty())
  22155. {
  22156. (*err) += " in " + parent_node;
  22157. }
  22158. (*err) += ".\n";
  22159. }
  22160. }
  22161. return false;
  22162. }
  22163. auto &value = GetValue(it);
  22164. bool isBoolean;
  22165. bool boolValue = false;
  22166. #ifdef TINYGLTF_USE_RAPIDJSON
  22167. isBoolean = value.IsBool();
  22168. if (isBoolean)
  22169. {
  22170. boolValue = value.GetBool();
  22171. }
  22172. #else
  22173. isBoolean = value.is_boolean();
  22174. if (isBoolean)
  22175. {
  22176. boolValue = value.get<bool>();
  22177. }
  22178. #endif
  22179. if (!isBoolean)
  22180. {
  22181. if (required)
  22182. {
  22183. if (err)
  22184. {
  22185. (*err) += "'" + property + "' property is not a bool type.\n";
  22186. }
  22187. }
  22188. return false;
  22189. }
  22190. if (ret)
  22191. {
  22192. (*ret) = boolValue;
  22193. }
  22194. return true;
  22195. }
  22196. static bool ParseIntegerProperty(int *ret, std::string *err, const json &o,
  22197. const std::string &property,
  22198. const bool required,
  22199. const std::string &parent_node = "")
  22200. {
  22201. json_const_iterator it;
  22202. if (!FindMember(o, property.c_str(), it))
  22203. {
  22204. if (required)
  22205. {
  22206. if (err)
  22207. {
  22208. (*err) += "'" + property + "' property is missing";
  22209. if (!parent_node.empty())
  22210. {
  22211. (*err) += " in " + parent_node;
  22212. }
  22213. (*err) += ".\n";
  22214. }
  22215. }
  22216. return false;
  22217. }
  22218. int intValue;
  22219. bool isInt = GetInt(GetValue(it), intValue);
  22220. if (!isInt)
  22221. {
  22222. if (required)
  22223. {
  22224. if (err)
  22225. {
  22226. (*err) += "'" + property + "' property is not an integer type.\n";
  22227. }
  22228. }
  22229. return false;
  22230. }
  22231. if (ret)
  22232. {
  22233. (*ret) = intValue;
  22234. }
  22235. return true;
  22236. }
  22237. static bool ParseUnsignedProperty(size_t *ret, std::string *err, const json &o,
  22238. const std::string &property,
  22239. const bool required,
  22240. const std::string &parent_node = "")
  22241. {
  22242. json_const_iterator it;
  22243. if (!FindMember(o, property.c_str(), it))
  22244. {
  22245. if (required)
  22246. {
  22247. if (err)
  22248. {
  22249. (*err) += "'" + property + "' property is missing";
  22250. if (!parent_node.empty())
  22251. {
  22252. (*err) += " in " + parent_node;
  22253. }
  22254. (*err) += ".\n";
  22255. }
  22256. }
  22257. return false;
  22258. }
  22259. auto &value = GetValue(it);
  22260. size_t uValue = 0;
  22261. bool isUValue;
  22262. #ifdef TINYGLTF_USE_RAPIDJSON
  22263. isUValue = false;
  22264. if (value.IsUint())
  22265. {
  22266. uValue = value.GetUint();
  22267. isUValue = true;
  22268. }
  22269. else if (value.IsUint64())
  22270. {
  22271. uValue = value.GetUint64();
  22272. isUValue = true;
  22273. }
  22274. #else
  22275. isUValue = value.is_number_unsigned();
  22276. if (isUValue)
  22277. {
  22278. uValue = value.get<size_t>();
  22279. }
  22280. #endif
  22281. if (!isUValue)
  22282. {
  22283. if (required)
  22284. {
  22285. if (err)
  22286. {
  22287. (*err) += "'" + property + "' property is not a positive integer.\n";
  22288. }
  22289. }
  22290. return false;
  22291. }
  22292. if (ret)
  22293. {
  22294. (*ret) = uValue;
  22295. }
  22296. return true;
  22297. }
  22298. static bool ParseNumberProperty(double *ret, std::string *err, const json &o,
  22299. const std::string &property,
  22300. const bool required,
  22301. const std::string &parent_node = "")
  22302. {
  22303. json_const_iterator it;
  22304. if (!FindMember(o, property.c_str(), it))
  22305. {
  22306. if (required)
  22307. {
  22308. if (err)
  22309. {
  22310. (*err) += "'" + property + "' property is missing";
  22311. if (!parent_node.empty())
  22312. {
  22313. (*err) += " in " + parent_node;
  22314. }
  22315. (*err) += ".\n";
  22316. }
  22317. }
  22318. return false;
  22319. }
  22320. double numberValue;
  22321. bool isNumber = GetNumber(GetValue(it), numberValue);
  22322. if (!isNumber)
  22323. {
  22324. if (required)
  22325. {
  22326. if (err)
  22327. {
  22328. (*err) += "'" + property + "' property is not a number type.\n";
  22329. }
  22330. }
  22331. return false;
  22332. }
  22333. if (ret)
  22334. {
  22335. (*ret) = numberValue;
  22336. }
  22337. return true;
  22338. }
  22339. static bool ParseNumberArrayProperty(std::vector<double> *ret, std::string *err,
  22340. const json &o, const std::string &property,
  22341. bool required,
  22342. const std::string &parent_node = "")
  22343. {
  22344. json_const_iterator it;
  22345. if (!FindMember(o, property.c_str(), it))
  22346. {
  22347. if (required)
  22348. {
  22349. if (err)
  22350. {
  22351. (*err) += "'" + property + "' property is missing";
  22352. if (!parent_node.empty())
  22353. {
  22354. (*err) += " in " + parent_node;
  22355. }
  22356. (*err) += ".\n";
  22357. }
  22358. }
  22359. return false;
  22360. }
  22361. if (!IsArray(GetValue(it)))
  22362. {
  22363. if (required)
  22364. {
  22365. if (err)
  22366. {
  22367. (*err) += "'" + property + "' property is not an array";
  22368. if (!parent_node.empty())
  22369. {
  22370. (*err) += " in " + parent_node;
  22371. }
  22372. (*err) += ".\n";
  22373. }
  22374. }
  22375. return false;
  22376. }
  22377. ret->clear();
  22378. auto end = ArrayEnd(GetValue(it));
  22379. for (auto i = ArrayBegin(GetValue(it)); i != end; ++i)
  22380. {
  22381. double numberValue;
  22382. const bool isNumber = GetNumber(*i, numberValue);
  22383. if (!isNumber)
  22384. {
  22385. if (required)
  22386. {
  22387. if (err)
  22388. {
  22389. (*err) += "'" + property + "' property is not a number.\n";
  22390. if (!parent_node.empty())
  22391. {
  22392. (*err) += " in " + parent_node;
  22393. }
  22394. (*err) += ".\n";
  22395. }
  22396. }
  22397. return false;
  22398. }
  22399. ret->push_back(numberValue);
  22400. }
  22401. return true;
  22402. }
  22403. static bool ParseIntegerArrayProperty(std::vector<int> *ret, std::string *err,
  22404. const json &o,
  22405. const std::string &property,
  22406. bool required,
  22407. const std::string &parent_node = "")
  22408. {
  22409. json_const_iterator it;
  22410. if (!FindMember(o, property.c_str(), it))
  22411. {
  22412. if (required)
  22413. {
  22414. if (err)
  22415. {
  22416. (*err) += "'" + property + "' property is missing";
  22417. if (!parent_node.empty())
  22418. {
  22419. (*err) += " in " + parent_node;
  22420. }
  22421. (*err) += ".\n";
  22422. }
  22423. }
  22424. return false;
  22425. }
  22426. if (!IsArray(GetValue(it)))
  22427. {
  22428. if (required)
  22429. {
  22430. if (err)
  22431. {
  22432. (*err) += "'" + property + "' property is not an array";
  22433. if (!parent_node.empty())
  22434. {
  22435. (*err) += " in " + parent_node;
  22436. }
  22437. (*err) += ".\n";
  22438. }
  22439. }
  22440. return false;
  22441. }
  22442. ret->clear();
  22443. auto end = ArrayEnd(GetValue(it));
  22444. for (auto i = ArrayBegin(GetValue(it)); i != end; ++i)
  22445. {
  22446. int numberValue;
  22447. bool isNumber = GetInt(*i, numberValue);
  22448. if (!isNumber)
  22449. {
  22450. if (required)
  22451. {
  22452. if (err)
  22453. {
  22454. (*err) += "'" + property + "' property is not an integer type.\n";
  22455. if (!parent_node.empty())
  22456. {
  22457. (*err) += " in " + parent_node;
  22458. }
  22459. (*err) += ".\n";
  22460. }
  22461. }
  22462. return false;
  22463. }
  22464. ret->push_back(numberValue);
  22465. }
  22466. return true;
  22467. }
  22468. static bool ParseStringProperty(
  22469. std::string *ret, std::string *err, const json &o,
  22470. const std::string &property, bool required,
  22471. const std::string &parent_node = std::string())
  22472. {
  22473. json_const_iterator it;
  22474. if (!FindMember(o, property.c_str(), it))
  22475. {
  22476. if (required)
  22477. {
  22478. if (err)
  22479. {
  22480. (*err) += "'" + property + "' property is missing";
  22481. if (parent_node.empty())
  22482. {
  22483. (*err) += ".\n";
  22484. }
  22485. else
  22486. {
  22487. (*err) += " in `" + parent_node + "'.\n";
  22488. }
  22489. }
  22490. }
  22491. return false;
  22492. }
  22493. std::string strValue;
  22494. if (!GetString(GetValue(it), strValue))
  22495. {
  22496. if (required)
  22497. {
  22498. if (err)
  22499. {
  22500. (*err) += "'" + property + "' property is not a string type.\n";
  22501. }
  22502. }
  22503. return false;
  22504. }
  22505. if (ret)
  22506. {
  22507. (*ret) = std::move(strValue);
  22508. }
  22509. return true;
  22510. }
  22511. static bool ParseStringIntegerProperty(std::map<std::string, int> *ret,
  22512. std::string *err, const json &o,
  22513. const std::string &property,
  22514. bool required,
  22515. const std::string &parent = "")
  22516. {
  22517. json_const_iterator it;
  22518. if (!FindMember(o, property.c_str(), it))
  22519. {
  22520. if (required)
  22521. {
  22522. if (err)
  22523. {
  22524. if (!parent.empty())
  22525. {
  22526. (*err) +=
  22527. "'" + property + "' property is missing in " + parent + ".\n";
  22528. }
  22529. else
  22530. {
  22531. (*err) += "'" + property + "' property is missing.\n";
  22532. }
  22533. }
  22534. }
  22535. return false;
  22536. }
  22537. const json &dict = GetValue(it);
  22538. // Make sure we are dealing with an object / dictionary.
  22539. if (!IsObject(dict))
  22540. {
  22541. if (required)
  22542. {
  22543. if (err)
  22544. {
  22545. (*err) += "'" + property + "' property is not an object.\n";
  22546. }
  22547. }
  22548. return false;
  22549. }
  22550. ret->clear();
  22551. json_const_iterator dictIt(ObjectBegin(dict));
  22552. json_const_iterator dictItEnd(ObjectEnd(dict));
  22553. for (; dictIt != dictItEnd; ++dictIt)
  22554. {
  22555. int intVal;
  22556. if (!GetInt(GetValue(dictIt), intVal))
  22557. {
  22558. if (required)
  22559. {
  22560. if (err)
  22561. {
  22562. (*err) += "'" + property + "' value is not an integer type.\n";
  22563. }
  22564. }
  22565. return false;
  22566. }
  22567. // Insert into the list.
  22568. (*ret)[GetKey(dictIt)] = intVal;
  22569. }
  22570. return true;
  22571. }
  22572. static bool ParseJSONProperty(std::map<std::string, double> *ret,
  22573. std::string *err, const json &o,
  22574. const std::string &property, bool required)
  22575. {
  22576. json_const_iterator it;
  22577. if (!FindMember(o, property.c_str(), it))
  22578. {
  22579. if (required)
  22580. {
  22581. if (err)
  22582. {
  22583. (*err) += "'" + property + "' property is missing. \n'";
  22584. }
  22585. }
  22586. return false;
  22587. }
  22588. const json &obj = GetValue(it);
  22589. if (!IsObject(obj))
  22590. {
  22591. if (required)
  22592. {
  22593. if (err)
  22594. {
  22595. (*err) += "'" + property + "' property is not a JSON object.\n";
  22596. }
  22597. }
  22598. return false;
  22599. }
  22600. ret->clear();
  22601. json_const_iterator it2(ObjectBegin(obj));
  22602. json_const_iterator itEnd(ObjectEnd(obj));
  22603. for (; it2 != itEnd; ++it2)
  22604. {
  22605. double numVal;
  22606. if (GetNumber(GetValue(it2), numVal))
  22607. ret->emplace(std::string(GetKey(it2)), numVal);
  22608. }
  22609. return true;
  22610. }
  22611. static bool ParseParameterProperty(Parameter *param, std::string *err,
  22612. const json &o, const std::string &prop,
  22613. bool required)
  22614. {
  22615. // A parameter value can either be a string or an array of either a boolean or
  22616. // a number. Booleans of any kind aren't supported here. Granted, it
  22617. // complicates the Parameter structure and breaks it semantically in the sense
  22618. // that the client probably works off the assumption that if the string is
  22619. // empty the vector is used, etc. Would a tagged union work?
  22620. if (ParseStringProperty(&param->string_value, err, o, prop, false))
  22621. {
  22622. // Found string property.
  22623. return true;
  22624. }
  22625. else if (ParseNumberArrayProperty(&param->number_array, err, o, prop,
  22626. false))
  22627. {
  22628. // Found a number array.
  22629. return true;
  22630. }
  22631. else if (ParseNumberProperty(&param->number_value, err, o, prop, false))
  22632. {
  22633. return param->has_number_value = true;
  22634. }
  22635. else if (ParseJSONProperty(&param->json_double_value, err, o, prop,
  22636. false))
  22637. {
  22638. return true;
  22639. }
  22640. else if (ParseBooleanProperty(&param->bool_value, err, o, prop, false))
  22641. {
  22642. return true;
  22643. }
  22644. else
  22645. {
  22646. if (required)
  22647. {
  22648. if (err)
  22649. {
  22650. (*err) += "parameter must be a string or number / number array.\n";
  22651. }
  22652. }
  22653. return false;
  22654. }
  22655. }
  22656. static bool ParseExtensionsProperty(ExtensionMap *ret, std::string *err,
  22657. const json &o)
  22658. {
  22659. (void)err;
  22660. json_const_iterator it;
  22661. if (!FindMember(o, "extensions", it))
  22662. {
  22663. return false;
  22664. }
  22665. auto &obj = GetValue(it);
  22666. if (!IsObject(obj))
  22667. {
  22668. return false;
  22669. }
  22670. ExtensionMap extensions;
  22671. json_const_iterator extIt = ObjectBegin(obj); // it.value().begin();
  22672. json_const_iterator extEnd = ObjectEnd(obj);
  22673. for (; extIt != extEnd; ++extIt)
  22674. {
  22675. auto &itObj = GetValue(extIt);
  22676. if (!IsObject(itObj)) continue;
  22677. std::string key(GetKey(extIt));
  22678. if (!ParseJsonAsValue(&extensions[key], itObj))
  22679. {
  22680. if (!key.empty())
  22681. {
  22682. // create empty object so that an extension object is still of type
  22683. // object
  22684. extensions[key] = Value{ Value::Object{} };
  22685. }
  22686. }
  22687. }
  22688. if (ret)
  22689. {
  22690. (*ret) = std::move(extensions);
  22691. }
  22692. return true;
  22693. }
  22694. static bool ParseAsset(Asset *asset, std::string *err, const json &o,
  22695. bool store_original_json_for_extras_and_extensions)
  22696. {
  22697. ParseStringProperty(&asset->version, err, o, "version", true, "Asset");
  22698. ParseStringProperty(&asset->generator, err, o, "generator", false, "Asset");
  22699. ParseStringProperty(&asset->minVersion, err, o, "minVersion", false, "Asset");
  22700. ParseStringProperty(&asset->copyright, err, o, "copyright", false, "Asset");
  22701. ParseExtensionsProperty(&asset->extensions, err, o);
  22702. // Unity exporter version is added as extra here
  22703. ParseExtrasProperty(&(asset->extras), o);
  22704. if (store_original_json_for_extras_and_extensions)
  22705. {
  22706. {
  22707. json_const_iterator it;
  22708. if (FindMember(o, "extensions", it))
  22709. {
  22710. asset->extensions_json_string = JsonToString(GetValue(it));
  22711. }
  22712. }
  22713. {
  22714. json_const_iterator it;
  22715. if (FindMember(o, "extras", it))
  22716. {
  22717. asset->extras_json_string = JsonToString(GetValue(it));
  22718. }
  22719. }
  22720. }
  22721. return true;
  22722. }
  22723. static bool ParseImage(Image *image, const int image_idx, std::string *err,
  22724. std::string *warn, const json &o,
  22725. bool store_original_json_for_extras_and_extensions,
  22726. const std::string &basedir, FsCallbacks *fs,
  22727. LoadImageDataFunction *LoadImageData = nullptr,
  22728. void *load_image_user_data = nullptr)
  22729. {
  22730. // A glTF image must either reference a bufferView or an image uri
  22731. // schema says oneOf [`bufferView`, `uri`]
  22732. // TODO(syoyo): Check the type of each parameters.
  22733. json_const_iterator it;
  22734. bool hasBufferView = FindMember(o, "bufferView", it);
  22735. bool hasURI = FindMember(o, "uri", it);
  22736. ParseStringProperty(&image->name, err, o, "name", false);
  22737. if (hasBufferView && hasURI)
  22738. {
  22739. // Should not both defined.
  22740. if (err)
  22741. {
  22742. (*err) +=
  22743. "Only one of `bufferView` or `uri` should be defined, but both are "
  22744. "defined for image[" +
  22745. std::to_string(image_idx) + "] name = \"" + image->name + "\"\n";
  22746. }
  22747. return false;
  22748. }
  22749. if (!hasBufferView && !hasURI)
  22750. {
  22751. if (err)
  22752. {
  22753. (*err) += "Neither required `bufferView` nor `uri` defined for image[" +
  22754. std::to_string(image_idx) + "] name = \"" + image->name +
  22755. "\"\n";
  22756. }
  22757. return false;
  22758. }
  22759. ParseExtensionsProperty(&image->extensions, err, o);
  22760. ParseExtrasProperty(&image->extras, o);
  22761. if (store_original_json_for_extras_and_extensions)
  22762. {
  22763. {
  22764. json_const_iterator eit;
  22765. if (FindMember(o, "extensions", eit))
  22766. {
  22767. image->extensions_json_string = JsonToString(GetValue(eit));
  22768. }
  22769. }
  22770. {
  22771. json_const_iterator eit;
  22772. if (FindMember(o, "extras", eit))
  22773. {
  22774. image->extras_json_string = JsonToString(GetValue(eit));
  22775. }
  22776. }
  22777. }
  22778. if (hasBufferView)
  22779. {
  22780. int bufferView = -1;
  22781. if (!ParseIntegerProperty(&bufferView, err, o, "bufferView", true))
  22782. {
  22783. if (err)
  22784. {
  22785. (*err) += "Failed to parse `bufferView` for image[" +
  22786. std::to_string(image_idx) + "] name = \"" + image->name +
  22787. "\"\n";
  22788. }
  22789. return false;
  22790. }
  22791. std::string mime_type;
  22792. ParseStringProperty(&mime_type, err, o, "mimeType", false);
  22793. int width = 0;
  22794. ParseIntegerProperty(&width, err, o, "width", false);
  22795. int height = 0;
  22796. ParseIntegerProperty(&height, err, o, "height", false);
  22797. // Just only save some information here. Loading actual image data from
  22798. // bufferView is done after this `ParseImage` function.
  22799. image->bufferView = bufferView;
  22800. image->mimeType = mime_type;
  22801. image->width = width;
  22802. image->height = height;
  22803. return true;
  22804. }
  22805. // Parse URI & Load image data.
  22806. std::string uri;
  22807. std::string tmp_err;
  22808. if (!ParseStringProperty(&uri, &tmp_err, o, "uri", true))
  22809. {
  22810. if (err)
  22811. {
  22812. (*err) += "Failed to parse `uri` for image[" + std::to_string(image_idx) +
  22813. "] name = \"" + image->name + "\".\n";
  22814. }
  22815. return false;
  22816. }
  22817. std::vector<unsigned char> img;
  22818. if (IsDataURI(uri))
  22819. {
  22820. if (!DecodeDataURI(&img, image->mimeType, uri, 0, false))
  22821. {
  22822. if (err)
  22823. {
  22824. (*err) += "Failed to decode 'uri' for image[" +
  22825. std::to_string(image_idx) + "] name = [" + image->name +
  22826. "]\n";
  22827. }
  22828. return false;
  22829. }
  22830. }
  22831. else
  22832. {
  22833. // Assume external file
  22834. // Keep texture path (for textures that cannot be decoded)
  22835. image->uri = uri;
  22836. #ifdef TINYGLTF_NO_EXTERNAL_IMAGE
  22837. return true;
  22838. #endif
  22839. std::string decoded_uri = dlib::urldecode(uri);
  22840. if (!LoadExternalFile(&img, err, warn, decoded_uri, basedir,
  22841. /* required */ false, /* required bytes */ 0,
  22842. /* checksize */ false, fs))
  22843. {
  22844. if (warn)
  22845. {
  22846. (*warn) += "Failed to load external 'uri' for image[" +
  22847. std::to_string(image_idx) + "] name = [" + image->name +
  22848. "]\n";
  22849. }
  22850. // If the image cannot be loaded, keep uri as image->uri.
  22851. return true;
  22852. }
  22853. if (img.empty())
  22854. {
  22855. if (warn)
  22856. {
  22857. (*warn) += "Image data is empty for image[" +
  22858. std::to_string(image_idx) + "] name = [" + image->name +
  22859. "] \n";
  22860. }
  22861. return false;
  22862. }
  22863. }
  22864. if (*LoadImageData == nullptr)
  22865. {
  22866. if (err)
  22867. {
  22868. (*err) += "No LoadImageData callback specified.\n";
  22869. }
  22870. return false;
  22871. }
  22872. return (*LoadImageData)(image, image_idx, err, warn, 0, 0, &img.at(0),
  22873. static_cast<int>(img.size()), load_image_user_data);
  22874. }
  22875. static bool ParseTexture(Texture *texture, std::string *err, const json &o,
  22876. bool store_original_json_for_extras_and_extensions,
  22877. const std::string &basedir)
  22878. {
  22879. (void)basedir;
  22880. int sampler = -1;
  22881. int source = -1;
  22882. ParseIntegerProperty(&sampler, err, o, "sampler", false);
  22883. ParseIntegerProperty(&source, err, o, "source", false);
  22884. texture->sampler = sampler;
  22885. texture->source = source;
  22886. ParseExtensionsProperty(&texture->extensions, err, o);
  22887. ParseExtrasProperty(&texture->extras, o);
  22888. if (store_original_json_for_extras_and_extensions)
  22889. {
  22890. {
  22891. json_const_iterator it;
  22892. if (FindMember(o, "extensions", it))
  22893. {
  22894. texture->extensions_json_string = JsonToString(GetValue(it));
  22895. }
  22896. }
  22897. {
  22898. json_const_iterator it;
  22899. if (FindMember(o, "extras", it))
  22900. {
  22901. texture->extras_json_string = JsonToString(GetValue(it));
  22902. }
  22903. }
  22904. }
  22905. ParseStringProperty(&texture->name, err, o, "name", false);
  22906. return true;
  22907. }
  22908. static bool ParseTextureInfo(
  22909. TextureInfo *texinfo, std::string *err, const json &o,
  22910. bool store_original_json_for_extras_and_extensions)
  22911. {
  22912. if (texinfo == nullptr)
  22913. {
  22914. return false;
  22915. }
  22916. if (!ParseIntegerProperty(&texinfo->index, err, o, "index",
  22917. /* required */ true, "TextureInfo"))
  22918. {
  22919. return false;
  22920. }
  22921. ParseIntegerProperty(&texinfo->texCoord, err, o, "texCoord", false);
  22922. ParseExtensionsProperty(&texinfo->extensions, err, o);
  22923. ParseExtrasProperty(&texinfo->extras, o);
  22924. if (store_original_json_for_extras_and_extensions)
  22925. {
  22926. {
  22927. json_const_iterator it;
  22928. if (FindMember(o, "extensions", it))
  22929. {
  22930. texinfo->extensions_json_string = JsonToString(GetValue(it));
  22931. }
  22932. }
  22933. {
  22934. json_const_iterator it;
  22935. if (FindMember(o, "extras", it))
  22936. {
  22937. texinfo->extras_json_string = JsonToString(GetValue(it));
  22938. }
  22939. }
  22940. }
  22941. return true;
  22942. }
  22943. static bool ParseNormalTextureInfo(
  22944. NormalTextureInfo *texinfo, std::string *err, const json &o,
  22945. bool store_original_json_for_extras_and_extensions)
  22946. {
  22947. if (texinfo == nullptr)
  22948. {
  22949. return false;
  22950. }
  22951. if (!ParseIntegerProperty(&texinfo->index, err, o, "index",
  22952. /* required */ true, "NormalTextureInfo"))
  22953. {
  22954. return false;
  22955. }
  22956. ParseIntegerProperty(&texinfo->texCoord, err, o, "texCoord", false);
  22957. ParseNumberProperty(&texinfo->scale, err, o, "scale", false);
  22958. ParseExtensionsProperty(&texinfo->extensions, err, o);
  22959. ParseExtrasProperty(&texinfo->extras, o);
  22960. if (store_original_json_for_extras_and_extensions)
  22961. {
  22962. {
  22963. json_const_iterator it;
  22964. if (FindMember(o, "extensions", it))
  22965. {
  22966. texinfo->extensions_json_string = JsonToString(GetValue(it));
  22967. }
  22968. }
  22969. {
  22970. json_const_iterator it;
  22971. if (FindMember(o, "extras", it))
  22972. {
  22973. texinfo->extras_json_string = JsonToString(GetValue(it));
  22974. }
  22975. }
  22976. }
  22977. return true;
  22978. }
  22979. static bool ParseOcclusionTextureInfo(
  22980. OcclusionTextureInfo *texinfo, std::string *err, const json &o,
  22981. bool store_original_json_for_extras_and_extensions)
  22982. {
  22983. if (texinfo == nullptr)
  22984. {
  22985. return false;
  22986. }
  22987. if (!ParseIntegerProperty(&texinfo->index, err, o, "index",
  22988. /* required */ true, "NormalTextureInfo"))
  22989. {
  22990. return false;
  22991. }
  22992. ParseIntegerProperty(&texinfo->texCoord, err, o, "texCoord", false);
  22993. ParseNumberProperty(&texinfo->strength, err, o, "strength", false);
  22994. ParseExtensionsProperty(&texinfo->extensions, err, o);
  22995. ParseExtrasProperty(&texinfo->extras, o);
  22996. if (store_original_json_for_extras_and_extensions)
  22997. {
  22998. {
  22999. json_const_iterator it;
  23000. if (FindMember(o, "extensions", it))
  23001. {
  23002. texinfo->extensions_json_string = JsonToString(GetValue(it));
  23003. }
  23004. }
  23005. {
  23006. json_const_iterator it;
  23007. if (FindMember(o, "extras", it))
  23008. {
  23009. texinfo->extras_json_string = JsonToString(GetValue(it));
  23010. }
  23011. }
  23012. }
  23013. return true;
  23014. }
  23015. static bool ParseBuffer(Buffer *buffer, std::string *err, const json &o,
  23016. bool store_original_json_for_extras_and_extensions,
  23017. FsCallbacks *fs, const std::string &basedir,
  23018. bool is_binary = false,
  23019. const unsigned char *bin_data = nullptr,
  23020. size_t bin_size = 0)
  23021. {
  23022. size_t byteLength;
  23023. if (!ParseUnsignedProperty(&byteLength, err, o, "byteLength", true,
  23024. "Buffer"))
  23025. {
  23026. return false;
  23027. }
  23028. // In glTF 2.0, uri is not mandatory anymore
  23029. buffer->uri.clear();
  23030. ParseStringProperty(&buffer->uri, err, o, "uri", false, "Buffer");
  23031. // having an empty uri for a non embedded image should not be valid
  23032. if (!is_binary && buffer->uri.empty())
  23033. {
  23034. if (err)
  23035. {
  23036. (*err) += "'uri' is missing from non binary glTF file buffer.\n";
  23037. }
  23038. }
  23039. json_const_iterator type;
  23040. if (FindMember(o, "type", type))
  23041. {
  23042. std::string typeStr;
  23043. if (GetString(GetValue(type), typeStr))
  23044. {
  23045. if (typeStr.compare("arraybuffer") == 0)
  23046. {
  23047. // buffer.type = "arraybuffer";
  23048. }
  23049. }
  23050. }
  23051. if (is_binary)
  23052. {
  23053. // Still binary glTF accepts external dataURI.
  23054. if (!buffer->uri.empty())
  23055. {
  23056. // First try embedded data URI.
  23057. if (IsDataURI(buffer->uri))
  23058. {
  23059. std::string mime_type;
  23060. if (!DecodeDataURI(&buffer->data, mime_type, buffer->uri, byteLength,
  23061. true))
  23062. {
  23063. if (err)
  23064. {
  23065. (*err) +=
  23066. "Failed to decode 'uri' : " + buffer->uri + " in Buffer\n";
  23067. }
  23068. return false;
  23069. }
  23070. }
  23071. else
  23072. {
  23073. // External .bin file.
  23074. std::string decoded_uri = dlib::urldecode(buffer->uri);
  23075. if (!LoadExternalFile(&buffer->data, err, /* warn */ nullptr,
  23076. decoded_uri, basedir, /* required */ true,
  23077. byteLength, /* checkSize */ true, fs))
  23078. {
  23079. return false;
  23080. }
  23081. }
  23082. }
  23083. else
  23084. {
  23085. // load data from (embedded) binary data
  23086. if ((bin_size == 0) || (bin_data == nullptr))
  23087. {
  23088. if (err)
  23089. {
  23090. (*err) += "Invalid binary data in `Buffer'.\n";
  23091. }
  23092. return false;
  23093. }
  23094. if (byteLength > bin_size)
  23095. {
  23096. if (err)
  23097. {
  23098. std::stringstream ss;
  23099. ss << "Invalid `byteLength'. Must be equal or less than binary size: "
  23100. "`byteLength' = "
  23101. << byteLength << ", binary size = " << bin_size << std::endl;
  23102. (*err) += ss.str();
  23103. }
  23104. return false;
  23105. }
  23106. // Read buffer data
  23107. buffer->data.resize(static_cast<size_t>(byteLength));
  23108. memcpy(&(buffer->data.at(0)), bin_data, static_cast<size_t>(byteLength));
  23109. }
  23110. }
  23111. else
  23112. {
  23113. if (IsDataURI(buffer->uri))
  23114. {
  23115. std::string mime_type;
  23116. if (!DecodeDataURI(&buffer->data, mime_type, buffer->uri, byteLength,
  23117. true))
  23118. {
  23119. if (err)
  23120. {
  23121. (*err) += "Failed to decode 'uri' : " + buffer->uri + " in Buffer\n";
  23122. }
  23123. return false;
  23124. }
  23125. }
  23126. else
  23127. {
  23128. // Assume external .bin file.
  23129. std::string decoded_uri = dlib::urldecode(buffer->uri);
  23130. if (!LoadExternalFile(&buffer->data, err, /* warn */ nullptr, decoded_uri,
  23131. basedir, /* required */ true, byteLength,
  23132. /* checkSize */ true, fs))
  23133. {
  23134. return false;
  23135. }
  23136. }
  23137. }
  23138. ParseStringProperty(&buffer->name, err, o, "name", false);
  23139. ParseExtensionsProperty(&buffer->extensions, err, o);
  23140. ParseExtrasProperty(&buffer->extras, o);
  23141. if (store_original_json_for_extras_and_extensions)
  23142. {
  23143. {
  23144. json_const_iterator it;
  23145. if (FindMember(o, "extensions", it))
  23146. {
  23147. buffer->extensions_json_string = JsonToString(GetValue(it));
  23148. }
  23149. }
  23150. {
  23151. json_const_iterator it;
  23152. if (FindMember(o, "extras", it))
  23153. {
  23154. buffer->extras_json_string = JsonToString(GetValue(it));
  23155. }
  23156. }
  23157. }
  23158. return true;
  23159. }
  23160. static bool ParseBufferView(
  23161. BufferView *bufferView, std::string *err, const json &o,
  23162. bool store_original_json_for_extras_and_extensions)
  23163. {
  23164. int buffer = -1;
  23165. if (!ParseIntegerProperty(&buffer, err, o, "buffer", true, "BufferView"))
  23166. {
  23167. return false;
  23168. }
  23169. size_t byteOffset = 0;
  23170. ParseUnsignedProperty(&byteOffset, err, o, "byteOffset", false);
  23171. size_t byteLength = 1;
  23172. if (!ParseUnsignedProperty(&byteLength, err, o, "byteLength", true,
  23173. "BufferView"))
  23174. {
  23175. return false;
  23176. }
  23177. size_t byteStride = 0;
  23178. if (!ParseUnsignedProperty(&byteStride, err, o, "byteStride", false))
  23179. {
  23180. // Spec says: When byteStride of referenced bufferView is not defined, it
  23181. // means that accessor elements are tightly packed, i.e., effective stride
  23182. // equals the size of the element.
  23183. // We cannot determine the actual byteStride until Accessor are parsed, thus
  23184. // set 0(= tightly packed) here(as done in OpenGL's VertexAttribPoiner)
  23185. byteStride = 0;
  23186. }
  23187. if ((byteStride > 252) || ((byteStride % 4) != 0))
  23188. {
  23189. if (err)
  23190. {
  23191. std::stringstream ss;
  23192. ss << "Invalid `byteStride' value. `byteStride' must be the multiple of "
  23193. "4 : "
  23194. << byteStride << std::endl;
  23195. (*err) += ss.str();
  23196. }
  23197. return false;
  23198. }
  23199. int target = 0;
  23200. ParseIntegerProperty(&target, err, o, "target", false);
  23201. if ((target == TINYGLTF_TARGET_ARRAY_BUFFER) ||
  23202. (target == TINYGLTF_TARGET_ELEMENT_ARRAY_BUFFER))
  23203. {
  23204. // OK
  23205. }
  23206. else
  23207. {
  23208. target = 0;
  23209. }
  23210. bufferView->target = target;
  23211. ParseStringProperty(&bufferView->name, err, o, "name", false);
  23212. ParseExtensionsProperty(&bufferView->extensions, err, o);
  23213. ParseExtrasProperty(&bufferView->extras, o);
  23214. if (store_original_json_for_extras_and_extensions)
  23215. {
  23216. {
  23217. json_const_iterator it;
  23218. if (FindMember(o, "extensions", it))
  23219. {
  23220. bufferView->extensions_json_string = JsonToString(GetValue(it));
  23221. }
  23222. }
  23223. {
  23224. json_const_iterator it;
  23225. if (FindMember(o, "extras", it))
  23226. {
  23227. bufferView->extras_json_string = JsonToString(GetValue(it));
  23228. }
  23229. }
  23230. }
  23231. bufferView->buffer = buffer;
  23232. bufferView->byteOffset = byteOffset;
  23233. bufferView->byteLength = byteLength;
  23234. bufferView->byteStride = byteStride;
  23235. return true;
  23236. }
  23237. static bool ParseSparseAccessor(Accessor *accessor, std::string *err,
  23238. const json &o)
  23239. {
  23240. accessor->sparse.isSparse = true;
  23241. int count = 0;
  23242. ParseIntegerProperty(&count, err, o, "count", true);
  23243. json_const_iterator indices_iterator;
  23244. json_const_iterator values_iterator;
  23245. if (!FindMember(o, "indices", indices_iterator))
  23246. {
  23247. (*err) = "the sparse object of this accessor doesn't have indices";
  23248. return false;
  23249. }
  23250. if (!FindMember(o, "values", values_iterator))
  23251. {
  23252. (*err) = "the sparse object ob ths accessor doesn't have values";
  23253. return false;
  23254. }
  23255. const json &indices_obj = GetValue(indices_iterator);
  23256. const json &values_obj = GetValue(values_iterator);
  23257. int indices_buffer_view = 0, indices_byte_offset = 0, component_type = 0;
  23258. ParseIntegerProperty(&indices_buffer_view, err, indices_obj, "bufferView",
  23259. true);
  23260. ParseIntegerProperty(&indices_byte_offset, err, indices_obj, "byteOffset",
  23261. true);
  23262. ParseIntegerProperty(&component_type, err, indices_obj, "componentType",
  23263. true);
  23264. int values_buffer_view = 0, values_byte_offset = 0;
  23265. ParseIntegerProperty(&values_buffer_view, err, values_obj, "bufferView",
  23266. true);
  23267. ParseIntegerProperty(&values_byte_offset, err, values_obj, "byteOffset",
  23268. true);
  23269. accessor->sparse.count = count;
  23270. accessor->sparse.indices.bufferView = indices_buffer_view;
  23271. accessor->sparse.indices.byteOffset = indices_byte_offset;
  23272. accessor->sparse.indices.componentType = component_type;
  23273. accessor->sparse.values.bufferView = values_buffer_view;
  23274. accessor->sparse.values.byteOffset = values_byte_offset;
  23275. // todo check theses values
  23276. return true;
  23277. }
  23278. static bool ParseAccessor(Accessor *accessor, std::string *err, const json &o,
  23279. bool store_original_json_for_extras_and_extensions)
  23280. {
  23281. int bufferView = -1;
  23282. ParseIntegerProperty(&bufferView, err, o, "bufferView", false, "Accessor");
  23283. size_t byteOffset = 0;
  23284. ParseUnsignedProperty(&byteOffset, err, o, "byteOffset", false, "Accessor");
  23285. bool normalized = false;
  23286. ParseBooleanProperty(&normalized, err, o, "normalized", false, "Accessor");
  23287. size_t componentType = 0;
  23288. if (!ParseUnsignedProperty(&componentType, err, o, "componentType", true,
  23289. "Accessor"))
  23290. {
  23291. return false;
  23292. }
  23293. size_t count = 0;
  23294. if (!ParseUnsignedProperty(&count, err, o, "count", true, "Accessor"))
  23295. {
  23296. return false;
  23297. }
  23298. std::string type;
  23299. if (!ParseStringProperty(&type, err, o, "type", true, "Accessor"))
  23300. {
  23301. return false;
  23302. }
  23303. if (type.compare("SCALAR") == 0)
  23304. {
  23305. accessor->type = TINYGLTF_TYPE_SCALAR;
  23306. }
  23307. else if (type.compare("VEC2") == 0)
  23308. {
  23309. accessor->type = TINYGLTF_TYPE_VEC2;
  23310. }
  23311. else if (type.compare("VEC3") == 0)
  23312. {
  23313. accessor->type = TINYGLTF_TYPE_VEC3;
  23314. }
  23315. else if (type.compare("VEC4") == 0)
  23316. {
  23317. accessor->type = TINYGLTF_TYPE_VEC4;
  23318. }
  23319. else if (type.compare("MAT2") == 0)
  23320. {
  23321. accessor->type = TINYGLTF_TYPE_MAT2;
  23322. }
  23323. else if (type.compare("MAT3") == 0)
  23324. {
  23325. accessor->type = TINYGLTF_TYPE_MAT3;
  23326. }
  23327. else if (type.compare("MAT4") == 0)
  23328. {
  23329. accessor->type = TINYGLTF_TYPE_MAT4;
  23330. }
  23331. else
  23332. {
  23333. std::stringstream ss;
  23334. ss << "Unsupported `type` for accessor object. Got \"" << type << "\"\n";
  23335. if (err)
  23336. {
  23337. (*err) += ss.str();
  23338. }
  23339. return false;
  23340. }
  23341. ParseStringProperty(&accessor->name, err, o, "name", false);
  23342. accessor->minValues.clear();
  23343. accessor->maxValues.clear();
  23344. ParseNumberArrayProperty(&accessor->minValues, err, o, "min", false,
  23345. "Accessor");
  23346. ParseNumberArrayProperty(&accessor->maxValues, err, o, "max", false,
  23347. "Accessor");
  23348. accessor->count = count;
  23349. accessor->bufferView = bufferView;
  23350. accessor->byteOffset = byteOffset;
  23351. accessor->normalized = normalized;
  23352. {
  23353. if (componentType >= TINYGLTF_COMPONENT_TYPE_BYTE &&
  23354. componentType <= TINYGLTF_COMPONENT_TYPE_DOUBLE)
  23355. {
  23356. // OK
  23357. accessor->componentType = int(componentType);
  23358. }
  23359. else
  23360. {
  23361. std::stringstream ss;
  23362. ss << "Invalid `componentType` in accessor. Got " << componentType
  23363. << "\n";
  23364. if (err)
  23365. {
  23366. (*err) += ss.str();
  23367. }
  23368. return false;
  23369. }
  23370. }
  23371. ParseExtensionsProperty(&(accessor->extensions), err, o);
  23372. ParseExtrasProperty(&(accessor->extras), o);
  23373. if (store_original_json_for_extras_and_extensions)
  23374. {
  23375. {
  23376. json_const_iterator it;
  23377. if (FindMember(o, "extensions", it))
  23378. {
  23379. accessor->extensions_json_string = JsonToString(GetValue(it));
  23380. }
  23381. }
  23382. {
  23383. json_const_iterator it;
  23384. if (FindMember(o, "extras", it))
  23385. {
  23386. accessor->extras_json_string = JsonToString(GetValue(it));
  23387. }
  23388. }
  23389. }
  23390. // check if accessor has a "sparse" object:
  23391. json_const_iterator iterator;
  23392. if (FindMember(o, "sparse", iterator))
  23393. {
  23394. // here this accessor has a "sparse" subobject
  23395. return ParseSparseAccessor(accessor, err, GetValue(iterator));
  23396. }
  23397. return true;
  23398. }
  23399. #ifdef TINYGLTF_ENABLE_DRACO
  23400. static void DecodeIndexBuffer(draco::Mesh *mesh, size_t componentSize,
  23401. std::vector<uint8_t> &outBuffer)
  23402. {
  23403. if (componentSize == 4)
  23404. {
  23405. assert(sizeof(mesh->face(draco::FaceIndex(0))[0]) == componentSize);
  23406. memcpy(outBuffer.data(), &mesh->face(draco::FaceIndex(0))[0],
  23407. outBuffer.size());
  23408. }
  23409. else
  23410. {
  23411. size_t faceStride = componentSize * 3;
  23412. for (draco::FaceIndex f(0); f < mesh->num_faces(); ++f)
  23413. {
  23414. const draco::Mesh::Face &face = mesh->face(f);
  23415. if (componentSize == 2)
  23416. {
  23417. uint16_t indices[3] = { (uint16_t)face[0].value(),
  23418. (uint16_t)face[1].value(),
  23419. (uint16_t)face[2].value() };
  23420. memcpy(outBuffer.data() + f.value() * faceStride, &indices[0],
  23421. faceStride);
  23422. }
  23423. else
  23424. {
  23425. uint8_t indices[3] = { (uint8_t)face[0].value(),
  23426. (uint8_t)face[1].value(),
  23427. (uint8_t)face[2].value() };
  23428. memcpy(outBuffer.data() + f.value() * faceStride, &indices[0],
  23429. faceStride);
  23430. }
  23431. }
  23432. }
  23433. }
  23434. template <typename T>
  23435. static bool GetAttributeForAllPoints(draco::Mesh *mesh,
  23436. const draco::PointAttribute *pAttribute,
  23437. std::vector<uint8_t> &outBuffer)
  23438. {
  23439. size_t byteOffset = 0;
  23440. T values[4] = { 0, 0, 0, 0 };
  23441. for (draco::PointIndex i(0); i < mesh->num_points(); ++i)
  23442. {
  23443. const draco::AttributeValueIndex val_index = pAttribute->mapped_index(i);
  23444. if (!pAttribute->ConvertValue<T>(val_index, pAttribute->num_components(),
  23445. values))
  23446. return false;
  23447. memcpy(outBuffer.data() + byteOffset, &values[0],
  23448. sizeof(T) * pAttribute->num_components());
  23449. byteOffset += sizeof(T) * pAttribute->num_components();
  23450. }
  23451. return true;
  23452. }
  23453. static bool GetAttributeForAllPoints(uint32_t componentType, draco::Mesh *mesh,
  23454. const draco::PointAttribute *pAttribute,
  23455. std::vector<uint8_t> &outBuffer)
  23456. {
  23457. bool decodeResult = false;
  23458. switch (componentType)
  23459. {
  23460. case TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE:
  23461. decodeResult =
  23462. GetAttributeForAllPoints<uint8_t>(mesh, pAttribute, outBuffer);
  23463. break;
  23464. case TINYGLTF_COMPONENT_TYPE_BYTE:
  23465. decodeResult =
  23466. GetAttributeForAllPoints<int8_t>(mesh, pAttribute, outBuffer);
  23467. break;
  23468. case TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT:
  23469. decodeResult =
  23470. GetAttributeForAllPoints<uint16_t>(mesh, pAttribute, outBuffer);
  23471. break;
  23472. case TINYGLTF_COMPONENT_TYPE_SHORT:
  23473. decodeResult =
  23474. GetAttributeForAllPoints<int16_t>(mesh, pAttribute, outBuffer);
  23475. break;
  23476. case TINYGLTF_COMPONENT_TYPE_INT:
  23477. decodeResult =
  23478. GetAttributeForAllPoints<int32_t>(mesh, pAttribute, outBuffer);
  23479. break;
  23480. case TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT:
  23481. decodeResult =
  23482. GetAttributeForAllPoints<uint32_t>(mesh, pAttribute, outBuffer);
  23483. break;
  23484. case TINYGLTF_COMPONENT_TYPE_FLOAT:
  23485. decodeResult =
  23486. GetAttributeForAllPoints<float>(mesh, pAttribute, outBuffer);
  23487. break;
  23488. case TINYGLTF_COMPONENT_TYPE_DOUBLE:
  23489. decodeResult =
  23490. GetAttributeForAllPoints<double>(mesh, pAttribute, outBuffer);
  23491. break;
  23492. default:
  23493. return false;
  23494. }
  23495. return decodeResult;
  23496. }
  23497. static bool ParseDracoExtension(Primitive *primitive, Model *model,
  23498. std::string *err,
  23499. const Value &dracoExtensionValue)
  23500. {
  23501. auto bufferViewValue = dracoExtensionValue.Get("bufferView");
  23502. if (!bufferViewValue.IsInt()) return false;
  23503. auto attributesValue = dracoExtensionValue.Get("attributes");
  23504. if (!attributesValue.IsObject()) return false;
  23505. auto attributesObject = attributesValue.Get<Value::Object>();
  23506. int bufferView = bufferViewValue.Get<int>();
  23507. BufferView &view = model->bufferViews[bufferView];
  23508. Buffer &buffer = model->buffers[view.buffer];
  23509. // BufferView has already been decoded
  23510. if (view.dracoDecoded) return true;
  23511. view.dracoDecoded = true;
  23512. const char *bufferViewData =
  23513. reinterpret_cast<const char *>(buffer.data.data() + view.byteOffset);
  23514. size_t bufferViewSize = view.byteLength;
  23515. // decode draco
  23516. draco::DecoderBuffer decoderBuffer;
  23517. decoderBuffer.Init(bufferViewData, bufferViewSize);
  23518. draco::Decoder decoder;
  23519. auto decodeResult = decoder.DecodeMeshFromBuffer(&decoderBuffer);
  23520. if (!decodeResult.ok())
  23521. {
  23522. return false;
  23523. }
  23524. const std::unique_ptr<draco::Mesh> &mesh = decodeResult.value();
  23525. // create new bufferView for indices
  23526. if (primitive->indices >= 0)
  23527. {
  23528. int32_t componentSize = GetComponentSizeInBytes(
  23529. model->accessors[primitive->indices].componentType);
  23530. Buffer decodedIndexBuffer;
  23531. decodedIndexBuffer.data.resize(mesh->num_faces() * 3 * componentSize);
  23532. DecodeIndexBuffer(mesh.get(), componentSize, decodedIndexBuffer.data);
  23533. model->buffers.emplace_back(std::move(decodedIndexBuffer));
  23534. BufferView decodedIndexBufferView;
  23535. decodedIndexBufferView.buffer = int(model->buffers.size() - 1);
  23536. decodedIndexBufferView.byteLength =
  23537. int(mesh->num_faces() * 3 * componentSize);
  23538. decodedIndexBufferView.byteOffset = 0;
  23539. decodedIndexBufferView.byteStride = 0;
  23540. decodedIndexBufferView.target = TINYGLTF_TARGET_ARRAY_BUFFER;
  23541. model->bufferViews.emplace_back(std::move(decodedIndexBufferView));
  23542. model->accessors[primitive->indices].bufferView =
  23543. int(model->bufferViews.size() - 1);
  23544. model->accessors[primitive->indices].count = int(mesh->num_faces() * 3);
  23545. }
  23546. for (const auto &attribute : attributesObject)
  23547. {
  23548. if (!attribute.second.IsInt()) return false;
  23549. auto primitiveAttribute = primitive->attributes.find(attribute.first);
  23550. if (primitiveAttribute == primitive->attributes.end()) return false;
  23551. int dracoAttributeIndex = attribute.second.Get<int>();
  23552. const auto pAttribute = mesh->GetAttributeByUniqueId(dracoAttributeIndex);
  23553. const auto pBuffer = pAttribute->buffer();
  23554. const auto componentType =
  23555. model->accessors[primitiveAttribute->second].componentType;
  23556. // Create a new buffer for this decoded buffer
  23557. Buffer decodedBuffer;
  23558. size_t bufferSize = mesh->num_points() * pAttribute->num_components() *
  23559. GetComponentSizeInBytes(componentType);
  23560. decodedBuffer.data.resize(bufferSize);
  23561. if (!GetAttributeForAllPoints(componentType, mesh.get(), pAttribute,
  23562. decodedBuffer.data))
  23563. return false;
  23564. model->buffers.emplace_back(std::move(decodedBuffer));
  23565. BufferView decodedBufferView;
  23566. decodedBufferView.buffer = int(model->buffers.size() - 1);
  23567. decodedBufferView.byteLength = bufferSize;
  23568. decodedBufferView.byteOffset = pAttribute->byte_offset();
  23569. decodedBufferView.byteStride = pAttribute->byte_stride();
  23570. decodedBufferView.target = primitive->indices >= 0
  23571. ? TINYGLTF_TARGET_ELEMENT_ARRAY_BUFFER
  23572. : TINYGLTF_TARGET_ARRAY_BUFFER;
  23573. model->bufferViews.emplace_back(std::move(decodedBufferView));
  23574. model->accessors[primitiveAttribute->second].bufferView =
  23575. int(model->bufferViews.size() - 1);
  23576. model->accessors[primitiveAttribute->second].count =
  23577. int(mesh->num_points());
  23578. }
  23579. return true;
  23580. }
  23581. #endif
  23582. static bool ParsePrimitive(Primitive *primitive, Model *model, std::string *err,
  23583. const json &o,
  23584. bool store_original_json_for_extras_and_extensions)
  23585. {
  23586. int material = -1;
  23587. ParseIntegerProperty(&material, err, o, "material", false);
  23588. primitive->material = material;
  23589. int mode = TINYGLTF_MODE_TRIANGLES;
  23590. ParseIntegerProperty(&mode, err, o, "mode", false);
  23591. primitive->mode = mode; // Why only triangled were supported ?
  23592. int indices = -1;
  23593. ParseIntegerProperty(&indices, err, o, "indices", false);
  23594. primitive->indices = indices;
  23595. if (!ParseStringIntegerProperty(&primitive->attributes, err, o, "attributes",
  23596. true, "Primitive"))
  23597. {
  23598. return false;
  23599. }
  23600. // Look for morph targets
  23601. json_const_iterator targetsObject;
  23602. if (FindMember(o, "targets", targetsObject) &&
  23603. IsArray(GetValue(targetsObject)))
  23604. {
  23605. auto targetsObjectEnd = ArrayEnd(GetValue(targetsObject));
  23606. for (json_const_array_iterator i = ArrayBegin(GetValue(targetsObject));
  23607. i != targetsObjectEnd; ++i)
  23608. {
  23609. std::map<std::string, int> targetAttribues;
  23610. const json &dict = *i;
  23611. if (IsObject(dict))
  23612. {
  23613. json_const_iterator dictIt(ObjectBegin(dict));
  23614. json_const_iterator dictItEnd(ObjectEnd(dict));
  23615. for (; dictIt != dictItEnd; ++dictIt)
  23616. {
  23617. int iVal;
  23618. if (GetInt(GetValue(dictIt), iVal))
  23619. targetAttribues[GetKey(dictIt)] = iVal;
  23620. }
  23621. primitive->targets.emplace_back(std::move(targetAttribues));
  23622. }
  23623. }
  23624. }
  23625. ParseExtrasProperty(&(primitive->extras), o);
  23626. ParseExtensionsProperty(&primitive->extensions, err, o);
  23627. if (store_original_json_for_extras_and_extensions)
  23628. {
  23629. {
  23630. json_const_iterator it;
  23631. if (FindMember(o, "extensions", it))
  23632. {
  23633. primitive->extensions_json_string = JsonToString(GetValue(it));
  23634. }
  23635. }
  23636. {
  23637. json_const_iterator it;
  23638. if (FindMember(o, "extras", it))
  23639. {
  23640. primitive->extras_json_string = JsonToString(GetValue(it));
  23641. }
  23642. }
  23643. }
  23644. #ifdef TINYGLTF_ENABLE_DRACO
  23645. auto dracoExtension =
  23646. primitive->extensions.find("KHR_draco_mesh_compression");
  23647. if (dracoExtension != primitive->extensions.end())
  23648. {
  23649. ParseDracoExtension(primitive, model, err, dracoExtension->second);
  23650. }
  23651. #else
  23652. (void)model;
  23653. #endif
  23654. return true;
  23655. }
  23656. static bool ParseMesh(Mesh *mesh, Model *model, std::string *err, const json &o,
  23657. bool store_original_json_for_extras_and_extensions)
  23658. {
  23659. ParseStringProperty(&mesh->name, err, o, "name", false);
  23660. mesh->primitives.clear();
  23661. json_const_iterator primObject;
  23662. if (FindMember(o, "primitives", primObject) &&
  23663. IsArray(GetValue(primObject)))
  23664. {
  23665. json_const_array_iterator primEnd = ArrayEnd(GetValue(primObject));
  23666. for (json_const_array_iterator i = ArrayBegin(GetValue(primObject));
  23667. i != primEnd; ++i)
  23668. {
  23669. Primitive primitive;
  23670. if (ParsePrimitive(&primitive, model, err, *i,
  23671. store_original_json_for_extras_and_extensions))
  23672. {
  23673. // Only add the primitive if the parsing succeeds.
  23674. mesh->primitives.emplace_back(std::move(primitive));
  23675. }
  23676. }
  23677. }
  23678. // Should probably check if has targets and if dimensions fit
  23679. ParseNumberArrayProperty(&mesh->weights, err, o, "weights", false);
  23680. ParseExtensionsProperty(&mesh->extensions, err, o);
  23681. ParseExtrasProperty(&(mesh->extras), o);
  23682. if (store_original_json_for_extras_and_extensions)
  23683. {
  23684. {
  23685. json_const_iterator it;
  23686. if (FindMember(o, "extensions", it))
  23687. {
  23688. mesh->extensions_json_string = JsonToString(GetValue(it));
  23689. }
  23690. }
  23691. {
  23692. json_const_iterator it;
  23693. if (FindMember(o, "extras", it))
  23694. {
  23695. mesh->extras_json_string = JsonToString(GetValue(it));
  23696. }
  23697. }
  23698. }
  23699. return true;
  23700. }
  23701. static bool ParseNode(Node *node, std::string *err, const json &o,
  23702. bool store_original_json_for_extras_and_extensions)
  23703. {
  23704. ParseStringProperty(&node->name, err, o, "name", false);
  23705. int skin = -1;
  23706. ParseIntegerProperty(&skin, err, o, "skin", false);
  23707. node->skin = skin;
  23708. // Matrix and T/R/S are exclusive
  23709. if (!ParseNumberArrayProperty(&node->matrix, err, o, "matrix", false))
  23710. {
  23711. ParseNumberArrayProperty(&node->rotation, err, o, "rotation", false);
  23712. ParseNumberArrayProperty(&node->scale, err, o, "scale", false);
  23713. ParseNumberArrayProperty(&node->translation, err, o, "translation", false);
  23714. }
  23715. int camera = -1;
  23716. ParseIntegerProperty(&camera, err, o, "camera", false);
  23717. node->camera = camera;
  23718. int mesh = -1;
  23719. ParseIntegerProperty(&mesh, err, o, "mesh", false);
  23720. node->mesh = mesh;
  23721. node->children.clear();
  23722. ParseIntegerArrayProperty(&node->children, err, o, "children", false);
  23723. ParseNumberArrayProperty(&node->weights, err, o, "weights", false);
  23724. ParseExtensionsProperty(&node->extensions, err, o);
  23725. ParseExtrasProperty(&(node->extras), o);
  23726. if (store_original_json_for_extras_and_extensions)
  23727. {
  23728. {
  23729. json_const_iterator it;
  23730. if (FindMember(o, "extensions", it))
  23731. {
  23732. node->extensions_json_string = JsonToString(GetValue(it));
  23733. }
  23734. }
  23735. {
  23736. json_const_iterator it;
  23737. if (FindMember(o, "extras", it))
  23738. {
  23739. node->extras_json_string = JsonToString(GetValue(it));
  23740. }
  23741. }
  23742. }
  23743. return true;
  23744. }
  23745. static bool ParsePbrMetallicRoughness(
  23746. PbrMetallicRoughness *pbr, std::string *err, const json &o,
  23747. bool store_original_json_for_extras_and_extensions)
  23748. {
  23749. if (pbr == nullptr)
  23750. {
  23751. return false;
  23752. }
  23753. std::vector<double> baseColorFactor;
  23754. if (ParseNumberArrayProperty(&baseColorFactor, err, o, "baseColorFactor",
  23755. /* required */ false))
  23756. {
  23757. if (baseColorFactor.size() != 4)
  23758. {
  23759. if (err)
  23760. {
  23761. (*err) +=
  23762. "Array length of `baseColorFactor` parameter in "
  23763. "pbrMetallicRoughness must be 4, but got " +
  23764. std::to_string(baseColorFactor.size()) + "\n";
  23765. }
  23766. return false;
  23767. }
  23768. pbr->baseColorFactor = baseColorFactor;
  23769. }
  23770. {
  23771. json_const_iterator it;
  23772. if (FindMember(o, "baseColorTexture", it))
  23773. {
  23774. ParseTextureInfo(&pbr->baseColorTexture, err, GetValue(it),
  23775. store_original_json_for_extras_and_extensions);
  23776. }
  23777. }
  23778. {
  23779. json_const_iterator it;
  23780. if (FindMember(o, "metallicRoughnessTexture", it))
  23781. {
  23782. ParseTextureInfo(&pbr->metallicRoughnessTexture, err, GetValue(it),
  23783. store_original_json_for_extras_and_extensions);
  23784. }
  23785. }
  23786. ParseNumberProperty(&pbr->metallicFactor, err, o, "metallicFactor", false);
  23787. ParseNumberProperty(&pbr->roughnessFactor, err, o, "roughnessFactor", false);
  23788. ParseExtensionsProperty(&pbr->extensions, err, o);
  23789. ParseExtrasProperty(&pbr->extras, o);
  23790. if (store_original_json_for_extras_and_extensions)
  23791. {
  23792. {
  23793. json_const_iterator it;
  23794. if (FindMember(o, "extensions", it))
  23795. {
  23796. pbr->extensions_json_string = JsonToString(GetValue(it));
  23797. }
  23798. }
  23799. {
  23800. json_const_iterator it;
  23801. if (FindMember(o, "extras", it))
  23802. {
  23803. pbr->extras_json_string = JsonToString(GetValue(it));
  23804. }
  23805. }
  23806. }
  23807. return true;
  23808. }
  23809. static bool ParseMaterial(Material *material, std::string *err, const json &o,
  23810. bool store_original_json_for_extras_and_extensions)
  23811. {
  23812. ParseStringProperty(&material->name, err, o, "name", /* required */ false);
  23813. if (ParseNumberArrayProperty(&material->emissiveFactor, err, o,
  23814. "emissiveFactor",
  23815. /* required */ false))
  23816. {
  23817. if (material->emissiveFactor.size() != 3)
  23818. {
  23819. if (err)
  23820. {
  23821. (*err) +=
  23822. "Array length of `emissiveFactor` parameter in "
  23823. "material must be 3, but got " +
  23824. std::to_string(material->emissiveFactor.size()) + "\n";
  23825. }
  23826. return false;
  23827. }
  23828. }
  23829. else
  23830. {
  23831. // fill with default values
  23832. material->emissiveFactor = { 0.0, 0.0, 0.0 };
  23833. }
  23834. ParseStringProperty(&material->alphaMode, err, o, "alphaMode",
  23835. /* required */ false);
  23836. ParseNumberProperty(&material->alphaCutoff, err, o, "alphaCutoff",
  23837. /* required */ false);
  23838. ParseBooleanProperty(&material->doubleSided, err, o, "doubleSided",
  23839. /* required */ false);
  23840. {
  23841. json_const_iterator it;
  23842. if (FindMember(o, "pbrMetallicRoughness", it))
  23843. {
  23844. ParsePbrMetallicRoughness(&material->pbrMetallicRoughness, err,
  23845. GetValue(it),
  23846. store_original_json_for_extras_and_extensions);
  23847. }
  23848. }
  23849. {
  23850. json_const_iterator it;
  23851. if (FindMember(o, "normalTexture", it))
  23852. {
  23853. ParseNormalTextureInfo(&material->normalTexture, err, GetValue(it),
  23854. store_original_json_for_extras_and_extensions);
  23855. }
  23856. }
  23857. {
  23858. json_const_iterator it;
  23859. if (FindMember(o, "occlusionTexture", it))
  23860. {
  23861. ParseOcclusionTextureInfo(&material->occlusionTexture, err, GetValue(it),
  23862. store_original_json_for_extras_and_extensions);
  23863. }
  23864. }
  23865. {
  23866. json_const_iterator it;
  23867. if (FindMember(o, "emissiveTexture", it))
  23868. {
  23869. ParseTextureInfo(&material->emissiveTexture, err, GetValue(it),
  23870. store_original_json_for_extras_and_extensions);
  23871. }
  23872. }
  23873. // Old code path. For backward compatibility, we still store material values
  23874. // as Parameter. This will create duplicated information for
  23875. // example(pbrMetallicRoughness), but should be neglible in terms of memory
  23876. // consumption.
  23877. // TODO(syoyo): Remove in the next major release.
  23878. material->values.clear();
  23879. material->additionalValues.clear();
  23880. json_const_iterator it(ObjectBegin(o));
  23881. json_const_iterator itEnd(ObjectEnd(o));
  23882. for (; it != itEnd; ++it)
  23883. {
  23884. std::string key(GetKey(it));
  23885. if (key == "pbrMetallicRoughness")
  23886. {
  23887. if (IsObject(GetValue(it)))
  23888. {
  23889. const json &values_object = GetValue(it);
  23890. json_const_iterator itVal(ObjectBegin(values_object));
  23891. json_const_iterator itValEnd(ObjectEnd(values_object));
  23892. for (; itVal != itValEnd; ++itVal)
  23893. {
  23894. Parameter param;
  23895. if (ParseParameterProperty(&param, err, values_object, GetKey(itVal),
  23896. false))
  23897. {
  23898. material->values.emplace(GetKey(itVal), std::move(param));
  23899. }
  23900. }
  23901. }
  23902. }
  23903. else if (key == "extensions" || key == "extras")
  23904. {
  23905. // done later, skip, otherwise poorly parsed contents will be saved in the
  23906. // parametermap and serialized again later
  23907. }
  23908. else
  23909. {
  23910. Parameter param;
  23911. if (ParseParameterProperty(&param, err, o, key, false))
  23912. {
  23913. // names of materials have already been parsed. Putting it in this map
  23914. // doesn't correctly reflext the glTF specification
  23915. if (key != "name")
  23916. material->additionalValues.emplace(std::move(key), std::move(param));
  23917. }
  23918. }
  23919. }
  23920. material->extensions.clear();
  23921. ParseExtensionsProperty(&material->extensions, err, o);
  23922. ParseExtrasProperty(&(material->extras), o);
  23923. if (store_original_json_for_extras_and_extensions)
  23924. {
  23925. {
  23926. json_const_iterator eit;
  23927. if (FindMember(o, "extensions", eit))
  23928. {
  23929. material->extensions_json_string = JsonToString(GetValue(eit));
  23930. }
  23931. }
  23932. {
  23933. json_const_iterator eit;
  23934. if (FindMember(o, "extras", eit))
  23935. {
  23936. material->extras_json_string = JsonToString(GetValue(eit));
  23937. }
  23938. }
  23939. }
  23940. return true;
  23941. }
  23942. static bool ParseAnimationChannel(
  23943. AnimationChannel *channel, std::string *err, const json &o,
  23944. bool store_original_json_for_extras_and_extensions)
  23945. {
  23946. int samplerIndex = -1;
  23947. int targetIndex = -1;
  23948. if (!ParseIntegerProperty(&samplerIndex, err, o, "sampler", true,
  23949. "AnimationChannel"))
  23950. {
  23951. if (err)
  23952. {
  23953. (*err) += "`sampler` field is missing in animation channels\n";
  23954. }
  23955. return false;
  23956. }
  23957. json_const_iterator targetIt;
  23958. if (FindMember(o, "target", targetIt) && IsObject(GetValue(targetIt)))
  23959. {
  23960. const json &target_object = GetValue(targetIt);
  23961. if (!ParseIntegerProperty(&targetIndex, err, target_object, "node", true))
  23962. {
  23963. if (err)
  23964. {
  23965. (*err) += "`node` field is missing in animation.channels.target\n";
  23966. }
  23967. return false;
  23968. }
  23969. if (!ParseStringProperty(&channel->target_path, err, target_object, "path",
  23970. true))
  23971. {
  23972. if (err)
  23973. {
  23974. (*err) += "`path` field is missing in animation.channels.target\n";
  23975. }
  23976. return false;
  23977. }
  23978. ParseExtensionsProperty(&channel->target_extensions, err, target_object);
  23979. if (store_original_json_for_extras_and_extensions)
  23980. {
  23981. json_const_iterator it;
  23982. if (FindMember(target_object, "extensions", it))
  23983. {
  23984. channel->target_extensions_json_string = JsonToString(GetValue(it));
  23985. }
  23986. }
  23987. }
  23988. channel->sampler = samplerIndex;
  23989. channel->target_node = targetIndex;
  23990. ParseExtensionsProperty(&channel->extensions, err, o);
  23991. ParseExtrasProperty(&(channel->extras), o);
  23992. if (store_original_json_for_extras_and_extensions)
  23993. {
  23994. {
  23995. json_const_iterator it;
  23996. if (FindMember(o, "extensions", it))
  23997. {
  23998. channel->extensions_json_string = JsonToString(GetValue(it));
  23999. }
  24000. }
  24001. {
  24002. json_const_iterator it;
  24003. if (FindMember(o, "extras", it))
  24004. {
  24005. channel->extras_json_string = JsonToString(GetValue(it));
  24006. }
  24007. }
  24008. }
  24009. return true;
  24010. }
  24011. static bool ParseAnimation(Animation *animation, std::string *err,
  24012. const json &o,
  24013. bool store_original_json_for_extras_and_extensions)
  24014. {
  24015. {
  24016. json_const_iterator channelsIt;
  24017. if (FindMember(o, "channels", channelsIt) &&
  24018. IsArray(GetValue(channelsIt)))
  24019. {
  24020. json_const_array_iterator channelEnd = ArrayEnd(GetValue(channelsIt));
  24021. for (json_const_array_iterator i = ArrayBegin(GetValue(channelsIt));
  24022. i != channelEnd; ++i)
  24023. {
  24024. AnimationChannel channel;
  24025. if (ParseAnimationChannel(
  24026. &channel, err, *i,
  24027. store_original_json_for_extras_and_extensions))
  24028. {
  24029. // Only add the channel if the parsing succeeds.
  24030. animation->channels.emplace_back(std::move(channel));
  24031. }
  24032. }
  24033. }
  24034. }
  24035. {
  24036. json_const_iterator samplerIt;
  24037. if (FindMember(o, "samplers", samplerIt) && IsArray(GetValue(samplerIt)))
  24038. {
  24039. const json &sampler_array = GetValue(samplerIt);
  24040. json_const_array_iterator it = ArrayBegin(sampler_array);
  24041. json_const_array_iterator itEnd = ArrayEnd(sampler_array);
  24042. for (; it != itEnd; ++it)
  24043. {
  24044. const json &s = *it;
  24045. AnimationSampler sampler;
  24046. int inputIndex = -1;
  24047. int outputIndex = -1;
  24048. if (!ParseIntegerProperty(&inputIndex, err, s, "input", true))
  24049. {
  24050. if (err)
  24051. {
  24052. (*err) += "`input` field is missing in animation.sampler\n";
  24053. }
  24054. return false;
  24055. }
  24056. ParseStringProperty(&sampler.interpolation, err, s, "interpolation",
  24057. false);
  24058. if (!ParseIntegerProperty(&outputIndex, err, s, "output", true))
  24059. {
  24060. if (err)
  24061. {
  24062. (*err) += "`output` field is missing in animation.sampler\n";
  24063. }
  24064. return false;
  24065. }
  24066. sampler.input = inputIndex;
  24067. sampler.output = outputIndex;
  24068. ParseExtensionsProperty(&(sampler.extensions), err, o);
  24069. ParseExtrasProperty(&(sampler.extras), s);
  24070. if (store_original_json_for_extras_and_extensions)
  24071. {
  24072. {
  24073. json_const_iterator eit;
  24074. if (FindMember(o, "extensions", eit))
  24075. {
  24076. sampler.extensions_json_string = JsonToString(GetValue(eit));
  24077. }
  24078. }
  24079. {
  24080. json_const_iterator eit;
  24081. if (FindMember(o, "extras", eit))
  24082. {
  24083. sampler.extras_json_string = JsonToString(GetValue(eit));
  24084. }
  24085. }
  24086. }
  24087. animation->samplers.emplace_back(std::move(sampler));
  24088. }
  24089. }
  24090. }
  24091. ParseStringProperty(&animation->name, err, o, "name", false);
  24092. ParseExtensionsProperty(&animation->extensions, err, o);
  24093. ParseExtrasProperty(&(animation->extras), o);
  24094. if (store_original_json_for_extras_and_extensions)
  24095. {
  24096. {
  24097. json_const_iterator it;
  24098. if (FindMember(o, "extensions", it))
  24099. {
  24100. animation->extensions_json_string = JsonToString(GetValue(it));
  24101. }
  24102. }
  24103. {
  24104. json_const_iterator it;
  24105. if (FindMember(o, "extras", it))
  24106. {
  24107. animation->extras_json_string = JsonToString(GetValue(it));
  24108. }
  24109. }
  24110. }
  24111. return true;
  24112. }
  24113. static bool ParseSampler(Sampler *sampler, std::string *err, const json &o,
  24114. bool store_original_json_for_extras_and_extensions)
  24115. {
  24116. ParseStringProperty(&sampler->name, err, o, "name", false);
  24117. int minFilter = -1;
  24118. int magFilter = -1;
  24119. int wrapS = TINYGLTF_TEXTURE_WRAP_REPEAT;
  24120. int wrapT = TINYGLTF_TEXTURE_WRAP_REPEAT;
  24121. int wrapR = TINYGLTF_TEXTURE_WRAP_REPEAT;
  24122. ParseIntegerProperty(&minFilter, err, o, "minFilter", false);
  24123. ParseIntegerProperty(&magFilter, err, o, "magFilter", false);
  24124. ParseIntegerProperty(&wrapS, err, o, "wrapS", false);
  24125. ParseIntegerProperty(&wrapT, err, o, "wrapT", false);
  24126. ParseIntegerProperty(&wrapR, err, o, "wrapR", false); // tinygltf extension
  24127. // TODO(syoyo): Check the value is alloed one.
  24128. // (e.g. we allow 9728(NEAREST), but don't allow 9727)
  24129. sampler->minFilter = minFilter;
  24130. sampler->magFilter = magFilter;
  24131. sampler->wrapS = wrapS;
  24132. sampler->wrapT = wrapT;
  24133. sampler->wrapR = wrapR;
  24134. ParseExtensionsProperty(&(sampler->extensions), err, o);
  24135. ParseExtrasProperty(&(sampler->extras), o);
  24136. if (store_original_json_for_extras_and_extensions)
  24137. {
  24138. {
  24139. json_const_iterator it;
  24140. if (FindMember(o, "extensions", it))
  24141. {
  24142. sampler->extensions_json_string = JsonToString(GetValue(it));
  24143. }
  24144. }
  24145. {
  24146. json_const_iterator it;
  24147. if (FindMember(o, "extras", it))
  24148. {
  24149. sampler->extras_json_string = JsonToString(GetValue(it));
  24150. }
  24151. }
  24152. }
  24153. return true;
  24154. }
  24155. static bool ParseSkin(Skin *skin, std::string *err, const json &o,
  24156. bool store_original_json_for_extras_and_extensions)
  24157. {
  24158. ParseStringProperty(&skin->name, err, o, "name", false, "Skin");
  24159. std::vector<int> joints;
  24160. if (!ParseIntegerArrayProperty(&joints, err, o, "joints", false, "Skin"))
  24161. {
  24162. return false;
  24163. }
  24164. skin->joints = std::move(joints);
  24165. int skeleton = -1;
  24166. ParseIntegerProperty(&skeleton, err, o, "skeleton", false, "Skin");
  24167. skin->skeleton = skeleton;
  24168. int invBind = -1;
  24169. ParseIntegerProperty(&invBind, err, o, "inverseBindMatrices", true, "Skin");
  24170. skin->inverseBindMatrices = invBind;
  24171. ParseExtensionsProperty(&(skin->extensions), err, o);
  24172. ParseExtrasProperty(&(skin->extras), o);
  24173. if (store_original_json_for_extras_and_extensions)
  24174. {
  24175. {
  24176. json_const_iterator it;
  24177. if (FindMember(o, "extensions", it))
  24178. {
  24179. skin->extensions_json_string = JsonToString(GetValue(it));
  24180. }
  24181. }
  24182. {
  24183. json_const_iterator it;
  24184. if (FindMember(o, "extras", it))
  24185. {
  24186. skin->extras_json_string = JsonToString(GetValue(it));
  24187. }
  24188. }
  24189. }
  24190. return true;
  24191. }
  24192. static bool ParsePerspectiveCamera(
  24193. PerspectiveCamera *camera, std::string *err, const json &o,
  24194. bool store_original_json_for_extras_and_extensions)
  24195. {
  24196. double yfov = 0.0;
  24197. if (!ParseNumberProperty(&yfov, err, o, "yfov", true, "OrthographicCamera"))
  24198. {
  24199. return false;
  24200. }
  24201. double znear = 0.0;
  24202. if (!ParseNumberProperty(&znear, err, o, "znear", true,
  24203. "PerspectiveCamera"))
  24204. {
  24205. return false;
  24206. }
  24207. double aspectRatio = 0.0; // = invalid
  24208. ParseNumberProperty(&aspectRatio, err, o, "aspectRatio", false,
  24209. "PerspectiveCamera");
  24210. double zfar = 0.0; // = invalid
  24211. ParseNumberProperty(&zfar, err, o, "zfar", false, "PerspectiveCamera");
  24212. camera->aspectRatio = aspectRatio;
  24213. camera->zfar = zfar;
  24214. camera->yfov = yfov;
  24215. camera->znear = znear;
  24216. ParseExtensionsProperty(&camera->extensions, err, o);
  24217. ParseExtrasProperty(&(camera->extras), o);
  24218. if (store_original_json_for_extras_and_extensions)
  24219. {
  24220. {
  24221. json_const_iterator it;
  24222. if (FindMember(o, "extensions", it))
  24223. {
  24224. camera->extensions_json_string = JsonToString(GetValue(it));
  24225. }
  24226. }
  24227. {
  24228. json_const_iterator it;
  24229. if (FindMember(o, "extras", it))
  24230. {
  24231. camera->extras_json_string = JsonToString(GetValue(it));
  24232. }
  24233. }
  24234. }
  24235. // TODO(syoyo): Validate parameter values.
  24236. return true;
  24237. }
  24238. static bool ParseSpotLight(SpotLight *light, std::string *err, const json &o,
  24239. bool store_original_json_for_extras_and_extensions)
  24240. {
  24241. ParseNumberProperty(&light->innerConeAngle, err, o, "innerConeAngle", false);
  24242. ParseNumberProperty(&light->outerConeAngle, err, o, "outerConeAngle", false);
  24243. ParseExtensionsProperty(&light->extensions, err, o);
  24244. ParseExtrasProperty(&light->extras, o);
  24245. if (store_original_json_for_extras_and_extensions)
  24246. {
  24247. {
  24248. json_const_iterator it;
  24249. if (FindMember(o, "extensions", it))
  24250. {
  24251. light->extensions_json_string = JsonToString(GetValue(it));
  24252. }
  24253. }
  24254. {
  24255. json_const_iterator it;
  24256. if (FindMember(o, "extras", it))
  24257. {
  24258. light->extras_json_string = JsonToString(GetValue(it));
  24259. }
  24260. }
  24261. }
  24262. // TODO(syoyo): Validate parameter values.
  24263. return true;
  24264. }
  24265. static bool ParseOrthographicCamera(
  24266. OrthographicCamera *camera, std::string *err, const json &o,
  24267. bool store_original_json_for_extras_and_extensions)
  24268. {
  24269. double xmag = 0.0;
  24270. if (!ParseNumberProperty(&xmag, err, o, "xmag", true, "OrthographicCamera"))
  24271. {
  24272. return false;
  24273. }
  24274. double ymag = 0.0;
  24275. if (!ParseNumberProperty(&ymag, err, o, "ymag", true, "OrthographicCamera"))
  24276. {
  24277. return false;
  24278. }
  24279. double zfar = 0.0;
  24280. if (!ParseNumberProperty(&zfar, err, o, "zfar", true, "OrthographicCamera"))
  24281. {
  24282. return false;
  24283. }
  24284. double znear = 0.0;
  24285. if (!ParseNumberProperty(&znear, err, o, "znear", true,
  24286. "OrthographicCamera"))
  24287. {
  24288. return false;
  24289. }
  24290. ParseExtensionsProperty(&camera->extensions, err, o);
  24291. ParseExtrasProperty(&(camera->extras), o);
  24292. if (store_original_json_for_extras_and_extensions)
  24293. {
  24294. {
  24295. json_const_iterator it;
  24296. if (FindMember(o, "extensions", it))
  24297. {
  24298. camera->extensions_json_string = JsonToString(GetValue(it));
  24299. }
  24300. }
  24301. {
  24302. json_const_iterator it;
  24303. if (FindMember(o, "extras", it))
  24304. {
  24305. camera->extras_json_string = JsonToString(GetValue(it));
  24306. }
  24307. }
  24308. }
  24309. camera->xmag = xmag;
  24310. camera->ymag = ymag;
  24311. camera->zfar = zfar;
  24312. camera->znear = znear;
  24313. // TODO(syoyo): Validate parameter values.
  24314. return true;
  24315. }
  24316. static bool ParseCamera(Camera *camera, std::string *err, const json &o,
  24317. bool store_original_json_for_extras_and_extensions)
  24318. {
  24319. if (!ParseStringProperty(&camera->type, err, o, "type", true, "Camera"))
  24320. {
  24321. return false;
  24322. }
  24323. if (camera->type.compare("orthographic") == 0)
  24324. {
  24325. json_const_iterator orthoIt;
  24326. if (!FindMember(o, "orthographic", orthoIt))
  24327. {
  24328. if (err)
  24329. {
  24330. std::stringstream ss;
  24331. ss << "Orhographic camera description not found." << std::endl;
  24332. (*err) += ss.str();
  24333. }
  24334. return false;
  24335. }
  24336. const json &v = GetValue(orthoIt);
  24337. if (!IsObject(v))
  24338. {
  24339. if (err)
  24340. {
  24341. std::stringstream ss;
  24342. ss << "\"orthographic\" is not a JSON object." << std::endl;
  24343. (*err) += ss.str();
  24344. }
  24345. return false;
  24346. }
  24347. if (!ParseOrthographicCamera(
  24348. &camera->orthographic, err, v,
  24349. store_original_json_for_extras_and_extensions))
  24350. {
  24351. return false;
  24352. }
  24353. }
  24354. else if (camera->type.compare("perspective") == 0)
  24355. {
  24356. json_const_iterator perspIt;
  24357. if (!FindMember(o, "perspective", perspIt))
  24358. {
  24359. if (err)
  24360. {
  24361. std::stringstream ss;
  24362. ss << "Perspective camera description not found." << std::endl;
  24363. (*err) += ss.str();
  24364. }
  24365. return false;
  24366. }
  24367. const json &v = GetValue(perspIt);
  24368. if (!IsObject(v))
  24369. {
  24370. if (err)
  24371. {
  24372. std::stringstream ss;
  24373. ss << "\"perspective\" is not a JSON object." << std::endl;
  24374. (*err) += ss.str();
  24375. }
  24376. return false;
  24377. }
  24378. if (!ParsePerspectiveCamera(
  24379. &camera->perspective, err, v,
  24380. store_original_json_for_extras_and_extensions))
  24381. {
  24382. return false;
  24383. }
  24384. }
  24385. else
  24386. {
  24387. if (err)
  24388. {
  24389. std::stringstream ss;
  24390. ss << "Invalid camera type: \"" << camera->type
  24391. << "\". Must be \"perspective\" or \"orthographic\"" << std::endl;
  24392. (*err) += ss.str();
  24393. }
  24394. return false;
  24395. }
  24396. ParseStringProperty(&camera->name, err, o, "name", false);
  24397. ParseExtensionsProperty(&camera->extensions, err, o);
  24398. ParseExtrasProperty(&(camera->extras), o);
  24399. if (store_original_json_for_extras_and_extensions)
  24400. {
  24401. {
  24402. json_const_iterator it;
  24403. if (FindMember(o, "extensions", it))
  24404. {
  24405. camera->extensions_json_string = JsonToString(GetValue(it));
  24406. }
  24407. }
  24408. {
  24409. json_const_iterator it;
  24410. if (FindMember(o, "extras", it))
  24411. {
  24412. camera->extras_json_string = JsonToString(GetValue(it));
  24413. }
  24414. }
  24415. }
  24416. return true;
  24417. }
  24418. static bool ParseLight(Light *light, std::string *err, const json &o,
  24419. bool store_original_json_for_extras_and_extensions)
  24420. {
  24421. if (!ParseStringProperty(&light->type, err, o, "type", true))
  24422. {
  24423. return false;
  24424. }
  24425. if (light->type == "spot")
  24426. {
  24427. json_const_iterator spotIt;
  24428. if (!FindMember(o, "spot", spotIt))
  24429. {
  24430. if (err)
  24431. {
  24432. std::stringstream ss;
  24433. ss << "Spot light description not found." << std::endl;
  24434. (*err) += ss.str();
  24435. }
  24436. return false;
  24437. }
  24438. const json &v = GetValue(spotIt);
  24439. if (!IsObject(v))
  24440. {
  24441. if (err)
  24442. {
  24443. std::stringstream ss;
  24444. ss << "\"spot\" is not a JSON object." << std::endl;
  24445. (*err) += ss.str();
  24446. }
  24447. return false;
  24448. }
  24449. if (!ParseSpotLight(&light->spot, err, v,
  24450. store_original_json_for_extras_and_extensions))
  24451. {
  24452. return false;
  24453. }
  24454. }
  24455. ParseStringProperty(&light->name, err, o, "name", false);
  24456. ParseNumberArrayProperty(&light->color, err, o, "color", false);
  24457. ParseNumberProperty(&light->range, err, o, "range", false);
  24458. ParseNumberProperty(&light->intensity, err, o, "intensity", false);
  24459. ParseExtensionsProperty(&light->extensions, err, o);
  24460. ParseExtrasProperty(&(light->extras), o);
  24461. if (store_original_json_for_extras_and_extensions)
  24462. {
  24463. {
  24464. json_const_iterator it;
  24465. if (FindMember(o, "extensions", it))
  24466. {
  24467. light->extensions_json_string = JsonToString(GetValue(it));
  24468. }
  24469. }
  24470. {
  24471. json_const_iterator it;
  24472. if (FindMember(o, "extras", it))
  24473. {
  24474. light->extras_json_string = JsonToString(GetValue(it));
  24475. }
  24476. }
  24477. }
  24478. return true;
  24479. }
  24480. bool TinyGLTF::LoadFromString(Model *model, std::string *err, std::string *warn,
  24481. const char *json_str,
  24482. unsigned int json_str_length,
  24483. const std::string &base_dir,
  24484. unsigned int check_sections)
  24485. {
  24486. if (json_str_length < 4)
  24487. {
  24488. if (err)
  24489. {
  24490. (*err) = "JSON string too short.\n";
  24491. }
  24492. return false;
  24493. }
  24494. JsonDocument v;
  24495. #if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || \
  24496. defined(_CPPUNWIND)) && \
  24497. !defined(TINYGLTF_NOEXCEPTION)
  24498. try
  24499. {
  24500. JsonParse(v, json_str, json_str_length, true);
  24501. }
  24502. catch (const std::exception &e)
  24503. {
  24504. if (err)
  24505. {
  24506. (*err) = e.what();
  24507. }
  24508. return false;
  24509. }
  24510. #else
  24511. {
  24512. JsonParse(v, json_str, json_str_length);
  24513. if (!IsObject(v))
  24514. {
  24515. // Assume parsing was failed.
  24516. if (err)
  24517. {
  24518. (*err) = "Failed to parse JSON object\n";
  24519. }
  24520. return false;
  24521. }
  24522. }
  24523. #endif
  24524. if (!IsObject(v))
  24525. {
  24526. // root is not an object.
  24527. if (err)
  24528. {
  24529. (*err) = "Root element is not a JSON object\n";
  24530. }
  24531. return false;
  24532. }
  24533. {
  24534. bool version_found = false;
  24535. json_const_iterator it;
  24536. if (FindMember(v, "asset", it) && IsObject(GetValue(it)))
  24537. {
  24538. auto &itObj = GetValue(it);
  24539. json_const_iterator version_it;
  24540. std::string versionStr;
  24541. if (FindMember(itObj, "version", version_it) &&
  24542. GetString(GetValue(version_it), versionStr))
  24543. {
  24544. version_found = true;
  24545. }
  24546. }
  24547. if (version_found)
  24548. {
  24549. // OK
  24550. }
  24551. else if (check_sections & REQUIRE_VERSION)
  24552. {
  24553. if (err)
  24554. {
  24555. (*err) += "\"asset\" object not found in .gltf or not an object type\n";
  24556. }
  24557. return false;
  24558. }
  24559. }
  24560. // scene is not mandatory.
  24561. // FIXME Maybe a better way to handle it than removing the code
  24562. auto IsArrayMemberPresent = [](const json &_v, const char *name) -> bool
  24563. {
  24564. json_const_iterator it;
  24565. return FindMember(_v, name, it) && IsArray(GetValue(it));
  24566. };
  24567. {
  24568. if ((check_sections & REQUIRE_SCENES) &&
  24569. !IsArrayMemberPresent(v, "scenes"))
  24570. {
  24571. if (err)
  24572. {
  24573. (*err) += "\"scenes\" object not found in .gltf or not an array type\n";
  24574. }
  24575. return false;
  24576. }
  24577. }
  24578. {
  24579. if ((check_sections & REQUIRE_NODES) && !IsArrayMemberPresent(v, "nodes"))
  24580. {
  24581. if (err)
  24582. {
  24583. (*err) += "\"nodes\" object not found in .gltf\n";
  24584. }
  24585. return false;
  24586. }
  24587. }
  24588. {
  24589. if ((check_sections & REQUIRE_ACCESSORS) &&
  24590. !IsArrayMemberPresent(v, "accessors"))
  24591. {
  24592. if (err)
  24593. {
  24594. (*err) += "\"accessors\" object not found in .gltf\n";
  24595. }
  24596. return false;
  24597. }
  24598. }
  24599. {
  24600. if ((check_sections & REQUIRE_BUFFERS) &&
  24601. !IsArrayMemberPresent(v, "buffers"))
  24602. {
  24603. if (err)
  24604. {
  24605. (*err) += "\"buffers\" object not found in .gltf\n";
  24606. }
  24607. return false;
  24608. }
  24609. }
  24610. {
  24611. if ((check_sections & REQUIRE_BUFFER_VIEWS) &&
  24612. !IsArrayMemberPresent(v, "bufferViews"))
  24613. {
  24614. if (err)
  24615. {
  24616. (*err) += "\"bufferViews\" object not found in .gltf\n";
  24617. }
  24618. return false;
  24619. }
  24620. }
  24621. model->buffers.clear();
  24622. model->bufferViews.clear();
  24623. model->accessors.clear();
  24624. model->meshes.clear();
  24625. model->cameras.clear();
  24626. model->nodes.clear();
  24627. model->extensionsUsed.clear();
  24628. model->extensionsRequired.clear();
  24629. model->extensions.clear();
  24630. model->defaultScene = -1;
  24631. // 1. Parse Asset
  24632. {
  24633. json_const_iterator it;
  24634. if (FindMember(v, "asset", it) && IsObject(GetValue(it)))
  24635. {
  24636. const json &root = GetValue(it);
  24637. ParseAsset(&model->asset, err, root,
  24638. store_original_json_for_extras_and_extensions_);
  24639. }
  24640. }
  24641. #ifdef TINYGLTF_USE_CPP14
  24642. auto ForEachInArray = [](const json &_v, const char *member,
  24643. const auto &cb) -> bool
  24644. #else
  24645. // The std::function<> implementation can be less efficient because it will
  24646. // allocate heap when the size of the captured lambda is above 16 bytes with
  24647. // clang and gcc, but it does not require C++14.
  24648. auto ForEachInArray = [](const json &_v, const char *member,
  24649. const std::function<bool(const json &)> &cb) -> bool
  24650. #endif
  24651. {
  24652. json_const_iterator itm;
  24653. if (FindMember(_v, member, itm) && IsArray(GetValue(itm)))
  24654. {
  24655. const json &root = GetValue(itm);
  24656. auto it = ArrayBegin(root);
  24657. auto end = ArrayEnd(root);
  24658. for (; it != end; ++it)
  24659. {
  24660. if (!cb(*it)) return false;
  24661. }
  24662. }
  24663. return true;
  24664. };
  24665. // 2. Parse extensionUsed
  24666. {
  24667. ForEachInArray(v, "extensionsUsed", [&](const json &o)
  24668. {
  24669. std::string str;
  24670. GetString(o, str);
  24671. model->extensionsUsed.emplace_back(std::move(str));
  24672. return true;
  24673. });
  24674. }
  24675. {
  24676. ForEachInArray(v, "extensionsRequired", [&](const json &o)
  24677. {
  24678. std::string str;
  24679. GetString(o, str);
  24680. model->extensionsRequired.emplace_back(std::move(str));
  24681. return true;
  24682. });
  24683. }
  24684. // 3. Parse Buffer
  24685. {
  24686. bool success = ForEachInArray(v, "buffers", [&](const json &o)
  24687. {
  24688. if (!IsObject(o))
  24689. {
  24690. if (err)
  24691. {
  24692. (*err) += "`buffers' does not contain an JSON object.";
  24693. }
  24694. return false;
  24695. }
  24696. Buffer buffer;
  24697. if (!ParseBuffer(&buffer, err, o,
  24698. store_original_json_for_extras_and_extensions_, &fs,
  24699. base_dir, is_binary_, bin_data_, bin_size_))
  24700. {
  24701. return false;
  24702. }
  24703. model->buffers.emplace_back(std::move(buffer));
  24704. return true;
  24705. });
  24706. if (!success)
  24707. {
  24708. return false;
  24709. }
  24710. }
  24711. // 4. Parse BufferView
  24712. {
  24713. bool success = ForEachInArray(v, "bufferViews", [&](const json &o)
  24714. {
  24715. if (!IsObject(o))
  24716. {
  24717. if (err)
  24718. {
  24719. (*err) += "`bufferViews' does not contain an JSON object.";
  24720. }
  24721. return false;
  24722. }
  24723. BufferView bufferView;
  24724. if (!ParseBufferView(&bufferView, err, o,
  24725. store_original_json_for_extras_and_extensions_))
  24726. {
  24727. return false;
  24728. }
  24729. model->bufferViews.emplace_back(std::move(bufferView));
  24730. return true;
  24731. });
  24732. if (!success)
  24733. {
  24734. return false;
  24735. }
  24736. }
  24737. // 5. Parse Accessor
  24738. {
  24739. bool success = ForEachInArray(v, "accessors", [&](const json &o)
  24740. {
  24741. if (!IsObject(o))
  24742. {
  24743. if (err)
  24744. {
  24745. (*err) += "`accessors' does not contain an JSON object.";
  24746. }
  24747. return false;
  24748. }
  24749. Accessor accessor;
  24750. if (!ParseAccessor(&accessor, err, o,
  24751. store_original_json_for_extras_and_extensions_))
  24752. {
  24753. return false;
  24754. }
  24755. model->accessors.emplace_back(std::move(accessor));
  24756. return true;
  24757. });
  24758. if (!success)
  24759. {
  24760. return false;
  24761. }
  24762. }
  24763. // 6. Parse Mesh
  24764. {
  24765. bool success = ForEachInArray(v, "meshes", [&](const json &o)
  24766. {
  24767. if (!IsObject(o))
  24768. {
  24769. if (err)
  24770. {
  24771. (*err) += "`meshes' does not contain an JSON object.";
  24772. }
  24773. return false;
  24774. }
  24775. Mesh mesh;
  24776. if (!ParseMesh(&mesh, model, err, o,
  24777. store_original_json_for_extras_and_extensions_))
  24778. {
  24779. return false;
  24780. }
  24781. model->meshes.emplace_back(std::move(mesh));
  24782. return true;
  24783. });
  24784. if (!success)
  24785. {
  24786. return false;
  24787. }
  24788. }
  24789. // Assign missing bufferView target types
  24790. // - Look for missing Mesh indices
  24791. // - Look for missing Mesh attributes
  24792. for (auto &mesh : model->meshes)
  24793. {
  24794. for (auto &primitive : mesh.primitives)
  24795. {
  24796. if (primitive.indices >
  24797. -1) // has indices from parsing step, must be Element Array Buffer
  24798. {
  24799. if (size_t(primitive.indices) >= model->accessors.size())
  24800. {
  24801. if (err)
  24802. {
  24803. (*err) += "primitive indices accessor out of bounds";
  24804. }
  24805. return false;
  24806. }
  24807. auto bufferView =
  24808. model->accessors[size_t(primitive.indices)].bufferView;
  24809. if (bufferView < 0 || size_t(bufferView) >= model->bufferViews.size())
  24810. {
  24811. if (err)
  24812. {
  24813. (*err) += "accessor[" + std::to_string(primitive.indices) +
  24814. "] invalid bufferView";
  24815. }
  24816. return false;
  24817. }
  24818. model->bufferViews[size_t(bufferView)].target =
  24819. TINYGLTF_TARGET_ELEMENT_ARRAY_BUFFER;
  24820. // we could optionally check if acessors' bufferView type is Scalar, as
  24821. // it should be
  24822. }
  24823. for (auto &attribute : primitive.attributes)
  24824. {
  24825. model
  24826. ->bufferViews[size_t(
  24827. model->accessors[size_t(attribute.second)].bufferView)]
  24828. .target = TINYGLTF_TARGET_ARRAY_BUFFER;
  24829. }
  24830. for (auto &target : primitive.targets)
  24831. {
  24832. for (auto &attribute : target)
  24833. {
  24834. auto bufferView =
  24835. model->accessors[size_t(attribute.second)].bufferView;
  24836. // bufferView could be null(-1) for sparse morph target
  24837. if (bufferView >= 0)
  24838. {
  24839. model->bufferViews[size_t(bufferView)].target =
  24840. TINYGLTF_TARGET_ARRAY_BUFFER;
  24841. }
  24842. }
  24843. }
  24844. }
  24845. }
  24846. // 7. Parse Node
  24847. {
  24848. bool success = ForEachInArray(v, "nodes", [&](const json &o)
  24849. {
  24850. if (!IsObject(o))
  24851. {
  24852. if (err)
  24853. {
  24854. (*err) += "`nodes' does not contain an JSON object.";
  24855. }
  24856. return false;
  24857. }
  24858. Node node;
  24859. if (!ParseNode(&node, err, o,
  24860. store_original_json_for_extras_and_extensions_))
  24861. {
  24862. return false;
  24863. }
  24864. model->nodes.emplace_back(std::move(node));
  24865. return true;
  24866. });
  24867. if (!success)
  24868. {
  24869. return false;
  24870. }
  24871. }
  24872. // 8. Parse scenes.
  24873. {
  24874. bool success = ForEachInArray(v, "scenes", [&](const json &o)
  24875. {
  24876. if (!IsObject(o))
  24877. {
  24878. if (err)
  24879. {
  24880. (*err) += "`scenes' does not contain an JSON object.";
  24881. }
  24882. return false;
  24883. }
  24884. std::vector<int> nodes;
  24885. ParseIntegerArrayProperty(&nodes, err, o, "nodes", false);
  24886. Scene scene;
  24887. scene.nodes = std::move(nodes);
  24888. ParseStringProperty(&scene.name, err, o, "name", false);
  24889. ParseExtensionsProperty(&scene.extensions, err, o);
  24890. ParseExtrasProperty(&scene.extras, o);
  24891. if (store_original_json_for_extras_and_extensions_)
  24892. {
  24893. {
  24894. json_const_iterator it;
  24895. if (FindMember(o, "extensions", it))
  24896. {
  24897. model->extensions_json_string = JsonToString(GetValue(it));
  24898. }
  24899. }
  24900. {
  24901. json_const_iterator it;
  24902. if (FindMember(o, "extras", it))
  24903. {
  24904. model->extras_json_string = JsonToString(GetValue(it));
  24905. }
  24906. }
  24907. }
  24908. model->scenes.emplace_back(std::move(scene));
  24909. return true;
  24910. });
  24911. if (!success)
  24912. {
  24913. return false;
  24914. }
  24915. }
  24916. // 9. Parse default scenes.
  24917. {
  24918. json_const_iterator rootIt;
  24919. int iVal;
  24920. if (FindMember(v, "scene", rootIt) && GetInt(GetValue(rootIt), iVal))
  24921. {
  24922. model->defaultScene = iVal;
  24923. }
  24924. }
  24925. // 10. Parse Material
  24926. {
  24927. bool success = ForEachInArray(v, "materials", [&](const json &o)
  24928. {
  24929. if (!IsObject(o))
  24930. {
  24931. if (err)
  24932. {
  24933. (*err) += "`materials' does not contain an JSON object.";
  24934. }
  24935. return false;
  24936. }
  24937. Material material;
  24938. ParseStringProperty(&material.name, err, o, "name", false);
  24939. if (!ParseMaterial(&material, err, o,
  24940. store_original_json_for_extras_and_extensions_))
  24941. {
  24942. return false;
  24943. }
  24944. model->materials.emplace_back(std::move(material));
  24945. return true;
  24946. });
  24947. if (!success)
  24948. {
  24949. return false;
  24950. }
  24951. }
  24952. // 11. Parse Image
  24953. void *load_image_user_data{ nullptr };
  24954. LoadImageDataOption load_image_option;
  24955. if (user_image_loader_)
  24956. {
  24957. // Use user supplied pointer
  24958. load_image_user_data = load_image_user_data_;
  24959. }
  24960. else
  24961. {
  24962. load_image_option.preserve_channels = preserve_image_channels_;
  24963. load_image_user_data = reinterpret_cast<void *>(&load_image_option);
  24964. }
  24965. {
  24966. int idx = 0;
  24967. bool success = ForEachInArray(v, "images", [&](const json &o)
  24968. {
  24969. if (!IsObject(o))
  24970. {
  24971. if (err)
  24972. {
  24973. (*err) += "image[" + std::to_string(idx) + "] is not a JSON object.";
  24974. }
  24975. return false;
  24976. }
  24977. Image image;
  24978. if (!ParseImage(&image, idx, err, warn, o,
  24979. store_original_json_for_extras_and_extensions_, base_dir,
  24980. &fs, &this->LoadImageData, load_image_user_data))
  24981. {
  24982. return false;
  24983. }
  24984. if (image.bufferView != -1)
  24985. {
  24986. // Load image from the buffer view.
  24987. if (size_t(image.bufferView) >= model->bufferViews.size())
  24988. {
  24989. if (err)
  24990. {
  24991. std::stringstream ss;
  24992. ss << "image[" << idx << "] bufferView \"" << image.bufferView
  24993. << "\" not found in the scene." << std::endl;
  24994. (*err) += ss.str();
  24995. }
  24996. return false;
  24997. }
  24998. const BufferView &bufferView =
  24999. model->bufferViews[size_t(image.bufferView)];
  25000. if (size_t(bufferView.buffer) >= model->buffers.size())
  25001. {
  25002. if (err)
  25003. {
  25004. std::stringstream ss;
  25005. ss << "image[" << idx << "] buffer \"" << bufferView.buffer
  25006. << "\" not found in the scene." << std::endl;
  25007. (*err) += ss.str();
  25008. }
  25009. return false;
  25010. }
  25011. const Buffer &buffer = model->buffers[size_t(bufferView.buffer)];
  25012. if (*LoadImageData == nullptr)
  25013. {
  25014. if (err)
  25015. {
  25016. (*err) += "No LoadImageData callback specified.\n";
  25017. }
  25018. return false;
  25019. }
  25020. bool ret = LoadImageData(
  25021. &image, idx, err, warn, image.width, image.height,
  25022. &buffer.data[bufferView.byteOffset],
  25023. static_cast<int>(bufferView.byteLength), load_image_user_data);
  25024. if (!ret)
  25025. {
  25026. return false;
  25027. }
  25028. }
  25029. model->images.emplace_back(std::move(image));
  25030. ++idx;
  25031. return true;
  25032. });
  25033. if (!success)
  25034. {
  25035. return false;
  25036. }
  25037. }
  25038. // 12. Parse Texture
  25039. {
  25040. bool success = ForEachInArray(v, "textures", [&](const json &o)
  25041. {
  25042. if (!IsObject(o))
  25043. {
  25044. if (err)
  25045. {
  25046. (*err) += "`textures' does not contain an JSON object.";
  25047. }
  25048. return false;
  25049. }
  25050. Texture texture;
  25051. if (!ParseTexture(&texture, err, o,
  25052. store_original_json_for_extras_and_extensions_,
  25053. base_dir))
  25054. {
  25055. return false;
  25056. }
  25057. model->textures.emplace_back(std::move(texture));
  25058. return true;
  25059. });
  25060. if (!success)
  25061. {
  25062. return false;
  25063. }
  25064. }
  25065. // 13. Parse Animation
  25066. {
  25067. bool success = ForEachInArray(v, "animations", [&](const json &o)
  25068. {
  25069. if (!IsObject(o))
  25070. {
  25071. if (err)
  25072. {
  25073. (*err) += "`animations' does not contain an JSON object.";
  25074. }
  25075. return false;
  25076. }
  25077. Animation animation;
  25078. if (!ParseAnimation(&animation, err, o,
  25079. store_original_json_for_extras_and_extensions_))
  25080. {
  25081. return false;
  25082. }
  25083. model->animations.emplace_back(std::move(animation));
  25084. return true;
  25085. });
  25086. if (!success)
  25087. {
  25088. return false;
  25089. }
  25090. }
  25091. // 14. Parse Skin
  25092. {
  25093. bool success = ForEachInArray(v, "skins", [&](const json &o)
  25094. {
  25095. if (!IsObject(o))
  25096. {
  25097. if (err)
  25098. {
  25099. (*err) += "`skins' does not contain an JSON object.";
  25100. }
  25101. return false;
  25102. }
  25103. Skin skin;
  25104. if (!ParseSkin(&skin, err, o,
  25105. store_original_json_for_extras_and_extensions_))
  25106. {
  25107. return false;
  25108. }
  25109. model->skins.emplace_back(std::move(skin));
  25110. return true;
  25111. });
  25112. if (!success)
  25113. {
  25114. return false;
  25115. }
  25116. }
  25117. // 15. Parse Sampler
  25118. {
  25119. bool success = ForEachInArray(v, "samplers", [&](const json &o)
  25120. {
  25121. if (!IsObject(o))
  25122. {
  25123. if (err)
  25124. {
  25125. (*err) += "`samplers' does not contain an JSON object.";
  25126. }
  25127. return false;
  25128. }
  25129. Sampler sampler;
  25130. if (!ParseSampler(&sampler, err, o,
  25131. store_original_json_for_extras_and_extensions_))
  25132. {
  25133. return false;
  25134. }
  25135. model->samplers.emplace_back(std::move(sampler));
  25136. return true;
  25137. });
  25138. if (!success)
  25139. {
  25140. return false;
  25141. }
  25142. }
  25143. // 16. Parse Camera
  25144. {
  25145. bool success = ForEachInArray(v, "cameras", [&](const json &o)
  25146. {
  25147. if (!IsObject(o))
  25148. {
  25149. if (err)
  25150. {
  25151. (*err) += "`cameras' does not contain an JSON object.";
  25152. }
  25153. return false;
  25154. }
  25155. Camera camera;
  25156. if (!ParseCamera(&camera, err, o,
  25157. store_original_json_for_extras_and_extensions_))
  25158. {
  25159. return false;
  25160. }
  25161. model->cameras.emplace_back(std::move(camera));
  25162. return true;
  25163. });
  25164. if (!success)
  25165. {
  25166. return false;
  25167. }
  25168. }
  25169. // 17. Parse Extensions
  25170. ParseExtensionsProperty(&model->extensions, err, v);
  25171. // 18. Specific extension implementations
  25172. {
  25173. json_const_iterator rootIt;
  25174. if (FindMember(v, "extensions", rootIt) && IsObject(GetValue(rootIt)))
  25175. {
  25176. const json &root = GetValue(rootIt);
  25177. json_const_iterator it(ObjectBegin(root));
  25178. json_const_iterator itEnd(ObjectEnd(root));
  25179. for (; it != itEnd; ++it)
  25180. {
  25181. // parse KHR_lights_punctual extension
  25182. std::string key(GetKey(it));
  25183. if ((key == "KHR_lights_punctual") && IsObject(GetValue(it)))
  25184. {
  25185. const json &object = GetValue(it);
  25186. json_const_iterator itLight;
  25187. if (FindMember(object, "lights", itLight))
  25188. {
  25189. const json &lights = GetValue(itLight);
  25190. if (!IsArray(lights))
  25191. {
  25192. continue;
  25193. }
  25194. auto arrayIt(ArrayBegin(lights));
  25195. auto arrayItEnd(ArrayEnd(lights));
  25196. for (; arrayIt != arrayItEnd; ++arrayIt)
  25197. {
  25198. Light light;
  25199. if (!ParseLight(&light, err, *arrayIt,
  25200. store_original_json_for_extras_and_extensions_))
  25201. {
  25202. return false;
  25203. }
  25204. model->lights.emplace_back(std::move(light));
  25205. }
  25206. }
  25207. }
  25208. }
  25209. }
  25210. }
  25211. // 19. Parse Extras
  25212. ParseExtrasProperty(&model->extras, v);
  25213. if (store_original_json_for_extras_and_extensions_)
  25214. {
  25215. model->extras_json_string = JsonToString(v["extras"]);
  25216. model->extensions_json_string = JsonToString(v["extensions"]);
  25217. }
  25218. return true;
  25219. }
  25220. bool TinyGLTF::LoadASCIIFromString(Model *model, std::string *err,
  25221. std::string *warn, const char *str,
  25222. unsigned int length,
  25223. const std::string &base_dir,
  25224. unsigned int check_sections)
  25225. {
  25226. is_binary_ = false;
  25227. bin_data_ = nullptr;
  25228. bin_size_ = 0;
  25229. return LoadFromString(model, err, warn, str, length, base_dir,
  25230. check_sections);
  25231. }
  25232. bool TinyGLTF::LoadASCIIFromFile(Model *model, std::string *err,
  25233. std::string *warn, const std::string &filename,
  25234. unsigned int check_sections)
  25235. {
  25236. std::stringstream ss;
  25237. if (fs.ReadWholeFile == nullptr)
  25238. {
  25239. // Programmer error, assert() ?
  25240. ss << "Failed to read file: " << filename
  25241. << ": one or more FS callback not set" << std::endl;
  25242. if (err)
  25243. {
  25244. (*err) = ss.str();
  25245. }
  25246. return false;
  25247. }
  25248. std::vector<unsigned char> data;
  25249. std::string fileerr;
  25250. bool fileread = fs.ReadWholeFile(&data, &fileerr, filename, fs.user_data);
  25251. if (!fileread)
  25252. {
  25253. ss << "Failed to read file: " << filename << ": " << fileerr << std::endl;
  25254. if (err)
  25255. {
  25256. (*err) = ss.str();
  25257. }
  25258. return false;
  25259. }
  25260. size_t sz = data.size();
  25261. if (sz == 0)
  25262. {
  25263. if (err)
  25264. {
  25265. (*err) = "Empty file.";
  25266. }
  25267. return false;
  25268. }
  25269. std::string basedir = GetBaseDir(filename);
  25270. bool ret = LoadASCIIFromString(
  25271. model, err, warn, reinterpret_cast<const char *>(&data.at(0)),
  25272. static_cast<unsigned int>(data.size()), basedir, check_sections);
  25273. return ret;
  25274. }
  25275. bool TinyGLTF::LoadBinaryFromMemory(Model *model, std::string *err,
  25276. std::string *warn,
  25277. const unsigned char *bytes,
  25278. unsigned int size,
  25279. const std::string &base_dir,
  25280. unsigned int check_sections)
  25281. {
  25282. if (size < 20)
  25283. {
  25284. if (err)
  25285. {
  25286. (*err) = "Too short data size for glTF Binary.";
  25287. }
  25288. return false;
  25289. }
  25290. if (bytes[0] == 'g' && bytes[1] == 'l' && bytes[2] == 'T' &&
  25291. bytes[3] == 'F')
  25292. {
  25293. // ok
  25294. }
  25295. else
  25296. {
  25297. if (err)
  25298. {
  25299. (*err) = "Invalid magic.";
  25300. }
  25301. return false;
  25302. }
  25303. unsigned int version; // 4 bytes
  25304. unsigned int length; // 4 bytes
  25305. unsigned int model_length; // 4 bytes
  25306. unsigned int model_format; // 4 bytes;
  25307. // @todo { Endian swap for big endian machine. }
  25308. memcpy(&version, bytes + 4, 4);
  25309. swap4(&version);
  25310. memcpy(&length, bytes + 8, 4);
  25311. swap4(&length);
  25312. memcpy(&model_length, bytes + 12, 4);
  25313. swap4(&model_length);
  25314. memcpy(&model_format, bytes + 16, 4);
  25315. swap4(&model_format);
  25316. // In case the Bin buffer is not present, the size is exactly 20 + size of
  25317. // JSON contents,
  25318. // so use "greater than" operator.
  25319. if ((20 + model_length > size) || (model_length < 1) || (length > size) ||
  25320. (20 + model_length > length) ||
  25321. (model_format != 0x4E4F534A))
  25322. { // 0x4E4F534A = JSON format.
  25323. if (err)
  25324. {
  25325. (*err) = "Invalid glTF binary.";
  25326. }
  25327. return false;
  25328. }
  25329. // Extract JSON string.
  25330. std::string jsonString(reinterpret_cast<const char *>(&bytes[20]),
  25331. model_length);
  25332. is_binary_ = true;
  25333. bin_data_ = bytes + 20 + model_length +
  25334. 8; // 4 bytes (buffer_length) + 4 bytes(buffer_format)
  25335. bin_size_ =
  25336. length - (20 + model_length); // extract header + JSON scene data.
  25337. bool ret = LoadFromString(model, err, warn,
  25338. reinterpret_cast<const char *>(&bytes[20]),
  25339. model_length, base_dir, check_sections);
  25340. if (!ret)
  25341. {
  25342. return ret;
  25343. }
  25344. return true;
  25345. }
  25346. bool TinyGLTF::LoadBinaryFromFile(Model *model, std::string *err,
  25347. std::string *warn,
  25348. const std::string &filename,
  25349. unsigned int check_sections)
  25350. {
  25351. std::stringstream ss;
  25352. if (fs.ReadWholeFile == nullptr)
  25353. {
  25354. // Programmer error, assert() ?
  25355. ss << "Failed to read file: " << filename
  25356. << ": one or more FS callback not set" << std::endl;
  25357. if (err)
  25358. {
  25359. (*err) = ss.str();
  25360. }
  25361. return false;
  25362. }
  25363. std::vector<unsigned char> data;
  25364. std::string fileerr;
  25365. bool fileread = fs.ReadWholeFile(&data, &fileerr, filename, fs.user_data);
  25366. if (!fileread)
  25367. {
  25368. ss << "Failed to read file: " << filename << ": " << fileerr << std::endl;
  25369. if (err)
  25370. {
  25371. (*err) = ss.str();
  25372. }
  25373. return false;
  25374. }
  25375. std::string basedir = GetBaseDir(filename);
  25376. bool ret = LoadBinaryFromMemory(model, err, warn, &data.at(0),
  25377. static_cast<unsigned int>(data.size()),
  25378. basedir, check_sections);
  25379. return ret;
  25380. }
  25381. ///////////////////////
  25382. // GLTF Serialization
  25383. ///////////////////////
  25384. namespace
  25385. {
  25386. json JsonFromString(const char *s)
  25387. {
  25388. #ifdef TINYGLTF_USE_RAPIDJSON
  25389. return json(s, GetAllocator());
  25390. #else
  25391. return json(s);
  25392. #endif
  25393. }
  25394. void JsonAssign(json &dest, const json &src)
  25395. {
  25396. #ifdef TINYGLTF_USE_RAPIDJSON
  25397. dest.CopyFrom(src, GetAllocator());
  25398. #else
  25399. dest = src;
  25400. #endif
  25401. }
  25402. void JsonAddMember(json &o, const char *key, json &&value)
  25403. {
  25404. #ifdef TINYGLTF_USE_RAPIDJSON
  25405. if (!o.IsObject())
  25406. {
  25407. o.SetObject();
  25408. }
  25409. o.AddMember(json(key, GetAllocator()), std::move(value), GetAllocator());
  25410. #else
  25411. o[key] = std::move(value);
  25412. #endif
  25413. }
  25414. void JsonPushBack(json &o, json &&value)
  25415. {
  25416. #ifdef TINYGLTF_USE_RAPIDJSON
  25417. o.PushBack(std::move(value), GetAllocator());
  25418. #else
  25419. o.push_back(std::move(value));
  25420. #endif
  25421. }
  25422. bool JsonIsNull(const json &o)
  25423. {
  25424. #ifdef TINYGLTF_USE_RAPIDJSON
  25425. return o.IsNull();
  25426. #else
  25427. return o.is_null();
  25428. #endif
  25429. }
  25430. void JsonSetObject(json &o)
  25431. {
  25432. #ifdef TINYGLTF_USE_RAPIDJSON
  25433. o.SetObject();
  25434. #else
  25435. o = o.object({});
  25436. #endif
  25437. }
  25438. void JsonReserveArray(json &o, size_t s)
  25439. {
  25440. #ifdef TINYGLTF_USE_RAPIDJSON
  25441. o.SetArray();
  25442. o.Reserve(static_cast<rapidjson::SizeType>(s), GetAllocator());
  25443. #endif
  25444. (void)(o);
  25445. (void)(s);
  25446. }
  25447. } // namespace
  25448. // typedef std::pair<std::string, json> json_object_pair;
  25449. template <typename T>
  25450. static void SerializeNumberProperty(const std::string &key, T number,
  25451. json &obj)
  25452. {
  25453. // obj.insert(
  25454. // json_object_pair(key, json(static_cast<double>(number))));
  25455. // obj[key] = static_cast<double>(number);
  25456. JsonAddMember(obj, key.c_str(), json(number));
  25457. }
  25458. #ifdef TINYGLTF_USE_RAPIDJSON
  25459. template <>
  25460. void SerializeNumberProperty(const std::string &key, size_t number, json &obj)
  25461. {
  25462. JsonAddMember(obj, key.c_str(), json(static_cast<uint64_t>(number)));
  25463. }
  25464. #endif
  25465. template <typename T>
  25466. static void SerializeNumberArrayProperty(const std::string &key,
  25467. const std::vector<T> &value,
  25468. json &obj)
  25469. {
  25470. if (value.empty()) return;
  25471. json ary;
  25472. JsonReserveArray(ary, value.size());
  25473. for (const auto &s : value)
  25474. {
  25475. JsonPushBack(ary, json(s));
  25476. }
  25477. JsonAddMember(obj, key.c_str(), std::move(ary));
  25478. }
  25479. static void SerializeStringProperty(const std::string &key,
  25480. const std::string &value, json &obj)
  25481. {
  25482. JsonAddMember(obj, key.c_str(), JsonFromString(value.c_str()));
  25483. }
  25484. static void SerializeStringArrayProperty(const std::string &key,
  25485. const std::vector<std::string> &value,
  25486. json &obj)
  25487. {
  25488. json ary;
  25489. JsonReserveArray(ary, value.size());
  25490. for (auto &s : value)
  25491. {
  25492. JsonPushBack(ary, JsonFromString(s.c_str()));
  25493. }
  25494. JsonAddMember(obj, key.c_str(), std::move(ary));
  25495. }
  25496. static bool ValueToJson(const Value &value, json *ret)
  25497. {
  25498. json obj;
  25499. #ifdef TINYGLTF_USE_RAPIDJSON
  25500. switch (value.Type())
  25501. {
  25502. case REAL_TYPE:
  25503. obj.SetDouble(value.Get<double>());
  25504. break;
  25505. case INT_TYPE:
  25506. obj.SetInt(value.Get<int>());
  25507. break;
  25508. case BOOL_TYPE:
  25509. obj.SetBool(value.Get<bool>());
  25510. break;
  25511. case STRING_TYPE:
  25512. obj.SetString(value.Get<std::string>().c_str(), GetAllocator());
  25513. break;
  25514. case ARRAY_TYPE:
  25515. {
  25516. obj.SetArray();
  25517. obj.Reserve(static_cast<rapidjson::SizeType>(value.ArrayLen()),
  25518. GetAllocator());
  25519. for (unsigned int i = 0; i < value.ArrayLen(); ++i)
  25520. {
  25521. Value elementValue = value.Get(int(i));
  25522. json elementJson;
  25523. if (ValueToJson(value.Get(int(i)), &elementJson))
  25524. obj.PushBack(std::move(elementJson), GetAllocator());
  25525. }
  25526. break;
  25527. }
  25528. case BINARY_TYPE:
  25529. // TODO
  25530. // obj = json(value.Get<std::vector<unsigned char>>());
  25531. return false;
  25532. break;
  25533. case OBJECT_TYPE:
  25534. {
  25535. obj.SetObject();
  25536. Value::Object objMap = value.Get<Value::Object>();
  25537. for (auto &it : objMap)
  25538. {
  25539. json elementJson;
  25540. if (ValueToJson(it.second, &elementJson))
  25541. {
  25542. obj.AddMember(json(it.first.c_str(), GetAllocator()),
  25543. std::move(elementJson), GetAllocator());
  25544. }
  25545. }
  25546. break;
  25547. }
  25548. case NULL_TYPE:
  25549. default:
  25550. return false;
  25551. }
  25552. #else
  25553. switch (value.Type())
  25554. {
  25555. case REAL_TYPE:
  25556. obj = json(value.Get<double>());
  25557. break;
  25558. case INT_TYPE:
  25559. obj = json(value.Get<int>());
  25560. break;
  25561. case BOOL_TYPE:
  25562. obj = json(value.Get<bool>());
  25563. break;
  25564. case STRING_TYPE:
  25565. obj = json(value.Get<std::string>());
  25566. break;
  25567. case ARRAY_TYPE:
  25568. {
  25569. for (unsigned int i = 0; i < value.ArrayLen(); ++i)
  25570. {
  25571. Value elementValue = value.Get(int(i));
  25572. json elementJson;
  25573. if (ValueToJson(value.Get(int(i)), &elementJson))
  25574. obj.push_back(elementJson);
  25575. }
  25576. break;
  25577. }
  25578. case BINARY_TYPE:
  25579. // TODO
  25580. // obj = json(value.Get<std::vector<unsigned char>>());
  25581. return false;
  25582. break;
  25583. case OBJECT_TYPE:
  25584. {
  25585. Value::Object objMap = value.Get<Value::Object>();
  25586. for (auto &it : objMap)
  25587. {
  25588. json elementJson;
  25589. if (ValueToJson(it.second, &elementJson)) obj[it.first] = elementJson;
  25590. }
  25591. break;
  25592. }
  25593. case NULL_TYPE:
  25594. default:
  25595. return false;
  25596. }
  25597. #endif
  25598. if (ret) *ret = std::move(obj);
  25599. return true;
  25600. }
  25601. static void SerializeValue(const std::string &key, const Value &value,
  25602. json &obj)
  25603. {
  25604. json ret;
  25605. if (ValueToJson(value, &ret))
  25606. {
  25607. JsonAddMember(obj, key.c_str(), std::move(ret));
  25608. }
  25609. }
  25610. static void SerializeGltfBufferData(const std::vector<unsigned char> &data,
  25611. json &o)
  25612. {
  25613. std::string header = "data:application/octet-stream;base64,";
  25614. if (data.size() > 0)
  25615. {
  25616. std::string encodedData =
  25617. base64_encode(&data[0], static_cast<unsigned int>(data.size()));
  25618. SerializeStringProperty("uri", header + encodedData, o);
  25619. }
  25620. else
  25621. {
  25622. // Issue #229
  25623. // size 0 is allowd. Just emit mime header.
  25624. SerializeStringProperty("uri", header, o);
  25625. }
  25626. }
  25627. //removed writing data
  25628. static bool SerializeGltfBufferData(const std::vector<unsigned char> &data,
  25629. const std::string &binFilename)
  25630. {
  25631. assert(0);
  25632. /*
  25633. #ifdef _WIN32
  25634. #if defined(__GLIBCXX__) // mingw
  25635. int file_descriptor = _wopen(UTF8ToWchar(binFilename).c_str(),
  25636. _O_CREAT | _O_WRONLY | _O_TRUNC | _O_BINARY);
  25637. __gnu_cxx::stdio_filebuf<char> wfile_buf(
  25638. file_descriptor, std::ios_base::out | std::ios_base::binary);
  25639. std::ostream output(&wfile_buf);
  25640. if (!wfile_buf.is_open()) return false;
  25641. #elif defined(_MSC_VER)
  25642. std::ofstream output(UTF8ToWchar(binFilename).c_str(), std::ofstream::binary);
  25643. if (!output.is_open()) return false;
  25644. #else
  25645. std::ofstream output(binFilename.c_str(), std::ofstream::binary);
  25646. if (!output.is_open()) return false;
  25647. #endif
  25648. #else
  25649. std::ofstream output(binFilename.c_str(), std::ofstream::binary);
  25650. if (!output.is_open()) return false;
  25651. #endif
  25652. if (data.size() > 0)
  25653. {
  25654. output.write(reinterpret_cast<const char *>(&data[0]),
  25655. std::streamsize(data.size()));
  25656. }
  25657. else
  25658. {
  25659. // Issue #229
  25660. // size 0 will be still valid buffer data.
  25661. // write empty file.
  25662. }
  25663. */
  25664. return true;
  25665. }
  25666. #if 0 // FIXME(syoyo): not used. will be removed in the future release.
  25667. static void SerializeParameterMap(ParameterMap &param, json &o)
  25668. {
  25669. for (ParameterMap::iterator paramIt = param.begin(); paramIt != param.end();
  25670. ++paramIt)
  25671. {
  25672. if (paramIt->second.number_array.size())
  25673. {
  25674. SerializeNumberArrayProperty<double>(paramIt->first,
  25675. paramIt->second.number_array, o);
  25676. }
  25677. else if (paramIt->second.json_double_value.size())
  25678. {
  25679. json json_double_value;
  25680. for (std::map<std::string, double>::iterator it =
  25681. paramIt->second.json_double_value.begin();
  25682. it != paramIt->second.json_double_value.end(); ++it)
  25683. {
  25684. if (it->first == "index")
  25685. {
  25686. json_double_value[it->first] = paramIt->second.TextureIndex();
  25687. }
  25688. else
  25689. {
  25690. json_double_value[it->first] = it->second;
  25691. }
  25692. }
  25693. o[paramIt->first] = json_double_value;
  25694. }
  25695. else if (!paramIt->second.string_value.empty())
  25696. {
  25697. SerializeStringProperty(paramIt->first, paramIt->second.string_value, o);
  25698. }
  25699. else if (paramIt->second.has_number_value)
  25700. {
  25701. o[paramIt->first] = paramIt->second.number_value;
  25702. }
  25703. else
  25704. {
  25705. o[paramIt->first] = paramIt->second.bool_value;
  25706. }
  25707. }
  25708. }
  25709. #endif
  25710. static void SerializeExtensionMap(const ExtensionMap &extensions, json &o)
  25711. {
  25712. if (!extensions.size()) return;
  25713. json extMap;
  25714. for (ExtensionMap::const_iterator extIt = extensions.begin();
  25715. extIt != extensions.end(); ++extIt)
  25716. {
  25717. // Allow an empty object for extension(#97)
  25718. json ret;
  25719. bool isNull = true;
  25720. if (ValueToJson(extIt->second, &ret))
  25721. {
  25722. isNull = JsonIsNull(ret);
  25723. JsonAddMember(extMap, extIt->first.c_str(), std::move(ret));
  25724. }
  25725. if (isNull)
  25726. {
  25727. if (!(extIt->first.empty()))
  25728. { // name should not be empty, but for sure
  25729. // create empty object so that an extension name is still included in
  25730. // json.
  25731. json empty;
  25732. JsonSetObject(empty);
  25733. JsonAddMember(extMap, extIt->first.c_str(), std::move(empty));
  25734. }
  25735. }
  25736. }
  25737. JsonAddMember(o, "extensions", std::move(extMap));
  25738. }
  25739. static void SerializeGltfAccessor(Accessor &accessor, json &o)
  25740. {
  25741. if (accessor.bufferView >= 0)
  25742. SerializeNumberProperty<int>("bufferView", accessor.bufferView, o);
  25743. if (accessor.byteOffset != 0)
  25744. SerializeNumberProperty<int>("byteOffset", int(accessor.byteOffset), o);
  25745. SerializeNumberProperty<int>("componentType", accessor.componentType, o);
  25746. SerializeNumberProperty<size_t>("count", accessor.count, o);
  25747. SerializeNumberArrayProperty<double>("min", accessor.minValues, o);
  25748. SerializeNumberArrayProperty<double>("max", accessor.maxValues, o);
  25749. if (accessor.normalized)
  25750. SerializeValue("normalized", Value(accessor.normalized), o);
  25751. std::string type;
  25752. switch (accessor.type)
  25753. {
  25754. case TINYGLTF_TYPE_SCALAR:
  25755. type = "SCALAR";
  25756. break;
  25757. case TINYGLTF_TYPE_VEC2:
  25758. type = "VEC2";
  25759. break;
  25760. case TINYGLTF_TYPE_VEC3:
  25761. type = "VEC3";
  25762. break;
  25763. case TINYGLTF_TYPE_VEC4:
  25764. type = "VEC4";
  25765. break;
  25766. case TINYGLTF_TYPE_MAT2:
  25767. type = "MAT2";
  25768. break;
  25769. case TINYGLTF_TYPE_MAT3:
  25770. type = "MAT3";
  25771. break;
  25772. case TINYGLTF_TYPE_MAT4:
  25773. type = "MAT4";
  25774. break;
  25775. }
  25776. SerializeStringProperty("type", type, o);
  25777. if (!accessor.name.empty()) SerializeStringProperty("name", accessor.name, o);
  25778. if (accessor.extras.Type() != NULL_TYPE)
  25779. {
  25780. SerializeValue("extras", accessor.extras, o);
  25781. }
  25782. }
  25783. static void SerializeGltfAnimationChannel(AnimationChannel &channel, json &o)
  25784. {
  25785. SerializeNumberProperty("sampler", channel.sampler, o);
  25786. {
  25787. json target;
  25788. SerializeNumberProperty("node", channel.target_node, target);
  25789. SerializeStringProperty("path", channel.target_path, target);
  25790. SerializeExtensionMap(channel.target_extensions, target);
  25791. JsonAddMember(o, "target", std::move(target));
  25792. }
  25793. if (channel.extras.Type() != NULL_TYPE)
  25794. {
  25795. SerializeValue("extras", channel.extras, o);
  25796. }
  25797. SerializeExtensionMap(channel.extensions, o);
  25798. }
  25799. static void SerializeGltfAnimationSampler(AnimationSampler &sampler, json &o)
  25800. {
  25801. SerializeNumberProperty("input", sampler.input, o);
  25802. SerializeNumberProperty("output", sampler.output, o);
  25803. SerializeStringProperty("interpolation", sampler.interpolation, o);
  25804. if (sampler.extras.Type() != NULL_TYPE)
  25805. {
  25806. SerializeValue("extras", sampler.extras, o);
  25807. }
  25808. }
  25809. static void SerializeGltfAnimation(Animation &animation, json &o)
  25810. {
  25811. if (!animation.name.empty())
  25812. SerializeStringProperty("name", animation.name, o);
  25813. {
  25814. json channels;
  25815. JsonReserveArray(channels, animation.channels.size());
  25816. for (unsigned int i = 0; i < animation.channels.size(); ++i)
  25817. {
  25818. json channel;
  25819. AnimationChannel gltfChannel = animation.channels[i];
  25820. SerializeGltfAnimationChannel(gltfChannel, channel);
  25821. JsonPushBack(channels, std::move(channel));
  25822. }
  25823. JsonAddMember(o, "channels", std::move(channels));
  25824. }
  25825. {
  25826. json samplers;
  25827. JsonReserveArray(samplers, animation.samplers.size());
  25828. for (unsigned int i = 0; i < animation.samplers.size(); ++i)
  25829. {
  25830. json sampler;
  25831. AnimationSampler gltfSampler = animation.samplers[i];
  25832. SerializeGltfAnimationSampler(gltfSampler, sampler);
  25833. JsonPushBack(samplers, std::move(sampler));
  25834. }
  25835. JsonAddMember(o, "samplers", std::move(samplers));
  25836. }
  25837. if (animation.extras.Type() != NULL_TYPE)
  25838. {
  25839. SerializeValue("extras", animation.extras, o);
  25840. }
  25841. SerializeExtensionMap(animation.extensions, o);
  25842. }
  25843. static void SerializeGltfAsset(Asset &asset, json &o)
  25844. {
  25845. if (!asset.generator.empty())
  25846. {
  25847. SerializeStringProperty("generator", asset.generator, o);
  25848. }
  25849. if (!asset.copyright.empty())
  25850. {
  25851. SerializeStringProperty("copyright", asset.copyright, o);
  25852. }
  25853. if (!asset.version.empty())
  25854. {
  25855. SerializeStringProperty("version", asset.version, o);
  25856. }
  25857. if (asset.extras.Keys().size())
  25858. {
  25859. SerializeValue("extras", asset.extras, o);
  25860. }
  25861. SerializeExtensionMap(asset.extensions, o);
  25862. }
  25863. static void SerializeGltfBufferBin(Buffer &buffer, json &o,
  25864. std::vector<unsigned char> &binBuffer)
  25865. {
  25866. SerializeNumberProperty("byteLength", buffer.data.size(), o);
  25867. binBuffer = buffer.data;
  25868. if (buffer.name.size()) SerializeStringProperty("name", buffer.name, o);
  25869. if (buffer.extras.Type() != NULL_TYPE)
  25870. {
  25871. SerializeValue("extras", buffer.extras, o);
  25872. }
  25873. }
  25874. static void SerializeGltfBuffer(Buffer &buffer, json &o)
  25875. {
  25876. SerializeNumberProperty("byteLength", buffer.data.size(), o);
  25877. SerializeGltfBufferData(buffer.data, o);
  25878. if (buffer.name.size()) SerializeStringProperty("name", buffer.name, o);
  25879. if (buffer.extras.Type() != NULL_TYPE)
  25880. {
  25881. SerializeValue("extras", buffer.extras, o);
  25882. }
  25883. }
  25884. static bool SerializeGltfBuffer(Buffer &buffer, json &o,
  25885. const std::string &binFilename,
  25886. const std::string &binBaseFilename)
  25887. {
  25888. if (!SerializeGltfBufferData(buffer.data, binFilename)) return false;
  25889. SerializeNumberProperty("byteLength", buffer.data.size(), o);
  25890. SerializeStringProperty("uri", binBaseFilename, o);
  25891. if (buffer.name.size()) SerializeStringProperty("name", buffer.name, o);
  25892. if (buffer.extras.Type() != NULL_TYPE)
  25893. {
  25894. SerializeValue("extras", buffer.extras, o);
  25895. }
  25896. return true;
  25897. }
  25898. static void SerializeGltfBufferView(BufferView &bufferView, json &o)
  25899. {
  25900. SerializeNumberProperty("buffer", bufferView.buffer, o);
  25901. SerializeNumberProperty<size_t>("byteLength", bufferView.byteLength, o);
  25902. // byteStride is optional, minimum allowed is 4
  25903. if (bufferView.byteStride >= 4)
  25904. {
  25905. SerializeNumberProperty<size_t>("byteStride", bufferView.byteStride, o);
  25906. }
  25907. // byteOffset is optional, default is 0
  25908. if (bufferView.byteOffset > 0)
  25909. {
  25910. SerializeNumberProperty<size_t>("byteOffset", bufferView.byteOffset, o);
  25911. }
  25912. // Target is optional, check if it contains a valid value
  25913. if (bufferView.target == TINYGLTF_TARGET_ARRAY_BUFFER ||
  25914. bufferView.target == TINYGLTF_TARGET_ELEMENT_ARRAY_BUFFER)
  25915. {
  25916. SerializeNumberProperty("target", bufferView.target, o);
  25917. }
  25918. if (bufferView.name.size())
  25919. {
  25920. SerializeStringProperty("name", bufferView.name, o);
  25921. }
  25922. if (bufferView.extras.Type() != NULL_TYPE)
  25923. {
  25924. SerializeValue("extras", bufferView.extras, o);
  25925. }
  25926. }
  25927. static void SerializeGltfImage(Image &image, json &o)
  25928. {
  25929. // if uri empty, the mimeType and bufferview should be set
  25930. if (image.uri.empty())
  25931. {
  25932. SerializeStringProperty("mimeType", image.mimeType, o);
  25933. SerializeNumberProperty<int>("bufferView", image.bufferView, o);
  25934. }
  25935. else
  25936. {
  25937. // TODO(syoyo): dlib::urilencode?
  25938. SerializeStringProperty("uri", image.uri, o);
  25939. }
  25940. if (image.name.size())
  25941. {
  25942. SerializeStringProperty("name", image.name, o);
  25943. }
  25944. if (image.extras.Type() != NULL_TYPE)
  25945. {
  25946. SerializeValue("extras", image.extras, o);
  25947. }
  25948. SerializeExtensionMap(image.extensions, o);
  25949. }
  25950. static void SerializeGltfTextureInfo(TextureInfo &texinfo, json &o)
  25951. {
  25952. SerializeNumberProperty("index", texinfo.index, o);
  25953. if (texinfo.texCoord != 0)
  25954. {
  25955. SerializeNumberProperty("texCoord", texinfo.texCoord, o);
  25956. }
  25957. if (texinfo.extras.Type() != NULL_TYPE)
  25958. {
  25959. SerializeValue("extras", texinfo.extras, o);
  25960. }
  25961. SerializeExtensionMap(texinfo.extensions, o);
  25962. }
  25963. static void SerializeGltfNormalTextureInfo(NormalTextureInfo &texinfo,
  25964. json &o)
  25965. {
  25966. SerializeNumberProperty("index", texinfo.index, o);
  25967. if (texinfo.texCoord != 0)
  25968. {
  25969. SerializeNumberProperty("texCoord", texinfo.texCoord, o);
  25970. }
  25971. if (!TINYGLTF_DOUBLE_EQUAL(texinfo.scale, 1.0))
  25972. {
  25973. SerializeNumberProperty("scale", texinfo.scale, o);
  25974. }
  25975. if (texinfo.extras.Type() != NULL_TYPE)
  25976. {
  25977. SerializeValue("extras", texinfo.extras, o);
  25978. }
  25979. SerializeExtensionMap(texinfo.extensions, o);
  25980. }
  25981. static void SerializeGltfOcclusionTextureInfo(OcclusionTextureInfo &texinfo,
  25982. json &o)
  25983. {
  25984. SerializeNumberProperty("index", texinfo.index, o);
  25985. if (texinfo.texCoord != 0)
  25986. {
  25987. SerializeNumberProperty("texCoord", texinfo.texCoord, o);
  25988. }
  25989. if (!TINYGLTF_DOUBLE_EQUAL(texinfo.strength, 1.0))
  25990. {
  25991. SerializeNumberProperty("strength", texinfo.strength, o);
  25992. }
  25993. if (texinfo.extras.Type() != NULL_TYPE)
  25994. {
  25995. SerializeValue("extras", texinfo.extras, o);
  25996. }
  25997. SerializeExtensionMap(texinfo.extensions, o);
  25998. }
  25999. static void SerializeGltfPbrMetallicRoughness(PbrMetallicRoughness &pbr,
  26000. json &o)
  26001. {
  26002. std::vector<double> default_baseColorFactor = { 1.0, 1.0, 1.0, 1.0 };
  26003. if (!Equals(pbr.baseColorFactor, default_baseColorFactor))
  26004. {
  26005. SerializeNumberArrayProperty<double>("baseColorFactor", pbr.baseColorFactor,
  26006. o);
  26007. }
  26008. if (!TINYGLTF_DOUBLE_EQUAL(pbr.metallicFactor, 1.0))
  26009. {
  26010. SerializeNumberProperty("metallicFactor", pbr.metallicFactor, o);
  26011. }
  26012. if (!TINYGLTF_DOUBLE_EQUAL(pbr.roughnessFactor, 1.0))
  26013. {
  26014. SerializeNumberProperty("roughnessFactor", pbr.roughnessFactor, o);
  26015. }
  26016. if (pbr.baseColorTexture.index > -1)
  26017. {
  26018. json texinfo;
  26019. SerializeGltfTextureInfo(pbr.baseColorTexture, texinfo);
  26020. JsonAddMember(o, "baseColorTexture", std::move(texinfo));
  26021. }
  26022. if (pbr.metallicRoughnessTexture.index > -1)
  26023. {
  26024. json texinfo;
  26025. SerializeGltfTextureInfo(pbr.metallicRoughnessTexture, texinfo);
  26026. JsonAddMember(o, "metallicRoughnessTexture", std::move(texinfo));
  26027. }
  26028. SerializeExtensionMap(pbr.extensions, o);
  26029. if (pbr.extras.Type() != NULL_TYPE)
  26030. {
  26031. SerializeValue("extras", pbr.extras, o);
  26032. }
  26033. }
  26034. static void SerializeGltfMaterial(Material &material, json &o)
  26035. {
  26036. if (material.name.size())
  26037. {
  26038. SerializeStringProperty("name", material.name, o);
  26039. }
  26040. // QUESTION(syoyo): Write material parameters regardless of its default value?
  26041. if (!TINYGLTF_DOUBLE_EQUAL(material.alphaCutoff, 0.5))
  26042. {
  26043. SerializeNumberProperty("alphaCutoff", material.alphaCutoff, o);
  26044. }
  26045. if (material.alphaMode.compare("OPAQUE") != 0)
  26046. {
  26047. SerializeStringProperty("alphaMode", material.alphaMode, o);
  26048. }
  26049. if (material.doubleSided != false)
  26050. JsonAddMember(o, "doubleSided", json(material.doubleSided));
  26051. if (material.normalTexture.index > -1)
  26052. {
  26053. json texinfo;
  26054. SerializeGltfNormalTextureInfo(material.normalTexture, texinfo);
  26055. JsonAddMember(o, "normalTexture", std::move(texinfo));
  26056. }
  26057. if (material.occlusionTexture.index > -1)
  26058. {
  26059. json texinfo;
  26060. SerializeGltfOcclusionTextureInfo(material.occlusionTexture, texinfo);
  26061. JsonAddMember(o, "occlusionTexture", std::move(texinfo));
  26062. }
  26063. if (material.emissiveTexture.index > -1)
  26064. {
  26065. json texinfo;
  26066. SerializeGltfTextureInfo(material.emissiveTexture, texinfo);
  26067. JsonAddMember(o, "emissiveTexture", std::move(texinfo));
  26068. }
  26069. std::vector<double> default_emissiveFactor = { 0.0, 0.0, 0.0 };
  26070. if (!Equals(material.emissiveFactor, default_emissiveFactor))
  26071. {
  26072. SerializeNumberArrayProperty<double>("emissiveFactor",
  26073. material.emissiveFactor, o);
  26074. }
  26075. {
  26076. json pbrMetallicRoughness;
  26077. SerializeGltfPbrMetallicRoughness(material.pbrMetallicRoughness,
  26078. pbrMetallicRoughness);
  26079. // Issue 204
  26080. // Do not serialize `pbrMetallicRoughness` if pbrMetallicRoughness has all
  26081. // default values(json is null). Otherwise it will serialize to
  26082. // `pbrMetallicRoughness : null`, which cannot be read by other glTF
  26083. // importers(and validators).
  26084. //
  26085. if (!JsonIsNull(pbrMetallicRoughness))
  26086. {
  26087. JsonAddMember(o, "pbrMetallicRoughness", std::move(pbrMetallicRoughness));
  26088. }
  26089. }
  26090. #if 0 // legacy way. just for the record.
  26091. if (material.values.size())
  26092. {
  26093. json pbrMetallicRoughness;
  26094. SerializeParameterMap(material.values, pbrMetallicRoughness);
  26095. JsonAddMember(o, "pbrMetallicRoughness", std::move(pbrMetallicRoughness));
  26096. }
  26097. SerializeParameterMap(material.additionalValues, o);
  26098. #else
  26099. #endif
  26100. SerializeExtensionMap(material.extensions, o);
  26101. if (material.extras.Type() != NULL_TYPE)
  26102. {
  26103. SerializeValue("extras", material.extras, o);
  26104. }
  26105. }
  26106. static void SerializeGltfMesh(Mesh &mesh, json &o)
  26107. {
  26108. json primitives;
  26109. JsonReserveArray(primitives, mesh.primitives.size());
  26110. for (unsigned int i = 0; i < mesh.primitives.size(); ++i)
  26111. {
  26112. json primitive;
  26113. const Primitive &gltfPrimitive = mesh.primitives[i]; // don't make a copy
  26114. {
  26115. json attributes;
  26116. for (auto attrIt = gltfPrimitive.attributes.begin();
  26117. attrIt != gltfPrimitive.attributes.end(); ++attrIt)
  26118. {
  26119. SerializeNumberProperty<int>(attrIt->first, attrIt->second, attributes);
  26120. }
  26121. JsonAddMember(primitive, "attributes", std::move(attributes));
  26122. }
  26123. // Indicies is optional
  26124. if (gltfPrimitive.indices > -1)
  26125. {
  26126. SerializeNumberProperty<int>("indices", gltfPrimitive.indices, primitive);
  26127. }
  26128. // Material is optional
  26129. if (gltfPrimitive.material > -1)
  26130. {
  26131. SerializeNumberProperty<int>("material", gltfPrimitive.material,
  26132. primitive);
  26133. }
  26134. SerializeNumberProperty<int>("mode", gltfPrimitive.mode, primitive);
  26135. // Morph targets
  26136. if (gltfPrimitive.targets.size())
  26137. {
  26138. json targets;
  26139. JsonReserveArray(targets, gltfPrimitive.targets.size());
  26140. for (unsigned int k = 0; k < gltfPrimitive.targets.size(); ++k)
  26141. {
  26142. json targetAttributes;
  26143. std::map<std::string, int> targetData = gltfPrimitive.targets[k];
  26144. for (std::map<std::string, int>::iterator attrIt = targetData.begin();
  26145. attrIt != targetData.end(); ++attrIt)
  26146. {
  26147. SerializeNumberProperty<int>(attrIt->first, attrIt->second,
  26148. targetAttributes);
  26149. }
  26150. JsonPushBack(targets, std::move(targetAttributes));
  26151. }
  26152. JsonAddMember(primitive, "targets", std::move(targets));
  26153. }
  26154. SerializeExtensionMap(gltfPrimitive.extensions, primitive);
  26155. if (gltfPrimitive.extras.Type() != NULL_TYPE)
  26156. {
  26157. SerializeValue("extras", gltfPrimitive.extras, primitive);
  26158. }
  26159. JsonPushBack(primitives, std::move(primitive));
  26160. }
  26161. JsonAddMember(o, "primitives", std::move(primitives));
  26162. if (mesh.weights.size())
  26163. {
  26164. SerializeNumberArrayProperty<double>("weights", mesh.weights, o);
  26165. }
  26166. if (mesh.name.size())
  26167. {
  26168. SerializeStringProperty("name", mesh.name, o);
  26169. }
  26170. SerializeExtensionMap(mesh.extensions, o);
  26171. if (mesh.extras.Type() != NULL_TYPE)
  26172. {
  26173. SerializeValue("extras", mesh.extras, o);
  26174. }
  26175. }
  26176. static void SerializeSpotLight(SpotLight &spot, json &o)
  26177. {
  26178. SerializeNumberProperty("innerConeAngle", spot.innerConeAngle, o);
  26179. SerializeNumberProperty("outerConeAngle", spot.outerConeAngle, o);
  26180. SerializeExtensionMap(spot.extensions, o);
  26181. if (spot.extras.Type() != NULL_TYPE)
  26182. {
  26183. SerializeValue("extras", spot.extras, o);
  26184. }
  26185. }
  26186. static void SerializeGltfLight(Light &light, json &o)
  26187. {
  26188. if (!light.name.empty()) SerializeStringProperty("name", light.name, o);
  26189. SerializeNumberProperty("intensity", light.intensity, o);
  26190. if (light.range > 0.0)
  26191. {
  26192. SerializeNumberProperty("range", light.range, o);
  26193. }
  26194. SerializeNumberArrayProperty("color", light.color, o);
  26195. SerializeStringProperty("type", light.type, o);
  26196. if (light.type == "spot")
  26197. {
  26198. json spot;
  26199. SerializeSpotLight(light.spot, spot);
  26200. JsonAddMember(o, "spot", std::move(spot));
  26201. }
  26202. SerializeExtensionMap(light.extensions, o);
  26203. if (light.extras.Type() != NULL_TYPE)
  26204. {
  26205. SerializeValue("extras", light.extras, o);
  26206. }
  26207. }
  26208. static void SerializeGltfNode(Node &node, json &o)
  26209. {
  26210. if (node.translation.size() > 0)
  26211. {
  26212. SerializeNumberArrayProperty<double>("translation", node.translation, o);
  26213. }
  26214. if (node.rotation.size() > 0)
  26215. {
  26216. SerializeNumberArrayProperty<double>("rotation", node.rotation, o);
  26217. }
  26218. if (node.scale.size() > 0)
  26219. {
  26220. SerializeNumberArrayProperty<double>("scale", node.scale, o);
  26221. }
  26222. if (node.matrix.size() > 0)
  26223. {
  26224. SerializeNumberArrayProperty<double>("matrix", node.matrix, o);
  26225. }
  26226. if (node.mesh != -1)
  26227. {
  26228. SerializeNumberProperty<int>("mesh", node.mesh, o);
  26229. }
  26230. if (node.skin != -1)
  26231. {
  26232. SerializeNumberProperty<int>("skin", node.skin, o);
  26233. }
  26234. if (node.camera != -1)
  26235. {
  26236. SerializeNumberProperty<int>("camera", node.camera, o);
  26237. }
  26238. if (node.weights.size() > 0)
  26239. {
  26240. SerializeNumberArrayProperty<double>("weights", node.weights, o);
  26241. }
  26242. if (node.extras.Type() != NULL_TYPE)
  26243. {
  26244. SerializeValue("extras", node.extras, o);
  26245. }
  26246. SerializeExtensionMap(node.extensions, o);
  26247. if (!node.name.empty()) SerializeStringProperty("name", node.name, o);
  26248. SerializeNumberArrayProperty<int>("children", node.children, o);
  26249. }
  26250. static void SerializeGltfSampler(Sampler &sampler, json &o)
  26251. {
  26252. if (sampler.magFilter != -1)
  26253. {
  26254. SerializeNumberProperty("magFilter", sampler.magFilter, o);
  26255. }
  26256. if (sampler.minFilter != -1)
  26257. {
  26258. SerializeNumberProperty("minFilter", sampler.minFilter, o);
  26259. }
  26260. SerializeNumberProperty("wrapR", sampler.wrapR, o);
  26261. SerializeNumberProperty("wrapS", sampler.wrapS, o);
  26262. SerializeNumberProperty("wrapT", sampler.wrapT, o);
  26263. if (sampler.extras.Type() != NULL_TYPE)
  26264. {
  26265. SerializeValue("extras", sampler.extras, o);
  26266. }
  26267. }
  26268. static void SerializeGltfOrthographicCamera(const OrthographicCamera &camera,
  26269. json &o)
  26270. {
  26271. SerializeNumberProperty("zfar", camera.zfar, o);
  26272. SerializeNumberProperty("znear", camera.znear, o);
  26273. SerializeNumberProperty("xmag", camera.xmag, o);
  26274. SerializeNumberProperty("ymag", camera.ymag, o);
  26275. if (camera.extras.Type() != NULL_TYPE)
  26276. {
  26277. SerializeValue("extras", camera.extras, o);
  26278. }
  26279. }
  26280. static void SerializeGltfPerspectiveCamera(const PerspectiveCamera &camera,
  26281. json &o)
  26282. {
  26283. SerializeNumberProperty("zfar", camera.zfar, o);
  26284. SerializeNumberProperty("znear", camera.znear, o);
  26285. if (camera.aspectRatio > 0)
  26286. {
  26287. SerializeNumberProperty("aspectRatio", camera.aspectRatio, o);
  26288. }
  26289. if (camera.yfov > 0)
  26290. {
  26291. SerializeNumberProperty("yfov", camera.yfov, o);
  26292. }
  26293. if (camera.extras.Type() != NULL_TYPE)
  26294. {
  26295. SerializeValue("extras", camera.extras, o);
  26296. }
  26297. }
  26298. static void SerializeGltfCamera(const Camera &camera, json &o)
  26299. {
  26300. SerializeStringProperty("type", camera.type, o);
  26301. if (!camera.name.empty())
  26302. {
  26303. SerializeStringProperty("name", camera.name, o);
  26304. }
  26305. if (camera.type.compare("orthographic") == 0)
  26306. {
  26307. json orthographic;
  26308. SerializeGltfOrthographicCamera(camera.orthographic, orthographic);
  26309. JsonAddMember(o, "orthographic", std::move(orthographic));
  26310. }
  26311. else if (camera.type.compare("perspective") == 0)
  26312. {
  26313. json perspective;
  26314. SerializeGltfPerspectiveCamera(camera.perspective, perspective);
  26315. JsonAddMember(o, "perspective", std::move(perspective));
  26316. }
  26317. else
  26318. {
  26319. // ???
  26320. }
  26321. if (camera.extras.Type() != NULL_TYPE)
  26322. {
  26323. SerializeValue("extras", camera.extras, o);
  26324. }
  26325. SerializeExtensionMap(camera.extensions, o);
  26326. }
  26327. static void SerializeGltfScene(Scene &scene, json &o)
  26328. {
  26329. SerializeNumberArrayProperty<int>("nodes", scene.nodes, o);
  26330. if (scene.name.size())
  26331. {
  26332. SerializeStringProperty("name", scene.name, o);
  26333. }
  26334. if (scene.extras.Type() != NULL_TYPE)
  26335. {
  26336. SerializeValue("extras", scene.extras, o);
  26337. }
  26338. SerializeExtensionMap(scene.extensions, o);
  26339. }
  26340. static void SerializeGltfSkin(Skin &skin, json &o)
  26341. {
  26342. if (skin.inverseBindMatrices != -1)
  26343. SerializeNumberProperty("inverseBindMatrices", skin.inverseBindMatrices, o);
  26344. SerializeNumberArrayProperty<int>("joints", skin.joints, o);
  26345. SerializeNumberProperty("skeleton", skin.skeleton, o);
  26346. if (skin.name.size())
  26347. {
  26348. SerializeStringProperty("name", skin.name, o);
  26349. }
  26350. }
  26351. static void SerializeGltfTexture(Texture &texture, json &o)
  26352. {
  26353. if (texture.sampler > -1)
  26354. {
  26355. SerializeNumberProperty("sampler", texture.sampler, o);
  26356. }
  26357. if (texture.source > -1)
  26358. {
  26359. SerializeNumberProperty("source", texture.source, o);
  26360. }
  26361. if (texture.name.size())
  26362. {
  26363. SerializeStringProperty("name", texture.name, o);
  26364. }
  26365. if (texture.extras.Type() != NULL_TYPE)
  26366. {
  26367. SerializeValue("extras", texture.extras, o);
  26368. }
  26369. SerializeExtensionMap(texture.extensions, o);
  26370. }
  26371. ///
  26372. /// Serialize all properties except buffers and images.
  26373. ///
  26374. static void SerializeGltfModel(Model *model, json &o)
  26375. {
  26376. // ACCESSORS
  26377. if (model->accessors.size())
  26378. {
  26379. json accessors;
  26380. JsonReserveArray(accessors, model->accessors.size());
  26381. for (unsigned int i = 0; i < model->accessors.size(); ++i)
  26382. {
  26383. json accessor;
  26384. SerializeGltfAccessor(model->accessors[i], accessor);
  26385. JsonPushBack(accessors, std::move(accessor));
  26386. }
  26387. JsonAddMember(o, "accessors", std::move(accessors));
  26388. }
  26389. // ANIMATIONS
  26390. if (model->animations.size())
  26391. {
  26392. json animations;
  26393. JsonReserveArray(animations, model->animations.size());
  26394. for (unsigned int i = 0; i < model->animations.size(); ++i)
  26395. {
  26396. if (model->animations[i].channels.size())
  26397. {
  26398. json animation;
  26399. SerializeGltfAnimation(model->animations[i], animation);
  26400. JsonPushBack(animations, std::move(animation));
  26401. }
  26402. }
  26403. JsonAddMember(o, "animations", std::move(animations));
  26404. }
  26405. // ASSET
  26406. json asset;
  26407. SerializeGltfAsset(model->asset, asset);
  26408. JsonAddMember(o, "asset", std::move(asset));
  26409. // BUFFERVIEWS
  26410. if (model->bufferViews.size())
  26411. {
  26412. json bufferViews;
  26413. JsonReserveArray(bufferViews, model->bufferViews.size());
  26414. for (unsigned int i = 0; i < model->bufferViews.size(); ++i)
  26415. {
  26416. json bufferView;
  26417. SerializeGltfBufferView(model->bufferViews[i], bufferView);
  26418. JsonPushBack(bufferViews, std::move(bufferView));
  26419. }
  26420. JsonAddMember(o, "bufferViews", std::move(bufferViews));
  26421. }
  26422. // Extensions required
  26423. if (model->extensionsRequired.size())
  26424. {
  26425. SerializeStringArrayProperty("extensionsRequired",
  26426. model->extensionsRequired, o);
  26427. }
  26428. // MATERIALS
  26429. if (model->materials.size())
  26430. {
  26431. json materials;
  26432. JsonReserveArray(materials, model->materials.size());
  26433. for (unsigned int i = 0; i < model->materials.size(); ++i)
  26434. {
  26435. json material;
  26436. SerializeGltfMaterial(model->materials[i], material);
  26437. if (JsonIsNull(material))
  26438. {
  26439. // Issue 294.
  26440. // `material` does not have any required parameters
  26441. // so the result may be null(unmodified) when all material parameters have default value.
  26442. //
  26443. // null is not allowed thus we create an empty JSON object.
  26444. JsonSetObject(material);
  26445. }
  26446. JsonPushBack(materials, std::move(material));
  26447. }
  26448. JsonAddMember(o, "materials", std::move(materials));
  26449. }
  26450. // MESHES
  26451. if (model->meshes.size())
  26452. {
  26453. json meshes;
  26454. JsonReserveArray(meshes, model->meshes.size());
  26455. for (unsigned int i = 0; i < model->meshes.size(); ++i)
  26456. {
  26457. json mesh;
  26458. SerializeGltfMesh(model->meshes[i], mesh);
  26459. JsonPushBack(meshes, std::move(mesh));
  26460. }
  26461. JsonAddMember(o, "meshes", std::move(meshes));
  26462. }
  26463. // NODES
  26464. if (model->nodes.size())
  26465. {
  26466. json nodes;
  26467. JsonReserveArray(nodes, model->nodes.size());
  26468. for (unsigned int i = 0; i < model->nodes.size(); ++i)
  26469. {
  26470. json node;
  26471. SerializeGltfNode(model->nodes[i], node);
  26472. JsonPushBack(nodes, std::move(node));
  26473. }
  26474. JsonAddMember(o, "nodes", std::move(nodes));
  26475. }
  26476. // SCENE
  26477. if (model->defaultScene > -1)
  26478. {
  26479. SerializeNumberProperty<int>("scene", model->defaultScene, o);
  26480. }
  26481. // SCENES
  26482. if (model->scenes.size())
  26483. {
  26484. json scenes;
  26485. JsonReserveArray(scenes, model->scenes.size());
  26486. for (unsigned int i = 0; i < model->scenes.size(); ++i)
  26487. {
  26488. json currentScene;
  26489. SerializeGltfScene(model->scenes[i], currentScene);
  26490. JsonPushBack(scenes, std::move(currentScene));
  26491. }
  26492. JsonAddMember(o, "scenes", std::move(scenes));
  26493. }
  26494. // SKINS
  26495. if (model->skins.size())
  26496. {
  26497. json skins;
  26498. JsonReserveArray(skins, model->skins.size());
  26499. for (unsigned int i = 0; i < model->skins.size(); ++i)
  26500. {
  26501. json skin;
  26502. SerializeGltfSkin(model->skins[i], skin);
  26503. JsonPushBack(skins, std::move(skin));
  26504. }
  26505. JsonAddMember(o, "skins", std::move(skins));
  26506. }
  26507. // TEXTURES
  26508. if (model->textures.size())
  26509. {
  26510. json textures;
  26511. JsonReserveArray(textures, model->textures.size());
  26512. for (unsigned int i = 0; i < model->textures.size(); ++i)
  26513. {
  26514. json texture;
  26515. SerializeGltfTexture(model->textures[i], texture);
  26516. JsonPushBack(textures, std::move(texture));
  26517. }
  26518. JsonAddMember(o, "textures", std::move(textures));
  26519. }
  26520. // SAMPLERS
  26521. if (model->samplers.size())
  26522. {
  26523. json samplers;
  26524. JsonReserveArray(samplers, model->samplers.size());
  26525. for (unsigned int i = 0; i < model->samplers.size(); ++i)
  26526. {
  26527. json sampler;
  26528. SerializeGltfSampler(model->samplers[i], sampler);
  26529. JsonPushBack(samplers, std::move(sampler));
  26530. }
  26531. JsonAddMember(o, "samplers", std::move(samplers));
  26532. }
  26533. // CAMERAS
  26534. if (model->cameras.size())
  26535. {
  26536. json cameras;
  26537. JsonReserveArray(cameras, model->cameras.size());
  26538. for (unsigned int i = 0; i < model->cameras.size(); ++i)
  26539. {
  26540. json camera;
  26541. SerializeGltfCamera(model->cameras[i], camera);
  26542. JsonPushBack(cameras, std::move(camera));
  26543. }
  26544. JsonAddMember(o, "cameras", std::move(cameras));
  26545. }
  26546. // EXTENSIONS
  26547. SerializeExtensionMap(model->extensions, o);
  26548. auto extensionsUsed = model->extensionsUsed;
  26549. // LIGHTS as KHR_lights_punctual
  26550. if (model->lights.size())
  26551. {
  26552. json lights;
  26553. JsonReserveArray(lights, model->lights.size());
  26554. for (unsigned int i = 0; i < model->lights.size(); ++i)
  26555. {
  26556. json light;
  26557. SerializeGltfLight(model->lights[i], light);
  26558. JsonPushBack(lights, std::move(light));
  26559. }
  26560. json khr_lights_cmn;
  26561. JsonAddMember(khr_lights_cmn, "lights", std::move(lights));
  26562. json ext_j;
  26563. {
  26564. json_const_iterator it;
  26565. if (FindMember(o, "extensions", it))
  26566. {
  26567. JsonAssign(ext_j, GetValue(it));
  26568. }
  26569. }
  26570. JsonAddMember(ext_j, "KHR_lights_punctual", std::move(khr_lights_cmn));
  26571. JsonAddMember(o, "extensions", std::move(ext_j));
  26572. // Also add "KHR_lights_punctual" to `extensionsUsed`
  26573. {
  26574. auto has_khr_lights_punctual =
  26575. std::find_if(extensionsUsed.begin(), extensionsUsed.end(),
  26576. [](const std::string &s)
  26577. {
  26578. return (s.compare("KHR_lights_punctual") == 0);
  26579. });
  26580. if (has_khr_lights_punctual == extensionsUsed.end())
  26581. {
  26582. extensionsUsed.push_back("KHR_lights_punctual");
  26583. }
  26584. }
  26585. }
  26586. // Extensions used
  26587. if (model->extensionsUsed.size())
  26588. {
  26589. SerializeStringArrayProperty("extensionsUsed", extensionsUsed, o);
  26590. }
  26591. // EXTRAS
  26592. if (model->extras.Type() != NULL_TYPE)
  26593. {
  26594. SerializeValue("extras", model->extras, o);
  26595. }
  26596. }
  26597. static bool WriteGltfStream(std::ostream &stream, const std::string &content)
  26598. {
  26599. stream << content << std::endl;
  26600. return true;
  26601. }
  26602. //removed writing data
  26603. static bool WriteGltfFile(const std::string &output,
  26604. const std::string &content)
  26605. {
  26606. assert(0);
  26607. /*
  26608. #ifdef _WIN32
  26609. #if defined(_MSC_VER)
  26610. std::ofstream gltfFile(UTF8ToWchar(output).c_str());
  26611. #elif defined(__GLIBCXX__)
  26612. int file_descriptor = _wopen(UTF8ToWchar(output).c_str(),
  26613. _O_CREAT | _O_WRONLY | _O_TRUNC | _O_BINARY);
  26614. __gnu_cxx::stdio_filebuf<char> wfile_buf(
  26615. file_descriptor, std::ios_base::out | std::ios_base::binary);
  26616. std::ostream gltfFile(&wfile_buf);
  26617. if (!wfile_buf.is_open()) return false;
  26618. #else
  26619. std::ofstream gltfFile(output.c_str());
  26620. if (!gltfFile.is_open()) return false;
  26621. #endif
  26622. #else
  26623. std::ofstream gltfFile(output.c_str());
  26624. if (!gltfFile.is_open()) return false;
  26625. #endif
  26626. return WriteGltfStream(gltfFile, content);
  26627. */
  26628. return 0;
  26629. }
  26630. static void WriteBinaryGltfStream(std::ostream &stream,
  26631. const std::string &content,
  26632. const std::vector<unsigned char> &binBuffer)
  26633. {
  26634. const std::string header = "glTF";
  26635. const int version = 2;
  26636. // https://stackoverflow.com/questions/3407012/c-rounding-up-to-the-nearest-multiple-of-a-number
  26637. auto roundUp = [](uint32_t numToRound, uint32_t multiple)
  26638. {
  26639. if (multiple == 0) return numToRound;
  26640. uint32_t remainder = numToRound % multiple;
  26641. if (remainder == 0) return numToRound;
  26642. return numToRound + multiple - remainder;
  26643. };
  26644. const uint32_t padding_size =
  26645. roundUp(uint32_t(content.size()), 4) - uint32_t(content.size());
  26646. // 12 bytes for header, JSON content length, 8 bytes for JSON chunk info.
  26647. // Chunk data must be located at 4-byte boundary.
  26648. const uint32_t length =
  26649. 12 + 8 + roundUp(uint32_t(content.size()), 4) +
  26650. (binBuffer.size() ? (8 + roundUp(uint32_t(binBuffer.size()), 4)) : 0);
  26651. stream.write(header.c_str(), std::streamsize(header.size()));
  26652. stream.write(reinterpret_cast<const char *>(&version), sizeof(version));
  26653. stream.write(reinterpret_cast<const char *>(&length), sizeof(length));
  26654. // JSON chunk info, then JSON data
  26655. const uint32_t model_length = uint32_t(content.size()) + padding_size;
  26656. const uint32_t model_format = 0x4E4F534A;
  26657. stream.write(reinterpret_cast<const char *>(&model_length),
  26658. sizeof(model_length));
  26659. stream.write(reinterpret_cast<const char *>(&model_format),
  26660. sizeof(model_format));
  26661. stream.write(content.c_str(), std::streamsize(content.size()));
  26662. // Chunk must be multiplies of 4, so pad with spaces
  26663. if (padding_size > 0)
  26664. {
  26665. const std::string padding = std::string(size_t(padding_size), ' ');
  26666. stream.write(padding.c_str(), std::streamsize(padding.size()));
  26667. }
  26668. if (binBuffer.size() > 0)
  26669. {
  26670. const uint32_t bin_padding_size =
  26671. roundUp(uint32_t(binBuffer.size()), 4) - uint32_t(binBuffer.size());
  26672. // BIN chunk info, then BIN data
  26673. const uint32_t bin_length = uint32_t(binBuffer.size()) + bin_padding_size;
  26674. const uint32_t bin_format = 0x004e4942;
  26675. stream.write(reinterpret_cast<const char *>(&bin_length),
  26676. sizeof(bin_length));
  26677. stream.write(reinterpret_cast<const char *>(&bin_format),
  26678. sizeof(bin_format));
  26679. stream.write(reinterpret_cast<const char *>(binBuffer.data()),
  26680. std::streamsize(binBuffer.size()));
  26681. // Chunksize must be multiplies of 4, so pad with zeroes
  26682. if (bin_padding_size > 0)
  26683. {
  26684. const std::vector<unsigned char> padding =
  26685. std::vector<unsigned char>(size_t(bin_padding_size), 0);
  26686. stream.write(reinterpret_cast<const char *>(padding.data()),
  26687. std::streamsize(padding.size()));
  26688. }
  26689. }
  26690. }
  26691. //removed writing data
  26692. static void WriteBinaryGltfFile(const std::string &output,
  26693. const std::string &content,
  26694. const std::vector<unsigned char> &binBuffer)
  26695. {
  26696. assert(0);
  26697. /*
  26698. #ifdef _WIN32
  26699. #if defined(_MSC_VER)
  26700. std::ofstream gltfFile(UTF8ToWchar(output).c_str(), std::ios::binary);
  26701. #elif defined(__GLIBCXX__)
  26702. int file_descriptor = _wopen(UTF8ToWchar(output).c_str(),
  26703. _O_CREAT | _O_WRONLY | _O_TRUNC | _O_BINARY);
  26704. __gnu_cxx::stdio_filebuf<char> wfile_buf(
  26705. file_descriptor, std::ios_base::out | std::ios_base::binary);
  26706. std::ostream gltfFile(&wfile_buf);
  26707. #else
  26708. std::ofstream gltfFile(output.c_str(), std::ios::binary);
  26709. #endif
  26710. #else
  26711. std::ofstream gltfFile(output.c_str(), std::ios::binary);
  26712. #endif
  26713. WriteBinaryGltfStream(gltfFile, content, binBuffer);
  26714. */
  26715. }
  26716. bool TinyGLTF::WriteGltfSceneToStream(Model *model, std::ostream &stream,
  26717. bool prettyPrint = true,
  26718. bool writeBinary = false)
  26719. {
  26720. JsonDocument output;
  26721. /// Serialize all properties except buffers and images.
  26722. SerializeGltfModel(model, output);
  26723. // BUFFERS
  26724. std::vector<unsigned char> binBuffer;
  26725. if (model->buffers.size())
  26726. {
  26727. json buffers;
  26728. JsonReserveArray(buffers, model->buffers.size());
  26729. for (unsigned int i = 0; i < model->buffers.size(); ++i)
  26730. {
  26731. json buffer;
  26732. if (writeBinary && i == 0 && model->buffers[i].uri.empty())
  26733. {
  26734. SerializeGltfBufferBin(model->buffers[i], buffer, binBuffer);
  26735. }
  26736. else
  26737. {
  26738. SerializeGltfBuffer(model->buffers[i], buffer);
  26739. }
  26740. JsonPushBack(buffers, std::move(buffer));
  26741. }
  26742. JsonAddMember(output, "buffers", std::move(buffers));
  26743. }
  26744. // IMAGES
  26745. if (model->images.size())
  26746. {
  26747. json images;
  26748. JsonReserveArray(images, model->images.size());
  26749. for (unsigned int i = 0; i < model->images.size(); ++i)
  26750. {
  26751. json image;
  26752. std::string dummystring = "";
  26753. // UpdateImageObject need baseDir but only uses it if embeddedImages is
  26754. // enabled, since we won't write separate images when writing to a stream
  26755. // we
  26756. UpdateImageObject(model->images[i], dummystring, int(i), false,
  26757. &this->WriteImageData, this->write_image_user_data_);
  26758. SerializeGltfImage(model->images[i], image);
  26759. JsonPushBack(images, std::move(image));
  26760. }
  26761. JsonAddMember(output, "images", std::move(images));
  26762. }
  26763. if (writeBinary)
  26764. {
  26765. WriteBinaryGltfStream(stream, JsonToString(output), binBuffer);
  26766. }
  26767. else
  26768. {
  26769. WriteGltfStream(stream, JsonToString(output, prettyPrint ? 2 : -1));
  26770. }
  26771. return true;
  26772. }
  26773. bool TinyGLTF::WriteGltfSceneToFile(Model *model, const std::string &filename,
  26774. bool embedImages = false,
  26775. bool embedBuffers = false,
  26776. bool prettyPrint = true,
  26777. bool writeBinary = false)
  26778. {
  26779. JsonDocument output;
  26780. std::string defaultBinFilename = GetBaseFilename(filename);
  26781. std::string defaultBinFileExt = ".bin";
  26782. std::string::size_type pos =
  26783. defaultBinFilename.rfind('.', defaultBinFilename.length());
  26784. if (pos != std::string::npos)
  26785. {
  26786. defaultBinFilename = defaultBinFilename.substr(0, pos);
  26787. }
  26788. std::string baseDir = GetBaseDir(filename);
  26789. if (baseDir.empty())
  26790. {
  26791. baseDir = "./";
  26792. }
  26793. /// Serialize all properties except buffers and images.
  26794. SerializeGltfModel(model, output);
  26795. // BUFFERS
  26796. std::vector<std::string> usedUris;
  26797. std::vector<unsigned char> binBuffer;
  26798. if (model->buffers.size())
  26799. {
  26800. json buffers;
  26801. JsonReserveArray(buffers, model->buffers.size());
  26802. for (unsigned int i = 0; i < model->buffers.size(); ++i)
  26803. {
  26804. json buffer;
  26805. if (writeBinary && i == 0 && model->buffers[i].uri.empty())
  26806. {
  26807. SerializeGltfBufferBin(model->buffers[i], buffer, binBuffer);
  26808. }
  26809. else if (embedBuffers)
  26810. {
  26811. SerializeGltfBuffer(model->buffers[i], buffer);
  26812. }
  26813. else
  26814. {
  26815. std::string binSavePath;
  26816. std::string binUri;
  26817. if (!model->buffers[i].uri.empty() &&
  26818. !IsDataURI(model->buffers[i].uri))
  26819. {
  26820. binUri = model->buffers[i].uri;
  26821. }
  26822. else
  26823. {
  26824. binUri = defaultBinFilename + defaultBinFileExt;
  26825. bool inUse = true;
  26826. int numUsed = 0;
  26827. while (inUse)
  26828. {
  26829. inUse = false;
  26830. for (const std::string &usedName : usedUris)
  26831. {
  26832. if (binUri.compare(usedName) != 0) continue;
  26833. inUse = true;
  26834. binUri = defaultBinFilename + std::to_string(numUsed++) +
  26835. defaultBinFileExt;
  26836. break;
  26837. }
  26838. }
  26839. }
  26840. usedUris.push_back(binUri);
  26841. binSavePath = JoinPath(baseDir, binUri);
  26842. if (!SerializeGltfBuffer(model->buffers[i], buffer, binSavePath,
  26843. binUri))
  26844. {
  26845. return false;
  26846. }
  26847. }
  26848. JsonPushBack(buffers, std::move(buffer));
  26849. }
  26850. JsonAddMember(output, "buffers", std::move(buffers));
  26851. }
  26852. // IMAGES
  26853. if (model->images.size())
  26854. {
  26855. json images;
  26856. JsonReserveArray(images, model->images.size());
  26857. for (unsigned int i = 0; i < model->images.size(); ++i)
  26858. {
  26859. json image;
  26860. UpdateImageObject(model->images[i], baseDir, int(i), embedImages,
  26861. &this->WriteImageData, this->write_image_user_data_);
  26862. SerializeGltfImage(model->images[i], image);
  26863. JsonPushBack(images, std::move(image));
  26864. }
  26865. JsonAddMember(output, "images", std::move(images));
  26866. }
  26867. if (writeBinary)
  26868. {
  26869. WriteBinaryGltfFile(filename, JsonToString(output), binBuffer);
  26870. }
  26871. else
  26872. {
  26873. WriteGltfFile(filename, JsonToString(output, (prettyPrint ? 2 : -1)));
  26874. }
  26875. return true;
  26876. }
  26877. } // namespace tinygltf
  26878. #ifdef __clang__
  26879. #pragma clang diagnostic pop
  26880. #endif
  26881. #endif // TINYGLTF_IMPLEMENTATION
  26882. #pragma endregion
  26883. ////////////////////////////////////////////////
  26884. //OBJ_Loader.h
  26885. ////////////////////////////////////////////////
  26886. #pragma region OBJ_Loader
  26887. // OBJ_Loader.h - A Single Header OBJ Model Loader
  26888. // This piece of code is not owned by gl3d
  26889. #pragma once
  26890. // Vector - STD Vector/Array Library
  26891. #include <vector>
  26892. #include <unordered_set>
  26893. #include <algorithm>
  26894. // String - STD String Library
  26895. #include <string>
  26896. #include <sstream>
  26897. // Math.h - STD math Library
  26898. #include <math.h>
  26899. // Print progress to console while loading (large models)
  26900. //#define OBJL_CONSOLE_OUTPUT
  26901. #include <glm/vec3.hpp>
  26902. #include <glm/vec4.hpp>
  26903. #include <glm/gtc/quaternion.hpp>
  26904. #include <glm/gtx/quaternion.hpp>
  26905. // Namespace: OBJL
  26906. //
  26907. // Description: The namespace that holds eveyrthing that
  26908. // is needed and used for the OBJ Model Loader
  26909. namespace objl
  26910. {
  26911. // Structure: Vector2
  26912. //
  26913. // Description: A 2D Vector that Holds Positional Data
  26914. struct Vector2
  26915. {
  26916. // Default Constructor
  26917. Vector2()
  26918. {
  26919. X = 0.0f;
  26920. Y = 0.0f;
  26921. }
  26922. // Variable Set Constructor
  26923. Vector2(float X_, float Y_)
  26924. {
  26925. X = X_;
  26926. Y = Y_;
  26927. }
  26928. // Bool Equals Operator Overload
  26929. bool operator==(const Vector2& other) const
  26930. {
  26931. return (this->X == other.X && this->Y == other.Y);
  26932. }
  26933. // Bool Not Equals Operator Overload
  26934. bool operator!=(const Vector2& other) const
  26935. {
  26936. return !(this->X == other.X && this->Y == other.Y);
  26937. }
  26938. // Addition Operator Overload
  26939. Vector2 operator+(const Vector2& right) const
  26940. {
  26941. return Vector2(this->X + right.X, this->Y + right.Y);
  26942. }
  26943. // Subtraction Operator Overload
  26944. Vector2 operator-(const Vector2& right) const
  26945. {
  26946. return Vector2(this->X - right.X, this->Y - right.Y);
  26947. }
  26948. // Float Multiplication Operator Overload
  26949. Vector2 operator*(const float& other) const
  26950. {
  26951. return Vector2(this->X *other, this->Y * other);
  26952. }
  26953. // Positional Variables
  26954. float X;
  26955. float Y;
  26956. };
  26957. // Structure: Vector3
  26958. //
  26959. // Description: A 3D Vector that Holds Positional Data
  26960. struct Vector3
  26961. {
  26962. // Default Constructor
  26963. Vector3()
  26964. {
  26965. X = 0.0f;
  26966. Y = 0.0f;
  26967. Z = 0.0f;
  26968. }
  26969. // Variable Set Constructor
  26970. Vector3(float X_, float Y_, float Z_)
  26971. {
  26972. X = X_;
  26973. Y = Y_;
  26974. Z = Z_;
  26975. }
  26976. // Bool Equals Operator Overload
  26977. bool operator==(const Vector3& other) const
  26978. {
  26979. return (this->X == other.X && this->Y == other.Y && this->Z == other.Z);
  26980. }
  26981. // Bool Not Equals Operator Overload
  26982. bool operator!=(const Vector3& other) const
  26983. {
  26984. return !(this->X == other.X && this->Y == other.Y && this->Z == other.Z);
  26985. }
  26986. // Addition Operator Overload
  26987. Vector3 operator+(const Vector3& right) const
  26988. {
  26989. return Vector3(this->X + right.X, this->Y + right.Y, this->Z + right.Z);
  26990. }
  26991. // Subtraction Operator Overload
  26992. Vector3 operator-(const Vector3& right) const
  26993. {
  26994. return Vector3(this->X - right.X, this->Y - right.Y, this->Z - right.Z);
  26995. }
  26996. // Float Multiplication Operator Overload
  26997. Vector3 operator*(const float& other) const
  26998. {
  26999. return Vector3(this->X * other, this->Y * other, this->Z * other);
  27000. }
  27001. // Float Division Operator Overload
  27002. Vector3 operator/(const float& other) const
  27003. {
  27004. return Vector3(this->X / other, this->Y / other, this->Z / other);
  27005. }
  27006. glm::vec3 operator= (glm::vec3 &other)
  27007. {
  27008. other.x = X;
  27009. other.y = Y;
  27010. other.z = Z;
  27011. return other;
  27012. }
  27013. operator glm::vec3() const { return glm::vec3(X,Y,Z); }
  27014. // Positional Variables
  27015. float X;
  27016. float Y;
  27017. float Z;
  27018. };
  27019. // Structure: Vertex
  27020. //
  27021. // Description: Model Vertex object that holds
  27022. // a Position, Normal, and Texture Coordinate
  27023. struct Vertex
  27024. {
  27025. // Position Vector
  27026. Vector3 Position;
  27027. // Normal Vector
  27028. Vector3 Normal;
  27029. // Texture Coordinate Vector
  27030. Vector2 TextureCoordinate;
  27031. };
  27032. struct VertexAnimations
  27033. {
  27034. // Position Vector
  27035. Vector3 Position;
  27036. // Normal Vector
  27037. Vector3 Normal;
  27038. // Texture Coordinate Vector
  27039. Vector2 TextureCoordinate;
  27040. glm::ivec4 jointsId;
  27041. glm::vec4 weights;
  27042. };
  27043. struct LoadedTexture
  27044. {
  27045. std::vector<unsigned char > data;
  27046. int w = 0;
  27047. int h = 0;
  27048. int components = 0;
  27049. };
  27050. struct Material
  27051. {
  27052. // Material Name
  27053. std::string name = "";
  27054. // Ambient Color
  27055. Vector3 Ka;
  27056. // Diffuse Color
  27057. glm::vec4 Kd = glm::vec4{ 1,1,1,1 };
  27058. // Specular Color
  27059. //Vector3 Ks;
  27060. // Specular Exponent
  27061. //float Ns;
  27062. // Optical Density
  27063. //float Ni;
  27064. // Dissolve
  27065. //float d;
  27066. // Illumination
  27067. //int illum;
  27068. // metallic
  27069. float metallic = 0;
  27070. // roughness
  27071. float roughness = 0.5;
  27072. //ambient factor for pbr
  27073. float ao = 1.f;
  27074. // Ambient Texture Map
  27075. std::string map_Ka;
  27076. // Diffuse Texture Map
  27077. std::string map_Kd;
  27078. LoadedTexture loadedDiffuse;
  27079. // Specular Texture Map
  27080. //std::string map_Ks;
  27081. // Specularity Map
  27082. //std::string map_Ns;
  27083. // Alpha Texture Map
  27084. //std::string map_d;
  27085. // Bump Map
  27086. //std::string map_bump;
  27087. // Normal Map
  27088. std::string map_Kn;
  27089. LoadedTexture loadedNormal;
  27090. //Roughness Map
  27091. std::string map_Pr;
  27092. //AO map
  27093. //std::string map_Ao;
  27094. //matallic map
  27095. std::string map_Pm;
  27096. //ORM map
  27097. std::string map_ORM;
  27098. LoadedTexture loadedORM;
  27099. //RMA map
  27100. std::string map_RMA;
  27101. //Emissive map
  27102. std::string map_emissive;
  27103. LoadedTexture loadedEmissive;
  27104. };
  27105. // Structure: Mesh
  27106. //
  27107. // Description: A Simple Mesh Object that holds
  27108. // a name, a vertex list, and an index list
  27109. struct Mesh
  27110. {
  27111. // Default Constructor
  27112. Mesh()
  27113. {
  27114. }
  27115. // Variable Set Constructor
  27116. Mesh(std::vector<Vertex>& _Vertices, std::vector<unsigned int>& _Indices)
  27117. {
  27118. Vertices = _Vertices;
  27119. Indices = _Indices;
  27120. }
  27121. // Mesh Name
  27122. std::string MeshName;
  27123. // Vertex List
  27124. std::vector<Vertex> Vertices;
  27125. std::vector<VertexAnimations> VerticesAnimations;
  27126. // Index List
  27127. std::vector<unsigned int> Indices;
  27128. bool hasBones = 0;
  27129. // Material
  27130. //Material MeshMaterial;
  27131. int materialIndex = -1;
  27132. };
  27133. // Namespace: Math
  27134. //
  27135. // Description: The namespace that holds all of the math
  27136. // functions need for OBJL
  27137. namespace math
  27138. {
  27139. // Vector3 Cross Product
  27140. inline Vector3 CrossV3(const Vector3 a, const Vector3 b)
  27141. {
  27142. return Vector3(a.Y * b.Z - a.Z * b.Y,
  27143. a.Z * b.X - a.X * b.Z,
  27144. a.X * b.Y - a.Y * b.X);
  27145. }
  27146. // Vector3 Magnitude Calculation
  27147. inline float MagnitudeV3(const Vector3 in)
  27148. {
  27149. return (sqrtf(powf(in.X, 2) + powf(in.Y, 2) + powf(in.Z, 2)));
  27150. }
  27151. // Vector3 DotProduct
  27152. inline float DotV3(const Vector3 a, const Vector3 b)
  27153. {
  27154. return (a.X * b.X) + (a.Y * b.Y) + (a.Z * b.Z);
  27155. }
  27156. // Angle between 2 Vector3 Objects
  27157. inline float AngleBetweenV3(const Vector3 a, const Vector3 b)
  27158. {
  27159. float angle = DotV3(a, b);
  27160. angle /= (MagnitudeV3(a) * MagnitudeV3(b));
  27161. return angle = acosf(angle);
  27162. }
  27163. // Projection Calculation of a onto b
  27164. inline Vector3 ProjV3(const Vector3 a, const Vector3 b)
  27165. {
  27166. Vector3 bn = b / MagnitudeV3(b);
  27167. return bn * DotV3(a, bn);
  27168. }
  27169. }
  27170. // Namespace: Algorithm
  27171. //
  27172. // Description: The namespace that holds all of the
  27173. // Algorithms needed for OBJL
  27174. namespace algorithm
  27175. {
  27176. // Vector3 Multiplication Opertor Overload
  27177. inline Vector3 operator*(const float& left, const Vector3& right)
  27178. {
  27179. return Vector3(right.X * left, right.Y * left, right.Z * left);
  27180. }
  27181. // A test to see if P1 is on the same side as P2 of a line segment ab
  27182. inline bool SameSide(Vector3 p1, Vector3 p2, Vector3 a, Vector3 b)
  27183. {
  27184. Vector3 cp1 = math::CrossV3(b - a, p1 - a);
  27185. Vector3 cp2 = math::CrossV3(b - a, p2 - a);
  27186. if (math::DotV3(cp1, cp2) >= 0)
  27187. return true;
  27188. else
  27189. return false;
  27190. }
  27191. // Generate a cross produect normal for a triangle
  27192. inline Vector3 GenTriNormal(Vector3 t1, Vector3 t2, Vector3 t3)
  27193. {
  27194. Vector3 u = t2 - t1;
  27195. Vector3 v = t3 - t1;
  27196. Vector3 normal = math::CrossV3(u,v);
  27197. return normal;
  27198. }
  27199. // Check to see if a Vector3 Point is within a 3 Vector3 Triangle
  27200. inline bool inTriangle(Vector3 point, Vector3 tri1, Vector3 tri2, Vector3 tri3)
  27201. {
  27202. // Test to see if it is within an infinite prism that the triangle outlines.
  27203. bool within_tri_prisim = SameSide(point, tri1, tri2, tri3) && SameSide(point, tri2, tri1, tri3)
  27204. && SameSide(point, tri3, tri1, tri2);
  27205. // If it isn't it will never be on the triangle
  27206. if (!within_tri_prisim)
  27207. return false;
  27208. // Calulate Triangle's Normal
  27209. Vector3 n = GenTriNormal(tri1, tri2, tri3);
  27210. // Project the point onto this normal
  27211. Vector3 proj = math::ProjV3(point, n);
  27212. // If the distance from the triangle to the point is 0
  27213. // it lies on the triangle
  27214. if (math::MagnitudeV3(proj) == 0)
  27215. return true;
  27216. else
  27217. return false;
  27218. }
  27219. // Split a String into a string array at a given token
  27220. inline void split(const std::string &in,
  27221. std::vector<std::string> &out,
  27222. std::string token)
  27223. {
  27224. out.clear();
  27225. out.reserve(12);
  27226. std::string temp;
  27227. for (int i = 0; i < int(in.size()); i++)
  27228. {
  27229. std::string test = in.substr(i, token.size());
  27230. if (test == token)
  27231. {
  27232. if (!temp.empty())
  27233. {
  27234. out.push_back(temp);
  27235. temp.clear();
  27236. i += (int)token.size() - 1;
  27237. }
  27238. else
  27239. {
  27240. out.push_back("");
  27241. }
  27242. }
  27243. else if (i + token.size() >= in.size())
  27244. {
  27245. temp += in.substr(i, token.size());
  27246. out.push_back(temp);
  27247. break;
  27248. }
  27249. else
  27250. {
  27251. temp += in[i];
  27252. }
  27253. }
  27254. }
  27255. inline void split2(const std::string &in,
  27256. std::vector<std::string> &out,
  27257. char token)
  27258. {
  27259. out.clear();
  27260. out.reserve(12);
  27261. std::string temp;
  27262. temp.reserve(25);
  27263. for (int i = 0; i < int(in.size()); i++)
  27264. {
  27265. if (in[i] == token)
  27266. {
  27267. if (!temp.empty())
  27268. {
  27269. out.push_back(temp);
  27270. temp.clear();
  27271. }
  27272. else
  27273. {
  27274. }
  27275. }
  27276. else if (i + 1 >= in.size())
  27277. {
  27278. temp += in[i];
  27279. out.push_back(temp);
  27280. break;
  27281. }
  27282. else
  27283. {
  27284. temp += in[i];
  27285. }
  27286. }
  27287. }
  27288. // Get tail of string after first token and possibly following spaces
  27289. inline std::string tail(const std::string &in)
  27290. {
  27291. size_t token_start = in.find_first_not_of(" \t");
  27292. size_t space_start = in.find_first_of(" \t", token_start);
  27293. size_t tail_start = in.find_first_not_of(" \t", space_start);
  27294. size_t tail_end = in.find_last_not_of(" \t");
  27295. if (tail_start != std::string::npos && tail_end != std::string::npos)
  27296. {
  27297. return in.substr(tail_start, tail_end - tail_start + 1);
  27298. }
  27299. else if (tail_start != std::string::npos)
  27300. {
  27301. return in.substr(tail_start);
  27302. }
  27303. return "";
  27304. }
  27305. // Get first token of string
  27306. inline std::string firstToken(const std::string &in)
  27307. {
  27308. if (!in.empty())
  27309. {
  27310. size_t token_start = in.find_first_not_of(" \t");
  27311. size_t token_end = in.find_first_of(" \t", token_start);
  27312. if (token_start != std::string::npos && token_end != std::string::npos)
  27313. {
  27314. return in.substr(token_start, token_end - token_start);
  27315. }
  27316. else if (token_start != std::string::npos)
  27317. {
  27318. return in.substr(token_start);
  27319. }
  27320. }
  27321. return "";
  27322. }
  27323. // Get element at given index position
  27324. template <class T>
  27325. inline const T & getElement(const std::vector<T> &elements, std::string &index)
  27326. {
  27327. int idx = std::stoi(index);
  27328. if (idx < 0)
  27329. idx = int(elements.size()) + idx;
  27330. else
  27331. idx--;
  27332. return elements[idx];
  27333. }
  27334. }
  27335. inline bool customFileExists(const std::string &abs_filename, void *userData)
  27336. {
  27337. gl3d::FileOpener *fileOpener = (gl3d::FileOpener *)userData;
  27338. return fileOpener->exists(abs_filename.c_str());
  27339. }
  27340. inline bool customGLTFReadWholeFile(std::vector<unsigned char> *out, std::string *err,
  27341. const std::string &filepath, void *userData)
  27342. {
  27343. gl3d::FileOpener *fileOpener = (gl3d::FileOpener *)userData;
  27344. bool couldNotOpen = false;
  27345. auto rez = fileOpener->binary(filepath.c_str(), couldNotOpen);
  27346. if (couldNotOpen)
  27347. {
  27348. *err = "Error, could not open file: " + filepath;
  27349. return 0;
  27350. }
  27351. out->assign(rez.begin(), rez.end());
  27352. return 1;
  27353. }
  27354. // Class: Loader
  27355. //
  27356. // Description: The OBJ Model Loader
  27357. class Loader
  27358. {
  27359. public:
  27360. // Default Constructor
  27361. Loader() = default;
  27362. // Load a file into the loader
  27363. //
  27364. // If file is loaded return true
  27365. //
  27366. // If the file is unable to be found
  27367. // or unable to be loaded return false
  27368. bool LoadFile(std::string Path, gl3d::ErrorReporter &reporter, gl3d::FileOpener &fileOpener, bool *outShouldFlipUV = 0)
  27369. {
  27370. if (outShouldFlipUV) { *outShouldFlipUV = 0; }
  27371. // If the file is not an .obj file return false
  27372. if (Path.substr(Path.size() - 4, 4) == ".obj")
  27373. {
  27374. return loadObj(Path, reporter, fileOpener);
  27375. }
  27376. else if (Path.substr(Path.size() - 5, 5) == ".gltf")
  27377. {
  27378. if (outShouldFlipUV) { *outShouldFlipUV = 1; }
  27379. return loadGltf(Path, reporter, fileOpener, 0);
  27380. }else if (Path.substr(Path.size() - 4, 4) == ".glb")
  27381. {
  27382. if (outShouldFlipUV) { *outShouldFlipUV = 1; }
  27383. return loadGltf(Path, reporter, fileOpener, 1);
  27384. }
  27385. else
  27386. {
  27387. reporter.callErrorCallback("3D model format not supported: " + Path);
  27388. return 0;
  27389. }
  27390. }
  27391. void calculateInverseBindTransform(int index, glm::mat4 parentBindTransform, std::vector<gl3d::Joint> &joints)
  27392. {
  27393. glm::mat4 bindTransform = parentBindTransform * joints[index].localBindTransform;
  27394. joints[index].inverseBindTransform = glm::inverse(bindTransform);
  27395. for (auto &c : joints[index].children)
  27396. {
  27397. calculateInverseBindTransform(c, bindTransform, joints);
  27398. }
  27399. };
  27400. bool loadGltf(const std::string &Path, gl3d::ErrorReporter &errorReporter,
  27401. gl3d::FileOpener &fileOpener, bool glb = 0)
  27402. {
  27403. tinygltf::Model model;
  27404. tinygltf::TinyGLTF loader;
  27405. tinygltf::FsCallbacks callBacks;
  27406. callBacks.ExpandFilePath = tinygltf::ExpandFilePath;
  27407. callBacks.FileExists = customFileExists;
  27408. callBacks.ReadWholeFile = customGLTFReadWholeFile;
  27409. callBacks.WriteWholeFile = tinygltf::WriteWholeFile;
  27410. callBacks.user_data = &fileOpener;
  27411. loader.SetFsCallbacks(callBacks);
  27412. std::string err;
  27413. std::string warn;
  27414. #pragma region load and check
  27415. bool ret;
  27416. if (glb)
  27417. {
  27418. ret = loader.LoadBinaryFromFile(&model, &err, &warn, Path); // for binary glTF(.glb)
  27419. }
  27420. else
  27421. {
  27422. ret = loader.LoadASCIIFromFile(&model, &err, &warn, Path);
  27423. }
  27424. if (!warn.empty())
  27425. {
  27426. errorReporter.callErrorCallback("Warn: " + warn);
  27427. }
  27428. if (!err.empty())
  27429. {
  27430. errorReporter.callErrorCallback("Err: " + err);
  27431. }
  27432. if (!ret)
  27433. {
  27434. errorReporter.callErrorCallback("Failed to parse glTF\n");
  27435. return 0;
  27436. }
  27437. #pragma endregion
  27438. #pragma region materials
  27439. int count = 0;
  27440. LoadedMaterials.resize(model.materials.size());
  27441. for (int i = 0; i < model.materials.size(); i++)
  27442. {
  27443. auto &mat = model.materials[i];
  27444. LoadedMaterials[i].name = mat.name;
  27445. LoadedMaterials[i].Kd.x = mat.pbrMetallicRoughness.baseColorFactor[0];
  27446. LoadedMaterials[i].Kd.y = mat.pbrMetallicRoughness.baseColorFactor[1];
  27447. LoadedMaterials[i].Kd.z = mat.pbrMetallicRoughness.baseColorFactor[2];
  27448. LoadedMaterials[i].Kd.w = mat.pbrMetallicRoughness.baseColorFactor[3];
  27449. LoadedMaterials[i].metallic = mat.pbrMetallicRoughness.metallicFactor;
  27450. LoadedMaterials[i].roughness = mat.pbrMetallicRoughness.roughnessFactor;
  27451. auto MimeToExt = [](const std::string &mimeType) -> std::string
  27452. {
  27453. if (mimeType == "image/jpeg")
  27454. {
  27455. return "jpg";
  27456. }
  27457. else if (mimeType == "image/png")
  27458. {
  27459. return "png";
  27460. }
  27461. else if (mimeType == "image/bmp")
  27462. {
  27463. return "bmp";
  27464. }
  27465. else if (mimeType == "image/gif")
  27466. {
  27467. return "gif";
  27468. }
  27469. return "";
  27470. };
  27471. auto setTexture = [&](int index, LoadedTexture *t, bool checkData)
  27472. {
  27473. if (index != -1)
  27474. {
  27475. auto &texture = model.textures[index];
  27476. auto &image = model.images[texture.source];
  27477. if (t)
  27478. {
  27479. if (checkData)
  27480. {
  27481. bool isData = false;
  27482. int comp = image.component;
  27483. t->data.resize(comp * image.width * image.height);
  27484. for (int i = 0; i < image.width * image.height; i++)
  27485. {
  27486. unsigned char r = 0;
  27487. unsigned char g = 0;
  27488. unsigned char b = 0;
  27489. unsigned char a = 0;
  27490. r = image.image[i * comp + 0];
  27491. t->data[i * comp + 0] = r;
  27492. if (comp >= 2)
  27493. {
  27494. g = image.image[i * comp + 1];
  27495. t->data[i * comp + 1] = g;
  27496. }
  27497. if(comp >= 3)
  27498. {
  27499. b = image.image[i * comp + 2];
  27500. t->data[i * comp + 2] = b;
  27501. }
  27502. if (comp >= 4)
  27503. {
  27504. a = image.image[i * comp + 3];
  27505. t->data[i * comp + 3] = a;
  27506. }
  27507. if (r != 0 || g != 0 || b != 0) { isData = true; }
  27508. }
  27509. if (isData)
  27510. {
  27511. t->w = image.width;
  27512. t->h = image.height;
  27513. t->components = image.component;
  27514. }
  27515. else
  27516. {
  27517. t->data.clear();
  27518. }
  27519. }
  27520. else
  27521. {
  27522. t->w = image.width;
  27523. t->h = image.height;
  27524. t->components = image.component;
  27525. t->data = image.image;
  27526. }
  27527. }
  27528. }
  27529. };
  27530. //LoadedMaterials[i].map_Kd
  27531. setTexture(mat.pbrMetallicRoughness.baseColorTexture.index,
  27532. &LoadedMaterials[i].loadedDiffuse, false);
  27533. //LoadedMaterials[i].map_Kn
  27534. setTexture(mat.normalTexture.index,
  27535. &LoadedMaterials[i].loadedNormal, false);
  27536. //LoadedMaterials[i].map_emissive
  27537. setTexture(mat.emissiveTexture.index,
  27538. &LoadedMaterials[i].loadedEmissive, false);
  27539. //LoadedMaterials[i].map_Ka = setTexture(mat.occlusionTexture.index);
  27540. //LoadedMaterials[i].map_Pr = setTexture(mat.pbrMetallicRoughness.metallicRoughnessTexture.index);
  27541. //LoadedMaterials[i].map_Pm = setTexture(mat.pbrMetallicRoughness.metallicRoughnessTexture.index);
  27542. //LoadedMaterials[i].map_ORM
  27543. setTexture(mat.pbrMetallicRoughness.metallicRoughnessTexture.index,
  27544. &LoadedMaterials[i].loadedORM, true);
  27545. }
  27546. #pragma endregion
  27547. #pragma region bones
  27548. //int indexCount = 0;
  27549. std::vector<int> isMain; //used if the skeleton root is not specified for some wierd reason
  27550. std::vector<int> skinJoints;
  27551. std::vector<int> skeletonRoots = {};
  27552. auto convertNode = [&skinJoints](int index) -> int
  27553. {
  27554. auto convertedNode = std::find(skinJoints.begin(), skinJoints.end(), index);
  27555. if (convertedNode == skinJoints.end())
  27556. {
  27557. return -1;
  27558. }
  27559. return convertedNode - skinJoints.begin();
  27560. };
  27561. if (!model.skins.empty())
  27562. {
  27563. auto skin = model.skins[0];
  27564. joints.resize(skin.joints.size());
  27565. isMain.resize(skin.joints.size(), 1);
  27566. skinJoints.reserve(skin.joints.size());
  27567. for (auto &j : skin.joints)
  27568. {
  27569. skinJoints.push_back(j);
  27570. }
  27571. int jCount = 0;
  27572. for (auto &j : skin.joints)
  27573. {
  27574. auto &b = model.nodes[j];
  27575. //std::cout << jCount << ": " << b.name << "\n";
  27576. gl3d::Joint joint;
  27577. joint.children.reserve(b.children.size());
  27578. for (int i = 0; i < b.children.size(); i++)
  27579. {
  27580. joint.children.push_back(convertNode(b.children[i]));
  27581. }
  27582. for (auto &c : joint.children)
  27583. {
  27584. isMain[c] = 0;
  27585. }
  27586. joint.name = b.name;
  27587. glm::mat4 rotation(1.f);
  27588. glm::mat4 scale(1.f);
  27589. glm::mat4 translation(1.f);
  27590. //suppose 4 component quaternion
  27591. if (!b.rotation.empty())
  27592. {
  27593. glm::quat rot;
  27594. rot.x = b.rotation[0];
  27595. rot.y = b.rotation[1];
  27596. rot.z = b.rotation[2];
  27597. rot.w = b.rotation[3];
  27598. rotation = glm::toMat4(rot);
  27599. joint.rotation = rot;
  27600. }
  27601. //suppose 3 component translation
  27602. if (!b.translation.empty())
  27603. {
  27604. glm::vec3 trans;
  27605. trans.x = b.translation[0];
  27606. trans.y = b.translation[1];
  27607. trans.z = b.translation[2];
  27608. translation = glm::translate(trans);
  27609. joint.trans = trans;
  27610. }
  27611. //suppose 3 component scale
  27612. if (!b.scale.empty())
  27613. {
  27614. glm::vec3 s;
  27615. s.x = b.scale[0];
  27616. s.y = b.scale[1];
  27617. s.z = b.scale[2];
  27618. scale = glm::scale(s);
  27619. joint.scale = s;
  27620. }
  27621. joint.localBindTransform = translation * rotation * scale;
  27622. //glm::mat4 trans = *(glm::mat4*)b.matrix.data();
  27623. //joint.localBindTransform = trans;
  27624. tinygltf::Accessor &accessor = model.accessors[skin.inverseBindMatrices];
  27625. tinygltf::BufferView &bufferView = model.bufferViews[accessor.bufferView];
  27626. tinygltf::Buffer &buffer = model.buffers[bufferView.buffer];
  27627. float *matData = (float *)
  27628. (&buffer.data[bufferView.byteOffset + accessor.byteOffset]);
  27629. glm::mat4 inversBindMat;
  27630. for (int i = 0; i < 16; i++)
  27631. {
  27632. ((float*)&inversBindMat[0][0])[i] = matData[(jCount) * 16 + i];
  27633. }
  27634. //inversBindMat = glm::transpose(inversBindMat);
  27635. joint.inverseBindTransform = inversBindMat;
  27636. joints[convertNode(j)] = std::move(joint);
  27637. jCount++;
  27638. }
  27639. if (skin.skeleton < 0)
  27640. {
  27641. for (int i = 0; i < isMain.size(); i++)
  27642. {
  27643. if (isMain[i] == 1)
  27644. {
  27645. skeletonRoots.push_back(i);
  27646. //calculateInverseBindTransform(i, glm::mat4(1.f), joints);
  27647. //break;
  27648. };
  27649. }
  27650. }
  27651. else
  27652. {
  27653. skeletonRoots.push_back(skin.skeleton);
  27654. //calculateInverseBindTransform(skeletonRoot, glm::mat4(1.f), joints);
  27655. }
  27656. }
  27657. #pragma endregion
  27658. #pragma region animations
  27659. animations.reserve(model.animations.size());
  27660. //std::cout << model.animations.size() << " :size\n";
  27661. for (auto &a : model.animations)
  27662. {
  27663. gl3d::Animation animation;
  27664. animation.name = a.name;
  27665. animation.root = skeletonRoots;
  27666. #pragma region set key frames a default value
  27667. animation.keyFramesRot.resize(joints.size()); //each joint will potentially have keyframes
  27668. animation.keyFramesTrans.resize(joints.size()); //each joint will potentially have keyframes
  27669. animation.keyFramesScale.resize(joints.size()); //each joint will potentially have keyframes
  27670. //animation.timeStamps.resize(joints.size());
  27671. //animation.timePassed.resize(joints.size());
  27672. //animation.keyFrames.resize(joints.size());
  27673. #pragma endregion
  27674. for (int i = 0; i < a.channels.size(); i++)
  27675. {
  27676. auto &channel = a.channels[i];
  27677. auto &sampler = a.samplers[channel.sampler];
  27678. int node = convertNode(channel.target_node);
  27679. if (node == -1)
  27680. {
  27681. //std::cout << channel.target_path <<" : " << i <<"\n";
  27682. continue;
  27683. }
  27684. int type = 0; //translation rotation scale
  27685. std::vector<float> timeStamps;
  27686. {
  27687. tinygltf::Accessor &accessor = model.accessors[sampler.input];
  27688. tinygltf::BufferView &bufferView = model.bufferViews[accessor.bufferView];
  27689. tinygltf::Buffer &buffer = model.buffers[bufferView.buffer];
  27690. float *timeStamp = (float *)
  27691. (&buffer.data[bufferView.byteOffset + accessor.byteOffset]);
  27692. timeStamps.resize(accessor.count);
  27693. for (size_t i = 0; i < accessor.count; ++i)
  27694. {
  27695. timeStamps[i] = timeStamp[i];
  27696. }
  27697. }
  27698. for (auto &t : timeStamps)
  27699. {
  27700. if (t > animation.animationDuration)
  27701. {
  27702. animation.animationDuration = t;
  27703. }
  27704. }
  27705. if (channel.target_path == "translation")
  27706. {
  27707. type = 0;
  27708. animation.keyFramesTrans[node].resize(timeStamps.size());
  27709. for (int t = 0; t < timeStamps.size(); t++)
  27710. {
  27711. glm::vec3 move = {};
  27712. tinygltf::Accessor &accessor = model.accessors[sampler.output];
  27713. tinygltf::BufferView &bufferView = model.bufferViews[accessor.bufferView];
  27714. tinygltf::Buffer &buffer = model.buffers[bufferView.buffer];
  27715. float *translation = (float *)
  27716. (&buffer.data[bufferView.byteOffset + accessor.byteOffset]);
  27717. gl3dAssert(accessor.count == timeStamps.size());
  27718. move.x = translation[t * 3 + 0];
  27719. move.y = translation[t * 3 + 1];
  27720. move.z = translation[t * 3 + 2];
  27721. animation.keyFramesTrans[node][t].timeStamp = timeStamps[t];
  27722. animation.keyFramesTrans[node][t].translation = move;
  27723. }
  27724. }
  27725. else if (channel.target_path == "rotation")
  27726. {
  27727. type = 1;
  27728. animation.keyFramesRot[node].resize(timeStamps.size());
  27729. for (int t = 0; t < timeStamps.size(); t++)
  27730. {
  27731. glm::quat rot = {};
  27732. tinygltf::Accessor &accessor = model.accessors[sampler.output];
  27733. tinygltf::BufferView &bufferView = model.bufferViews[accessor.bufferView];
  27734. tinygltf::Buffer &buffer = model.buffers[bufferView.buffer];
  27735. float *rotation = (float *)
  27736. (&buffer.data[bufferView.byteOffset + accessor.byteOffset]);
  27737. gl3dAssert(accessor.count == timeStamps.size());
  27738. rot.x = rotation[t * 4 + 0];
  27739. rot.y = rotation[t * 4 + 1];
  27740. rot.z = rotation[t * 4 + 2];
  27741. rot.w = rotation[t * 4 + 3];
  27742. animation.keyFramesRot[node][t].timeStamp = timeStamps[t];
  27743. animation.keyFramesRot[node][t].rotation = rot;
  27744. }
  27745. }
  27746. else if (channel.target_path == "scale")
  27747. {
  27748. type = 2;
  27749. animation.keyFramesScale[node].resize(timeStamps.size());
  27750. for (int t = 0; t < timeStamps.size(); t++)
  27751. {
  27752. glm::vec3 scale = {};
  27753. tinygltf::Accessor &accessor = model.accessors[sampler.output];
  27754. tinygltf::BufferView &bufferView = model.bufferViews[accessor.bufferView];
  27755. tinygltf::Buffer &buffer = model.buffers[bufferView.buffer];
  27756. float *scaleBuffer = (float *)
  27757. (&buffer.data[bufferView.byteOffset + accessor.byteOffset]);
  27758. gl3dAssert(accessor.count == timeStamps.size());
  27759. scale.x = scaleBuffer[t * 3 + 0];
  27760. scale.y = scaleBuffer[t * 3 + 1];
  27761. scale.z = scaleBuffer[t * 3 + 2];
  27762. animation.keyFramesScale[node][t].timeStamp = timeStamps[t];
  27763. animation.keyFramesScale[node][t].scale = scale;
  27764. }
  27765. }
  27766. else if (channel.target_path == "weights")
  27767. {
  27768. errorReporter.callErrorCallback("weights are supported");
  27769. }
  27770. else
  27771. {
  27772. std::string message = "unknow animation target";
  27773. message += channel.target_path;
  27774. errorReporter.callErrorCallback(message);
  27775. }
  27776. }
  27777. //this code is not done
  27778. // it merges frames
  27779. // it will probably not be used
  27780. //for (int node = 0; node < animation.timeStamps.size(); node++)
  27781. //{
  27782. //
  27783. // #pragma region get all the time stamps
  27784. // std::vector<float> allFrames;
  27785. // {
  27786. // std::unordered_set<float> allFramesSet;
  27787. // allFramesSet.reserve(animation.keyFramesTrans[node].size() +
  27788. // animation.keyFramesScale[node].size() +
  27789. // animation.keyFramesRot[node].size()
  27790. // );
  27791. //
  27792. // for (auto &frame : animation.keyFramesTrans[node])
  27793. // {
  27794. // allFramesSet.insert(frame.timeStamp);
  27795. // }
  27796. // for (auto &frame : animation.keyFramesScale[node])
  27797. // {
  27798. // allFramesSet.insert(frame.timeStamp);
  27799. // }
  27800. // for (auto &frame : animation.keyFramesRot[node])
  27801. // {
  27802. // allFramesSet.insert(frame.timeStamp);
  27803. // }
  27804. //
  27805. // allFrames.reserve(allFramesSet.size());
  27806. // for (auto i : allFramesSet)
  27807. // {
  27808. // allFrames.push_back(i);
  27809. // }
  27810. //
  27811. // std::sort(allFrames.begin(), allFrames.end());
  27812. // }
  27813. // #pragma endregion
  27814. //
  27815. // animation.keyFrames[node].resize(allFrames.size());
  27816. // for (int i = 0; i < allFrames.size(); i++)
  27817. // {
  27818. // animation.keyFrames[node][i].timeStamp = allFrames[i];
  27819. // }
  27820. //
  27821. // //add translation
  27822. // for (auto &frame : animation.keyFramesTrans[node])
  27823. // {
  27824. // for (int i = 0; i < allFrames.size(); i++)
  27825. // {
  27826. // if (allFrames[i] == frame.timeStamp)
  27827. // {
  27828. // animation.keyFrames[node][i].translation = frame.translation;
  27829. // animation.keyFrames[node][i].translationSet = true;
  27830. // break;
  27831. // }
  27832. // }
  27833. // }
  27834. //
  27835. // //add rotation
  27836. // for (auto &frame : animation.keyFramesRot[node])
  27837. // {
  27838. // for (int i = 0; i < allFrames.size(); i++)
  27839. // {
  27840. // if (allFrames[i] == frame.timeStamp)
  27841. // {
  27842. // animation.keyFrames[node][i].rotation = frame.rotation;
  27843. // animation.keyFrames[node][i].rotationSet = true;
  27844. // break;
  27845. // }
  27846. // }
  27847. // }
  27848. //
  27849. // //add rotation
  27850. // for (auto &frame : animation.keyFramesRot[node])
  27851. // {
  27852. // for (int i = 0; i < allFrames.size(); i++)
  27853. // {
  27854. // if (allFrames[i] == frame.timeStamp)
  27855. // {
  27856. // animation.keyFrames[node][i].rotation = frame.rotation;
  27857. // animation.keyFrames[node][i].rotationSet = true;
  27858. // break;
  27859. // }
  27860. // }
  27861. // }
  27862. //
  27863. //}
  27864. animations.push_back(std::move(animation));
  27865. }
  27866. #pragma endregion
  27867. #pragma region meshes
  27868. if (!model.meshes.empty())
  27869. {
  27870. int meshesCount = 0;
  27871. for (int j = 0; j < model.meshes.size(); j++)
  27872. {
  27873. meshesCount += model.meshes[j].primitives.size();
  27874. }
  27875. LoadedMeshes.reserve(model.meshes.size());
  27876. for (int j = 0; j < model.meshes.size(); j++)
  27877. {
  27878. for (int i = 0; i < model.meshes[j].primitives.size(); i++)
  27879. {
  27880. Mesh m;
  27881. m.MeshName = model.meshes[j].name;
  27882. m.materialIndex = model.meshes[j].primitives[i].material;
  27883. auto &p = model.meshes[j].primitives[i];
  27884. tinygltf::Accessor &accessor = model.accessors[p.attributes["POSITION"]];
  27885. tinygltf::BufferView &bufferView = model.bufferViews[accessor.bufferView];
  27886. tinygltf::Buffer &buffer = model.buffers[bufferView.buffer];
  27887. float *positions = (float *)
  27888. (&buffer.data[bufferView.byteOffset + accessor.byteOffset]);
  27889. tinygltf::Accessor &accessorNor = model.accessors[p.attributes["NORMAL"]];
  27890. tinygltf::BufferView &bufferViewNor = model.bufferViews[accessorNor.bufferView];
  27891. tinygltf::Buffer &bufferNor = model.buffers[bufferViewNor.buffer];
  27892. float *normals = (float *)
  27893. (&bufferNor.data[bufferViewNor.byteOffset + accessorNor.byteOffset]);
  27894. tinygltf::Accessor &accessorTex = model.accessors[p.attributes["TEXCOORD_0"]];
  27895. tinygltf::BufferView &bufferViewTex = model.bufferViews[accessorTex.bufferView];
  27896. tinygltf::Buffer &bufferTex = model.buffers[bufferViewTex.buffer];
  27897. float *tex = (float *)
  27898. (&bufferTex.data[bufferViewTex.byteOffset + accessorTex.byteOffset]);
  27899. bool hasBones = 0;
  27900. //todo look into support models without texcoords
  27901. if(p.attributes.find("JOINTS_0") != p.attributes.end() &&
  27902. p.attributes.find("WEIGHTS_0") != p.attributes.end()
  27903. )
  27904. {
  27905. hasBones = 1;
  27906. tinygltf::Accessor &accessorJoints = model.accessors[p.attributes["JOINTS_0"]];
  27907. tinygltf::BufferView &bufferViewJoints = model.bufferViews[accessorJoints.bufferView];
  27908. tinygltf::Buffer &bufferJoints = model.buffers[bufferViewJoints.buffer];
  27909. tinygltf::Accessor &accessorWeights = model.accessors[p.attributes["WEIGHTS_0"]];
  27910. tinygltf::BufferView &bufferViewWeights = model.bufferViews[accessorWeights.bufferView];
  27911. tinygltf::Buffer &bufferWeights = model.buffers[bufferViewWeights.buffer];
  27912. float *weights = (float *)
  27913. (&bufferWeights.data[bufferViewWeights.byteOffset + accessorWeights.byteOffset]);
  27914. m.VerticesAnimations.reserve(accessor.count);
  27915. for (size_t i = 0; i < accessor.count; ++i)
  27916. {
  27917. glm::ivec4 jointsIndex(-1,-1,-1,-1);
  27918. glm::vec4 weightsVec{};
  27919. int componentCount = accessorJoints.ByteStride(bufferViewJoints);
  27920. switch (accessorJoints.componentType)
  27921. {
  27922. case TINYGLTF_COMPONENT_TYPE_INT:
  27923. {
  27924. componentCount /= sizeof(int);
  27925. int *joint = (int *)
  27926. (&bufferJoints.data[bufferViewJoints.byteOffset + accessorJoints.byteOffset]);
  27927. for (int j = 0; j < std::min(componentCount, 4); j++)
  27928. {
  27929. jointsIndex[j] = joint[i * componentCount + j];
  27930. weightsVec[j] = weights[i * componentCount + j];
  27931. }
  27932. break;
  27933. }
  27934. case TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT:
  27935. {
  27936. componentCount /= sizeof(unsigned int);
  27937. unsigned int *joint = (unsigned int *)
  27938. (&bufferJoints.data[bufferViewJoints.byteOffset + accessorJoints.byteOffset]);
  27939. for (int j = 0; j < std::min(componentCount, 4); j++)
  27940. {
  27941. jointsIndex[j] = joint[i * componentCount + j];
  27942. weightsVec[j] = weights[i * componentCount + j];
  27943. }
  27944. break;
  27945. }
  27946. case TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT:
  27947. {
  27948. componentCount /= sizeof(unsigned short);
  27949. unsigned short *joint = (unsigned short *)
  27950. (&bufferJoints.data[bufferViewJoints.byteOffset + accessorJoints.byteOffset]);
  27951. for (int j = 0; j < std::min(componentCount, 4); j++)
  27952. {
  27953. jointsIndex[j] = joint[i * componentCount + j];
  27954. weightsVec[j] = weights[i * componentCount + j];
  27955. }
  27956. break;
  27957. }
  27958. case TINYGLTF_COMPONENT_TYPE_SHORT:
  27959. {
  27960. componentCount /= sizeof(unsigned short);
  27961. short *joint = (short *)
  27962. (&bufferJoints.data[bufferViewJoints.byteOffset + accessorJoints.byteOffset]);
  27963. for (int j = 0; j < std::min(componentCount, 4); j++)
  27964. {
  27965. jointsIndex[j] = joint[i * componentCount + j];
  27966. weightsVec[j] = weights[i * componentCount + j];
  27967. }
  27968. break;
  27969. }
  27970. case TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE:
  27971. {
  27972. componentCount /= sizeof(unsigned char);
  27973. unsigned char* joint = (unsigned char*)
  27974. (&bufferJoints.data[bufferViewJoints.byteOffset + accessorJoints.byteOffset]);
  27975. for (int j = 0; j < std::min(componentCount, 4); j++)
  27976. {
  27977. jointsIndex[j] = joint[i * componentCount + j];
  27978. weightsVec[j] = weights[i * componentCount + j];
  27979. }
  27980. break;
  27981. }
  27982. case TINYGLTF_COMPONENT_TYPE_BYTE:
  27983. {
  27984. componentCount /= sizeof(char);
  27985. char* joint = (char*)
  27986. (&bufferJoints.data[bufferViewJoints.byteOffset + accessorJoints.byteOffset]);
  27987. for (int j = 0; j < std::min(componentCount, 4); j++)
  27988. {
  27989. jointsIndex[j] = joint[i * componentCount + j];
  27990. weightsVec[j] = weights[i * componentCount + j];
  27991. }
  27992. break;
  27993. }
  27994. default:
  27995. gl3dAssertComment(0, "model parsing error");
  27996. break;
  27997. };
  27998. for (int j = 3; j>=0; j--)
  27999. {
  28000. if (weightsVec[j] == 0.f)
  28001. {
  28002. jointsIndex[j] = -1;
  28003. }
  28004. else
  28005. {
  28006. break;
  28007. }
  28008. }
  28009. //for (int j = 0; j < 4; j++)
  28010. //{
  28011. // if (jointsIndex[j] == -1)
  28012. // {
  28013. // break;
  28014. // }
  28015. // else
  28016. // {
  28017. // jointsIndex[j] = skinJoints[jointsIndex[j]];
  28018. // }
  28019. //
  28020. //}
  28021. //if (componentCount > 4)
  28022. {
  28023. weightsVec /= weightsVec.x + weightsVec.y + weightsVec.z + weightsVec.w;
  28024. }
  28025. // Positions are Vec3 components, so for each vec3 stride, offset for x, y, and z.
  28026. float x = positions[i * 3 + 0];// x
  28027. float y = positions[i * 3 + 1];// y
  28028. float z = positions[i * 3 + 2];// z
  28029. float nx = normals[i * 3 + 0];// x
  28030. float ny = normals[i * 3 + 1];// y
  28031. float nz = normals[i * 3 + 2];// z
  28032. float s = tex[i * 2 + 0];// s
  28033. float t = tex[i * 2 + 1];// t
  28034. VertexAnimations v;
  28035. v.Position = Vector3(x, y, z);
  28036. v.Normal = Vector3(nx, ny, nz);
  28037. v.TextureCoordinate = Vector2(s, t);
  28038. v.jointsId = jointsIndex;
  28039. v.weights = weightsVec;
  28040. m.VerticesAnimations.push_back(v);
  28041. }
  28042. }
  28043. else
  28044. {
  28045. m.Vertices.reserve(accessor.count);
  28046. for (size_t i = 0; i < accessor.count; ++i)
  28047. {
  28048. // Positions are Vec3 components, so for each vec3 stride, offset for x, y, and z.
  28049. float x = positions[i * 3 + 0];// x
  28050. float y = positions[i * 3 + 1];// y
  28051. float z = positions[i * 3 + 2];// z
  28052. float nx = normals[i * 3 + 0];// x
  28053. float ny = normals[i * 3 + 1];// y
  28054. float nz = normals[i * 3 + 2];// z
  28055. float s = tex[i * 2 + 0];// s
  28056. float t = tex[i * 2 + 1];// t
  28057. Vertex v;
  28058. v.Position = Vector3(x, y, z);
  28059. v.Normal = Vector3(nx, ny, nz);
  28060. v.TextureCoordinate = Vector2(s, t);
  28061. m.Vertices.push_back(v);
  28062. }
  28063. }
  28064. tinygltf::Accessor &accessorIndices = model.accessors[p.indices];
  28065. tinygltf::BufferView &bufferViewInd = model.bufferViews[accessorIndices.bufferView];
  28066. tinygltf::Buffer &bufferInd = model.buffers[bufferViewInd.buffer];
  28067. for (int i = 0; i < accessorIndices.count; i++)
  28068. {
  28069. switch (accessorIndices.componentType)
  28070. {
  28071. case TINYGLTF_COMPONENT_TYPE_INT:
  28072. {
  28073. int *ind = (int *)
  28074. (&bufferInd.data[bufferViewInd.byteOffset + accessorIndices.byteOffset]);
  28075. m.Indices.push_back(ind[i]);
  28076. break;
  28077. }
  28078. case TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT:
  28079. {
  28080. unsigned int *ind = (unsigned int *)
  28081. (&bufferInd.data[bufferViewInd.byteOffset + accessorIndices.byteOffset]);
  28082. m.Indices.push_back(ind[i]);
  28083. break;
  28084. }
  28085. case TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT:
  28086. {
  28087. unsigned short *ind = (unsigned short *)
  28088. (&bufferInd.data[bufferViewInd.byteOffset + accessorIndices.byteOffset]);
  28089. m.Indices.push_back(ind[i]);
  28090. break;
  28091. }
  28092. case TINYGLTF_COMPONENT_TYPE_SHORT:
  28093. {
  28094. short *ind = (short *)
  28095. (&bufferInd.data[bufferViewInd.byteOffset + accessorIndices.byteOffset]);
  28096. m.Indices.push_back(ind[i]);
  28097. break;
  28098. }
  28099. case TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE:
  28100. {
  28101. unsigned char* ind = (unsigned char*)
  28102. (&bufferInd.data[bufferViewInd.byteOffset + accessorIndices.byteOffset]);
  28103. m.Indices.push_back(ind[i]);
  28104. break;
  28105. }
  28106. case TINYGLTF_COMPONENT_TYPE_BYTE:
  28107. {
  28108. char* ind = (char*)
  28109. (&bufferInd.data[bufferViewInd.byteOffset + accessorIndices.byteOffset]);
  28110. m.Indices.push_back(ind[i]);
  28111. break;
  28112. }
  28113. default:
  28114. gl3dAssertComment(0, "model parsing error");
  28115. break;
  28116. };
  28117. }
  28118. m.hasBones = hasBones;
  28119. LoadedMeshes.push_back(std::move(m)); //todo add move constructor
  28120. }
  28121. }
  28122. }
  28123. #pragma endregion
  28124. return 1;
  28125. }
  28126. bool loadObj(const std::string &Path, gl3d::ErrorReporter &repoter, gl3d::FileOpener &fileOpener)
  28127. {
  28128. bool couldNotOpen = 0;
  28129. auto fileContent = fileOpener(Path.c_str(), couldNotOpen);
  28130. if (couldNotOpen)
  28131. return false;
  28132. std::stringstream stream;
  28133. stream.str(std::move(fileContent));
  28134. //todo delete materials or make sure you can't load over things
  28135. LoadedMeshes.clear();
  28136. std::vector<Vector3> Positions;
  28137. std::vector<Vector2> TCoords;
  28138. std::vector<Vector3> Normals;
  28139. std::vector<Vertex> Vertices;
  28140. std::vector<unsigned int> Indices;
  28141. std::vector<std::string> MeshMatNames;
  28142. bool listening = false;
  28143. std::string meshname;
  28144. Mesh tempMesh;
  28145. #ifdef OBJL_CONSOLE_OUTPUT
  28146. const unsigned int outputEveryNth = 1000;
  28147. unsigned int outputIndicator = outputEveryNth;
  28148. #endif
  28149. std::string curline;
  28150. while (std::getline(stream, curline))
  28151. {
  28152. #ifdef OBJL_CONSOLE_OUTPUT
  28153. if ((outputIndicator = ((outputIndicator + 1) % outputEveryNth)) == 1)
  28154. {
  28155. if (!meshname.empty())
  28156. {
  28157. std::cout
  28158. << "\r- " << meshname
  28159. << "\t| vertices > " << Positions.size()
  28160. << "\t| texcoords > " << TCoords.size()
  28161. << "\t| normals > " << Normals.size()
  28162. << "\t| triangles > " << (Vertices.size() / 3)
  28163. << (!MeshMatNames.empty() ? "\t| material: " + MeshMatNames.back() : "");
  28164. }
  28165. }
  28166. #endif
  28167. // Generate a Mesh Object or Prepare for an object to be created
  28168. if (algorithm::firstToken(curline) == "o" || algorithm::firstToken(curline) == "g" || curline[0] == 'g')
  28169. {
  28170. if (!listening)
  28171. {
  28172. listening = true;
  28173. if (algorithm::firstToken(curline) == "o" || algorithm::firstToken(curline) == "g")
  28174. {
  28175. meshname = algorithm::tail(curline);
  28176. }
  28177. else
  28178. {
  28179. meshname = "unnamed";
  28180. }
  28181. }
  28182. else
  28183. {
  28184. // Generate the mesh to put into the array
  28185. if (!Indices.empty() && !Vertices.empty())
  28186. {
  28187. // Create Mesh
  28188. tempMesh = Mesh(Vertices, Indices);
  28189. tempMesh.MeshName = meshname;
  28190. // Insert Mesh
  28191. LoadedMeshes.push_back(tempMesh);
  28192. // Cleanup
  28193. Vertices.clear();
  28194. Indices.clear();
  28195. meshname.clear();
  28196. meshname = algorithm::tail(curline);
  28197. }
  28198. else
  28199. {
  28200. if (algorithm::firstToken(curline) == "o" || algorithm::firstToken(curline) == "g")
  28201. {
  28202. meshname = algorithm::tail(curline);
  28203. }
  28204. else
  28205. {
  28206. meshname = "unnamed";
  28207. }
  28208. }
  28209. }
  28210. #ifdef OBJL_CONSOLE_OUTPUT
  28211. std::cout << std::endl;
  28212. outputIndicator = 0;
  28213. #endif
  28214. }
  28215. // Generate a Vertex Position
  28216. if (algorithm::firstToken(curline) == "v")
  28217. {
  28218. std::vector<std::string> spos;
  28219. Vector3 vpos;
  28220. algorithm::split2(algorithm::tail(curline), spos, ' ');
  28221. vpos.X = std::stof(spos[0]);
  28222. vpos.Y = std::stof(spos[1]);
  28223. vpos.Z = std::stof(spos[2]);
  28224. Positions.push_back(vpos);
  28225. }
  28226. // Generate a Vertex Texture Coordinate
  28227. if (algorithm::firstToken(curline) == "vt")
  28228. {
  28229. std::vector<std::string> stex;
  28230. Vector2 vtex;
  28231. algorithm::split2(algorithm::tail(curline), stex, ' ');
  28232. vtex.X = std::stof(stex[0]);
  28233. vtex.Y = std::stof(stex[1]);
  28234. TCoords.push_back(vtex);
  28235. }
  28236. // Generate a Vertex Normal;
  28237. if (algorithm::firstToken(curline) == "vn")
  28238. {
  28239. std::vector<std::string> snor;
  28240. Vector3 vnor;
  28241. algorithm::split2(algorithm::tail(curline), snor, ' ');
  28242. vnor.X = std::stof(snor[0]);
  28243. vnor.Y = std::stof(snor[1]);
  28244. vnor.Z = std::stof(snor[2]);
  28245. Normals.push_back(vnor);
  28246. }
  28247. // Generate a Face (vertices & indices)
  28248. if (algorithm::firstToken(curline) == "f")
  28249. {
  28250. // Generate the vertices
  28251. std::vector<Vertex> vVerts;
  28252. GenVerticesFromRawOBJ(vVerts, Positions, TCoords, Normals, curline);
  28253. // Add Vertices
  28254. for (int i = 0; i < int(vVerts.size()); i++)
  28255. {
  28256. Vertices.push_back(vVerts[i]);
  28257. }
  28258. std::vector<unsigned int> iIndices;
  28259. VertexTriangluation(iIndices, vVerts);
  28260. // Add Indices
  28261. for (int i = 0; i < int(iIndices.size()); i++)
  28262. {
  28263. unsigned int indnum = (unsigned int)((Vertices.size()) - vVerts.size()) + iIndices[i];
  28264. Indices.push_back(indnum);
  28265. //indnum = (unsigned int)((LoadedVertices.size()) - vVerts.size()) + iIndices[i];
  28266. //LoadedIndices.push_back(indnum);
  28267. }
  28268. }
  28269. // Get Mesh Material Name
  28270. if (algorithm::firstToken(curline) == "usemtl")
  28271. {
  28272. MeshMatNames.push_back(algorithm::tail(curline));
  28273. // Create new Mesh, if Material changes within a group
  28274. if (!Indices.empty() && !Vertices.empty())
  28275. {
  28276. // Create Mesh
  28277. tempMesh = Mesh(Vertices, Indices);
  28278. tempMesh.MeshName = meshname;
  28279. int i = 2;
  28280. while (1)
  28281. {
  28282. tempMesh.MeshName = meshname + "_" + std::to_string(i);
  28283. for (auto &m : LoadedMeshes)
  28284. if (m.MeshName == tempMesh.MeshName)
  28285. continue;
  28286. break;
  28287. }
  28288. // Insert Mesh
  28289. LoadedMeshes.push_back(tempMesh);
  28290. // Cleanup
  28291. Vertices.clear();
  28292. Indices.clear();
  28293. }
  28294. #ifdef OBJL_CONSOLE_OUTPUT
  28295. outputIndicator = 0;
  28296. #endif
  28297. }
  28298. // Load Materials
  28299. if (algorithm::firstToken(curline) == "mtllib")
  28300. {
  28301. // Generate LoadedMaterial
  28302. // Generate a path to the material file
  28303. std::vector<std::string> temp;
  28304. algorithm::split2(Path, temp, '/');
  28305. std::string pathtomat = "";
  28306. if (temp.size() != 1)
  28307. {
  28308. for (int i = 0; i < temp.size() - 1; i++)
  28309. {
  28310. pathtomat += temp[i] + "/";
  28311. }
  28312. }
  28313. pathtomat += algorithm::tail(curline);
  28314. #ifdef OBJL_CONSOLE_OUTPUT
  28315. std::cout << std::endl << "- find materials in: " << pathtomat << std::endl;
  28316. #endif
  28317. // Load Materials
  28318. LoadMaterials(pathtomat, repoter, fileOpener);
  28319. }
  28320. }
  28321. #ifdef OBJL_CONSOLE_OUTPUT
  28322. std::cout << std::endl;
  28323. #endif
  28324. // Deal with last mesh
  28325. if (!Indices.empty() && !Vertices.empty())
  28326. {
  28327. // Create Mesh
  28328. tempMesh = Mesh(Vertices, Indices);
  28329. tempMesh.MeshName = meshname;
  28330. // Insert Mesh
  28331. LoadedMeshes.push_back(tempMesh);
  28332. }
  28333. stream.clear();
  28334. // Set Materials for each Mesh
  28335. for (int i = 0; i < MeshMatNames.size(); i++)
  28336. {
  28337. std::string matname = MeshMatNames[i];
  28338. // Find corresponding material name in loaded materials
  28339. // when found copy material variables into mesh material
  28340. for (int j = 0; j < LoadedMaterials.size(); j++)
  28341. {
  28342. if (LoadedMaterials[j].name == matname)
  28343. {
  28344. //LoadedMeshes[i].MeshMaterial = LoadedMaterials[j];
  28345. LoadedMeshes[i].materialIndex = j;
  28346. break;
  28347. }
  28348. }
  28349. }
  28350. if (LoadedMeshes.empty())
  28351. {
  28352. return false;
  28353. }
  28354. else
  28355. {
  28356. return true;
  28357. }
  28358. }
  28359. // Loaded Mesh Objects
  28360. std::vector<Mesh> LoadedMeshes;
  28361. std::vector<gl3d::Joint> joints;
  28362. std::vector<gl3d::Animation> animations;
  28363. // Loaded Material Objects
  28364. std::vector<Material> LoadedMaterials;
  28365. // Load Materials from .mtl file
  28366. bool LoadMaterials(std::string path, gl3d::ErrorReporter &errorReporter, gl3d::FileOpener &fileOpener)
  28367. {
  28368. // If the file is not a material file return false
  28369. if (path.substr(path.size() - 4, path.size()) != ".mtl")
  28370. return false;
  28371. bool couldNotOpen = 0;
  28372. auto fileContent = fileOpener(path.c_str(), couldNotOpen);
  28373. if (couldNotOpen)
  28374. {
  28375. errorReporter.callErrorCallback("error loading mtl file: " + path);
  28376. return false;
  28377. }
  28378. std::stringstream stream;
  28379. stream.str(std::move(fileContent));
  28380. Material tempMaterial;
  28381. bool listening = false;
  28382. // Go through each line looking for material variables
  28383. std::string curline;
  28384. while (std::getline(stream, curline))
  28385. {
  28386. auto firstToken = algorithm::firstToken(curline);
  28387. // new material and material name
  28388. if (firstToken == "newmtl")
  28389. {
  28390. if (!listening)
  28391. {
  28392. listening = true;
  28393. if (curline.size() > 7)
  28394. {
  28395. tempMaterial.name = algorithm::tail(curline);
  28396. }
  28397. else
  28398. {
  28399. tempMaterial.name = "none";
  28400. }
  28401. }
  28402. else
  28403. {
  28404. // Generate the material
  28405. // Push Back loaded Material
  28406. LoadedMaterials.push_back(tempMaterial);
  28407. // Clear Loaded Material
  28408. tempMaterial = Material();
  28409. if (curline.size() > 7)
  28410. {
  28411. tempMaterial.name = algorithm::tail(curline);
  28412. }
  28413. else
  28414. {
  28415. tempMaterial.name = "none";
  28416. }
  28417. }
  28418. }
  28419. else
  28420. // Ambient Color
  28421. if (firstToken == "Ka")
  28422. {
  28423. std::vector<std::string> temp;
  28424. algorithm::split2(algorithm::tail(curline), temp, ' ');
  28425. if (temp.size() != 3)
  28426. continue;
  28427. tempMaterial.Ka.X = std::stof(temp[0]);
  28428. tempMaterial.Ka.Y = std::stof(temp[1]);
  28429. tempMaterial.Ka.Z = std::stof(temp[2]);
  28430. }
  28431. else
  28432. // Diffuse Color
  28433. if (firstToken == "Kd")
  28434. {
  28435. std::vector<std::string> temp;
  28436. algorithm::split2(algorithm::tail(curline), temp, ' ');
  28437. if (temp.size() != 3 && temp.size() != 4)
  28438. continue;
  28439. if (temp.size() == 3)
  28440. {
  28441. temp.push_back("1.f");
  28442. }
  28443. tempMaterial.Kd.x = std::stof(temp[0]);
  28444. tempMaterial.Kd.y = std::stof(temp[1]);
  28445. tempMaterial.Kd.z = std::stof(temp[2]);
  28446. tempMaterial.Kd.w = std::stof(temp[3]);
  28447. }
  28448. else
  28449. // Specular Color
  28450. //if (algorithm::firstToken(curline) == "Ks")
  28451. //{
  28452. // std::vector<std::string> temp;
  28453. // algorithm::split2(algorithm::tail(curline), temp, ' ');
  28454. //
  28455. // if (temp.size() != 3)
  28456. // continue;
  28457. //
  28458. // tempMaterial.Ks.X = std::stof(temp[0]);
  28459. // tempMaterial.Ks.Y = std::stof(temp[1]);
  28460. // tempMaterial.Ks.Z = std::stof(temp[2]);
  28461. //}
  28462. //else
  28463. // Specular Exponent
  28464. //if (algorithm::firstToken(curline) == "Ns")
  28465. //{
  28466. // tempMaterial.Ns = std::stof(algorithm::tail(curline));
  28467. //}
  28468. //else
  28469. // Optical Density
  28470. //if (algorithm::firstToken(curline) == "Ni")
  28471. //{
  28472. // tempMaterial.Ni = std::stof(algorithm::tail(curline));
  28473. //}
  28474. //else
  28475. //// Dissolve
  28476. //if (algorithm::firstToken(curline) == "d")
  28477. //{
  28478. // tempMaterial.d = std::stof(algorithm::tail(curline));
  28479. //}
  28480. //else
  28481. //// Illumination
  28482. //if (algorithm::firstToken(curline) == "illum")
  28483. //{
  28484. // tempMaterial.illum = std::stoi(algorithm::tail(curline));
  28485. //}
  28486. //else
  28487. if (firstToken == "Pm")
  28488. {
  28489. tempMaterial.metallic = std::stoi(algorithm::tail(curline));
  28490. }
  28491. else
  28492. if (firstToken == "Pr")
  28493. {
  28494. tempMaterial.roughness = std::stoi(algorithm::tail(curline));
  28495. }
  28496. else
  28497. if (firstToken == "Ao")
  28498. {
  28499. tempMaterial.ao = std::stoi(algorithm::tail(curline));
  28500. }
  28501. else
  28502. // Ambient Texture Map
  28503. if (firstToken == "map_Ka" ||
  28504. firstToken == "map_Ao")
  28505. {
  28506. tempMaterial.map_Ka = algorithm::tail(curline);
  28507. }
  28508. else
  28509. // Diffuse Texture Map
  28510. if (firstToken == "map_Kd")
  28511. {
  28512. tempMaterial.map_Kd = algorithm::tail(curline);
  28513. }
  28514. else
  28515. // Specular Texture Map
  28516. //if (firstToken == "map_Ks")
  28517. //{
  28518. // tempMaterial.map_Ks = algorithm::tail(curline);
  28519. //}
  28520. //else
  28521. //// Specular Hightlight Map
  28522. //if (firstToken == "map_Ns")
  28523. //{
  28524. // tempMaterial.map_Ns = algorithm::tail(curline);
  28525. //}
  28526. //else
  28527. // Alpha Texture Map
  28528. //if (firstToken == "map_d")
  28529. //{
  28530. // tempMaterial.map_d = algorithm::tail(curline);
  28531. //}
  28532. //else
  28533. // Bump Map
  28534. //if (algorithm::firstToken(curline) == "map_Bump" || algorithm::firstToken(curline) == "map_bump" || algorithm::firstToken(curline) == "bump")
  28535. //{
  28536. // tempMaterial.map_bump = algorithm::tail(curline);
  28537. //}
  28538. //else
  28539. // Normal Map
  28540. if ( firstToken == "map_Kn"
  28541. || firstToken == "norm"
  28542. || firstToken == "Norm"
  28543. )
  28544. {
  28545. tempMaterial.map_Kn = algorithm::tail(curline);
  28546. }
  28547. else
  28548. // Roughness Map
  28549. if (firstToken == "map_Pr")
  28550. {
  28551. tempMaterial.map_Pr = algorithm::tail(curline);
  28552. }
  28553. else
  28554. // Metallic Map
  28555. if (firstToken == "map_Pm")
  28556. {
  28557. tempMaterial.map_Pm = algorithm::tail(curline);
  28558. }
  28559. else
  28560. if (firstToken == "map_ORM" || firstToken == "map_orm" || firstToken == "map_Orm")
  28561. {
  28562. tempMaterial.map_ORM = algorithm::tail(curline);
  28563. }
  28564. else
  28565. if (firstToken == "map_RMA" || firstToken == "map_rma" || firstToken == "map_Rma")
  28566. {
  28567. tempMaterial.map_RMA = algorithm::tail(curline);
  28568. }
  28569. else
  28570. if (firstToken == "map_emissive" || firstToken == "map_Ke" || firstToken == "map_Emissive")
  28571. {
  28572. tempMaterial.map_emissive = algorithm::tail(curline);
  28573. }
  28574. }
  28575. // Deal with last material
  28576. // Push Back loaded Material
  28577. LoadedMaterials.push_back(tempMaterial);
  28578. // Test to see if anything was loaded
  28579. // If not return false
  28580. if (LoadedMaterials.empty())
  28581. return false;
  28582. // If so return true
  28583. else
  28584. return true;
  28585. }
  28586. private:
  28587. // Generate vertices from a list of positions,
  28588. // tcoords, normals and a face line
  28589. void GenVerticesFromRawOBJ(std::vector<Vertex>& oVerts,
  28590. const std::vector<Vector3>& iPositions,
  28591. const std::vector<Vector2>& iTCoords,
  28592. const std::vector<Vector3>& iNormals,
  28593. std::string icurline)
  28594. {
  28595. std::vector<std::string> sface, svert;
  28596. Vertex vVert;
  28597. algorithm::split2(algorithm::tail(icurline), sface, ' ');
  28598. bool noNormal = false;
  28599. // For every given vertex do this
  28600. for (int i = 0; i < int(sface.size()); i++)
  28601. {
  28602. // See What type the vertex is.
  28603. int vtype;
  28604. algorithm::split2(sface[i], svert, '/');
  28605. // Check for just position - v1
  28606. if (svert.size() == 1)
  28607. {
  28608. // Only position
  28609. vtype = 1;
  28610. }
  28611. // Check for position & texture - v1/vt1
  28612. if (svert.size() == 2)
  28613. {
  28614. // Position & Texture
  28615. vtype = 2;
  28616. }
  28617. // Check for Position, Texture and Normal - v1/vt1/vn1
  28618. // or if Position and Normal - v1//vn1
  28619. if (svert.size() == 3)
  28620. {
  28621. if (svert[1] != "")
  28622. {
  28623. // Position, Texture, and Normal
  28624. vtype = 4;
  28625. }
  28626. else
  28627. {
  28628. // Position & Normal
  28629. vtype = 3;
  28630. }
  28631. }
  28632. // Calculate and store the vertex
  28633. switch (vtype)
  28634. {
  28635. case 1: // P
  28636. {
  28637. vVert.Position = algorithm::getElement(iPositions, svert[0]);
  28638. vVert.TextureCoordinate = Vector2(0, 0);
  28639. noNormal = true;
  28640. oVerts.push_back(vVert);
  28641. break;
  28642. }
  28643. case 2: // P/T
  28644. {
  28645. vVert.Position = algorithm::getElement(iPositions, svert[0]);
  28646. vVert.TextureCoordinate = algorithm::getElement(iTCoords, svert[1]);
  28647. noNormal = true;
  28648. oVerts.push_back(vVert);
  28649. break;
  28650. }
  28651. case 3: // P//N
  28652. {
  28653. vVert.Position = algorithm::getElement(iPositions, svert[0]);
  28654. vVert.TextureCoordinate = Vector2(0, 0);
  28655. vVert.Normal = algorithm::getElement(iNormals, svert[2]);
  28656. oVerts.push_back(vVert);
  28657. break;
  28658. }
  28659. case 4: // P/T/N
  28660. {
  28661. vVert.Position = algorithm::getElement(iPositions, svert[0]);
  28662. vVert.TextureCoordinate = algorithm::getElement(iTCoords, svert[1]);
  28663. vVert.Normal = algorithm::getElement(iNormals, svert[2]);
  28664. oVerts.push_back(vVert);
  28665. break;
  28666. }
  28667. default:
  28668. {
  28669. break;
  28670. }
  28671. }
  28672. }
  28673. // take care of missing normals
  28674. // these may not be truly acurate but it is the
  28675. // best they get for not compiling a mesh with normals
  28676. if (noNormal)
  28677. {
  28678. Vector3 A = oVerts[0].Position - oVerts[1].Position;
  28679. Vector3 B = oVerts[2].Position - oVerts[1].Position;
  28680. Vector3 normal = math::CrossV3(B, A); //note vlod: changed order
  28681. float size = normal.X * normal.X + normal.Y * normal.Y + normal.Z * normal.Z;
  28682. size = sqrt(size);
  28683. normal.X /= size;
  28684. normal.Y /= size;
  28685. normal.Z /= size;
  28686. //note Vlod: normalized
  28687. for (int i = 0; i < int(oVerts.size()); i++)
  28688. {
  28689. oVerts[i].Normal = normal;
  28690. }
  28691. }
  28692. }
  28693. // Triangulate a list of vertices into a face by printing
  28694. // inducies corresponding with triangles within it
  28695. void VertexTriangluation(std::vector<unsigned int>& oIndices,
  28696. const std::vector<Vertex>& iVerts)
  28697. {
  28698. // If there are 2 or less verts,
  28699. // no triangle can be created,
  28700. // so exit
  28701. if (iVerts.size() < 3)
  28702. {
  28703. return;
  28704. }
  28705. // If it is a triangle no need to calculate it
  28706. if (iVerts.size() == 3)
  28707. {
  28708. oIndices.push_back(0);
  28709. oIndices.push_back(1);
  28710. oIndices.push_back(2);
  28711. return;
  28712. }
  28713. // Create a list of vertices
  28714. std::vector<Vertex> tVerts = iVerts;
  28715. while (true)
  28716. {
  28717. // For every vertex
  28718. for (int i = 0; i < int(tVerts.size()); i++)
  28719. {
  28720. // pPrev = the previous vertex in the list
  28721. Vertex pPrev;
  28722. if (i == 0)
  28723. {
  28724. pPrev = tVerts[tVerts.size() - 1];
  28725. }
  28726. else
  28727. {
  28728. pPrev = tVerts[i - 1];
  28729. }
  28730. // pCur = the current vertex;
  28731. Vertex pCur = tVerts[i];
  28732. // pNext = the next vertex in the list
  28733. Vertex pNext;
  28734. if (i == tVerts.size() - 1)
  28735. {
  28736. pNext = tVerts[0];
  28737. }
  28738. else
  28739. {
  28740. pNext = tVerts[i + 1];
  28741. }
  28742. // Check to see if there are only 3 verts left
  28743. // if so this is the last triangle
  28744. if (tVerts.size() == 3)
  28745. {
  28746. // Create a triangle from pCur, pPrev, pNext
  28747. for (int j = 0; j < int(tVerts.size()); j++)
  28748. {
  28749. if (iVerts[j].Position == pCur.Position)
  28750. oIndices.push_back(j);
  28751. if (iVerts[j].Position == pPrev.Position)
  28752. oIndices.push_back(j);
  28753. if (iVerts[j].Position == pNext.Position)
  28754. oIndices.push_back(j);
  28755. }
  28756. tVerts.clear();
  28757. break;
  28758. }
  28759. if (tVerts.size() == 4)
  28760. {
  28761. // Create a triangle from pCur, pPrev, pNext
  28762. for (int j = 0; j < int(iVerts.size()); j++)
  28763. {
  28764. if (iVerts[j].Position == pCur.Position)
  28765. oIndices.push_back(j);
  28766. if (iVerts[j].Position == pPrev.Position)
  28767. oIndices.push_back(j);
  28768. if (iVerts[j].Position == pNext.Position)
  28769. oIndices.push_back(j);
  28770. }
  28771. Vector3 tempVec;
  28772. for (int j = 0; j < int(tVerts.size()); j++)
  28773. {
  28774. if (tVerts[j].Position != pCur.Position
  28775. && tVerts[j].Position != pPrev.Position
  28776. && tVerts[j].Position != pNext.Position)
  28777. {
  28778. tempVec = tVerts[j].Position;
  28779. break;
  28780. }
  28781. }
  28782. // Create a triangle from pCur, pPrev, pNext
  28783. for (int j = 0; j < int(iVerts.size()); j++)
  28784. {
  28785. if (iVerts[j].Position == pPrev.Position)
  28786. oIndices.push_back(j);
  28787. if (iVerts[j].Position == pNext.Position)
  28788. oIndices.push_back(j);
  28789. if (iVerts[j].Position == tempVec)
  28790. oIndices.push_back(j);
  28791. }
  28792. tVerts.clear();
  28793. break;
  28794. }
  28795. // If Vertex is not an interior vertex
  28796. float angle = math::AngleBetweenV3(pPrev.Position - pCur.Position, pNext.Position - pCur.Position) * (180 / 3.14159265359);
  28797. if (angle <= 0 && angle >= 180)
  28798. continue;
  28799. // If any vertices are within this triangle
  28800. bool inTri = false;
  28801. for (int j = 0; j < int(iVerts.size()); j++)
  28802. {
  28803. if (algorithm::inTriangle(iVerts[j].Position, pPrev.Position, pCur.Position, pNext.Position)
  28804. && iVerts[j].Position != pPrev.Position
  28805. && iVerts[j].Position != pCur.Position
  28806. && iVerts[j].Position != pNext.Position)
  28807. {
  28808. inTri = true;
  28809. break;
  28810. }
  28811. }
  28812. if (inTri)
  28813. continue;
  28814. // Create a triangle from pCur, pPrev, pNext
  28815. for (int j = 0; j < int(iVerts.size()); j++)
  28816. {
  28817. if (iVerts[j].Position == pCur.Position)
  28818. oIndices.push_back(j);
  28819. if (iVerts[j].Position == pPrev.Position)
  28820. oIndices.push_back(j);
  28821. if (iVerts[j].Position == pNext.Position)
  28822. oIndices.push_back(j);
  28823. }
  28824. // Delete pCur from the list
  28825. for (int j = 0; j < int(tVerts.size()); j++)
  28826. {
  28827. if (tVerts[j].Position == pCur.Position)
  28828. {
  28829. tVerts.erase(tVerts.begin() + j);
  28830. break;
  28831. }
  28832. }
  28833. // reset i to the start
  28834. // -1 since loop will add 1 to it
  28835. i = -1;
  28836. }
  28837. // if no triangles were created
  28838. if (oIndices.size() == 0)
  28839. break;
  28840. // if no more vertices
  28841. if (tVerts.size() == 0)
  28842. break;
  28843. }
  28844. }
  28845. };
  28846. }
  28847. #pragma endregion
  28848. ////////////////////////////////////////////////
  28849. //Texture.h
  28850. ////////////////////////////////////////////////
  28851. #pragma region Texture
  28852. #pragma once
  28853. #include <glm/vec2.hpp>
  28854. #include <string>
  28855. namespace gl3d
  28856. {
  28857. enum TextureLoadQuality
  28858. {
  28859. dontSet = -1, //won't create mipmap
  28860. leastPossible = 0,
  28861. linearNoMipmap,
  28862. nearestMipmap,
  28863. linearMipmap,
  28864. maxQuality
  28865. };
  28866. struct GpuTexture
  28867. {
  28868. GLuint id = 0;
  28869. GpuTexture() = default;
  28870. //GpuTexture(const char *file) { loadTextureFromFile(file); };
  28871. //returns error
  28872. std::string loadTextureFromFile(const char *file, FileOpener &fileOpener, int quality, int channels = 4);
  28873. void loadTextureFromMemory(void* data, int w, int h, int chanels = 4, int quality = maxQuality);
  28874. void loadTextureFromMemoryAndCheckAlpha
  28875. (void *data, int w, int h, int &alpha, int &alphaWithData, int chanels = 4, int quality = maxQuality);
  28876. //returns error
  28877. std::string loadTextureFromFileAndCheckAlpha(const char* file, int& alpha, int& alphaData,
  28878. int quality, int channels = 4);
  28879. void clear();
  28880. void setTextureQuality(int quality);
  28881. int getTextureQuality();
  28882. glm::ivec2 getTextureSize();
  28883. };
  28884. namespace internal
  28885. {
  28886. struct GpuTextureWithFlags
  28887. {
  28888. GpuTextureWithFlags() = default;
  28889. GpuTexture texture;
  28890. unsigned int flags = {};
  28891. GL3D_ADD_FLAG(alphaExists, setAlphaExists, 0); //has alpha
  28892. GL3D_ADD_FLAG(alphaWithData, setAlphaWithData, 1); //will contribute to transparency else just discard fragments with alpha 0
  28893. };
  28894. };
  28895. void gausianBlurRGB(unsigned char *data, int w, int h, int kernel);
  28896. };
  28897. #pragma endregion
  28898. ////////////////////////////////////////////////
  28899. //Shader.h
  28900. ////////////////////////////////////////////////
  28901. #pragma region Shader
  28902. #pragma once
  28903. #include <glm\mat4x4.hpp>
  28904. #include <vector>
  28905. #include <string>
  28906. namespace gl3d
  28907. {
  28908. namespace internal
  28909. {
  28910. enum ShaderStorageBlockBindings
  28911. {
  28912. MaterialBlockBinding = 0,
  28913. PointLightsBlockBinding = 1,
  28914. DirectionalLightsBlockBinding = 2,
  28915. SpotLightsBlockBinding = 3,
  28916. JointsTransformBlockBinding = 4,
  28917. };
  28918. enum UniformBlockBindings
  28919. {
  28920. LightPassDataBlockBinding = 0,
  28921. SSAODataBlockBinding = 1,
  28922. FXAADataBlockBinding = 2,
  28923. };
  28924. }
  28925. struct Shader
  28926. {
  28927. GLuint id = 0;
  28928. bool loadShaderProgramFromFile(const char *vertexShader, const char *fragmentShader, ErrorReporter &errorReporter, FileOpener &fileOpener);
  28929. bool loadShaderProgramFromFile(const char *vertexShader,
  28930. const char *geometryShader, const char *fragmentShader, ErrorReporter &errorReporter, FileOpener &fileOpener);
  28931. void bind();
  28932. void clear();
  28933. };
  28934. GLint getUniform(GLuint id, const char *name, ErrorReporter &errorReporter);
  28935. //todo this will probably dissapear
  28936. struct LightShader
  28937. {
  28938. std::string create(ErrorReporter &errorReporter, FileOpener &fileOpener, const char *BRDFIntegrationMapFileLocation);
  28939. void getSubroutines(ErrorReporter &errorReporter);
  28940. struct
  28941. {
  28942. GLuint quadBuffer = 0;
  28943. GLuint quadVAO = 0;
  28944. }quadDrawer;
  28945. GLint u_transform = -1;
  28946. GLint u_hasAnimations = -1;
  28947. GLint u_modelTransform = -1;
  28948. GLint u_motelViewTransform = -1;
  28949. GLint normalShaderLightposLocation = -1;
  28950. GLint normalMapSamplerLocation = -1;
  28951. GLint eyePositionLocation = -1;
  28952. GLint skyBoxSamplerLocation = -1;
  28953. GLint gamaLocation = -1;
  28954. GLint pointLightCountLocation = -1;
  28955. GLint pointLightBufferLocation = -1;
  28956. GLint materialIndexLocation = -1;
  28957. GLint light_u_normals = -1;
  28958. GLint light_u_skyboxFiltered = -1;
  28959. GLint light_u_eyePosition = -1;
  28960. GLint light_u_pointLightCount = -1;
  28961. GLint light_u_directionalLightCount = -1;
  28962. GLint light_u_spotLightCount = -1;
  28963. GLint light_u_skyboxIradiance = -1;
  28964. GLint light_u_brdfTexture = -1;
  28965. GLint light_u_cascades = -1;
  28966. GLint light_u_spotShadows = -1;
  28967. GLint light_u_pointShadows = -1;
  28968. GLint light_u_materialIndex = -1;
  28969. GLint light_u_textureUV = -1;
  28970. GLint light_u_textureDerivates = -1;
  28971. GLint light_u_lastTexture = -1;
  28972. GLint light_u_positionViewSpace = -1;
  28973. GLint light_u_transparentPass = -1;
  28974. GLint light_u_hasLastFrameTexture = -1;
  28975. GLint light_u_cameraProjection = -1;
  28976. GLint light_u_view = -1;
  28977. GLint light_u_inverseView = -1;
  28978. GLint light_materialBlockLocation = GL_INVALID_INDEX;
  28979. GLuint materialBlockLocation = GL_INVALID_INDEX;
  28980. GLuint materialBlockBuffer = 0;
  28981. GLuint u_jointTransforms = GL_INVALID_INDEX;
  28982. GLuint pointLightsBlockLocation = GL_INVALID_INDEX;
  28983. GLuint pointLightsBlockBuffer = 0;
  28984. GLuint directionalLightsBlockLocation = GL_INVALID_INDEX;
  28985. GLuint directionalLightsBlockBuffer = 0;
  28986. GLuint spotLightsBlockLocation = GL_INVALID_INDEX;
  28987. GLuint spotLightsBlockBuffer = 0;
  28988. GLint normalSubroutineLocation = -1;
  28989. GLuint normalSubroutine_noMap = GL_INVALID_INDEX;
  28990. GLuint normalSubroutine_normalMap = GL_INVALID_INDEX;
  28991. GLuint materialSubroutine_functions[8] = {
  28992. GL_INVALID_INDEX, GL_INVALID_INDEX, GL_INVALID_INDEX, GL_INVALID_INDEX,
  28993. GL_INVALID_INDEX, GL_INVALID_INDEX, GL_INVALID_INDEX, GL_INVALID_INDEX,
  28994. };
  28995. //todo refactor and move things here
  28996. struct
  28997. {
  28998. //the uniform block stuff
  28999. GLuint u_lightPassData;
  29000. GLuint lightPassDataBlockBuffer;
  29001. //
  29002. }lightPassShaderData;
  29003. //to pass to the shader as an uniform block (light pass shader)
  29004. struct LightPassData
  29005. {
  29006. glm::vec4 ambientLight = glm::vec4(1, 1, 1, 0); //last value is not used
  29007. float bloomTresshold = 0.91f;
  29008. int lightSubScater = 1;
  29009. float exposure = 1.7;
  29010. int skyBoxPresent = 0;
  29011. struct SSRdata
  29012. {
  29013. float minRayStep = 0.4;
  29014. int maxSteps = 20;
  29015. int numBinarySearchSteps = 7;
  29016. float maxRayStep = 1.75;
  29017. float maxRayDelta = 0.6;
  29018. void setLowQuality()
  29019. { *this = SSRdata(); };
  29020. void setMediumQuality()
  29021. {
  29022. minRayStep = 0.4;
  29023. maxSteps = 40;
  29024. numBinarySearchSteps = 5;
  29025. maxRayStep = 1.2;
  29026. maxRayDelta = 0.6;
  29027. };
  29028. }SSR;
  29029. }lightPassUniformBlockCpuData;
  29030. struct
  29031. {
  29032. Shader shader;
  29033. GLint u_transform;
  29034. GLint u_hasTexture;
  29035. GLint u_albedoSampler;
  29036. GLint u_lightIndex;
  29037. GLint u_shadowMatrices;
  29038. GLint u_lightPos;
  29039. GLint u_farPlane;
  29040. GLint u_hasAnimations;
  29041. GLuint u_jointTransforms = GL_INVALID_INDEX;
  29042. }pointShadowShader;
  29043. struct
  29044. {
  29045. Shader shader;
  29046. GLint u_transform;
  29047. GLint u_hasTexture;
  29048. GLint u_albedoSampler;
  29049. GLint u_hasAnimations;
  29050. GLuint u_jointTransforms = GL_INVALID_INDEX;
  29051. }prePass;
  29052. Shader geometryPassShader;
  29053. Shader lightingPassShader;
  29054. bool normalMap = 1;
  29055. bool useSSAO = 1;
  29056. bool useTheHbaoImplementation = 1;
  29057. //todo split stuff into separate things
  29058. bool bloom = 1;
  29059. GpuTexture brdfTexture;
  29060. void clear();
  29061. };
  29062. };
  29063. #pragma endregion
  29064. ////////////////////////////////////////////////
  29065. //Camera.h
  29066. ////////////////////////////////////////////////
  29067. #pragma region Camera
  29068. #pragma once
  29069. #include <glm/vec3.hpp>
  29070. #include <glm/vec4.hpp>
  29071. #include <glm/mat4x4.hpp>
  29072. #include <glm/trigonometric.hpp>
  29073. #include <cmath>
  29074. namespace gl3d
  29075. {
  29076. constexpr float PI = 3.1415926535897932384626433;
  29077. void generateTangentSpace(glm::vec3 v, glm::vec3& outUp, glm::vec3& outRight);
  29078. //near w,h far w, h center near, far
  29079. void computeFrustumDimensions(glm::vec3 position, glm::vec3 viewDirection,
  29080. float fovRadians, float aspectRatio, float nearPlane, float farPlane,
  29081. glm::vec2& nearDimensions, glm::vec2& farDimensions, glm::vec3& centerNear,
  29082. glm::vec3& centerFar);
  29083. void computeFrustumSplitCorners(glm::vec3 directionVector,
  29084. glm::vec2 nearDimensions, glm::vec2 farDimensions, glm::vec3 centerNear, glm::vec3 centerFar,
  29085. glm::vec3& nearTopLeft, glm::vec3& nearTopRight, glm::vec3& nearBottomLeft, glm::vec3& nearBottomRight,
  29086. glm::vec3& farTopLeft, glm::vec3& farTopRight, glm::vec3& farBottomLeft, glm::vec3& farBottomRight
  29087. );
  29088. glm::vec3 fromAnglesToDirection(float zenith, float azimuth);
  29089. glm::vec2 fromDirectionToAngles(glm::vec3 direction);
  29090. struct Camera
  29091. {
  29092. Camera() = default;
  29093. Camera(float aspectRatio, float fovRadians)
  29094. :aspectRatio(aspectRatio),
  29095. fovRadians(fovRadians)
  29096. {}
  29097. glm::vec3 up = { 0.f,1.f,0.f };
  29098. float aspectRatio = 1;
  29099. float fovRadians = glm::radians(60.f);
  29100. float closePlane = 0.01f;
  29101. float farPlane = 200.f;
  29102. glm::vec3 position = {};
  29103. glm::vec3 viewDirection = {0,0,-1};
  29104. glm::mat4x4 getProjectionMatrix();
  29105. glm::mat4x4 getWorldToViewMatrix();
  29106. void rotateCamera(const glm::vec2 delta);
  29107. void moveFPS(glm::vec3 direction);
  29108. bool operator==(const Camera& other)
  29109. {
  29110. return
  29111. (up == other.up)
  29112. && (aspectRatio == other.aspectRatio)
  29113. && (fovRadians == other.fovRadians)
  29114. && (closePlane == other.closePlane)
  29115. && (farPlane == other.farPlane)
  29116. && (position == other.position)
  29117. && (viewDirection == other.viewDirection)
  29118. ;
  29119. };
  29120. bool operator!=(const Camera& other)
  29121. {
  29122. return !(*this == other);
  29123. };
  29124. };
  29125. };
  29126. #pragma endregion
  29127. ////////////////////////////////////////////////
  29128. //GraphicModel.h
  29129. ////////////////////////////////////////////////
  29130. #pragma region GraphicModel
  29131. #pragma once
  29132. #include <glm/vec3.hpp>
  29133. #include <glm/gtc/quaternion.hpp>
  29134. #include <glm/gtc/matrix_transform.hpp>
  29135. #include <glm/gtx/transform.hpp>
  29136. namespace gl3d
  29137. {
  29138. struct Transform
  29139. {
  29140. glm::vec3 position = {};
  29141. glm::vec3 rotation = {};
  29142. glm::vec3 scale = { 1,1,1 };
  29143. glm::mat4 getTransformMatrix();
  29144. bool operator==(const Transform& other)
  29145. {
  29146. return
  29147. (position == other.position)
  29148. &&(rotation == other.rotation)
  29149. &&(scale == other.scale)
  29150. ;
  29151. };
  29152. bool operator!=(const Transform& other)
  29153. {
  29154. return !(*this == other);
  29155. };
  29156. void setFromMatrix(const glm::mat4 &mat);
  29157. };
  29158. glm::mat4 getTransformMatrix(glm::vec3 position, glm::vec3 rotation, glm::vec3 scale);
  29159. glm::mat4 getTransformMatrix(const Transform &t);
  29160. struct LoadedModelData
  29161. {
  29162. LoadedModelData() = default;
  29163. LoadedModelData(const char *file, ErrorReporter &errorReporter,
  29164. FileOpener &fileOpener, float scale = 1.f) { load(file, errorReporter, fileOpener, scale); }
  29165. void load(const char *file, ErrorReporter &errorReporter, FileOpener &fileOpener, float scale = 1.f);
  29166. objl::Loader loader;
  29167. std::string path;
  29168. };
  29169. struct DebugGraphicModel
  29170. {
  29171. std::string name = {};
  29172. GLuint vertexArray = 0;
  29173. GLuint vertexBuffer = 0;
  29174. GLuint indexBuffer = 0;
  29175. GLsizei primitiveCount = 0;
  29176. void loadFromComputedData(size_t vertexSize, const float * vercies, size_t indexSize = 0, const unsigned int * indexes = nullptr, bool noTexture = false);
  29177. void clear();
  29178. void draw();
  29179. //todo probably move this in the final version
  29180. glm::vec3 position = {};
  29181. glm::vec3 rotation = {};
  29182. glm::vec3 scale = {1,1,1};
  29183. glm::mat4 getTransformMatrix();
  29184. };
  29185. struct GraphicModel
  29186. {
  29187. std::string name = "";
  29188. GLuint vertexArray = 0;
  29189. GLuint vertexBuffer = 0;
  29190. GLuint indexBuffer = 0;
  29191. GLsizei primitiveCount = 0;
  29192. void loadFromComputedData(size_t vertexSize, const float *vertices, size_t indexSize = 0,
  29193. const unsigned int *indexes = nullptr, bool noTexture = false, bool hasAnimationData = false,
  29194. std::vector<Animation> animation = {},
  29195. std::vector<Joint> joints = {}
  29196. );
  29197. void clear();
  29198. bool hasBones = 0;
  29199. glm::vec3 minBoundary = {};
  29200. glm::vec3 maxBoundary = {};
  29201. Material material = 0;
  29202. unsigned char ownMaterial = 0;
  29203. unsigned char culledThisFrame= 0;
  29204. };
  29205. struct Renderer3D;
  29206. struct ModelData
  29207. {
  29208. std::vector < GraphicModel >models;
  29209. std::vector < char* > subModelsNames; //for imgui
  29210. std::vector <Material> createdMaterials;
  29211. void clear(Renderer3D &renderer);
  29212. void internalClear();
  29213. std::vector<gl3d::Animation> animations;
  29214. std::vector<gl3d::Joint> joints;
  29215. };
  29216. //the data for an entity
  29217. //todo move to internal
  29218. struct CpuEntity
  29219. {
  29220. Transform transform;
  29221. std::vector < GraphicModel >models;
  29222. std::vector < char* > subModelsNames; //for imgui
  29223. void clear();
  29224. void allocateGpuData();
  29225. void deleteGpuData();
  29226. int animationIndex = 0;
  29227. float animationSpeed = 1.f;
  29228. float totalTimePassed = 0;
  29229. struct
  29230. {
  29231. //remainintgTime used to check if we are performing a transition
  29232. float remainintgTime = 0;
  29233. float totalTime = 0;
  29234. float ToTime = 0;
  29235. int ToIndex = 0;
  29236. }animationTransition;
  29237. std::vector<Animation> animations;
  29238. std::vector<Joint> joints; //todo root should be here not at animations probably?
  29239. GLuint appliedSkinningMatricesBuffer;
  29240. bool canBeAnimated() { return !animations.empty() && !joints.empty(); }
  29241. unsigned char flags = {}; // lsb -> 1 static, visible, shadows
  29242. GL3D_ADD_FLAG(isStatic, setStatic, 0);
  29243. GL3D_ADD_FLAG(isVisible, setVisible, 1); //set by user
  29244. GL3D_ADD_FLAG(castShadows, setCastShadows, 2);
  29245. GL3D_ADD_FLAG(animate, setAnimate, 3);
  29246. };
  29247. struct LoadedTextures
  29248. {
  29249. std::string name;
  29250. GpuTexture t;
  29251. };
  29252. #pragma region skyBox
  29253. struct SkyBox
  29254. {
  29255. GLuint texture = 0; //environment cubemap
  29256. GLuint convolutedTexture = 0; //convoluted environment (used for difuse iradiance)
  29257. GLuint preFilteredMap = 0; //multiple mipmaps used for speclar
  29258. glm::vec3 color = { 1,1,1 };
  29259. void clearTextures();
  29260. };
  29261. struct SkyBoxLoaderAndDrawer
  29262. {
  29263. GLuint vertexArray = 0;
  29264. GLuint vertexBuffer = 0;
  29265. GLuint captureFBO;
  29266. void createGpuData(ErrorReporter &errorReporter, FileOpener &fileOpener, GLuint frameBuffer);
  29267. struct
  29268. {
  29269. Shader shader;
  29270. GLuint samplerUniformLocation;
  29271. GLuint modelViewUniformLocation;
  29272. GLuint u_skyBoxPresent;
  29273. GLuint u_ambient;
  29274. }normalSkyBox;
  29275. struct
  29276. {
  29277. Shader shader;
  29278. GLuint u_equirectangularMap;
  29279. GLuint modelViewUniformLocation;
  29280. }hdrtoCubeMap;
  29281. struct
  29282. {
  29283. Shader shader;
  29284. GLuint u_environmentMap;
  29285. GLuint u_sampleQuality;
  29286. GLuint modelViewUniformLocation;
  29287. }convolute;
  29288. struct
  29289. {
  29290. Shader shader;
  29291. GLuint u_environmentMap;
  29292. GLuint u_roughness;
  29293. GLuint u_sampleCount;
  29294. GLuint modelViewUniformLocation;
  29295. }preFilterSpecular;
  29296. struct
  29297. {
  29298. Shader shader;
  29299. GLuint u_lightPos;
  29300. GLuint u_g;
  29301. GLuint u_color1;
  29302. GLuint u_color2;
  29303. GLuint u_groundColor;
  29304. GLuint u_useGround;
  29305. GLuint modelViewUniformLocation;
  29306. }atmosphericScatteringShader;
  29307. enum CrossskyBoxFormats
  29308. {
  29309. BottomOfTheCrossRight,
  29310. BottomOfTheCrossDown,
  29311. BottomOfTheCrossLeft,
  29312. };
  29313. void loadTexture(const char *names[6], SkyBox &skyBox, ErrorReporter &errorReporter, FileOpener &fileOpener, GLuint frameBuffer);
  29314. void loadTexture(const char *name, SkyBox &skyBox, ErrorReporter &errorReporter, FileOpener &fileOpener, GLuint frameBuffer, int format = 0);
  29315. void loadHDRtexture(const char *name, ErrorReporter &errorReporter, FileOpener &fileOpener, SkyBox &skyBox, GLuint frameBuffer);
  29316. void atmosphericScattering(glm::vec3 sun, glm::vec3 color1, glm::vec3 color2,
  29317. glm::vec3 groundColor, bool useGroundColor, float g, SkyBox& skyBox,
  29318. GLuint frameBuffer);
  29319. //, GLuint frameBuffer is the default fbo
  29320. void createConvolutedAndPrefilteredTextureData(SkyBox &skyBox, GLuint frameBuffer,
  29321. float sampleQuality = 0.025, unsigned int specularSamples = 1024);
  29322. //void clearGpuData();
  29323. void draw(const glm::mat4& viewProjMat, SkyBox& skyBox, float exposure,
  29324. glm::vec3 ambient);
  29325. void drawBefore(const glm::mat4 &viewProjMat, SkyBox &skyBox, float exposure,
  29326. glm::vec3 ambient);
  29327. void clear();
  29328. };
  29329. /*
  29330. "right.jpg",
  29331. "left.jpg",
  29332. "top.jpg",
  29333. "bottom.jpg",
  29334. "front.jpg",
  29335. "back.jpg"
  29336. */
  29337. #pragma endregion
  29338. };
  29339. #pragma endregion
  29340. ////////////////////////////////////////////////
  29341. //gl3d.h
  29342. ////////////////////////////////////////////////
  29343. #pragma region gl3d
  29344. #pragma once
  29345. #include <algorithm>
  29346. //todo get mesh material?
  29347. namespace gl3d
  29348. {
  29349. namespace internal
  29350. {
  29351. //todo probably just keep a counter and get the next one
  29352. template <class T>
  29353. int generateNewIndex(T indexesVec)
  29354. {
  29355. int id = 0;
  29356. auto indexesCopy = indexesVec;
  29357. std::sort(indexesCopy.begin(), indexesCopy.end());
  29358. if (indexesCopy.empty())
  29359. {
  29360. id = 1;
  29361. }
  29362. else
  29363. {
  29364. id = 1;
  29365. for (int i = 0; i < indexesCopy.size(); i++)
  29366. {
  29367. if (indexesCopy[i] != id)
  29368. {
  29369. break;
  29370. }
  29371. else
  29372. {
  29373. id++;
  29374. }
  29375. }
  29376. }
  29377. return id;
  29378. };
  29379. };
  29380. struct AtmosfericScatteringSettings
  29381. {
  29382. glm::vec3 sun = {0,1,0};
  29383. //sky
  29384. glm::vec3 color1 = {0,0,0};
  29385. //sun
  29386. glm::vec3 color2 = {0,0,0};
  29387. glm::vec3 ground = {0,0,0};
  29388. float g = 0;
  29389. bool useGroundColor = 0;
  29390. };
  29391. //load using renderer.loadColorLookupTextureFromFile
  29392. //the memory of the lookup texture is not managed by the engine and not cleared by the engine on clearAll.
  29393. struct ColorLookupTexture
  29394. {
  29395. GpuTexture t;
  29396. void clear() { t.clear(); }
  29397. };
  29398. struct Renderer3D
  29399. {
  29400. //todo store the default fbo
  29401. //size of the screen and the default frameBuffer
  29402. void init(int x, int y, const char *BRDFIntegrationMapFileLocation, GLuint frameBuffer = 0);
  29403. GLuint frameBuffer = 0;
  29404. ErrorReporter errorReporter;
  29405. FileOpener fileOpener;
  29406. ErrorCallback_t *setErrorCallback(ErrorCallback_t *errorCallback, void *userData);
  29407. ErrorCallback_t *getErrorCallback();
  29408. #pragma region material
  29409. Material createMaterial(int quality, glm::vec4 kd = glm::vec4(1),
  29410. float roughness = 0.5f, float metallic = 0.1, float ao = 1.f, float emissive = 0.f, std::string name = "",
  29411. gl3d::Texture albedoTexture = {}, gl3d::Texture normalTexture = {}, gl3d::Texture roughnessTexture = {}, gl3d::Texture metallicTexture = {},
  29412. gl3d::Texture occlusionTexture = {}, gl3d::Texture emmisiveTexture = {});
  29413. Material createMaterial(Material m);
  29414. std::vector<Material> loadMaterial(std::string file, int quality);
  29415. bool deleteMaterial(Material m);
  29416. bool copyMaterialData(Material dest, Material source);
  29417. MaterialValues getMaterialValues(Material m);
  29418. void setMaterialValues(Material m, MaterialValues values);
  29419. std::string getMaterialName(Material m);
  29420. void setMaterialName(Material m, const std::string& name);
  29421. TextureDataForMaterial getMaterialTextures(Material m);
  29422. void setMaterialTextures(Material m, TextureDataForMaterial textures);
  29423. bool isMaterial(Material& m);
  29424. //returns true if succeded
  29425. //bool setMaterialData(Material m, const MaterialValues &data, std::string *s = nullptr);
  29426. #pragma endregion
  29427. #pragma region Texture
  29428. Texture loadTexture(std::string path, int quality);
  29429. Texture loadTextureFromMemory(objl::LoadedTexture &t, int quality);
  29430. GLuint getTextureOpenglId(Texture& t);
  29431. bool isTexture(Texture& t);
  29432. void deleteTexture(Texture& t);
  29433. GpuTexture* getTextureData(Texture& t);
  29434. //internal
  29435. Texture createIntenralTexture(GpuTexture t, int alphaData, int alphaValues, const std::string &name = "");
  29436. Texture createIntenralTexture(GLuint id_, int alphaData, int alphaValues, const std::string &name = "");
  29437. PBRTexture createPBRTexture(Texture& roughness, Texture& metallic,
  29438. Texture& ambientOcclusion, int quality);
  29439. void deletePBRTexture(PBRTexture &t);
  29440. #pragma endregion
  29441. #pragma region skyBox
  29442. SkyBox loadSkyBox(const char* names[6]);
  29443. SkyBox loadSkyBox(const char* name, int format = 0);
  29444. SkyBox loadHDRSkyBox(const char* name);
  29445. void deleteSkyBoxTextures(SkyBox& skyBox);
  29446. SkyBox atmosfericScattering(glm::vec3 sun, glm::vec3 color1, glm::vec3 color2,
  29447. glm::vec3 groundColor, bool useGroundColor, float g);
  29448. SkyBox atmosfericScattering(AtmosfericScatteringSettings settings);
  29449. #pragma endregion
  29450. #pragma region model
  29451. //todo implement stuff here
  29452. //position 3, normal 3, optional texture 2
  29453. Model createModelFromData(Material material, std::string name,
  29454. size_t vertexCount, const float *vertices, size_t indexesCount = 0,
  29455. const unsigned int *indexes = nullptr, bool noTexture = false);
  29456. Model loadModel(std::string path, int quality, float scale);
  29457. bool isModel(Model& m);
  29458. void deleteModel(Model &m);
  29459. void clearModelData(Model& m);
  29460. int getModelMeshesCount(Model& m);
  29461. std::string getModelMeshesName(Model& m, int index);
  29462. //for apis like imgui
  29463. std::vector<char*>* getModelMeshesNames(Model& m);
  29464. #pragma endregion
  29465. #pragma region point lights
  29466. PointLight createPointLight(glm::vec3 position, glm::vec3 color = glm::vec3(1.f),
  29467. float dist = 20, float attenuation = 1);
  29468. void detletePointLight(PointLight& l);
  29469. glm::vec3 getPointLightPosition(PointLight& l);
  29470. void setPointLightPosition(PointLight& l, glm::vec3 position);
  29471. bool isPointLight(PointLight& l);
  29472. glm::vec3 getPointLightColor(PointLight& l);
  29473. void setPointLightColor(PointLight& l, glm::vec3 color);
  29474. float getPointLightDistance(PointLight& l); //light distance
  29475. void setPointLightDistance(PointLight& l, float distance); //light distance
  29476. float getPointLightAttenuation(PointLight& l); //light distance
  29477. void setPointLightAttenuation(PointLight& l, float attenuation); //light distance
  29478. bool getPointLightShadows(PointLight& l);
  29479. void setPointLightShadows(PointLight& l, bool castShadows = true);
  29480. float getPointLightHardness(PointLight& l);
  29481. void setPointLightHardness(PointLight& l, float hardness);
  29482. int getPointLightShadowSize();
  29483. void setPointLightShadowSize(int size);
  29484. #pragma endregion
  29485. #pragma region directional light
  29486. DirectionalLight createDirectionalLight(glm::vec3 direction,
  29487. glm::vec3 color = glm::vec3(1.f), float hardness = 1, bool castShadows = 1);
  29488. void deleteDirectionalLight(DirectionalLight& l);
  29489. bool isDirectionalLight(DirectionalLight& l);
  29490. glm::vec3 getDirectionalLightDirection(DirectionalLight& l);
  29491. void setDirectionalLightDirection(DirectionalLight& l, glm::vec3 direction);
  29492. glm::vec3 getDirectionalLightColor(DirectionalLight& l);
  29493. void setDirectionalLightColor(DirectionalLight& l, glm::vec3 color);
  29494. float getDirectionalLightHardness(DirectionalLight& l);
  29495. void setDirectionalLightHardness(DirectionalLight& l, float hardness);
  29496. bool getDirectionalLightShadows(DirectionalLight& l);
  29497. void setDirectionalLightShadows(DirectionalLight& l, bool castShadows);
  29498. int getDirectionalLightShadowSize();
  29499. void setDirectionalLightShadowSize(int size);
  29500. #pragma endregion
  29501. #pragma region spot light
  29502. SpotLight createSpotLight(glm::vec3 position, float fovRadians,
  29503. glm::vec3 direction, float dist = 20, float attenuation = 1,
  29504. glm::vec3 color = glm::vec3(1), float hardness = 1, int castShadows = 1);
  29505. //angles is the angle from zenith and azimuth
  29506. SpotLight createSpotLight(glm::vec3 position, float fovRadians,
  29507. glm::vec2 anglesRadians, float dist = 20, float attenuation = 1,
  29508. glm::vec3 color = glm::vec3(1), float hardness = 1, int castShadows = 1);
  29509. void deleteSpotLight(SpotLight& l);
  29510. glm::vec3 getSpotLightPosition(SpotLight& l);
  29511. void setSpotLightPosition(SpotLight& l, glm::vec3 position);
  29512. bool isSpotLight(SpotLight& l);
  29513. glm::vec3 getSpotLightColor(SpotLight& l);
  29514. void setSpotLightColor(SpotLight& l, glm::vec3 color);
  29515. float getSpotLightFov(SpotLight& l);
  29516. void setSpotLightFov(SpotLight& l, float fovRadians);
  29517. glm::vec3 getSpotLightDirection(SpotLight& l);
  29518. void setSpotLightDirection(SpotLight& l, glm::vec3 direction);
  29519. float getSpotLightDistance(SpotLight& l); //light distance
  29520. void setSpotLightDistance(SpotLight& l, float distance); //light distance
  29521. float getSpotLightAttenuation(SpotLight& l); //light distance
  29522. void setSpotLightAttenuation(SpotLight& l, float attenuation); //light distance
  29523. float getSpotLightHardness(SpotLight& l);
  29524. void setSpotLightHardness(SpotLight& l, float hardness);
  29525. void setSpotLightShadows(SpotLight& l, bool castShadows);
  29526. bool getSpotLightShadows(SpotLight& l);
  29527. int getSpotLightShadowSize();
  29528. void setSpotLightShadowSize(int size);
  29529. #pragma endregion
  29530. #pragma region Entity
  29531. Entity createEntity(Model m, Transform transform = {},
  29532. bool staticGeometry = 1, bool visible = 1, bool castShadows = 1);
  29533. Entity duplicateEntity(Entity &e);
  29534. void setEntityModel(Entity& e, Model m);
  29535. void clearEntityModel(Entity& e);
  29536. CpuEntity* getEntityData(Entity &e); //todo this will probably dissapear
  29537. Transform getEntityTransform(Entity &e);
  29538. void setEntityTransform(Entity &e, Transform transform);
  29539. bool isEntityStatic(Entity &e);
  29540. void setEntityStatic(Entity &e, bool s = true);
  29541. void deleteEntity(Entity& e);
  29542. bool isEntity(Entity& e);
  29543. bool isEntityVisible(Entity& e);
  29544. void setEntityVisible(Entity& e, bool v = true);
  29545. void setEntityCastShadows(Entity& e, bool s = true);
  29546. bool getEntityCastShadows(Entity& e);
  29547. void setEntityAnimationIndex(Entity &e, int ind);
  29548. //transitions from to ecurrent state
  29549. //to a new animation specified newAnimationIndex. it takes transitionTimeSecconds,
  29550. //and it will translate to newAnimationIndex at newAnimationTimeStampSecconds time stamp.
  29551. //if the object is not curently animated it will start the animation.
  29552. void transitionToAnimation(Entity& e, int newAnimationIndex, float transitionTimeSecconds,
  29553. float newAnimationTimeStampSecconds = 0);
  29554. int getEntityAnimationIndex(Entity &e);
  29555. void setEntityAnimationSpeed(Entity &e, float speed);
  29556. float getEntityAnimationSpeed(Entity &e);
  29557. void setEntityAnimate(Entity& e, bool animate); //true = display animations. To pause set the animation speed to 0.
  29558. bool getEntityAnimate(Entity& e); //returns true if it is animating
  29559. bool entityCanAnimate(Entity& e); //returns true if it is can be animated
  29560. int getEntityJointIndex(Entity &e, std::string name); //returns -1 on error
  29561. bool getEntityJointTransform(Entity &e, std::string boneName, Transform &t); //returns 0 on error
  29562. bool getEntityJointTransform(Entity &e, int boneIndex, Transform &t); //returns 0 on error
  29563. //this is used for apis like imgui.
  29564. std::vector<char*> *getEntityMeshesNames(Entity& e);
  29565. int getEntityMeshesCount(Entity& e);
  29566. MaterialValues getEntityMeshMaterialValues(Entity& e, int meshIndex);
  29567. void setEntityMeshMaterialValues(Entity& e, int meshIndex, MaterialValues mat);
  29568. std::string getEntityMeshMaterialName(Entity& e, int meshIndex);
  29569. void setEntityMeshMaterialName(Entity& e, int meshIndex, const std::string &name);
  29570. void setEntityMeshMaterial(Entity& e, int meshIndex, Material mat);
  29571. TextureDataForMaterial getEntityMeshMaterialTextures(Entity& e, int meshIndex);
  29572. void setEntityMeshMaterialTextures(Entity& e, int meshIndex, TextureDataForMaterial texture);
  29573. #pragma endregion
  29574. #pragma region settings
  29575. void setExposure(float exposure);
  29576. float getExposure();
  29577. //cheap
  29578. void enableNormalMapping(bool normalMapping = 1);
  29579. bool isNormalMappingEnabeled();
  29580. //cheap (will calculate the light from sky box more accurately)
  29581. void enableLightSubScattering(bool lightSubScatter = 1);
  29582. bool isLightSubScatteringEnabeled();
  29583. //rather expensive
  29584. void enableSSAO(bool ssao = 1);
  29585. bool &isSSAOenabeled();
  29586. float &getSSAOBias();
  29587. void setSSAOBias(float bias);
  29588. float &getSSAORadius();
  29589. void setSSAORadius(float radius);
  29590. int &getSSAOSampleCount();
  29591. void setSSAOSampleCount(int samples);
  29592. float &getSSAOExponent();
  29593. void setSSAOExponent(float exponent);
  29594. bool &colorCorrection();
  29595. //the memory of the lookup texture is not managed by the engine and not cleared by the engine on clearAll.
  29596. ColorLookupTexture &colorCorrectionTexture();
  29597. //bloom
  29598. //more or less expensive
  29599. //this is setter and getter
  29600. bool &bloom();
  29601. float getBloomTresshold();
  29602. void setBloomTresshold(float b);
  29603. void setBloomIntensisy(float b);
  29604. bool &bloomHighQualityDownSample();
  29605. bool &bloomHighQualityUpSample();
  29606. //SSR
  29607. LightShader::LightPassData::SSRdata &getSSRdata();
  29608. void setSSRdata(LightShader::LightPassData::SSRdata data);
  29609. void enableSSR(bool enable = true);
  29610. bool isSSRenabeled();
  29611. //
  29612. float &getDirectionalShadowCascadesFrustumSplit(int cascadeIndex);
  29613. //chromatic aberation
  29614. bool &chromaticAberationEnabeled();
  29615. //in pixels
  29616. float getChromaticAberationStrength();
  29617. //in pixels
  29618. void setChromaticAberationStrength(float pixels);
  29619. float getChromaticAberationUnfocusDistance();
  29620. void setChromaticAberationUnfocusDistance(float distance);
  29621. #pragma region fxaa
  29622. //todo explain this parameters
  29623. //http://blog.simonrodriguez.fr/articles/2016/07/implementing_fxaa.html
  29624. struct FXAAData
  29625. {
  29626. float edgeMinTreshold = 0.028;
  29627. float edgeDarkTreshold = 0.125;
  29628. int ITERATIONS = 12;
  29629. float quaityMultiplier = 0.8;
  29630. float SUBPIXEL_QUALITY = 0.95;
  29631. };
  29632. //very little performance penalty
  29633. void enableFXAA(bool fxaa = 1);
  29634. FXAAData& getFxaaSettings();
  29635. bool& isFXAAenabeled();
  29636. #pragma endregion
  29637. //saves the current settings to a string;
  29638. std::string saveSettingsToJson(bool includeRenderingSettings, std::string skyBoxName = "",
  29639. gl3d::AtmosfericScatteringSettings *atmosphericScattering = nullptr);
  29640. //this will terminate if data is not a valid json
  29641. void loadSettingsFromJson(const char *data, bool includeRenderingSettings, bool loadSkyBox, bool loadAtmosphericScattering);
  29642. #pragma endregion
  29643. Camera camera;
  29644. SkyBox skyBox;
  29645. //debug stuff todo
  29646. void renderModelNormals(Model o, glm::vec3 position, glm::vec3 rotation = {},
  29647. glm::vec3 scale = { 1,1,1 }, float normalSize = 0.5, glm::vec3 normalColor = {0.7, 0.7, 0.1});
  29648. void renderSubModelNormals(Model o, int index, glm::vec3 position, glm::vec3 rotation = {},
  29649. glm::vec3 scale = { 1,1,1 }, float normalSize = 0.5, glm::vec3 normalColor = { 0.7, 0.7, 0.1 });
  29650. void renderSubModelBorder(Model o, int index, glm::vec3 position, glm::vec3 rotation = {},
  29651. glm::vec3 scale = { 1,1,1 }, float borderSize = 0.5, glm::vec3 borderColor = { 0.7, 0.7, 0.1 });
  29652. struct ColorCorrection
  29653. {
  29654. GLuint u_texture;
  29655. GLuint u_lookup;
  29656. Shader shader;
  29657. GLuint fbo;
  29658. //fbo texture
  29659. GLuint texture;
  29660. glm::ivec2 currentDimensions = {};
  29661. bool colorCorrection = 0; //todo also check if texture is present
  29662. void create(int w, int h, ErrorReporter &errorReporter, FileOpener &fileOpener);
  29663. void resize(int w, int h);
  29664. void clear();
  29665. //the memory of the lookup texture is not managed by the engine and not cleared by the engine on clearAll.
  29666. ColorLookupTexture currentTexture; //the texture supplied by user
  29667. };
  29668. struct InternalStruct
  29669. {
  29670. Camera lastFrameCamera;
  29671. LightShader lightShader;
  29672. int w; int h;
  29673. int adaptiveW; int adaptiveH;
  29674. struct PBRtextureMaker
  29675. {
  29676. Shader shader;
  29677. GLuint fbo;
  29678. void init(ErrorReporter &errorReporter, FileOpener &fileOpener);
  29679. GLuint createRMAtexture(
  29680. GpuTexture roughness, GpuTexture metallic, GpuTexture ambientOcclusion,
  29681. GLuint quadVAO, int &RMA_loadedTextures, GLuint frameBuffer, int quality);
  29682. void clear();
  29683. }pBRtextureMaker;
  29684. SkyBoxLoaderAndDrawer skyBoxLoaderAndDrawer;
  29685. void renderSkyBox(Camera &c, SkyBox &s); //todo remove this later
  29686. void renderSkyBoxBefore(Camera& c, SkyBox& s);
  29687. int getMaterialIndex(Material &m);
  29688. int getModelIndex(Model &o);
  29689. int getTextureIndex(Texture &t);
  29690. int getEntityIndex(Entity &e);
  29691. int getSpotLightIndex(SpotLight &l);
  29692. int getPointLightIndex(PointLight &l);
  29693. int getDirectionalLightIndex(DirectionalLight &l);
  29694. //material
  29695. std::vector<MaterialValues> materials;
  29696. std::vector<int> materialIndexes;
  29697. std::vector<std::string> materialNames;
  29698. std::vector<TextureDataForMaterial> materialTexturesData;
  29699. bool getMaterialData(Material m, MaterialValues* gpuMaterial,
  29700. std::string* name, TextureDataForMaterial* textureData);
  29701. //texture
  29702. std::vector <internal::GpuTextureWithFlags> loadedTextures;
  29703. std::vector<GLuint64> loadedTexturesBindlessHandle;
  29704. std::vector<int> loadedTexturesIndexes;
  29705. std::vector<std::string> loadedTexturesNames;
  29706. //models
  29707. std::vector< ModelData > graphicModels;
  29708. std::vector<int> graphicModelsIndexes;
  29709. ModelData* getModelData(Model o);
  29710. //entities
  29711. std::vector<CpuEntity> cpuEntities;
  29712. std::vector<int> entitiesIndexes;
  29713. //spot lights
  29714. std::vector<internal::GpuSpotLight> spotLights;
  29715. std::vector<int> spotLightIndexes;
  29716. //point lights
  29717. std::vector<internal::GpuPointLight> pointLights;
  29718. std::vector<int> pointLightIndexes;
  29719. //directional lights
  29720. std::vector<internal::GpuDirectionalLight> directionalLights;
  29721. std::vector<int> directionalLightIndexes;
  29722. struct PerFrameFlags
  29723. {
  29724. bool staticGeometryChanged = 0;
  29725. bool shouldUpdateSpotShadows = 0;
  29726. bool shouldUpdatePointShadows = 0;
  29727. bool shouldUpdateDirectionalShadows = 0;
  29728. }perFrameFlags;
  29729. #pragma region different shaders
  29730. struct
  29731. {
  29732. Shader shader;
  29733. GLint modelTransformLocation;
  29734. GLint projectionLocation;
  29735. GLint sizeLocation;
  29736. GLint colorLocation;
  29737. //todo add a create function when ill work at this
  29738. }showNormalsProgram;
  29739. struct SSAO
  29740. {
  29741. //https://learnopengl.com/Advanced-Lighting/SSAO
  29742. void create(int w, int h, ErrorReporter &errorReporter, FileOpener &fileOpener, GLuint frameBuffer);
  29743. void resize(int w, int h);
  29744. void clear();
  29745. glm::ivec2 currentDimensions = {};
  29746. GLuint noiseTexture;
  29747. GLuint ssaoFBO;
  29748. GLuint ssaoColorBuffer;
  29749. Shader shader;
  29750. GLuint ssaoUniformBlockBuffer;
  29751. struct SsaoShaderUniformBlockData
  29752. {
  29753. float radius = 0.2;
  29754. float bias = 0.025;
  29755. int samplesTestSize = 16; // should be less than kernelSize (64)
  29756. }ssaoShaderUniformBlockData;
  29757. GLint u_projection = -1;
  29758. GLint u_view = -1;
  29759. GLint u_gPosition = -1;
  29760. GLint u_gNormal = -1;
  29761. GLint u_texNoise = -1;
  29762. GLint u_samples = -1;
  29763. GLuint u_SSAODATA;
  29764. std::vector<glm::vec3> ssaoKernel;
  29765. GLuint blurBuffer;
  29766. GLuint blurColorBuffer;
  29767. GLint u_ssaoInput;
  29768. Shader blurShader;
  29769. float ssao_finalColor_exponent = 5.f;
  29770. }ssao;
  29771. struct HBAO
  29772. {
  29773. //https://developer.download.nvidia.com/presentations/2008/SIGGRAPH/HBAO_SIG08b.pdf
  29774. void create(ErrorReporter &errorReporter, FileOpener &fileOpener);
  29775. void clear();
  29776. Shader shader;
  29777. GLint u_projection = -1;
  29778. GLint u_view = -1;
  29779. GLint u_gPosition = -1;
  29780. GLint u_gNormal = -1;
  29781. GLint u_texNoise = -1;
  29782. }hbao;
  29783. struct GBuffer
  29784. {
  29785. void create(int w, int h, ErrorReporter &errorReporter, GLuint frameBuffer);
  29786. void resize(int w, int h);
  29787. void clear();
  29788. enum bufferTargers
  29789. {
  29790. normal = 0,
  29791. textureDerivates,
  29792. //albedo,
  29793. //material,
  29794. positionViewSpace,
  29795. //emissive,
  29796. materialIndex,
  29797. textureUV,
  29798. //textureDerivates,
  29799. bufferCount,
  29800. };
  29801. unsigned int gBuffer;
  29802. unsigned int buffers[bufferCount];
  29803. unsigned int depthBuffer;
  29804. glm::ivec2 currentDimensions = {};
  29805. }gBuffer;
  29806. #pragma endregion
  29807. bool hasLastFrameTexture = 1;
  29808. ColorCorrection colorCorrection;
  29809. }internal;
  29810. struct PostProcess
  29811. {
  29812. struct
  29813. {
  29814. Shader shader;
  29815. GLint u_texture;
  29816. GLint u_exposure;
  29817. GLint u_tresshold;
  29818. }filterShader;
  29819. struct
  29820. {
  29821. Shader shader;
  29822. GLint u_texture;
  29823. GLint u_mip;
  29824. }addMips;
  29825. struct
  29826. {
  29827. Shader shader;
  29828. GLint u_texture;
  29829. GLint u_mip;
  29830. }addMipsBlur;
  29831. struct
  29832. {
  29833. Shader shader;
  29834. GLint u_texture;
  29835. GLint u_mip;
  29836. }filterDown;
  29837. struct
  29838. {
  29839. Shader shader;
  29840. GLint u_finalColorTexture;
  29841. GLint u_windowSize;
  29842. GLint u_strength;
  29843. GLint u_DepthTexture;
  29844. GLint u_near;
  29845. GLint u_far;
  29846. GLint u_unfocusDistance;
  29847. GLuint fbo;
  29848. }chromaticAberation;
  29849. Shader postProcessShader;
  29850. Shader gausianBLurShader;
  29851. GLint u_colorTexture; //post process shader
  29852. GLint u_bloomTexture; //post process shader
  29853. GLint u_bloomNotBluredTexture; //post process shader
  29854. GLint u_bloomIntensity; //post process shader
  29855. GLint u_exposure; //post process shader
  29856. GLint u_useSSAO; //post process shader
  29857. GLint u_ssaoExponent; //post process shader
  29858. GLint u_ssao; //post process shader
  29859. GLint u_toBlurcolorInput;
  29860. GLint u_horizontal;
  29861. GLint u_mip;
  29862. GLint u_texel;
  29863. GLuint fbo;
  29864. GLuint filterFbo;
  29865. GLuint blurFbo[2];
  29866. GLuint colorBuffers[3]; // 0 for color, 1 for bloom, 2 last frame
  29867. GLuint bluredColorBuffer[2];
  29868. void create(int w, int h, ErrorReporter &errorReporter, FileOpener &fileOpener, GLuint frameBuffer);
  29869. void resize(int w, int h);
  29870. void clear();
  29871. glm::ivec2 currentDimensions = {};
  29872. int currentMips = 1;
  29873. //for post process shader
  29874. float bloomIntensty = 1;
  29875. bool highQualityDownSample = 0;
  29876. bool highQualityUpSample = 0;
  29877. bool chromaticAberationOn = 0;
  29878. float chromaticAberationStrength = 20.f;
  29879. float unfocusDistance = 5.f;
  29880. }postProcess;
  29881. //used for adaptive resolution or fxaa or both
  29882. struct AdaptiveResolution
  29883. {
  29884. void create(int w, int h);
  29885. void resize(int w, int h);
  29886. void clear();
  29887. static constexpr int timeSamplesCount = 20;
  29888. float msSampled[timeSamplesCount] = {};
  29889. int timeSample = 0;
  29890. float stepDownSecTarget = 17.f;
  29891. float stepUpSecTarget = 12.f;
  29892. glm::ivec2 currentDimensions = {};
  29893. float rezRatio = 1.f;
  29894. float maxScaleDown = 0.6;
  29895. bool useAdaptiveResolution = true;
  29896. bool shouldUseAdaptiveResolution = false;
  29897. GLuint texture;
  29898. GLuint fbo;
  29899. GLuint texture2;
  29900. GLuint fbo2;
  29901. }adaptiveResolution;
  29902. struct AntiAlias
  29903. {
  29904. Shader shader;
  29905. Shader noAAshader;
  29906. void create(ErrorReporter &errorReporter, FileOpener &fileOpener);
  29907. void clear();
  29908. GLuint u_texture;
  29909. GLuint noAAu_texture;
  29910. GLuint u_FXAAData;
  29911. GLuint fxaaDataBuffer;
  29912. FXAAData fxaaData;
  29913. bool usingFXAA = true;
  29914. }antiAlias;
  29915. ColorLookupTexture loadColorLookupTextureFromFile(const char *path);
  29916. struct CopyDepth
  29917. {
  29918. gl3d::Shader shader;
  29919. GLint u_depth = -1;
  29920. void create(ErrorReporter &errorReporter, FileOpener &fileOpener);
  29921. void clear();
  29922. }copyDepth;
  29923. struct DirectionalShadows
  29924. {
  29925. void create(GLuint frameBuffer);
  29926. void allocateTextures(int count);
  29927. void clear();
  29928. constexpr static int CASCADES = 3;
  29929. int textureCount = 0;
  29930. GLuint cascadesTexture;
  29931. GLuint staticGeometryTexture;
  29932. GLuint cascadesFbo;
  29933. GLuint staticGeometryFbo;
  29934. int shadowSize = 2048;
  29935. int currentShadowSize = 2048;
  29936. float frustumSplits[CASCADES] = { 0.01,0.03,0.1 };
  29937. }directionalShadows;
  29938. struct SpotShadows
  29939. {
  29940. void create(GLuint frameBuffer);
  29941. void allocateTextures(int count);
  29942. void clear();
  29943. int textureCount = 0;
  29944. GLuint shadowTextures;
  29945. GLuint staticGeometryTextures;
  29946. GLuint fbo;
  29947. GLuint staticGeometryfbo;
  29948. int shadowSize = 1024;
  29949. int currentShadowSize = 1024;
  29950. }spotShadows;
  29951. struct PointShadows
  29952. {
  29953. void create(GLuint frameBuffer);
  29954. void allocateTextures(int count);
  29955. void clear();
  29956. int textureCount = 0;
  29957. int shadowSize = 1024;
  29958. int currentShadowSize = 1024;
  29959. GLuint shadowTextures;
  29960. GLuint staticGeometryTextures;
  29961. GLuint fbo;
  29962. GLuint staticGeometryFbo;
  29963. }pointShadows;
  29964. void render(float deltaTime);
  29965. void updateWindowMetrics(int x, int y);
  29966. void clearAllLoadedResource();
  29967. //only cleares the current skybox!, other user created skyboxes won't be cleared
  29968. void clearAllRendererResources();
  29969. bool frustumCulling = 1;
  29970. bool zPrePass = 0;
  29971. bool copyDepthForLaterForwardRendering = 0;
  29972. };
  29973. };
  29974. #pragma endregion