2 Star 2 Fork 0

干饭小白/轮子

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
yyjson.h 154.20 KB
一键复制 编辑 原始数据 按行查看 历史
干饭小白 提交于 2023-04-21 06:26 . yyjson接口封装
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252
/*==============================================================================
* Created by Yaoyuan on 2019/3/9.
* Copyright (C) 2019 Yaoyuan <ibireme@gmail.com>.
*
* Released under the MIT License:
* https://github.com/ibireme/yyjson/blob/master/LICENSE
*============================================================================*/
#ifndef YYJSON_H
#define YYJSON_H
/*==============================================================================
* Header Files
*============================================================================*/
#include <stdlib.h>
#include <stddef.h>
#include <limits.h>
#include <string.h>
#include <float.h>
/*==============================================================================
* Version
*============================================================================*/
#define YYJSON_VERSION_MAJOR 0
#define YYJSON_VERSION_MINOR 4
#define YYJSON_VERSION_PATCH 0
#define YYJSON_VERSION_HEX 0x000400
#define YYJSON_VERSION_STRING "0.4.0"
/*==============================================================================
* Compile Flags
*============================================================================*/
/* Define as 1 to disable JSON reader.
This may reduce binary size if you don't need JSON reader */
#ifndef YYJSON_DISABLE_READER
#endif
/* Define as 1 to disable JSON writer.
This may reduce binary size if you don't need JSON writer */
#ifndef YYJSON_DISABLE_WRITER
#endif
/* Define as 1 to disable the fast floating-point number conversion in yyjson,
and use libc's `strtod/snprintf` instead. This may reduce binary size,
but slow down floating-point reading and writing speed. */
#ifndef YYJSON_DISABLE_FAST_FP_CONV
#endif
/* Define as 1 to disable non-standard JSON support at compile time:
Reading and writing inf/nan literal, such as 'NaN', '-Infinity'.
Single line and multiple line comments.
Single trailing comma at the end of an object or array.
This may also invalidate these options:
YYJSON_READ_ALLOW_INF_AND_NAN
YYJSON_READ_ALLOW_COMMENTS
YYJSON_READ_ALLOW_TRAILING_COMMAS
YYJSON_WRITE_ALLOW_INF_AND_NAN
This may reduce binary size, and increase performance slightly. */
#ifndef YYJSON_DISABLE_NON_STANDARD
#endif
/* Define as 1 to disable unaligned memory access if target architecture does
not support unaligned memory access (such as some embedded processors).
If this value is not defined, yyjson will perform some automatic detection.
Wrong definition of this flag may cause performance degradation, but will not
cause runtime errors. */
#ifndef YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS
#endif
/* Define as 1 to export symbols when build library as Windows DLL. */
#ifndef YYJSON_EXPORTS
#endif
/* Define as 1 to import symbols when use library as Windows DLL. */
#ifndef YYJSON_IMPORTS
#endif
/* Define as 1 to include <stdint.h> for compiler which doesn't support C99. */
#ifndef YYJSON_HAS_STDINT_H
#endif
/* Define as 1 to include <stdbool.h> for compiler which doesn't support C99. */
#ifndef YYJSON_HAS_STDBOOL_H
#endif
/*==============================================================================
* Compiler Macros
*============================================================================*/
/* compiler version check (MSVC) */
#ifdef _MSC_VER
# define YYJSON_MSC_VER _MSC_VER
#else
# define YYJSON_MSC_VER 0
#endif
/* compiler version check (GCC) */
#ifdef __GNUC__
# define YYJSON_GCC_VER __GNUC__
#else
# define YYJSON_GCC_VER 0
#endif
/* C version check */
#if defined(__STDC__) && (__STDC__ >= 1) && defined(__STDC_VERSION__)
# define YYJSON_STDC_VER __STDC_VERSION__
#else
# define YYJSON_STDC_VER 0
#endif
/* C++ version check */
#if defined(__cplusplus)
# define YYJSON_CPP_VER __cplusplus
#else
# define YYJSON_CPP_VER 0
#endif
/* compiler builtin check (since gcc 10.0, clang 2.6, icc 2021) */
#ifndef yyjson_has_builtin
# ifdef __has_builtin
# define yyjson_has_builtin(x) __has_builtin(x)
# else
# define yyjson_has_builtin(x) 0
# endif
#endif
/* compiler attribute check (since gcc 5.0, clang 2.9, icc 17) */
#ifndef yyjson_has_attribute
# ifdef __has_attribute
# define yyjson_has_attribute(x) __has_attribute(x)
# else
# define yyjson_has_attribute(x) 0
# endif
#endif
/* include check (since gcc 5.0, clang 2.7, icc 16) */
#ifndef yyjson_has_include
# ifdef __has_include
# define yyjson_has_include(x) __has_include(x)
# else
# define yyjson_has_include(x) 0
# endif
#endif
/* inline */
#ifndef yyjson_inline
# if YYJSON_MSC_VER >= 1200
# define yyjson_inline __forceinline
# elif defined(_MSC_VER)
# define yyjson_inline __inline
# elif yyjson_has_attribute(always_inline) || YYJSON_GCC_VER >= 4
# define yyjson_inline __inline__ __attribute__((always_inline))
# elif defined(__clang__) || defined(__GNUC__)
# define yyjson_inline __inline__
# elif defined(__cplusplus) || YYJSON_STDC_VER >= 199901L
# define yyjson_inline inline
# else
# define yyjson_inline
# endif
#endif
/* noinline */
#ifndef yyjson_noinline
# if YYJSON_MSC_VER >= 1400
# define yyjson_noinline __declspec(noinline)
# elif yyjson_has_attribute(noinline) || YYJSON_GCC_VER >= 4
# define yyjson_noinline __attribute__((noinline))
# else
# define yyjson_noinline
# endif
#endif
/* align */
#ifndef yyjson_align
# if YYJSON_MSC_VER >= 1300
# define yyjson_align(x) __declspec(align(x))
# elif yyjson_has_attribute(aligned) || defined(__GNUC__)
# define yyjson_align(x) __attribute__((aligned(x)))
# elif YYJSON_CPP_VER >= 201103L
# define yyjson_align(x) alignas(x)
# else
# define yyjson_align(x)
# endif
#endif
/* likely */
#ifndef yyjson_likely
# if yyjson_has_builtin(__builtin_expect) || YYJSON_GCC_VER >= 4
# define yyjson_likely(expr) __builtin_expect(!!(expr), 1)
# else
# define yyjson_likely(expr) (expr)
# endif
#endif
/* unlikely */
#ifndef yyjson_unlikely
# if yyjson_has_builtin(__builtin_expect) || YYJSON_GCC_VER >= 4
# define yyjson_unlikely(expr) __builtin_expect(!!(expr), 0)
# else
# define yyjson_unlikely(expr) (expr)
# endif
#endif
/* function export */
#ifndef yyjson_api
# if defined(_WIN32)
# if defined(YYJSON_EXPORTS) && YYJSON_EXPORTS
# define yyjson_api __declspec(dllexport)
# elif defined(YYJSON_IMPORTS) && YYJSON_IMPORTS
# define yyjson_api __declspec(dllimport)
# else
# define yyjson_api
# endif
# elif yyjson_has_attribute(visibility) || YYJSON_GCC_VER >= 4
# define yyjson_api __attribute__((visibility("default")))
# else
# define yyjson_api
# endif
#endif
/* inline function export */
#ifndef yyjson_api_inline
# define yyjson_api_inline static yyjson_inline
#endif
/* stdint (C89 compatible) */
#if (defined(YYJSON_HAS_STDINT_H) && YYJSON_HAS_STDINT_H) || \
YYJSON_MSC_VER >= 1600 || YYJSON_STDC_VER >= 199901L || \
defined(_STDINT_H) || defined(_STDINT_H_) || \
defined(__CLANG_STDINT_H) || defined(_STDINT_H_INCLUDED) || \
yyjson_has_include(<stdint.h>)
# include <stdint.h>
#elif defined(_MSC_VER)
# if _MSC_VER < 1300
typedef signed char int8_t;
typedef signed short int16_t;
typedef signed int int32_t;
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef signed __int64 int64_t;
typedef unsigned __int64 uint64_t;
# else
typedef signed __int8 int8_t;
typedef signed __int16 int16_t;
typedef signed __int32 int32_t;
typedef unsigned __int8 uint8_t;
typedef unsigned __int16 uint16_t;
typedef unsigned __int32 uint32_t;
typedef signed __int64 int64_t;
typedef unsigned __int64 uint64_t;
# endif
#else
# if UCHAR_MAX == 0xFFU
typedef signed char int8_t;
typedef unsigned char uint8_t;
# else
# error cannot find 8-bit integer type
# endif
# if USHRT_MAX == 0xFFFFU
typedef unsigned short uint16_t;
typedef signed short int16_t;
# elif UINT_MAX == 0xFFFFU
typedef unsigned int uint16_t;
typedef signed int int16_t;
# else
# error cannot find 16-bit integer type
# endif
# if UINT_MAX == 0xFFFFFFFFUL
typedef unsigned int uint32_t;
typedef signed int int32_t;
# elif ULONG_MAX == 0xFFFFFFFFUL
typedef unsigned long uint32_t;
typedef signed long int32_t;
# elif USHRT_MAX == 0xFFFFFFFFUL
typedef unsigned short uint32_t;
typedef signed short int32_t;
# else
# error cannot find 32-bit integer type
# endif
# if defined(__INT64_TYPE__) && defined(__UINT64_TYPE__)
typedef __INT64_TYPE__ int64_t;
typedef __UINT64_TYPE__ uint64_t;
# elif defined(__GNUC__) || defined(__clang__)
# if !defined(_SYS_TYPES_H) && !defined(__int8_t_defined)
__extension__ typedef long long int64_t;
# endif
__extension__ typedef unsigned long long uint64_t;
# elif defined(_LONG_LONG) || defined(__MWERKS__) || defined(_CRAYC) || \
defined(__SUNPRO_C) || defined(__SUNPRO_CC)
typedef long long int64_t;
typedef unsigned long long uint64_t;
# elif (defined(__BORLANDC__) && __BORLANDC__ > 0x460) || \
defined(__WATCOM_INT64__) || defined (__alpha) || defined (__DECC)
typedef __int64 int64_t;
typedef unsigned __int64 uint64_t;
# else
# error cannot find 64-bit integer type
# endif
#endif
/* stdbool (C89 compatible) */
#if (defined(YYJSON_HAS_STDBOOL_H) && YYJSON_HAS_STDBOOL_H) || \
(yyjson_has_include(<stdbool.h>) && !defined(__STRICT_ANSI__)) || \
YYJSON_MSC_VER >= 1800 || YYJSON_STDC_VER >= 199901L
# include <stdbool.h>
#elif !defined(__bool_true_false_are_defined)
# define __bool_true_false_are_defined 1
# if defined(__cplusplus)
# if defined(__GNUC__) && !defined(__STRICT_ANSI__)
# define _Bool bool
# if __cplusplus < 201103L
# define bool bool
# define false false
# define true true
# endif
# endif
# else
# define bool unsigned char
# define true 1
# define false 0
# endif
#endif
/* char bit check */
#if defined(CHAR_BIT)
# if CHAR_BIT != 8
# error non 8-bit char is not supported
# endif
#endif
/*==============================================================================
* Compile Hint Begin
*============================================================================*/
/* extern "C" begin */
#ifdef __cplusplus
extern "C" {
#endif
/* warning suppress begin */
#if defined(__clang__)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wunused-function"
# pragma clang diagnostic ignored "-Wunused-parameter"
#elif defined(__GNUC__)
# if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
# pragma GCC diagnostic push
# endif
# pragma GCC diagnostic ignored "-Wunused-function"
# pragma GCC diagnostic ignored "-Wunused-parameter"
#elif defined(_MSC_VER)
# pragma warning(push)
# pragma warning(disable:4800) /* 'int': forcing value to 'true' or 'false' */
#endif
/* version, same as YYJSON_VERSION_HEX */
yyjson_api uint32_t yyjson_version(void);
/*==============================================================================
* JSON Types
*============================================================================*/
/** Type of JSON value (3 bit). */
typedef uint8_t yyjson_type;
#define YYJSON_TYPE_NONE ((uint8_t)0) /* _____000 */
#define YYJSON_TYPE_NULL ((uint8_t)2) /* _____010 */
#define YYJSON_TYPE_BOOL ((uint8_t)3) /* _____011 */
#define YYJSON_TYPE_NUM ((uint8_t)4) /* _____100 */
#define YYJSON_TYPE_STR ((uint8_t)5) /* _____101 */
#define YYJSON_TYPE_ARR ((uint8_t)6) /* _____110 */
#define YYJSON_TYPE_OBJ ((uint8_t)7) /* _____111 */
/** Subtype of JSON value (2 bit). */
typedef uint8_t yyjson_subtype;
#define YYJSON_SUBTYPE_NONE ((uint8_t)(0 << 3)) /* ___00___ */
#define YYJSON_SUBTYPE_FALSE ((uint8_t)(0 << 3)) /* ___00___ */
#define YYJSON_SUBTYPE_TRUE ((uint8_t)(1 << 3)) /* ___01___ */
#define YYJSON_SUBTYPE_UINT ((uint8_t)(0 << 3)) /* ___00___ */
#define YYJSON_SUBTYPE_SINT ((uint8_t)(1 << 3)) /* ___01___ */
#define YYJSON_SUBTYPE_REAL ((uint8_t)(2 << 3)) /* ___10___ */
/** Mask and bits of JSON value. */
#define YYJSON_TYPE_MASK ((uint8_t)0x07) /* _____111 */
#define YYJSON_TYPE_BIT ((uint8_t)3)
#define YYJSON_SUBTYPE_MASK ((uint8_t)0x18) /* ___11___ */
#define YYJSON_SUBTYPE_BIT ((uint8_t)2)
#define YYJSON_RESERVED_MASK ((uint8_t)0xE0) /* 111_____ */
#define YYJSON_RESERVED_BIT ((uint8_t)3)
#define YYJSON_TAG_MASK ((uint8_t)0xFF) /* 11111111 */
#define YYJSON_TAG_BIT ((uint8_t)8)
/** Padding size for JSON reader. */
#define YYJSON_PADDING_SIZE 4
/*==============================================================================
* Allocator
*============================================================================*/
/**
A memory allocator.
Typically you don't need to use it, unless you want to customize your own
memory allocator.
*/
typedef struct yyjson_alc {
/* Same as libc's malloc(), should not be NULL. */
void *(*malloc)(void *ctx, size_t size);
/* Same as libc's realloc(), should not be NULL. */
void *(*realloc)(void *ctx, void *ptr, size_t size);
/* Same as libc's free(), should not be NULL. */
void (*free)(void *ctx, void *ptr);
/* A context for malloc/realloc/free, can be NULL. */
void *ctx;
} yyjson_alc;
/**
A pool allocator uses fixed length pre-allocated memory.
This allocator may used to avoid malloc()/memmove() calls.
The pre-allocated memory should be held by the caller. This is not
a general-purpose allocator, and should only be used to read or write
single JSON document.
Sample code (parse JSON with stack memory only):
char buf[65536];
yyjson_alc alc;
yyjson_alc_pool_init(&alc, buf, 65536);
const char *json = "{\"name\":\"Helvetica\",\"size\":14}"
yyjson_doc *doc = yyjson_read_opts(json, strlen(json), 0, &alc, NULL);
*/
yyjson_api bool yyjson_alc_pool_init(yyjson_alc *alc, void *buf, size_t size);
/*==============================================================================
* JSON Structure
*============================================================================*/
/** An immutable JSON document. */
typedef struct yyjson_doc yyjson_doc;
/** An immutable JSON value. */
typedef struct yyjson_val yyjson_val;
/** A mutable JSON document. */
typedef struct yyjson_mut_doc yyjson_mut_doc;
/** A mutable JSON value. */
typedef struct yyjson_mut_val yyjson_mut_val;
/*==============================================================================
* JSON Reader API
*============================================================================*/
/** Options for JSON reader. */
typedef uint32_t yyjson_read_flag;
/** Default option (RFC 8259 compliant):
- Read positive integer as uint64_t.
- Read negative integer as int64_t.
- Read floating-point number as double with correct rounding.
- Read integer which cannot fit in uint64_t or int64_t as double.
- Report error if real number is infinity.
- Report error if string contains invalid UTF-8 character or BOM.
- Report error on trailing commas, comments, inf and nan literals. */
static const yyjson_read_flag YYJSON_READ_NOFLAG = 0 << 0;
/** Read the input data in-situ.
This option allows the reader to modify and use input data to store string
values, which can increase reading speed slightly.
The caller should hold the input data before free the document.
The input data must be padded by at least `YYJSON_PADDING_SIZE` byte.
For example: "[1,2]" should be "[1,2]\0\0\0\0", length should be 5. */
static const yyjson_read_flag YYJSON_READ_INSITU = 1 << 0;
/** Stop when done instead of issues an error if there's additional content
after a JSON document. This option may used to parse small pieces of JSON
in larger data, such as NDJSON. */
static const yyjson_read_flag YYJSON_READ_STOP_WHEN_DONE = 1 << 1;
/** Allow single trailing comma at the end of an object or array,
such as [1,2,3,] {"a":1,"b":2,}. */
static const yyjson_read_flag YYJSON_READ_ALLOW_TRAILING_COMMAS = 1 << 2;
/** Allow C-style single line and multiple line comments. */
static const yyjson_read_flag YYJSON_READ_ALLOW_COMMENTS = 1 << 3;
/** Allow inf/nan number and literal, case-insensitive,
such as 1e999, NaN, inf, -Infinity. */
static const yyjson_read_flag YYJSON_READ_ALLOW_INF_AND_NAN = 1 << 4;
/** Result code for JSON reader. */
typedef uint32_t yyjson_read_code;
/** Success, no error. */
static const yyjson_read_code YYJSON_READ_SUCCESS = 0;
/** Invalid parameter, such as NULL string or invalid file path. */
static const yyjson_read_code YYJSON_READ_ERROR_INVALID_PARAMETER = 1;
/** Memory allocation failure occurs. */
static const yyjson_read_code YYJSON_READ_ERROR_MEMORY_ALLOCATION = 2;
/** Input JSON string is empty. */
static const yyjson_read_code YYJSON_READ_ERROR_EMPTY_CONTENT = 3;
/** Unexpected content after document, such as "[1]#". */
static const yyjson_read_code YYJSON_READ_ERROR_UNEXPECTED_CONTENT = 4;
/** Unexpected ending, such as "[123". */
static const yyjson_read_code YYJSON_READ_ERROR_UNEXPECTED_END = 5;
/** Unexpected character inside the document, such as "[#]". */
static const yyjson_read_code YYJSON_READ_ERROR_UNEXPECTED_CHARACTER = 6;
/** Invalid JSON structure, such as "[1,]". */
static const yyjson_read_code YYJSON_READ_ERROR_JSON_STRUCTURE = 7;
/** Invalid comment, such as unclosed multi-line comment. */
static const yyjson_read_code YYJSON_READ_ERROR_INVALID_COMMENT = 8;
/** Invalid number, such as "123.e12", "000". */
static const yyjson_read_code YYJSON_READ_ERROR_INVALID_NUMBER = 9;
/** Invalid string, such as invalid escaped character inside a string. */
static const yyjson_read_code YYJSON_READ_ERROR_INVALID_STRING = 10;
/** Invalid JSON literal, such as "truu". */
static const yyjson_read_code YYJSON_READ_ERROR_LITERAL = 11;
/** Failed to open a file. */
static const yyjson_read_code YYJSON_READ_ERROR_FILE_OPEN = 12;
/** Failed to read a file. */
static const yyjson_read_code YYJSON_READ_ERROR_FILE_READ = 13;
/** Error information for JSON reader. */
typedef struct yyjson_read_err {
/** Error code, see `yyjson_read_code` for all available values. */
yyjson_read_code code;
/** Short error message (NULL for success). */
const char *msg;
/** Error byte position for input data (0 for success). */
size_t pos;
} yyjson_read_err;
/**
Read JSON with options.
This function is thread-safe if you make sure that:
1. The `dat` is not modified by other threads.
2. The `alc` is thread-safe or NULL.
@param dat The JSON data (UTF-8 without BOM).
If you pass NULL, you will get NULL result.
The data will not be modified without the flag `YYJSON_READ_INSITU`,
so you can pass a (const char *) string and case it to (char *) iff
you don't use the `YYJSON_READ_INSITU` flag.
@param len The JSON data's length.
If you pass 0, you will get NULL result.
@param flg The JSON read options.
You can combine multiple options using bitwise `|` operator.
@param alc The memory allocator used by JSON reader.
Pass NULL to use the libc's default allocator (thread-safe).
@param err A pointer to receive error information.
Pass NULL if you don't need error information.
@return A new JSON document, or NULL if error occurs.
You should use yyjson_doc_free() to release it
when it's no longer needed.
*/
yyjson_api yyjson_doc *yyjson_read_opts(char *dat,
size_t len,
yyjson_read_flag flg,
const yyjson_alc *alc,
yyjson_read_err *err);
/**
Read a JSON file.
This function is thread-safe if you make sure that:
1. The file is not modified by other threads.
2. The `alc` is thread-safe or NULL.
@param path The JSON file's path.
If you pass an invalid path, you will get NULL result.
@param flg The JSON read options.
You can combine multiple options using bitwise `|` operator.
@param alc The memory allocator used by JSON reader.
Pass NULL to use the libc's default allocator (thread-safe).
@param err A pointer to receive error information.
Pass NULL if you don't need error information.
@return A new JSON document, or NULL if error occurs.
You should use yyjson_doc_free() to release it
when it's no longer needed.
*/
yyjson_api yyjson_doc *yyjson_read_file(const char *path,
yyjson_read_flag flg,
const yyjson_alc *alc,
yyjson_read_err *err);
/**
Read a JSON string.
This function is thread-safe.
@param dat The JSON string (UTF-8 without BOM).
If you pass NULL, you will get NULL result.
@param len The JSON data's length.
If you pass 0, you will get NULL result.
@param flg The JSON read options.
You can combine multiple options using bitwise `|` operator.
@return A new JSON document, or NULL if error occurs.
You should use yyjson_doc_free() to release it
when it's no longer needed.
*/
yyjson_api_inline yyjson_doc *yyjson_read(const char *dat,
size_t len,
yyjson_read_flag flg) {
flg &= ~YYJSON_READ_INSITU; /* const string cannot be modified */
return yyjson_read_opts((char *)dat, len, flg, NULL, NULL);
}
/**
Returns the size of maximum memory usage to read a JSON data.
You may use this value to avoid malloc() or calloc() call inside the reader
to get better performance, or read multiple JSON.
Sample code:
char *dat1, *dat2, *dat3; // JSON data
size_t len1, len2, len3; // JSON length
size_t max_len = max(len1, len2, len3);
yyjson_doc *doc;
// use one allocator for multiple JSON
size_t size = yyjson_read_max_memory_usage(max_len, 0);
void *buf = malloc(size);
yyjson_alc alc;
yyjson_alc_pool_init(&alc, buf, size);
// no more alloc() or realloc() call during reading
doc = yyjson_read_opts(dat1, len1, 0, &alc, NULL);
yyjson_doc_free(doc);
doc = yyjson_read_opts(dat2, len2, 0, &alc, NULL);
yyjson_doc_free(doc);
doc = yyjson_read_opts(dat3, len3, 0, &alc, NULL);
yyjson_doc_free(doc);
free(buf);
@param len The JSON data's length.
@param flg The JSON read options.
@return The maximum memory size, or 0 if overflow.
*/
yyjson_api_inline size_t yyjson_read_max_memory_usage(size_t len,
yyjson_read_flag flg) {
/*
1. The max value count is (json_size / 2 + 1),
for example: "[1,2,3,4]" size is 9, value count is 5.
2. Some broken JSON may cost more memory during reading, but fail at end,
for example: "[[[[[[[[".
3. yyjson use 16 bytes per value, see struct yyjson_val.
4. yyjson use dynamic memory with a growth factor of 1.5.
The max memory size is (json_size / 2 * 16 * 1.5 + padding).
*/
size_t mul = (size_t)12 + !(flg & YYJSON_READ_INSITU);
size_t pad = 256;
size_t max = (size_t)(~(size_t)0);
if (flg & YYJSON_READ_STOP_WHEN_DONE) len = len < 256 ? 256 : len;
if (len >= (max - pad - mul) / mul) return 0;
return len * mul + pad;
}
/*==============================================================================
* JSON Writer API
*============================================================================*/
/** Options for JSON writer. */
typedef uint32_t yyjson_write_flag;
/** Default option:
- Write JSON minify.
- Report error on inf or nan number.
- Do not validate string encoding.
- Do not escape unicode or slash. */
static const yyjson_write_flag YYJSON_WRITE_NOFLAG = 0 << 0;
/** Write JSON pretty with 4 space indent. */
static const yyjson_write_flag YYJSON_WRITE_PRETTY = 1 << 0;
/** Escape unicode as `uXXXX`, make the output ASCII only. */
static const yyjson_write_flag YYJSON_WRITE_ESCAPE_UNICODE = 1 << 1;
/** Escape '/' as '\/'. */
static const yyjson_write_flag YYJSON_WRITE_ESCAPE_SLASHES = 1 << 2;
/** Write inf and nan number as 'Infinity' and 'NaN' literal (non-standard). */
static const yyjson_write_flag YYJSON_WRITE_ALLOW_INF_AND_NAN = 1 << 3;
/** Write inf and nan number as null literal.
This flag will override `YYJSON_WRITE_ALLOW_INF_AND_NAN` flag. */
static const yyjson_write_flag YYJSON_WRITE_INF_AND_NAN_AS_NULL = 1 << 4;
/** Result code for JSON writer */
typedef uint32_t yyjson_write_code;
/** Success, no error. */
static const yyjson_write_code YYJSON_WRITE_SUCCESS = 0;
/** Invalid parameter, such as NULL document. */
static const yyjson_write_code YYJSON_WRITE_ERROR_INVALID_PARAMETER = 1;
/** Memory allocation failure occurs. */
static const yyjson_write_code YYJSON_WRITE_ERROR_MEMORY_ALLOCATION = 2;
/** Invalid value type in JSON document. */
static const yyjson_write_code YYJSON_WRITE_ERROR_INVALID_VALUE_TYPE = 3;
/** NaN or Infinity number occurs. */
static const yyjson_write_code YYJSON_WRITE_ERROR_NAN_OR_INF = 4;
/** Failed to open a file. */
static const yyjson_write_code YYJSON_WRITE_ERROR_FILE_OPEN = 5;
/** Failed to write a file. */
static const yyjson_write_code YYJSON_WRITE_ERROR_FILE_WRITE = 6;
/** Error information for JSON writer. */
typedef struct yyjson_write_err {
/** Error code, see yyjson_write_code for all available values. */
yyjson_write_code code;
/** Short error message (NULL for success). */
const char *msg;
} yyjson_write_err;
/**
Write JSON with options.
This function is thread-safe if you make sure that:
1. The `alc` is thread-safe or NULL.
@param doc The JSON document.
If you pass NULL, you will get NULL result.
@param flg The JSON write options.
You can combine multiple options using bitwise `|` operator.
@param alc The memory allocator used by JSON writer.
Pass NULL to use the libc's default allocator (thread-safe).
@param len A pointer to receive output length in bytes.
Pass NULL if you don't need length information.
@param err A pointer to receive error information.
Pass NULL if you don't need error information.
@return A new JSON string, or NULL if error occurs.
This string is encoded as UTF-8 with a null-terminator.
You should use free() or alc->free() to release it
when it's no longer needed.
*/
yyjson_api char *yyjson_write_opts(const yyjson_doc *doc,
yyjson_write_flag flg,
const yyjson_alc *alc,
size_t *len,
yyjson_write_err *err);
/**
Write JSON file with options.
This function is thread-safe if you make sure that:
1. The file is not accessed by other threads.
2. The `alc` is thread-safe or NULL.
@param path The JSON file's path.
If you pass an invalid path, you will get an error.
If the file is not empty, the content will be discarded.
@param doc The JSON document.
If you pass NULL or empty document, you will get an error.
@param flg The JSON write options.
You can combine multiple options using bitwise `|` operator.
@param alc The memory allocator used by JSON writer.
Pass NULL to use the libc's default allocator (thread-safe).
@param err A pointer to receive error information.
Pass NULL if you don't need error information.
@return true for success, false for error.
*/
yyjson_api bool yyjson_write_file(const char *path,
const yyjson_doc *doc,
yyjson_write_flag flg,
const yyjson_alc *alc,
yyjson_write_err *err);
/**
Write JSON.
This function is thread-safe.
@param doc The JSON document.
If you pass NULL, you will get NULL result.
@param flg The JSON write options.
You can combine multiple options using bitwise `|` operator.
@param len A pointer to receive output length in bytes.
Pass NULL if you don't need length information.
@return A new JSON string, or NULL if error occurs.
This string is encoded as UTF-8 with a null-terminator.
You should use free() to release it when it's no longer needed.
*/
yyjson_api_inline char *yyjson_write(const yyjson_doc *doc,
yyjson_write_flag flg,
size_t *len) {
return yyjson_write_opts(doc, flg, NULL, len, NULL);
}
/**
Write JSON with options.
This function is thread-safe if you make sure that:
1. The `doc` is not modified by other threads.
2. The `alc` is thread-safe or NULL.
@param doc The mutable JSON document.
If you pass NULL or empty document, you will get NULL result.
@param flg The JSON write options.
You can combine multiple options using bitwise `|` operator.
@param alc The memory allocator used by JSON writer.
Pass NULL to use the libc's default allocator (thread-safe).
@param len A pointer to receive output length in bytes.
Pass NULL if you don't need length information.
@param err A pointer to receive error information.
Pass NULL if you don't need error information.
@return A new JSON string, or NULL if error occurs.
This string is encoded as UTF-8 with a null-terminator.
You should use free() or alc->free() to release it
when it's no longer needed.
*/
yyjson_api char *yyjson_mut_write_opts(const yyjson_mut_doc *doc,
yyjson_write_flag flg,
const yyjson_alc *alc,
size_t *len,
yyjson_write_err *err);
/**
Write JSON file with options.
This function is thread-safe if you make sure that:
1. The file is not accessed by other threads.
2. The `doc` is not modified by other threads.
3. The `alc` is thread-safe or NULL.
@param path The JSON file's path.
If you pass an invalid path, you will get an error.
If the file is not empty, the content will be discarded.
@param doc The mutable JSON document.
If you pass NULL or empty document, you will get an error.
@param flg The JSON write options.
You can combine multiple options using bitwise `|` operator.
@param alc The memory allocator used by JSON writer.
Pass NULL to use the libc's default allocator (thread-safe).
@param err A pointer to receive error information.
Pass NULL if you don't need error information.
@return true for success, false for error.
*/
yyjson_api bool yyjson_mut_write_file(const char *path,
const yyjson_mut_doc *doc,
yyjson_write_flag flg,
const yyjson_alc *alc,
yyjson_write_err *err);
/**
Write JSON.
This function is thread-safe if you make sure that:
1. The `doc` is not is not modified by other threads.
@param doc The JSON document.
If you pass NULL, you will get NULL result.
@param flg The JSON write options.
You can combine multiple options using bitwise `|` operator.
@param len A pointer to receive output length in bytes.
Pass NULL if you don't need length information.
@return A new JSON string, or NULL if error occurs.
This string is encoded as UTF-8 with a null-terminator.
You should use free() or alc->free() to release it
when it's no longer needed.
*/
yyjson_api_inline char *yyjson_mut_write(const yyjson_mut_doc *doc,
yyjson_write_flag flg,
size_t *len) {
return yyjson_mut_write_opts(doc, flg, NULL, len, NULL);
}
/*==============================================================================
* JSON Document API
*============================================================================*/
/** Returns the root value of this JSON document. */
yyjson_api_inline yyjson_val *yyjson_doc_get_root(yyjson_doc *doc);
/** Returns read size of input JSON data. */
yyjson_api_inline size_t yyjson_doc_get_read_size(yyjson_doc *doc);
/** Returns total value count in this JSON document. */
yyjson_api_inline size_t yyjson_doc_get_val_count(yyjson_doc *doc);
/** Release the JSON document and free the memory. */
yyjson_api_inline void yyjson_doc_free(yyjson_doc *doc);
/*==============================================================================
* JSON Value Type API
*============================================================================*/
/** Returns whether the JSON value is null. */
yyjson_api_inline bool yyjson_is_null(yyjson_val *val);
/** Returns whether the JSON value is true. */
yyjson_api_inline bool yyjson_is_true(yyjson_val *val);
/** Returns whether the JSON value is false. */
yyjson_api_inline bool yyjson_is_false(yyjson_val *val);
/** Returns whether the JSON value is bool (true/false). */
yyjson_api_inline bool yyjson_is_bool(yyjson_val *val);
/** Returns whether the JSON value is unsigned integer (uint64_t). */
yyjson_api_inline bool yyjson_is_uint(yyjson_val *val);
/** Returns whether the JSON value is signed integer (int64_t). */
yyjson_api_inline bool yyjson_is_sint(yyjson_val *val);
/** Returns whether the JSON value is integer (uint64_t/int64_t). */
yyjson_api_inline bool yyjson_is_int(yyjson_val *val);
/** Returns whether the JSON value is real number (double). */
yyjson_api_inline bool yyjson_is_real(yyjson_val *val);
/** Returns whether the JSON value is number (uint64_t/int64_t/double). */
yyjson_api_inline bool yyjson_is_num(yyjson_val *val);
/** Returns whether the JSON value is string. */
yyjson_api_inline bool yyjson_is_str(yyjson_val *val);
/** Returns whether the JSON value is array. */
yyjson_api_inline bool yyjson_is_arr(yyjson_val *val);
/** Returns whether the JSON value is object. */
yyjson_api_inline bool yyjson_is_obj(yyjson_val *val);
/** Returns whether the JSON value is container (array/object). */
yyjson_api_inline bool yyjson_is_ctn(yyjson_val *val);
/*==============================================================================
* JSON Value Content API
*============================================================================*/
/** Returns the JSON value's type. */
yyjson_api_inline yyjson_type yyjson_get_type(yyjson_val *val);
/** Returns the JSON value's subtype. */
yyjson_api_inline yyjson_subtype yyjson_get_subtype(yyjson_val *val);
/** Returns the JSON value's tag. */
yyjson_api_inline uint8_t yyjson_get_tag(yyjson_val *val);
/** Returns the JSON value's type description.
The return description should be one of these strings: "null", "string",
"array", "object", "true", "false", "uint", "sint", "real", "unknown". */
yyjson_api_inline const char *yyjson_get_type_desc(yyjson_val *val);
/** Returns the content if the value is bool, or false on error. */
yyjson_api_inline bool yyjson_get_bool(yyjson_val *val);
/** Returns the content if the value is integer, or 0 on error. */
yyjson_api_inline uint64_t yyjson_get_uint(yyjson_val *val);
/** Returns the content if the value is integer, or 0 on error. */
yyjson_api_inline int64_t yyjson_get_sint(yyjson_val *val);
/** Returns the content if the value is integer, or 0 on error. */
yyjson_api_inline int yyjson_get_int(yyjson_val *val);
/** Returns the content if the value is real number, or 0.0 on error. */
yyjson_api_inline double yyjson_get_real(yyjson_val *val);
/** Returns the content if the value is string, or NULL on error. */
yyjson_api_inline const char *yyjson_get_str(yyjson_val *val);
/** Returns the content length if the value is string, or 0 on error. */
yyjson_api_inline size_t yyjson_get_len(yyjson_val *val);
/** Returns whether the JSON value is equals to a string. */
yyjson_api_inline bool yyjson_equals_str(yyjson_val *val, const char *str);
/** Returns whether the JSON value is equals to a string. */
yyjson_api_inline bool yyjson_equals_strn(yyjson_val *val, const char *str,
size_t len);
/*==============================================================================
* JSON Array API
*============================================================================*/
/** Returns the number of elements in this array, or 0 on error. */
yyjson_api_inline size_t yyjson_arr_size(yyjson_val *arr);
/** Returns the element at the specified position in this array,
or NULL if array is empty or the index is out of bounds.
@warning This function takes a linear search time if array is not flat. */
yyjson_api_inline yyjson_val *yyjson_arr_get(yyjson_val *arr, size_t idx);
/** Returns the first element of this array, or NULL if array is empty. */
yyjson_api_inline yyjson_val *yyjson_arr_get_first(yyjson_val *arr);
/** Returns the last element of this array, or NULL if array is empty.
@warning This function takes a linear search time if array is not flat. */
yyjson_api_inline yyjson_val *yyjson_arr_get_last(yyjson_val *arr);
/*==============================================================================
* JSON Array Iterator API
*============================================================================*/
/**
A JSON array iterator.
Sample code:
yyjson_val *val;
yyjson_arr_iter iter;
yyjson_arr_iter_init(arr, &iter);
while ((val = yyjson_arr_iter_next(&iter))) {
print(val);
}
*/
typedef struct yyjson_arr_iter yyjson_arr_iter;
/** Initialize an iterator for this array. */
yyjson_api_inline bool yyjson_arr_iter_init(yyjson_val *arr,
yyjson_arr_iter *iter);
/** Returns whether the iteration has more elements. */
yyjson_api_inline bool yyjson_arr_iter_has_next(yyjson_arr_iter *iter);
/** Returns the next element in the iteration, or NULL on end. */
yyjson_api_inline yyjson_val *yyjson_arr_iter_next(yyjson_arr_iter *iter);
/**
Macro for iterating over an array.
Sample code:
size_t idx, max;
yyjson_val *val;
yyjson_arr_foreach(arr, idx, max, val) {
print(idx, val);
}
*/
#define yyjson_arr_foreach(arr, idx, max, val) \
for ((idx) = 0, \
(max) = yyjson_arr_size(arr), \
(val) = yyjson_arr_get_first(arr); \
(idx) < (max); \
(idx)++, \
(val) = unsafe_yyjson_get_next(val))
/*==============================================================================
* JSON Object API
*============================================================================*/
/** Returns the number of key-value pairs in this object, or 0 on error. */
yyjson_api_inline size_t yyjson_obj_size(yyjson_val *obj);
/** Returns the value to which the specified key is mapped,
or NULL if this object contains no mapping for the key.
@warning This function takes a linear search time. */
yyjson_api_inline yyjson_val *yyjson_obj_get(yyjson_val *obj, const char *key);
/** Returns the value to which the specified key is mapped,
or NULL if this object contains no mapping for the key.
@warning This function takes a linear search time. */
yyjson_api_inline yyjson_val *yyjson_obj_getn(yyjson_val *obj, const char *key,
size_t key_len);
/*==============================================================================
* JSON Object Iterator API
*============================================================================*/
/**
A JSON object iterator.
Sample code:
yyjson_val *key, *val;
yyjson_obj_iter iter;
yyjson_obj_iter_init(obj, &iter);
while ((key = yyjson_obj_iter_next(&iter))) {
val = yyjson_obj_iter_get_val(key);
print(key, val);
}
*/
typedef struct yyjson_obj_iter yyjson_obj_iter;
/** Initialize an object iterator. */
yyjson_api_inline bool yyjson_obj_iter_init(yyjson_val *obj,
yyjson_obj_iter *iter);
/** Returns whether the iteration has more elements. */
yyjson_api_inline bool yyjson_obj_iter_has_next(yyjson_obj_iter *iter);
/** Returns the next key in the iteration, or NULL on end. */
yyjson_api_inline yyjson_val *yyjson_obj_iter_next(yyjson_obj_iter *iter);
/** Returns the value for key inside the iteration. */
yyjson_api_inline yyjson_val *yyjson_obj_iter_get_val(yyjson_val *key);
/**
Iterates to a specified key and returns the value.
If the key exists in the object, then the iterator will stop at the next key,
otherwise the iterator will not change and NULL is returned.
@warning This function takes a linear search time if the key is not nearby.
*/
yyjson_api_inline yyjson_val *yyjson_obj_iter_get(yyjson_obj_iter *iter,
const char *key);
/**
Iterates to a specified key and returns the value.
If the key exists in the object, then the iterator will stop at the next key,
otherwise the iterator will not change and NULL is returned.
@warning This function takes a linear search time if the key is not nearby.
*/
yyjson_api_inline yyjson_val *yyjson_obj_iter_getn(yyjson_obj_iter *iter,
const char *key,
size_t key_len);
/**
Macro for iterating over an object.
Sample code:
size_t idx, max;
yyjson_val *key, *val;
yyjson_obj_foreach(obj, idx, max, key, val) {
print(key, val);
}
*/
#define yyjson_obj_foreach(obj, idx, max, key, val) \
for ((idx) = 0, \
(max) = yyjson_obj_size(obj), \
(key) = (obj) ? unsafe_yyjson_get_first(obj) : NULL, \
(val) = (key) + 1; \
(idx) < (max); \
(idx)++, \
(key) = unsafe_yyjson_get_next(val), \
(val) = (key) + 1)
/*==============================================================================
* Mutable JSON Document API
*============================================================================*/
/** Returns the root value of this JSON document. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_get_root(yyjson_mut_doc *doc);
/** Sets the root value of this JSON document. */
yyjson_api_inline void yyjson_mut_doc_set_root(yyjson_mut_doc *doc,
yyjson_mut_val *root);
/** Delete the JSON document and free the memory. */
yyjson_api void yyjson_mut_doc_free(yyjson_mut_doc *doc);
/** Creates and returns a new mutable JSON document, returns NULL on error.
If allocator is NULL, the default allocator will be used. */
yyjson_api yyjson_mut_doc *yyjson_mut_doc_new(const yyjson_alc *alc);
/** Copies and returns a new mutable document from input, returns NULL on error.
This makes a `deep-copy` on the immutable document.
If allocator is NULL, the default allocator will be used. */
yyjson_api yyjson_mut_doc *yyjson_doc_mut_copy(yyjson_doc *doc,
const yyjson_alc *alc);
/** Copies and returns a new mutable document from input, returns NULL on error.
This makes a `deep-copy` on the mutable document.
If allocator is NULL, the default allocator will be used. */
yyjson_api yyjson_mut_doc *yyjson_mut_doc_mut_copy(yyjson_mut_doc *doc,
const yyjson_alc *alc);
/** Copies and returns a new mutable value from input, returns NULL on error.
This makes a `deep-copy` on the immutable value.
The memory was managed by mutable document. */
yyjson_api yyjson_mut_val *yyjson_val_mut_copy(yyjson_mut_doc *doc,
yyjson_val *val);
/** Copies and return a new mutable value from input, returns NULL on error,
This makes a `deep-copy` on the mutable value.
The memory was managed by mutable document.
@warning This function is recursive and may cause a stack overflow
if the object level is too deep. */
yyjson_api yyjson_mut_val *yyjson_mut_val_mut_copy(yyjson_mut_doc *doc,
yyjson_mut_val *val);
/*==============================================================================
* Mutable JSON Value Type API
*============================================================================*/
/** Returns whether the JSON value is null. */
yyjson_api_inline bool yyjson_mut_is_null(yyjson_mut_val *val);
/** Returns whether the JSON value is true. */
yyjson_api_inline bool yyjson_mut_is_true(yyjson_mut_val *val);
/** Returns whether the JSON value is false. */
yyjson_api_inline bool yyjson_mut_is_false(yyjson_mut_val *val);
/** Returns whether the JSON value is bool (true/false). */
yyjson_api_inline bool yyjson_mut_is_bool(yyjson_mut_val *val);
/** Returns whether the JSON value is unsigned integer (uint64_t). */
yyjson_api_inline bool yyjson_mut_is_uint(yyjson_mut_val *val);
/** Returns whether the JSON value is signed integer (int64_t). */
yyjson_api_inline bool yyjson_mut_is_sint(yyjson_mut_val *val);
/** Returns whether the JSON value is integer (uint64_t/int64_t). */
yyjson_api_inline bool yyjson_mut_is_int(yyjson_mut_val *val);
/** Returns whether the JSON value is real number (double). */
yyjson_api_inline bool yyjson_mut_is_real(yyjson_mut_val *val);
/** Returns whether the JSON value is number (uint/sint/real). */
yyjson_api_inline bool yyjson_mut_is_num(yyjson_mut_val *val);
/** Returns whether the JSON value is string. */
yyjson_api_inline bool yyjson_mut_is_str(yyjson_mut_val *val);
/** Returns whether the JSON value is array. */
yyjson_api_inline bool yyjson_mut_is_arr(yyjson_mut_val *val);
/** Returns whether the JSON value is object. */
yyjson_api_inline bool yyjson_mut_is_obj(yyjson_mut_val *val);
/** Returns whether the JSON value is container (array/object). */
yyjson_api_inline bool yyjson_mut_is_ctn(yyjson_mut_val *val);
/*==============================================================================
* Mutable JSON Value Content API
*============================================================================*/
/** Returns the JSON value's type. */
yyjson_api_inline yyjson_type yyjson_mut_get_type(yyjson_mut_val *val);
/** Returns the JSON value's subtype. */
yyjson_api_inline yyjson_subtype yyjson_mut_get_subtype(yyjson_mut_val *val);
/** Returns the JSON value's tag. */
yyjson_api_inline uint8_t yyjson_mut_get_tag(yyjson_mut_val *val);
/** Returns the JSON value's type description.
The return description should be one of these strings: "null", "string",
"array", "object", "true", "false", "uint", "sint", "real", "unknown". */
yyjson_api_inline const char *yyjson_mut_get_type_desc(yyjson_mut_val *val);
/** Returns whether two JSON values are equal (deep compare).
@warning This function takes a quadratic time. */
yyjson_api bool yyjson_mut_equals(yyjson_mut_val *lhs,
yyjson_mut_val *rhs);
/** Returns the content if the value is bool, or false on error. */
yyjson_api_inline bool yyjson_mut_get_bool(yyjson_mut_val *val);
/** Returns the content if the value is integer, or 0 on error. */
yyjson_api_inline uint64_t yyjson_mut_get_uint(yyjson_mut_val *val);
/** Returns the content if the value is integer, or 0 on error. */
yyjson_api_inline int64_t yyjson_mut_get_sint(yyjson_mut_val *val);
/** Returns the content if the value is integer, or 0 on error. */
yyjson_api_inline int yyjson_mut_get_int(yyjson_mut_val *val);
/** Returns the content if the value is real number, or 0.0 on error. */
yyjson_api_inline double yyjson_mut_get_real(yyjson_mut_val *val);
/** Returns the content if the value is string, or NULL on error. */
yyjson_api_inline const char *yyjson_mut_get_str(yyjson_mut_val *val);
/** Returns the content length if the value is string, or 0 on error. */
yyjson_api_inline size_t yyjson_mut_get_len(yyjson_mut_val *val);
/** Returns whether the JSON value is equals to a string. */
yyjson_api_inline bool yyjson_mut_equals_str(yyjson_mut_val *val,
const char *str);
/** Returns whether the JSON value is equals to a string. */
yyjson_api_inline bool yyjson_mut_equals_strn(yyjson_mut_val *val,
const char *str, size_t len);
/*==============================================================================
* Mutable JSON Value Creation API
*============================================================================*/
/** Creates and returns a null value, returns NULL on error. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_null(yyjson_mut_doc *doc);
/** Creates and returns a true value, returns NULL on error. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_true(yyjson_mut_doc *doc);
/** Creates and returns a false value, returns NULL on error. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_false(yyjson_mut_doc *doc);
/** Creates and returns a bool value, returns NULL on error. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_bool(yyjson_mut_doc *doc,
bool val);
/** Creates and returns an unsigned integer value, returns NULL on error. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_uint(yyjson_mut_doc *doc,
uint64_t num);
/** Creates and returns a signed integer value, returns NULL on error. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_sint(yyjson_mut_doc *doc,
int64_t num);
/** Creates and returns a signed integer value, returns NULL on error. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_int(yyjson_mut_doc *doc,
int64_t num);
/** Creates and returns an real number value, returns NULL on error. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_real(yyjson_mut_doc *doc,
double num);
/** Creates and returns a string value, returns NULL on error.
The input value should be a valid UTF-8 encoded string with null-terminator.
@warning The input string is not copied, you should keep this string
unmodified for the lifetime of this document. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_str(yyjson_mut_doc *doc,
const char *str);
/** Creates and returns a string value, returns NULL on error.
The input value should be a valid UTF-8 encoded string.
@warning The input string is not copied, you should keep this string
unmodified for the lifetime of this document. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_strn(yyjson_mut_doc *doc,
const char *str,
size_t len);
/** Creates and returns a string value, returns NULL on error.
The input value should be a valid UTF-8 encoded string with null-terminator.
The input string is copied and held by the document. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_strcpy(yyjson_mut_doc *doc,
const char *str);
/** Creates and returns a string value, returns NULL on error.
The input value should be a valid UTF-8 encoded string.
The input string is copied and held by the document. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_strncpy(yyjson_mut_doc *doc,
const char *str,
size_t len);
/*==============================================================================
* Mutable JSON Array API
*============================================================================*/
/** Returns the number of elements in this array. */
yyjson_api_inline size_t yyjson_mut_arr_size(yyjson_mut_val *arr);
/** Returns the element at the specified position in this array,
or NULL if array is empty or the index is out of bounds.
@warning This function takes a linear search time. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get(yyjson_mut_val *arr,
size_t idx);
/** Returns the first element of this array, or NULL if array is empty. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get_first(yyjson_mut_val *arr);
/** Returns the last element of this array, or NULL if array is empty. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get_last(yyjson_mut_val *arr);
/*==============================================================================
* Mutable JSON Array Iterator API
*============================================================================*/
/**
A mutable JSON array iterator.
Sample code:
yyjson_mut_val *val;
yyjson_mut_arr_iter iter;
yyjson_mut_arr_iter_init(arr, &iter);
while ((val = yyjson_mut_arr_iter_next(&iter))) {
print(val);
if (val_is_unused(val)) {
yyjson_mut_arr_iter_remove(&iter);
}
}
@warning You should not modify the array while enumerating through it,
but you can use yyjson_mut_arr_iter_remove() to remove current value.
*/
typedef struct yyjson_mut_arr_iter yyjson_mut_arr_iter;
/** Initialize an iterator for this array. */
yyjson_api_inline bool yyjson_mut_arr_iter_init(yyjson_mut_val *arr,
yyjson_mut_arr_iter *iter);
/** Returns whether the iteration has more elements. */
yyjson_api_inline bool yyjson_mut_arr_iter_has_next(yyjson_mut_arr_iter *iter);
/** Returns the next element in the iteration, or NULL on end. */
yyjson_api_inline
yyjson_mut_val *yyjson_mut_arr_iter_next(yyjson_mut_arr_iter *iter);
/** Removes and returns current element in the iteration. */
yyjson_api_inline
yyjson_mut_val *yyjson_mut_arr_iter_remove(yyjson_mut_arr_iter *iter);
/**
Macro for iterating over an array.
Sample code:
size_t idx, max;
yyjson_mut_val *val;
yyjson_mut_arr_foreach(arr, idx, max, val) {
print(idx, val);
}
@warning You should not modify the array while enumerating through it.
*/
#define yyjson_mut_arr_foreach(arr, idx, max, val) \
for ((idx) = 0, \
(max) = yyjson_mut_arr_size(arr), \
(val) = yyjson_mut_arr_get_first(arr); \
(idx) < (max); \
(idx)++, \
(val) = (val)->next)
/*==============================================================================
* Mutable JSON Array Creation API
*============================================================================*/
/** Creates and returns a mutable array, returns NULL on error. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr(yyjson_mut_doc *doc);
/** Creates and returns a mutable array with bool. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_bool(
yyjson_mut_doc *doc, const bool *vals, size_t count);
/** Creates and returns a mutable array with sint numbers. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint(
yyjson_mut_doc *doc, const int64_t *vals, size_t count);
/** Creates and returns a mutable array with uint numbers. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint(
yyjson_mut_doc *doc, const uint64_t *vals, size_t count);
/** Creates and returns a mutable array with real numbers. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_real(
yyjson_mut_doc *doc, const double *vals, size_t count);
/** Creates and returns a mutable array with int8 numbers. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint8(
yyjson_mut_doc *doc, const int8_t *vals, size_t count);
/** Creates and returns a mutable array with int16 numbers. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint16(
yyjson_mut_doc *doc, const int16_t *vals, size_t count);
/** Creates and returns a mutable array with int32 numbers. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint32(
yyjson_mut_doc *doc, const int32_t *vals, size_t count);
/** Creates and returns a mutable array with int64 numbers. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint64(
yyjson_mut_doc *doc, const int64_t *vals, size_t count);
/** Creates and returns a mutable array with uint8 numbers. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint8(
yyjson_mut_doc *doc, const uint8_t *vals, size_t count);
/** Creates and returns a mutable array with uint16 numbers. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint16(
yyjson_mut_doc *doc, const uint16_t *vals, size_t count);
/** Creates and returns a mutable array with uint32 numbers. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint32(
yyjson_mut_doc *doc, const uint32_t *vals, size_t count);
/** Creates and returns a mutable array with uint64 numbers. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint64(
yyjson_mut_doc *doc, const uint64_t *vals, size_t count);
/** Creates and returns a mutable array with float numbers. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_float(
yyjson_mut_doc *doc, const float *vals, size_t count);
/** Creates and returns a mutable array with double numbers. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_double(
yyjson_mut_doc *doc, const double *vals, size_t count);
/** Creates and returns a mutable array with strings (no copy).
The strings should be encoded as UTF-8 with null-terminator. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_str(
yyjson_mut_doc *doc, const char **vals, size_t count);
/** Creates and returns a mutable array with strings (no copy).
The strings should be encoded as UTF-8. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strn(
yyjson_mut_doc *doc, const char **vals, const size_t *lens, size_t count);
/** Creates and returns a mutable array with strings (copied).
The strings should be encoded as UTF-8 with null-terminator. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strcpy(
yyjson_mut_doc *doc, const char **vals, size_t count);
/** Creates and returns a mutable array with strings (copied).
The strings should be encoded as UTF-8. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strncpy(
yyjson_mut_doc *doc, const char **vals, const size_t *lens, size_t count);
/*==============================================================================
* Mutable JSON Array Modification API
*============================================================================*/
/** Inserts a value into an array at a given index, returns false on error.
@warning This function takes a linear search time. */
yyjson_api_inline bool yyjson_mut_arr_insert(yyjson_mut_val *arr,
yyjson_mut_val *val, size_t idx);
/** Inserts a val at the end of the array, returns false on error. */
yyjson_api_inline bool yyjson_mut_arr_append(yyjson_mut_val *arr,
yyjson_mut_val *val);
/** Inserts a val at the head of the array, returns false on error. */
yyjson_api_inline bool yyjson_mut_arr_prepend(yyjson_mut_val *arr,
yyjson_mut_val *val);
/** Replaces a value at index and returns old value, returns NULL on error.
@warning This function takes a linear search time. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_replace(yyjson_mut_val *arr,
size_t idx,
yyjson_mut_val *val);
/** Removes and returns a value at index, returns NULL on error.
@warning This function takes a linear search time. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_remove(yyjson_mut_val *arr,
size_t idx);
/** Removes and returns the first value in this array, returns NULL on error. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_remove_first(
yyjson_mut_val *arr);
/** Removes and returns the last value in this array, returns NULL on error. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_remove_last(
yyjson_mut_val *arr);
/** Removes all values within a specified range in the array.
@warning This function takes a linear search time. */
yyjson_api_inline bool yyjson_mut_arr_remove_range(yyjson_mut_val *arr,
size_t idx, size_t len);
/** Removes all values in this array. */
yyjson_api_inline bool yyjson_mut_arr_clear(yyjson_mut_val *arr);
/** Rotates values in this array for the given number of times.
@warning This function takes a linear search time. */
yyjson_api_inline bool yyjson_mut_arr_rotate(yyjson_mut_val *arr,
size_t idx);
/*==============================================================================
* Mutable JSON Array Modification Convenience API
*============================================================================*/
/** Adds a value at the end of the array. */
yyjson_api_inline bool yyjson_mut_arr_add_val(yyjson_mut_val *arr,
yyjson_mut_val *val);
/** Adds a null val at the end of the array. */
yyjson_api_inline bool yyjson_mut_arr_add_null(yyjson_mut_doc *doc,
yyjson_mut_val *arr);
/** Adds a true val at the end of the array. */
yyjson_api_inline bool yyjson_mut_arr_add_true(yyjson_mut_doc *doc,
yyjson_mut_val *arr);
/** Adds a false val at the end of the array. */
yyjson_api_inline bool yyjson_mut_arr_add_false(yyjson_mut_doc *doc,
yyjson_mut_val *arr);
/** Adds a bool val at the end of the array. */
yyjson_api_inline bool yyjson_mut_arr_add_bool(yyjson_mut_doc *doc,
yyjson_mut_val *arr,
bool val);
/** Adds a uint val at the end of the array. */
yyjson_api_inline bool yyjson_mut_arr_add_uint(yyjson_mut_doc *doc,
yyjson_mut_val *arr,
uint64_t num);
/** Adds a sint val at the end of the array. */
yyjson_api_inline bool yyjson_mut_arr_add_sint(yyjson_mut_doc *doc,
yyjson_mut_val *arr,
int64_t num);
/** Adds an int val at the end of the array. */
yyjson_api_inline bool yyjson_mut_arr_add_int(yyjson_mut_doc *doc,
yyjson_mut_val *arr,
int64_t num);
/** Adds a double val at the end of the array. */
yyjson_api_inline bool yyjson_mut_arr_add_real(yyjson_mut_doc *doc,
yyjson_mut_val *arr,
double num);
/** Adds a string val at the end of the array (no copy).
The string should be encoded as UTF-8 with null-terminator. */
yyjson_api_inline bool yyjson_mut_arr_add_str(yyjson_mut_doc *doc,
yyjson_mut_val *arr,
const char *str);
/** Adds a string val at the end of the array (no copy).
The strings should be encoded as UTF-8. */
yyjson_api_inline bool yyjson_mut_arr_add_strn(yyjson_mut_doc *doc,
yyjson_mut_val *arr,
const char *str,
size_t len);
/** Adds a string val at the end of the array (copied).
The strings should be encoded as UTF-8 with null-terminator. */
yyjson_api_inline bool yyjson_mut_arr_add_strcpy(yyjson_mut_doc *doc,
yyjson_mut_val *arr,
const char *str);
/** Adds a string val at the end of the array (copied).
The strings should be encoded as UTF-8. */
yyjson_api_inline bool yyjson_mut_arr_add_strncpy(yyjson_mut_doc *doc,
yyjson_mut_val *arr,
const char *str,
size_t len);
/** Creates and adds a new array at the end of the array.
Returns the new array, or NULL on error. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_add_arr(yyjson_mut_doc *doc,
yyjson_mut_val *arr);
/** Creates and adds a new object at the end of the array.
Returns the new object, or NULL on error. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_add_obj(yyjson_mut_doc *doc,
yyjson_mut_val *arr);
/*==============================================================================
* Mutable JSON Object API
*============================================================================*/
/** Returns the number of key-value pair in this object. */
yyjson_api_inline size_t yyjson_mut_obj_size(yyjson_mut_val *obj);
/** Returns the value to which the specified key is mapped,
or NULL if this object contains no mapping for the key.
@warning This function takes a linear search time. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_get(yyjson_mut_val *obj,
const char *key_str);
/** Returns the value to which the specified key is mapped,
or NULL if this object contains no mapping for the key.
@warning This function takes a linear search time. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_getn(yyjson_mut_val *obj,
const char *key_str,
size_t key_len);
/*==============================================================================
* Mutable JSON Object Iterator API
*============================================================================*/
/**
A mutable JSON object iterator.
Sample code:
yyjson_mut_val *key, *val;
yyjson_mut_obj_iter iter;
yyjson_mut_obj_iter_init(obj, &iter);
while ((key = yyjson_mut_obj_iter_next(&iter))) {
val = yyjson_mut_obj_iter_get_val(key);
print(key, val);
if (key_is_unused(key)) {
yyjson_mut_obj_iter_remove(&iter);
}
}
@warning You should not modify the object while enumerating through it,
but you can use yyjson_mut_obj_iter_remove() to remove current value.
*/
typedef struct yyjson_mut_obj_iter yyjson_mut_obj_iter;
/** Initialize an object iterator. */
yyjson_api_inline bool yyjson_mut_obj_iter_init(yyjson_mut_val *obj,
yyjson_mut_obj_iter *iter);
/** Returns whether the iteration has more elements. */
yyjson_api_inline bool yyjson_mut_obj_iter_has_next(yyjson_mut_obj_iter *iter);
/** Returns the next key in the iteration, or NULL on end. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_next(
yyjson_mut_obj_iter *iter);
/** Returns the value for key inside the iteration. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_get_val(
yyjson_mut_val *key);
/** Removes and returns current key in the iteration, the value can be
accessed by key->next. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_remove(
yyjson_mut_obj_iter *iter);
/**
Iterates to a specified key and returns the value.
If the key exists in the object, then the iterator will stop at the next key,
otherwise the iterator will not change and NULL is returned.
@warning This function takes a linear search time if the key is not nearby.
*/
yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_get(
yyjson_mut_obj_iter *iter,
const char *key);
/**
Iterates to a specified key and returns the value.
If the key exists in the object, then the iterator will stop at the next key,
otherwise the iterator will not change and NULL is returned.
@warning This function takes a linear search time if the key is not nearby.
*/
yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_getn(
yyjson_mut_obj_iter *iter,
const char *key,
size_t key_len);
/**
Macro for iterating over an object.
Sample code:
size_t idx, max;
yyjson_val *key, *val;
yyjson_obj_foreach(obj, idx, max, key, val) {
print(key, val);
}
@warning You should not modify the object while enumerating through it.
*/
#define yyjson_mut_obj_foreach(obj, idx, max, key, val) \
for ((idx) = 0, \
(max) = yyjson_mut_obj_size(obj), \
(key) = (max) ? ((yyjson_mut_val *)(obj)->uni.ptr)->next->next : NULL, \
(val) = (key) ? (key)->next : NULL; \
(idx) < (max); \
(idx)++, \
(key) = (val)->next, \
(val) = (key)->next)
/*==============================================================================
* Mutable JSON Object Creation API
*============================================================================*/
/** Creates and returns a mutable object, returns NULL on error. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_obj(yyjson_mut_doc *doc);
/** Creates and returns a mutable object with keys and values,
returns NULL on error. The keys and values are not copied.
The strings should be encoded as UTF-8 with null-terminator. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_with_str(yyjson_mut_doc *doc,
const char **keys,
const char **vals,
size_t count);
/** Creates and returns a mutable object with key-value pairs and pair count,
returns NULL on error. The keys and values are not copied.
The strings should be encoded as UTF-8 with null-terminator. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_with_kv(yyjson_mut_doc *doc,
const char **kv_pairs,
size_t pair_count);
/*==============================================================================
* Mutable JSON Object Modification API
*============================================================================*/
/** Adds a key-value pair at the end of the object. The key must be a string.
This function allows duplicated key in one object. */
yyjson_api_inline bool yyjson_mut_obj_add(yyjson_mut_val *obj,
yyjson_mut_val *key,
yyjson_mut_val *val);
/** Adds a key-value pair to the object. The key must be a string.
This function may remove all key-value pairs for the given key before add.
@warning This function takes a linear search time. */
yyjson_api_inline bool yyjson_mut_obj_put(yyjson_mut_val *obj,
yyjson_mut_val *key,
yyjson_mut_val *val);
/** Inserts a key-value pair to the object at the given position.
The key must be a string. This function allows duplicated key in one object.
@warning This function takes a linear search time. */
yyjson_api_inline bool yyjson_mut_obj_insert(yyjson_mut_val *obj,
yyjson_mut_val *key,
yyjson_mut_val *val,
size_t idx);
/** Removes all key-value pair from the object with given key,
and return the first match one.
@warning This function takes a linear search time. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove(yyjson_mut_val *obj,
yyjson_mut_val *key);
/** Removes all key-value pairs in this object. */
yyjson_api_inline bool yyjson_mut_obj_clear(yyjson_mut_val *obj);
/** Replaces value from the object with given key.
@warning This function takes a linear search time. */
yyjson_api_inline bool yyjson_mut_obj_replace(yyjson_mut_val *obj,
yyjson_mut_val *key,
yyjson_mut_val *val);
/** Rotates key-value pairs in the object for the given number of times.
@warning This function takes a linear search time. */
yyjson_api_inline bool yyjson_mut_obj_rotate(yyjson_mut_val *obj,
size_t idx);
/*==============================================================================
* Mutable JSON Object Modification Convenience API
*============================================================================*/
/** Adds a null value at the end of the object. The key is not copied.
This function allows duplicated key in one object. */
yyjson_api_inline bool yyjson_mut_obj_add_null(yyjson_mut_doc *doc,
yyjson_mut_val *obj,
const char *key);
/** Adds a true value at the end of the object. The key is not copied.
This function allows duplicated key in one object. */
yyjson_api_inline bool yyjson_mut_obj_add_true(yyjson_mut_doc *doc,
yyjson_mut_val *obj,
const char *key);
/** Adds a false value at the end of the object. The key is not copied.
This function allows duplicated key in one object. */
yyjson_api_inline bool yyjson_mut_obj_add_false(yyjson_mut_doc *doc,
yyjson_mut_val *obj,
const char *key);
/** Adds a bool value at the end of the object. The key is not copied.
This function allows duplicated key in one object. */
yyjson_api_inline bool yyjson_mut_obj_add_bool(yyjson_mut_doc *doc,
yyjson_mut_val *obj,
const char *key, bool val);
/** Adds a uint value at the end of the object. The key is not copied.
This function allows duplicated key in one object. */
yyjson_api_inline bool yyjson_mut_obj_add_uint(yyjson_mut_doc *doc,
yyjson_mut_val *obj,
const char *key, uint64_t val);
/** Adds a sint value at the end of the object. The key is not copied.
This function allows duplicated key in one object. */
yyjson_api_inline bool yyjson_mut_obj_add_sint(yyjson_mut_doc *doc,
yyjson_mut_val *obj,
const char *key, int64_t val);
/** Adds an int value at the end of the object. The key is not copied.
This function allows duplicated key in one object. */
yyjson_api_inline bool yyjson_mut_obj_add_int(yyjson_mut_doc *doc,
yyjson_mut_val *obj,
const char *key, int64_t val);
/** Adds a double value at the end of the object. The key is not copied.
This function allows duplicated key in one object. */
yyjson_api_inline bool yyjson_mut_obj_add_real(yyjson_mut_doc *doc,
yyjson_mut_val *obj,
const char *key, double val);
/** Adds a string value at the end of the object. The key/value is not copied.
This function allows duplicated key in one object. */
yyjson_api_inline bool yyjson_mut_obj_add_str(yyjson_mut_doc *doc,
yyjson_mut_val *obj,
const char *key, const char *val);
/** Adds a string value at the end of the object.
The key and value are not copied.
This function allows duplicated key in one object. */
yyjson_api_inline bool yyjson_mut_obj_add_strn(yyjson_mut_doc *doc,
yyjson_mut_val *obj,
const char *key,
const char *val, size_t len);
/** Adds a string value at the end of the object.
The key is not copied, but the value is copied.
This function allows duplicated key in one object. */
yyjson_api_inline bool yyjson_mut_obj_add_strcpy(yyjson_mut_doc *doc,
yyjson_mut_val *obj,
const char *key,
const char *val);
/** Adds a string value at the end of the object.
The key is not copied, but the value is copied.
This function allows duplicated key in one object. */
yyjson_api_inline bool yyjson_mut_obj_add_strncpy(yyjson_mut_doc *doc,
yyjson_mut_val *obj,
const char *key,
const char *val, size_t len);
/** Removes all key-value pairs for the given key,
and return the first match one.
@warning This function takes a linear search time. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_str(yyjson_mut_val *obj,
const char *key);
/** Removes all key-value pairs for the given key,
and return the first match one.
@warning This function takes a linear search time. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_strn(
yyjson_mut_val *obj,
const char *key, size_t len);
/*==============================================================================
* JSON Pointer API
*============================================================================*/
/** Get a JSON value with JSON Pointer: https://tools.ietf.org/html/rfc6901
For example: "/users/0/uid".
Returns NULL if there's no matched value. */
yyjson_api_inline yyjson_val *yyjson_get_pointer(yyjson_val *val,
const char *pointer);
/** Get a JSON value with JSON Pointer: https://tools.ietf.org/html/rfc6901
For example: "/users/0/uid".
Returns NULL if there's no matched value. */
yyjson_api_inline yyjson_val *yyjson_doc_get_pointer(yyjson_doc *doc,
const char *pointer);
/** Get a JSON value with JSON Pointer: https://tools.ietf.org/html/rfc6901
For example: "/users/0/uid".
Returns NULL if there's no matched value. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_get_pointer(yyjson_mut_val *val,
const char *pointer);
/** Get a JSON value with JSON Pointer: https://tools.ietf.org/html/rfc6901
For example: "/users/0/uid".
Returns NULL if there's no matched value. */
yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_get_pointer(
yyjson_mut_doc *doc, const char *pointer);
/*==============================================================================
* JSON Merge-Patch API
*============================================================================*/
/** Creates and returns a merge-patched JSON value:
https://tools.ietf.org/html/rfc7386
Returns NULL if the patch could not be applied. */
yyjson_api yyjson_mut_val *yyjson_merge_patch(yyjson_mut_doc *doc,
yyjson_val *orig,
yyjson_val *patch);
/*==============================================================================
* JSON Structure (Implementation)
*============================================================================*/
/* Payload of a JSON value (8 bytes). */
typedef union yyjson_val_uni {
uint64_t u64;
int64_t i64;
double f64;
const char *str;
void *ptr;
size_t ofs;
} yyjson_val_uni;
/* An immutable JSON value (16 bytes). */
struct yyjson_val {
uint64_t tag;
yyjson_val_uni uni;
};
/* An immutable JSON Document. */
struct yyjson_doc {
/* Root value of the document (nonnull). */
yyjson_val *root;
/* Allocator used by document (nonnull). */
yyjson_alc alc;
/* The total number of bytes read when parsing JSON (nonzero). */
size_t dat_read;
/* The total number of value read when parsing JSON (nonzero). */
size_t val_read;
/* The string pool used by JSON values (nullable). */
char *str_pool;
};
/*==============================================================================
* Unsafe JSON Value API (Implementation)
*============================================================================*/
yyjson_api_inline yyjson_type unsafe_yyjson_get_type(void *val) {
uint8_t tag = (uint8_t)((yyjson_val *)val)->tag;
return (yyjson_type)(tag & YYJSON_TYPE_MASK);
}
yyjson_api_inline yyjson_subtype unsafe_yyjson_get_subtype(void *val) {
uint8_t tag = (uint8_t)((yyjson_val *)val)->tag;
return (yyjson_subtype)(tag & YYJSON_SUBTYPE_MASK);
}
yyjson_api_inline uint8_t unsafe_yyjson_get_tag(void *val) {
uint8_t tag = (uint8_t)((yyjson_val *)val)->tag;
return (uint8_t)(tag & YYJSON_TAG_MASK);
}
yyjson_api_inline bool unsafe_yyjson_is_null(void *val) {
return unsafe_yyjson_get_type(val) == YYJSON_TYPE_NULL;
}
yyjson_api_inline bool unsafe_yyjson_is_bool(void *val) {
return unsafe_yyjson_get_type(val) == YYJSON_TYPE_BOOL;
}
yyjson_api_inline bool unsafe_yyjson_is_num(void *val) {
return unsafe_yyjson_get_type(val) == YYJSON_TYPE_NUM;
}
yyjson_api_inline bool unsafe_yyjson_is_str(void *val) {
return unsafe_yyjson_get_type(val) == YYJSON_TYPE_STR;
}
yyjson_api_inline bool unsafe_yyjson_is_arr(void *val) {
return unsafe_yyjson_get_type(val) == YYJSON_TYPE_ARR;
}
yyjson_api_inline bool unsafe_yyjson_is_obj(void *val) {
return unsafe_yyjson_get_type(val) == YYJSON_TYPE_OBJ;
}
yyjson_api_inline bool unsafe_yyjson_is_ctn(void *val) {
uint8_t mask = YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ;
return (unsafe_yyjson_get_tag(val) & mask) == mask;
}
yyjson_api_inline bool unsafe_yyjson_is_uint(void *val) {
const uint8_t patt = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT;
return unsafe_yyjson_get_tag(val) == patt;
}
yyjson_api_inline bool unsafe_yyjson_is_sint(void *val) {
const uint8_t patt = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT;
return unsafe_yyjson_get_tag(val) == patt;
}
yyjson_api_inline bool unsafe_yyjson_is_int(void *val) {
const uint8_t mask = YYJSON_TAG_MASK & (~YYJSON_SUBTYPE_SINT);
const uint8_t patt = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT;
return (unsafe_yyjson_get_tag(val) & mask) == patt;
}
yyjson_api_inline bool unsafe_yyjson_is_real(void *val) {
const uint8_t patt = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL;
return unsafe_yyjson_get_tag(val) == patt;
}
yyjson_api_inline bool unsafe_yyjson_is_true(void *val) {
const uint8_t patt = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_TRUE;
return unsafe_yyjson_get_tag(val) == patt;
}
yyjson_api_inline bool unsafe_yyjson_is_false(void *val) {
const uint8_t patt = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_FALSE;
return unsafe_yyjson_get_tag(val) == patt;
}
yyjson_api_inline bool unsafe_yyjson_arr_is_flat(yyjson_val *val) {
size_t ofs = val->uni.ofs;
size_t len = (size_t)(val->tag >> YYJSON_TAG_BIT);
return len * sizeof(yyjson_val) + sizeof(yyjson_val) == ofs;
}
yyjson_api_inline bool unsafe_yyjson_get_bool(void *val) {
uint8_t tag = unsafe_yyjson_get_tag(val);
return (bool)((tag & YYJSON_SUBTYPE_MASK) >> YYJSON_TYPE_BIT);
}
yyjson_api_inline uint64_t unsafe_yyjson_get_uint(void *val) {
return ((yyjson_val *)val)->uni.u64;
}
yyjson_api_inline int64_t unsafe_yyjson_get_sint(void *val) {
return ((yyjson_val *)val)->uni.i64;
}
yyjson_api_inline int unsafe_yyjson_get_int(void *val) {
return (int)((yyjson_val *)val)->uni.i64;
}
yyjson_api_inline double unsafe_yyjson_get_real(void *val) {
return ((yyjson_val *)val)->uni.f64;
}
yyjson_api_inline const char *unsafe_yyjson_get_str(void *val) {
return ((yyjson_val *)val)->uni.str;
}
yyjson_api_inline size_t unsafe_yyjson_get_len(void *val) {
return (size_t)(((yyjson_val *)val)->tag >> YYJSON_TAG_BIT);
}
yyjson_api_inline void unsafe_yyjson_set_len(void *val, size_t len) {
uint64_t tag = ((yyjson_val *)val)->tag & YYJSON_TAG_MASK;
tag |= (uint64_t)len << YYJSON_TAG_BIT;
((yyjson_val *)val)->tag = tag;
}
yyjson_api_inline yyjson_val *unsafe_yyjson_get_first(yyjson_val *ctn) {
return ctn + 1;
}
yyjson_api_inline yyjson_val *unsafe_yyjson_get_next(yyjson_val *val) {
bool is_ctn = unsafe_yyjson_is_ctn(val);
size_t ctn_ofs = val->uni.ofs;
size_t ofs = (is_ctn ? ctn_ofs : sizeof(yyjson_val));
return (yyjson_val *)(void *)((uint8_t *)val + ofs);
}
yyjson_api_inline bool unsafe_yyjson_equals_strn(void *val, const char *str,
size_t len) {
uint64_t tag = ((uint64_t)len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR;
return ((yyjson_val *)val)->tag == tag &&
memcmp(((yyjson_val *)val)->uni.str, str, len) == 0;
}
yyjson_api_inline bool unsafe_yyjson_equals_str(void *val, const char *str) {
return unsafe_yyjson_equals_strn(val, str, strlen(str));
}
/*==============================================================================
* JSON Document API (Implementation)
*============================================================================*/
yyjson_api_inline yyjson_val *yyjson_doc_get_root(yyjson_doc *doc) {
return doc ? doc->root : NULL;
}
yyjson_api_inline size_t yyjson_doc_get_read_size(yyjson_doc *doc) {
return doc ? doc->dat_read : 0;
}
yyjson_api_inline size_t yyjson_doc_get_val_count(yyjson_doc *doc) {
return doc ? doc->val_read : 0;
}
yyjson_api_inline void yyjson_doc_free(yyjson_doc *doc) {
if (doc) {
yyjson_alc alc = doc->alc;
if (doc->str_pool) alc.free(alc.ctx, doc->str_pool);
alc.free(alc.ctx, doc);
}
}
/*==============================================================================
* JSON Value Type API (Implementation)
*============================================================================*/
yyjson_api_inline bool yyjson_is_null(yyjson_val *val) {
return val ? unsafe_yyjson_is_null(val) : false;
}
yyjson_api_inline bool yyjson_is_true(yyjson_val *val) {
return val ? unsafe_yyjson_is_true(val) : false;
}
yyjson_api_inline bool yyjson_is_false(yyjson_val *val) {
return val ? unsafe_yyjson_is_false(val) : false;
}
yyjson_api_inline bool yyjson_is_bool(yyjson_val *val) {
return val ? unsafe_yyjson_is_bool(val) : false;
}
yyjson_api_inline bool yyjson_is_uint(yyjson_val *val) {
return val ? unsafe_yyjson_is_uint(val) : false;
}
yyjson_api_inline bool yyjson_is_sint(yyjson_val *val) {
return val ? unsafe_yyjson_is_sint(val) : false;
}
yyjson_api_inline bool yyjson_is_int(yyjson_val *val) {
return val ? unsafe_yyjson_is_int(val) : false;
}
yyjson_api_inline bool yyjson_is_real(yyjson_val *val) {
return val ? unsafe_yyjson_is_real(val) : false;
}
yyjson_api_inline bool yyjson_is_num(yyjson_val *val) {
return val ? unsafe_yyjson_is_num(val) : false;
}
yyjson_api_inline bool yyjson_is_str(yyjson_val *val) {
return val ? unsafe_yyjson_is_str(val) : false;
}
yyjson_api_inline bool yyjson_is_arr(yyjson_val *val) {
return val ? unsafe_yyjson_is_arr(val) : false;
}
yyjson_api_inline bool yyjson_is_obj(yyjson_val *val) {
return val ? unsafe_yyjson_is_obj(val) : false;
}
yyjson_api_inline bool yyjson_is_ctn(yyjson_val *val) {
return val ? unsafe_yyjson_is_ctn(val) : false;
}
/*==============================================================================
* JSON Value Content API (Implementation)
*============================================================================*/
yyjson_api_inline yyjson_type yyjson_get_type(yyjson_val *val) {
return val ? unsafe_yyjson_get_type(val) : YYJSON_TYPE_NONE;
}
yyjson_api_inline yyjson_subtype yyjson_get_subtype(yyjson_val *val) {
return val ? unsafe_yyjson_get_subtype(val) : YYJSON_SUBTYPE_NONE;
}
yyjson_api_inline uint8_t yyjson_get_tag(yyjson_val *val) {
return val ? unsafe_yyjson_get_tag(val) : 0;
}
yyjson_api_inline const char *yyjson_get_type_desc(yyjson_val *val) {
switch (yyjson_get_tag(val)) {
case YYJSON_TYPE_NULL | YYJSON_SUBTYPE_NONE: return "null";
case YYJSON_TYPE_STR | YYJSON_SUBTYPE_NONE: return "string";
case YYJSON_TYPE_ARR | YYJSON_SUBTYPE_NONE: return "array";
case YYJSON_TYPE_OBJ | YYJSON_SUBTYPE_NONE: return "object";
case YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_TRUE: return "true";
case YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_FALSE: return "false";
case YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT: return "uint";
case YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT: return "sint";
case YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL: return "real";
default: return "unknown";
}
}
yyjson_api_inline bool yyjson_get_bool(yyjson_val *val) {
return yyjson_is_bool(val) ? unsafe_yyjson_get_bool(val) : false;
}
yyjson_api_inline uint64_t yyjson_get_uint(yyjson_val *val) {
return yyjson_is_int(val) ? unsafe_yyjson_get_uint(val) : 0;
}
yyjson_api_inline int64_t yyjson_get_sint(yyjson_val *val) {
return yyjson_is_int(val) ? unsafe_yyjson_get_sint(val) : 0;
}
yyjson_api_inline int yyjson_get_int(yyjson_val *val) {
return yyjson_is_int(val) ? unsafe_yyjson_get_int(val) : 0;
}
yyjson_api_inline double yyjson_get_real(yyjson_val *val) {
return yyjson_is_real(val) ? unsafe_yyjson_get_real(val) : 0.0;
}
yyjson_api_inline const char *yyjson_get_str(yyjson_val *val) {
return yyjson_is_str(val) ? unsafe_yyjson_get_str(val) : NULL;
}
yyjson_api_inline size_t yyjson_get_len(yyjson_val *val) {
return yyjson_is_str(val) ? unsafe_yyjson_get_len(val) : 0;
}
yyjson_api_inline bool yyjson_equals_str(yyjson_val *val, const char *str) {
if (yyjson_likely(val && str)) {
return unsafe_yyjson_equals_str(val, str);
}
return false;
}
yyjson_api_inline bool yyjson_equals_strn(yyjson_val *val, const char *str,
size_t len) {
if (yyjson_likely(val && str)) {
return unsafe_yyjson_equals_strn(val, str, len);
}
return false;
}
/*==============================================================================
* JSON Array API (Implementation)
*============================================================================*/
yyjson_api_inline size_t yyjson_arr_size(yyjson_val *arr) {
return yyjson_is_arr(arr) ? unsafe_yyjson_get_len(arr) : 0;
}
yyjson_api_inline yyjson_val *yyjson_arr_get(yyjson_val *arr, size_t idx) {
if (yyjson_likely(yyjson_is_arr(arr))) {
if (yyjson_likely(unsafe_yyjson_get_len(arr) > idx)) {
yyjson_val *val = unsafe_yyjson_get_first(arr);
if (unsafe_yyjson_arr_is_flat(arr)) {
return val + idx;
} else {
while (idx-- > 0) val = unsafe_yyjson_get_next(val);
return val;
}
}
}
return NULL;
}
yyjson_api_inline yyjson_val *yyjson_arr_get_first(yyjson_val *arr) {
if (yyjson_likely(yyjson_is_arr(arr))) {
if (yyjson_likely(unsafe_yyjson_get_len(arr) > 0)) {
return unsafe_yyjson_get_first(arr);
}
}
return NULL;
}
yyjson_api_inline yyjson_val *yyjson_arr_get_last(yyjson_val *arr) {
if (yyjson_likely(yyjson_is_arr(arr))) {
size_t len = unsafe_yyjson_get_len(arr);
if (yyjson_likely(len > 0)) {
yyjson_val *val = unsafe_yyjson_get_first(arr);
if (unsafe_yyjson_arr_is_flat(arr)) {
return val + (len - 1);
} else {
while (len-- > 1) val = unsafe_yyjson_get_next(val);
return val;
}
}
}
return NULL;
}
/*==============================================================================
* JSON Array Iterator API (Implementation)
*============================================================================*/
struct yyjson_arr_iter {
size_t idx;
size_t max;
yyjson_val *cur;
};
yyjson_api_inline bool yyjson_arr_iter_init(yyjson_val *arr,
yyjson_arr_iter *iter) {
if (yyjson_likely(yyjson_is_arr(arr) && iter)) {
iter->idx = 0;
iter->max = unsafe_yyjson_get_len(arr);
iter->cur = unsafe_yyjson_get_first(arr);
return true;
}
if (iter) memset(iter, 0, sizeof(yyjson_arr_iter));
return false;
}
yyjson_api_inline bool yyjson_arr_iter_has_next(yyjson_arr_iter *iter) {
return iter ? iter->idx < iter->max : false;
}
yyjson_api_inline yyjson_val *yyjson_arr_iter_next(yyjson_arr_iter *iter) {
yyjson_val *val;
if (iter && iter->idx < iter->max) {
val = iter->cur;
iter->cur = unsafe_yyjson_get_next(val);
iter->idx++;
return val;
}
return NULL;
}
/*==============================================================================
* JSON Object API (Implementation)
*============================================================================*/
yyjson_api_inline size_t yyjson_obj_size(yyjson_val *obj) {
return yyjson_is_obj(obj) ? unsafe_yyjson_get_len(obj) : 0;
}
yyjson_api_inline yyjson_val *yyjson_obj_get(yyjson_val *obj,
const char *key_str) {
return yyjson_obj_getn(obj, key_str, key_str ? strlen(key_str) : 0);
}
yyjson_api_inline yyjson_val *yyjson_obj_getn(yyjson_val *obj,
const char *key_str,
size_t key_len) {
uint64_t tag = (((uint64_t)key_len) << YYJSON_TAG_BIT) | YYJSON_TYPE_STR;
if (yyjson_likely(yyjson_is_obj(obj) && key_str)) {
size_t len = unsafe_yyjson_get_len(obj);
yyjson_val *key = unsafe_yyjson_get_first(obj);
while (len-- > 0) {
if (key->tag == tag &&
memcmp(key->uni.ptr, key_str, key_len) == 0) {
return key + 1;
}
key = unsafe_yyjson_get_next(key + 1);
}
}
return NULL;
}
/*==============================================================================
* JSON Object Iterator API (Implementation)
*============================================================================*/
struct yyjson_obj_iter {
size_t idx;
size_t max;
yyjson_val *cur;
yyjson_val *obj;
};
yyjson_api_inline bool yyjson_obj_iter_init(yyjson_val *obj,
yyjson_obj_iter *iter) {
if (yyjson_likely(yyjson_is_obj(obj) && iter)) {
iter->idx = 0;
iter->max = unsafe_yyjson_get_len(obj);
iter->cur = unsafe_yyjson_get_first(obj);
iter->obj = obj;
return true;
}
if (iter) {
iter->idx = 0;
iter->max = 0;
}
return false;
}
yyjson_api_inline bool yyjson_obj_iter_has_next(yyjson_obj_iter *iter) {
return iter ? iter->idx < iter->max : false;
}
yyjson_api_inline yyjson_val *yyjson_obj_iter_next(yyjson_obj_iter *iter) {
if (iter && iter->idx < iter->max) {
yyjson_val *key = iter->cur;
iter->idx++;
iter->cur = unsafe_yyjson_get_next(key + 1);
return key;
}
return NULL;
}
yyjson_api_inline yyjson_val *yyjson_obj_iter_get_val(yyjson_val *key) {
return key + 1;
}
yyjson_api_inline yyjson_val *yyjson_obj_iter_get(yyjson_obj_iter *iter,
const char *key) {
return yyjson_obj_iter_getn(iter, key, key ? strlen(key) : 0);
}
yyjson_api_inline yyjson_val *yyjson_obj_iter_getn(yyjson_obj_iter *iter,
const char *key,
size_t key_len) {
if (iter && key) {
size_t idx = iter->idx;
size_t max = iter->max;
yyjson_val *cur = iter->cur;
if (yyjson_unlikely(idx == max)) {
idx = 0;
cur = unsafe_yyjson_get_first(iter->obj);
}
while (idx++ < max) {
yyjson_val *next = unsafe_yyjson_get_next(cur + 1);
if (unsafe_yyjson_get_len(cur) == key_len &&
memcmp(cur->uni.str, key, key_len) == 0) {
iter->idx = idx;
iter->cur = next;
return cur + 1;
}
cur = next;
if (idx == iter->max && iter->idx < iter->max) {
idx = 0;
max = iter->idx;
cur = unsafe_yyjson_get_first(iter->obj);
}
}
}
return NULL;
}
/*==============================================================================
* Mutable JSON Structure (Implementation)
*============================================================================*/
/*
Mutable JSON value, 24 bytes.
The 'tag' and 'uni' field is same as immutable value.
The 'next' field links all elements inside the container to be a cycle.
*/
struct yyjson_mut_val {
uint64_t tag;
yyjson_val_uni uni;
yyjson_mut_val *next;
};
typedef struct yyjson_str_chunk {
struct yyjson_str_chunk *next;
/* flexible array member here */
} yyjson_str_chunk;
typedef struct yyjson_str_pool {
char *cur; /* cursor inside current chunk */
char *end; /* the end of current chunk */
size_t chunk_size; /* chunk size in bytes while creating new chunk */
size_t chunk_size_max; /* maximum chunk size in bytes */
yyjson_str_chunk *chunks; /* a linked list of chunks, nullable */
} yyjson_str_pool;
typedef struct yyjson_val_chunk {
struct yyjson_val_chunk *next;
/* flexible array member here */
} yyjson_val_chunk;
typedef struct yyjson_val_pool {
yyjson_mut_val *cur; /* cursor inside current chunk */
yyjson_mut_val *end; /* the end of current chunk */
size_t chunk_size; /* chunk size in bytes while creating new chunk */
size_t chunk_size_max; /* maximum chunk size in bytes */
yyjson_val_chunk *chunks; /* a linked list of chunks, nullable */
} yyjson_val_pool;
struct yyjson_mut_doc {
yyjson_mut_val *root; /* root value of the JSON document, nullable */
yyjson_alc alc; /* a valid allocator, nonnull */
yyjson_str_pool str_pool; /* string memory holder */
yyjson_val_pool val_pool; /* value memory holder */
};
/* Ensures the capacity to at least equal to the specified byte length. */
yyjson_api bool unsafe_yyjson_str_pool_grow(yyjson_str_pool *pool,
yyjson_alc *alc, size_t len);
/* Ensures the capacity to at least equal to the specified value count. */
yyjson_api bool unsafe_yyjson_val_pool_grow(yyjson_val_pool *pool,
yyjson_alc *alc, size_t count);
yyjson_api_inline char *unsafe_yyjson_mut_strncpy(yyjson_mut_doc *doc,
const char *str, size_t len) {
if (len == 0) len = strlen(str);
char *mem;
yyjson_alc *alc = &doc->alc;
yyjson_str_pool *pool = &doc->str_pool;
if (!str) return NULL;
if (yyjson_unlikely((size_t)(pool->end - pool->cur) <= len)) {
if (yyjson_unlikely(!unsafe_yyjson_str_pool_grow(pool, alc, len + 1))) {
return NULL;
}
}
mem = pool->cur;
pool->cur = mem + len + 1;
memcpy((void *)mem, (const void *)str, len);
mem[len] = '\0';
return mem;
}
yyjson_api_inline yyjson_mut_val *unsafe_yyjson_mut_val(yyjson_mut_doc *doc,
size_t count) {
yyjson_mut_val *val;
yyjson_alc *alc = &doc->alc;
yyjson_val_pool *pool = &doc->val_pool;
if (yyjson_unlikely((size_t)(pool->end - pool->cur) < count)) {
if (yyjson_unlikely(!unsafe_yyjson_val_pool_grow(pool, alc, count))) {
return NULL;
}
}
val = pool->cur;
pool->cur += count;
return val;
}
/*==============================================================================
* Mutable JSON Document API (Implementation)
*============================================================================*/
yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_get_root(yyjson_mut_doc *doc) {
return doc ? doc->root : NULL;
}
yyjson_api_inline void yyjson_mut_doc_set_root(yyjson_mut_doc *doc,
yyjson_mut_val *root) {
if (doc) doc->root = root;
}
/*==============================================================================
* Mutable JSON Value Type API (Implementation)
*============================================================================*/
yyjson_api_inline bool yyjson_mut_is_null(yyjson_mut_val *val) {
return val ? unsafe_yyjson_is_null(val) : false;
}
yyjson_api_inline bool yyjson_mut_is_true(yyjson_mut_val *val) {
return val ? unsafe_yyjson_is_true(val) : false;
}
yyjson_api_inline bool yyjson_mut_is_false(yyjson_mut_val *val) {
return val ? unsafe_yyjson_is_false(val) : false;
}
yyjson_api_inline bool yyjson_mut_is_bool(yyjson_mut_val *val) {
return val ? unsafe_yyjson_is_bool(val) : false;
}
yyjson_api_inline bool yyjson_mut_is_uint(yyjson_mut_val *val) {
return val ? unsafe_yyjson_is_uint(val) : false;
}
yyjson_api_inline bool yyjson_mut_is_sint(yyjson_mut_val *val) {
return val ? unsafe_yyjson_is_sint(val) : false;
}
yyjson_api_inline bool yyjson_mut_is_int(yyjson_mut_val *val) {
return val ? unsafe_yyjson_is_int(val) : false;
}
yyjson_api_inline bool yyjson_mut_is_real(yyjson_mut_val *val) {
return val ? unsafe_yyjson_is_real(val) : false;
}
yyjson_api_inline bool yyjson_mut_is_num(yyjson_mut_val *val) {
return val ? unsafe_yyjson_is_num(val) : false;
}
yyjson_api_inline bool yyjson_mut_is_str(yyjson_mut_val *val) {
return val ? unsafe_yyjson_is_str(val) : false;
}
yyjson_api_inline bool yyjson_mut_is_arr(yyjson_mut_val *val) {
return val ? unsafe_yyjson_is_arr(val) : false;
}
yyjson_api_inline bool yyjson_mut_is_obj(yyjson_mut_val *val) {
return val ? unsafe_yyjson_is_obj(val) : false;
}
yyjson_api_inline bool yyjson_mut_is_ctn(yyjson_mut_val *val) {
return val ? unsafe_yyjson_is_ctn(val) : false;
}
/*==============================================================================
* Mutable JSON Value Content API (Implementation)
*============================================================================*/
yyjson_api_inline yyjson_type yyjson_mut_get_type(yyjson_mut_val *val) {
return yyjson_get_type((yyjson_val *)val);
}
yyjson_api_inline yyjson_subtype yyjson_mut_get_subtype(yyjson_mut_val *val) {
return yyjson_get_subtype((yyjson_val *)val);
}
yyjson_api_inline uint8_t yyjson_mut_get_tag(yyjson_mut_val *val) {
return yyjson_get_tag((yyjson_val *)val);
}
yyjson_api_inline const char *yyjson_mut_get_type_desc(yyjson_mut_val *val) {
return yyjson_get_type_desc((yyjson_val *)val);
}
yyjson_api_inline bool yyjson_mut_get_bool(yyjson_mut_val *val) {
return yyjson_get_bool((yyjson_val *)val);
}
yyjson_api_inline uint64_t yyjson_mut_get_uint(yyjson_mut_val *val) {
return yyjson_get_uint((yyjson_val *)val);
}
yyjson_api_inline int64_t yyjson_mut_get_sint(yyjson_mut_val *val) {
return yyjson_get_sint((yyjson_val *)val);
}
yyjson_api_inline int yyjson_mut_get_int(yyjson_mut_val *val) {
return yyjson_get_int((yyjson_val *)val);
}
yyjson_api_inline double yyjson_mut_get_real(yyjson_mut_val *val) {
return yyjson_get_real((yyjson_val *)val);
}
yyjson_api_inline const char *yyjson_mut_get_str(yyjson_mut_val *val) {
return yyjson_get_str((yyjson_val *)val);
}
yyjson_api_inline size_t yyjson_mut_get_len(yyjson_mut_val *val) {
return yyjson_get_len((yyjson_val *)val);
}
yyjson_api_inline bool yyjson_mut_equals_str(yyjson_mut_val *val,
const char *str) {
return yyjson_equals_str((yyjson_val *)val, str);
}
yyjson_api_inline bool yyjson_mut_equals_strn(yyjson_mut_val *val,
const char *str, size_t len) {
return yyjson_equals_strn((yyjson_val *)val, str, len);
}
/*==============================================================================
* Mutable JSON Value Creation API (Implementation)
*============================================================================*/
yyjson_api_inline yyjson_mut_val *yyjson_mut_null(yyjson_mut_doc *doc) {
if (yyjson_likely(doc)) {
yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1);
if (yyjson_likely(val)) {
val->tag = YYJSON_TYPE_NULL | YYJSON_SUBTYPE_NONE;
return val;
}
}
return NULL;
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_true(yyjson_mut_doc *doc) {
if (yyjson_likely(doc)) {
yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1);
if (yyjson_likely(val)) {
val->tag = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_TRUE;
return val;
}
}
return NULL;
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_false(yyjson_mut_doc *doc) {
if (yyjson_likely(doc)) {
yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1);
if (yyjson_likely(val)) {
val->tag = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_FALSE;
return val;
}
}
return NULL;
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_bool(yyjson_mut_doc *doc,
bool _val) {
if (yyjson_likely(doc)) {
yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1);
if (yyjson_likely(val)) {
val->tag = YYJSON_TYPE_BOOL | (uint8_t)((uint8_t)_val << 3);
return val;
}
}
return NULL;
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_uint(yyjson_mut_doc *doc,
uint64_t num) {
if (yyjson_likely(doc)) {
yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1);
if (yyjson_likely(val)) {
val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT;
val->uni.u64 = num;
return val;
}
}
return NULL;
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_sint(yyjson_mut_doc *doc,
int64_t num) {
if (yyjson_likely(doc)) {
yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1);
if (yyjson_likely(val)) {
val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT;
val->uni.i64 = num;
return val;
}
}
return NULL;
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_int(yyjson_mut_doc *doc,
int64_t num) {
return yyjson_mut_sint(doc, num);
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_real(yyjson_mut_doc *doc,
double num) {
if (yyjson_likely(doc)) {
yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1);
if (yyjson_likely(val)) {
val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL;
val->uni.f64 = num;
return val;
}
}
return NULL;
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_str(yyjson_mut_doc *doc,
const char *str) {
if (yyjson_likely(str)) return yyjson_mut_strn(doc, str, strlen(str));
return NULL;
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_strn(yyjson_mut_doc *doc,
const char *str,
size_t len) {
if (yyjson_likely(doc && str)) {
yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1);
if (yyjson_likely(val)) {
val->tag = ((uint64_t)len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR;
val->uni.str = str;
return val;
}
}
return NULL;
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_strcpy(yyjson_mut_doc *doc,
const char *str) {
if (yyjson_likely(str)) return yyjson_mut_strncpy(doc, str, strlen(str));
return NULL;
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_strncpy(yyjson_mut_doc *doc,
const char *str,
size_t len) {
if (yyjson_likely(doc && str)) {
yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1);
char *new_str = unsafe_yyjson_mut_strncpy(doc, str, len);
if (yyjson_likely(val && new_str)) {
val->tag = ((uint64_t)len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR;
val->uni.str = new_str;
return val;
}
}
return NULL;
}
/*==============================================================================
* Mutable JSON Array API (Implementation)
*============================================================================*/
yyjson_api_inline size_t yyjson_mut_arr_size(yyjson_mut_val *arr) {
return yyjson_mut_is_arr(arr) ? unsafe_yyjson_get_len(arr) : 0;
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get(yyjson_mut_val *arr,
size_t idx) {
if (yyjson_likely(idx < yyjson_mut_arr_size(arr))) {
yyjson_mut_val *val = (yyjson_mut_val *)arr->uni.ptr;
while (idx-- > 0) val = val->next;
return val->next;
}
return NULL;
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get_first(
yyjson_mut_val *arr) {
if (yyjson_likely(yyjson_mut_arr_size(arr) > 0)) {
return ((yyjson_mut_val *)arr->uni.ptr)->next;
}
return NULL;
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get_last(yyjson_mut_val *arr) {
if (yyjson_likely(yyjson_mut_arr_size(arr) > 0)) {
return ((yyjson_mut_val *)arr->uni.ptr);
}
return NULL;
}
/*==============================================================================
* Mutable JSON Array Iterator API (Implementation)
*============================================================================*/
struct yyjson_mut_arr_iter {
size_t idx;
size_t max;
yyjson_mut_val *cur;
yyjson_mut_val *pre;
yyjson_mut_val *arr;
};
yyjson_api_inline bool yyjson_mut_arr_iter_init(yyjson_mut_val *arr,
yyjson_mut_arr_iter *iter) {
if (yyjson_likely(yyjson_mut_is_arr(arr) && iter)) {
iter->idx = 0;
iter->max = unsafe_yyjson_get_len(arr);
iter->cur = iter->max ? (yyjson_mut_val *)arr->uni.ptr : NULL;
iter->pre = NULL;
iter->arr = arr;
return true;
}
if (iter) memset(iter, 0, sizeof(yyjson_mut_arr_iter));
return false;
}
yyjson_api_inline bool yyjson_mut_arr_iter_has_next(yyjson_mut_arr_iter *iter) {
return iter ? iter->idx < iter->max : false;
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_iter_next(
yyjson_mut_arr_iter *iter) {
if (iter && iter->idx < iter->max) {
yyjson_mut_val *val = iter->cur;
iter->pre = val;
iter->cur = val->next;
iter->idx++;
return iter->cur;
}
return NULL;
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_iter_remove(
yyjson_mut_arr_iter *iter) {
if (yyjson_likely(iter && 0 < iter->idx && iter->idx <= iter->max)) {
yyjson_mut_val *prev = iter->pre;
yyjson_mut_val *cur = iter->cur;
yyjson_mut_val *next = cur->next;
if (yyjson_unlikely(iter->idx == iter->max)) iter->arr->uni.ptr = prev;
iter->idx--;
iter->max--;
unsafe_yyjson_set_len(iter->arr, iter->max);
prev->next = next;
iter->cur = next;
return cur;
}
return NULL;
}
/*==============================================================================
* Mutable JSON Array Creation API (Implementation)
*============================================================================*/
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr(yyjson_mut_doc *doc) {
if (yyjson_likely(doc)) {
yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1);
if (yyjson_likely(val)) {
val->tag = YYJSON_TYPE_ARR | YYJSON_SUBTYPE_NONE;
return val;
}
}
return NULL;
}
#define yyjson_mut_arr_with_func(func) \
if (yyjson_likely(doc && ((0 < count && count < \
(~(size_t)0) / sizeof(yyjson_mut_val) && vals) || count == 0))) { \
yyjson_mut_val *arr = unsafe_yyjson_mut_val(doc, 1 + count); \
if (yyjson_likely(arr)) { \
arr->tag = ((uint64_t)count << YYJSON_TAG_BIT) | YYJSON_TYPE_ARR; \
if (count > 0) { \
size_t i; \
for (i = 0; i < count; i++) { \
yyjson_mut_val *val = arr + i + 1; \
func \
val->next = val + 1; \
} \
arr[count].next = arr + 1; \
arr->uni.ptr = arr + count; \
} \
return arr; \
} \
} \
return NULL
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_bool(
yyjson_mut_doc *doc, const bool *vals, size_t count) {
yyjson_mut_arr_with_func({
val->tag = YYJSON_TYPE_BOOL | (uint8_t)((uint8_t)vals[i] << 3);
});
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint(
yyjson_mut_doc *doc, const int64_t *vals, size_t count) {
return yyjson_mut_arr_with_sint64(doc, vals, count);
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint(
yyjson_mut_doc *doc, const uint64_t *vals, size_t count) {
return yyjson_mut_arr_with_uint64(doc, vals, count);
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_real(
yyjson_mut_doc *doc, const double *vals, size_t count) {
return yyjson_mut_arr_with_double(doc, vals, count);
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint8(
yyjson_mut_doc *doc, const int8_t *vals, size_t count) {
yyjson_mut_arr_with_func({
val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT;
val->uni.i64 = (int64_t)vals[i];
});
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint16(
yyjson_mut_doc *doc, const int16_t *vals, size_t count) {
yyjson_mut_arr_with_func({
val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT;
val->uni.i64 = vals[i];
});
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint32(
yyjson_mut_doc *doc, const int32_t *vals, size_t count) {
yyjson_mut_arr_with_func({
val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT;
val->uni.i64 = vals[i];
});
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint64(
yyjson_mut_doc *doc, const int64_t *vals, size_t count) {
yyjson_mut_arr_with_func({
val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT;
val->uni.i64 = vals[i];
});
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint8(
yyjson_mut_doc *doc, const uint8_t *vals, size_t count) {
yyjson_mut_arr_with_func({
val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT;
val->uni.u64 = vals[i];
});
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint16(
yyjson_mut_doc *doc, const uint16_t *vals, size_t count) {
yyjson_mut_arr_with_func({
val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT;
val->uni.u64 = vals[i];
});
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint32(
yyjson_mut_doc *doc, const uint32_t *vals, size_t count) {
yyjson_mut_arr_with_func({
val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT;
val->uni.u64 = vals[i];
});
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint64(
yyjson_mut_doc *doc, const uint64_t *vals, size_t count) {
yyjson_mut_arr_with_func({
val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT;
val->uni.u64 = vals[i];
});
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_float(
yyjson_mut_doc *doc, const float *vals, size_t count) {
yyjson_mut_arr_with_func({
val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL;
val->uni.f64 = (double)vals[i];
});
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_double(
yyjson_mut_doc *doc, const double *vals, size_t count) {
yyjson_mut_arr_with_func({
val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL;
val->uni.f64 = vals[i];
});
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_str(
yyjson_mut_doc *doc, const char **vals, size_t count) {
yyjson_mut_arr_with_func({
uint64_t len = (uint64_t)strlen(vals[i]);
val->tag = (len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR;
val->uni.str = vals[i];
});
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strn(
yyjson_mut_doc *doc, const char **vals, const size_t *lens, size_t count) {
if (yyjson_unlikely(count > 0 && !lens)) return NULL;
yyjson_mut_arr_with_func({
val->tag = ((uint64_t)lens[i] << YYJSON_TAG_BIT) | YYJSON_TYPE_STR;
val->uni.str = vals[i];
});
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strcpy(
yyjson_mut_doc *doc, const char **vals, size_t count) {
size_t len;
const char *str;
yyjson_mut_arr_with_func({
str = vals[i];
if (!str) return NULL;
len = strlen(str);
val->tag = ((uint64_t)len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR;
val->uni.str = unsafe_yyjson_mut_strncpy(doc, str, len);
if (!val->uni.str) return NULL;
});
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strncpy(
yyjson_mut_doc *doc, const char **vals, const size_t *lens, size_t count) {
size_t len;
const char *str;
if (yyjson_unlikely(count > 0 && !lens)) return NULL;
yyjson_mut_arr_with_func({
str = vals[i];
len = lens[i];
val->tag = ((uint64_t)len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR;
val->uni.str = unsafe_yyjson_mut_strncpy(doc, str, len);
if (!val->uni.str) return NULL;
});
}
#undef yyjson_mut_arr_with_func
/*==============================================================================
* Mutable JSON Array Modification API (Implementation)
*============================================================================*/
yyjson_api_inline bool yyjson_mut_arr_insert(yyjson_mut_val *arr,
yyjson_mut_val *val, size_t idx) {
if (yyjson_likely(yyjson_mut_is_arr(arr) && val)) {
size_t len = unsafe_yyjson_get_len(arr);
if (yyjson_likely(idx <= len)) {
unsafe_yyjson_set_len(arr, len + 1);
if (len == 0) {
val->next = val;
arr->uni.ptr = val;
} else {
yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr);
yyjson_mut_val *next = prev->next;
if (idx == len) {
prev->next = val;
val->next = next;
arr->uni.ptr = val;
} else {
while (idx-- > 0) {
prev = next;
next = next->next;
}
prev->next = val;
val->next = next;
}
}
return true;
}
}
return false;
}
yyjson_api_inline bool yyjson_mut_arr_append(yyjson_mut_val *arr,
yyjson_mut_val *val) {
if (yyjson_likely(yyjson_mut_is_arr(arr) && val)) {
size_t len = unsafe_yyjson_get_len(arr);
unsafe_yyjson_set_len(arr, len + 1);
if (len == 0) {
val->next = val;
} else {
yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr);
yyjson_mut_val *next = prev->next;
prev->next = val;
val->next = next;
}
arr->uni.ptr = val;
return true;
}
return false;
}
yyjson_api_inline bool yyjson_mut_arr_prepend(yyjson_mut_val *arr,
yyjson_mut_val *val) {
if (yyjson_likely(yyjson_mut_is_arr(arr) && val)) {
size_t len = unsafe_yyjson_get_len(arr);
unsafe_yyjson_set_len(arr, len + 1);
if (len == 0) {
val->next = val;
arr->uni.ptr = val;
} else {
yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr);
yyjson_mut_val *next = prev->next;
prev->next = val;
val->next = next;
}
return true;
}
return false;
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_replace(yyjson_mut_val *arr,
size_t idx,
yyjson_mut_val *val) {
if (yyjson_likely(yyjson_mut_is_arr(arr) && val)) {
size_t len = unsafe_yyjson_get_len(arr);
if (yyjson_likely(idx < len)) {
if (yyjson_likely(len > 1)) {
yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr);
yyjson_mut_val *next = prev->next;
while (idx-- > 0) {
prev = next;
next = next->next;
}
prev->next = val;
val->next = next->next;
if ((void *)next == arr->uni.ptr) arr->uni.ptr = val;
return next;
} else {
yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr);
val->next = val;
arr->uni.ptr = val;
return prev;
};
}
}
return NULL;
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_remove(yyjson_mut_val *arr,
size_t idx) {
if (yyjson_likely(yyjson_mut_is_arr(arr))) {
size_t len = unsafe_yyjson_get_len(arr);
if (yyjson_likely(idx < len)) {
unsafe_yyjson_set_len(arr, len - 1);
if (yyjson_likely(len > 1)) {
yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr);
yyjson_mut_val *next = prev->next;
while (idx-- > 0) {
prev = next;
next = next->next;
}
prev->next = next->next;
if ((void *)next == arr->uni.ptr) arr->uni.ptr = prev;
return next;
} else {
return ((yyjson_mut_val *)arr->uni.ptr);
}
}
}
return NULL;
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_remove_first(
yyjson_mut_val *arr) {
if (yyjson_likely(yyjson_mut_is_arr(arr))) {
size_t len = unsafe_yyjson_get_len(arr);
if (len > 1) {
yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr);
yyjson_mut_val *next = prev->next;
prev->next = next->next;
unsafe_yyjson_set_len(arr, len - 1);
return next;
} else if (len == 1) {
yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr);
unsafe_yyjson_set_len(arr, 0);
return prev;
}
}
return NULL;
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_remove_last(
yyjson_mut_val *arr) {
if (yyjson_likely(yyjson_mut_is_arr(arr))) {
size_t len = unsafe_yyjson_get_len(arr);
if (yyjson_likely(len > 1)) {
yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr);
yyjson_mut_val *next = prev->next;
unsafe_yyjson_set_len(arr, len - 1);
while (--len > 0) prev = prev->next;
prev->next = next;
next = (yyjson_mut_val *)arr->uni.ptr;
arr->uni.ptr = prev;
return next;
} else if (len == 1) {
yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr);
unsafe_yyjson_set_len(arr, 0);
return prev;
}
}
return NULL;
}
yyjson_api_inline bool yyjson_mut_arr_remove_range(yyjson_mut_val *arr,
size_t _idx, size_t _len) {
if (yyjson_likely(yyjson_mut_is_arr(arr))) {
yyjson_mut_val *prev, *next;
bool tail_removed;
size_t len = unsafe_yyjson_get_len(arr);
if (yyjson_unlikely(_idx + _len > len)) return false;
if (yyjson_unlikely(_len == 0)) return true;
unsafe_yyjson_set_len(arr, len - _len);
if (yyjson_unlikely(len == _len)) return true;
tail_removed = (_idx + _len == len);
prev = ((yyjson_mut_val *)arr->uni.ptr);
while (_idx-- > 0) prev = prev->next;
next = prev->next;
while (_len-- > 0) next = next->next;
prev->next = next;
if (yyjson_unlikely(tail_removed)) arr->uni.ptr = prev;
return true;
}
return false;
}
yyjson_api_inline bool yyjson_mut_arr_clear(yyjson_mut_val *arr) {
if (yyjson_likely(yyjson_mut_is_arr(arr))) {
unsafe_yyjson_set_len(arr, 0);
return true;
}
return false;
}
yyjson_api_inline bool yyjson_mut_arr_rotate(yyjson_mut_val *arr,
size_t idx) {
if (yyjson_likely(yyjson_mut_is_arr(arr) &&
unsafe_yyjson_get_len(arr) > idx)) {
yyjson_mut_val *val = (yyjson_mut_val *)arr->uni.ptr;
while (idx-- > 0) val = val->next;
arr->uni.ptr = (void *)val;
return true;
}
return false;
}
/*==============================================================================
* Mutable JSON Array Modification Convenience API (Implementation)
*============================================================================*/
yyjson_api_inline bool yyjson_mut_arr_add_val(yyjson_mut_val *arr,
yyjson_mut_val *val) {
return yyjson_mut_arr_append(arr, val);
}
yyjson_api_inline bool yyjson_mut_arr_add_null(yyjson_mut_doc *doc,
yyjson_mut_val *arr) {
if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) {
yyjson_mut_val *val = yyjson_mut_null(doc);
return yyjson_mut_arr_append(arr, val);
}
return false;
}
yyjson_api_inline bool yyjson_mut_arr_add_true(yyjson_mut_doc *doc,
yyjson_mut_val *arr) {
if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) {
yyjson_mut_val *val = yyjson_mut_true(doc);
return yyjson_mut_arr_append(arr, val);
}
return false;
}
yyjson_api_inline bool yyjson_mut_arr_add_false(yyjson_mut_doc *doc,
yyjson_mut_val *arr) {
if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) {
yyjson_mut_val *val = yyjson_mut_false(doc);
return yyjson_mut_arr_append(arr, val);
}
return false;
}
yyjson_api_inline bool yyjson_mut_arr_add_bool(yyjson_mut_doc *doc,
yyjson_mut_val *arr,
bool _val) {
if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) {
yyjson_mut_val *val = yyjson_mut_bool(doc, _val);
return yyjson_mut_arr_append(arr, val);
}
return false;
}
yyjson_api_inline bool yyjson_mut_arr_add_uint(yyjson_mut_doc *doc,
yyjson_mut_val *arr,
uint64_t num) {
if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) {
yyjson_mut_val *val = yyjson_mut_uint(doc, num);
return yyjson_mut_arr_append(arr, val);
}
return false;
}
yyjson_api_inline bool yyjson_mut_arr_add_sint(yyjson_mut_doc *doc,
yyjson_mut_val *arr,
int64_t num) {
if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) {
yyjson_mut_val *val = yyjson_mut_sint(doc, num);
return yyjson_mut_arr_append(arr, val);
}
return false;
}
yyjson_api_inline bool yyjson_mut_arr_add_int(yyjson_mut_doc *doc,
yyjson_mut_val *arr,
int64_t num) {
if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) {
yyjson_mut_val *val = yyjson_mut_sint(doc, num);
return yyjson_mut_arr_append(arr, val);
}
return false;
}
yyjson_api_inline bool yyjson_mut_arr_add_real(yyjson_mut_doc *doc,
yyjson_mut_val *arr,
double num) {
if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) {
yyjson_mut_val *val = yyjson_mut_real(doc, num);
return yyjson_mut_arr_append(arr, val);
}
return false;
}
yyjson_api_inline bool yyjson_mut_arr_add_str(yyjson_mut_doc *doc,
yyjson_mut_val *arr,
const char *str) {
if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) {
yyjson_mut_val *val = yyjson_mut_str(doc, str);
return yyjson_mut_arr_append(arr, val);
}
return false;
}
yyjson_api_inline bool yyjson_mut_arr_add_strn(yyjson_mut_doc *doc,
yyjson_mut_val *arr,
const char *str, size_t len) {
if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) {
yyjson_mut_val *val = yyjson_mut_strn(doc, str, len);
return yyjson_mut_arr_append(arr, val);
}
return false;
}
yyjson_api_inline bool yyjson_mut_arr_add_strcpy(yyjson_mut_doc *doc,
yyjson_mut_val *arr,
const char *str) {
if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) {
yyjson_mut_val *val = yyjson_mut_strcpy(doc, str);
return yyjson_mut_arr_append(arr, val);
}
return false;
}
yyjson_api_inline bool yyjson_mut_arr_add_strncpy(yyjson_mut_doc *doc,
yyjson_mut_val *arr,
const char *str, size_t len) {
if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) {
yyjson_mut_val *val = yyjson_mut_strncpy(doc, str, len);
return yyjson_mut_arr_append(arr, val);
}
return false;
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_add_arr(yyjson_mut_doc *doc,
yyjson_mut_val *arr) {
if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) {
yyjson_mut_val *val = yyjson_mut_arr(doc);
return yyjson_mut_arr_append(arr, val) ? val : NULL;
}
return NULL;
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_add_obj(yyjson_mut_doc *doc,
yyjson_mut_val *arr) {
if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) {
yyjson_mut_val *val = yyjson_mut_obj(doc);
return yyjson_mut_arr_append(arr, val) ? val : NULL;
}
return NULL;
}
/*==============================================================================
* Mutable JSON Object API (Implementation)
*============================================================================*/
yyjson_api_inline size_t yyjson_mut_obj_size(yyjson_mut_val *obj) {
return yyjson_mut_is_obj(obj) ? unsafe_yyjson_get_len(obj) : 0;
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_get(yyjson_mut_val *obj,
const char *key_str) {
return yyjson_mut_obj_getn(obj, key_str, key_str ? strlen(key_str) : 0);
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_getn(yyjson_mut_val *obj,
const char *key_str,
size_t key_len) {
uint64_t tag = (((uint64_t)key_len) << YYJSON_TAG_BIT) | YYJSON_TYPE_STR;
size_t len = yyjson_mut_obj_size(obj);
if (yyjson_likely(len && key_str)) {
yyjson_mut_val *key = ((yyjson_mut_val *)obj->uni.ptr)->next->next;
while (len-- > 0) {
if (key->tag == tag &&
memcmp(key->uni.ptr, key_str, key_len) == 0) {
return key->next;
}
key = key->next->next;
}
}
return NULL;
}
/*==============================================================================
* Mutable JSON Object Iterator API (Implementation)
*============================================================================*/
struct yyjson_mut_obj_iter {
size_t idx;
size_t max;
yyjson_mut_val *cur;
yyjson_mut_val *pre;
yyjson_mut_val *obj;
};
yyjson_api_inline bool yyjson_mut_obj_iter_init(yyjson_mut_val *obj,
yyjson_mut_obj_iter *iter) {
if (yyjson_likely(yyjson_mut_is_obj(obj) && iter)) {
iter->idx = 0;
iter->max = unsafe_yyjson_get_len(obj);
iter->cur = iter->max ? (yyjson_mut_val *)obj->uni.ptr : NULL;
iter->pre = NULL;
iter->obj = obj;
return true;
}
if (iter) memset(iter, 0, sizeof(yyjson_mut_obj_iter));
return false;
}
yyjson_api_inline bool yyjson_mut_obj_iter_has_next(yyjson_mut_obj_iter *iter) {
return iter ? iter->idx < iter->max : false;
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_next(
yyjson_mut_obj_iter *iter) {
if (iter && iter->idx < iter->max) {
yyjson_mut_val *key = iter->cur;
iter->pre = key;
iter->cur = key->next->next;
iter->idx++;
return iter->cur;
}
return NULL;
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_get_val(
yyjson_mut_val *key) {
return key->next;
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_remove(
yyjson_mut_obj_iter *iter) {
if (yyjson_likely(iter && 0 < iter->idx && iter->idx <= iter->max)) {
yyjson_mut_val *prev = iter->pre;
yyjson_mut_val *cur = iter->cur;
yyjson_mut_val *next = cur->next->next;
if (yyjson_unlikely(iter->idx == iter->max)) iter->obj->uni.ptr = prev;
iter->idx--;
iter->max--;
unsafe_yyjson_set_len(iter->obj, iter->max);
prev->next->next = next;
iter->cur = next;
return cur;
}
return NULL;
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_get(
yyjson_mut_obj_iter *iter, const char *key) {
return yyjson_mut_obj_iter_getn(iter, key, key ? strlen(key) : 0);
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_getn(
yyjson_mut_obj_iter *iter, const char *key, size_t key_len) {
if (iter && key) {
size_t idx = 0;
size_t max = iter->max;
yyjson_mut_val *pre, *cur = iter->cur;
while (idx++ < max) {
pre = cur;
cur = cur->next->next;
if (unsafe_yyjson_get_len(cur) == key_len &&
memcmp(cur->uni.str, key, key_len) == 0) {
iter->idx += idx;
if (iter->idx > max) iter->idx -= max + 1;
iter->pre = pre;
iter->cur = cur;
return cur->next;
}
}
}
return NULL;
}
/*==============================================================================
* Mutable JSON Object Creation API (Implementation)
*============================================================================*/
yyjson_api_inline yyjson_mut_val *yyjson_mut_obj(yyjson_mut_doc *doc) {
if (yyjson_likely(doc)) {
yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1);
if (yyjson_likely(val)) {
val->tag = YYJSON_TYPE_OBJ | YYJSON_SUBTYPE_NONE;
return val;
}
}
return NULL;
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_with_str(yyjson_mut_doc *doc,
const char **keys,
const char **vals,
size_t count) {
if (yyjson_likely(doc && ((count > 0 && keys && vals) || (count == 0)))) {
yyjson_mut_val *obj = unsafe_yyjson_mut_val(doc, 1 + count * 2);
if (yyjson_likely(obj)) {
obj->tag = ((uint64_t)count << YYJSON_TAG_BIT) | YYJSON_TYPE_OBJ;
if (count > 0) {
size_t i;
for (i = 0; i < count; i++) {
yyjson_mut_val *key = obj + (i * 2 + 1);
yyjson_mut_val *val = obj + (i * 2 + 2);
uint64_t key_len = (uint64_t)strlen(keys[i]);
uint64_t val_len = (uint64_t)strlen(vals[i]);
key->tag = (key_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR;
val->tag = (val_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR;
key->uni.str = keys[i];
val->uni.str = vals[i];
key->next = val;
val->next = val + 1;
}
obj[count * 2].next = obj + 1;
obj->uni.ptr = obj + (count * 2 - 1);
}
return obj;
}
}
return NULL;
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_with_kv(yyjson_mut_doc *doc,
const char **pairs,
size_t count) {
if (yyjson_likely(doc && ((count > 0 && pairs) || (count == 0)))) {
yyjson_mut_val *obj = unsafe_yyjson_mut_val(doc, 1 + count * 2);
if (yyjson_likely(obj)) {
obj->tag = ((uint64_t)count << YYJSON_TAG_BIT) | YYJSON_TYPE_OBJ;
if (count > 0) {
size_t i;
for (i = 0; i < count; i++) {
yyjson_mut_val *key = obj + (i * 2 + 1);
yyjson_mut_val *val = obj + (i * 2 + 2);
const char *key_str = pairs[i * 2 + 0];
const char *val_str = pairs[i * 2 + 1];
uint64_t key_len = (uint64_t)strlen(key_str);
uint64_t val_len = (uint64_t)strlen(val_str);
key->tag = (key_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR;
val->tag = (val_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR;
key->uni.str = key_str;
val->uni.str = val_str;
key->next = val;
val->next = val + 1;
}
obj[count * 2].next = obj + 1;
obj->uni.ptr = obj + (count * 2 - 1);
}
return obj;
}
}
return NULL;
}
/*==============================================================================
* Mutable JSON Object Modification API (Implementation)
*============================================================================*/
yyjson_api_inline void unsafe_yyjson_mut_obj_add(yyjson_mut_val *obj,
yyjson_mut_val *key,
yyjson_mut_val *val,
size_t len) {
if (yyjson_likely(len)) {
yyjson_mut_val *prev_val = ((yyjson_mut_val *)obj->uni.ptr)->next;
yyjson_mut_val *next_key = prev_val->next;
prev_val->next = key;
val->next = next_key;
} else {
val->next = key;
}
key->next = val;
obj->uni.ptr = (void *)key;
unsafe_yyjson_set_len(obj, len + 1);
}
yyjson_api_inline yyjson_mut_val *unsafe_yyjson_mut_obj_remove(
yyjson_mut_val *obj,
const char *key,
size_t key_len,
uint64_t key_tag) {
size_t obj_len = unsafe_yyjson_get_len(obj);
if (obj_len) {
yyjson_mut_val *pre_key = (yyjson_mut_val *)obj->uni.ptr;
yyjson_mut_val *cur_key = pre_key->next->next;
yyjson_mut_val *removed_item = NULL;
size_t i;
for (i = 0; i < obj_len; i++) {
if (key_tag == cur_key->tag &&
memcmp(key, cur_key->uni.ptr, key_len) == 0) {
if (!removed_item) removed_item = cur_key->next;
cur_key = cur_key->next->next;
pre_key->next->next = cur_key;
if (i + 1 == obj_len) obj->uni.ptr = pre_key;
i--;
obj_len--;
} else {
pre_key = cur_key;
cur_key = cur_key->next->next;
}
}
unsafe_yyjson_set_len(obj, obj_len);
return removed_item;
} else {
return NULL;
}
}
yyjson_api_inline bool unsafe_yyjson_mut_obj_replace(yyjson_mut_val *obj,
yyjson_mut_val *key,
yyjson_mut_val *val) {
size_t key_len = unsafe_yyjson_get_len(key);
size_t obj_len = unsafe_yyjson_get_len(obj);
if (obj_len) {
yyjson_mut_val *pre_key = (yyjson_mut_val *)obj->uni.ptr;
yyjson_mut_val *cur_key = pre_key->next->next;
size_t i;
for (i = 0; i < obj_len; i++) {
if (key->tag == cur_key->tag &&
memcmp(key->uni.str, cur_key->uni.ptr, key_len) == 0) {
size_t cpy_len = sizeof(*key) - sizeof(key->next);
yyjson_mut_val tmp;
memcpy(&tmp, cur_key, cpy_len);
memcpy(cur_key, key, cpy_len);
memcpy(key, &tmp, cpy_len);
memcpy(&tmp, cur_key->next, cpy_len);
memcpy(cur_key->next, val, cpy_len);
memcpy(val, &tmp, cpy_len);
return true;
} else {
pre_key = cur_key;
cur_key = cur_key->next->next;
}
}
}
return false;
}
yyjson_api_inline void unsafe_yyjson_mut_obj_rotate(yyjson_mut_val *obj,
size_t idx) {
yyjson_mut_val *key = (yyjson_mut_val *)obj->uni.ptr;
while (idx-- > 0) key = key->next->next;
obj->uni.ptr = (void *)key;
}
yyjson_api_inline bool yyjson_mut_obj_add(yyjson_mut_val *obj,
yyjson_mut_val *key,
yyjson_mut_val *val) {
if (yyjson_likely(yyjson_mut_is_obj(obj) &&
yyjson_mut_is_str(key) && val)) {
unsafe_yyjson_mut_obj_add(obj, key, val, unsafe_yyjson_get_len(obj));
return true;
}
return false;
}
yyjson_api_inline bool yyjson_mut_obj_put(yyjson_mut_val *obj,
yyjson_mut_val *key,
yyjson_mut_val *val) {
if (yyjson_likely(yyjson_mut_is_obj(obj) &&
yyjson_mut_is_str(key))) {
unsafe_yyjson_mut_obj_remove(obj, key->uni.str,
unsafe_yyjson_get_len(key), key->tag);
if (yyjson_likely(val)) {
unsafe_yyjson_mut_obj_add(obj, key, val,
unsafe_yyjson_get_len(obj));
}
return true;
}
return false;
}
yyjson_api_inline bool yyjson_mut_obj_insert(yyjson_mut_val *obj,
yyjson_mut_val *key,
yyjson_mut_val *val,
size_t idx) {
if (yyjson_likely(yyjson_mut_is_obj(obj) &&
yyjson_mut_is_str(key) && val)) {
size_t len = unsafe_yyjson_get_len(obj);
if (yyjson_likely(len >= idx)) {
if (len > idx) {
void *ptr = obj->uni.ptr;
unsafe_yyjson_mut_obj_rotate(obj, idx);
unsafe_yyjson_mut_obj_add(obj, key, val, len);
obj->uni.ptr = ptr;
} else {
unsafe_yyjson_mut_obj_add(obj, key, val, len);
}
return true;
}
}
return false;
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove(yyjson_mut_val *obj,
yyjson_mut_val *key) {
if (yyjson_likely(yyjson_mut_is_obj(obj) && yyjson_mut_is_str(key))) {
return unsafe_yyjson_mut_obj_remove(obj, key->uni.str,
unsafe_yyjson_get_len(key), key->tag);
}
return NULL;
}
yyjson_api_inline bool yyjson_mut_obj_clear(yyjson_mut_val *obj) {
if (yyjson_likely(yyjson_mut_is_obj(obj))) {
unsafe_yyjson_set_len(obj, 0);
return true;
}
return false;
}
yyjson_api_inline bool yyjson_mut_obj_replace(yyjson_mut_val *obj,
yyjson_mut_val *key,
yyjson_mut_val *val) {
if (yyjson_likely(yyjson_mut_is_obj(obj) &&
yyjson_mut_is_str(key) && val)) {
return unsafe_yyjson_mut_obj_replace(obj, key, val);
}
return false;
}
yyjson_api_inline bool yyjson_mut_obj_rotate(yyjson_mut_val *obj,
size_t idx) {
if (yyjson_likely(yyjson_mut_is_obj(obj) &&
unsafe_yyjson_get_len(obj) > idx)) {
unsafe_yyjson_mut_obj_rotate(obj, idx);
return true;
}
return false;
}
/*==============================================================================
* Mutable JSON Object Modification Convenience API (Implementation)
*============================================================================*/
#define yyjson_mut_obj_add_func(func) \
if (yyjson_likely(doc && yyjson_mut_is_obj(obj) && _key)) { \
yyjson_mut_val *key = unsafe_yyjson_mut_val(doc, 2); \
if (yyjson_likely(key)) { \
size_t len = unsafe_yyjson_get_len(obj); \
yyjson_mut_val *val = key + 1; \
key->tag = YYJSON_TYPE_STR | YYJSON_SUBTYPE_NONE; \
key->tag |= (uint64_t)strlen(_key) << YYJSON_TAG_BIT; \
key->uni.str = _key; \
func \
unsafe_yyjson_mut_obj_add(obj, key, val, len); \
return true; \
} \
} \
return false
yyjson_api_inline bool yyjson_mut_obj_add_null(yyjson_mut_doc *doc,
yyjson_mut_val *obj,
const char *_key) {
yyjson_mut_obj_add_func({
val->tag = YYJSON_TYPE_NULL | YYJSON_SUBTYPE_NONE;
});
}
yyjson_api_inline bool yyjson_mut_obj_add_true(yyjson_mut_doc *doc,
yyjson_mut_val *obj,
const char *_key) {
yyjson_mut_obj_add_func({
val->tag = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_TRUE;
});
}
yyjson_api_inline bool yyjson_mut_obj_add_false(yyjson_mut_doc *doc,
yyjson_mut_val *obj,
const char *_key) {
yyjson_mut_obj_add_func({
val->tag = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_FALSE;
});
}
yyjson_api_inline bool yyjson_mut_obj_add_bool(yyjson_mut_doc *doc,
yyjson_mut_val *obj,
const char *_key,
bool _val) {
yyjson_mut_obj_add_func({
val->tag = YYJSON_TYPE_BOOL | (uint8_t)((uint8_t)(_val) << 3);
});
}
yyjson_api_inline bool yyjson_mut_obj_add_uint(yyjson_mut_doc *doc,
yyjson_mut_val *obj,
const char *_key,
uint64_t _val) {
yyjson_mut_obj_add_func({
val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT;
val->uni.u64 = _val;
});
}
yyjson_api_inline bool yyjson_mut_obj_add_sint(yyjson_mut_doc *doc,
yyjson_mut_val *obj,
const char *_key,
int64_t _val) {
yyjson_mut_obj_add_func({
val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT;
val->uni.i64 = _val;
});
}
yyjson_api_inline bool yyjson_mut_obj_add_int(yyjson_mut_doc *doc,
yyjson_mut_val *obj,
const char *_key,
int64_t _val) {
yyjson_mut_obj_add_func({
val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT;
val->uni.i64 = _val;
});
}
yyjson_api_inline bool yyjson_mut_obj_add_real(yyjson_mut_doc *doc,
yyjson_mut_val *obj,
const char *_key,
double _val) {
yyjson_mut_obj_add_func({
val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL;
val->uni.f64 = _val;
});
}
yyjson_api_inline bool yyjson_mut_obj_add_str(yyjson_mut_doc *doc,
yyjson_mut_val *obj,
const char *_key,
const char *_val) {
if (yyjson_unlikely(!_val)) return false;
yyjson_mut_obj_add_func({
val->tag = ((uint64_t)strlen(_val) << YYJSON_TAG_BIT) | YYJSON_TYPE_STR;
val->uni.str = _val;
});
}
yyjson_api_inline bool yyjson_mut_obj_add_strn(yyjson_mut_doc *doc,
yyjson_mut_val *obj,
const char *_key,
const char *_val,
size_t _len) {
if (yyjson_unlikely(!_val)) return false;
yyjson_mut_obj_add_func({
val->tag = ((uint64_t)_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR;
val->uni.str = _val;
});
}
yyjson_api_inline bool yyjson_mut_obj_add_strcpy(yyjson_mut_doc *doc,
yyjson_mut_val *obj,
const char *_key,
const char *_val) {
if (yyjson_unlikely(!_val)) return false;
yyjson_mut_obj_add_func({
size_t _len = strlen(_val);
val->uni.str = unsafe_yyjson_mut_strncpy(doc, _val, _len);
if (yyjson_unlikely(!val->uni.str)) return false;
val->tag = ((uint64_t)_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR;
});
}
yyjson_api_inline bool yyjson_mut_obj_add_strncpy(yyjson_mut_doc *doc,
yyjson_mut_val *obj,
const char *_key,
const char *_val,
size_t _len) {
if (yyjson_unlikely(!_val)) return false;
yyjson_mut_obj_add_func({
val->uni.str = unsafe_yyjson_mut_strncpy(doc, _val, _len);
if (yyjson_unlikely(!val->uni.str)) return false;
val->tag = ((uint64_t)_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR;
});
}
yyjson_api_inline bool yyjson_mut_obj_add_val(yyjson_mut_doc *doc,
yyjson_mut_val *obj,
const char *_key,
yyjson_mut_val *_val) {
if (yyjson_unlikely(!_val)) return false;
yyjson_mut_obj_add_func({
val = _val;
});
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_str(yyjson_mut_val *obj,
const char *key) {
return yyjson_mut_obj_remove_strn(obj, key, key ? strlen(key) : 0);
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_strn(
yyjson_mut_val *obj,
const char *_key,
size_t _len) {
if (yyjson_likely(yyjson_mut_is_obj(obj) && _key)) {
yyjson_mut_val *key;
yyjson_mut_obj_iter iter;
yyjson_mut_val *val_removed = NULL;
yyjson_mut_obj_iter_init(obj, &iter);
while ((key = yyjson_mut_obj_iter_next(&iter)) != NULL) {
if (unsafe_yyjson_get_len(key) == _len &&
memcmp(key->uni.str, _key, _len) == 0) {
if (!val_removed) val_removed = key->next;
yyjson_mut_obj_iter_remove(&iter);
}
}
return val_removed;
}
return NULL;
}
/*==============================================================================
* JSON Pointer API (Implementation)
*============================================================================*/
yyjson_api yyjson_val *unsafe_yyjson_get_pointer(yyjson_val *val,
const char *ptr,
size_t len);
yyjson_api yyjson_mut_val *unsafe_yyjson_mut_get_pointer(yyjson_mut_val *val,
const char *ptr,
size_t len);
yyjson_api_inline yyjson_val *yyjson_get_pointer(yyjson_val *val,
const char *ptr) {
if (val && ptr) {
if (*ptr == '\0') return val;
if (*ptr != '/') return NULL;
return unsafe_yyjson_get_pointer(val, ptr, strlen(ptr));
}
return NULL;
}
yyjson_api_inline yyjson_val *yyjson_doc_get_pointer(yyjson_doc *doc,
const char *ptr) {
if (doc) return yyjson_get_pointer(doc->root, ptr);
return NULL;
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_get_pointer(yyjson_mut_val *val,
const char *ptr) {
if (val && ptr) {
if (*ptr == '\0') return val;
if (*ptr != '/') return NULL;
return unsafe_yyjson_mut_get_pointer(val, ptr, strlen(ptr));
}
return NULL;
}
yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_get_pointer(
yyjson_mut_doc *doc, const char *ptr) {
if (doc) return yyjson_mut_get_pointer(doc->root, ptr);
return NULL;
}
/*==============================================================================
* Compiler Hint End
*============================================================================*/
#if defined(__clang__)
# pragma clang diagnostic pop
#elif defined(__GNUC__)
# if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
# pragma GCC diagnostic pop
# endif
#elif defined(_MSC_VER)
# pragma warning(pop)
#endif /* warning suppress end */
#ifdef __cplusplus
}
#endif /* extern "C" end */
#endif /* YYJSON_H */
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/chenMr-Errr113/wheel.git
git@gitee.com:chenMr-Errr113/wheel.git
chenMr-Errr113
wheel
轮子
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385