#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;
|
}
|
|
|
|
|
};
|