C语言实现序列化与反序列化

C语言实现序列化与反序列化

特点

  • 除去繁琐的上下位机交互的代码编写
  • 结构体不需要特殊字节对齐
  • 只需要一个接口函数即可实现数据的正反序列化
  • 数据可直接存储为二进制文件或直接存储进flash等,方便了软件配置模块数据保存

已支持功能

  • 支持 1字节、2字节、4字节、8字节长度的基本数据类型的正反序列化
  • 支持数组正反序列化(支持不定长)
  • 支持指针数据正反序列化
  • 定长字符数组正反序列化(支持不定长)

例子

#include "libframeoperate.h"

#include <stdlib.h>
#include <string.h>

struct device
{
	int      id;
	char     name[26];
	float    temprature;
	uint32_t tick;
	uint32_t ip;
	uint32_t netmask;
	uint32_t gateway;
};

struct date
{
	uint16_t year;
	uint8_t  month;
	uint8_t  day;
	uint8_t  hour;
	uint8_t  min;
	uint16_t sec;
	uint8_t* pu8;
	uint16_t* pu16;
	uint32_t* pu32;
	uint64_t* pu64;

	struct device dev;
	struct device devls[5000];
	struct device* pdev;
};

const reflectinfo_t device_reflection[] = {
    _field_stc_refectinfo_int(struct device, id),
    _field_stc_refectinfo_array(struct device, name, ext_default_reflect_8bits),
    _field_stc_refectinfo_floatpoint(struct device, temprature),
    _field_stc_refectinfo_int(struct device, tick),
    _field_stc_refectinfo_int(struct device, ip),
    _field_stc_refectinfo_int(struct device, netmask),
    _field_stc_refectinfo_int(struct device, gateway),

    __refectinfo_end()};

const reflectinfo_t date_reflection[] = {
    _field_stc_refectinfo_int(struct date, year),
    _field_stc_refectinfo_int(struct date, month),
    _field_stc_refectinfo_int(struct date, day),
    _field_stc_refectinfo_int(struct date, hour),
    _field_stc_refectinfo_int(struct date, min),
    _field_stc_refectinfo_int(struct date, sec),
    _field_stc_refectinfo_object(struct date, dev, device_reflection),
    _field_stc_refectinfo_array(struct date, devls, device_reflection),
	_field_stc_refectinfo_ptr_object(struct date, pdev, device_reflection),
	_field_stc_refectinfo_ptr_basetype(struct date, pu8, ext_default_reflect_8bits),
	_field_stc_refectinfo_ptr_basetype(struct date, pu16, ext_default_reflect_16bits),
	_field_stc_refectinfo_ptr_basetype(struct date, pu32, ext_default_reflect_32bits),
	_field_stc_refectinfo_ptr_basetype(struct date, pu64, ext_default_reflect_64bits),

    __refectinfo_end()};

lwrb_t  mlwrb;
uint8_t data[1 << 18];

int main()
{
	int result = 0xFF;
	uint16_t u16da = 0xFF, u16db = 0x00;
	uint8_t u8temp = 0xFF, u8temp1 = 0x00;
	uint16_t u16temp = 0xFF, u16temp1 = 0x00;
	uint32_t u32temp = 0xFF, u32temp1 = 0x00;
	uint64_t u64temp = 0xFF, u64temp1 = 0x00;

	struct device dev3, dev2, dev1 =
	                        {
	                            .ip         = 192168102,
	                            .netmask    = 192168101,
	                            .name       = "1234567",
	                            .gateway    = 2552552550,
	                            .temprature = 23.66f,
	                            .id         = 1,
	                            .tick       = 1000};

	struct date now_date = {
	    .year  = 2024,
	    .month = 2,
	    .day   = 26,
	    .hour  = 15,
	    .sec   = 20,
	    .min   = 23,
	    .dev   = {
	          .ip         = 1921681021,
	          .netmask    = 1921681020,
	          .name       = "444444444444444444",
	          .gateway    = 2552552550,
	          .temprature = 23.66f,
	          .id         = 1,
	          .tick       = 1000},
	    .devls = {
		{.ip = 192168102, .netmask = 192168101, .name = "1112222", .gateway = 2552552550, .temprature = 23.66f, .id = 1, .tick = 1000},
		{.ip = 192168102, .netmask = 192168101, .name = "3333333", .gateway = 2552552550, .temprature = 23.66f, .id = 1, .tick = 1000}, 
		{.ip = 192168102, .netmask = 192168101, .name = "4444444", .gateway = 2552552550, .temprature = 23.66f, .id = 1, .tick = 1000},
		{.ip = 192168102, .netmask = 192168101, .name = "5555555", .gateway = 2552552550, .temprature = 23.66f, .id = 1, .tick = 1000},
		{.ip = 192168102, .netmask = 192168101, .name = "6666666", .gateway = 2552552550, .temprature = 23.66f, .id = 1, .tick = 1000},
		
	},
		.pdev = &dev1,
		.pu8 = &u8temp,
		.pu16 = &u16temp,
		.pu32 = &u32temp,
		.pu64 = &u64temp, };

	struct date now_date1;

	now_date1.pdev = &dev3;
	now_date1.pu8 = &u8temp1;
	now_date1.pu16 = &u16temp1;
	now_date1.pu32 = &u32temp1;
	now_date1.pu64 = &u64temp1;

	libframeoperate_init(malloc, free);
	lwrb_init(&mlwrb, data, sizeof(data));

	while (1)
	{
		result = libframeoperate_serialize(&mlwrb, &dev1, device_reflection, BIG_ORDER);
		result = libframeoperate_unserialize(&mlwrb, &dev2, device_reflection, BIG_ORDER);

		result = libframeoperate_serialize(&mlwrb, &now_date, date_reflection, BIG_ORDER);
		result = libframeoperate_unserialize(&mlwrb, &now_date1, date_reflection, BIG_ORDER);
	}
	
	return 0;
}

目前以上示例的正反序列化已完成,已测试的平台有win11 msvc、qt、stm32、zynq\zynqmp、复旦微zynq
本文只是介绍,该库完成前不开源,需要源代码可联系我+1401626167(请注明来意q)

  • 该库参考了json与结构体的相互映射开源库
  • 该库依赖于lwrb的循环队列
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值