| 2330 | } |
| 2331 | |
| 2332 | CJSON_PUBLIC(cJSON *) cJSON_CreateInt64(long long num) |
| 2333 | { |
| 2334 | cJSON *item = cJSON_New_Item(&global_hooks); |
| 2335 | if(item) |
| 2336 | { |
| 2337 | item->type = cJSON_Number; |
| 2338 | item->valuedouble = static_cast<double>(num); |
| 2339 | |
| 2340 | // For integer which is out of the range of [INT_MIN, INT_MAX], it may lose precision if we cast it to double. |
| 2341 | // Instead, we keep the integer literal as a string. |
| 2342 | if (num > INT_MAX || num < INT_MIN) |
| 2343 | { |
| 2344 | char buf[21]; |
| 2345 | sprintf(buf, "%lld", num); |
| 2346 | item->valuestring = (char*)cJSON_strdup((const unsigned char*)buf, &global_hooks); |
| 2347 | } |
| 2348 | |
| 2349 | /* use saturation in case of overflow */ |
| 2350 | if (num >= INT_MAX) |
| 2351 | { |
| 2352 | item->valueint = INT_MAX; |
| 2353 | } |
| 2354 | else if (num <= INT_MIN) |
| 2355 | { |
| 2356 | item->valueint = INT_MIN; |
| 2357 | } |
| 2358 | else |
| 2359 | { |
| 2360 | item->valueint = (int)num; |
| 2361 | } |
| 2362 | } |
| 2363 | |
| 2364 | return item; |
| 2365 | } |
| 2366 | |
| 2367 | CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string) |
| 2368 | { |
nothing calls this directly
no test coverage detected