| 701 | } |
| 702 | |
| 703 | static int json_encode(lua_State *l) |
| 704 | { |
| 705 | json_config_t *cfg = json_fetch_config(l); |
| 706 | strbuf_t local_encode_buf; |
| 707 | strbuf_t *encode_buf; |
| 708 | char *json; |
| 709 | int len; |
| 710 | |
| 711 | luaL_argcheck(l, lua_gettop(l) == 1, 1, "expected 1 argument"); |
| 712 | |
| 713 | if (!cfg->encode_keep_buffer) { |
| 714 | /* Use private buffer */ |
| 715 | encode_buf = &local_encode_buf; |
| 716 | strbuf_init(encode_buf, 0); |
| 717 | } else { |
| 718 | /* Reuse existing buffer */ |
| 719 | encode_buf = &cfg->encode_buf; |
| 720 | strbuf_reset(encode_buf); |
| 721 | } |
| 722 | |
| 723 | json_append_data(l, cfg, 0, encode_buf); |
| 724 | json = strbuf_string(encode_buf, &len); |
| 725 | |
| 726 | lua_pushlstring(l, json, len); |
| 727 | |
| 728 | if (!cfg->encode_keep_buffer) |
| 729 | strbuf_free(encode_buf); |
| 730 | |
| 731 | return 1; |
| 732 | } |
| 733 | |
| 734 | /* ===== DECODING ===== */ |
| 735 |
nothing calls this directly
no test coverage detected