/* * @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 #include #include #include #include #include #include #include "NettyDataDefine.h" using namespace std; class SendCarData { public: // 列车编号 std::string Train_Number; list datas; public: SendCarData(){}; ~SendCarData(){}; /** * 对象序列化为Json 对外接口 * @param {std::string& str} 需要转换的数据 * @return {*} */ void objectToJson(std::string& str) { rapidjson::StringBuffer strBuf; rapidjson::Writer writer(strBuf); this->objectToJson(writer); str = strBuf.GetString(); } /** * 对象序列化为Json * @param {rapidjson::Writer& writer} * @return {*} */ void objectToJson(rapidjson::Writer& writer) { writer.StartObject(); writer.Key("Train_Number"); writer.String(Train_Number.c_str()); writer.Key("CarDatas"); writer.StartArray(); list::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(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(); } } };