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

JsonSlurperCharSource decimal parsing producing unexpected results

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 2.4.5
    • 2.4.6
    • JSON
    • 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

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

            Dates

              Created:
              Updated:
              Resolved: