| 644 | } |
| 645 | |
| 646 | static void json_append_object(lua_State *l, json_config_t *cfg, |
| 647 | int current_depth, strbuf_t *json) |
| 648 | { |
| 649 | int comma, keytype; |
| 650 | |
| 651 | /* Object */ |
| 652 | strbuf_append_char(json, '{'); |
| 653 | |
| 654 | lua_pushnil(l); |
| 655 | /* table, startkey */ |
| 656 | comma = 0; |
| 657 | while (lua_next(l, -2) != 0) { |
| 658 | if (comma) |
| 659 | strbuf_append_char(json, ','); |
| 660 | else |
| 661 | comma = 1; |
| 662 | |
| 663 | /* table, key, value */ |
| 664 | keytype = lua_type(l, -2); |
| 665 | if (keytype == LUA_TNUMBER) { |
| 666 | strbuf_append_char(json, '"'); |
| 667 | json_append_number(l, cfg, json, -2); |
| 668 | strbuf_append_mem(json, "\":", 2); |
| 669 | } else if (keytype == LUA_TSTRING) { |
| 670 | json_append_string(l, json, -2); |
| 671 | strbuf_append_char(json, ':'); |
| 672 | } else { |
| 673 | json_encode_exception(l, cfg, json, -2, |
| 674 | "table key must be a number or string"); |
| 675 | /* never returns */ |
| 676 | } |
| 677 | |
| 678 | /* table, key, value */ |
| 679 | json_append_data(l, cfg, current_depth, json); |
| 680 | lua_pop(l, 1); |
| 681 | /* table, key */ |
| 682 | } |
| 683 | |
| 684 | strbuf_append_char(json, '}'); |
| 685 | } |
| 686 | |
| 687 | /* Serialise Lua data into JSON string. */ |
| 688 | static void json_append_data(lua_State *l, json_config_t *cfg, |
no test coverage detected