基于CJSON库实现序列化

下载

bash 复制代码
git clone https://github.com/DaveGamble/cJSON.git

保留cJSON.h和cJSON.c即可。

序列化和反序列化

cpp 复制代码
root@DESKTOP:~/project/cjson_demo$ cat main.cpp
#include <iostream>
#include "cJSON.h"

using namespace std;

class SystemInfo {
public:
    std::string m_msg_id;
    std::string m_time;
    int m_err;
    std::string m_err_msg;
    std::string m_ntp_address;

    //反序列化(解析)
    int SetJsonStr(const std::string& json) {
        cJSON *js = cJSON_Parse(json.c_str());
        if (js == NULL) {
            return -1;
        }

        try {
            cJSON *item = cJSON_GetObjectItem(js, "msg_id");
            if (item != NULL && cJSON_IsString(item)) {
                m_msg_id = item->valuestring;
            }

            item = cJSON_GetObjectItem(js, "time");
            if (item != NULL && cJSON_IsString(item)) {
                m_time = item->valuestring;
            }

            item = cJSON_GetObjectItem(js, "err");
            if (item != NULL && cJSON_IsNumber(item)) {
                m_err = item->valueint;
            }

            item = cJSON_GetObjectItem(js, "err_msg");
            if (item != NULL && cJSON_IsString(item)) {
                m_err_msg = item->valuestring;
            }

            item = cJSON_GetObjectItem(js, "data");
            if (item != NULL && cJSON_IsObject(item)) {
                cJSON *it = cJSON_GetObjectItem(item, "ntp_address");
                if (it != NULL && cJSON_IsString(it)) {
                    m_ntp_address = it->valuestring;
                }
            }

            cJSON_Delete(js);
            return 0;
        } catch (...) {
            cJSON_Delete(js);
            return -1;
        }
    }

    //序列化(生成)
    std::string GetJsonStr() {
        cJSON *js = cJSON_CreateObject();
        cJSON_AddStringToObject(js, "msg_id", m_msg_id.c_str());
        cJSON_AddStringToObject(js, "time", m_time.c_str());
        cJSON_AddNumberToObject(js, "err", m_err);
        cJSON_AddStringToObject(js, "err_msg", m_err_msg.c_str());
        cJSON *item = cJSON_AddObjectToObject(js, "data");
        cJSON_AddStringToObject(item, "ntp_address", m_ntp_address.c_str());

        char* json_str = cJSON_PrintUnformatted(js);
        std::string result(json_str);
        free(json_str);
        cJSON_Delete(js);

        return result;
    }
};

int main() {
    const char *json_str = "{\"msg_id\":\"1211232322321111222\",\"time\":\"1356443343\",\"err\":0,\"err_msg\":\"成功\",\"data\":{\"ntp_address\":\"8.8.8.8\"}}";

    printf("cJSON Version:%s\n", cJSON_Version());

    SystemInfo info;
    info.SetJsonStr(string(json_str));
    string serialized_json = info.GetJsonStr();
    cout << "Serialized JSON:\n" << serialized_json << endl;

    return 0;
}

编译运行

bash 复制代码
root@DESKTOP:~/project/cjson_demo$ ls -l
total 100
-rw-r--r-- 1 root root78291 Apr 16 10:59 cJSON.c
-rw-r--r-- 1 root root16193 Apr 16 10:59 cJSON.h
-rw-r--r-- 1 root root  2575 Apr 16 14:42 main.cpp
root@DESKTOP:~/project/cjson_demo$
root@DESKTOP:~/project/cjson_demo$ g++ main.cpp cJSON.c
root@DESKTOP:~/project/cjson_demo$
root@DESKTOP:~/project/cjson_demo$ ./a.out
cJSON Version:1.7.17
Serialized JSON:
{"msg_id":"1211232322321111222","time":"1356443343","err":0,"err_msg":"成功","data":{"ntp_address":"8.8.8.8"}}
root@DESKTOP:~/project/cjson_demo$
相关推荐
我不会起名字3225 天前
Redis 从 0 到 1(二十七):JSON 体积是 Protobuf 的 3.7 倍?3 种序列化的 Go 基准测试
redis·go·序列化·protobuf·messagepack
十五年专注C++开发7 天前
FlatBuffers: 一个高效的零拷贝二进制序列化库
c++·序列化·反序列化·flatbuffers
程序员大阳10 天前
副队长JavaScript教程(65)-详解JSON:轻量级数据交换格式
javascript·json·序列化·反序列化·stringify·parse
j7~12 天前
【ProtoBuf】(篇一)初识 ProtoBuf
运维·服务器·序列化·protobuf·protobuf的适用特点
骑着蜗牛撵大象32719 天前
字节序、TLV 到 netlink:Linux 网络消息的编解码全链路
linux·网络·php·etl·序列化·字节序·tlv
换元不配限19 天前
Android 接入 protobuf 实战:从 Gradle 配置到避开 R8 混淆坑
android·grpc·序列化·protobuf·protocolbuffers·二进制序列化·.proto
星 海24 天前
【json】cJSON库解析文件
c语言·json·cjson
AIgorithmGEEK1 个月前
[Linux]从手写报头到内核套路:序列化、反序列化与自定义协议全链路
linux·运维·服务器·网络·序列化·反序列化
ShineWinsu1 个月前
对于ProtoBuffer:ProtoBuffer的介绍以及安装
linux·网络·google·协议·序列化·反序列化·protobuffer
j7~2 个月前
【Linux网络加餐】(篇六)网络版计算器(上):模板方法模式、序列化与 JSON
linux·c++·json·序列化·反序列化·网络版计算机