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

JsonBuilder.toPrettyString() incorrect for zero-length String

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 2.1.0-rc-1
    • 2.1.0-rc-2, 2.1.0, 2.0.7
    • groovy-jdk
    • None
    •  > groovy --version
      Groovy Version: 2.1.0-rc-1 JVM: 1.7.0_09 Vendor: Oracle Corporation OS: Windows 7

    Description

      JsonBuilder.toString() works correctly for both zero-length and longer strings. JsonBuilder.toPrettyString() incorrectly inserts a pair of escaped double-quotes into the output for zero-length strings.

      Consider the following test case:

      import groovy.json.*
      import groovy.util.GroovyTestCase
      class MyTest extends GroovyTestCase {
        void testStringZero() {
          def m = [ s3:'abc', s0:'' ]
          def json = new JsonBuilder( m )
          def str = json.toString()
          def pretty = json.toPrettyString()
          def prettyTrim = pretty.replaceAll( ~/\s/, '' )
      
          if ( false ) {
            println ''
            println 'm: '           + m
            println 'str: '         + str
            println 'pretty: '      + pretty
            println 'prettyTrim: '  + prettyTrim
          }
      
          assert str == prettyTrim
        }
      }
      

      Enabling the prints produces:

      m: [s3:abc, s0:]
      str: {"s3":"abc","s0":""}
      pretty: {
          "s3": "abc",
          "s0": "\"\""
      }
      prettyTrim: {"s3":"abc","s0":"\"\""}
      
      E
      Time: 0.165
      There was 1 error:
      1) testStringZero(MyTest)Assertion failed:
      
      assert str == prettyTrim
             |   |  |
             |   |  {"s3":"abc","s0":"\"\""}
             |   false
             {"s3":"abc","s0":""}
      
      

      Attachments

        1. bug3.groovy
          0.5 kB
          Alan Thompson
        2. bug3.out
          0.2 kB
          Alan Thompson

        Activity

          People

            paulk Paul King
            thompson2526 Alan Thompson
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: