| 616 | } |
| 617 | |
| 618 | static void json_append_object(lua_State *l, json_config_t *cfg, |
| 619 | int current_depth, strbuf_t *json) |
| 620 | { |
| 621 | int comma, keytype; |
| 622 | |
| 623 | /* Object */ |
| 624 | strbuf_append_char(json, '{'); |
| 625 | |
| 626 | lua_pushnil(l); |
| 627 | /* table, startkey */ |
| 628 | comma = 0; |
| 629 | while (lua_next(l, -2) != 0) { |
| 630 | if (comma) |
| 631 | strbuf_append_char(json, ','); |
| 632 | else |
| 633 | comma = 1; |
| 634 | |
| 635 | /* table, key, value */ |
| 636 | keytype = lua_type(l, -2); |
| 637 | if (keytype == LUA_TNUMBER) { |
| 638 | strbuf_append_char(json, '"'); |
| 639 | json_append_number(l, cfg, json, -2); |
| 640 | strbuf_append_mem(json, "\":", 2); |
| 641 | } else if (keytype == LUA_TSTRING) { |
| 642 | json_append_string(l, json, -2); |
| 643 | strbuf_append_char(json, ':'); |
| 644 | } else { |
| 645 | json_encode_exception(l, cfg, json, -2, |
| 646 | "table key must be a number or string"); |
| 647 | /* never returns */ |
| 648 | } |
| 649 | |
| 650 | /* table, key, value */ |
| 651 | json_append_data(l, cfg, current_depth, json); |
| 652 | lua_pop(l, 1); |
| 653 | /* table, key */ |
| 654 | } |
| 655 | |
| 656 | strbuf_append_char(json, '}'); |
| 657 | } |
| 658 | |
| 659 | /* Serialise Lua data into JSON string. */ |
| 660 | static void json_append_data(lua_State *l, json_config_t *cfg, |
no test coverage detected