Serialise Lua data into JSON string. */
| 686 | |
| 687 | /* Serialise Lua data into JSON string. */ |
| 688 | static void json_append_data(lua_State *l, json_config_t *cfg, |
| 689 | int current_depth, strbuf_t *json) |
| 690 | { |
| 691 | int len; |
| 692 | |
| 693 | switch (lua_type(l, -1)) { |
| 694 | case LUA_TSTRING: |
| 695 | json_append_string(l, json, -1); |
| 696 | break; |
| 697 | case LUA_TNUMBER: |
| 698 | json_append_number(l, cfg, json, -1); |
| 699 | break; |
| 700 | case LUA_TBOOLEAN: |
| 701 | if (lua_toboolean(l, -1)) |
| 702 | strbuf_append_mem(json, "true", 4); |
| 703 | else |
| 704 | strbuf_append_mem(json, "false", 5); |
| 705 | break; |
| 706 | case LUA_TTABLE: |
| 707 | current_depth++; |
| 708 | json_check_encode_depth(l, cfg, current_depth, json); |
| 709 | len = lua_array_length(l, cfg, json); |
| 710 | if (len > 0) |
| 711 | json_append_array(l, cfg, current_depth, json, len); |
| 712 | else |
| 713 | json_append_object(l, cfg, current_depth, json); |
| 714 | break; |
| 715 | case LUA_TNIL: |
| 716 | strbuf_append_mem(json, "null", 4); |
| 717 | break; |
| 718 | case LUA_TLIGHTUSERDATA: |
| 719 | if (lua_touserdata(l, -1) == NULL) { |
| 720 | strbuf_append_mem(json, "null", 4); |
| 721 | break; |
| 722 | } |
| 723 | default: |
| 724 | /* Remaining types (LUA_TFUNCTION, LUA_TUSERDATA, LUA_TTHREAD, |
| 725 | * and LUA_TLIGHTUSERDATA) cannot be serialised */ |
| 726 | json_encode_exception(l, cfg, json, -1, "type not supported"); |
| 727 | /* never returns */ |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | static int json_encode(lua_State *l) |
| 732 | { |
no test coverage detected