20
20
import com .google .gson .JsonStreamParser ;
21
21
import com .google .gson .JsonSyntaxException ;
22
22
import com .google .gson .common .TestTypes .BagOfPrimitives ;
23
-
24
23
import com .google .gson .reflect .TypeToken ;
25
- import java .util .Map ;
26
- import junit .framework .TestCase ;
27
-
28
24
import java .io .CharArrayReader ;
29
25
import java .io .CharArrayWriter ;
30
26
import java .io .IOException ;
31
27
import java .io .Reader ;
32
28
import java .io .StringReader ;
33
29
import java .io .StringWriter ;
34
30
import java .io .Writer ;
31
+ import java .util .Arrays ;
32
+ import java .util .Map ;
33
+ import junit .framework .TestCase ;
35
34
36
35
/**
37
36
* Functional tests for the support of {@link Reader}s and {@link Writer}s.
@@ -89,8 +88,8 @@ public void testTopLevelNullObjectDeserializationWithReaderAndSerializeNulls() {
89
88
}
90
89
91
90
public void testReadWriteTwoStrings () throws IOException {
92
- Gson gson = new Gson ();
93
- CharArrayWriter writer = new CharArrayWriter ();
91
+ Gson gson = new Gson ();
92
+ CharArrayWriter writer = new CharArrayWriter ();
94
93
writer .write (gson .toJson ("one" ).toCharArray ());
95
94
writer .write (gson .toJson ("two" ).toCharArray ());
96
95
CharArrayReader reader = new CharArrayReader (writer .toCharArray ());
@@ -102,8 +101,8 @@ public void testReadWriteTwoStrings() throws IOException {
102
101
}
103
102
104
103
public void testReadWriteTwoObjects () throws IOException {
105
- Gson gson = new Gson ();
106
- CharArrayWriter writer = new CharArrayWriter ();
104
+ Gson gson = new Gson ();
105
+ CharArrayWriter writer = new CharArrayWriter ();
107
106
BagOfPrimitives expectedOne = new BagOfPrimitives (1 , 1 , true , "one" );
108
107
writer .write (gson .toJson (expectedOne ).toCharArray ());
109
108
BagOfPrimitives expectedTwo = new BagOfPrimitives (2 , 2 , false , "two" );
@@ -132,4 +131,50 @@ public void testTypeMismatchThrowsJsonSyntaxExceptionForReaders() {
132
131
} catch (JsonSyntaxException expected ) {
133
132
}
134
133
}
134
+
135
+ /**
136
+ * Verifies that passing an {@link Appendable} which is not an instance of {@link Writer}
137
+ * to {@code Gson.toJson} works correctly.
138
+ */
139
+ public void testToJsonAppendable () {
140
+ class CustomAppendable implements Appendable {
141
+ final StringBuilder stringBuilder = new StringBuilder ();
142
+ int toStringCallCount = 0 ;
143
+
144
+ @ Override
145
+ public Appendable append (char c ) throws IOException {
146
+ stringBuilder .append (c );
147
+ return this ;
148
+ }
149
+
150
+ @ Override
151
+ public Appendable append (CharSequence csq ) throws IOException {
152
+ if (csq == null ) {
153
+ csq = "null" ; // Requirement by Writer.append
154
+ }
155
+ append (csq , 0 , csq .length ());
156
+ return this ;
157
+ }
158
+
159
+ @ Override
160
+ public Appendable append (CharSequence csq , int start , int end ) throws IOException {
161
+ if (csq == null ) {
162
+ csq = "null" ; // Requirement by Writer.append
163
+ }
164
+
165
+ // According to doc, toString() must return string representation
166
+ String s = csq .toString ();
167
+ toStringCallCount ++;
168
+ stringBuilder .append (s , start , end );
169
+ return this ;
170
+ }
171
+ }
172
+
173
+ CustomAppendable appendable = new CustomAppendable ();
174
+ gson .toJson (Arrays .asList ("test" , 123 , true ), appendable );
175
+ // Make sure CharSequence.toString() was called at least two times to verify that
176
+ // CurrentWrite.cachedString is properly overwritten when char array changes
177
+ assertTrue (appendable .toStringCallCount >= 2 );
178
+ assertEquals ("[\" test\" ,123,true]" , appendable .stringBuilder .toString ());
179
+ }
135
180
}
0 commit comments