Parse the input text to generate a number, and populate the result into item. */
| 237 | |
| 238 | /* Parse the input text to generate a number, and populate the result into item. */ |
| 239 | static const unsigned char *parse_number(cJSON *item, const unsigned char *num) |
| 240 | { |
| 241 | double number = 0; |
| 242 | unsigned char *endpointer = NULL; |
| 243 | |
| 244 | if (num == NULL) |
| 245 | { |
| 246 | return NULL; |
| 247 | } |
| 248 | |
| 249 | number = strtod((const char*)num, (char**)&endpointer); |
| 250 | if ((num == endpointer) || (num == NULL)) |
| 251 | { |
| 252 | /* parse_error */ |
| 253 | return NULL; |
| 254 | } |
| 255 | |
| 256 | item->valuedouble = number; |
| 257 | |
| 258 | /* use saturation in case of overflow */ |
| 259 | if (number >= INT_MAX) |
| 260 | { |
| 261 | item->valueint = INT_MAX; |
| 262 | } |
| 263 | else if (number <= INT_MIN) |
| 264 | { |
| 265 | item->valueint = INT_MIN; |
| 266 | } |
| 267 | else |
| 268 | { |
| 269 | item->valueint = (int)number; |
| 270 | } |
| 271 | item->type = cJSON_Number; |
| 272 | |
| 273 | return endpointer; |
| 274 | } |
| 275 | |
| 276 | /* don't ask me, but the original cJSON_SetNumberValue returns an integer or double */ |
| 277 | double cJSON_SetNumberHelper(cJSON *object, double number) |