json.hpp 194 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258
  1. // __ _____ _____ _____
  2. // __| | __| | | | JSON for Modern C++
  3. // | | |__ | | | | | | version 3.11.3
  4. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5. //
  6. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  7. // SPDX-License-Identifier: MIT
  8. /****************************************************************************\
  9. * Note on documentation: The source files contain links to the online *
  10. * documentation of the public API at https://json.nlohmann.me. This URL *
  11. * contains the most recent documentation and should also be applicable to *
  12. * previous versions; documentation for deprecated functions is not *
  13. * removed, but marked deprecated. See "Generate documentation" section in *
  14. * file docs/README.md. *
  15. \****************************************************************************/
  16. #ifndef INCLUDE_NLOHMANN_JSON_HPP_
  17. #define INCLUDE_NLOHMANN_JSON_HPP_
  18. #include <algorithm> // all_of, find, for_each
  19. #include <cstddef> // nullptr_t, ptrdiff_t, size_t
  20. #include <functional> // hash, less
  21. #include <initializer_list> // initializer_list
  22. #ifndef JSON_NO_IO
  23. #include <iosfwd> // istream, ostream
  24. #endif // JSON_NO_IO
  25. #include <iterator> // random_access_iterator_tag
  26. #include <memory> // unique_ptr
  27. #include <string> // string, stoi, to_string
  28. #include <utility> // declval, forward, move, pair, swap
  29. #include <vector> // vector
  30. #include <nlohmann/adl_serializer.hpp>
  31. #include <nlohmann/byte_container_with_subtype.hpp>
  32. #include <nlohmann/detail/conversions/from_json.hpp>
  33. #include <nlohmann/detail/conversions/to_json.hpp>
  34. #include <nlohmann/detail/exceptions.hpp>
  35. #include <nlohmann/detail/hash.hpp>
  36. #include <nlohmann/detail/input/binary_reader.hpp>
  37. #include <nlohmann/detail/input/input_adapters.hpp>
  38. #include <nlohmann/detail/input/lexer.hpp>
  39. #include <nlohmann/detail/input/parser.hpp>
  40. #include <nlohmann/detail/iterators/internal_iterator.hpp>
  41. #include <nlohmann/detail/iterators/iter_impl.hpp>
  42. #include <nlohmann/detail/iterators/iteration_proxy.hpp>
  43. #include <nlohmann/detail/iterators/json_reverse_iterator.hpp>
  44. #include <nlohmann/detail/iterators/primitive_iterator.hpp>
  45. #include <nlohmann/detail/json_custom_base_class.hpp>
  46. #include <nlohmann/detail/json_pointer.hpp>
  47. #include <nlohmann/detail/json_ref.hpp>
  48. #include <nlohmann/detail/macro_scope.hpp>
  49. #include <nlohmann/detail/string_concat.hpp>
  50. #include <nlohmann/detail/string_escape.hpp>
  51. #include <nlohmann/detail/meta/cpp_future.hpp>
  52. #include <nlohmann/detail/meta/type_traits.hpp>
  53. #include <nlohmann/detail/output/binary_writer.hpp>
  54. #include <nlohmann/detail/output/output_adapters.hpp>
  55. #include <nlohmann/detail/output/serializer.hpp>
  56. #include <nlohmann/detail/value_t.hpp>
  57. #include <nlohmann/json_fwd.hpp>
  58. #include <nlohmann/ordered_map.hpp>
  59. #if defined(JSON_HAS_CPP_17)
  60. #if JSON_HAS_STATIC_RTTI
  61. #include <any>
  62. #endif
  63. #include <string_view>
  64. #endif
  65. /*!
  66. @brief namespace for Niels Lohmann
  67. @see https://github.com/nlohmann
  68. @since version 1.0.0
  69. */
  70. NLOHMANN_JSON_NAMESPACE_BEGIN
  71. /*!
  72. @brief a class to store JSON values
  73. @internal
  74. @invariant The member variables @a m_value and @a m_type have the following
  75. relationship:
  76. - If `m_type == value_t::object`, then `m_value.object != nullptr`.
  77. - If `m_type == value_t::array`, then `m_value.array != nullptr`.
  78. - If `m_type == value_t::string`, then `m_value.string != nullptr`.
  79. The invariants are checked by member function assert_invariant().
  80. @note ObjectType trick from https://stackoverflow.com/a/9860911
  81. @endinternal
  82. @since version 1.0.0
  83. @nosubgrouping
  84. */
  85. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  86. class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions)
  87. : public ::nlohmann::detail::json_base_class<CustomBaseClass>
  88. {
  89. private:
  90. template<detail::value_t> friend struct detail::external_constructor;
  91. template<typename>
  92. friend class ::nlohmann::json_pointer;
  93. // can be restored when json_pointer backwards compatibility is removed
  94. // friend ::nlohmann::json_pointer<StringType>;
  95. template<typename BasicJsonType, typename InputType>
  96. friend class ::nlohmann::detail::parser;
  97. friend ::nlohmann::detail::serializer<basic_json>;
  98. template<typename BasicJsonType>
  99. friend class ::nlohmann::detail::iter_impl;
  100. template<typename BasicJsonType, typename CharType>
  101. friend class ::nlohmann::detail::binary_writer;
  102. template<typename BasicJsonType, typename InputType, typename SAX>
  103. friend class ::nlohmann::detail::binary_reader;
  104. template<typename BasicJsonType>
  105. friend class ::nlohmann::detail::json_sax_dom_parser;
  106. template<typename BasicJsonType>
  107. friend class ::nlohmann::detail::json_sax_dom_callback_parser;
  108. friend class ::nlohmann::detail::exception;
  109. /// workaround type for MSVC
  110. using basic_json_t = NLOHMANN_BASIC_JSON_TPL;
  111. using json_base_class_t = ::nlohmann::detail::json_base_class<CustomBaseClass>;
  112. JSON_PRIVATE_UNLESS_TESTED:
  113. // convenience aliases for types residing in namespace detail;
  114. using lexer = ::nlohmann::detail::lexer_base<basic_json>;
  115. template<typename InputAdapterType>
  116. static ::nlohmann::detail::parser<basic_json, InputAdapterType> parser(
  117. InputAdapterType adapter,
  118. detail::parser_callback_t<basic_json>cb = nullptr,
  119. const bool allow_exceptions = true,
  120. const bool ignore_comments = false
  121. )
  122. {
  123. return ::nlohmann::detail::parser<basic_json, InputAdapterType>(std::move(adapter),
  124. std::move(cb), allow_exceptions, ignore_comments);
  125. }
  126. private:
  127. using primitive_iterator_t = ::nlohmann::detail::primitive_iterator_t;
  128. template<typename BasicJsonType>
  129. using internal_iterator = ::nlohmann::detail::internal_iterator<BasicJsonType>;
  130. template<typename BasicJsonType>
  131. using iter_impl = ::nlohmann::detail::iter_impl<BasicJsonType>;
  132. template<typename Iterator>
  133. using iteration_proxy = ::nlohmann::detail::iteration_proxy<Iterator>;
  134. template<typename Base> using json_reverse_iterator = ::nlohmann::detail::json_reverse_iterator<Base>;
  135. template<typename CharType>
  136. using output_adapter_t = ::nlohmann::detail::output_adapter_t<CharType>;
  137. template<typename InputType>
  138. using binary_reader = ::nlohmann::detail::binary_reader<basic_json, InputType>;
  139. template<typename CharType> using binary_writer = ::nlohmann::detail::binary_writer<basic_json, CharType>;
  140. JSON_PRIVATE_UNLESS_TESTED:
  141. using serializer = ::nlohmann::detail::serializer<basic_json>;
  142. public:
  143. using value_t = detail::value_t;
  144. /// JSON Pointer, see @ref nlohmann::json_pointer
  145. using json_pointer = ::nlohmann::json_pointer<StringType>;
  146. template<typename T, typename SFINAE>
  147. using json_serializer = JSONSerializer<T, SFINAE>;
  148. /// how to treat decoding errors
  149. using error_handler_t = detail::error_handler_t;
  150. /// how to treat CBOR tags
  151. using cbor_tag_handler_t = detail::cbor_tag_handler_t;
  152. /// helper type for initializer lists of basic_json values
  153. using initializer_list_t = std::initializer_list<detail::json_ref<basic_json>>;
  154. using input_format_t = detail::input_format_t;
  155. /// SAX interface type, see @ref nlohmann::json_sax
  156. using json_sax_t = json_sax<basic_json>;
  157. ////////////////
  158. // exceptions //
  159. ////////////////
  160. /// @name exceptions
  161. /// Classes to implement user-defined exceptions.
  162. /// @{
  163. using exception = detail::exception;
  164. using parse_error = detail::parse_error;
  165. using invalid_iterator = detail::invalid_iterator;
  166. using type_error = detail::type_error;
  167. using out_of_range = detail::out_of_range;
  168. using other_error = detail::other_error;
  169. /// @}
  170. /////////////////////
  171. // container types //
  172. /////////////////////
  173. /// @name container types
  174. /// The canonic container types to use @ref basic_json like any other STL
  175. /// container.
  176. /// @{
  177. /// the type of elements in a basic_json container
  178. using value_type = basic_json;
  179. /// the type of an element reference
  180. using reference = value_type&;
  181. /// the type of an element const reference
  182. using const_reference = const value_type&;
  183. /// a type to represent differences between iterators
  184. using difference_type = std::ptrdiff_t;
  185. /// a type to represent container sizes
  186. using size_type = std::size_t;
  187. /// the allocator type
  188. using allocator_type = AllocatorType<basic_json>;
  189. /// the type of an element pointer
  190. using pointer = typename std::allocator_traits<allocator_type>::pointer;
  191. /// the type of an element const pointer
  192. using const_pointer = typename std::allocator_traits<allocator_type>::const_pointer;
  193. /// an iterator for a basic_json container
  194. using iterator = iter_impl<basic_json>;
  195. /// a const iterator for a basic_json container
  196. using const_iterator = iter_impl<const basic_json>;
  197. /// a reverse iterator for a basic_json container
  198. using reverse_iterator = json_reverse_iterator<typename basic_json::iterator>;
  199. /// a const reverse iterator for a basic_json container
  200. using const_reverse_iterator = json_reverse_iterator<typename basic_json::const_iterator>;
  201. /// @}
  202. /// @brief returns the allocator associated with the container
  203. /// @sa https://json.nlohmann.me/api/basic_json/get_allocator/
  204. static allocator_type get_allocator()
  205. {
  206. return allocator_type();
  207. }
  208. /// @brief returns version information on the library
  209. /// @sa https://json.nlohmann.me/api/basic_json/meta/
  210. JSON_HEDLEY_WARN_UNUSED_RESULT
  211. static basic_json meta()
  212. {
  213. basic_json result;
  214. result["copyright"] = "(C) 2013-2023 Niels Lohmann";
  215. result["name"] = "JSON for Modern C++";
  216. result["url"] = "https://github.com/nlohmann/json";
  217. result["version"]["string"] =
  218. detail::concat(std::to_string(NLOHMANN_JSON_VERSION_MAJOR), '.',
  219. std::to_string(NLOHMANN_JSON_VERSION_MINOR), '.',
  220. std::to_string(NLOHMANN_JSON_VERSION_PATCH));
  221. result["version"]["major"] = NLOHMANN_JSON_VERSION_MAJOR;
  222. result["version"]["minor"] = NLOHMANN_JSON_VERSION_MINOR;
  223. result["version"]["patch"] = NLOHMANN_JSON_VERSION_PATCH;
  224. #ifdef _WIN32
  225. result["platform"] = "win32";
  226. #elif defined __linux__
  227. result["platform"] = "linux";
  228. #elif defined __APPLE__
  229. result["platform"] = "apple";
  230. #elif defined __unix__
  231. result["platform"] = "unix";
  232. #else
  233. result["platform"] = "unknown";
  234. #endif
  235. #if defined(__ICC) || defined(__INTEL_COMPILER)
  236. result["compiler"] = {{"family", "icc"}, {"version", __INTEL_COMPILER}};
  237. #elif defined(__clang__)
  238. result["compiler"] = {{"family", "clang"}, {"version", __clang_version__}};
  239. #elif defined(__GNUC__) || defined(__GNUG__)
  240. result["compiler"] = {{"family", "gcc"}, {"version", detail::concat(
  241. std::to_string(__GNUC__), '.',
  242. std::to_string(__GNUC_MINOR__), '.',
  243. std::to_string(__GNUC_PATCHLEVEL__))
  244. }
  245. };
  246. #elif defined(__HP_cc) || defined(__HP_aCC)
  247. result["compiler"] = "hp"
  248. #elif defined(__IBMCPP__)
  249. result["compiler"] = {{"family", "ilecpp"}, {"version", __IBMCPP__}};
  250. #elif defined(_MSC_VER)
  251. result["compiler"] = {{"family", "msvc"}, {"version", _MSC_VER}};
  252. #elif defined(__PGI)
  253. result["compiler"] = {{"family", "pgcpp"}, {"version", __PGI}};
  254. #elif defined(__SUNPRO_CC)
  255. result["compiler"] = {{"family", "sunpro"}, {"version", __SUNPRO_CC}};
  256. #else
  257. result["compiler"] = {{"family", "unknown"}, {"version", "unknown"}};
  258. #endif
  259. #if defined(_MSVC_LANG)
  260. result["compiler"]["c++"] = std::to_string(_MSVC_LANG);
  261. #elif defined(__cplusplus)
  262. result["compiler"]["c++"] = std::to_string(__cplusplus);
  263. #else
  264. result["compiler"]["c++"] = "unknown";
  265. #endif
  266. return result;
  267. }
  268. ///////////////////////////
  269. // JSON value data types //
  270. ///////////////////////////
  271. /// @name JSON value data types
  272. /// The data types to store a JSON value. These types are derived from
  273. /// the template arguments passed to class @ref basic_json.
  274. /// @{
  275. /// @brief default object key comparator type
  276. /// The actual object key comparator type (@ref object_comparator_t) may be
  277. /// different.
  278. /// @sa https://json.nlohmann.me/api/basic_json/default_object_comparator_t/
  279. #if defined(JSON_HAS_CPP_14)
  280. // use of transparent comparator avoids unnecessary repeated construction of temporaries
  281. // in functions involving lookup by key with types other than object_t::key_type (aka. StringType)
  282. using default_object_comparator_t = std::less<>;
  283. #else
  284. using default_object_comparator_t = std::less<StringType>;
  285. #endif
  286. /// @brief a type for an object
  287. /// @sa https://json.nlohmann.me/api/basic_json/object_t/
  288. using object_t = ObjectType<StringType,
  289. basic_json,
  290. default_object_comparator_t,
  291. AllocatorType<std::pair<const StringType,
  292. basic_json>>>;
  293. /// @brief a type for an array
  294. /// @sa https://json.nlohmann.me/api/basic_json/array_t/
  295. using array_t = ArrayType<basic_json, AllocatorType<basic_json>>;
  296. /// @brief a type for a string
  297. /// @sa https://json.nlohmann.me/api/basic_json/string_t/
  298. using string_t = StringType;
  299. /// @brief a type for a boolean
  300. /// @sa https://json.nlohmann.me/api/basic_json/boolean_t/
  301. using boolean_t = BooleanType;
  302. /// @brief a type for a number (integer)
  303. /// @sa https://json.nlohmann.me/api/basic_json/number_integer_t/
  304. using number_integer_t = NumberIntegerType;
  305. /// @brief a type for a number (unsigned)
  306. /// @sa https://json.nlohmann.me/api/basic_json/number_unsigned_t/
  307. using number_unsigned_t = NumberUnsignedType;
  308. /// @brief a type for a number (floating-point)
  309. /// @sa https://json.nlohmann.me/api/basic_json/number_float_t/
  310. using number_float_t = NumberFloatType;
  311. /// @brief a type for a packed binary type
  312. /// @sa https://json.nlohmann.me/api/basic_json/binary_t/
  313. using binary_t = nlohmann::byte_container_with_subtype<BinaryType>;
  314. /// @brief object key comparator type
  315. /// @sa https://json.nlohmann.me/api/basic_json/object_comparator_t/
  316. using object_comparator_t = detail::actual_object_comparator_t<basic_json>;
  317. /// @}
  318. private:
  319. /// helper for exception-safe object creation
  320. template<typename T, typename... Args>
  321. JSON_HEDLEY_RETURNS_NON_NULL
  322. static T* create(Args&& ... args)
  323. {
  324. AllocatorType<T> alloc;
  325. using AllocatorTraits = std::allocator_traits<AllocatorType<T>>;
  326. auto deleter = [&](T * obj)
  327. {
  328. AllocatorTraits::deallocate(alloc, obj, 1);
  329. };
  330. std::unique_ptr<T, decltype(deleter)> obj(AllocatorTraits::allocate(alloc, 1), deleter);
  331. AllocatorTraits::construct(alloc, obj.get(), std::forward<Args>(args)...);
  332. JSON_ASSERT(obj != nullptr);
  333. return obj.release();
  334. }
  335. ////////////////////////
  336. // JSON value storage //
  337. ////////////////////////
  338. JSON_PRIVATE_UNLESS_TESTED:
  339. /*!
  340. @brief a JSON value
  341. The actual storage for a JSON value of the @ref basic_json class. This
  342. union combines the different storage types for the JSON value types
  343. defined in @ref value_t.
  344. JSON type | value_t type | used type
  345. --------- | --------------- | ------------------------
  346. object | object | pointer to @ref object_t
  347. array | array | pointer to @ref array_t
  348. string | string | pointer to @ref string_t
  349. boolean | boolean | @ref boolean_t
  350. number | number_integer | @ref number_integer_t
  351. number | number_unsigned | @ref number_unsigned_t
  352. number | number_float | @ref number_float_t
  353. binary | binary | pointer to @ref binary_t
  354. null | null | *no value is stored*
  355. @note Variable-length types (objects, arrays, and strings) are stored as
  356. pointers. The size of the union should not exceed 64 bits if the default
  357. value types are used.
  358. @since version 1.0.0
  359. */
  360. union json_value
  361. {
  362. /// object (stored with pointer to save storage)
  363. object_t* object;
  364. /// array (stored with pointer to save storage)
  365. array_t* array;
  366. /// string (stored with pointer to save storage)
  367. string_t* string;
  368. /// binary (stored with pointer to save storage)
  369. binary_t* binary;
  370. /// boolean
  371. boolean_t boolean;
  372. /// number (integer)
  373. number_integer_t number_integer;
  374. /// number (unsigned integer)
  375. number_unsigned_t number_unsigned;
  376. /// number (floating-point)
  377. number_float_t number_float;
  378. /// default constructor (for null values)
  379. json_value() = default;
  380. /// constructor for booleans
  381. json_value(boolean_t v) noexcept : boolean(v) {}
  382. /// constructor for numbers (integer)
  383. json_value(number_integer_t v) noexcept : number_integer(v) {}
  384. /// constructor for numbers (unsigned)
  385. json_value(number_unsigned_t v) noexcept : number_unsigned(v) {}
  386. /// constructor for numbers (floating-point)
  387. json_value(number_float_t v) noexcept : number_float(v) {}
  388. /// constructor for empty values of a given type
  389. json_value(value_t t)
  390. {
  391. switch (t)
  392. {
  393. case value_t::object:
  394. {
  395. object = create<object_t>();
  396. break;
  397. }
  398. case value_t::array:
  399. {
  400. array = create<array_t>();
  401. break;
  402. }
  403. case value_t::string:
  404. {
  405. string = create<string_t>("");
  406. break;
  407. }
  408. case value_t::binary:
  409. {
  410. binary = create<binary_t>();
  411. break;
  412. }
  413. case value_t::boolean:
  414. {
  415. boolean = static_cast<boolean_t>(false);
  416. break;
  417. }
  418. case value_t::number_integer:
  419. {
  420. number_integer = static_cast<number_integer_t>(0);
  421. break;
  422. }
  423. case value_t::number_unsigned:
  424. {
  425. number_unsigned = static_cast<number_unsigned_t>(0);
  426. break;
  427. }
  428. case value_t::number_float:
  429. {
  430. number_float = static_cast<number_float_t>(0.0);
  431. break;
  432. }
  433. case value_t::null:
  434. {
  435. object = nullptr; // silence warning, see #821
  436. break;
  437. }
  438. case value_t::discarded:
  439. default:
  440. {
  441. object = nullptr; // silence warning, see #821
  442. if (JSON_HEDLEY_UNLIKELY(t == value_t::null))
  443. {
  444. JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.11.3", nullptr)); // LCOV_EXCL_LINE
  445. }
  446. break;
  447. }
  448. }
  449. }
  450. /// constructor for strings
  451. json_value(const string_t& value) : string(create<string_t>(value)) {}
  452. /// constructor for rvalue strings
  453. json_value(string_t&& value) : string(create<string_t>(std::move(value))) {}
  454. /// constructor for objects
  455. json_value(const object_t& value) : object(create<object_t>(value)) {}
  456. /// constructor for rvalue objects
  457. json_value(object_t&& value) : object(create<object_t>(std::move(value))) {}
  458. /// constructor for arrays
  459. json_value(const array_t& value) : array(create<array_t>(value)) {}
  460. /// constructor for rvalue arrays
  461. json_value(array_t&& value) : array(create<array_t>(std::move(value))) {}
  462. /// constructor for binary arrays
  463. json_value(const typename binary_t::container_type& value) : binary(create<binary_t>(value)) {}
  464. /// constructor for rvalue binary arrays
  465. json_value(typename binary_t::container_type&& value) : binary(create<binary_t>(std::move(value))) {}
  466. /// constructor for binary arrays (internal type)
  467. json_value(const binary_t& value) : binary(create<binary_t>(value)) {}
  468. /// constructor for rvalue binary arrays (internal type)
  469. json_value(binary_t&& value) : binary(create<binary_t>(std::move(value))) {}
  470. void destroy(value_t t)
  471. {
  472. if (
  473. (t == value_t::object && object == nullptr) ||
  474. (t == value_t::array && array == nullptr) ||
  475. (t == value_t::string && string == nullptr) ||
  476. (t == value_t::binary && binary == nullptr)
  477. )
  478. {
  479. //not initialized (e.g. due to exception in the ctor)
  480. return;
  481. }
  482. if (t == value_t::array || t == value_t::object)
  483. {
  484. // flatten the current json_value to a heap-allocated stack
  485. std::vector<basic_json> stack;
  486. // move the top-level items to stack
  487. if (t == value_t::array)
  488. {
  489. stack.reserve(array->size());
  490. std::move(array->begin(), array->end(), std::back_inserter(stack));
  491. }
  492. else
  493. {
  494. stack.reserve(object->size());
  495. for (auto&& it : *object)
  496. {
  497. stack.push_back(std::move(it.second));
  498. }
  499. }
  500. while (!stack.empty())
  501. {
  502. // move the last item to local variable to be processed
  503. basic_json current_item(std::move(stack.back()));
  504. stack.pop_back();
  505. // if current_item is array/object, move
  506. // its children to the stack to be processed later
  507. if (current_item.is_array())
  508. {
  509. std::move(current_item.m_data.m_value.array->begin(), current_item.m_data.m_value.array->end(), std::back_inserter(stack));
  510. current_item.m_data.m_value.array->clear();
  511. }
  512. else if (current_item.is_object())
  513. {
  514. for (auto&& it : *current_item.m_data.m_value.object)
  515. {
  516. stack.push_back(std::move(it.second));
  517. }
  518. current_item.m_data.m_value.object->clear();
  519. }
  520. // it's now safe that current_item get destructed
  521. // since it doesn't have any children
  522. }
  523. }
  524. switch (t)
  525. {
  526. case value_t::object:
  527. {
  528. AllocatorType<object_t> alloc;
  529. std::allocator_traits<decltype(alloc)>::destroy(alloc, object);
  530. std::allocator_traits<decltype(alloc)>::deallocate(alloc, object, 1);
  531. break;
  532. }
  533. case value_t::array:
  534. {
  535. AllocatorType<array_t> alloc;
  536. std::allocator_traits<decltype(alloc)>::destroy(alloc, array);
  537. std::allocator_traits<decltype(alloc)>::deallocate(alloc, array, 1);
  538. break;
  539. }
  540. case value_t::string:
  541. {
  542. AllocatorType<string_t> alloc;
  543. std::allocator_traits<decltype(alloc)>::destroy(alloc, string);
  544. std::allocator_traits<decltype(alloc)>::deallocate(alloc, string, 1);
  545. break;
  546. }
  547. case value_t::binary:
  548. {
  549. AllocatorType<binary_t> alloc;
  550. std::allocator_traits<decltype(alloc)>::destroy(alloc, binary);
  551. std::allocator_traits<decltype(alloc)>::deallocate(alloc, binary, 1);
  552. break;
  553. }
  554. case value_t::null:
  555. case value_t::boolean:
  556. case value_t::number_integer:
  557. case value_t::number_unsigned:
  558. case value_t::number_float:
  559. case value_t::discarded:
  560. default:
  561. {
  562. break;
  563. }
  564. }
  565. }
  566. };
  567. private:
  568. /*!
  569. @brief checks the class invariants
  570. This function asserts the class invariants. It needs to be called at the
  571. end of every constructor to make sure that created objects respect the
  572. invariant. Furthermore, it has to be called each time the type of a JSON
  573. value is changed, because the invariant expresses a relationship between
  574. @a m_type and @a m_value.
  575. Furthermore, the parent relation is checked for arrays and objects: If
  576. @a check_parents true and the value is an array or object, then the
  577. container's elements must have the current value as parent.
  578. @param[in] check_parents whether the parent relation should be checked.
  579. The value is true by default and should only be set to false
  580. during destruction of objects when the invariant does not
  581. need to hold.
  582. */
  583. void assert_invariant(bool check_parents = true) const noexcept
  584. {
  585. JSON_ASSERT(m_data.m_type != value_t::object || m_data.m_value.object != nullptr);
  586. JSON_ASSERT(m_data.m_type != value_t::array || m_data.m_value.array != nullptr);
  587. JSON_ASSERT(m_data.m_type != value_t::string || m_data.m_value.string != nullptr);
  588. JSON_ASSERT(m_data.m_type != value_t::binary || m_data.m_value.binary != nullptr);
  589. #if JSON_DIAGNOSTICS
  590. JSON_TRY
  591. {
  592. // cppcheck-suppress assertWithSideEffect
  593. JSON_ASSERT(!check_parents || !is_structured() || std::all_of(begin(), end(), [this](const basic_json & j)
  594. {
  595. return j.m_parent == this;
  596. }));
  597. }
  598. JSON_CATCH(...) {} // LCOV_EXCL_LINE
  599. #endif
  600. static_cast<void>(check_parents);
  601. }
  602. void set_parents()
  603. {
  604. #if JSON_DIAGNOSTICS
  605. switch (m_data.m_type)
  606. {
  607. case value_t::array:
  608. {
  609. for (auto& element : *m_data.m_value.array)
  610. {
  611. element.m_parent = this;
  612. }
  613. break;
  614. }
  615. case value_t::object:
  616. {
  617. for (auto& element : *m_data.m_value.object)
  618. {
  619. element.second.m_parent = this;
  620. }
  621. break;
  622. }
  623. case value_t::null:
  624. case value_t::string:
  625. case value_t::boolean:
  626. case value_t::number_integer:
  627. case value_t::number_unsigned:
  628. case value_t::number_float:
  629. case value_t::binary:
  630. case value_t::discarded:
  631. default:
  632. break;
  633. }
  634. #endif
  635. }
  636. iterator set_parents(iterator it, typename iterator::difference_type count_set_parents)
  637. {
  638. #if JSON_DIAGNOSTICS
  639. for (typename iterator::difference_type i = 0; i < count_set_parents; ++i)
  640. {
  641. (it + i)->m_parent = this;
  642. }
  643. #else
  644. static_cast<void>(count_set_parents);
  645. #endif
  646. return it;
  647. }
  648. reference set_parent(reference j, std::size_t old_capacity = static_cast<std::size_t>(-1))
  649. {
  650. #if JSON_DIAGNOSTICS
  651. if (old_capacity != static_cast<std::size_t>(-1))
  652. {
  653. // see https://github.com/nlohmann/json/issues/2838
  654. JSON_ASSERT(type() == value_t::array);
  655. if (JSON_HEDLEY_UNLIKELY(m_data.m_value.array->capacity() != old_capacity))
  656. {
  657. // capacity has changed: update all parents
  658. set_parents();
  659. return j;
  660. }
  661. }
  662. // ordered_json uses a vector internally, so pointers could have
  663. // been invalidated; see https://github.com/nlohmann/json/issues/2962
  664. #ifdef JSON_HEDLEY_MSVC_VERSION
  665. #pragma warning(push )
  666. #pragma warning(disable : 4127) // ignore warning to replace if with if constexpr
  667. #endif
  668. if (detail::is_ordered_map<object_t>::value)
  669. {
  670. set_parents();
  671. return j;
  672. }
  673. #ifdef JSON_HEDLEY_MSVC_VERSION
  674. #pragma warning( pop )
  675. #endif
  676. j.m_parent = this;
  677. #else
  678. static_cast<void>(j);
  679. static_cast<void>(old_capacity);
  680. #endif
  681. return j;
  682. }
  683. public:
  684. //////////////////////////
  685. // JSON parser callback //
  686. //////////////////////////
  687. /// @brief parser event types
  688. /// @sa https://json.nlohmann.me/api/basic_json/parse_event_t/
  689. using parse_event_t = detail::parse_event_t;
  690. /// @brief per-element parser callback type
  691. /// @sa https://json.nlohmann.me/api/basic_json/parser_callback_t/
  692. using parser_callback_t = detail::parser_callback_t<basic_json>;
  693. //////////////////
  694. // constructors //
  695. //////////////////
  696. /// @name constructors and destructors
  697. /// Constructors of class @ref basic_json, copy/move constructor, copy
  698. /// assignment, static functions creating objects, and the destructor.
  699. /// @{
  700. /// @brief create an empty value with a given type
  701. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  702. basic_json(const value_t v)
  703. : m_data(v)
  704. {
  705. assert_invariant();
  706. }
  707. /// @brief create a null object
  708. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  709. basic_json(std::nullptr_t = nullptr) noexcept // NOLINT(bugprone-exception-escape)
  710. : basic_json(value_t::null)
  711. {
  712. assert_invariant();
  713. }
  714. /// @brief create a JSON value from compatible types
  715. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  716. template < typename CompatibleType,
  717. typename U = detail::uncvref_t<CompatibleType>,
  718. detail::enable_if_t <
  719. !detail::is_basic_json<U>::value && detail::is_compatible_type<basic_json_t, U>::value, int > = 0 >
  720. basic_json(CompatibleType && val) noexcept(noexcept( // NOLINT(bugprone-forwarding-reference-overload,bugprone-exception-escape)
  721. JSONSerializer<U>::to_json(std::declval<basic_json_t&>(),
  722. std::forward<CompatibleType>(val))))
  723. {
  724. JSONSerializer<U>::to_json(*this, std::forward<CompatibleType>(val));
  725. set_parents();
  726. assert_invariant();
  727. }
  728. /// @brief create a JSON value from an existing one
  729. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  730. template < typename BasicJsonType,
  731. detail::enable_if_t <
  732. detail::is_basic_json<BasicJsonType>::value&& !std::is_same<basic_json, BasicJsonType>::value, int > = 0 >
  733. basic_json(const BasicJsonType& val)
  734. {
  735. using other_boolean_t = typename BasicJsonType::boolean_t;
  736. using other_number_float_t = typename BasicJsonType::number_float_t;
  737. using other_number_integer_t = typename BasicJsonType::number_integer_t;
  738. using other_number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  739. using other_string_t = typename BasicJsonType::string_t;
  740. using other_object_t = typename BasicJsonType::object_t;
  741. using other_array_t = typename BasicJsonType::array_t;
  742. using other_binary_t = typename BasicJsonType::binary_t;
  743. switch (val.type())
  744. {
  745. case value_t::boolean:
  746. JSONSerializer<other_boolean_t>::to_json(*this, val.template get<other_boolean_t>());
  747. break;
  748. case value_t::number_float:
  749. JSONSerializer<other_number_float_t>::to_json(*this, val.template get<other_number_float_t>());
  750. break;
  751. case value_t::number_integer:
  752. JSONSerializer<other_number_integer_t>::to_json(*this, val.template get<other_number_integer_t>());
  753. break;
  754. case value_t::number_unsigned:
  755. JSONSerializer<other_number_unsigned_t>::to_json(*this, val.template get<other_number_unsigned_t>());
  756. break;
  757. case value_t::string:
  758. JSONSerializer<other_string_t>::to_json(*this, val.template get_ref<const other_string_t&>());
  759. break;
  760. case value_t::object:
  761. JSONSerializer<other_object_t>::to_json(*this, val.template get_ref<const other_object_t&>());
  762. break;
  763. case value_t::array:
  764. JSONSerializer<other_array_t>::to_json(*this, val.template get_ref<const other_array_t&>());
  765. break;
  766. case value_t::binary:
  767. JSONSerializer<other_binary_t>::to_json(*this, val.template get_ref<const other_binary_t&>());
  768. break;
  769. case value_t::null:
  770. *this = nullptr;
  771. break;
  772. case value_t::discarded:
  773. m_data.m_type = value_t::discarded;
  774. break;
  775. default: // LCOV_EXCL_LINE
  776. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  777. }
  778. JSON_ASSERT(m_data.m_type == val.type());
  779. set_parents();
  780. assert_invariant();
  781. }
  782. /// @brief create a container (array or object) from an initializer list
  783. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  784. basic_json(initializer_list_t init,
  785. bool type_deduction = true,
  786. value_t manual_type = value_t::array)
  787. {
  788. // check if each element is an array with two elements whose first
  789. // element is a string
  790. bool is_an_object = std::all_of(init.begin(), init.end(),
  791. [](const detail::json_ref<basic_json>& element_ref)
  792. {
  793. // The cast is to ensure op[size_type] is called, bearing in mind size_type may not be int;
  794. // (many string types can be constructed from 0 via its null-pointer guise, so we get a
  795. // broken call to op[key_type], the wrong semantics and a 4804 warning on Windows)
  796. return element_ref->is_array() && element_ref->size() == 2 && (*element_ref)[static_cast<size_type>(0)].is_string();
  797. });
  798. // adjust type if type deduction is not wanted
  799. if (!type_deduction)
  800. {
  801. // if array is wanted, do not create an object though possible
  802. if (manual_type == value_t::array)
  803. {
  804. is_an_object = false;
  805. }
  806. // if object is wanted but impossible, throw an exception
  807. if (JSON_HEDLEY_UNLIKELY(manual_type == value_t::object && !is_an_object))
  808. {
  809. JSON_THROW(type_error::create(301, "cannot create object from initializer list", nullptr));
  810. }
  811. }
  812. if (is_an_object)
  813. {
  814. // the initializer list is a list of pairs -> create object
  815. m_data.m_type = value_t::object;
  816. m_data.m_value = value_t::object;
  817. for (auto& element_ref : init)
  818. {
  819. auto element = element_ref.moved_or_copied();
  820. m_data.m_value.object->emplace(
  821. std::move(*((*element.m_data.m_value.array)[0].m_data.m_value.string)),
  822. std::move((*element.m_data.m_value.array)[1]));
  823. }
  824. }
  825. else
  826. {
  827. // the initializer list describes an array -> create array
  828. m_data.m_type = value_t::array;
  829. m_data.m_value.array = create<array_t>(init.begin(), init.end());
  830. }
  831. set_parents();
  832. assert_invariant();
  833. }
  834. /// @brief explicitly create a binary array (without subtype)
  835. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  836. JSON_HEDLEY_WARN_UNUSED_RESULT
  837. static basic_json binary(const typename binary_t::container_type& init)
  838. {
  839. auto res = basic_json();
  840. res.m_data.m_type = value_t::binary;
  841. res.m_data.m_value = init;
  842. return res;
  843. }
  844. /// @brief explicitly create a binary array (with subtype)
  845. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  846. JSON_HEDLEY_WARN_UNUSED_RESULT
  847. static basic_json binary(const typename binary_t::container_type& init, typename binary_t::subtype_type subtype)
  848. {
  849. auto res = basic_json();
  850. res.m_data.m_type = value_t::binary;
  851. res.m_data.m_value = binary_t(init, subtype);
  852. return res;
  853. }
  854. /// @brief explicitly create a binary array
  855. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  856. JSON_HEDLEY_WARN_UNUSED_RESULT
  857. static basic_json binary(typename binary_t::container_type&& init)
  858. {
  859. auto res = basic_json();
  860. res.m_data.m_type = value_t::binary;
  861. res.m_data.m_value = std::move(init);
  862. return res;
  863. }
  864. /// @brief explicitly create a binary array (with subtype)
  865. /// @sa https://json.nlohmann.me/api/basic_json/binary/
  866. JSON_HEDLEY_WARN_UNUSED_RESULT
  867. static basic_json binary(typename binary_t::container_type&& init, typename binary_t::subtype_type subtype)
  868. {
  869. auto res = basic_json();
  870. res.m_data.m_type = value_t::binary;
  871. res.m_data.m_value = binary_t(std::move(init), subtype);
  872. return res;
  873. }
  874. /// @brief explicitly create an array from an initializer list
  875. /// @sa https://json.nlohmann.me/api/basic_json/array/
  876. JSON_HEDLEY_WARN_UNUSED_RESULT
  877. static basic_json array(initializer_list_t init = {})
  878. {
  879. return basic_json(init, false, value_t::array);
  880. }
  881. /// @brief explicitly create an object from an initializer list
  882. /// @sa https://json.nlohmann.me/api/basic_json/object/
  883. JSON_HEDLEY_WARN_UNUSED_RESULT
  884. static basic_json object(initializer_list_t init = {})
  885. {
  886. return basic_json(init, false, value_t::object);
  887. }
  888. /// @brief construct an array with count copies of given value
  889. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  890. basic_json(size_type cnt, const basic_json& val):
  891. m_data{cnt, val}
  892. {
  893. set_parents();
  894. assert_invariant();
  895. }
  896. /// @brief construct a JSON container given an iterator range
  897. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  898. template < class InputIT, typename std::enable_if <
  899. std::is_same<InputIT, typename basic_json_t::iterator>::value ||
  900. std::is_same<InputIT, typename basic_json_t::const_iterator>::value, int >::type = 0 >
  901. basic_json(InputIT first, InputIT last)
  902. {
  903. JSON_ASSERT(first.m_object != nullptr);
  904. JSON_ASSERT(last.m_object != nullptr);
  905. // make sure iterator fits the current value
  906. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  907. {
  908. JSON_THROW(invalid_iterator::create(201, "iterators are not compatible", nullptr));
  909. }
  910. // copy type from first iterator
  911. m_data.m_type = first.m_object->m_data.m_type;
  912. // check if iterator range is complete for primitive values
  913. switch (m_data.m_type)
  914. {
  915. case value_t::boolean:
  916. case value_t::number_float:
  917. case value_t::number_integer:
  918. case value_t::number_unsigned:
  919. case value_t::string:
  920. {
  921. if (JSON_HEDLEY_UNLIKELY(!first.m_it.primitive_iterator.is_begin()
  922. || !last.m_it.primitive_iterator.is_end()))
  923. {
  924. JSON_THROW(invalid_iterator::create(204, "iterators out of range", first.m_object));
  925. }
  926. break;
  927. }
  928. case value_t::null:
  929. case value_t::object:
  930. case value_t::array:
  931. case value_t::binary:
  932. case value_t::discarded:
  933. default:
  934. break;
  935. }
  936. switch (m_data.m_type)
  937. {
  938. case value_t::number_integer:
  939. {
  940. m_data.m_value.number_integer = first.m_object->m_data.m_value.number_integer;
  941. break;
  942. }
  943. case value_t::number_unsigned:
  944. {
  945. m_data.m_value.number_unsigned = first.m_object->m_data.m_value.number_unsigned;
  946. break;
  947. }
  948. case value_t::number_float:
  949. {
  950. m_data.m_value.number_float = first.m_object->m_data.m_value.number_float;
  951. break;
  952. }
  953. case value_t::boolean:
  954. {
  955. m_data.m_value.boolean = first.m_object->m_data.m_value.boolean;
  956. break;
  957. }
  958. case value_t::string:
  959. {
  960. m_data.m_value = *first.m_object->m_data.m_value.string;
  961. break;
  962. }
  963. case value_t::object:
  964. {
  965. m_data.m_value.object = create<object_t>(first.m_it.object_iterator,
  966. last.m_it.object_iterator);
  967. break;
  968. }
  969. case value_t::array:
  970. {
  971. m_data.m_value.array = create<array_t>(first.m_it.array_iterator,
  972. last.m_it.array_iterator);
  973. break;
  974. }
  975. case value_t::binary:
  976. {
  977. m_data.m_value = *first.m_object->m_data.m_value.binary;
  978. break;
  979. }
  980. case value_t::null:
  981. case value_t::discarded:
  982. default:
  983. JSON_THROW(invalid_iterator::create(206, detail::concat("cannot construct with iterators from ", first.m_object->type_name()), first.m_object));
  984. }
  985. set_parents();
  986. assert_invariant();
  987. }
  988. ///////////////////////////////////////
  989. // other constructors and destructor //
  990. ///////////////////////////////////////
  991. template<typename JsonRef,
  992. detail::enable_if_t<detail::conjunction<detail::is_json_ref<JsonRef>,
  993. std::is_same<typename JsonRef::value_type, basic_json>>::value, int> = 0 >
  994. basic_json(const JsonRef& ref) : basic_json(ref.moved_or_copied()) {}
  995. /// @brief copy constructor
  996. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  997. basic_json(const basic_json& other)
  998. : json_base_class_t(other)
  999. {
  1000. m_data.m_type = other.m_data.m_type;
  1001. // check of passed value is valid
  1002. other.assert_invariant();
  1003. switch (m_data.m_type)
  1004. {
  1005. case value_t::object:
  1006. {
  1007. m_data.m_value = *other.m_data.m_value.object;
  1008. break;
  1009. }
  1010. case value_t::array:
  1011. {
  1012. m_data.m_value = *other.m_data.m_value.array;
  1013. break;
  1014. }
  1015. case value_t::string:
  1016. {
  1017. m_data.m_value = *other.m_data.m_value.string;
  1018. break;
  1019. }
  1020. case value_t::boolean:
  1021. {
  1022. m_data.m_value = other.m_data.m_value.boolean;
  1023. break;
  1024. }
  1025. case value_t::number_integer:
  1026. {
  1027. m_data.m_value = other.m_data.m_value.number_integer;
  1028. break;
  1029. }
  1030. case value_t::number_unsigned:
  1031. {
  1032. m_data.m_value = other.m_data.m_value.number_unsigned;
  1033. break;
  1034. }
  1035. case value_t::number_float:
  1036. {
  1037. m_data.m_value = other.m_data.m_value.number_float;
  1038. break;
  1039. }
  1040. case value_t::binary:
  1041. {
  1042. m_data.m_value = *other.m_data.m_value.binary;
  1043. break;
  1044. }
  1045. case value_t::null:
  1046. case value_t::discarded:
  1047. default:
  1048. break;
  1049. }
  1050. set_parents();
  1051. assert_invariant();
  1052. }
  1053. /// @brief move constructor
  1054. /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
  1055. basic_json(basic_json&& other) noexcept
  1056. : json_base_class_t(std::forward<json_base_class_t>(other)),
  1057. m_data(std::move(other.m_data))
  1058. {
  1059. // check that passed value is valid
  1060. other.assert_invariant(false);
  1061. // invalidate payload
  1062. other.m_data.m_type = value_t::null;
  1063. other.m_data.m_value = {};
  1064. set_parents();
  1065. assert_invariant();
  1066. }
  1067. /// @brief copy assignment
  1068. /// @sa https://json.nlohmann.me/api/basic_json/operator=/
  1069. basic_json& operator=(basic_json other) noexcept (
  1070. std::is_nothrow_move_constructible<value_t>::value&&
  1071. std::is_nothrow_move_assignable<value_t>::value&&
  1072. std::is_nothrow_move_constructible<json_value>::value&&
  1073. std::is_nothrow_move_assignable<json_value>::value&&
  1074. std::is_nothrow_move_assignable<json_base_class_t>::value
  1075. )
  1076. {
  1077. // check that passed value is valid
  1078. other.assert_invariant();
  1079. using std::swap;
  1080. swap(m_data.m_type, other.m_data.m_type);
  1081. swap(m_data.m_value, other.m_data.m_value);
  1082. json_base_class_t::operator=(std::move(other));
  1083. set_parents();
  1084. assert_invariant();
  1085. return *this;
  1086. }
  1087. /// @brief destructor
  1088. /// @sa https://json.nlohmann.me/api/basic_json/~basic_json/
  1089. ~basic_json() noexcept
  1090. {
  1091. assert_invariant(false);
  1092. }
  1093. /// @}
  1094. public:
  1095. ///////////////////////
  1096. // object inspection //
  1097. ///////////////////////
  1098. /// @name object inspection
  1099. /// Functions to inspect the type of a JSON value.
  1100. /// @{
  1101. /// @brief serialization
  1102. /// @sa https://json.nlohmann.me/api/basic_json/dump/
  1103. string_t dump(const int indent = -1,
  1104. const char indent_char = ' ',
  1105. const bool ensure_ascii = false,
  1106. const error_handler_t error_handler = error_handler_t::strict) const
  1107. {
  1108. string_t result;
  1109. serializer s(detail::output_adapter<char, string_t>(result), indent_char, error_handler);
  1110. if (indent >= 0)
  1111. {
  1112. s.dump(*this, true, ensure_ascii, static_cast<unsigned int>(indent));
  1113. }
  1114. else
  1115. {
  1116. s.dump(*this, false, ensure_ascii, 0);
  1117. }
  1118. return result;
  1119. }
  1120. /// @brief return the type of the JSON value (explicit)
  1121. /// @sa https://json.nlohmann.me/api/basic_json/type/
  1122. constexpr value_t type() const noexcept
  1123. {
  1124. return m_data.m_type;
  1125. }
  1126. /// @brief return whether type is primitive
  1127. /// @sa https://json.nlohmann.me/api/basic_json/is_primitive/
  1128. constexpr bool is_primitive() const noexcept
  1129. {
  1130. return is_null() || is_string() || is_boolean() || is_number() || is_binary();
  1131. }
  1132. /// @brief return whether type is structured
  1133. /// @sa https://json.nlohmann.me/api/basic_json/is_structured/
  1134. constexpr bool is_structured() const noexcept
  1135. {
  1136. return is_array() || is_object();
  1137. }
  1138. /// @brief return whether value is null
  1139. /// @sa https://json.nlohmann.me/api/basic_json/is_null/
  1140. constexpr bool is_null() const noexcept
  1141. {
  1142. return m_data.m_type == value_t::null;
  1143. }
  1144. /// @brief return whether value is a boolean
  1145. /// @sa https://json.nlohmann.me/api/basic_json/is_boolean/
  1146. constexpr bool is_boolean() const noexcept
  1147. {
  1148. return m_data.m_type == value_t::boolean;
  1149. }
  1150. /// @brief return whether value is a number
  1151. /// @sa https://json.nlohmann.me/api/basic_json/is_number/
  1152. constexpr bool is_number() const noexcept
  1153. {
  1154. return is_number_integer() || is_number_float();
  1155. }
  1156. /// @brief return whether value is an integer number
  1157. /// @sa https://json.nlohmann.me/api/basic_json/is_number_integer/
  1158. constexpr bool is_number_integer() const noexcept
  1159. {
  1160. return m_data.m_type == value_t::number_integer || m_data.m_type == value_t::number_unsigned;
  1161. }
  1162. /// @brief return whether value is an unsigned integer number
  1163. /// @sa https://json.nlohmann.me/api/basic_json/is_number_unsigned/
  1164. constexpr bool is_number_unsigned() const noexcept
  1165. {
  1166. return m_data.m_type == value_t::number_unsigned;
  1167. }
  1168. /// @brief return whether value is a floating-point number
  1169. /// @sa https://json.nlohmann.me/api/basic_json/is_number_float/
  1170. constexpr bool is_number_float() const noexcept
  1171. {
  1172. return m_data.m_type == value_t::number_float;
  1173. }
  1174. /// @brief return whether value is an object
  1175. /// @sa https://json.nlohmann.me/api/basic_json/is_object/
  1176. constexpr bool is_object() const noexcept
  1177. {
  1178. return m_data.m_type == value_t::object;
  1179. }
  1180. /// @brief return whether value is an array
  1181. /// @sa https://json.nlohmann.me/api/basic_json/is_array/
  1182. constexpr bool is_array() const noexcept
  1183. {
  1184. return m_data.m_type == value_t::array;
  1185. }
  1186. /// @brief return whether value is a string
  1187. /// @sa https://json.nlohmann.me/api/basic_json/is_string/
  1188. constexpr bool is_string() const noexcept
  1189. {
  1190. return m_data.m_type == value_t::string;
  1191. }
  1192. /// @brief return whether value is a binary array
  1193. /// @sa https://json.nlohmann.me/api/basic_json/is_binary/
  1194. constexpr bool is_binary() const noexcept
  1195. {
  1196. return m_data.m_type == value_t::binary;
  1197. }
  1198. /// @brief return whether value is discarded
  1199. /// @sa https://json.nlohmann.me/api/basic_json/is_discarded/
  1200. constexpr bool is_discarded() const noexcept
  1201. {
  1202. return m_data.m_type == value_t::discarded;
  1203. }
  1204. /// @brief return the type of the JSON value (implicit)
  1205. /// @sa https://json.nlohmann.me/api/basic_json/operator_value_t/
  1206. constexpr operator value_t() const noexcept
  1207. {
  1208. return m_data.m_type;
  1209. }
  1210. /// @}
  1211. private:
  1212. //////////////////
  1213. // value access //
  1214. //////////////////
  1215. /// get a boolean (explicit)
  1216. boolean_t get_impl(boolean_t* /*unused*/) const
  1217. {
  1218. if (JSON_HEDLEY_LIKELY(is_boolean()))
  1219. {
  1220. return m_data.m_value.boolean;
  1221. }
  1222. JSON_THROW(type_error::create(302, detail::concat("type must be boolean, but is ", type_name()), this));
  1223. }
  1224. /// get a pointer to the value (object)
  1225. object_t* get_impl_ptr(object_t* /*unused*/) noexcept
  1226. {
  1227. return is_object() ? m_data.m_value.object : nullptr;
  1228. }
  1229. /// get a pointer to the value (object)
  1230. constexpr const object_t* get_impl_ptr(const object_t* /*unused*/) const noexcept
  1231. {
  1232. return is_object() ? m_data.m_value.object : nullptr;
  1233. }
  1234. /// get a pointer to the value (array)
  1235. array_t* get_impl_ptr(array_t* /*unused*/) noexcept
  1236. {
  1237. return is_array() ? m_data.m_value.array : nullptr;
  1238. }
  1239. /// get a pointer to the value (array)
  1240. constexpr const array_t* get_impl_ptr(const array_t* /*unused*/) const noexcept
  1241. {
  1242. return is_array() ? m_data.m_value.array : nullptr;
  1243. }
  1244. /// get a pointer to the value (string)
  1245. string_t* get_impl_ptr(string_t* /*unused*/) noexcept
  1246. {
  1247. return is_string() ? m_data.m_value.string : nullptr;
  1248. }
  1249. /// get a pointer to the value (string)
  1250. constexpr const string_t* get_impl_ptr(const string_t* /*unused*/) const noexcept
  1251. {
  1252. return is_string() ? m_data.m_value.string : nullptr;
  1253. }
  1254. /// get a pointer to the value (boolean)
  1255. boolean_t* get_impl_ptr(boolean_t* /*unused*/) noexcept
  1256. {
  1257. return is_boolean() ? &m_data.m_value.boolean : nullptr;
  1258. }
  1259. /// get a pointer to the value (boolean)
  1260. constexpr const boolean_t* get_impl_ptr(const boolean_t* /*unused*/) const noexcept
  1261. {
  1262. return is_boolean() ? &m_data.m_value.boolean : nullptr;
  1263. }
  1264. /// get a pointer to the value (integer number)
  1265. number_integer_t* get_impl_ptr(number_integer_t* /*unused*/) noexcept
  1266. {
  1267. return is_number_integer() ? &m_data.m_value.number_integer : nullptr;
  1268. }
  1269. /// get a pointer to the value (integer number)
  1270. constexpr const number_integer_t* get_impl_ptr(const number_integer_t* /*unused*/) const noexcept
  1271. {
  1272. return is_number_integer() ? &m_data.m_value.number_integer : nullptr;
  1273. }
  1274. /// get a pointer to the value (unsigned number)
  1275. number_unsigned_t* get_impl_ptr(number_unsigned_t* /*unused*/) noexcept
  1276. {
  1277. return is_number_unsigned() ? &m_data.m_value.number_unsigned : nullptr;
  1278. }
  1279. /// get a pointer to the value (unsigned number)
  1280. constexpr const number_unsigned_t* get_impl_ptr(const number_unsigned_t* /*unused*/) const noexcept
  1281. {
  1282. return is_number_unsigned() ? &m_data.m_value.number_unsigned : nullptr;
  1283. }
  1284. /// get a pointer to the value (floating-point number)
  1285. number_float_t* get_impl_ptr(number_float_t* /*unused*/) noexcept
  1286. {
  1287. return is_number_float() ? &m_data.m_value.number_float : nullptr;
  1288. }
  1289. /// get a pointer to the value (floating-point number)
  1290. constexpr const number_float_t* get_impl_ptr(const number_float_t* /*unused*/) const noexcept
  1291. {
  1292. return is_number_float() ? &m_data.m_value.number_float : nullptr;
  1293. }
  1294. /// get a pointer to the value (binary)
  1295. binary_t* get_impl_ptr(binary_t* /*unused*/) noexcept
  1296. {
  1297. return is_binary() ? m_data.m_value.binary : nullptr;
  1298. }
  1299. /// get a pointer to the value (binary)
  1300. constexpr const binary_t* get_impl_ptr(const binary_t* /*unused*/) const noexcept
  1301. {
  1302. return is_binary() ? m_data.m_value.binary : nullptr;
  1303. }
  1304. /*!
  1305. @brief helper function to implement get_ref()
  1306. This function helps to implement get_ref() without code duplication for
  1307. const and non-const overloads
  1308. @tparam ThisType will be deduced as `basic_json` or `const basic_json`
  1309. @throw type_error.303 if ReferenceType does not match underlying value
  1310. type of the current JSON
  1311. */
  1312. template<typename ReferenceType, typename ThisType>
  1313. static ReferenceType get_ref_impl(ThisType& obj)
  1314. {
  1315. // delegate the call to get_ptr<>()
  1316. auto* ptr = obj.template get_ptr<typename std::add_pointer<ReferenceType>::type>();
  1317. if (JSON_HEDLEY_LIKELY(ptr != nullptr))
  1318. {
  1319. return *ptr;
  1320. }
  1321. JSON_THROW(type_error::create(303, detail::concat("incompatible ReferenceType for get_ref, actual type is ", obj.type_name()), &obj));
  1322. }
  1323. public:
  1324. /// @name value access
  1325. /// Direct access to the stored value of a JSON value.
  1326. /// @{
  1327. /// @brief get a pointer value (implicit)
  1328. /// @sa https://json.nlohmann.me/api/basic_json/get_ptr/
  1329. template<typename PointerType, typename std::enable_if<
  1330. std::is_pointer<PointerType>::value, int>::type = 0>
  1331. auto get_ptr() noexcept -> decltype(std::declval<basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
  1332. {
  1333. // delegate the call to get_impl_ptr<>()
  1334. return get_impl_ptr(static_cast<PointerType>(nullptr));
  1335. }
  1336. /// @brief get a pointer value (implicit)
  1337. /// @sa https://json.nlohmann.me/api/basic_json/get_ptr/
  1338. template < typename PointerType, typename std::enable_if <
  1339. std::is_pointer<PointerType>::value&&
  1340. std::is_const<typename std::remove_pointer<PointerType>::type>::value, int >::type = 0 >
  1341. constexpr auto get_ptr() const noexcept -> decltype(std::declval<const basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
  1342. {
  1343. // delegate the call to get_impl_ptr<>() const
  1344. return get_impl_ptr(static_cast<PointerType>(nullptr));
  1345. }
  1346. private:
  1347. /*!
  1348. @brief get a value (explicit)
  1349. Explicit type conversion between the JSON value and a compatible value
  1350. which is [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)
  1351. and [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible).
  1352. The value is converted by calling the @ref json_serializer<ValueType>
  1353. `from_json()` method.
  1354. The function is equivalent to executing
  1355. @code {.cpp}
  1356. ValueType ret;
  1357. JSONSerializer<ValueType>::from_json(*this, ret);
  1358. return ret;
  1359. @endcode
  1360. This overloads is chosen if:
  1361. - @a ValueType is not @ref basic_json,
  1362. - @ref json_serializer<ValueType> has a `from_json()` method of the form
  1363. `void from_json(const basic_json&, ValueType&)`, and
  1364. - @ref json_serializer<ValueType> does not have a `from_json()` method of
  1365. the form `ValueType from_json(const basic_json&)`
  1366. @tparam ValueType the returned value type
  1367. @return copy of the JSON value, converted to @a ValueType
  1368. @throw what @ref json_serializer<ValueType> `from_json()` method throws
  1369. @liveexample{The example below shows several conversions from JSON values
  1370. to other types. There a few things to note: (1) Floating-point numbers can
  1371. be converted to integers\, (2) A JSON array can be converted to a standard
  1372. `std::vector<short>`\, (3) A JSON object can be converted to C++
  1373. associative containers such as `std::unordered_map<std::string\,
  1374. json>`.,get__ValueType_const}
  1375. @since version 2.1.0
  1376. */
  1377. template < typename ValueType,
  1378. detail::enable_if_t <
  1379. detail::is_default_constructible<ValueType>::value&&
  1380. detail::has_from_json<basic_json_t, ValueType>::value,
  1381. int > = 0 >
  1382. ValueType get_impl(detail::priority_tag<0> /*unused*/) const noexcept(noexcept(
  1383. JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), std::declval<ValueType&>())))
  1384. {
  1385. auto ret = ValueType();
  1386. JSONSerializer<ValueType>::from_json(*this, ret);
  1387. return ret;
  1388. }
  1389. /*!
  1390. @brief get a value (explicit); special case
  1391. Explicit type conversion between the JSON value and a compatible value
  1392. which is **not** [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)
  1393. and **not** [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible).
  1394. The value is converted by calling the @ref json_serializer<ValueType>
  1395. `from_json()` method.
  1396. The function is equivalent to executing
  1397. @code {.cpp}
  1398. return JSONSerializer<ValueType>::from_json(*this);
  1399. @endcode
  1400. This overloads is chosen if:
  1401. - @a ValueType is not @ref basic_json and
  1402. - @ref json_serializer<ValueType> has a `from_json()` method of the form
  1403. `ValueType from_json(const basic_json&)`
  1404. @note If @ref json_serializer<ValueType> has both overloads of
  1405. `from_json()`, this one is chosen.
  1406. @tparam ValueType the returned value type
  1407. @return copy of the JSON value, converted to @a ValueType
  1408. @throw what @ref json_serializer<ValueType> `from_json()` method throws
  1409. @since version 2.1.0
  1410. */
  1411. template < typename ValueType,
  1412. detail::enable_if_t <
  1413. detail::has_non_default_from_json<basic_json_t, ValueType>::value,
  1414. int > = 0 >
  1415. ValueType get_impl(detail::priority_tag<1> /*unused*/) const noexcept(noexcept(
  1416. JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>())))
  1417. {
  1418. return JSONSerializer<ValueType>::from_json(*this);
  1419. }
  1420. /*!
  1421. @brief get special-case overload
  1422. This overloads converts the current @ref basic_json in a different
  1423. @ref basic_json type
  1424. @tparam BasicJsonType == @ref basic_json
  1425. @return a copy of *this, converted into @a BasicJsonType
  1426. @complexity Depending on the implementation of the called `from_json()`
  1427. method.
  1428. @since version 3.2.0
  1429. */
  1430. template < typename BasicJsonType,
  1431. detail::enable_if_t <
  1432. detail::is_basic_json<BasicJsonType>::value,
  1433. int > = 0 >
  1434. BasicJsonType get_impl(detail::priority_tag<2> /*unused*/) const
  1435. {
  1436. return *this;
  1437. }
  1438. /*!
  1439. @brief get special-case overload
  1440. This overloads avoids a lot of template boilerplate, it can be seen as the
  1441. identity method
  1442. @tparam BasicJsonType == @ref basic_json
  1443. @return a copy of *this
  1444. @complexity Constant.
  1445. @since version 2.1.0
  1446. */
  1447. template<typename BasicJsonType,
  1448. detail::enable_if_t<
  1449. std::is_same<BasicJsonType, basic_json_t>::value,
  1450. int> = 0>
  1451. basic_json get_impl(detail::priority_tag<3> /*unused*/) const
  1452. {
  1453. return *this;
  1454. }
  1455. /*!
  1456. @brief get a pointer value (explicit)
  1457. @copydoc get()
  1458. */
  1459. template<typename PointerType,
  1460. detail::enable_if_t<
  1461. std::is_pointer<PointerType>::value,
  1462. int> = 0>
  1463. constexpr auto get_impl(detail::priority_tag<4> /*unused*/) const noexcept
  1464. -> decltype(std::declval<const basic_json_t&>().template get_ptr<PointerType>())
  1465. {
  1466. // delegate the call to get_ptr
  1467. return get_ptr<PointerType>();
  1468. }
  1469. public:
  1470. /*!
  1471. @brief get a (pointer) value (explicit)
  1472. Performs explicit type conversion between the JSON value and a compatible value if required.
  1473. - If the requested type is a pointer to the internally stored JSON value that pointer is returned.
  1474. No copies are made.
  1475. - If the requested type is the current @ref basic_json, or a different @ref basic_json convertible
  1476. from the current @ref basic_json.
  1477. - Otherwise the value is converted by calling the @ref json_serializer<ValueType> `from_json()`
  1478. method.
  1479. @tparam ValueTypeCV the provided value type
  1480. @tparam ValueType the returned value type
  1481. @return copy of the JSON value, converted to @tparam ValueType if necessary
  1482. @throw what @ref json_serializer<ValueType> `from_json()` method throws if conversion is required
  1483. @since version 2.1.0
  1484. */
  1485. template < typename ValueTypeCV, typename ValueType = detail::uncvref_t<ValueTypeCV>>
  1486. #if defined(JSON_HAS_CPP_14)
  1487. constexpr
  1488. #endif
  1489. auto get() const noexcept(
  1490. noexcept(std::declval<const basic_json_t&>().template get_impl<ValueType>(detail::priority_tag<4> {})))
  1491. -> decltype(std::declval<const basic_json_t&>().template get_impl<ValueType>(detail::priority_tag<4> {}))
  1492. {
  1493. // we cannot static_assert on ValueTypeCV being non-const, because
  1494. // there is support for get<const basic_json_t>(), which is why we
  1495. // still need the uncvref
  1496. static_assert(!std::is_reference<ValueTypeCV>::value,
  1497. "get() cannot be used with reference types, you might want to use get_ref()");
  1498. return get_impl<ValueType>(detail::priority_tag<4> {});
  1499. }
  1500. /*!
  1501. @brief get a pointer value (explicit)
  1502. Explicit pointer access to the internally stored JSON value. No copies are
  1503. made.
  1504. @warning The pointer becomes invalid if the underlying JSON object
  1505. changes.
  1506. @tparam PointerType pointer type; must be a pointer to @ref array_t, @ref
  1507. object_t, @ref string_t, @ref boolean_t, @ref number_integer_t,
  1508. @ref number_unsigned_t, or @ref number_float_t.
  1509. @return pointer to the internally stored JSON value if the requested
  1510. pointer type @a PointerType fits to the JSON value; `nullptr` otherwise
  1511. @complexity Constant.
  1512. @liveexample{The example below shows how pointers to internal values of a
  1513. JSON value can be requested. Note that no type conversions are made and a
  1514. `nullptr` is returned if the value and the requested pointer type does not
  1515. match.,get__PointerType}
  1516. @sa see @ref get_ptr() for explicit pointer-member access
  1517. @since version 1.0.0
  1518. */
  1519. template<typename PointerType, typename std::enable_if<
  1520. std::is_pointer<PointerType>::value, int>::type = 0>
  1521. auto get() noexcept -> decltype(std::declval<basic_json_t&>().template get_ptr<PointerType>())
  1522. {
  1523. // delegate the call to get_ptr
  1524. return get_ptr<PointerType>();
  1525. }
  1526. /// @brief get a value (explicit)
  1527. /// @sa https://json.nlohmann.me/api/basic_json/get_to/
  1528. template < typename ValueType,
  1529. detail::enable_if_t <
  1530. !detail::is_basic_json<ValueType>::value&&
  1531. detail::has_from_json<basic_json_t, ValueType>::value,
  1532. int > = 0 >
  1533. ValueType & get_to(ValueType& v) const noexcept(noexcept(
  1534. JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), v)))
  1535. {
  1536. JSONSerializer<ValueType>::from_json(*this, v);
  1537. return v;
  1538. }
  1539. // specialization to allow calling get_to with a basic_json value
  1540. // see https://github.com/nlohmann/json/issues/2175
  1541. template<typename ValueType,
  1542. detail::enable_if_t <
  1543. detail::is_basic_json<ValueType>::value,
  1544. int> = 0>
  1545. ValueType & get_to(ValueType& v) const
  1546. {
  1547. v = *this;
  1548. return v;
  1549. }
  1550. template <
  1551. typename T, std::size_t N,
  1552. typename Array = T (&)[N], // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  1553. detail::enable_if_t <
  1554. detail::has_from_json<basic_json_t, Array>::value, int > = 0 >
  1555. Array get_to(T (&v)[N]) const // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  1556. noexcept(noexcept(JSONSerializer<Array>::from_json(
  1557. std::declval<const basic_json_t&>(), v)))
  1558. {
  1559. JSONSerializer<Array>::from_json(*this, v);
  1560. return v;
  1561. }
  1562. /// @brief get a reference value (implicit)
  1563. /// @sa https://json.nlohmann.me/api/basic_json/get_ref/
  1564. template<typename ReferenceType, typename std::enable_if<
  1565. std::is_reference<ReferenceType>::value, int>::type = 0>
  1566. ReferenceType get_ref()
  1567. {
  1568. // delegate call to get_ref_impl
  1569. return get_ref_impl<ReferenceType>(*this);
  1570. }
  1571. /// @brief get a reference value (implicit)
  1572. /// @sa https://json.nlohmann.me/api/basic_json/get_ref/
  1573. template < typename ReferenceType, typename std::enable_if <
  1574. std::is_reference<ReferenceType>::value&&
  1575. std::is_const<typename std::remove_reference<ReferenceType>::type>::value, int >::type = 0 >
  1576. ReferenceType get_ref() const
  1577. {
  1578. // delegate call to get_ref_impl
  1579. return get_ref_impl<ReferenceType>(*this);
  1580. }
  1581. /*!
  1582. @brief get a value (implicit)
  1583. Implicit type conversion between the JSON value and a compatible value.
  1584. The call is realized by calling @ref get() const.
  1585. @tparam ValueType non-pointer type compatible to the JSON value, for
  1586. instance `int` for JSON integer numbers, `bool` for JSON booleans, or
  1587. `std::vector` types for JSON arrays. The character type of @ref string_t
  1588. as well as an initializer list of this type is excluded to avoid
  1589. ambiguities as these types implicitly convert to `std::string`.
  1590. @return copy of the JSON value, converted to type @a ValueType
  1591. @throw type_error.302 in case passed type @a ValueType is incompatible
  1592. to the JSON value type (e.g., the JSON value is of type boolean, but a
  1593. string is requested); see example below
  1594. @complexity Linear in the size of the JSON value.
  1595. @liveexample{The example below shows several conversions from JSON values
  1596. to other types. There a few things to note: (1) Floating-point numbers can
  1597. be converted to integers\, (2) A JSON array can be converted to a standard
  1598. `std::vector<short>`\, (3) A JSON object can be converted to C++
  1599. associative containers such as `std::unordered_map<std::string\,
  1600. json>`.,operator__ValueType}
  1601. @since version 1.0.0
  1602. */
  1603. template < typename ValueType, typename std::enable_if <
  1604. detail::conjunction <
  1605. detail::negation<std::is_pointer<ValueType>>,
  1606. detail::negation<std::is_same<ValueType, std::nullptr_t>>,
  1607. detail::negation<std::is_same<ValueType, detail::json_ref<basic_json>>>,
  1608. detail::negation<std::is_same<ValueType, typename string_t::value_type>>,
  1609. detail::negation<detail::is_basic_json<ValueType>>,
  1610. detail::negation<std::is_same<ValueType, std::initializer_list<typename string_t::value_type>>>,
  1611. #if defined(JSON_HAS_CPP_17) && (defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER <= 1914))
  1612. detail::negation<std::is_same<ValueType, std::string_view>>,
  1613. #endif
  1614. #if defined(JSON_HAS_CPP_17) && JSON_HAS_STATIC_RTTI
  1615. detail::negation<std::is_same<ValueType, std::any>>,
  1616. #endif
  1617. detail::is_detected_lazy<detail::get_template_function, const basic_json_t&, ValueType>
  1618. >::value, int >::type = 0 >
  1619. JSON_EXPLICIT operator ValueType() const
  1620. {
  1621. // delegate the call to get<>() const
  1622. return get<ValueType>();
  1623. }
  1624. /// @brief get a binary value
  1625. /// @sa https://json.nlohmann.me/api/basic_json/get_binary/
  1626. binary_t& get_binary()
  1627. {
  1628. if (!is_binary())
  1629. {
  1630. JSON_THROW(type_error::create(302, detail::concat("type must be binary, but is ", type_name()), this));
  1631. }
  1632. return *get_ptr<binary_t*>();
  1633. }
  1634. /// @brief get a binary value
  1635. /// @sa https://json.nlohmann.me/api/basic_json/get_binary/
  1636. const binary_t& get_binary() const
  1637. {
  1638. if (!is_binary())
  1639. {
  1640. JSON_THROW(type_error::create(302, detail::concat("type must be binary, but is ", type_name()), this));
  1641. }
  1642. return *get_ptr<const binary_t*>();
  1643. }
  1644. /// @}
  1645. ////////////////////
  1646. // element access //
  1647. ////////////////////
  1648. /// @name element access
  1649. /// Access to the JSON value.
  1650. /// @{
  1651. /// @brief access specified array element with bounds checking
  1652. /// @sa https://json.nlohmann.me/api/basic_json/at/
  1653. reference at(size_type idx)
  1654. {
  1655. // at only works for arrays
  1656. if (JSON_HEDLEY_LIKELY(is_array()))
  1657. {
  1658. JSON_TRY
  1659. {
  1660. return set_parent(m_data.m_value.array->at(idx));
  1661. }
  1662. JSON_CATCH (std::out_of_range&)
  1663. {
  1664. // create better exception explanation
  1665. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
  1666. }
  1667. }
  1668. else
  1669. {
  1670. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  1671. }
  1672. }
  1673. /// @brief access specified array element with bounds checking
  1674. /// @sa https://json.nlohmann.me/api/basic_json/at/
  1675. const_reference at(size_type idx) const
  1676. {
  1677. // at only works for arrays
  1678. if (JSON_HEDLEY_LIKELY(is_array()))
  1679. {
  1680. JSON_TRY
  1681. {
  1682. return m_data.m_value.array->at(idx);
  1683. }
  1684. JSON_CATCH (std::out_of_range&)
  1685. {
  1686. // create better exception explanation
  1687. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
  1688. }
  1689. }
  1690. else
  1691. {
  1692. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  1693. }
  1694. }
  1695. /// @brief access specified object element with bounds checking
  1696. /// @sa https://json.nlohmann.me/api/basic_json/at/
  1697. reference at(const typename object_t::key_type& key)
  1698. {
  1699. // at only works for objects
  1700. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  1701. {
  1702. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  1703. }
  1704. auto it = m_data.m_value.object->find(key);
  1705. if (it == m_data.m_value.object->end())
  1706. {
  1707. JSON_THROW(out_of_range::create(403, detail::concat("key '", key, "' not found"), this));
  1708. }
  1709. return set_parent(it->second);
  1710. }
  1711. /// @brief access specified object element with bounds checking
  1712. /// @sa https://json.nlohmann.me/api/basic_json/at/
  1713. template<class KeyType, detail::enable_if_t<
  1714. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  1715. reference at(KeyType && key)
  1716. {
  1717. // at only works for objects
  1718. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  1719. {
  1720. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  1721. }
  1722. auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
  1723. if (it == m_data.m_value.object->end())
  1724. {
  1725. JSON_THROW(out_of_range::create(403, detail::concat("key '", string_t(std::forward<KeyType>(key)), "' not found"), this));
  1726. }
  1727. return set_parent(it->second);
  1728. }
  1729. /// @brief access specified object element with bounds checking
  1730. /// @sa https://json.nlohmann.me/api/basic_json/at/
  1731. const_reference at(const typename object_t::key_type& key) const
  1732. {
  1733. // at only works for objects
  1734. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  1735. {
  1736. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  1737. }
  1738. auto it = m_data.m_value.object->find(key);
  1739. if (it == m_data.m_value.object->end())
  1740. {
  1741. JSON_THROW(out_of_range::create(403, detail::concat("key '", key, "' not found"), this));
  1742. }
  1743. return it->second;
  1744. }
  1745. /// @brief access specified object element with bounds checking
  1746. /// @sa https://json.nlohmann.me/api/basic_json/at/
  1747. template<class KeyType, detail::enable_if_t<
  1748. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  1749. const_reference at(KeyType && key) const
  1750. {
  1751. // at only works for objects
  1752. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  1753. {
  1754. JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
  1755. }
  1756. auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
  1757. if (it == m_data.m_value.object->end())
  1758. {
  1759. JSON_THROW(out_of_range::create(403, detail::concat("key '", string_t(std::forward<KeyType>(key)), "' not found"), this));
  1760. }
  1761. return it->second;
  1762. }
  1763. /// @brief access specified array element
  1764. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  1765. reference operator[](size_type idx)
  1766. {
  1767. // implicitly convert null value to an empty array
  1768. if (is_null())
  1769. {
  1770. m_data.m_type = value_t::array;
  1771. m_data.m_value.array = create<array_t>();
  1772. assert_invariant();
  1773. }
  1774. // operator[] only works for arrays
  1775. if (JSON_HEDLEY_LIKELY(is_array()))
  1776. {
  1777. // fill up array with null values if given idx is outside range
  1778. if (idx >= m_data.m_value.array->size())
  1779. {
  1780. #if JSON_DIAGNOSTICS
  1781. // remember array size & capacity before resizing
  1782. const auto old_size = m_data.m_value.array->size();
  1783. const auto old_capacity = m_data.m_value.array->capacity();
  1784. #endif
  1785. m_data.m_value.array->resize(idx + 1);
  1786. #if JSON_DIAGNOSTICS
  1787. if (JSON_HEDLEY_UNLIKELY(m_data.m_value.array->capacity() != old_capacity))
  1788. {
  1789. // capacity has changed: update all parents
  1790. set_parents();
  1791. }
  1792. else
  1793. {
  1794. // set parent for values added above
  1795. set_parents(begin() + static_cast<typename iterator::difference_type>(old_size), static_cast<typename iterator::difference_type>(idx + 1 - old_size));
  1796. }
  1797. #endif
  1798. assert_invariant();
  1799. }
  1800. return m_data.m_value.array->operator[](idx);
  1801. }
  1802. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a numeric argument with ", type_name()), this));
  1803. }
  1804. /// @brief access specified array element
  1805. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  1806. const_reference operator[](size_type idx) const
  1807. {
  1808. // const operator[] only works for arrays
  1809. if (JSON_HEDLEY_LIKELY(is_array()))
  1810. {
  1811. return m_data.m_value.array->operator[](idx);
  1812. }
  1813. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a numeric argument with ", type_name()), this));
  1814. }
  1815. /// @brief access specified object element
  1816. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  1817. reference operator[](typename object_t::key_type key)
  1818. {
  1819. // implicitly convert null value to an empty object
  1820. if (is_null())
  1821. {
  1822. m_data.m_type = value_t::object;
  1823. m_data.m_value.object = create<object_t>();
  1824. assert_invariant();
  1825. }
  1826. // operator[] only works for objects
  1827. if (JSON_HEDLEY_LIKELY(is_object()))
  1828. {
  1829. auto result = m_data.m_value.object->emplace(std::move(key), nullptr);
  1830. return set_parent(result.first->second);
  1831. }
  1832. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  1833. }
  1834. /// @brief access specified object element
  1835. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  1836. const_reference operator[](const typename object_t::key_type& key) const
  1837. {
  1838. // const operator[] only works for objects
  1839. if (JSON_HEDLEY_LIKELY(is_object()))
  1840. {
  1841. auto it = m_data.m_value.object->find(key);
  1842. JSON_ASSERT(it != m_data.m_value.object->end());
  1843. return it->second;
  1844. }
  1845. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  1846. }
  1847. // these two functions resolve a (const) char * ambiguity affecting Clang and MSVC
  1848. // (they seemingly cannot be constrained to resolve the ambiguity)
  1849. template<typename T>
  1850. reference operator[](T* key)
  1851. {
  1852. return operator[](typename object_t::key_type(key));
  1853. }
  1854. template<typename T>
  1855. const_reference operator[](T* key) const
  1856. {
  1857. return operator[](typename object_t::key_type(key));
  1858. }
  1859. /// @brief access specified object element
  1860. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  1861. template<class KeyType, detail::enable_if_t<
  1862. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int > = 0 >
  1863. reference operator[](KeyType && key)
  1864. {
  1865. // implicitly convert null value to an empty object
  1866. if (is_null())
  1867. {
  1868. m_data.m_type = value_t::object;
  1869. m_data.m_value.object = create<object_t>();
  1870. assert_invariant();
  1871. }
  1872. // operator[] only works for objects
  1873. if (JSON_HEDLEY_LIKELY(is_object()))
  1874. {
  1875. auto result = m_data.m_value.object->emplace(std::forward<KeyType>(key), nullptr);
  1876. return set_parent(result.first->second);
  1877. }
  1878. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  1879. }
  1880. /// @brief access specified object element
  1881. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  1882. template<class KeyType, detail::enable_if_t<
  1883. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int > = 0 >
  1884. const_reference operator[](KeyType && key) const
  1885. {
  1886. // const operator[] only works for objects
  1887. if (JSON_HEDLEY_LIKELY(is_object()))
  1888. {
  1889. auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
  1890. JSON_ASSERT(it != m_data.m_value.object->end());
  1891. return it->second;
  1892. }
  1893. JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
  1894. }
  1895. private:
  1896. template<typename KeyType>
  1897. using is_comparable_with_object_key = detail::is_comparable <
  1898. object_comparator_t, const typename object_t::key_type&, KeyType >;
  1899. template<typename ValueType>
  1900. using value_return_type = std::conditional <
  1901. detail::is_c_string_uncvref<ValueType>::value,
  1902. string_t, typename std::decay<ValueType>::type >;
  1903. public:
  1904. /// @brief access specified object element with default value
  1905. /// @sa https://json.nlohmann.me/api/basic_json/value/
  1906. template < class ValueType, detail::enable_if_t <
  1907. !detail::is_transparent<object_comparator_t>::value
  1908. && detail::is_getable<basic_json_t, ValueType>::value
  1909. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  1910. ValueType value(const typename object_t::key_type& key, const ValueType& default_value) const
  1911. {
  1912. // value only works for objects
  1913. if (JSON_HEDLEY_LIKELY(is_object()))
  1914. {
  1915. // if key is found, return value and given default value otherwise
  1916. const auto it = find(key);
  1917. if (it != end())
  1918. {
  1919. return it->template get<ValueType>();
  1920. }
  1921. return default_value;
  1922. }
  1923. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  1924. }
  1925. /// @brief access specified object element with default value
  1926. /// @sa https://json.nlohmann.me/api/basic_json/value/
  1927. template < class ValueType, class ReturnType = typename value_return_type<ValueType>::type,
  1928. detail::enable_if_t <
  1929. !detail::is_transparent<object_comparator_t>::value
  1930. && detail::is_getable<basic_json_t, ReturnType>::value
  1931. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  1932. ReturnType value(const typename object_t::key_type& key, ValueType && default_value) const
  1933. {
  1934. // value only works for objects
  1935. if (JSON_HEDLEY_LIKELY(is_object()))
  1936. {
  1937. // if key is found, return value and given default value otherwise
  1938. const auto it = find(key);
  1939. if (it != end())
  1940. {
  1941. return it->template get<ReturnType>();
  1942. }
  1943. return std::forward<ValueType>(default_value);
  1944. }
  1945. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  1946. }
  1947. /// @brief access specified object element with default value
  1948. /// @sa https://json.nlohmann.me/api/basic_json/value/
  1949. template < class ValueType, class KeyType, detail::enable_if_t <
  1950. detail::is_transparent<object_comparator_t>::value
  1951. && !detail::is_json_pointer<KeyType>::value
  1952. && is_comparable_with_object_key<KeyType>::value
  1953. && detail::is_getable<basic_json_t, ValueType>::value
  1954. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  1955. ValueType value(KeyType && key, const ValueType& default_value) const
  1956. {
  1957. // value only works for objects
  1958. if (JSON_HEDLEY_LIKELY(is_object()))
  1959. {
  1960. // if key is found, return value and given default value otherwise
  1961. const auto it = find(std::forward<KeyType>(key));
  1962. if (it != end())
  1963. {
  1964. return it->template get<ValueType>();
  1965. }
  1966. return default_value;
  1967. }
  1968. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  1969. }
  1970. /// @brief access specified object element via JSON Pointer with default value
  1971. /// @sa https://json.nlohmann.me/api/basic_json/value/
  1972. template < class ValueType, class KeyType, class ReturnType = typename value_return_type<ValueType>::type,
  1973. detail::enable_if_t <
  1974. detail::is_transparent<object_comparator_t>::value
  1975. && !detail::is_json_pointer<KeyType>::value
  1976. && is_comparable_with_object_key<KeyType>::value
  1977. && detail::is_getable<basic_json_t, ReturnType>::value
  1978. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  1979. ReturnType value(KeyType && key, ValueType && default_value) const
  1980. {
  1981. // value only works for objects
  1982. if (JSON_HEDLEY_LIKELY(is_object()))
  1983. {
  1984. // if key is found, return value and given default value otherwise
  1985. const auto it = find(std::forward<KeyType>(key));
  1986. if (it != end())
  1987. {
  1988. return it->template get<ReturnType>();
  1989. }
  1990. return std::forward<ValueType>(default_value);
  1991. }
  1992. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  1993. }
  1994. /// @brief access specified object element via JSON Pointer with default value
  1995. /// @sa https://json.nlohmann.me/api/basic_json/value/
  1996. template < class ValueType, detail::enable_if_t <
  1997. detail::is_getable<basic_json_t, ValueType>::value
  1998. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  1999. ValueType value(const json_pointer& ptr, const ValueType& default_value) const
  2000. {
  2001. // value only works for objects
  2002. if (JSON_HEDLEY_LIKELY(is_object()))
  2003. {
  2004. // if pointer resolves a value, return it or use default value
  2005. JSON_TRY
  2006. {
  2007. return ptr.get_checked(this).template get<ValueType>();
  2008. }
  2009. JSON_INTERNAL_CATCH (out_of_range&)
  2010. {
  2011. return default_value;
  2012. }
  2013. }
  2014. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  2015. }
  2016. /// @brief access specified object element via JSON Pointer with default value
  2017. /// @sa https://json.nlohmann.me/api/basic_json/value/
  2018. template < class ValueType, class ReturnType = typename value_return_type<ValueType>::type,
  2019. detail::enable_if_t <
  2020. detail::is_getable<basic_json_t, ReturnType>::value
  2021. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  2022. ReturnType value(const json_pointer& ptr, ValueType && default_value) const
  2023. {
  2024. // value only works for objects
  2025. if (JSON_HEDLEY_LIKELY(is_object()))
  2026. {
  2027. // if pointer resolves a value, return it or use default value
  2028. JSON_TRY
  2029. {
  2030. return ptr.get_checked(this).template get<ReturnType>();
  2031. }
  2032. JSON_INTERNAL_CATCH (out_of_range&)
  2033. {
  2034. return std::forward<ValueType>(default_value);
  2035. }
  2036. }
  2037. JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
  2038. }
  2039. template < class ValueType, class BasicJsonType, detail::enable_if_t <
  2040. detail::is_basic_json<BasicJsonType>::value
  2041. && detail::is_getable<basic_json_t, ValueType>::value
  2042. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  2043. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  2044. ValueType value(const ::nlohmann::json_pointer<BasicJsonType>& ptr, const ValueType& default_value) const
  2045. {
  2046. return value(ptr.convert(), default_value);
  2047. }
  2048. template < class ValueType, class BasicJsonType, class ReturnType = typename value_return_type<ValueType>::type,
  2049. detail::enable_if_t <
  2050. detail::is_basic_json<BasicJsonType>::value
  2051. && detail::is_getable<basic_json_t, ReturnType>::value
  2052. && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
  2053. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  2054. ReturnType value(const ::nlohmann::json_pointer<BasicJsonType>& ptr, ValueType && default_value) const
  2055. {
  2056. return value(ptr.convert(), std::forward<ValueType>(default_value));
  2057. }
  2058. /// @brief access the first element
  2059. /// @sa https://json.nlohmann.me/api/basic_json/front/
  2060. reference front()
  2061. {
  2062. return *begin();
  2063. }
  2064. /// @brief access the first element
  2065. /// @sa https://json.nlohmann.me/api/basic_json/front/
  2066. const_reference front() const
  2067. {
  2068. return *cbegin();
  2069. }
  2070. /// @brief access the last element
  2071. /// @sa https://json.nlohmann.me/api/basic_json/back/
  2072. reference back()
  2073. {
  2074. auto tmp = end();
  2075. --tmp;
  2076. return *tmp;
  2077. }
  2078. /// @brief access the last element
  2079. /// @sa https://json.nlohmann.me/api/basic_json/back/
  2080. const_reference back() const
  2081. {
  2082. auto tmp = cend();
  2083. --tmp;
  2084. return *tmp;
  2085. }
  2086. /// @brief remove element given an iterator
  2087. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  2088. template < class IteratorType, detail::enable_if_t <
  2089. std::is_same<IteratorType, typename basic_json_t::iterator>::value ||
  2090. std::is_same<IteratorType, typename basic_json_t::const_iterator>::value, int > = 0 >
  2091. IteratorType erase(IteratorType pos)
  2092. {
  2093. // make sure iterator fits the current value
  2094. if (JSON_HEDLEY_UNLIKELY(this != pos.m_object))
  2095. {
  2096. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  2097. }
  2098. IteratorType result = end();
  2099. switch (m_data.m_type)
  2100. {
  2101. case value_t::boolean:
  2102. case value_t::number_float:
  2103. case value_t::number_integer:
  2104. case value_t::number_unsigned:
  2105. case value_t::string:
  2106. case value_t::binary:
  2107. {
  2108. if (JSON_HEDLEY_UNLIKELY(!pos.m_it.primitive_iterator.is_begin()))
  2109. {
  2110. JSON_THROW(invalid_iterator::create(205, "iterator out of range", this));
  2111. }
  2112. if (is_string())
  2113. {
  2114. AllocatorType<string_t> alloc;
  2115. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.string);
  2116. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.string, 1);
  2117. m_data.m_value.string = nullptr;
  2118. }
  2119. else if (is_binary())
  2120. {
  2121. AllocatorType<binary_t> alloc;
  2122. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.binary);
  2123. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.binary, 1);
  2124. m_data.m_value.binary = nullptr;
  2125. }
  2126. m_data.m_type = value_t::null;
  2127. assert_invariant();
  2128. break;
  2129. }
  2130. case value_t::object:
  2131. {
  2132. result.m_it.object_iterator = m_data.m_value.object->erase(pos.m_it.object_iterator);
  2133. break;
  2134. }
  2135. case value_t::array:
  2136. {
  2137. result.m_it.array_iterator = m_data.m_value.array->erase(pos.m_it.array_iterator);
  2138. break;
  2139. }
  2140. case value_t::null:
  2141. case value_t::discarded:
  2142. default:
  2143. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  2144. }
  2145. return result;
  2146. }
  2147. /// @brief remove elements given an iterator range
  2148. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  2149. template < class IteratorType, detail::enable_if_t <
  2150. std::is_same<IteratorType, typename basic_json_t::iterator>::value ||
  2151. std::is_same<IteratorType, typename basic_json_t::const_iterator>::value, int > = 0 >
  2152. IteratorType erase(IteratorType first, IteratorType last)
  2153. {
  2154. // make sure iterator fits the current value
  2155. if (JSON_HEDLEY_UNLIKELY(this != first.m_object || this != last.m_object))
  2156. {
  2157. JSON_THROW(invalid_iterator::create(203, "iterators do not fit current value", this));
  2158. }
  2159. IteratorType result = end();
  2160. switch (m_data.m_type)
  2161. {
  2162. case value_t::boolean:
  2163. case value_t::number_float:
  2164. case value_t::number_integer:
  2165. case value_t::number_unsigned:
  2166. case value_t::string:
  2167. case value_t::binary:
  2168. {
  2169. if (JSON_HEDLEY_LIKELY(!first.m_it.primitive_iterator.is_begin()
  2170. || !last.m_it.primitive_iterator.is_end()))
  2171. {
  2172. JSON_THROW(invalid_iterator::create(204, "iterators out of range", this));
  2173. }
  2174. if (is_string())
  2175. {
  2176. AllocatorType<string_t> alloc;
  2177. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.string);
  2178. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.string, 1);
  2179. m_data.m_value.string = nullptr;
  2180. }
  2181. else if (is_binary())
  2182. {
  2183. AllocatorType<binary_t> alloc;
  2184. std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.binary);
  2185. std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.binary, 1);
  2186. m_data.m_value.binary = nullptr;
  2187. }
  2188. m_data.m_type = value_t::null;
  2189. assert_invariant();
  2190. break;
  2191. }
  2192. case value_t::object:
  2193. {
  2194. result.m_it.object_iterator = m_data.m_value.object->erase(first.m_it.object_iterator,
  2195. last.m_it.object_iterator);
  2196. break;
  2197. }
  2198. case value_t::array:
  2199. {
  2200. result.m_it.array_iterator = m_data.m_value.array->erase(first.m_it.array_iterator,
  2201. last.m_it.array_iterator);
  2202. break;
  2203. }
  2204. case value_t::null:
  2205. case value_t::discarded:
  2206. default:
  2207. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  2208. }
  2209. return result;
  2210. }
  2211. private:
  2212. template < typename KeyType, detail::enable_if_t <
  2213. detail::has_erase_with_key_type<basic_json_t, KeyType>::value, int > = 0 >
  2214. size_type erase_internal(KeyType && key)
  2215. {
  2216. // this erase only works for objects
  2217. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  2218. {
  2219. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  2220. }
  2221. return m_data.m_value.object->erase(std::forward<KeyType>(key));
  2222. }
  2223. template < typename KeyType, detail::enable_if_t <
  2224. !detail::has_erase_with_key_type<basic_json_t, KeyType>::value, int > = 0 >
  2225. size_type erase_internal(KeyType && key)
  2226. {
  2227. // this erase only works for objects
  2228. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  2229. {
  2230. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  2231. }
  2232. const auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
  2233. if (it != m_data.m_value.object->end())
  2234. {
  2235. m_data.m_value.object->erase(it);
  2236. return 1;
  2237. }
  2238. return 0;
  2239. }
  2240. public:
  2241. /// @brief remove element from a JSON object given a key
  2242. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  2243. size_type erase(const typename object_t::key_type& key)
  2244. {
  2245. // the indirection via erase_internal() is added to avoid making this
  2246. // function a template and thus de-rank it during overload resolution
  2247. return erase_internal(key);
  2248. }
  2249. /// @brief remove element from a JSON object given a key
  2250. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  2251. template<class KeyType, detail::enable_if_t<
  2252. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  2253. size_type erase(KeyType && key)
  2254. {
  2255. return erase_internal(std::forward<KeyType>(key));
  2256. }
  2257. /// @brief remove element from a JSON array given an index
  2258. /// @sa https://json.nlohmann.me/api/basic_json/erase/
  2259. void erase(const size_type idx)
  2260. {
  2261. // this erase only works for arrays
  2262. if (JSON_HEDLEY_LIKELY(is_array()))
  2263. {
  2264. if (JSON_HEDLEY_UNLIKELY(idx >= size()))
  2265. {
  2266. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
  2267. }
  2268. m_data.m_value.array->erase(m_data.m_value.array->begin() + static_cast<difference_type>(idx));
  2269. }
  2270. else
  2271. {
  2272. JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
  2273. }
  2274. }
  2275. /// @}
  2276. ////////////
  2277. // lookup //
  2278. ////////////
  2279. /// @name lookup
  2280. /// @{
  2281. /// @brief find an element in a JSON object
  2282. /// @sa https://json.nlohmann.me/api/basic_json/find/
  2283. iterator find(const typename object_t::key_type& key)
  2284. {
  2285. auto result = end();
  2286. if (is_object())
  2287. {
  2288. result.m_it.object_iterator = m_data.m_value.object->find(key);
  2289. }
  2290. return result;
  2291. }
  2292. /// @brief find an element in a JSON object
  2293. /// @sa https://json.nlohmann.me/api/basic_json/find/
  2294. const_iterator find(const typename object_t::key_type& key) const
  2295. {
  2296. auto result = cend();
  2297. if (is_object())
  2298. {
  2299. result.m_it.object_iterator = m_data.m_value.object->find(key);
  2300. }
  2301. return result;
  2302. }
  2303. /// @brief find an element in a JSON object
  2304. /// @sa https://json.nlohmann.me/api/basic_json/find/
  2305. template<class KeyType, detail::enable_if_t<
  2306. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  2307. iterator find(KeyType && key)
  2308. {
  2309. auto result = end();
  2310. if (is_object())
  2311. {
  2312. result.m_it.object_iterator = m_data.m_value.object->find(std::forward<KeyType>(key));
  2313. }
  2314. return result;
  2315. }
  2316. /// @brief find an element in a JSON object
  2317. /// @sa https://json.nlohmann.me/api/basic_json/find/
  2318. template<class KeyType, detail::enable_if_t<
  2319. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  2320. const_iterator find(KeyType && key) const
  2321. {
  2322. auto result = cend();
  2323. if (is_object())
  2324. {
  2325. result.m_it.object_iterator = m_data.m_value.object->find(std::forward<KeyType>(key));
  2326. }
  2327. return result;
  2328. }
  2329. /// @brief returns the number of occurrences of a key in a JSON object
  2330. /// @sa https://json.nlohmann.me/api/basic_json/count/
  2331. size_type count(const typename object_t::key_type& key) const
  2332. {
  2333. // return 0 for all nonobject types
  2334. return is_object() ? m_data.m_value.object->count(key) : 0;
  2335. }
  2336. /// @brief returns the number of occurrences of a key in a JSON object
  2337. /// @sa https://json.nlohmann.me/api/basic_json/count/
  2338. template<class KeyType, detail::enable_if_t<
  2339. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  2340. size_type count(KeyType && key) const
  2341. {
  2342. // return 0 for all nonobject types
  2343. return is_object() ? m_data.m_value.object->count(std::forward<KeyType>(key)) : 0;
  2344. }
  2345. /// @brief check the existence of an element in a JSON object
  2346. /// @sa https://json.nlohmann.me/api/basic_json/contains/
  2347. bool contains(const typename object_t::key_type& key) const
  2348. {
  2349. return is_object() && m_data.m_value.object->find(key) != m_data.m_value.object->end();
  2350. }
  2351. /// @brief check the existence of an element in a JSON object
  2352. /// @sa https://json.nlohmann.me/api/basic_json/contains/
  2353. template<class KeyType, detail::enable_if_t<
  2354. detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
  2355. bool contains(KeyType && key) const
  2356. {
  2357. return is_object() && m_data.m_value.object->find(std::forward<KeyType>(key)) != m_data.m_value.object->end();
  2358. }
  2359. /// @brief check the existence of an element in a JSON object given a JSON pointer
  2360. /// @sa https://json.nlohmann.me/api/basic_json/contains/
  2361. bool contains(const json_pointer& ptr) const
  2362. {
  2363. return ptr.contains(this);
  2364. }
  2365. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  2366. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  2367. bool contains(const typename ::nlohmann::json_pointer<BasicJsonType>& ptr) const
  2368. {
  2369. return ptr.contains(this);
  2370. }
  2371. /// @}
  2372. ///////////////
  2373. // iterators //
  2374. ///////////////
  2375. /// @name iterators
  2376. /// @{
  2377. /// @brief returns an iterator to the first element
  2378. /// @sa https://json.nlohmann.me/api/basic_json/begin/
  2379. iterator begin() noexcept
  2380. {
  2381. iterator result(this);
  2382. result.set_begin();
  2383. return result;
  2384. }
  2385. /// @brief returns an iterator to the first element
  2386. /// @sa https://json.nlohmann.me/api/basic_json/begin/
  2387. const_iterator begin() const noexcept
  2388. {
  2389. return cbegin();
  2390. }
  2391. /// @brief returns a const iterator to the first element
  2392. /// @sa https://json.nlohmann.me/api/basic_json/cbegin/
  2393. const_iterator cbegin() const noexcept
  2394. {
  2395. const_iterator result(this);
  2396. result.set_begin();
  2397. return result;
  2398. }
  2399. /// @brief returns an iterator to one past the last element
  2400. /// @sa https://json.nlohmann.me/api/basic_json/end/
  2401. iterator end() noexcept
  2402. {
  2403. iterator result(this);
  2404. result.set_end();
  2405. return result;
  2406. }
  2407. /// @brief returns an iterator to one past the last element
  2408. /// @sa https://json.nlohmann.me/api/basic_json/end/
  2409. const_iterator end() const noexcept
  2410. {
  2411. return cend();
  2412. }
  2413. /// @brief returns an iterator to one past the last element
  2414. /// @sa https://json.nlohmann.me/api/basic_json/cend/
  2415. const_iterator cend() const noexcept
  2416. {
  2417. const_iterator result(this);
  2418. result.set_end();
  2419. return result;
  2420. }
  2421. /// @brief returns an iterator to the reverse-beginning
  2422. /// @sa https://json.nlohmann.me/api/basic_json/rbegin/
  2423. reverse_iterator rbegin() noexcept
  2424. {
  2425. return reverse_iterator(end());
  2426. }
  2427. /// @brief returns an iterator to the reverse-beginning
  2428. /// @sa https://json.nlohmann.me/api/basic_json/rbegin/
  2429. const_reverse_iterator rbegin() const noexcept
  2430. {
  2431. return crbegin();
  2432. }
  2433. /// @brief returns an iterator to the reverse-end
  2434. /// @sa https://json.nlohmann.me/api/basic_json/rend/
  2435. reverse_iterator rend() noexcept
  2436. {
  2437. return reverse_iterator(begin());
  2438. }
  2439. /// @brief returns an iterator to the reverse-end
  2440. /// @sa https://json.nlohmann.me/api/basic_json/rend/
  2441. const_reverse_iterator rend() const noexcept
  2442. {
  2443. return crend();
  2444. }
  2445. /// @brief returns a const reverse iterator to the last element
  2446. /// @sa https://json.nlohmann.me/api/basic_json/crbegin/
  2447. const_reverse_iterator crbegin() const noexcept
  2448. {
  2449. return const_reverse_iterator(cend());
  2450. }
  2451. /// @brief returns a const reverse iterator to one before the first
  2452. /// @sa https://json.nlohmann.me/api/basic_json/crend/
  2453. const_reverse_iterator crend() const noexcept
  2454. {
  2455. return const_reverse_iterator(cbegin());
  2456. }
  2457. public:
  2458. /// @brief wrapper to access iterator member functions in range-based for
  2459. /// @sa https://json.nlohmann.me/api/basic_json/items/
  2460. /// @deprecated This function is deprecated since 3.1.0 and will be removed in
  2461. /// version 4.0.0 of the library. Please use @ref items() instead;
  2462. /// that is, replace `json::iterator_wrapper(j)` with `j.items()`.
  2463. JSON_HEDLEY_DEPRECATED_FOR(3.1.0, items())
  2464. static iteration_proxy<iterator> iterator_wrapper(reference ref) noexcept
  2465. {
  2466. return ref.items();
  2467. }
  2468. /// @brief wrapper to access iterator member functions in range-based for
  2469. /// @sa https://json.nlohmann.me/api/basic_json/items/
  2470. /// @deprecated This function is deprecated since 3.1.0 and will be removed in
  2471. /// version 4.0.0 of the library. Please use @ref items() instead;
  2472. /// that is, replace `json::iterator_wrapper(j)` with `j.items()`.
  2473. JSON_HEDLEY_DEPRECATED_FOR(3.1.0, items())
  2474. static iteration_proxy<const_iterator> iterator_wrapper(const_reference ref) noexcept
  2475. {
  2476. return ref.items();
  2477. }
  2478. /// @brief helper to access iterator member functions in range-based for
  2479. /// @sa https://json.nlohmann.me/api/basic_json/items/
  2480. iteration_proxy<iterator> items() noexcept
  2481. {
  2482. return iteration_proxy<iterator>(*this);
  2483. }
  2484. /// @brief helper to access iterator member functions in range-based for
  2485. /// @sa https://json.nlohmann.me/api/basic_json/items/
  2486. iteration_proxy<const_iterator> items() const noexcept
  2487. {
  2488. return iteration_proxy<const_iterator>(*this);
  2489. }
  2490. /// @}
  2491. //////////////
  2492. // capacity //
  2493. //////////////
  2494. /// @name capacity
  2495. /// @{
  2496. /// @brief checks whether the container is empty.
  2497. /// @sa https://json.nlohmann.me/api/basic_json/empty/
  2498. bool empty() const noexcept
  2499. {
  2500. switch (m_data.m_type)
  2501. {
  2502. case value_t::null:
  2503. {
  2504. // null values are empty
  2505. return true;
  2506. }
  2507. case value_t::array:
  2508. {
  2509. // delegate call to array_t::empty()
  2510. return m_data.m_value.array->empty();
  2511. }
  2512. case value_t::object:
  2513. {
  2514. // delegate call to object_t::empty()
  2515. return m_data.m_value.object->empty();
  2516. }
  2517. case value_t::string:
  2518. case value_t::boolean:
  2519. case value_t::number_integer:
  2520. case value_t::number_unsigned:
  2521. case value_t::number_float:
  2522. case value_t::binary:
  2523. case value_t::discarded:
  2524. default:
  2525. {
  2526. // all other types are nonempty
  2527. return false;
  2528. }
  2529. }
  2530. }
  2531. /// @brief returns the number of elements
  2532. /// @sa https://json.nlohmann.me/api/basic_json/size/
  2533. size_type size() const noexcept
  2534. {
  2535. switch (m_data.m_type)
  2536. {
  2537. case value_t::null:
  2538. {
  2539. // null values are empty
  2540. return 0;
  2541. }
  2542. case value_t::array:
  2543. {
  2544. // delegate call to array_t::size()
  2545. return m_data.m_value.array->size();
  2546. }
  2547. case value_t::object:
  2548. {
  2549. // delegate call to object_t::size()
  2550. return m_data.m_value.object->size();
  2551. }
  2552. case value_t::string:
  2553. case value_t::boolean:
  2554. case value_t::number_integer:
  2555. case value_t::number_unsigned:
  2556. case value_t::number_float:
  2557. case value_t::binary:
  2558. case value_t::discarded:
  2559. default:
  2560. {
  2561. // all other types have size 1
  2562. return 1;
  2563. }
  2564. }
  2565. }
  2566. /// @brief returns the maximum possible number of elements
  2567. /// @sa https://json.nlohmann.me/api/basic_json/max_size/
  2568. size_type max_size() const noexcept
  2569. {
  2570. switch (m_data.m_type)
  2571. {
  2572. case value_t::array:
  2573. {
  2574. // delegate call to array_t::max_size()
  2575. return m_data.m_value.array->max_size();
  2576. }
  2577. case value_t::object:
  2578. {
  2579. // delegate call to object_t::max_size()
  2580. return m_data.m_value.object->max_size();
  2581. }
  2582. case value_t::null:
  2583. case value_t::string:
  2584. case value_t::boolean:
  2585. case value_t::number_integer:
  2586. case value_t::number_unsigned:
  2587. case value_t::number_float:
  2588. case value_t::binary:
  2589. case value_t::discarded:
  2590. default:
  2591. {
  2592. // all other types have max_size() == size()
  2593. return size();
  2594. }
  2595. }
  2596. }
  2597. /// @}
  2598. ///////////////
  2599. // modifiers //
  2600. ///////////////
  2601. /// @name modifiers
  2602. /// @{
  2603. /// @brief clears the contents
  2604. /// @sa https://json.nlohmann.me/api/basic_json/clear/
  2605. void clear() noexcept
  2606. {
  2607. switch (m_data.m_type)
  2608. {
  2609. case value_t::number_integer:
  2610. {
  2611. m_data.m_value.number_integer = 0;
  2612. break;
  2613. }
  2614. case value_t::number_unsigned:
  2615. {
  2616. m_data.m_value.number_unsigned = 0;
  2617. break;
  2618. }
  2619. case value_t::number_float:
  2620. {
  2621. m_data.m_value.number_float = 0.0;
  2622. break;
  2623. }
  2624. case value_t::boolean:
  2625. {
  2626. m_data.m_value.boolean = false;
  2627. break;
  2628. }
  2629. case value_t::string:
  2630. {
  2631. m_data.m_value.string->clear();
  2632. break;
  2633. }
  2634. case value_t::binary:
  2635. {
  2636. m_data.m_value.binary->clear();
  2637. break;
  2638. }
  2639. case value_t::array:
  2640. {
  2641. m_data.m_value.array->clear();
  2642. break;
  2643. }
  2644. case value_t::object:
  2645. {
  2646. m_data.m_value.object->clear();
  2647. break;
  2648. }
  2649. case value_t::null:
  2650. case value_t::discarded:
  2651. default:
  2652. break;
  2653. }
  2654. }
  2655. /// @brief add an object to an array
  2656. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  2657. void push_back(basic_json&& val)
  2658. {
  2659. // push_back only works for null objects or arrays
  2660. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
  2661. {
  2662. JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
  2663. }
  2664. // transform null object into an array
  2665. if (is_null())
  2666. {
  2667. m_data.m_type = value_t::array;
  2668. m_data.m_value = value_t::array;
  2669. assert_invariant();
  2670. }
  2671. // add element to array (move semantics)
  2672. const auto old_capacity = m_data.m_value.array->capacity();
  2673. m_data.m_value.array->push_back(std::move(val));
  2674. set_parent(m_data.m_value.array->back(), old_capacity);
  2675. // if val is moved from, basic_json move constructor marks it null, so we do not call the destructor
  2676. }
  2677. /// @brief add an object to an array
  2678. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  2679. reference operator+=(basic_json&& val)
  2680. {
  2681. push_back(std::move(val));
  2682. return *this;
  2683. }
  2684. /// @brief add an object to an array
  2685. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  2686. void push_back(const basic_json& val)
  2687. {
  2688. // push_back only works for null objects or arrays
  2689. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
  2690. {
  2691. JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
  2692. }
  2693. // transform null object into an array
  2694. if (is_null())
  2695. {
  2696. m_data.m_type = value_t::array;
  2697. m_data.m_value = value_t::array;
  2698. assert_invariant();
  2699. }
  2700. // add element to array
  2701. const auto old_capacity = m_data.m_value.array->capacity();
  2702. m_data.m_value.array->push_back(val);
  2703. set_parent(m_data.m_value.array->back(), old_capacity);
  2704. }
  2705. /// @brief add an object to an array
  2706. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  2707. reference operator+=(const basic_json& val)
  2708. {
  2709. push_back(val);
  2710. return *this;
  2711. }
  2712. /// @brief add an object to an object
  2713. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  2714. void push_back(const typename object_t::value_type& val)
  2715. {
  2716. // push_back only works for null objects or objects
  2717. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object())))
  2718. {
  2719. JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
  2720. }
  2721. // transform null object into an object
  2722. if (is_null())
  2723. {
  2724. m_data.m_type = value_t::object;
  2725. m_data.m_value = value_t::object;
  2726. assert_invariant();
  2727. }
  2728. // add element to object
  2729. auto res = m_data.m_value.object->insert(val);
  2730. set_parent(res.first->second);
  2731. }
  2732. /// @brief add an object to an object
  2733. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  2734. reference operator+=(const typename object_t::value_type& val)
  2735. {
  2736. push_back(val);
  2737. return *this;
  2738. }
  2739. /// @brief add an object to an object
  2740. /// @sa https://json.nlohmann.me/api/basic_json/push_back/
  2741. void push_back(initializer_list_t init)
  2742. {
  2743. if (is_object() && init.size() == 2 && (*init.begin())->is_string())
  2744. {
  2745. basic_json&& key = init.begin()->moved_or_copied();
  2746. push_back(typename object_t::value_type(
  2747. std::move(key.get_ref<string_t&>()), (init.begin() + 1)->moved_or_copied()));
  2748. }
  2749. else
  2750. {
  2751. push_back(basic_json(init));
  2752. }
  2753. }
  2754. /// @brief add an object to an object
  2755. /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
  2756. reference operator+=(initializer_list_t init)
  2757. {
  2758. push_back(init);
  2759. return *this;
  2760. }
  2761. /// @brief add an object to an array
  2762. /// @sa https://json.nlohmann.me/api/basic_json/emplace_back/
  2763. template<class... Args>
  2764. reference emplace_back(Args&& ... args)
  2765. {
  2766. // emplace_back only works for null objects or arrays
  2767. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
  2768. {
  2769. JSON_THROW(type_error::create(311, detail::concat("cannot use emplace_back() with ", type_name()), this));
  2770. }
  2771. // transform null object into an array
  2772. if (is_null())
  2773. {
  2774. m_data.m_type = value_t::array;
  2775. m_data.m_value = value_t::array;
  2776. assert_invariant();
  2777. }
  2778. // add element to array (perfect forwarding)
  2779. const auto old_capacity = m_data.m_value.array->capacity();
  2780. m_data.m_value.array->emplace_back(std::forward<Args>(args)...);
  2781. return set_parent(m_data.m_value.array->back(), old_capacity);
  2782. }
  2783. /// @brief add an object to an object if key does not exist
  2784. /// @sa https://json.nlohmann.me/api/basic_json/emplace/
  2785. template<class... Args>
  2786. std::pair<iterator, bool> emplace(Args&& ... args)
  2787. {
  2788. // emplace only works for null objects or arrays
  2789. if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object())))
  2790. {
  2791. JSON_THROW(type_error::create(311, detail::concat("cannot use emplace() with ", type_name()), this));
  2792. }
  2793. // transform null object into an object
  2794. if (is_null())
  2795. {
  2796. m_data.m_type = value_t::object;
  2797. m_data.m_value = value_t::object;
  2798. assert_invariant();
  2799. }
  2800. // add element to array (perfect forwarding)
  2801. auto res = m_data.m_value.object->emplace(std::forward<Args>(args)...);
  2802. set_parent(res.first->second);
  2803. // create result iterator and set iterator to the result of emplace
  2804. auto it = begin();
  2805. it.m_it.object_iterator = res.first;
  2806. // return pair of iterator and boolean
  2807. return {it, res.second};
  2808. }
  2809. /// Helper for insertion of an iterator
  2810. /// @note: This uses std::distance to support GCC 4.8,
  2811. /// see https://github.com/nlohmann/json/pull/1257
  2812. template<typename... Args>
  2813. iterator insert_iterator(const_iterator pos, Args&& ... args)
  2814. {
  2815. iterator result(this);
  2816. JSON_ASSERT(m_data.m_value.array != nullptr);
  2817. auto insert_pos = std::distance(m_data.m_value.array->begin(), pos.m_it.array_iterator);
  2818. m_data.m_value.array->insert(pos.m_it.array_iterator, std::forward<Args>(args)...);
  2819. result.m_it.array_iterator = m_data.m_value.array->begin() + insert_pos;
  2820. // This could have been written as:
  2821. // result.m_it.array_iterator = m_data.m_value.array->insert(pos.m_it.array_iterator, cnt, val);
  2822. // but the return value of insert is missing in GCC 4.8, so it is written this way instead.
  2823. set_parents();
  2824. return result;
  2825. }
  2826. /// @brief inserts element into array
  2827. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  2828. iterator insert(const_iterator pos, const basic_json& val)
  2829. {
  2830. // insert only works for arrays
  2831. if (JSON_HEDLEY_LIKELY(is_array()))
  2832. {
  2833. // check if iterator pos fits to this JSON value
  2834. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  2835. {
  2836. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  2837. }
  2838. // insert to array and return iterator
  2839. return insert_iterator(pos, val);
  2840. }
  2841. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  2842. }
  2843. /// @brief inserts element into array
  2844. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  2845. iterator insert(const_iterator pos, basic_json&& val)
  2846. {
  2847. return insert(pos, val);
  2848. }
  2849. /// @brief inserts copies of element into array
  2850. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  2851. iterator insert(const_iterator pos, size_type cnt, const basic_json& val)
  2852. {
  2853. // insert only works for arrays
  2854. if (JSON_HEDLEY_LIKELY(is_array()))
  2855. {
  2856. // check if iterator pos fits to this JSON value
  2857. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  2858. {
  2859. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  2860. }
  2861. // insert to array and return iterator
  2862. return insert_iterator(pos, cnt, val);
  2863. }
  2864. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  2865. }
  2866. /// @brief inserts range of elements into array
  2867. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  2868. iterator insert(const_iterator pos, const_iterator first, const_iterator last)
  2869. {
  2870. // insert only works for arrays
  2871. if (JSON_HEDLEY_UNLIKELY(!is_array()))
  2872. {
  2873. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  2874. }
  2875. // check if iterator pos fits to this JSON value
  2876. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  2877. {
  2878. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  2879. }
  2880. // check if range iterators belong to the same JSON object
  2881. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  2882. {
  2883. JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
  2884. }
  2885. if (JSON_HEDLEY_UNLIKELY(first.m_object == this))
  2886. {
  2887. JSON_THROW(invalid_iterator::create(211, "passed iterators may not belong to container", this));
  2888. }
  2889. // insert to array and return iterator
  2890. return insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator);
  2891. }
  2892. /// @brief inserts elements from initializer list into array
  2893. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  2894. iterator insert(const_iterator pos, initializer_list_t ilist)
  2895. {
  2896. // insert only works for arrays
  2897. if (JSON_HEDLEY_UNLIKELY(!is_array()))
  2898. {
  2899. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  2900. }
  2901. // check if iterator pos fits to this JSON value
  2902. if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
  2903. {
  2904. JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
  2905. }
  2906. // insert to array and return iterator
  2907. return insert_iterator(pos, ilist.begin(), ilist.end());
  2908. }
  2909. /// @brief inserts range of elements into object
  2910. /// @sa https://json.nlohmann.me/api/basic_json/insert/
  2911. void insert(const_iterator first, const_iterator last)
  2912. {
  2913. // insert only works for objects
  2914. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  2915. {
  2916. JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
  2917. }
  2918. // check if range iterators belong to the same JSON object
  2919. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  2920. {
  2921. JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
  2922. }
  2923. // passed iterators must belong to objects
  2924. if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()))
  2925. {
  2926. JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", this));
  2927. }
  2928. m_data.m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator);
  2929. }
  2930. /// @brief updates a JSON object from another object, overwriting existing keys
  2931. /// @sa https://json.nlohmann.me/api/basic_json/update/
  2932. void update(const_reference j, bool merge_objects = false)
  2933. {
  2934. update(j.begin(), j.end(), merge_objects);
  2935. }
  2936. /// @brief updates a JSON object from another object, overwriting existing keys
  2937. /// @sa https://json.nlohmann.me/api/basic_json/update/
  2938. void update(const_iterator first, const_iterator last, bool merge_objects = false)
  2939. {
  2940. // implicitly convert null value to an empty object
  2941. if (is_null())
  2942. {
  2943. m_data.m_type = value_t::object;
  2944. m_data.m_value.object = create<object_t>();
  2945. assert_invariant();
  2946. }
  2947. if (JSON_HEDLEY_UNLIKELY(!is_object()))
  2948. {
  2949. JSON_THROW(type_error::create(312, detail::concat("cannot use update() with ", type_name()), this));
  2950. }
  2951. // check if range iterators belong to the same JSON object
  2952. if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
  2953. {
  2954. JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
  2955. }
  2956. // passed iterators must belong to objects
  2957. if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()))
  2958. {
  2959. JSON_THROW(type_error::create(312, detail::concat("cannot use update() with ", first.m_object->type_name()), first.m_object));
  2960. }
  2961. for (auto it = first; it != last; ++it)
  2962. {
  2963. if (merge_objects && it.value().is_object())
  2964. {
  2965. auto it2 = m_data.m_value.object->find(it.key());
  2966. if (it2 != m_data.m_value.object->end())
  2967. {
  2968. it2->second.update(it.value(), true);
  2969. continue;
  2970. }
  2971. }
  2972. m_data.m_value.object->operator[](it.key()) = it.value();
  2973. #if JSON_DIAGNOSTICS
  2974. m_data.m_value.object->operator[](it.key()).m_parent = this;
  2975. #endif
  2976. }
  2977. }
  2978. /// @brief exchanges the values
  2979. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  2980. void swap(reference other) noexcept (
  2981. std::is_nothrow_move_constructible<value_t>::value&&
  2982. std::is_nothrow_move_assignable<value_t>::value&&
  2983. std::is_nothrow_move_constructible<json_value>::value&& // NOLINT(cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  2984. std::is_nothrow_move_assignable<json_value>::value
  2985. )
  2986. {
  2987. std::swap(m_data.m_type, other.m_data.m_type);
  2988. std::swap(m_data.m_value, other.m_data.m_value);
  2989. set_parents();
  2990. other.set_parents();
  2991. assert_invariant();
  2992. }
  2993. /// @brief exchanges the values
  2994. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  2995. friend void swap(reference left, reference right) noexcept (
  2996. std::is_nothrow_move_constructible<value_t>::value&&
  2997. std::is_nothrow_move_assignable<value_t>::value&&
  2998. std::is_nothrow_move_constructible<json_value>::value&& // NOLINT(cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  2999. std::is_nothrow_move_assignable<json_value>::value
  3000. )
  3001. {
  3002. left.swap(right);
  3003. }
  3004. /// @brief exchanges the values
  3005. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  3006. void swap(array_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  3007. {
  3008. // swap only works for arrays
  3009. if (JSON_HEDLEY_LIKELY(is_array()))
  3010. {
  3011. using std::swap;
  3012. swap(*(m_data.m_value.array), other);
  3013. }
  3014. else
  3015. {
  3016. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(array_t&) with ", type_name()), this));
  3017. }
  3018. }
  3019. /// @brief exchanges the values
  3020. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  3021. void swap(object_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  3022. {
  3023. // swap only works for objects
  3024. if (JSON_HEDLEY_LIKELY(is_object()))
  3025. {
  3026. using std::swap;
  3027. swap(*(m_data.m_value.object), other);
  3028. }
  3029. else
  3030. {
  3031. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(object_t&) with ", type_name()), this));
  3032. }
  3033. }
  3034. /// @brief exchanges the values
  3035. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  3036. void swap(string_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  3037. {
  3038. // swap only works for strings
  3039. if (JSON_HEDLEY_LIKELY(is_string()))
  3040. {
  3041. using std::swap;
  3042. swap(*(m_data.m_value.string), other);
  3043. }
  3044. else
  3045. {
  3046. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(string_t&) with ", type_name()), this));
  3047. }
  3048. }
  3049. /// @brief exchanges the values
  3050. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  3051. void swap(binary_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  3052. {
  3053. // swap only works for strings
  3054. if (JSON_HEDLEY_LIKELY(is_binary()))
  3055. {
  3056. using std::swap;
  3057. swap(*(m_data.m_value.binary), other);
  3058. }
  3059. else
  3060. {
  3061. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(binary_t&) with ", type_name()), this));
  3062. }
  3063. }
  3064. /// @brief exchanges the values
  3065. /// @sa https://json.nlohmann.me/api/basic_json/swap/
  3066. void swap(typename binary_t::container_type& other) // NOLINT(bugprone-exception-escape)
  3067. {
  3068. // swap only works for strings
  3069. if (JSON_HEDLEY_LIKELY(is_binary()))
  3070. {
  3071. using std::swap;
  3072. swap(*(m_data.m_value.binary), other);
  3073. }
  3074. else
  3075. {
  3076. JSON_THROW(type_error::create(310, detail::concat("cannot use swap(binary_t::container_type&) with ", type_name()), this));
  3077. }
  3078. }
  3079. /// @}
  3080. //////////////////////////////////////////
  3081. // lexicographical comparison operators //
  3082. //////////////////////////////////////////
  3083. /// @name lexicographical comparison operators
  3084. /// @{
  3085. // note parentheses around operands are necessary; see
  3086. // https://github.com/nlohmann/json/issues/1530
  3087. #define JSON_IMPLEMENT_OPERATOR(op, null_result, unordered_result, default_result) \
  3088. const auto lhs_type = lhs.type(); \
  3089. const auto rhs_type = rhs.type(); \
  3090. \
  3091. if (lhs_type == rhs_type) /* NOLINT(readability/braces) */ \
  3092. { \
  3093. switch (lhs_type) \
  3094. { \
  3095. case value_t::array: \
  3096. return (*lhs.m_data.m_value.array) op (*rhs.m_data.m_value.array); \
  3097. \
  3098. case value_t::object: \
  3099. return (*lhs.m_data.m_value.object) op (*rhs.m_data.m_value.object); \
  3100. \
  3101. case value_t::null: \
  3102. return (null_result); \
  3103. \
  3104. case value_t::string: \
  3105. return (*lhs.m_data.m_value.string) op (*rhs.m_data.m_value.string); \
  3106. \
  3107. case value_t::boolean: \
  3108. return (lhs.m_data.m_value.boolean) op (rhs.m_data.m_value.boolean); \
  3109. \
  3110. case value_t::number_integer: \
  3111. return (lhs.m_data.m_value.number_integer) op (rhs.m_data.m_value.number_integer); \
  3112. \
  3113. case value_t::number_unsigned: \
  3114. return (lhs.m_data.m_value.number_unsigned) op (rhs.m_data.m_value.number_unsigned); \
  3115. \
  3116. case value_t::number_float: \
  3117. return (lhs.m_data.m_value.number_float) op (rhs.m_data.m_value.number_float); \
  3118. \
  3119. case value_t::binary: \
  3120. return (*lhs.m_data.m_value.binary) op (*rhs.m_data.m_value.binary); \
  3121. \
  3122. case value_t::discarded: \
  3123. default: \
  3124. return (unordered_result); \
  3125. } \
  3126. } \
  3127. else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_float) \
  3128. { \
  3129. return static_cast<number_float_t>(lhs.m_data.m_value.number_integer) op rhs.m_data.m_value.number_float; \
  3130. } \
  3131. else if (lhs_type == value_t::number_float && rhs_type == value_t::number_integer) \
  3132. { \
  3133. return lhs.m_data.m_value.number_float op static_cast<number_float_t>(rhs.m_data.m_value.number_integer); \
  3134. } \
  3135. else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_float) \
  3136. { \
  3137. return static_cast<number_float_t>(lhs.m_data.m_value.number_unsigned) op rhs.m_data.m_value.number_float; \
  3138. } \
  3139. else if (lhs_type == value_t::number_float && rhs_type == value_t::number_unsigned) \
  3140. { \
  3141. return lhs.m_data.m_value.number_float op static_cast<number_float_t>(rhs.m_data.m_value.number_unsigned); \
  3142. } \
  3143. else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_integer) \
  3144. { \
  3145. return static_cast<number_integer_t>(lhs.m_data.m_value.number_unsigned) op rhs.m_data.m_value.number_integer; \
  3146. } \
  3147. else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_unsigned) \
  3148. { \
  3149. return lhs.m_data.m_value.number_integer op static_cast<number_integer_t>(rhs.m_data.m_value.number_unsigned); \
  3150. } \
  3151. else if(compares_unordered(lhs, rhs))\
  3152. {\
  3153. return (unordered_result);\
  3154. }\
  3155. \
  3156. return (default_result);
  3157. JSON_PRIVATE_UNLESS_TESTED:
  3158. // returns true if:
  3159. // - any operand is NaN and the other operand is of number type
  3160. // - any operand is discarded
  3161. // in legacy mode, discarded values are considered ordered if
  3162. // an operation is computed as an odd number of inverses of others
  3163. static bool compares_unordered(const_reference lhs, const_reference rhs, bool inverse = false) noexcept
  3164. {
  3165. if ((lhs.is_number_float() && std::isnan(lhs.m_data.m_value.number_float) && rhs.is_number())
  3166. || (rhs.is_number_float() && std::isnan(rhs.m_data.m_value.number_float) && lhs.is_number()))
  3167. {
  3168. return true;
  3169. }
  3170. #if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  3171. return (lhs.is_discarded() || rhs.is_discarded()) && !inverse;
  3172. #else
  3173. static_cast<void>(inverse);
  3174. return lhs.is_discarded() || rhs.is_discarded();
  3175. #endif
  3176. }
  3177. private:
  3178. bool compares_unordered(const_reference rhs, bool inverse = false) const noexcept
  3179. {
  3180. return compares_unordered(*this, rhs, inverse);
  3181. }
  3182. public:
  3183. #if JSON_HAS_THREE_WAY_COMPARISON
  3184. /// @brief comparison: equal
  3185. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  3186. bool operator==(const_reference rhs) const noexcept
  3187. {
  3188. #ifdef __GNUC__
  3189. #pragma GCC diagnostic push
  3190. #pragma GCC diagnostic ignored "-Wfloat-equal"
  3191. #endif
  3192. const_reference lhs = *this;
  3193. JSON_IMPLEMENT_OPERATOR( ==, true, false, false)
  3194. #ifdef __GNUC__
  3195. #pragma GCC diagnostic pop
  3196. #endif
  3197. }
  3198. /// @brief comparison: equal
  3199. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  3200. template<typename ScalarType>
  3201. requires std::is_scalar_v<ScalarType>
  3202. bool operator==(ScalarType rhs) const noexcept
  3203. {
  3204. return *this == basic_json(rhs);
  3205. }
  3206. /// @brief comparison: not equal
  3207. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  3208. bool operator!=(const_reference rhs) const noexcept
  3209. {
  3210. if (compares_unordered(rhs, true))
  3211. {
  3212. return false;
  3213. }
  3214. return !operator==(rhs);
  3215. }
  3216. /// @brief comparison: 3-way
  3217. /// @sa https://json.nlohmann.me/api/basic_json/operator_spaceship/
  3218. std::partial_ordering operator<=>(const_reference rhs) const noexcept // *NOPAD*
  3219. {
  3220. const_reference lhs = *this;
  3221. // default_result is used if we cannot compare values. In that case,
  3222. // we compare types.
  3223. JSON_IMPLEMENT_OPERATOR(<=>, // *NOPAD*
  3224. std::partial_ordering::equivalent,
  3225. std::partial_ordering::unordered,
  3226. lhs_type <=> rhs_type) // *NOPAD*
  3227. }
  3228. /// @brief comparison: 3-way
  3229. /// @sa https://json.nlohmann.me/api/basic_json/operator_spaceship/
  3230. template<typename ScalarType>
  3231. requires std::is_scalar_v<ScalarType>
  3232. std::partial_ordering operator<=>(ScalarType rhs) const noexcept // *NOPAD*
  3233. {
  3234. return *this <=> basic_json(rhs); // *NOPAD*
  3235. }
  3236. #if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
  3237. // all operators that are computed as an odd number of inverses of others
  3238. // need to be overloaded to emulate the legacy comparison behavior
  3239. /// @brief comparison: less than or equal
  3240. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  3241. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON)
  3242. bool operator<=(const_reference rhs) const noexcept
  3243. {
  3244. if (compares_unordered(rhs, true))
  3245. {
  3246. return false;
  3247. }
  3248. return !(rhs < *this);
  3249. }
  3250. /// @brief comparison: less than or equal
  3251. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  3252. template<typename ScalarType>
  3253. requires std::is_scalar_v<ScalarType>
  3254. bool operator<=(ScalarType rhs) const noexcept
  3255. {
  3256. return *this <= basic_json(rhs);
  3257. }
  3258. /// @brief comparison: greater than or equal
  3259. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  3260. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON)
  3261. bool operator>=(const_reference rhs) const noexcept
  3262. {
  3263. if (compares_unordered(rhs, true))
  3264. {
  3265. return false;
  3266. }
  3267. return !(*this < rhs);
  3268. }
  3269. /// @brief comparison: greater than or equal
  3270. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  3271. template<typename ScalarType>
  3272. requires std::is_scalar_v<ScalarType>
  3273. bool operator>=(ScalarType rhs) const noexcept
  3274. {
  3275. return *this >= basic_json(rhs);
  3276. }
  3277. #endif
  3278. #else
  3279. /// @brief comparison: equal
  3280. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  3281. friend bool operator==(const_reference lhs, const_reference rhs) noexcept
  3282. {
  3283. #ifdef __GNUC__
  3284. #pragma GCC diagnostic push
  3285. #pragma GCC diagnostic ignored "-Wfloat-equal"
  3286. #endif
  3287. JSON_IMPLEMENT_OPERATOR( ==, true, false, false)
  3288. #ifdef __GNUC__
  3289. #pragma GCC diagnostic pop
  3290. #endif
  3291. }
  3292. /// @brief comparison: equal
  3293. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  3294. template<typename ScalarType, typename std::enable_if<
  3295. std::is_scalar<ScalarType>::value, int>::type = 0>
  3296. friend bool operator==(const_reference lhs, ScalarType rhs) noexcept
  3297. {
  3298. return lhs == basic_json(rhs);
  3299. }
  3300. /// @brief comparison: equal
  3301. /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
  3302. template<typename ScalarType, typename std::enable_if<
  3303. std::is_scalar<ScalarType>::value, int>::type = 0>
  3304. friend bool operator==(ScalarType lhs, const_reference rhs) noexcept
  3305. {
  3306. return basic_json(lhs) == rhs;
  3307. }
  3308. /// @brief comparison: not equal
  3309. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  3310. friend bool operator!=(const_reference lhs, const_reference rhs) noexcept
  3311. {
  3312. if (compares_unordered(lhs, rhs, true))
  3313. {
  3314. return false;
  3315. }
  3316. return !(lhs == rhs);
  3317. }
  3318. /// @brief comparison: not equal
  3319. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  3320. template<typename ScalarType, typename std::enable_if<
  3321. std::is_scalar<ScalarType>::value, int>::type = 0>
  3322. friend bool operator!=(const_reference lhs, ScalarType rhs) noexcept
  3323. {
  3324. return lhs != basic_json(rhs);
  3325. }
  3326. /// @brief comparison: not equal
  3327. /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
  3328. template<typename ScalarType, typename std::enable_if<
  3329. std::is_scalar<ScalarType>::value, int>::type = 0>
  3330. friend bool operator!=(ScalarType lhs, const_reference rhs) noexcept
  3331. {
  3332. return basic_json(lhs) != rhs;
  3333. }
  3334. /// @brief comparison: less than
  3335. /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
  3336. friend bool operator<(const_reference lhs, const_reference rhs) noexcept
  3337. {
  3338. // default_result is used if we cannot compare values. In that case,
  3339. // we compare types. Note we have to call the operator explicitly,
  3340. // because MSVC has problems otherwise.
  3341. JSON_IMPLEMENT_OPERATOR( <, false, false, operator<(lhs_type, rhs_type))
  3342. }
  3343. /// @brief comparison: less than
  3344. /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
  3345. template<typename ScalarType, typename std::enable_if<
  3346. std::is_scalar<ScalarType>::value, int>::type = 0>
  3347. friend bool operator<(const_reference lhs, ScalarType rhs) noexcept
  3348. {
  3349. return lhs < basic_json(rhs);
  3350. }
  3351. /// @brief comparison: less than
  3352. /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
  3353. template<typename ScalarType, typename std::enable_if<
  3354. std::is_scalar<ScalarType>::value, int>::type = 0>
  3355. friend bool operator<(ScalarType lhs, const_reference rhs) noexcept
  3356. {
  3357. return basic_json(lhs) < rhs;
  3358. }
  3359. /// @brief comparison: less than or equal
  3360. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  3361. friend bool operator<=(const_reference lhs, const_reference rhs) noexcept
  3362. {
  3363. if (compares_unordered(lhs, rhs, true))
  3364. {
  3365. return false;
  3366. }
  3367. return !(rhs < lhs);
  3368. }
  3369. /// @brief comparison: less than or equal
  3370. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  3371. template<typename ScalarType, typename std::enable_if<
  3372. std::is_scalar<ScalarType>::value, int>::type = 0>
  3373. friend bool operator<=(const_reference lhs, ScalarType rhs) noexcept
  3374. {
  3375. return lhs <= basic_json(rhs);
  3376. }
  3377. /// @brief comparison: less than or equal
  3378. /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
  3379. template<typename ScalarType, typename std::enable_if<
  3380. std::is_scalar<ScalarType>::value, int>::type = 0>
  3381. friend bool operator<=(ScalarType lhs, const_reference rhs) noexcept
  3382. {
  3383. return basic_json(lhs) <= rhs;
  3384. }
  3385. /// @brief comparison: greater than
  3386. /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
  3387. friend bool operator>(const_reference lhs, const_reference rhs) noexcept
  3388. {
  3389. // double inverse
  3390. if (compares_unordered(lhs, rhs))
  3391. {
  3392. return false;
  3393. }
  3394. return !(lhs <= rhs);
  3395. }
  3396. /// @brief comparison: greater than
  3397. /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
  3398. template<typename ScalarType, typename std::enable_if<
  3399. std::is_scalar<ScalarType>::value, int>::type = 0>
  3400. friend bool operator>(const_reference lhs, ScalarType rhs) noexcept
  3401. {
  3402. return lhs > basic_json(rhs);
  3403. }
  3404. /// @brief comparison: greater than
  3405. /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
  3406. template<typename ScalarType, typename std::enable_if<
  3407. std::is_scalar<ScalarType>::value, int>::type = 0>
  3408. friend bool operator>(ScalarType lhs, const_reference rhs) noexcept
  3409. {
  3410. return basic_json(lhs) > rhs;
  3411. }
  3412. /// @brief comparison: greater than or equal
  3413. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  3414. friend bool operator>=(const_reference lhs, const_reference rhs) noexcept
  3415. {
  3416. if (compares_unordered(lhs, rhs, true))
  3417. {
  3418. return false;
  3419. }
  3420. return !(lhs < rhs);
  3421. }
  3422. /// @brief comparison: greater than or equal
  3423. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  3424. template<typename ScalarType, typename std::enable_if<
  3425. std::is_scalar<ScalarType>::value, int>::type = 0>
  3426. friend bool operator>=(const_reference lhs, ScalarType rhs) noexcept
  3427. {
  3428. return lhs >= basic_json(rhs);
  3429. }
  3430. /// @brief comparison: greater than or equal
  3431. /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
  3432. template<typename ScalarType, typename std::enable_if<
  3433. std::is_scalar<ScalarType>::value, int>::type = 0>
  3434. friend bool operator>=(ScalarType lhs, const_reference rhs) noexcept
  3435. {
  3436. return basic_json(lhs) >= rhs;
  3437. }
  3438. #endif
  3439. #undef JSON_IMPLEMENT_OPERATOR
  3440. /// @}
  3441. ///////////////////
  3442. // serialization //
  3443. ///////////////////
  3444. /// @name serialization
  3445. /// @{
  3446. #ifndef JSON_NO_IO
  3447. /// @brief serialize to stream
  3448. /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
  3449. friend std::ostream& operator<<(std::ostream& o, const basic_json& j)
  3450. {
  3451. // read width member and use it as indentation parameter if nonzero
  3452. const bool pretty_print = o.width() > 0;
  3453. const auto indentation = pretty_print ? o.width() : 0;
  3454. // reset width to 0 for subsequent calls to this stream
  3455. o.width(0);
  3456. // do the actual serialization
  3457. serializer s(detail::output_adapter<char>(o), o.fill());
  3458. s.dump(j, pretty_print, false, static_cast<unsigned int>(indentation));
  3459. return o;
  3460. }
  3461. /// @brief serialize to stream
  3462. /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
  3463. /// @deprecated This function is deprecated since 3.0.0 and will be removed in
  3464. /// version 4.0.0 of the library. Please use
  3465. /// operator<<(std::ostream&, const basic_json&) instead; that is,
  3466. /// replace calls like `j >> o;` with `o << j;`.
  3467. JSON_HEDLEY_DEPRECATED_FOR(3.0.0, operator<<(std::ostream&, const basic_json&))
  3468. friend std::ostream& operator>>(const basic_json& j, std::ostream& o)
  3469. {
  3470. return o << j;
  3471. }
  3472. #endif // JSON_NO_IO
  3473. /// @}
  3474. /////////////////////
  3475. // deserialization //
  3476. /////////////////////
  3477. /// @name deserialization
  3478. /// @{
  3479. /// @brief deserialize from a compatible input
  3480. /// @sa https://json.nlohmann.me/api/basic_json/parse/
  3481. template<typename InputType>
  3482. JSON_HEDLEY_WARN_UNUSED_RESULT
  3483. static basic_json parse(InputType&& i,
  3484. const parser_callback_t cb = nullptr,
  3485. const bool allow_exceptions = true,
  3486. const bool ignore_comments = false)
  3487. {
  3488. basic_json result;
  3489. parser(detail::input_adapter(std::forward<InputType>(i)), cb, allow_exceptions, ignore_comments).parse(true, result);
  3490. return result;
  3491. }
  3492. /// @brief deserialize from a pair of character iterators
  3493. /// @sa https://json.nlohmann.me/api/basic_json/parse/
  3494. template<typename IteratorType>
  3495. JSON_HEDLEY_WARN_UNUSED_RESULT
  3496. static basic_json parse(IteratorType first,
  3497. IteratorType last,
  3498. const parser_callback_t cb = nullptr,
  3499. const bool allow_exceptions = true,
  3500. const bool ignore_comments = false)
  3501. {
  3502. basic_json result;
  3503. parser(detail::input_adapter(std::move(first), std::move(last)), cb, allow_exceptions, ignore_comments).parse(true, result);
  3504. return result;
  3505. }
  3506. JSON_HEDLEY_WARN_UNUSED_RESULT
  3507. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, parse(ptr, ptr + len))
  3508. static basic_json parse(detail::span_input_adapter&& i,
  3509. const parser_callback_t cb = nullptr,
  3510. const bool allow_exceptions = true,
  3511. const bool ignore_comments = false)
  3512. {
  3513. basic_json result;
  3514. parser(i.get(), cb, allow_exceptions, ignore_comments).parse(true, result);
  3515. return result;
  3516. }
  3517. /// @brief check if the input is valid JSON
  3518. /// @sa https://json.nlohmann.me/api/basic_json/accept/
  3519. template<typename InputType>
  3520. static bool accept(InputType&& i,
  3521. const bool ignore_comments = false)
  3522. {
  3523. return parser(detail::input_adapter(std::forward<InputType>(i)), nullptr, false, ignore_comments).accept(true);
  3524. }
  3525. /// @brief check if the input is valid JSON
  3526. /// @sa https://json.nlohmann.me/api/basic_json/accept/
  3527. template<typename IteratorType>
  3528. static bool accept(IteratorType first, IteratorType last,
  3529. const bool ignore_comments = false)
  3530. {
  3531. return parser(detail::input_adapter(std::move(first), std::move(last)), nullptr, false, ignore_comments).accept(true);
  3532. }
  3533. JSON_HEDLEY_WARN_UNUSED_RESULT
  3534. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, accept(ptr, ptr + len))
  3535. static bool accept(detail::span_input_adapter&& i,
  3536. const bool ignore_comments = false)
  3537. {
  3538. return parser(i.get(), nullptr, false, ignore_comments).accept(true);
  3539. }
  3540. /// @brief generate SAX events
  3541. /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
  3542. template <typename InputType, typename SAX>
  3543. JSON_HEDLEY_NON_NULL(2)
  3544. static bool sax_parse(InputType&& i, SAX* sax,
  3545. input_format_t format = input_format_t::json,
  3546. const bool strict = true,
  3547. const bool ignore_comments = false)
  3548. {
  3549. auto ia = detail::input_adapter(std::forward<InputType>(i));
  3550. return format == input_format_t::json
  3551. ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
  3552. : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
  3553. }
  3554. /// @brief generate SAX events
  3555. /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
  3556. template<class IteratorType, class SAX>
  3557. JSON_HEDLEY_NON_NULL(3)
  3558. static bool sax_parse(IteratorType first, IteratorType last, SAX* sax,
  3559. input_format_t format = input_format_t::json,
  3560. const bool strict = true,
  3561. const bool ignore_comments = false)
  3562. {
  3563. auto ia = detail::input_adapter(std::move(first), std::move(last));
  3564. return format == input_format_t::json
  3565. ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
  3566. : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
  3567. }
  3568. /// @brief generate SAX events
  3569. /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
  3570. /// @deprecated This function is deprecated since 3.8.0 and will be removed in
  3571. /// version 4.0.0 of the library. Please use
  3572. /// sax_parse(ptr, ptr + len) instead.
  3573. template <typename SAX>
  3574. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, sax_parse(ptr, ptr + len, ...))
  3575. JSON_HEDLEY_NON_NULL(2)
  3576. static bool sax_parse(detail::span_input_adapter&& i, SAX* sax,
  3577. input_format_t format = input_format_t::json,
  3578. const bool strict = true,
  3579. const bool ignore_comments = false)
  3580. {
  3581. auto ia = i.get();
  3582. return format == input_format_t::json
  3583. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  3584. ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
  3585. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  3586. : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
  3587. }
  3588. #ifndef JSON_NO_IO
  3589. /// @brief deserialize from stream
  3590. /// @sa https://json.nlohmann.me/api/basic_json/operator_gtgt/
  3591. /// @deprecated This stream operator is deprecated since 3.0.0 and will be removed in
  3592. /// version 4.0.0 of the library. Please use
  3593. /// operator>>(std::istream&, basic_json&) instead; that is,
  3594. /// replace calls like `j << i;` with `i >> j;`.
  3595. JSON_HEDLEY_DEPRECATED_FOR(3.0.0, operator>>(std::istream&, basic_json&))
  3596. friend std::istream& operator<<(basic_json& j, std::istream& i)
  3597. {
  3598. return operator>>(i, j);
  3599. }
  3600. /// @brief deserialize from stream
  3601. /// @sa https://json.nlohmann.me/api/basic_json/operator_gtgt/
  3602. friend std::istream& operator>>(std::istream& i, basic_json& j)
  3603. {
  3604. parser(detail::input_adapter(i)).parse(false, j);
  3605. return i;
  3606. }
  3607. #endif // JSON_NO_IO
  3608. /// @}
  3609. ///////////////////////////
  3610. // convenience functions //
  3611. ///////////////////////////
  3612. /// @brief return the type as string
  3613. /// @sa https://json.nlohmann.me/api/basic_json/type_name/
  3614. JSON_HEDLEY_RETURNS_NON_NULL
  3615. const char* type_name() const noexcept
  3616. {
  3617. switch (m_data.m_type)
  3618. {
  3619. case value_t::null:
  3620. return "null";
  3621. case value_t::object:
  3622. return "object";
  3623. case value_t::array:
  3624. return "array";
  3625. case value_t::string:
  3626. return "string";
  3627. case value_t::boolean:
  3628. return "boolean";
  3629. case value_t::binary:
  3630. return "binary";
  3631. case value_t::discarded:
  3632. return "discarded";
  3633. case value_t::number_integer:
  3634. case value_t::number_unsigned:
  3635. case value_t::number_float:
  3636. default:
  3637. return "number";
  3638. }
  3639. }
  3640. JSON_PRIVATE_UNLESS_TESTED:
  3641. //////////////////////
  3642. // member variables //
  3643. //////////////////////
  3644. struct data
  3645. {
  3646. /// the type of the current element
  3647. value_t m_type = value_t::null;
  3648. /// the value of the current element
  3649. json_value m_value = {};
  3650. data(const value_t v)
  3651. : m_type(v), m_value(v)
  3652. {
  3653. }
  3654. data(size_type cnt, const basic_json& val)
  3655. : m_type(value_t::array)
  3656. {
  3657. m_value.array = create<array_t>(cnt, val);
  3658. }
  3659. data() noexcept = default;
  3660. data(data&&) noexcept = default;
  3661. data(const data&) noexcept = delete;
  3662. data& operator=(data&&) noexcept = delete;
  3663. data& operator=(const data&) noexcept = delete;
  3664. ~data() noexcept
  3665. {
  3666. m_value.destroy(m_type);
  3667. }
  3668. };
  3669. data m_data = {};
  3670. #if JSON_DIAGNOSTICS
  3671. /// a pointer to a parent value (for debugging purposes)
  3672. basic_json* m_parent = nullptr;
  3673. #endif
  3674. //////////////////////////////////////////
  3675. // binary serialization/deserialization //
  3676. //////////////////////////////////////////
  3677. /// @name binary serialization/deserialization support
  3678. /// @{
  3679. public:
  3680. /// @brief create a CBOR serialization of a given JSON value
  3681. /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
  3682. static std::vector<std::uint8_t> to_cbor(const basic_json& j)
  3683. {
  3684. std::vector<std::uint8_t> result;
  3685. to_cbor(j, result);
  3686. return result;
  3687. }
  3688. /// @brief create a CBOR serialization of a given JSON value
  3689. /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
  3690. static void to_cbor(const basic_json& j, detail::output_adapter<std::uint8_t> o)
  3691. {
  3692. binary_writer<std::uint8_t>(o).write_cbor(j);
  3693. }
  3694. /// @brief create a CBOR serialization of a given JSON value
  3695. /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
  3696. static void to_cbor(const basic_json& j, detail::output_adapter<char> o)
  3697. {
  3698. binary_writer<char>(o).write_cbor(j);
  3699. }
  3700. /// @brief create a MessagePack serialization of a given JSON value
  3701. /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
  3702. static std::vector<std::uint8_t> to_msgpack(const basic_json& j)
  3703. {
  3704. std::vector<std::uint8_t> result;
  3705. to_msgpack(j, result);
  3706. return result;
  3707. }
  3708. /// @brief create a MessagePack serialization of a given JSON value
  3709. /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
  3710. static void to_msgpack(const basic_json& j, detail::output_adapter<std::uint8_t> o)
  3711. {
  3712. binary_writer<std::uint8_t>(o).write_msgpack(j);
  3713. }
  3714. /// @brief create a MessagePack serialization of a given JSON value
  3715. /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
  3716. static void to_msgpack(const basic_json& j, detail::output_adapter<char> o)
  3717. {
  3718. binary_writer<char>(o).write_msgpack(j);
  3719. }
  3720. /// @brief create a UBJSON serialization of a given JSON value
  3721. /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
  3722. static std::vector<std::uint8_t> to_ubjson(const basic_json& j,
  3723. const bool use_size = false,
  3724. const bool use_type = false)
  3725. {
  3726. std::vector<std::uint8_t> result;
  3727. to_ubjson(j, result, use_size, use_type);
  3728. return result;
  3729. }
  3730. /// @brief create a UBJSON serialization of a given JSON value
  3731. /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
  3732. static void to_ubjson(const basic_json& j, detail::output_adapter<std::uint8_t> o,
  3733. const bool use_size = false, const bool use_type = false)
  3734. {
  3735. binary_writer<std::uint8_t>(o).write_ubjson(j, use_size, use_type);
  3736. }
  3737. /// @brief create a UBJSON serialization of a given JSON value
  3738. /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
  3739. static void to_ubjson(const basic_json& j, detail::output_adapter<char> o,
  3740. const bool use_size = false, const bool use_type = false)
  3741. {
  3742. binary_writer<char>(o).write_ubjson(j, use_size, use_type);
  3743. }
  3744. /// @brief create a BJData serialization of a given JSON value
  3745. /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
  3746. static std::vector<std::uint8_t> to_bjdata(const basic_json& j,
  3747. const bool use_size = false,
  3748. const bool use_type = false)
  3749. {
  3750. std::vector<std::uint8_t> result;
  3751. to_bjdata(j, result, use_size, use_type);
  3752. return result;
  3753. }
  3754. /// @brief create a BJData serialization of a given JSON value
  3755. /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
  3756. static void to_bjdata(const basic_json& j, detail::output_adapter<std::uint8_t> o,
  3757. const bool use_size = false, const bool use_type = false)
  3758. {
  3759. binary_writer<std::uint8_t>(o).write_ubjson(j, use_size, use_type, true, true);
  3760. }
  3761. /// @brief create a BJData serialization of a given JSON value
  3762. /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
  3763. static void to_bjdata(const basic_json& j, detail::output_adapter<char> o,
  3764. const bool use_size = false, const bool use_type = false)
  3765. {
  3766. binary_writer<char>(o).write_ubjson(j, use_size, use_type, true, true);
  3767. }
  3768. /// @brief create a BSON serialization of a given JSON value
  3769. /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
  3770. static std::vector<std::uint8_t> to_bson(const basic_json& j)
  3771. {
  3772. std::vector<std::uint8_t> result;
  3773. to_bson(j, result);
  3774. return result;
  3775. }
  3776. /// @brief create a BSON serialization of a given JSON value
  3777. /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
  3778. static void to_bson(const basic_json& j, detail::output_adapter<std::uint8_t> o)
  3779. {
  3780. binary_writer<std::uint8_t>(o).write_bson(j);
  3781. }
  3782. /// @brief create a BSON serialization of a given JSON value
  3783. /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
  3784. static void to_bson(const basic_json& j, detail::output_adapter<char> o)
  3785. {
  3786. binary_writer<char>(o).write_bson(j);
  3787. }
  3788. /// @brief create a JSON value from an input in CBOR format
  3789. /// @sa https://json.nlohmann.me/api/basic_json/from_cbor/
  3790. template<typename InputType>
  3791. JSON_HEDLEY_WARN_UNUSED_RESULT
  3792. static basic_json from_cbor(InputType&& i,
  3793. const bool strict = true,
  3794. const bool allow_exceptions = true,
  3795. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  3796. {
  3797. basic_json result;
  3798. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  3799. auto ia = detail::input_adapter(std::forward<InputType>(i));
  3800. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler);
  3801. return res ? result : basic_json(value_t::discarded);
  3802. }
  3803. /// @brief create a JSON value from an input in CBOR format
  3804. /// @sa https://json.nlohmann.me/api/basic_json/from_cbor/
  3805. template<typename IteratorType>
  3806. JSON_HEDLEY_WARN_UNUSED_RESULT
  3807. static basic_json from_cbor(IteratorType first, IteratorType last,
  3808. const bool strict = true,
  3809. const bool allow_exceptions = true,
  3810. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  3811. {
  3812. basic_json result;
  3813. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  3814. auto ia = detail::input_adapter(std::move(first), std::move(last));
  3815. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler);
  3816. return res ? result : basic_json(value_t::discarded);
  3817. }
  3818. template<typename T>
  3819. JSON_HEDLEY_WARN_UNUSED_RESULT
  3820. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_cbor(ptr, ptr + len))
  3821. static basic_json from_cbor(const T* ptr, std::size_t len,
  3822. const bool strict = true,
  3823. const bool allow_exceptions = true,
  3824. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  3825. {
  3826. return from_cbor(ptr, ptr + len, strict, allow_exceptions, tag_handler);
  3827. }
  3828. JSON_HEDLEY_WARN_UNUSED_RESULT
  3829. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_cbor(ptr, ptr + len))
  3830. static basic_json from_cbor(detail::span_input_adapter&& i,
  3831. const bool strict = true,
  3832. const bool allow_exceptions = true,
  3833. const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
  3834. {
  3835. basic_json result;
  3836. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  3837. auto ia = i.get();
  3838. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  3839. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler);
  3840. return res ? result : basic_json(value_t::discarded);
  3841. }
  3842. /// @brief create a JSON value from an input in MessagePack format
  3843. /// @sa https://json.nlohmann.me/api/basic_json/from_msgpack/
  3844. template<typename InputType>
  3845. JSON_HEDLEY_WARN_UNUSED_RESULT
  3846. static basic_json from_msgpack(InputType&& i,
  3847. const bool strict = true,
  3848. const bool allow_exceptions = true)
  3849. {
  3850. basic_json result;
  3851. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  3852. auto ia = detail::input_adapter(std::forward<InputType>(i));
  3853. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict);
  3854. return res ? result : basic_json(value_t::discarded);
  3855. }
  3856. /// @brief create a JSON value from an input in MessagePack format
  3857. /// @sa https://json.nlohmann.me/api/basic_json/from_msgpack/
  3858. template<typename IteratorType>
  3859. JSON_HEDLEY_WARN_UNUSED_RESULT
  3860. static basic_json from_msgpack(IteratorType first, IteratorType last,
  3861. const bool strict = true,
  3862. const bool allow_exceptions = true)
  3863. {
  3864. basic_json result;
  3865. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  3866. auto ia = detail::input_adapter(std::move(first), std::move(last));
  3867. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict);
  3868. return res ? result : basic_json(value_t::discarded);
  3869. }
  3870. template<typename T>
  3871. JSON_HEDLEY_WARN_UNUSED_RESULT
  3872. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_msgpack(ptr, ptr + len))
  3873. static basic_json from_msgpack(const T* ptr, std::size_t len,
  3874. const bool strict = true,
  3875. const bool allow_exceptions = true)
  3876. {
  3877. return from_msgpack(ptr, ptr + len, strict, allow_exceptions);
  3878. }
  3879. JSON_HEDLEY_WARN_UNUSED_RESULT
  3880. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_msgpack(ptr, ptr + len))
  3881. static basic_json from_msgpack(detail::span_input_adapter&& i,
  3882. const bool strict = true,
  3883. const bool allow_exceptions = true)
  3884. {
  3885. basic_json result;
  3886. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  3887. auto ia = i.get();
  3888. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  3889. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict);
  3890. return res ? result : basic_json(value_t::discarded);
  3891. }
  3892. /// @brief create a JSON value from an input in UBJSON format
  3893. /// @sa https://json.nlohmann.me/api/basic_json/from_ubjson/
  3894. template<typename InputType>
  3895. JSON_HEDLEY_WARN_UNUSED_RESULT
  3896. static basic_json from_ubjson(InputType&& i,
  3897. const bool strict = true,
  3898. const bool allow_exceptions = true)
  3899. {
  3900. basic_json result;
  3901. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  3902. auto ia = detail::input_adapter(std::forward<InputType>(i));
  3903. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict);
  3904. return res ? result : basic_json(value_t::discarded);
  3905. }
  3906. /// @brief create a JSON value from an input in UBJSON format
  3907. /// @sa https://json.nlohmann.me/api/basic_json/from_ubjson/
  3908. template<typename IteratorType>
  3909. JSON_HEDLEY_WARN_UNUSED_RESULT
  3910. static basic_json from_ubjson(IteratorType first, IteratorType last,
  3911. const bool strict = true,
  3912. const bool allow_exceptions = true)
  3913. {
  3914. basic_json result;
  3915. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  3916. auto ia = detail::input_adapter(std::move(first), std::move(last));
  3917. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict);
  3918. return res ? result : basic_json(value_t::discarded);
  3919. }
  3920. template<typename T>
  3921. JSON_HEDLEY_WARN_UNUSED_RESULT
  3922. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_ubjson(ptr, ptr + len))
  3923. static basic_json from_ubjson(const T* ptr, std::size_t len,
  3924. const bool strict = true,
  3925. const bool allow_exceptions = true)
  3926. {
  3927. return from_ubjson(ptr, ptr + len, strict, allow_exceptions);
  3928. }
  3929. JSON_HEDLEY_WARN_UNUSED_RESULT
  3930. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_ubjson(ptr, ptr + len))
  3931. static basic_json from_ubjson(detail::span_input_adapter&& i,
  3932. const bool strict = true,
  3933. const bool allow_exceptions = true)
  3934. {
  3935. basic_json result;
  3936. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  3937. auto ia = i.get();
  3938. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  3939. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict);
  3940. return res ? result : basic_json(value_t::discarded);
  3941. }
  3942. /// @brief create a JSON value from an input in BJData format
  3943. /// @sa https://json.nlohmann.me/api/basic_json/from_bjdata/
  3944. template<typename InputType>
  3945. JSON_HEDLEY_WARN_UNUSED_RESULT
  3946. static basic_json from_bjdata(InputType&& i,
  3947. const bool strict = true,
  3948. const bool allow_exceptions = true)
  3949. {
  3950. basic_json result;
  3951. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  3952. auto ia = detail::input_adapter(std::forward<InputType>(i));
  3953. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict);
  3954. return res ? result : basic_json(value_t::discarded);
  3955. }
  3956. /// @brief create a JSON value from an input in BJData format
  3957. /// @sa https://json.nlohmann.me/api/basic_json/from_bjdata/
  3958. template<typename IteratorType>
  3959. JSON_HEDLEY_WARN_UNUSED_RESULT
  3960. static basic_json from_bjdata(IteratorType first, IteratorType last,
  3961. const bool strict = true,
  3962. const bool allow_exceptions = true)
  3963. {
  3964. basic_json result;
  3965. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  3966. auto ia = detail::input_adapter(std::move(first), std::move(last));
  3967. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict);
  3968. return res ? result : basic_json(value_t::discarded);
  3969. }
  3970. /// @brief create a JSON value from an input in BSON format
  3971. /// @sa https://json.nlohmann.me/api/basic_json/from_bson/
  3972. template<typename InputType>
  3973. JSON_HEDLEY_WARN_UNUSED_RESULT
  3974. static basic_json from_bson(InputType&& i,
  3975. const bool strict = true,
  3976. const bool allow_exceptions = true)
  3977. {
  3978. basic_json result;
  3979. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  3980. auto ia = detail::input_adapter(std::forward<InputType>(i));
  3981. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict);
  3982. return res ? result : basic_json(value_t::discarded);
  3983. }
  3984. /// @brief create a JSON value from an input in BSON format
  3985. /// @sa https://json.nlohmann.me/api/basic_json/from_bson/
  3986. template<typename IteratorType>
  3987. JSON_HEDLEY_WARN_UNUSED_RESULT
  3988. static basic_json from_bson(IteratorType first, IteratorType last,
  3989. const bool strict = true,
  3990. const bool allow_exceptions = true)
  3991. {
  3992. basic_json result;
  3993. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  3994. auto ia = detail::input_adapter(std::move(first), std::move(last));
  3995. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict);
  3996. return res ? result : basic_json(value_t::discarded);
  3997. }
  3998. template<typename T>
  3999. JSON_HEDLEY_WARN_UNUSED_RESULT
  4000. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_bson(ptr, ptr + len))
  4001. static basic_json from_bson(const T* ptr, std::size_t len,
  4002. const bool strict = true,
  4003. const bool allow_exceptions = true)
  4004. {
  4005. return from_bson(ptr, ptr + len, strict, allow_exceptions);
  4006. }
  4007. JSON_HEDLEY_WARN_UNUSED_RESULT
  4008. JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_bson(ptr, ptr + len))
  4009. static basic_json from_bson(detail::span_input_adapter&& i,
  4010. const bool strict = true,
  4011. const bool allow_exceptions = true)
  4012. {
  4013. basic_json result;
  4014. detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
  4015. auto ia = i.get();
  4016. // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
  4017. const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict);
  4018. return res ? result : basic_json(value_t::discarded);
  4019. }
  4020. /// @}
  4021. //////////////////////////
  4022. // JSON Pointer support //
  4023. //////////////////////////
  4024. /// @name JSON Pointer functions
  4025. /// @{
  4026. /// @brief access specified element via JSON Pointer
  4027. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  4028. reference operator[](const json_pointer& ptr)
  4029. {
  4030. return ptr.get_unchecked(this);
  4031. }
  4032. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  4033. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  4034. reference operator[](const ::nlohmann::json_pointer<BasicJsonType>& ptr)
  4035. {
  4036. return ptr.get_unchecked(this);
  4037. }
  4038. /// @brief access specified element via JSON Pointer
  4039. /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
  4040. const_reference operator[](const json_pointer& ptr) const
  4041. {
  4042. return ptr.get_unchecked(this);
  4043. }
  4044. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  4045. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  4046. const_reference operator[](const ::nlohmann::json_pointer<BasicJsonType>& ptr) const
  4047. {
  4048. return ptr.get_unchecked(this);
  4049. }
  4050. /// @brief access specified element via JSON Pointer
  4051. /// @sa https://json.nlohmann.me/api/basic_json/at/
  4052. reference at(const json_pointer& ptr)
  4053. {
  4054. return ptr.get_checked(this);
  4055. }
  4056. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  4057. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  4058. reference at(const ::nlohmann::json_pointer<BasicJsonType>& ptr)
  4059. {
  4060. return ptr.get_checked(this);
  4061. }
  4062. /// @brief access specified element via JSON Pointer
  4063. /// @sa https://json.nlohmann.me/api/basic_json/at/
  4064. const_reference at(const json_pointer& ptr) const
  4065. {
  4066. return ptr.get_checked(this);
  4067. }
  4068. template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
  4069. JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
  4070. const_reference at(const ::nlohmann::json_pointer<BasicJsonType>& ptr) const
  4071. {
  4072. return ptr.get_checked(this);
  4073. }
  4074. /// @brief return flattened JSON value
  4075. /// @sa https://json.nlohmann.me/api/basic_json/flatten/
  4076. basic_json flatten() const
  4077. {
  4078. basic_json result(value_t::object);
  4079. json_pointer::flatten("", *this, result);
  4080. return result;
  4081. }
  4082. /// @brief unflatten a previously flattened JSON value
  4083. /// @sa https://json.nlohmann.me/api/basic_json/unflatten/
  4084. basic_json unflatten() const
  4085. {
  4086. return json_pointer::unflatten(*this);
  4087. }
  4088. /// @}
  4089. //////////////////////////
  4090. // JSON Patch functions //
  4091. //////////////////////////
  4092. /// @name JSON Patch functions
  4093. /// @{
  4094. /// @brief applies a JSON patch in-place without copying the object
  4095. /// @sa https://json.nlohmann.me/api/basic_json/patch/
  4096. void patch_inplace(const basic_json& json_patch)
  4097. {
  4098. basic_json& result = *this;
  4099. // the valid JSON Patch operations
  4100. enum class patch_operations {add, remove, replace, move, copy, test, invalid};
  4101. const auto get_op = [](const std::string & op)
  4102. {
  4103. if (op == "add")
  4104. {
  4105. return patch_operations::add;
  4106. }
  4107. if (op == "remove")
  4108. {
  4109. return patch_operations::remove;
  4110. }
  4111. if (op == "replace")
  4112. {
  4113. return patch_operations::replace;
  4114. }
  4115. if (op == "move")
  4116. {
  4117. return patch_operations::move;
  4118. }
  4119. if (op == "copy")
  4120. {
  4121. return patch_operations::copy;
  4122. }
  4123. if (op == "test")
  4124. {
  4125. return patch_operations::test;
  4126. }
  4127. return patch_operations::invalid;
  4128. };
  4129. // wrapper for "add" operation; add value at ptr
  4130. const auto operation_add = [&result](json_pointer & ptr, basic_json val)
  4131. {
  4132. // adding to the root of the target document means replacing it
  4133. if (ptr.empty())
  4134. {
  4135. result = val;
  4136. return;
  4137. }
  4138. // make sure the top element of the pointer exists
  4139. json_pointer const top_pointer = ptr.top();
  4140. if (top_pointer != ptr)
  4141. {
  4142. result.at(top_pointer);
  4143. }
  4144. // get reference to parent of JSON pointer ptr
  4145. const auto last_path = ptr.back();
  4146. ptr.pop_back();
  4147. // parent must exist when performing patch add per RFC6902 specs
  4148. basic_json& parent = result.at(ptr);
  4149. switch (parent.m_data.m_type)
  4150. {
  4151. case value_t::null:
  4152. case value_t::object:
  4153. {
  4154. // use operator[] to add value
  4155. parent[last_path] = val;
  4156. break;
  4157. }
  4158. case value_t::array:
  4159. {
  4160. if (last_path == "-")
  4161. {
  4162. // special case: append to back
  4163. parent.push_back(val);
  4164. }
  4165. else
  4166. {
  4167. const auto idx = json_pointer::template array_index<basic_json_t>(last_path);
  4168. if (JSON_HEDLEY_UNLIKELY(idx > parent.size()))
  4169. {
  4170. // avoid undefined behavior
  4171. JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), &parent));
  4172. }
  4173. // default case: insert add offset
  4174. parent.insert(parent.begin() + static_cast<difference_type>(idx), val);
  4175. }
  4176. break;
  4177. }
  4178. // if there exists a parent it cannot be primitive
  4179. case value_t::string: // LCOV_EXCL_LINE
  4180. case value_t::boolean: // LCOV_EXCL_LINE
  4181. case value_t::number_integer: // LCOV_EXCL_LINE
  4182. case value_t::number_unsigned: // LCOV_EXCL_LINE
  4183. case value_t::number_float: // LCOV_EXCL_LINE
  4184. case value_t::binary: // LCOV_EXCL_LINE
  4185. case value_t::discarded: // LCOV_EXCL_LINE
  4186. default: // LCOV_EXCL_LINE
  4187. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  4188. }
  4189. };
  4190. // wrapper for "remove" operation; remove value at ptr
  4191. const auto operation_remove = [this, & result](json_pointer & ptr)
  4192. {
  4193. // get reference to parent of JSON pointer ptr
  4194. const auto last_path = ptr.back();
  4195. ptr.pop_back();
  4196. basic_json& parent = result.at(ptr);
  4197. // remove child
  4198. if (parent.is_object())
  4199. {
  4200. // perform range check
  4201. auto it = parent.find(last_path);
  4202. if (JSON_HEDLEY_LIKELY(it != parent.end()))
  4203. {
  4204. parent.erase(it);
  4205. }
  4206. else
  4207. {
  4208. JSON_THROW(out_of_range::create(403, detail::concat("key '", last_path, "' not found"), this));
  4209. }
  4210. }
  4211. else if (parent.is_array())
  4212. {
  4213. // note erase performs range check
  4214. parent.erase(json_pointer::template array_index<basic_json_t>(last_path));
  4215. }
  4216. };
  4217. // type check: top level value must be an array
  4218. if (JSON_HEDLEY_UNLIKELY(!json_patch.is_array()))
  4219. {
  4220. JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", &json_patch));
  4221. }
  4222. // iterate and apply the operations
  4223. for (const auto& val : json_patch)
  4224. {
  4225. // wrapper to get a value for an operation
  4226. const auto get_value = [&val](const std::string & op,
  4227. const std::string & member,
  4228. bool string_type) -> basic_json &
  4229. {
  4230. // find value
  4231. auto it = val.m_data.m_value.object->find(member);
  4232. // context-sensitive error message
  4233. const auto error_msg = (op == "op") ? "operation" : detail::concat("operation '", op, '\'');
  4234. // check if desired value is present
  4235. if (JSON_HEDLEY_UNLIKELY(it == val.m_data.m_value.object->end()))
  4236. {
  4237. // NOLINTNEXTLINE(performance-inefficient-string-concatenation)
  4238. JSON_THROW(parse_error::create(105, 0, detail::concat(error_msg, " must have member '", member, "'"), &val));
  4239. }
  4240. // check if result is of type string
  4241. if (JSON_HEDLEY_UNLIKELY(string_type && !it->second.is_string()))
  4242. {
  4243. // NOLINTNEXTLINE(performance-inefficient-string-concatenation)
  4244. JSON_THROW(parse_error::create(105, 0, detail::concat(error_msg, " must have string member '", member, "'"), &val));
  4245. }
  4246. // no error: return value
  4247. return it->second;
  4248. };
  4249. // type check: every element of the array must be an object
  4250. if (JSON_HEDLEY_UNLIKELY(!val.is_object()))
  4251. {
  4252. JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", &val));
  4253. }
  4254. // collect mandatory members
  4255. const auto op = get_value("op", "op", true).template get<std::string>();
  4256. const auto path = get_value(op, "path", true).template get<std::string>();
  4257. json_pointer ptr(path);
  4258. switch (get_op(op))
  4259. {
  4260. case patch_operations::add:
  4261. {
  4262. operation_add(ptr, get_value("add", "value", false));
  4263. break;
  4264. }
  4265. case patch_operations::remove:
  4266. {
  4267. operation_remove(ptr);
  4268. break;
  4269. }
  4270. case patch_operations::replace:
  4271. {
  4272. // the "path" location must exist - use at()
  4273. result.at(ptr) = get_value("replace", "value", false);
  4274. break;
  4275. }
  4276. case patch_operations::move:
  4277. {
  4278. const auto from_path = get_value("move", "from", true).template get<std::string>();
  4279. json_pointer from_ptr(from_path);
  4280. // the "from" location must exist - use at()
  4281. basic_json const v = result.at(from_ptr);
  4282. // The move operation is functionally identical to a
  4283. // "remove" operation on the "from" location, followed
  4284. // immediately by an "add" operation at the target
  4285. // location with the value that was just removed.
  4286. operation_remove(from_ptr);
  4287. operation_add(ptr, v);
  4288. break;
  4289. }
  4290. case patch_operations::copy:
  4291. {
  4292. const auto from_path = get_value("copy", "from", true).template get<std::string>();
  4293. const json_pointer from_ptr(from_path);
  4294. // the "from" location must exist - use at()
  4295. basic_json const v = result.at(from_ptr);
  4296. // The copy is functionally identical to an "add"
  4297. // operation at the target location using the value
  4298. // specified in the "from" member.
  4299. operation_add(ptr, v);
  4300. break;
  4301. }
  4302. case patch_operations::test:
  4303. {
  4304. bool success = false;
  4305. JSON_TRY
  4306. {
  4307. // check if "value" matches the one at "path"
  4308. // the "path" location must exist - use at()
  4309. success = (result.at(ptr) == get_value("test", "value", false));
  4310. }
  4311. JSON_INTERNAL_CATCH (out_of_range&)
  4312. {
  4313. // ignore out of range errors: success remains false
  4314. }
  4315. // throw an exception if test fails
  4316. if (JSON_HEDLEY_UNLIKELY(!success))
  4317. {
  4318. JSON_THROW(other_error::create(501, detail::concat("unsuccessful: ", val.dump()), &val));
  4319. }
  4320. break;
  4321. }
  4322. case patch_operations::invalid:
  4323. default:
  4324. {
  4325. // op must be "add", "remove", "replace", "move", "copy", or
  4326. // "test"
  4327. JSON_THROW(parse_error::create(105, 0, detail::concat("operation value '", op, "' is invalid"), &val));
  4328. }
  4329. }
  4330. }
  4331. }
  4332. /// @brief applies a JSON patch to a copy of the current object
  4333. /// @sa https://json.nlohmann.me/api/basic_json/patch/
  4334. basic_json patch(const basic_json& json_patch) const
  4335. {
  4336. basic_json result = *this;
  4337. result.patch_inplace(json_patch);
  4338. return result;
  4339. }
  4340. /// @brief creates a diff as a JSON patch
  4341. /// @sa https://json.nlohmann.me/api/basic_json/diff/
  4342. JSON_HEDLEY_WARN_UNUSED_RESULT
  4343. static basic_json diff(const basic_json& source, const basic_json& target,
  4344. const std::string& path = "")
  4345. {
  4346. // the patch
  4347. basic_json result(value_t::array);
  4348. // if the values are the same, return empty patch
  4349. if (source == target)
  4350. {
  4351. return result;
  4352. }
  4353. if (source.type() != target.type())
  4354. {
  4355. // different types: replace value
  4356. result.push_back(
  4357. {
  4358. {"op", "replace"}, {"path", path}, {"value", target}
  4359. });
  4360. return result;
  4361. }
  4362. switch (source.type())
  4363. {
  4364. case value_t::array:
  4365. {
  4366. // first pass: traverse common elements
  4367. std::size_t i = 0;
  4368. while (i < source.size() && i < target.size())
  4369. {
  4370. // recursive call to compare array values at index i
  4371. auto temp_diff = diff(source[i], target[i], detail::concat(path, '/', std::to_string(i)));
  4372. result.insert(result.end(), temp_diff.begin(), temp_diff.end());
  4373. ++i;
  4374. }
  4375. // We now reached the end of at least one array
  4376. // in a second pass, traverse the remaining elements
  4377. // remove my remaining elements
  4378. const auto end_index = static_cast<difference_type>(result.size());
  4379. while (i < source.size())
  4380. {
  4381. // add operations in reverse order to avoid invalid
  4382. // indices
  4383. result.insert(result.begin() + end_index, object(
  4384. {
  4385. {"op", "remove"},
  4386. {"path", detail::concat(path, '/', std::to_string(i))}
  4387. }));
  4388. ++i;
  4389. }
  4390. // add other remaining elements
  4391. while (i < target.size())
  4392. {
  4393. result.push_back(
  4394. {
  4395. {"op", "add"},
  4396. {"path", detail::concat(path, "/-")},
  4397. {"value", target[i]}
  4398. });
  4399. ++i;
  4400. }
  4401. break;
  4402. }
  4403. case value_t::object:
  4404. {
  4405. // first pass: traverse this object's elements
  4406. for (auto it = source.cbegin(); it != source.cend(); ++it)
  4407. {
  4408. // escape the key name to be used in a JSON patch
  4409. const auto path_key = detail::concat(path, '/', detail::escape(it.key()));
  4410. if (target.find(it.key()) != target.end())
  4411. {
  4412. // recursive call to compare object values at key it
  4413. auto temp_diff = diff(it.value(), target[it.key()], path_key);
  4414. result.insert(result.end(), temp_diff.begin(), temp_diff.end());
  4415. }
  4416. else
  4417. {
  4418. // found a key that is not in o -> remove it
  4419. result.push_back(object(
  4420. {
  4421. {"op", "remove"}, {"path", path_key}
  4422. }));
  4423. }
  4424. }
  4425. // second pass: traverse other object's elements
  4426. for (auto it = target.cbegin(); it != target.cend(); ++it)
  4427. {
  4428. if (source.find(it.key()) == source.end())
  4429. {
  4430. // found a key that is not in this -> add it
  4431. const auto path_key = detail::concat(path, '/', detail::escape(it.key()));
  4432. result.push_back(
  4433. {
  4434. {"op", "add"}, {"path", path_key},
  4435. {"value", it.value()}
  4436. });
  4437. }
  4438. }
  4439. break;
  4440. }
  4441. case value_t::null:
  4442. case value_t::string:
  4443. case value_t::boolean:
  4444. case value_t::number_integer:
  4445. case value_t::number_unsigned:
  4446. case value_t::number_float:
  4447. case value_t::binary:
  4448. case value_t::discarded:
  4449. default:
  4450. {
  4451. // both primitive type: replace value
  4452. result.push_back(
  4453. {
  4454. {"op", "replace"}, {"path", path}, {"value", target}
  4455. });
  4456. break;
  4457. }
  4458. }
  4459. return result;
  4460. }
  4461. /// @}
  4462. ////////////////////////////////
  4463. // JSON Merge Patch functions //
  4464. ////////////////////////////////
  4465. /// @name JSON Merge Patch functions
  4466. /// @{
  4467. /// @brief applies a JSON Merge Patch
  4468. /// @sa https://json.nlohmann.me/api/basic_json/merge_patch/
  4469. void merge_patch(const basic_json& apply_patch)
  4470. {
  4471. if (apply_patch.is_object())
  4472. {
  4473. if (!is_object())
  4474. {
  4475. *this = object();
  4476. }
  4477. for (auto it = apply_patch.begin(); it != apply_patch.end(); ++it)
  4478. {
  4479. if (it.value().is_null())
  4480. {
  4481. erase(it.key());
  4482. }
  4483. else
  4484. {
  4485. operator[](it.key()).merge_patch(it.value());
  4486. }
  4487. }
  4488. }
  4489. else
  4490. {
  4491. *this = apply_patch;
  4492. }
  4493. }
  4494. /// @}
  4495. };
  4496. /// @brief user-defined to_string function for JSON values
  4497. /// @sa https://json.nlohmann.me/api/basic_json/to_string/
  4498. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  4499. std::string to_string(const NLOHMANN_BASIC_JSON_TPL& j)
  4500. {
  4501. return j.dump();
  4502. }
  4503. inline namespace literals
  4504. {
  4505. inline namespace json_literals
  4506. {
  4507. /// @brief user-defined string literal for JSON values
  4508. /// @sa https://json.nlohmann.me/api/basic_json/operator_literal_json/
  4509. JSON_HEDLEY_NON_NULL(1)
  4510. #if !defined(JSON_HEDLEY_GCC_VERSION) || JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0)
  4511. inline nlohmann::json operator ""_json(const char* s, std::size_t n)
  4512. #else
  4513. inline nlohmann::json operator "" _json(const char* s, std::size_t n)
  4514. #endif
  4515. {
  4516. return nlohmann::json::parse(s, s + n);
  4517. }
  4518. /// @brief user-defined string literal for JSON pointer
  4519. /// @sa https://json.nlohmann.me/api/basic_json/operator_literal_json_pointer/
  4520. JSON_HEDLEY_NON_NULL(1)
  4521. #if !defined(JSON_HEDLEY_GCC_VERSION) || JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0)
  4522. inline nlohmann::json::json_pointer operator ""_json_pointer(const char* s, std::size_t n)
  4523. #else
  4524. inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std::size_t n)
  4525. #endif
  4526. {
  4527. return nlohmann::json::json_pointer(std::string(s, n));
  4528. }
  4529. } // namespace json_literals
  4530. } // namespace literals
  4531. NLOHMANN_JSON_NAMESPACE_END
  4532. ///////////////////////
  4533. // nonmember support //
  4534. ///////////////////////
  4535. namespace std // NOLINT(cert-dcl58-cpp)
  4536. {
  4537. /// @brief hash value for JSON objects
  4538. /// @sa https://json.nlohmann.me/api/basic_json/std_hash/
  4539. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  4540. struct hash<nlohmann::NLOHMANN_BASIC_JSON_TPL> // NOLINT(cert-dcl58-cpp)
  4541. {
  4542. std::size_t operator()(const nlohmann::NLOHMANN_BASIC_JSON_TPL& j) const
  4543. {
  4544. return nlohmann::detail::hash(j);
  4545. }
  4546. };
  4547. // specialization for std::less<value_t>
  4548. template<>
  4549. struct less< ::nlohmann::detail::value_t> // do not remove the space after '<', see https://github.com/nlohmann/json/pull/679
  4550. {
  4551. /*!
  4552. @brief compare two value_t enum values
  4553. @since version 3.0.0
  4554. */
  4555. bool operator()(::nlohmann::detail::value_t lhs,
  4556. ::nlohmann::detail::value_t rhs) const noexcept
  4557. {
  4558. #if JSON_HAS_THREE_WAY_COMPARISON
  4559. return std::is_lt(lhs <=> rhs); // *NOPAD*
  4560. #else
  4561. return ::nlohmann::detail::operator<(lhs, rhs);
  4562. #endif
  4563. }
  4564. };
  4565. // C++20 prohibit function specialization in the std namespace.
  4566. #ifndef JSON_HAS_CPP_20
  4567. /// @brief exchanges the values of two JSON objects
  4568. /// @sa https://json.nlohmann.me/api/basic_json/std_swap/
  4569. NLOHMANN_BASIC_JSON_TPL_DECLARATION
  4570. inline void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL& j1, nlohmann::NLOHMANN_BASIC_JSON_TPL& j2) noexcept( // NOLINT(readability-inconsistent-declaration-parameter-name, cert-dcl58-cpp)
  4571. is_nothrow_move_constructible<nlohmann::NLOHMANN_BASIC_JSON_TPL>::value&& // NOLINT(misc-redundant-expression,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
  4572. is_nothrow_move_assignable<nlohmann::NLOHMANN_BASIC_JSON_TPL>::value)
  4573. {
  4574. j1.swap(j2);
  4575. }
  4576. #endif
  4577. } // namespace std
  4578. #if JSON_USE_GLOBAL_UDLS
  4579. #if !defined(JSON_HEDLEY_GCC_VERSION) || JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0)
  4580. using nlohmann::literals::json_literals::operator ""_json; // NOLINT(misc-unused-using-decls,google-global-names-in-headers)
  4581. using nlohmann::literals::json_literals::operator ""_json_pointer; //NOLINT(misc-unused-using-decls,google-global-names-in-headers)
  4582. #else
  4583. using nlohmann::literals::json_literals::operator "" _json; // NOLINT(misc-unused-using-decls,google-global-names-in-headers)
  4584. using nlohmann::literals::json_literals::operator "" _json_pointer; //NOLINT(misc-unused-using-decls,google-global-names-in-headers)
  4585. #endif
  4586. #endif
  4587. #include <nlohmann/detail/macro_unscope.hpp>
  4588. #endif // INCLUDE_NLOHMANN_JSON_HPP_