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