* 把map转成json写入字符串流 * @param map 源map * @param ss 要写入的字符串流 * @param is_array 是否作为一个array来进行json转换(因为lua的table中可能包含array和hashmap两部分) */
| 2682 | * @param is_array 是否作为一个array来进行json转换(因为lua的table中可能包含array和hashmap两部分) |
| 2683 | */ |
| 2684 | static void luatablemap_to_json_stream(GluaTableMapP map, std::stringstream &ss, bool is_array=false) |
| 2685 | { |
| 2686 | if(!is_array) |
| 2687 | { |
| 2688 | // 检查是否是数组 |
| 2689 | std::vector<int> all_int_keys; |
| 2690 | bool has_wrong_array_format = false; |
| 2691 | for(const auto &p : *map) |
| 2692 | { |
| 2693 | std::string key(p.first); |
| 2694 | if(key.length()<1) |
| 2695 | { |
| 2696 | has_wrong_array_format = true; |
| 2697 | break; |
| 2698 | } |
| 2699 | int int_key = 0; |
| 2700 | if(key=="0") |
| 2701 | { |
| 2702 | int_key = 0; |
| 2703 | } |
| 2704 | else |
| 2705 | { |
| 2706 | try |
| 2707 | { |
| 2708 | int_key = std::stoi(key); |
| 2709 | if (int_key == 0) |
| 2710 | { |
| 2711 | has_wrong_array_format = true; |
| 2712 | break; |
| 2713 | } |
| 2714 | }catch(std::exception e) |
| 2715 | { |
| 2716 | has_wrong_array_format = true; |
| 2717 | break; |
| 2718 | } |
| 2719 | } |
| 2720 | all_int_keys.push_back(int_key); |
| 2721 | } |
| 2722 | if(!has_wrong_array_format) |
| 2723 | { |
| 2724 | std::sort(all_int_keys.begin(), all_int_keys.end()); |
| 2725 | for (int i = 1; i <= all_int_keys.size(); ++i) |
| 2726 | { |
| 2727 | if (i != all_int_keys[i-1]) |
| 2728 | { |
| 2729 | has_wrong_array_format = true; |
| 2730 | break; |
| 2731 | } |
| 2732 | } |
| 2733 | if (!has_wrong_array_format) |
| 2734 | { |
| 2735 | is_array = true; |
| 2736 | } |
| 2737 | } |
| 2738 | } |
| 2739 | |
| 2740 | if (is_array) |
| 2741 | ss << "["; |
no test coverage detected