@@ -234,6 +234,24 @@ public void testDupFieldNameWrites() throws Exception
234
234
_testDupFieldNameWrites (f , true );
235
235
}
236
236
237
+ // [core#177]
238
+ // Also: should not try writing JSON String if field name expected
239
+ // (in future maybe take one as alias... but not yet)
240
+ /*
241
+ public void testFailOnWritingStringNotFieldName() throws Exception
242
+ {
243
+ JsonFactory f = new JsonFactory();
244
+ _testFailOnWritingStringNotFieldName(f, false);
245
+ _testFailOnWritingStringNotFieldName(f, true);
246
+ }
247
+ */
248
+
249
+ /*
250
+ /**********************************************************
251
+ /* Internal methods
252
+ /**********************************************************
253
+ */
254
+
237
255
private void _testDupFieldNameWrites (JsonFactory f , boolean useReader ) throws Exception
238
256
{
239
257
JsonGenerator gen ;
@@ -250,21 +268,36 @@ private void _testDupFieldNameWrites(JsonFactory f, boolean useReader) throws Ex
250
268
gen .writeFieldName ("b" );
251
269
gen .flush ();
252
270
String json = bout .toString ("UTF-8" );
253
- fail ("Should not have let two consequtive field name writes succeed: output = " +json );
271
+ fail ("Should not have let two consecutive field name writes succeed: output = " +json );
254
272
} catch (JsonProcessingException e ) {
255
273
verifyException (e , "can not write a field name, expecting a value" );
256
274
}
257
275
gen .close ();
258
276
}
259
277
260
- /*
261
- /**********************************************************
262
- /* Internal methods
263
- /**********************************************************
264
- */
265
-
266
- private void doTestIntWrite (boolean pad )
267
- throws Exception
278
+ private void _testFailOnWritingStringNotFieldName (JsonFactory f , boolean useReader ) throws Exception
279
+ {
280
+ JsonGenerator gen ;
281
+ ByteArrayOutputStream bout = new ByteArrayOutputStream ();
282
+ if (useReader ) {
283
+ gen = f .createGenerator (new OutputStreamWriter (bout , "UTF-8" ));
284
+ } else {
285
+ gen = f .createGenerator (bout , JsonEncoding .UTF8 );
286
+ }
287
+ gen .writeStartObject ();
288
+
289
+ try {
290
+ gen .writeString ("a" );
291
+ gen .flush ();
292
+ String json = bout .toString ("UTF-8" );
293
+ fail ("Should not have let 'writeString()' be used in place of 'writeFieldName()': output = " +json );
294
+ } catch (JsonProcessingException e ) {
295
+ verifyException (e , "can not write a field name, expecting a value" );
296
+ }
297
+ gen .close ();
298
+ }
299
+
300
+ private void doTestIntWrite (boolean pad ) throws Exception
268
301
{
269
302
int [] VALUES = new int [] {
270
303
0 , 1 , -9 , 32 , -32 , 57 , 189 , 2017 , -9999 , 13240 , 123456 ,
0 commit comments