admin
2022-10-28 086ec74e94654e92b3a1c6f42612d12ef33ff4b2
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#pragma once
#include <string>
#include <list>
#include "json/json.h"
#include <THSActionUtil.h>
#include "LimitUpCapture.h"
#include "TradeListCapture.h"
#include "IndustryCapture.h"
#include "CurrentPriceCapture.h"
#include "TaskChecker.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;
    }
 
 
 
    static  Json::Value toJson(set<string> dataList) {
        Json::Value root;
 
        set<string>::iterator ele;
        int index = 0;
        for (ele = dataList.begin();ele != dataList.end();ele++) {
            root[index++] = *ele;
        }
        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, long captureTime, long processTime, std::list<TradeData> dataList) {
        Json::Value data;
        data["channel"] = channel;
        data["code"] = code;
        data["captureTime"] = captureTime;
        data["processTime"] = processTime;
        data["data"] = toJson(dataList);
        Json::Value root;
        root["type"] = 0;
        root["client"] = clientID;
        root["data"] = data;
        return toJsonStr(root);
    }
 
 
    static  std::string loadTradeQueueData(int clientID, int channel, string code, std::list<int> numList) {
        Json::Value data;
        data["channel"] = channel;
        data["code"] = code;
 
        Json::Value _data;
 
        std::list<int>::iterator ele;
        int index = 0;
        for (ele = numList.begin();ele != numList.end();ele++) {
            _data[index++] = *ele;
        }
        data["data"] = _data;
        Json::Value root;
        root["type"] = 10;
        root["client"] = clientID;
        root["data"] = data;
        return toJsonStr(root);
    }
 
    static  std::string loadLimitUpData(list<LimitUpData>  dataList) {
        Json::Value root;
        root["type"] = 2;
        
 
        Json::Value data;
 
        std::list<LimitUpData>::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["limitMoney"] = (*ele).limitMoney;
            item["limitMoneyUnit"] = (*ele).limitMoneyUnit;
            item["limitUpPercent"] = (*ele).limitUpPercent;
            item["code"] = (*ele).code;
            data[index++] = item;
        }
 
        root["data"] = data;
 
 
        return toJsonStr(root);
    }
 
 
    static  std::string loadTradeSuccessData(list<TradeSuccessData>  dataList) {
        Json::Value root;
        root["type"] = 3;
 
 
        Json::Value data;
 
        std::list<TradeSuccessData>::iterator ele;
        int index = 0;
        for (ele = dataList.begin();ele != dataList.end();ele++) {
            Json::Value  item;
            item["price"] = (*ele).price;
            item["code"] = (*ele).code;
            item["time"] = (*ele).time;
            item["num"] = (*ele).num;
            item["money"] = (*ele).money;
            item["trade_num"] = (*ele).trade_num;
            item["type"] = (*ele).type;
            data[index++] = item;
        }
 
        root["data"] = data;
 
 
        return toJsonStr(root);
    }
 
 
    static  std::string loadTradeDelegateData(list<TradeDelegateData>  dataList) {
        Json::Value root;
        root["type"] = 5;
 
 
        Json::Value data;
 
        std::list<TradeDelegateData>::iterator ele;
        int index = 0;
        for (ele = dataList.begin();ele != dataList.end();ele++) {
            Json::Value  item;
            item["code"] = (*ele).code;
            item["time"] = (*ele).time;
            item["num"] = (*ele).num;
            item["type"] = (*ele).type;
            item["price"] = (*ele).price;
            item["trade_price"] = (*ele).trade_price;
            item["trade_num"] = (*ele).trade_num;
            data[index++] = item;
        }
 
        root["data"] = data;
 
 
        return toJsonStr(root);
    }
 
    static  std::string loadIndustryData(list<list<IndustryData>>  dataList) {
        Json::Value root;
        root["type"] = 4;
 
 
        Json::Value data;
 
        std::list<list<IndustryData>>::iterator ele;
        int index = 0;
        for (ele = dataList.begin();ele != dataList.end();ele++) {
            Json::Value  item;
 
            int cindex = 0;
            for (list<IndustryData >::iterator e = (*ele).begin();e != (*ele).end();e++) {
                Json::Value item_c;
                item_c["code"] = (*e).code;
                item_c["zyltgb"] = (*e).zyltMoney;
                item_c["zyltgb_unit"] = (*e).zyltMoneyUnit;
                item[cindex++] = item_c;
            }
            data[index++] = item;
        }
 
        root["data"] = data;
 
 
        return toJsonStr(root);
    }
 
    static  std::string loadGPPriceData(std::list<CurrentPriceData> dataList) {
        Json::Value root;
        root["type"] = 40;
        Json::Value data;
 
        std::list<CurrentPriceData>::iterator ele;
        int index = 0;
        for (ele = dataList.begin();ele != dataList.end();ele++) {
            Json::Value item;
            item["code"] = (*ele).code;
            item["price"] = (*ele).price;
            item["volumn"] = (*ele).volumn;
            item["volumnUnit"] = (*ele).volumnUnit;
            data[index++] = item;
        }
        root["data"] = data;
        return toJsonStr(root);
    }
 
 
    static  std::string loadGPCodeData(std::list<IndustryData> codeList) {
        Json::Value root;
        root["type"] = 1;
        Json::Value data;
 
        std::list<IndustryData>::iterator ele;
        int index = 0;
        for (ele = codeList.begin();ele != codeList.end();ele++) {
            Json::Value item;
            item["code"] = (*ele).code;
            item["zyltgb"] = (*ele).zyltMoney;
            item["zyltgb_unit"] = (*ele).zyltMoneyUnit;
            data[index++] = item;
        }
        root["data"] = data;
        return toJsonStr(root);
    }
 
    static  std::string loadHeartbeatData(int client,string memery,string ownMemery,bool thsDead,string thsDeadMsg) {
        Json::Value root;
        root["type"] = 30;
        Json::Value data;
        data["client"] = client;
        data["memery"] = memery;
        data["own_memery"] = ownMemery;
        data["thsDead"] = thsDead;
        data["thsDeadMsg"] = thsDeadMsg;
        root["data"] = data;
        return toJsonStr(root);
    }
 
 
 
    static  std::string loadLoginData(string account,string pwd) {
        Json::Value root;
        root["type"] = 20;
        Json::Value data;
        data["account"] = account;
        data["pwd"] = pwd;
        root["data"] = data;
        return toJsonStr(root);
    }
 
 
 
 
    static  std::string loadAvailableMoney(int client,string money) {
        Json::Value root;
        root["type"] = 6;
        Json::Value data;
        data["client"] = client;
        data["money"] = money;
        root["data"] = data;
        return toJsonStr(root);
    }
 
    static  std::string loadTradeQueue(int index, string codeName,int volumn,string price,string time) {
        Json::Value root;
        root["type"] = 50;
        Json::Value data;
        data["index"] = index;
        data["codeName"] = codeName;
        data["price"] = price;
        data["volumn"] = volumn;
        data["time"] = time;
        root["data"] = data;
        return toJsonStr(root);
    }
 
    static std::string toJson(ClientEnvState state) {
        Json::Value root;
        root["ths_l2_win"] = state.ths_l2_win;
        root["ths_fp_1"] = state.ths_fp_1;
        root["ths_fp_2"] = state.ths_fp_2;
        root["ths_trade_success"] = state.ths_trade_success;
        root["l2_channel_valid_count"] = state.l2_channel_valid_count;
        root["l2_channel_invalid_count"] = state.l2_channel_invalid_count;
        root["limitUp"] = state.limitUp;
        root["tradeSuccess"] = state.tradeSuccess;
        return toJsonStr(root);
    }
 
 
    
 
    static  Json::Value parseJson(string data) {
        Json::Value root;
        Json::Reader reader;
        reader.parse(data, root);
        return root;
    }
 
    static string jsonValue2String(Json::Value value) {
        Json::FastWriter fastWriter;
        std::string output = fastWriter.write(value);
        return output;
    }
 
 
 
 
};