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