You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello, I have an idea to use an union structure to store the value. Just as follows:
`// union of a cJSON value
typedef union cJSON_Value {
// suppose the size of a pointer is 4-byte. valuestring[1] may be useless.
char *valuestring[2];
int valueint[2];
double valuedouble;
} cJSON_Value;
It's a fair suggestion, and one that's been around a few times before.
The decision not to do this is based on a desire to /try/ and create some form of type-safety, however marginal. It would be trivial to directly set a double value and hence kill the char*, which is then memory leaked, and causes a crash in free(), etc... and those kinds of mistakes are really easy to make, and will take a long time to hunt down if you're new to the library.
So, yes, it sure could be done, but the memory cost is worth the debug-time cost.
Hello, I have an idea to use an union structure to store the value. Just as follows:
`// union of a cJSON value
typedef union cJSON_Value {
// suppose the size of a pointer is 4-byte. valuestring[1] may be useless.
char *valuestring[2];
int valueint[2];
double valuedouble;
} cJSON_Value;
// struct of cJSON
typedef struct cJSON {
struct cJSON _next,_prev;
struct cJSON *child;
int type;
cJSON_Value value;
char *string;
} cJSON;`
The use of union can reduce the size of a JSON on the sacrifice of the portability. So, it's just a suggestion.
The text was updated successfully, but these errors were encountered: