123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- /*
- * @Description:
- * @Version: 1.0
- * @Autor: lishengyin
- * @Date: 2021-10-19 14:58:18
- * @LastEditors: lishengyin
- * @LastEditTime: 2021-10-19 16:34:32
- */
- #pragma once
- #include <iostream>
- #include <list>
- #include <map>
- #include <rapidjson/document.h>
- #include <rapidjson/rapidjson.h>
- #include <rapidjson/stringbuffer.h>
- #include <rapidjson/writer.h>
- #include "NettyDataDefine.h"
- using namespace std;
- class SendCarData
- {
- public:
- // 列车编号
- std::string Train_Number;
- list<CarInfoData> datas;
- public:
- SendCarData(){};
- ~SendCarData(){};
- /**
- * 对象序列化为Json 对外接口
- * @param {std::string& str} 需要转换的数据
- * @return {*}
- */
- void objectToJson(std::string& str)
- {
- rapidjson::StringBuffer strBuf;
- rapidjson::Writer<rapidjson::StringBuffer> writer(strBuf);
- this->objectToJson(writer);
- str = strBuf.GetString();
- }
- /**
- * 对象序列化为Json
- * @param {rapidjson::Writer<rapidjson::StringBuffer>& writer}
- * @return {*}
- */
- void objectToJson(rapidjson::Writer<rapidjson::StringBuffer>& writer)
- {
- writer.StartObject();
- writer.Key("Train_Number");
- writer.String(Train_Number.c_str());
- writer.Key("CarDatas");
- writer.StartArray();
- list<CarInfoData>::iterator iter;
- for(iter = datas.begin(); iter != datas.end(); iter++)
- {
- writer.StartObject();
- writer.Key("CarId");
- writer.Int(iter->CarId);
- writer.Key("NoLoad");
- writer.Int(iter->NoLoad);
- writer.Key("DynamicLoad");
- writer.Int(iter->DynamicLoad);
- writer.Key("Grade");
- writer.Int(iter->Grade);
- writer.Key("DataTime");
- writer.String(iter->DataTime.c_str());
- writer.Key("Num");
- writer.Int(iter->Num);
- writer.EndObject();
- }
- writer.EndArray();
- writer.EndObject();
- }
- /**
- * json反序列化为对象
- * @param {const std::string& json} json数据
- * @return {bool} 是否反序列化成功
- */
- bool jsonToObject(const std::string& json)
- {
- rapidjson::Document doc;
- if (doc.Parse<rapidjson::kParseCommentsFlag>(json.c_str()).HasParseError()) {
- return false;
- }
- // get members
- const auto end = doc.MemberEnd();
- if (end == doc.FindMember("Train_Number") || !doc["Train_Number"].IsString()) {
- return false;
- } else {
- Train_Number = doc["Train_Number"].GetString();
- }
- }
- };
|