Pb与结构体互转

#pragma once
enum class EPackType
{
reqPackType = 0,
rspPackType = 1,
pubPackType = 9,
rpzmqPackType = 88, // 回报数据不走发布包,zmq发送
notFoundPackType = 99, // 总线找不到服务应答回来统一修改当前类型处理
};

struct AtxHead {
EPackType packType; //包类型
int dataType; //包体类型
int zone; //分片
int local; //本地服务标志
long long packId; //包编号
char destination[64]; //发送目标
int itemByte; //结构体大小
int itemCount;//结构体个数
long long refValue; //自定义异步唯一标识
int retCode; //结果值
char retMsg[256]; //结果消息
void* data;
~AtxHead() { if (data != nullptr) { delete[](unsigned char*)data; data = nullptr; } }
};

#pragma once
class PbData
{
public:
PbData() : m_pBuff(nullptr), m_buffLen(0)
{
}

inline void SetBuffSize(unsigned int len)
{
    m_contentLen = len;
    if (len > m_buffLen)
    {
        if (m_buffLen > 0 && m_pBuff != nullptr)
        {
            delete[] m_pBuff;
        }
        m_pBuff = new unsigned char[len];
        m_buffLen = len;
    }
    
}

inline unsigned int GetContentLen()
{
    return m_contentLen;
}

inline unsigned int GetBuffSize()
{
    return m_buffLen;
}

inline unsigned char* GetBuffData()
{
    return m_pBuff;
}

~PbData()
{
    if (m_pBuff != nullptr)
    {
        delete[] m_pBuff;
    }
    m_pBuff = nullptr;
    m_buffLen = 0;
}

private:
unsigned char * m_pBuff;
unsigned int m_buffLen;
unsigned int m_contentLen;
};

3.2序列化与反序列化

序列化:
PlatOrderCreate obj;
AtxPlatOrderCreate p = (AtxPlatOrderCreate)atxHead.data;
obj.set_sysquoteid(p->sysQuoteId);
obj.set_clorderid(p->clOrderId);
obj.set_sysorderid(p->sysOrderId);
obj.set_symbol(p->symbol);
obj.set_markettype((int)p->marketType);
obj.set_orderqty(p->orderQty);
obj.set_ordertype((int)p->orderType);
obj.set_orderpx(p->orderPx);
obj.set_side((int)p->side);
obj.set_openclose((int)p->openClose);
obj.set_transactime(p->transacTime);
obj.set_securitytype((int)p->securityType);
obj.set_algoid(p->algoId);
pbData.SetBuffSize(obj.ByteSize());
obj.SerializeToArray((void*)(pbData.GetBuffData()), pbData.GetContentLen());

反序列化:
atxHead.itemByte = sizeof(AtxPlatOrderCreate);
PlatOrderCreate obj;
obj.ParseFromArray((const void*)pStr, len);
atxHead.itemCount = 1;
atxHead.data = new unsigned char[atxHead.itemByte];
AtxPlatOrderCreate p = (AtxPlatOrderCreate)atxHead.data;
memset(p, 0, sizeof(AtxPlatOrderCreate));
p->sysQuoteId = obj.sysquoteid();
strncpy(p->clOrderId, obj.clorderid().c_str(), __min(sizeof(p->clOrderId) - 1, obj.clorderid().length()));
p->sysOrderId = obj.sysorderid();
strncpy(p->symbol, obj.symbol().c_str(), __min(sizeof(p->symbol) - 1, obj.symbol().length()));
p->marketType = (AtxMarketType)obj.markettype();
p->orderQty = obj.orderqty();
p->orderType = (AtxOrderPriceType)obj.ordertype();
p->orderPx = obj.orderpx();
p->side = (AtxSide)obj.side();
p->openClose = (AtxPositionEffectType)obj.openclose();
p->transacTime = obj.transactime();
p->securityType = (AtxSecurityType)obj.securitytype();
p->algoId = obj.algoid();

3.2.1Pb与结构体互转
1.protobuf 转 struct
// @brief : protobuf 转 struct
// @function : AlgoMsgPbParse::ProtoBuf2Struct
// @param : const char * pStr
// @param : int len
// @param : AtxHead & atxHead
// @return : void
// @retval :
// @remark : 1-请求包,2-应答包,9-主推包
// @date : 2020/12/11
static void ProtoBuf2Struct(const char* pStr, int len, AtxHead& atxHead);

2.struct 转 protobuf
// @brief : struct 转 protobuf
// @function : AtxAlgoParse::AlgoMsgPbParse::Struct2ProtoBuf
// @param : const AtxHead & atxHead
// @param : PbData & pbData
// @return : int
// @retval :
// @remark :
// @date : 2020/12/11
static int Struct2ProtoBuf(const AtxHead& atxHead, PbData& pbData);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C语言中没有原生支持JSON的数据类型和操作,但是可以通过第三方库实现C结构体和JSON的互转。以下是使用cJSON库实现C结构体和JSON互转的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include "cJSON.h" typedef struct { int id; char name[20]; double score; } Student; // 将C结构体转换为JSON字符串 char* struct_to_json(Student* stu) { cJSON* root = cJSON_CreateObject(); cJSON_AddNumberToObject(root, "id", stu->id); cJSON_AddStringToObject(root, "name", stu->name); cJSON_AddNumberToObject(root, "score", stu->score); char* json_str = cJSON_Print(root); cJSON_Delete(root); return json_str; } // 将JSON字符串转换为C结构体 Student* json_to_struct(const char* json_str) { cJSON* root = cJSON_Parse(json_str); Student* stu = (Student*)malloc(sizeof(Student)); stu->id = cJSON_GetObjectItem(root, "id")->valueint; strcpy(stu->name, cJSON_GetObjectItem(root, "name")->valuestring); stu->score = cJSON_GetObjectItem(root, "score")->valuedouble; cJSON_Delete(root); return stu; } int main() { // 创建一个C结构体 Student stu = { 1, "Tom", 90.5 }; // 将C结构体转换为JSON字符串 char* json_str = struct_to_json(&stu); printf("JSON string: %s\n", json_str); // 将JSON字符串转换为C结构体 Student* new_stu = json_to_struct(json_str); printf("ID: %d, Name: %s, Score: %.1f\n", new_stu->id, new_stu->name, new_stu->score); // 释放内存 free(json_str); free(new_stu); return 0; } ``` 运行结果: ``` JSON string: {"id":1,"name":"Tom","score":90.5} ID: 1, Name: Tom, Score: 90.5 ``` 注意,在使用cJSON库时,需要在代码中引用cJSON.h头文件,并且需要在编译时链接cJSON库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

R助手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值