Losing precision when reading data from JSON response

1,687 views
Skip to first unread message

djohn

unread,
May 22, 2012, 2:32:57 PM5/22/12
to REST assured
When I read data from JSON that looks like the one below , I am losing
precision.

{ response : [
{
data1 : 42.12718585
...
} ,
{
data1: 1
....
}

]
}

String JSON = get(url).asString();
JsonPath jsonPath = new JsonPath(JSON).setRoot("response");
List<Map <String, Object>> responseList = jsonPath.get("response");
for (Map<String, Object> map : responseList) {
BigDecimal rate = new BigDecimal(map.get("data1").toString());
System.out.println(" rate in BigDecimal: " + rate);
}

--- Prints out the following :
rate in BigDecimal: 42.127186

How can I avoid losing precision when reading this data using rest-
assured's JsonPath?

Johan Haleby

unread,
May 23, 2012, 2:19:20 AM5/23/12
to rest-a...@googlegroups.com
Hi, 

Never experienced this myself and I'm not sure how or even if it can be fixed. I suppose it's Groovy and not Rest Assured itself that causes it (please correct me if I'm wrong because then it should be fixable). If I remember it correctly then in a later minor version of Groovy than the one Rest Assured is using they've switched from representing decimal numbers with floats and doubles to BigDecimal. In Groovy this doesn't matter since it's a dynamically typed language but it has quite a big impact on Rest Assured. For exemple if data1 would be a BigDecimal we would have to do:

expect().body("response.data1", equalTo(new BigDecimal(42.12)). .. 

which in my view is not as obvious as just having:

expect().body("response.data1", equalTo(42.12f)). .. 

It would also break back-ward compatibility. I'm not quite sure how we should tackle this. It's an important issue because it may prevent us from upgrading a new Groovy version in the future. I would suggest you to google for possible work-arounds for the problem in Groovy 1.8.4 and please let us know if you find anything.

Regards,
/Johan

djohn

unread,
May 23, 2012, 2:11:52 PM5/23/12
to REST assured
I'm not that familiar with groovy. Looking thru the source code, I was
suspecting it could be groovy. If I find something, I'll post it
here.
In my case, I couldn't afford to lose precision since it's currency
value. So temporarily just for these kinda data I created a switch to
jayway's JsonPath (which is working fine) instead of rest-assured's
JsonPath .

Johan Haleby

unread,
May 24, 2012, 12:58:39 AM5/24/12
to rest-a...@googlegroups.com
Ok good that you found a work-around, the "jayway" JsonPath is actually created by a colleague of mine and I have plans to support in in Rest Assured as well. The issue is with Groovy, see: https://jira.codehaus.org/browse/GROOVY-5129. Do you know if "jayways" JsonPath returns a float or double for your value?

Regrads,
/Johan

Deepa John

unread,
May 24, 2012, 7:03:46 PM5/24/12
to rest-a...@googlegroups.com
  "jayways" JsonPath returns a Double. It will be nice to have support for jayway JsonPath in rest-assured. 

Johan Haleby

unread,
May 25, 2012, 2:04:41 AM5/25/12
to rest-a...@googlegroups.com
Ok thanks! So perhaps we can come up with our own algorithm for converting the BigDecimal of Groovy to float or double that doesn't mean that we'll loose precision. 

/Johan
Reply all
Reply to author
Forward
0 new messages