#pragma once
|
#include <string>
|
#include <list>
|
#include "json/json.h"
|
#include <THSActionUtil.h>
|
#include "LimitUpCapture.h"
|
#include "TradeSuccessCapture.h"
|
#include "IndustryCapture.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, 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 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["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["date"] = (*ele).date;
|
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 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 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;
|
}
|
|
|
|
|
};
|