Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-7665

JsonSlurperCharSource decimal parsing producing unexpected results

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 2.4.5
    • Fix Version/s: 2.4.6
    • Component/s: JSON
    • Labels:
      None

      Description

      When using the JsonParserType.CHARACTER_SOURCE decimal numbers are first parsed to double value and that value is used to construct the BigDecimal to return. This can produce some unexpected results.

      For example:

      import groovy.json.*
      
      def parser = new JsonSlurper().setType(JsonParserType.CHARACTER_SOURCE)
      def json = parser.parseText('{"num": 123.40}')
      
      assert json.num instanceof BigDecimal
      assert 123.40G == json.num​
      

      Fails with:

      Assertion failed: 
      
      assert 123.40G == json.num
                     |  |    |
                     |  |    123.400000000000005684341886080801486968994140625
                     |  [num:123.400000000000005684341886080801486968994140625]
                     false
      
      	at Script1.run(Script1.groovy:8)
      

        Attachments

          Activity

            People

            • Assignee:
              pascalschumacher Pascal Schumacher
              Reporter:
              jwagenleitner John Wagenleitner
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: