You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "John Wagenleitner (JIRA)" <ji...@apache.org> on 2015/11/09 22:57:11 UTC
[jira] [Commented] (GROOVY-6922) JsonSlurper loses trailing 0's in
numbers (regression in Groovy 2.3)
[ https://issues.apache.org/jira/browse/GROOVY-6922?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14997443#comment-14997443 ]
John Wagenleitner commented on GROOVY-6922:
-------------------------------------------
This is somewhat related to the changes for GROOVY-6674 and [commit 842bcd4|https://github.com/apache/incubator-groovy/commit/842bcd4e23fefdf5b5e17d8d91af50e1ce565096] that changed the decimal return types from double to BigDecimal. The assert {{assert 123.40G == 123.4G}} passes and the numbers are equivalent. However, the reduction in scale of the number might be unexpected and is different in pre-2.3 releases.
{code}
def num = new groovy.json.JsonSlurper().parseText('{"num": 123.40}').num
assert num instanceof BigDecimal
assert 2 == num.scale() // fails: scale is 1
{code}
One way to fix with the least change would be to change this [{{lvalue#divide(java.math.BigDecimal)}}|https://github.com/apache/incubator-groovy/blob/e6185f9e33d40039c2140751ade37972046b676f/subprojects/groovy-json/src/main/java/groovy/json/internal/CharScanner.java#L667] to use the [{{#divide(java.math.BigDecimal,int,java.math.RoundingMode)}}|http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#divide(java.math.BigDecimal,%20int,%20java.math.RoundingMode)] method using {{digitsPastPoint}} for the scale and {{RoundingMode.UNNECESSARY}}.
However, I think a lot of the code is left over from when it was originally parsing to a Double. So I think a better but bigger change would be to [replace the bulk of the parseJsonNumber method|https://github.com/apache/incubator-groovy/blob/e6185f9e33d40039c2140751ade37972046b676f/subprojects/groovy-json/src/main/java/groovy/json/internal/CharScanner.java#L657-L672] with a call to {{BigDecimal}} ctor that takes a buffer, offset and length. This would have the added benefit of eliminating the String allocations.
> JsonSlurper loses trailing 0's in numbers (regression in Groovy 2.3)
> --------------------------------------------------------------------
>
> Key: GROOVY-6922
> URL: https://issues.apache.org/jira/browse/GROOVY-6922
> Project: Groovy
> Issue Type: Bug
> Components: JSON
> Affects Versions: 2.3.0, 2.4.3
> Reporter: Craig
>
> In previous versions of Grails before JsonSlurper was significantly changed, this test passed. However, it fails in Groovy 2.3.x (tested up to 2.3.3).
> {code}
> void testParseNumWithDecimals() {
> def i = parser.parseText('123.40')
> BigDecimal i2 = 123.40G
> assert i instanceof BigDecimal
> assert i == i2
> }
> {code}
> This test should be added to https://github.com/groovy/groovy-core/blob/GROOVY_2_3_X/subprojects/groovy-json/src/test/groovy/groovy/json/JsonSlurperTest.groovy
> In Groovy 2.3.3, the assertion fails as i = 123.4G, not 123.40G.
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)