#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) {
|
clock_t start_time = clock();
|
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();
|
//ÊÍ·Å×ÊÔ´
|
//jsonWriter.reset();
|
//cout << "jsonת×Ö·û´®ºÄʱ:"<< clock()- start_time << endl;
|
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);
|
string st = "{";
|
st.append("\"type\":0,");
|
st.append("\"client\":").append(to_string(clientID)).append(",");
|
st.append("\"data\":{");
|
st.append("\"channel\":").append(to_string(channel)).append(",");
|
st.append("\"code\":").append("\"").append(code).append("\"").append(",");
|
st.append("\"captureTime\":").append(to_string(captureTime)).append(",");
|
st.append("\"processTime\":").append(to_string(channel)).append(",");
|
st.append("\"data\":[");
|
int index = 0;
|
for (std::list<TradeData*>::iterator ele = dataList.begin(); ele != dataList.end(); ele++) {
|
index++;
|
st.append("{");
|
st.append("\"time\":").append("\"").append((*ele)->time).append("\"").append(",");
|
st.append("\"price\":").append("\"").append((*ele)->price).append("\"").append(",");
|
st.append("\"num\":").append(to_string((*ele)->num)).append(",");
|
st.append("\"limitPrice\":").append(to_string((*ele)->limitPrice)).append(",");
|
st.append("\"operateType\":").append(to_string((*ele)->operateType)).append(",");
|
st.append("\"cancelTime\":").append(to_string((*ele)->cancelTime)).append(",");
|
st.append("\"cancelTimeUnit\":").append(to_string((*ele)->cancelTimeUnit));
|
st.append("}");
|
if (index != dataList.size()) {
|
st.append(",");
|
}
|
}
|
st.append("]");
|
st.append("}}");
|
return st;
|
}
|
|
|
static std::string loadTradeQueueData(int clientID, int channel, string code, L2TradeQueue tradeQueue) {
|
Json::Value data;
|
data["channel"] = channel;
|
data["code"] = code;
|
|
Json::Value _data;
|
_data["buyOnePrice"] = tradeQueue.buyOnePrice;
|
_data["buyOneVolumn"] = tradeQueue.buyOneVolumn;
|
_data["buyTime"] = tradeQueue.buyTime;
|
_data["sellOnePrice"] = tradeQueue.sellOnePrice;
|
_data["sellOneVolumn"] = tradeQueue.sellOneVolumn;
|
_data["sellTime"] = tradeQueue.sellTime;
|
Json::Value buyQueue;
|
int index = 0;
|
for (list<string>::iterator ele = tradeQueue.buyQueue.begin(); ele != tradeQueue.buyQueue.end(); ++ele) {
|
buyQueue[index++] = atoi((*ele).c_str());
|
}
|
_data["buyQueue"] = buyQueue;
|
|
|
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 loadFirstLimitUpData(list<FirstLimitUpCodeData> dataList) {
|
Json::Value root;
|
root["type"] = 22;
|
|
|
Json::Value data;
|
|
std::list<FirstLimitUpCodeData>::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["zyltMoney"] = (*ele).zyltMoney;
|
item["zyltMoneyUnit"] = (*ele).zyltMoneyUnit;
|
item["volume"] = (*ele).volume;
|
item["volumeUnit"] = (*ele).volumeUnit;
|
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["apply_time"] = (*ele).apply_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,bool add) {
|
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;
|
root["add"] = add;
|
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);
|
}
|
|
|
// ×Ô¶¯Æô¶¯L2³É¹¦
|
static std::string loadAutoStartL2(int client) {
|
Json::Value root;
|
root["type"] = 60;
|
Json::Value data;
|
data["client"] = client;
|
root["data"] = data;
|
return toJsonStr(root);
|
}
|
// ocrʶ±ð
|
static std::string loadOcrData(int client,cv::Mat mat,string key) {
|
Json::Value data;
|
Json::Value array;
|
int len = mat.rows * mat.cols;
|
for (int i = 0; i < len; i++) {
|
Json::Value d;
|
d[0] = mat.data[i];
|
array[i] = d;
|
}
|
data["data"] = array;
|
data["width"] = mat.cols;
|
data["height"] = mat.rows;
|
data["key"] = key;
|
Json::Value root;
|
root["data"] = data;
|
root["type"] = 100;
|
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;
|
}
|
|
|
|
|
};
|