added the ctx parameter to the json write callback

This commit is contained in:
hyung-hwan 2020-06-01 08:52:43 +00:00
parent 25e3744571
commit 775a9b4735
3 changed files with 11 additions and 8 deletions

View File

@ -90,7 +90,7 @@ static int on_json_inst (mio_json_t* json, mio_json_inst_t inst, mio_oow_t level
return 0;
}
static int write_json_element (mio_jsonwr_t* jsonwr, const mio_bch_t* dptr, mio_oow_t dlen)
static int write_json_element (mio_jsonwr_t* jsonwr, const mio_bch_t* dptr, mio_oow_t dlen, void* ctx)
{
write (1, dptr, dlen);
return 0;
@ -146,7 +146,7 @@ int main (int argc, char* argv[])
jsonwr = mio_jsonwr_open (mio, 0);
mio_jsonwr_setwritecb (jsonwr, write_json_element);
mio_jsonwr_setwritecb (jsonwr, write_json_element, MIO_NULL);
mio_jsonwr_startarray (jsonwr);

View File

@ -815,7 +815,7 @@ void mio_jsonwr_close (mio_jsonwr_t* jsonwr)
mio_freemem (jsonwr->mio, jsonwr);
}
static int write_nothing (mio_jsonwr_t* jsonwr, const mio_bch_t* dptr, mio_oow_t dlen)
static int write_nothing (mio_jsonwr_t* jsonwr, const mio_bch_t* dptr, mio_oow_t dlen, void* ctx)
{
return 0;
}
@ -837,7 +837,7 @@ static int flush_wbuf (mio_jsonwr_t* jsonwr)
{
int ret = 0;
if (jsonwr->writecb(jsonwr, jsonwr->wbuf, jsonwr->wbuf_len) <= -1) ret = -1;
if (jsonwr->writecb(jsonwr, jsonwr->wbuf, jsonwr->wbuf_len, jsonwr->wctx) <= -1) ret = -1;
jsonwr->wbuf_len = 0; /* reset the buffer length regardless of writing result */
return ret;
@ -851,9 +851,10 @@ void mio_jsonwr_fini (mio_jsonwr_t* jsonwr)
/* ========================================================================= */
void mio_jsonwr_setwritecb (mio_jsonwr_t* jsonwr, mio_jsonwr_writecb_t writecb)
void mio_jsonwr_setwritecb (mio_jsonwr_t* jsonwr, mio_jsonwr_writecb_t writecb, void* ctx)
{
jsonwr->writecb = writecb;
jsonwr->wctx = ctx;
}
static int escape_char (mio_uch_t uch, mio_uch_t* xch)

View File

@ -133,9 +133,9 @@ typedef struct mio_jsonwr_t mio_jsonwr_t;
typedef int (*mio_jsonwr_writecb_t) (
mio_jsonwr_t* jsonwr,
/*mio_oow_t level,*/
const mio_bch_t* dptr,
mio_oow_t dlen
mio_oow_t dlen,
void* ctx
);
typedef struct mio_jsonwr_state_node_t mio_jsonwr_state_node_t;
@ -156,6 +156,7 @@ struct mio_jsonwr_t
mio_jsonwr_state_node_t* state_stack;
int pretty;
void* wctx;
mio_bch_t wbuf[8192];
mio_oow_t wbuf_len;
};
@ -265,7 +266,8 @@ static MIO_INLINE void* mio_jsonwr_getxtn (mio_jsonwr_t* jsonwr) { return (void*
MIO_EXPORT void mio_jsonwr_setwritecb (
mio_jsonwr_t* jsonwr,
mio_jsonwr_writecb_t writecb
mio_jsonwr_writecb_t writecb,
void* ctx
);
MIO_EXPORT int mio_jsonwr_write (