Stuart Bramley - 2013-09-05

I encountered an error in cJSON when parsing JSON text that contains a long numeric value.

The error occurred parsing the following JSON response (from Twitter) :-

{
  "created_at": "Wed Sep 04 16:04:14 +0000 2013",
  "id": 375288199918391296,
  "id_str": "375288199918391296",
  "text": "Blue tweet. Sorry",
  "source": "\u003cahref=\"http:\/\/notapublicapplication.com\" rel=\"nofollow\"\u003eLogging\u003c\/a\u003e",
  "truncated": false,
  "in_reply_to_status_id": null,
  "in_reply_to_status_id_str": null,
  "in_reply_to_user_id": null,
  "in_reply_to_user_idstr": null,
  "in_reply_to_screen_name": null,
  "user": {
    "id": 172774242,
    "id_str": "172774242",
    "name": "XXXXXXX",
    "screen_name": "XXXXXXX",
    "location": "",
    "description": "",
    "url": null,
    "entities": {
      "description": {
        "urls": [

        ]
      }
    },
    "protected": true,
    "followers_count": 7,
    "friends_count": 6,
    "listed_count": 0,
    "created_at": "Fri Jul 30 15:38:00 +0000 2010",
    "favourites_count": 0,
    "utc_offset": 3600,
    "time_zone": "London",
    "geo_enabled": false,
    "verified": false,
    "statuses_count": 409,
    "lang": "en",
    "contributors_enabled": false,
    "is_translator": false,
    "profile_background_color": "000203",
    "profile_background_image_url": "http:\/\/a0.twimg.com\/profile_background_images\/130855570\/WhiteText400.png",
    "profile_background_image_url_https": "https:\/\/si0.twimg.com\/profile_background_images\/130855570\/WhiteText400.png",
    "profile_background_tile": false,
    "profile_image_url": "http:\/\/a0.twimg.com\/profile_images\/1095329490\/XXXXXXX.png",
    "profile_image_url_https": "https:\/\/si0.twimg.com\/profile_images\/1095329490\/XXXXXXX.png",
    "profile_link_color": "20FA08",
    "profile_sidebar_border_color": "606161",
    "profile_sidebar_fill_color": "030303",
    "profile_text_color": "828282",
    "profile_use_background_image": true,
    "default_profile": false,
    "default_profile_image": false,
    "following": false,
    "follow_request_sent": false,
    "notifications": false
  },
  "geo": null,
  "coordinates": null,
  "place": null,
  "contributors": null,
  "retweet_count": 0,
  "favorite_count": 0,
  "entities": {
    "hashtags": [

    ],
    "symbols": [

    ],
    "urls": [

    ],
    "user_mentions": [

    ]
  },
  "favorited": false,
  "retweeted": false,
  "lang": "en"
}

Specifically, the error was with the "id" item - "id": 375288199918391296.
The error message I received is likely to be platform-specific (I am running this on an IBM i) and was "Invalid floating-point format change detected" - but essentially the code is trying to put the value 375288199918391296 into an int in the parse_number function - and the value being too large to fit into an int, it blows up.

The code currently does the following :

    item->valuedouble=n;
    item->valueint=(int)n;
    item->type=cJSON_Number;
    return num;

I have amended this in my source to be :

    item->valuedouble=n;
    if (n<=INT_MAX && n>=INT_MIN)
      item->valueint=(int)n;
    item->type=cJSON_Number;
    return num;

This works and allows the JSON to be parsed.. however I am not a C expert and this may not be the best approach to have taken?

Regards