cJSON
cJSON copied to clipboard
Modify json object type inplace
For one of my projects, I keep a cJSON representation of a data structure that syncs with remote source. I common problem in I my experience. Sometimes, I need to modify some of the child nodes and make them switch from NULL to string or number. There seem to be no well defined way to do this in current cJSON so I implemented some functions that fiddle with the type and the contained that in the node:
void cJSON_TransformToInvalid(cJSON * item) {
if (item==NULL) return;
if (!(item->type & cJSON_IsReference) && (item->child != NULL)) {
cJSON_Delete(item->child);
}
item->child=NULL;
if (!(item->type & cJSON_IsReference) && (item->valuestring != NULL)) {
cJSON_free(item->valuestring);
}
item->valuestring = NULL;
item->type= item->type & ~(cJSON_IsReference|0xFF);
}
void cJSON_TransformToNull(cJSON * item) {
if (item==NULL) return;
cJSON_TransformToInvalid(item);
item->type =item->type | cJSON_NULL;
}
//... etc ...
Should I open a PR for such feature or is this out of scope for cJSON