admin
2022-07-07 30f434d78b58e3a4198cf5ba5a9e5a0ce1cd5292
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#pragma once
#include <string>
#include <list>
#include "json/json.h"
#include <THSActionUtil.h>
 
class JsonUtil {
 
public:
 
    static  Json::Value toJson(std::list<TradeData> dataList) {
        Json::Value root;
 
        std::list<TradeData>::iterator ele;
        int index = 0;
        for (ele = dataList.begin();ele != dataList.end();ele++) {
            Json::Value  item;
            item["time"] = (*ele).time;
            item["price"] = (*ele).price;
            item["num"] = (*ele).num;
            item["limitPrice"] = (*ele).limitPrice;
            item["operateType"] = (*ele).operateType;
            item["cancelTime"] = (*ele).cancelTime;
            item["cancelTimeUnit"] = (*ele).cancelTimeUnit;
            root[index++] = item;
        }
        return root;
    }
 
    //json¶ÔÏóתΪ×Ö·û´®
    static  std::string toJsonStr(Json::Value json) {
        Json::StreamWriterBuilder writerBuilder;
        //²»×Ô¶¯»»ÐÐ
        writerBuilder.settings_["indentation"] = "";
        std::ostringstream os;
        std::unique_ptr<Json::StreamWriter> jsonWriter(writerBuilder.newStreamWriter());
        jsonWriter->write(json, &os);
        string jsonStr = os.str();
        return jsonStr;
    }
 
    static  std::string loadL2Data(int clientID, int channel, string code, std::list<TradeData> dataList) {
        Json::Value data;
        data["channel"] = channel;
        data["code"] = code;
        data["data"] = toJson(dataList);
        Json::Value root;
        root["type"] = 0;
        root["client"] = clientID;
        root["data"] = data;
        return toJsonStr(root);
    }
 
 
    static  std::string loadGPCodeData(std::list<string> codeList) {
        Json::Value root;
        root["type"] = 1;
        Json::Value data;
 
        std::list<string>::iterator ele;
        int index = 0;
        for (ele = codeList.begin();ele != codeList.end();ele++) {
            data[index++] = *ele;
        }
        root["data"] = data;
        return toJsonStr(root);
    }
 
    static  Json::Value parseJson(string data) {
        Json::Value root;
        Json::Reader reader;
        reader.parse(data, root);
        return root;
    }
 
 
 
 
};