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