The delete operation seems to throw an invalid pointer error. Does anyone know why this might be happening and a workaround this? I am new to this library.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The delete operation seems to throw an invalid pointer error. Does anyone know why this might be happening and a workaround this? I am new to this library.
You sure are! Crikey. Not sure this is the right library for you. JSON is a serialisation format. You don't parse back/forth like that. You create your output structure in one hit and serialise.
You build up structure as you go. Don't try modifying the cJSON structure unless you really know what you're doing. That's not what it's for. It's a parser/printer, not a general-purpose-I-will-psychically-guess-the-lifetime-of-your-data management system. It's designed to be fast and simple, not feature-rich, and certainly not something that will manage data ownership by magic like that.
I have a piece of code as follows using the cJSON library
const char jSONSkeleton[] = "{\"name\": \"\",\"address\":{\"streetName\":\"\",\"houseNo\":0}}";
cJSON json= cJSON_Parse((char )jSONSkeleton);
cJSON *format = cJSON_GetObjectItem(json,"address");
cJSON_GetObjectItem(json,"name")->valuestring = "Mike";
cJSON_GetObjectItem(format,"streetName")->valuestring = "Jump Street";
cJSON_GetObjectItem(format,"houseNo")->valuedouble = 21;
char *output = cJSON_Print(json);
printf("PERSON DETAILS: %s\n", output);
cJSON_Delete(json);
The delete operation seems to throw an invalid pointer error. Does anyone know why this might be happening and a workaround this? I am new to this library.
Hi,
On 13 Oct 2014, at 17:00, Omkar omkarhegde@users.sf.net wrote:
Maybe if you did something like:
cJSON json=cJSON_CreateObject();
cJSON format=cJSON_CreateObject();
cJSON_AddItemToObject(json,"name",cJSON_CreateString("Mike"));
cJSON_AddItemToObject(json,"address", format);
cJSON_AddItemToObject(format,"streetName",cJSON_CreateString("Jump Street"));
cJSON_AddItemToObject(format,"houseNo",cJSON_CreateNumber(21));
char *output = cJSON_Print(json);
printf("blah blah blah... %s",output);
cJSON_Delete(json);
that'd get what you wanted.
You build up structure as you go. Don't try modifying the cJSON structure unless you really know what you're doing. That's not what it's for. It's a parser/printer, not a general-purpose-I-will-psychically-guess-the-lifetime-of-your-data management system. It's designed to be fast and simple, not feature-rich, and certainly not something that will manage data ownership by magic like that.
Dave.