@@ -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.core.JdkVersion
1112import org.springframework.validation.BeanPropertyBindingResult
1213import org.springframework.validation.Errors
@@ -104,6 +105,25 @@ class JSONConverterTests extends AbstractGrailsControllerTests {
104105 assertEquals (' {"quotedString":"I contain a \\ "Quote\\ "!","nonquotedString":"I don\' t!"}' , json. toString())
105106 }
106107
108+ void testGStringsWithQuotes () {
109+ def json = [quotedString : " I contain a \" ${ 'Quote'} \" !" , nonquotedString : " I ${ 'don'} 't!" ] as JSON
110+ assertEquals (' {"quotedString":"I contain a \\ "Quote\\ "!","nonquotedString":"I don\' t!"}' , json. toString())
111+ }
112+
113+ void testStreamCharBufferWithQuotes () {
114+ def quotedBuffer = new StreamCharBuffer ()
115+ quotedBuffer. writer << " I contain a \" Quote\" !"
116+ def nonquotedBuffer = new StreamCharBuffer ()
117+ nonquotedBuffer. writer << " I don't!"
118+ def json = [quotedString : quotedBuffer, nonquotedString : nonquotedBuffer] as JSON
119+ assertEquals (' {"quotedString":"I contain a \\ "Quote\\ "!","nonquotedString":"I don\' t!"}' , json. toString())
120+ }
121+
122+ void testObjectWithQuotes () {
123+ def json = [quotedString : new CustomCharSequence (" I contain a \" Quote\" !" ), nonquotedString : new CustomCharSequence (" I don't!" )] as JSON
124+ assertEquals (' {"quotedString":"I contain a \\ "Quote\\ "!","nonquotedString":"I don\' t!"}' , json. toString())
125+ }
126+
107127 // GRAILS-11515
108128 void testJsonMultilineSerialization () {
109129 String multiLine = " first line \n second line"
@@ -160,3 +180,31 @@ class Book {
160180 String title
161181 String author
162182}
183+
184+ class CustomCharSequence implements CharSequence {
185+ String source
186+
187+ CustomCharSequence (String source ) {
188+ this . source = source
189+ }
190+
191+ @Override
192+ public int length () {
193+ source. length()
194+ }
195+
196+ @Override
197+ public char charAt (int index ) {
198+ source. charAt(index)
199+ }
200+
201+ @Override
202+ public CharSequence subSequence (int start , int end ) {
203+ source. subSequence(start, end)
204+ }
205+
206+ @Override
207+ public String toString () {
208+ source
209+ }
210+ }
0 commit comments