cJSON icon indicating copy to clipboard operation
cJSON copied to clipboard

cJSON_True object consitency

Open danomimanchego123 opened this issue 3 years ago • 1 comments

I recently noticed that when cJSON parses a JSON true, it sets valueint to 1, which seems like a nice way to easly support number 0/1 and boolean false/true with a single test. However, I noticed that an object created by cJSON_CreateTrue does not set valueint to 1. Maybe cJSON_CreateTrue should, so that a True object created by either cJSON_Parse or cJSON_CreateTrue look the same?

If you were to set valueint to 1 in cJSON_CreateTrue, then maybe you should change cJSON_CreateBool to call cJSON_CreateTrue or cJSON_CreateFalse so that this kind of mod is more isolated to the basic cJSON_CreateTrue function. I.e.:

CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void)
{
    cJSON *item = cJSON_New_Item(&global_hooks);
    if(item)
    {
        item->type = cJSON_True;
        item->valueint = 1;
    }

    return item;
}

...snip...

CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean)
{
    return boolean ? cJSON_CreateTrue() : cJSON_CreateFalse();
}

danomimanchego123 avatar Sep 24 '20 02:09 danomimanchego123