如何在cJSON和protobuf字节变量之间转换

时间:2019-07-17 08:27:52

标签: c json protocol-buffers cjson

我正在使用protobuf-c库和cJSON,并且具有带有字段的protobuf消息     字节args = 1; //特定于命令的有效载荷

我正在像这样将cJSON解析为protobuf:

args = cJSON_GetObjectItemCaseSensitive(command, "args");
    if (args) {
        cJSON_ArrayForEach(arg, args) {
            key = arg->string;
            log_debug("PARSER: key = %s", key);
            if (key != NULL) {
                if (cJSON_IsNumber(arg)) {
                    log_info("received argument: %s : (int)%d : (double)%.2f",
                            key, arg->valueint, arg->valuedouble);
                } else if (cJSON_IsString(arg) && (arg->valuestring != NULL)) {
                    log_info("received argument: %s : %s", key,
                            arg->valuestring);
                } else {
                    log_error("error while parsing argument's value");
                }
            } else {
                log_error("error while parsing argument");
            }
        }
        log_debug("PARSER: print = %s", args);
        // not sure if it is proper way of adding cJSON object to protobuf's bytes variable
        req->args.data = cJSON_Print(args);
        req->args.len = strlen(req->args.data);

然后我试图将其解析回cJSON并添加为正确的消息格式:

char rpt_char[MAX_RPT_SIZE];
strncpy(rpt_char, (char*)cvp->args.data, cvp->args.len);
    cJSON_AddItemToObject(json_message, "rpt", rpt = cJSON_CreateObject());
    cJSON_AddStringToObject(rpt, "args", rpt_char);

我需要将消息从cJSON解析到protobuf,然后再返回到cJSON。 最终的JSON应该如下所示:

{
...
rpt: {
arg1: data, // don't know actual names of key strings and values
arg2: data
},
...
}

但是我得到的是:

{
...
"rpt":  {
           "args": "{\n\t\"arg1\":\t38,\n\t\"arg2\":\t\"Taylor Norman\"\n}[\u0002\u00106[\u0002\u0010�[\u0002\u0010�[\u0002\u0010�ȍ\u0003\u0001�[\u0002\u0010�ȍ\u0006"
        },
...
}

编辑。 一些随机生成器生成的示例消息:

{"cmd":0,"t":963205,"xi":"5d2c8cb34888fc8471991545","args":{"arg1":38,"arg2":"Taylor Norman"},"kid":"5d2c8cb3329f9599e4730b6a","sig":"5d2c8cb33949d35fb22d0cb3"}

1 个答案:

答案 0 :(得分:0)

我已经为这个任务苦苦挣扎了大约2天,当我发布这个话题时,一个灯泡出现在我的头上...

所以我将翻译从json略微更改为protobuf:

json = cJSON_PrintUnformatted(args);
req->args.data = json;
req->args.len = strlen(json);

然后我从json-> protobuf到protobuf-> json做了类似的事情:

    args = cJSON_Parse((char*)cvp->args.data);
    cJSON_AddItemToObject(json_message, "rpt", args);

    if (args) {
        cJSON_ArrayForEach(arg, args) {
            key = arg->string;
            log_debug("PARSER: key = %s", key);
            if (key != NULL) {
                if (cJSON_IsNumber(arg)) {
                    log_info("received argument: %s : (int)%d : (double)%.2f",
                            key, arg->valueint, arg->valuedouble);
                    cJSON_AddItemToObject(rpt, key, arg->valueint);
                } else if (cJSON_IsString(arg) && (arg->valuestring != NULL)) {
                    log_info("received argument: %s : %s", key,
                            arg->valuestring);
                    cJSON_AddItemToObject(rpt, key, arg->valuestring);
                } else {
                    log_error("error while parsing argument's value");
                }
            } else {
                log_error("error while parsing argument");
            }
        }
    }

基本上,我发现我忘记了cJSON_Parse()函数的存在...

相关问题