简体   繁体   中英

How to translate between cJSON and protobuf bytes variable

I'm using protobuf-c library and a cJSON and I have protobuf message with field bytes args = 1; // Command-specific payload

I'm parsing cJSON to protobuf like this:

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);

and then I'm trying to parse it back to cJSON and add to proper message format:

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);

I need to parse message from cJSON to protobuf and then back to cJSON. Final JSON should look like this:

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

but what I get is:

{
...
"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"
        },
...
}

edit. example message generated by some random generator:

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

I've been struggling with this task for like 2 days and when I posted this topic an light bulb appeared over my head...

so I've changed slightly translating from json to protobuf:

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

then I made something similar from json->protobuf to 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");
            }
        }
    }

basically it turned out that I forgot about existence of cJSON_Parse() function ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM