@@ -7,6 +7,7 @@ import grails.persistence.Entity
77import org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerTests
88import org.codehaus.groovy.grails.web.servlet.mvc.HibernateProxy
99import org.codehaus.groovy.grails.web.servlet.mvc.LazyInitializer
10+ import org.codehaus.groovy.grails.web.util.StreamCharBuffer
1011import org.springframework.validation.BeanPropertyBindingResult
1112import org.springframework.validation.Errors
1213
@@ -101,6 +102,25 @@ class JSONConverterTests extends AbstractGrailsControllerTests {
101102 assertEquals (' {"quotedString":"I contain a \\ "Quote\\ "!","nonquotedString":"I don\' t!"}' , json. toString())
102103 }
103104
105+ void testGStringsWithQuotes () {
106+ def json = [quotedString : " I contain a \" ${ 'Quote'} \" !" , nonquotedString : " I ${ 'don'} 't!" ] as JSON
107+ assertEquals (' {"quotedString":"I contain a \\ "Quote\\ "!","nonquotedString":"I don\' t!"}' , json. toString())
108+ }
109+
110+ void testStreamCharBufferWithQuotes () {
111+ def quotedBuffer = new StreamCharBuffer ()
112+ quotedBuffer. writer << " I contain a \" Quote\" !"
113+ def nonquotedBuffer = new StreamCharBuffer ()
114+ nonquotedBuffer. writer << " I don't!"
115+ def json = [quotedString : quotedBuffer, nonquotedString : nonquotedBuffer] as JSON
116+ assertEquals (' {"quotedString":"I contain a \\ "Quote\\ "!","nonquotedString":"I don\' t!"}' , json. toString())
117+ }
118+
119+ void testObjectWithQuotes () {
120+ def json = [quotedString : new CustomCharSequence (" I contain a \" Quote\" !" ), nonquotedString : new CustomCharSequence (" I don't!" )] as JSON
121+ assertEquals (' {"quotedString":"I contain a \\ "Quote\\ "!","nonquotedString":"I don\' t!"}' , json. toString())
122+ }
123+
104124 // GRAILS-11515
105125 void testJsonMultilineSerialization () {
106126 String multiLine = " first line \n second line"
@@ -155,3 +175,31 @@ class Book {
155175 String title
156176 String author
157177}
178+
179+ class CustomCharSequence implements CharSequence {
180+ String source
181+
182+ CustomCharSequence (String source ) {
183+ this . source = source
184+ }
185+
186+ @Override
187+ public int length () {
188+ source. length()
189+ }
190+
191+ @Override
192+ public char charAt (int index ) {
193+ source. charAt(index)
194+ }
195+
196+ @Override
197+ public CharSequence subSequence (int start , int end ) {
198+ source. subSequence(start, end)
199+ }
200+
201+ @Override
202+ public String toString () {
203+ source
204+ }
205+ }
0 commit comments