@@ -172,6 +172,30 @@ public void testJsonValue() throws IOException {
172
172
assertEquals ("{\" a\" :{\" b\" :true},\" c\" :1}" , stringWriter .toString ());
173
173
}
174
174
175
+ public void testNonFiniteFloats () throws IOException {
176
+ StringWriter stringWriter = new StringWriter ();
177
+ JsonWriter jsonWriter = new JsonWriter (stringWriter );
178
+ jsonWriter .beginArray ();
179
+ try {
180
+ jsonWriter .value (Float .NaN );
181
+ fail ();
182
+ } catch (IllegalArgumentException expected ) {
183
+ assertEquals ("Numeric values must be finite, but was NaN" , expected .getMessage ());
184
+ }
185
+ try {
186
+ jsonWriter .value (Float .NEGATIVE_INFINITY );
187
+ fail ();
188
+ } catch (IllegalArgumentException expected ) {
189
+ assertEquals ("Numeric values must be finite, but was -Infinity" , expected .getMessage ());
190
+ }
191
+ try {
192
+ jsonWriter .value (Float .POSITIVE_INFINITY );
193
+ fail ();
194
+ } catch (IllegalArgumentException expected ) {
195
+ assertEquals ("Numeric values must be finite, but was Infinity" , expected .getMessage ());
196
+ }
197
+ }
198
+
175
199
public void testNonFiniteDoubles () throws IOException {
176
200
StringWriter stringWriter = new StringWriter ();
177
201
JsonWriter jsonWriter = new JsonWriter (stringWriter );
@@ -226,6 +250,18 @@ public void testNonFiniteNumbers() throws IOException {
226
250
}
227
251
}
228
252
253
+ public void testNonFiniteFloatsWhenLenient () throws IOException {
254
+ StringWriter stringWriter = new StringWriter ();
255
+ JsonWriter jsonWriter = new JsonWriter (stringWriter );
256
+ jsonWriter .setLenient (true );
257
+ jsonWriter .beginArray ();
258
+ jsonWriter .value (Float .NaN );
259
+ jsonWriter .value (Float .NEGATIVE_INFINITY );
260
+ jsonWriter .value (Float .POSITIVE_INFINITY );
261
+ jsonWriter .endArray ();
262
+ assertEquals ("[NaN,-Infinity,Infinity]" , stringWriter .toString ());
263
+ }
264
+
229
265
public void testNonFiniteDoublesWhenLenient () throws IOException {
230
266
StringWriter stringWriter = new StringWriter ();
231
267
JsonWriter jsonWriter = new JsonWriter (stringWriter );
@@ -251,6 +287,36 @@ public void testNonFiniteNumbersWhenLenient() throws IOException {
251
287
assertEquals ("[NaN,-Infinity,Infinity,Infinity]" , stringWriter .toString ());
252
288
}
253
289
290
+ public void testFloats () throws IOException {
291
+ StringWriter stringWriter = new StringWriter ();
292
+ JsonWriter jsonWriter = new JsonWriter (stringWriter );
293
+ jsonWriter .beginArray ();
294
+ jsonWriter .value (-0.0f );
295
+ jsonWriter .value (1.0f );
296
+ jsonWriter .value (Float .MAX_VALUE );
297
+ jsonWriter .value (Float .MIN_VALUE );
298
+ jsonWriter .value (0.0f );
299
+ jsonWriter .value (-0.5f );
300
+ jsonWriter .value (2.2250739E-38f );
301
+ jsonWriter .value (3.723379f );
302
+ jsonWriter .value ((float ) Math .PI );
303
+ jsonWriter .value ((float ) Math .E );
304
+ jsonWriter .endArray ();
305
+ jsonWriter .close ();
306
+ assertEquals (
307
+ "[-0.0,"
308
+ + "1.0,"
309
+ + "3.4028235E38,"
310
+ + "1.4E-45,"
311
+ + "0.0,"
312
+ + "-0.5,"
313
+ + "2.2250739E-38,"
314
+ + "3.723379,"
315
+ + "3.1415927,"
316
+ + "2.7182817]" ,
317
+ stringWriter .toString ());
318
+ }
319
+
254
320
public void testDoubles () throws IOException {
255
321
StringWriter stringWriter = new StringWriter ();
256
322
JsonWriter jsonWriter = new JsonWriter (stringWriter );
0 commit comments