Serialise Lua data into JSON string. */
| 658 | |
| 659 | /* Serialise Lua data into JSON string. */ |
| 660 | static void json_append_data(lua_State *l, json_config_t *cfg, |
| 661 | int current_depth, strbuf_t *json) |
| 662 | { |
| 663 | int len; |
| 664 | |
| 665 | switch (lua_type(l, -1)) { |
| 666 | case LUA_TSTRING: |
| 667 | json_append_string(l, json, -1); |
| 668 | break; |
| 669 | case LUA_TNUMBER: |
| 670 | json_append_number(l, cfg, json, -1); |
| 671 | break; |
| 672 | case LUA_TBOOLEAN: |
| 673 | if (lua_toboolean(l, -1)) |
| 674 | strbuf_append_mem(json, "true", 4); |
| 675 | else |
| 676 | strbuf_append_mem(json, "false", 5); |
| 677 | break; |
| 678 | case LUA_TTABLE: |
| 679 | current_depth++; |
| 680 | json_check_encode_depth(l, cfg, current_depth, json); |
| 681 | len = lua_array_length(l, cfg, json); |
| 682 | if (len > 0) |
| 683 | json_append_array(l, cfg, current_depth, json, len); |
| 684 | else |
| 685 | json_append_object(l, cfg, current_depth, json); |
| 686 | break; |
| 687 | case LUA_TNIL: |
| 688 | strbuf_append_mem(json, "null", 4); |
| 689 | break; |
| 690 | case LUA_TLIGHTUSERDATA: |
| 691 | if (lua_touserdata(l, -1) == NULL) { |
| 692 | strbuf_append_mem(json, "null", 4); |
| 693 | break; |
| 694 | } |
| 695 | default: |
| 696 | /* Remaining types (LUA_TFUNCTION, LUA_TUSERDATA, LUA_TTHREAD, |
| 697 | * and LUA_TLIGHTUSERDATA) cannot be serialised */ |
| 698 | json_encode_exception(l, cfg, json, -1, "type not supported"); |
| 699 | /* never returns */ |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | static int json_encode(lua_State *l) |
| 704 | { |
no test coverage detected