bobolounna / restfb

Automatically exported from code.google.com/p/restfb
0 stars 0 forks source link

Large numbers in json can be parsed wrong #143

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
If you have mapped for example a facebook id to a java long and you get a very 
large id back from facebook, this can be parsed wrong. For example, if you get 
the number 10150227048477971 in a response from facebook this gets wrongly 
parsed into another number, 10150227048477972.

What steps will reproduce the problem?
  @Test
  public void testLargeNumber() {
      long largeNumber = 10150227048477971L;
      BasicUser basicUser = createJsonMapper().toJavaObject("{\"uid\":\"" + largeNumber + "\"}", BasicUser.class);
      assertTrue(basicUser.uid.equals(largeNumber));      
  }
  static class BasicUser {
    @Facebook
    Long uid;
  }

It is caused by a rounding problem in com.restfb.json.JsonObject.getLong(), 
which parses a string into a long by using getDouble() which can't handle large 
numbers (> 53 bits I think).

I changed the getLong() method to this:
    public long getLong(String key) {
      Object o = get(key);
        try {
            return o instanceof Number ? ((Number) o).longValue() : Long.valueOf((String) o).longValue();
        } catch (Exception e) {
            throw new JsonException("JsonObject[" + quote(key) + "] is not a number.");
        }
    }

and it seems to work.

Original issue reported on code.google.com by p...@midasplayer.com on 29 Jun 2011 at 9:19

GoogleCodeExporter commented 8 years ago
Hey, thanks very much for catching this - I will definitely fix it in 1.6.5.

I am happy to check https://github.com/douglascrockford/JSON-java to see if 
this bug also exists there (RestFB just repackages the json.org reference 
impl)...if so, I will file a bug report and give you credit for finding it...or 
you can open one yourself if you'd like.

Original comment by mark.a.a...@gmail.com on 30 Jun 2011 at 8:40

GoogleCodeExporter commented 8 years ago
Fixed in r352.  FWIW the json.org implementation has already been patched.  
Thanks again for reporting!

Original comment by mark.a.a...@gmail.com on 3 Jul 2011 at 12:56