| 729 | } |
| 730 | |
| 731 | static int json_encode(lua_State *l) |
| 732 | { |
| 733 | json_config_t *cfg = json_fetch_config(l); |
| 734 | strbuf_t local_encode_buf; |
| 735 | strbuf_t *encode_buf; |
| 736 | char *json; |
| 737 | int len; |
| 738 | |
| 739 | luaL_argcheck(l, lua_gettop(l) == 1, 1, "expected 1 argument"); |
| 740 | |
| 741 | if (!cfg->encode_keep_buffer) { |
| 742 | /* Use private buffer */ |
| 743 | encode_buf = &local_encode_buf; |
| 744 | strbuf_init(encode_buf, 0); |
| 745 | } else { |
| 746 | /* Reuse existing buffer */ |
| 747 | encode_buf = &cfg->encode_buf; |
| 748 | strbuf_reset(encode_buf); |
| 749 | } |
| 750 | |
| 751 | json_append_data(l, cfg, 0, encode_buf); |
| 752 | json = strbuf_string(encode_buf, &len); |
| 753 | |
| 754 | lua_pushlstring(l, json, len); |
| 755 | |
| 756 | if (!cfg->encode_keep_buffer) |
| 757 | strbuf_free(encode_buf); |
| 758 | |
| 759 | return 1; |
| 760 | } |
| 761 | |
| 762 | /* ===== DECODING ===== */ |
| 763 |
nothing calls this directly
no test coverage detected