Details
-
Bug
-
Status: Closed
-
Minor
-
Resolution: Fixed
-
2.1.0-rc-1
-
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":""}