@@ -24,6 +24,8 @@ of this software and associated documentation files (the "Software"), to deal
2424SOFTWARE.
2525*/
2626
27+ import groovy .lang .Writable ;
28+
2729import java .io .IOException ;
2830import java .io .Writer ;
2931import java .util .Collection ;
@@ -33,9 +35,10 @@ of this software and associated documentation files (the "Software"), to deal
3335import java .util .Map ;
3436import java .util .Set ;
3537
36- import org .codehaus .groovy .grails .support .encoding .Encoder ;
37- import org .codehaus .groovy .grails .support .encoding .StreamingStatelessEncoder ;
38- import org .codehaus .groovy .grails .web .util .CodecPrintWriter ;
38+ import org .codehaus .groovy .grails .support .encoding .EncodesToWriter ;
39+ import org .codehaus .groovy .grails .support .encoding .StreamingEncoder ;
40+ import org .codehaus .groovy .grails .support .encoding .StreamingEncoderWriter ;
41+ import org .codehaus .groovy .grails .support .encoding .StreamingEncoderWritable ;
3942import org .springframework .util .ClassUtils ;
4043
4144/**
@@ -94,13 +97,13 @@ of this software and associated documentation files (the "Software"), to deal
9497 */
9598@ SuppressWarnings ({ "unchecked" , "rawtypes" })
9699public class JSONObject implements JSONElement , Map {
97- private static StreamingStatelessEncoder javascriptEncoderStateless ;
98- private static Encoder javascriptEncoder ;
100+ private static EncodesToWriter javascriptEncoderStateless ;
101+ private static StreamingEncoder javascriptEncoder ;
99102 private static boolean useStreamingJavascriptEncoder =false ;
100103 static {
101104 try {
102- javascriptEncoder = (Encoder )ClassUtils .forName ("org.codehaus.groovy.grails.plugins.codecs.JSONEncoder" , JSONObject .class .getClassLoader ()).newInstance ();
103- javascriptEncoderStateless = (StreamingStatelessEncoder )javascriptEncoder ;
105+ javascriptEncoder = (StreamingEncoder )ClassUtils .forName ("org.codehaus.groovy.grails.plugins.codecs.JSONEncoder" , JSONObject .class .getClassLoader ()).newInstance ();
106+ javascriptEncoderStateless = (EncodesToWriter )javascriptEncoder ;
104107 useStreamingJavascriptEncoder = true ;
105108 }
106109 catch (Exception e ) {
@@ -1125,13 +1128,15 @@ static void writeValue(Writer writer, Object value) throws IOException {
11251128 static void writeQuoted (Writer writer , Object value ) throws IOException {
11261129 if (useStreamingJavascriptEncoder ) {
11271130 writer .write ("\" " );
1128- if (value instanceof String || value instanceof StringBuilder || value instanceof StringBuffer ) {
1129- javascriptEncoderStateless .encodeToWriter ((CharSequence )value , writer );
1131+ if (value .getClass () == String .class || value .getClass () == StringBuilder .class || value .getClass () == StringBuffer .class ) {
1132+ encodeToWriter ((CharSequence )value , writer );
1133+ } else if (value instanceof StreamingEncoderWritable ) {
1134+ ((StreamingEncoderWritable )value ).encodeTo (writer , javascriptEncoderStateless );
1135+ } else if (value instanceof Writable ) {
1136+ ((Writable )value ).writeTo (new StreamingEncoderWriter (writer , javascriptEncoder , null ));
11301137 }
1131- else {
1132- CodecPrintWriter codecWriter = new CodecPrintWriter (writer , javascriptEncoder , null , true );
1133- codecWriter .print (value );
1134- codecWriter .flush ();
1138+ else {
1139+ encodeToWriter (value .toString (), writer );
11351140 }
11361141 writer .write ("\" " );
11371142 }
@@ -1140,6 +1145,11 @@ static void writeQuoted(Writer writer, Object value) throws IOException {
11401145 }
11411146 }
11421147
1148+
1149+ protected static void encodeToWriter (CharSequence str , Writer writer ) throws IOException {
1150+ javascriptEncoderStateless .encodeToWriter (str , 0 , str .length (), writer , null );
1151+ }
1152+
11431153 static void writeDate (Writer writer , Date d ) throws IOException {
11441154 writer .write ("new Date(" );
11451155 writer .write (String .valueOf (d .getTime ()));
0 commit comments