static int json_encode(lua_State *l) { json_config_t *cfg = json_fetch_config(l); strbuf_t local_encode_buf; strbuf_t *encode_buf; char *json; int len; luaL_argcheck(l, lua_gettop(l) == 1, 1, "expected 1 argument"); if (!cfg->encode_keep_buffer) { /* Use private buffer */ encode_buf = &local_encode_buf; strbuf_init(encode_buf, 0); } else { /* Reuse existing buffer */ encode_buf = &cfg->encode_buf; strbuf_reset(encode_buf); } json_append_data(l, cfg, 0, encode_buf); json = strbuf_string(encode_buf, &len); lua_pushlstring(l, json, len); if (!cfg->encode_keep_buffer) strbuf_free(encode_buf); return 1; }
static int json_encode(lua_State *l) { json_config_t *cfg; char *json; int len; /* Can't use json_verify_arg_count() since we need to ensure * there is only 1 argument */ luaL_argcheck(l, lua_gettop(l) == 1, 1, "expected 1 argument"); cfg = json_fetch_config(l); cfg->current_depth = 0; /* Reset the persistent buffer if it exists. * Otherwise allocate a new buffer. */ if (strbuf_allocated(&cfg->encode_buf)) strbuf_reset(&cfg->encode_buf); else strbuf_init(&cfg->encode_buf, 0); json_append_data(l, cfg, &cfg->encode_buf); json = strbuf_string(&cfg->encode_buf, &len); lua_pushlstring(l, json, len); if (!cfg->encode_keep_buffer) strbuf_free(&cfg->encode_buf); return 1; }
static int json_encode(lua_State *l) { struct luaL_serializer *cfg = luaL_checkserializer(l); char *json; int len; luaL_argcheck(l, lua_gettop(l) == 1, 1, "expected 1 argument"); /* Reuse existing buffer */ strbuf_reset(&encode_buf); json_append_data(l, cfg, 0, &encode_buf); json = strbuf_string(&encode_buf, &len); lua_pushlstring(l, json, len); return 1; }