admin
2022-06-30 a17738e1545ff7dbef6398b8ec1eab93ab59c9a1
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
#pragma once
#include <string>
#include <list>
#include "json/json.h"
#include <THSActionUtil.h>
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;
        root[index++] = item;
    }   
    return root;
}
 
//json¶ÔÏóתΪ×Ö·û´®
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;
}
 
std::string loadData(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);
}
 
Json::Value parseJson(string data) {
    Json::Value root;
    Json::Reader reader;
    reader.parse(data, root);
    return root;
}