@@ -216,6 +216,8 @@ private Feature(boolean defaultState) {
216
216
217
217
protected DumperOptions _outputOptions ;
218
218
219
+ protected final org .yaml .snakeyaml .DumperOptions .Version _docVersion ;
220
+
219
221
// for field names, leave out quotes
220
222
private final static DumperOptions .ScalarStyle STYLE_UNQUOTED_NAME = DumperOptions .ScalarStyle .PLAIN ;
221
223
@@ -252,6 +254,8 @@ private Feature(boolean defaultState) {
252
254
*/
253
255
protected String _typeId ;
254
256
257
+ protected int _rootValueCount ;
258
+
255
259
/*
256
260
/**********************************************************
257
261
/* Life-cycle
@@ -267,19 +271,14 @@ public YAMLGenerator(IOContext ctxt, int jsonFeatures, int yamlFeatures,
267
271
_ioContext = ctxt ;
268
272
_formatFeatures = yamlFeatures ;
269
273
_writer = out ;
274
+ _docVersion = version ;
270
275
271
276
_outputOptions = buildDumperOptions (jsonFeatures , yamlFeatures , version );
272
277
273
278
_emitter = new Emitter (_writer , _outputOptions );
274
279
// should we start output now, or try to defer?
275
- _emitter .emit (new StreamStartEvent (null , null ));
276
- Map <String ,String > noTags = Collections .emptyMap ();
277
-
278
- boolean startMarker = Feature .WRITE_DOC_START_MARKER .enabledIn (yamlFeatures );
279
-
280
- _emitter .emit (new DocumentStartEvent (null , null , startMarker ,
281
- version , // for 1.10 was: ((version == null) ? null : version.getArray()),
282
- noTags ));
280
+ _emit (new StreamStartEvent (null , null ));
281
+ _emitStartDocument ();
283
282
}
284
283
285
284
protected DumperOptions buildDumperOptions (int jsonFeatures , int yamlFeatures ,
@@ -479,8 +478,11 @@ public final void flush() throws IOException
479
478
public void close () throws IOException
480
479
{
481
480
if (!isClosed ()) {
482
- _emitter .emit (new DocumentEndEvent (null , null , false ));
483
- _emitter .emit (new StreamEndEvent (null , null ));
481
+ // 11-Dec-2019, tatu: Should perhaps check if content is to be auto-closed...
482
+ // but need END_DOCUMENT regardless
483
+
484
+ _emitEndDocument ();
485
+ _emit (new StreamEndEvent (null , null ));
484
486
super .close ();
485
487
486
488
/* 25-Nov-2008, tatus: As per [JACKSON-16] we are not to call close()
@@ -518,7 +520,7 @@ public final void writeStartArray() throws IOException
518
520
if (anchor != null ) {
519
521
_objectId = null ;
520
522
}
521
- _emitter . emit (new SequenceStartEvent (anchor , yamlTag ,
523
+ _emit (new SequenceStartEvent (anchor , yamlTag ,
522
524
implicit , null , null , style ));
523
525
}
524
526
@@ -531,7 +533,7 @@ public final void writeEndArray() throws IOException
531
533
// just to make sure we don't "leak" type ids
532
534
_typeId = null ;
533
535
_writeContext = _writeContext .getParent ();
534
- _emitter . emit (new SequenceEndEvent (null , null ));
536
+ _emit (new SequenceEndEvent (null , null ));
535
537
}
536
538
537
539
@ Override
@@ -546,7 +548,7 @@ public final void writeStartObject() throws IOException
546
548
if (anchor != null ) {
547
549
_objectId = null ;
548
550
}
549
- _emitter . emit (new MappingStartEvent (anchor , yamlTag ,
551
+ _emit (new MappingStartEvent (anchor , yamlTag ,
550
552
implicit , null , null , style ));
551
553
}
552
554
@@ -559,7 +561,7 @@ public final void writeEndObject() throws IOException
559
561
// just to make sure we don't "leak" type ids
560
562
_typeId = null ;
561
563
_writeContext = _writeContext .getParent ();
562
- _emitter . emit (new MappingEndEvent (null , null ));
564
+ _emit (new MappingEndEvent (null , null ));
563
565
}
564
566
565
567
/*
@@ -815,7 +817,7 @@ public void writeObjectRef(Object id)
815
817
{
816
818
_verifyValueWrite ("write Object reference" );
817
819
AliasEvent evt = new AliasEvent (String .valueOf (id ), null , null );
818
- _emitter . emit (evt );
820
+ _emit (evt );
819
821
}
820
822
821
823
@ Override
@@ -840,6 +842,15 @@ protected final void _verifyValueWrite(String typeMsg)
840
842
if (status == JsonWriteContext .STATUS_EXPECT_NAME ) {
841
843
_reportError ("Can not " +typeMsg +", expecting field name" );
842
844
}
845
+ if (_writeContext .inRoot ()) {
846
+ // Start-doc emitted when creating generator, but otherwise need it; similarly,
847
+ // need matching end-document to close earlier open one
848
+ if (_writeContext .getCurrentIndex () > 0 ) {
849
+ _emitEndDocument ();
850
+ _emitStartDocument ();
851
+ }
852
+ }
853
+
843
854
}
844
855
845
856
@ Override
@@ -861,7 +872,7 @@ protected void _releaseBuffers() {
861
872
862
873
protected void _writeScalar (String value , String type , DumperOptions .ScalarStyle style ) throws IOException
863
874
{
864
- _emitter . emit (_scalarEvent (value , style ));
875
+ _emit (_scalarEvent (value , style ));
865
876
}
866
877
867
878
private void _writeScalarBinary (Base64Variant b64variant ,
@@ -874,7 +885,7 @@ private void _writeScalarBinary(Base64Variant b64variant,
874
885
}
875
886
final String lf = _lf ();
876
887
String encoded = _base64encode (b64variant , data , lf );
877
- _emitter . emit (new ScalarEvent (null , TAG_BINARY , EXPLICIT_TAGS , encoded ,
888
+ _emit (new ScalarEvent (null , TAG_BINARY , EXPLICIT_TAGS , encoded ,
878
889
null , null , STYLE_BASE64 ));
879
890
}
880
891
@@ -976,4 +987,24 @@ private boolean _valueNeedsQuoting(String name) {
976
987
protected String _lf () {
977
988
return _outputOptions .getLineBreak ().getString ();
978
989
}
990
+
991
+ // @since 2.10.2
992
+ protected void _emitStartDocument () throws IOException
993
+ {
994
+ Map <String ,String > noTags = Collections .emptyMap ();
995
+ boolean startMarker = Feature .WRITE_DOC_START_MARKER .enabledIn (_formatFeatures );
996
+ _emit (new DocumentStartEvent (null , null , startMarker ,
997
+ _docVersion , // for 1.10 was: ((version == null) ? null : version.getArray()),
998
+ noTags ));
999
+ }
1000
+
1001
+ // @since 2.10.2
1002
+ protected void _emitEndDocument () throws IOException {
1003
+ _emit (new DocumentEndEvent (null , null , false ));
1004
+ }
1005
+
1006
+ // @since 2.10.2
1007
+ protected final void _emit (Event e ) throws IOException {
1008
+ _emitter .emit (e );
1009
+ }
979
1010
}
0 commit comments