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; }
/* json_append_array args: * - lua_State * - JSON strbuf * - Size of passwd Lua array (top of stack) */ static void json_append_array(lua_State *l, json_config_t *cfg, strbuf_t *json, int array_length) { int comma, i; json_encode_descend(l, cfg); strbuf_append_char(json, '['); comma = 0; for (i = 1; i <= array_length; i++) { if (comma) strbuf_append_char(json, ','); else comma = 1; lua_rawgeti(l, -1, i); json_append_data(l, cfg, json); lua_pop(l, 1); } strbuf_append_char(json, ']'); cfg->current_depth--; }
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; }
static void json_append_object(lua_State *l, json_config_t *cfg, strbuf_t *json) { int comma, keytype; json_encode_descend(l, cfg); /* Object */ strbuf_append_char(json, '{'); lua_pushnil(l); /* table, startkey */ comma = 0; while (lua_next(l, -2) != 0) { if (comma) strbuf_append_char(json, ','); else comma = 1; /* table, key, value */ keytype = lua_type(l, -2); if (keytype == LUA_TNUMBER) { strbuf_append_char(json, '"'); json_append_number(l, json, -2, cfg); strbuf_append_mem(json, "\":", 2); } else if (keytype == LUA_TSTRING) { json_append_string(l, json, -2); strbuf_append_char(json, ':'); } else { json_encode_exception(l, cfg, -2, "table key must be a number or string"); /* never returns */ } /* table, key, value */ json_append_data(l, cfg, json); lua_pop(l, 1); /* table, key */ } strbuf_append_char(json, '}'); cfg->current_depth--; }
static void json_append_object(lua_State *l, struct luaL_serializer *cfg, int current_depth, strbuf_t *json) { int comma; /* Object */ strbuf_append_char(json, '{'); lua_pushnil(l); /* table, startkey */ comma = 0; while (lua_next(l, -2) != 0) { if (comma) strbuf_append_char(json, ','); else comma = 1; struct luaL_field field; luaL_checkfield(l, cfg, -2, &field); if (field.type == MP_UINT) { strbuf_append_char(json, '"'); json_append_uint(cfg, json, field.ival); strbuf_append_mem(json, "\":", 2); } else if (field.type == MP_INT) { strbuf_append_char(json, '"'); json_append_int(cfg, json, field.ival); strbuf_append_mem(json, "\":", 2); } else if (field.type == MP_STR) { json_append_string(cfg, json, field.sval.data, field.sval.len); strbuf_append_char(json, ':'); } else { luaL_error(l, "table key must be a number or string"); } /* table, key, value */ json_append_data(l, cfg, current_depth + 1, json); lua_pop(l, 1); /* table, key */ } strbuf_append_char(json, '}'); }
/* json_append_array args: * - lua_State * - JSON strbuf * - Size of passwd Lua array (top of stack) */ static void json_append_array(lua_State *l, struct luaL_serializer *cfg, int current_depth, strbuf_t *json, int array_length) { int comma, i; strbuf_append_char(json, '['); comma = 0; for (i = 1; i <= array_length; i++) { if (comma) strbuf_append_char(json, ','); else comma = 1; lua_rawgeti(l, -1, i); json_append_data(l, cfg, current_depth, json); lua_pop(l, 1); } strbuf_append_char(json, ']'); }