6
6
import java .util .Arrays ;
7
7
import java .util .Collections ;
8
8
import java .util .Map ;
9
+ import java .util .Optional ;
9
10
import java .util .regex .Pattern ;
10
11
11
- import org .yaml .snakeyaml .DumperOptions ;
12
- import org .yaml .snakeyaml .DumperOptions .FlowStyle ;
13
- import org .yaml .snakeyaml .emitter .Emitter ;
14
- import org .yaml .snakeyaml .events .*;
15
- import org .yaml .snakeyaml .nodes .Tag ;
12
+ import org .snakeyaml .engine .v1 .api .DumpSettings ;
13
+ import org .snakeyaml .engine .v1 .api .DumpSettingsBuilder ;
14
+ import org .snakeyaml .engine .v1 .common .Anchor ;
15
+ import org .snakeyaml .engine .v1 .common .FlowStyle ;
16
+ import org .snakeyaml .engine .v1 .common .ScalarStyle ;
17
+ import org .snakeyaml .engine .v1 .common .SpecVersion ;
18
+ import org .snakeyaml .engine .v1 .emitter .Emitter ;
19
+ import org .snakeyaml .engine .v1 .events .AliasEvent ;
20
+ import org .snakeyaml .engine .v1 .events .DocumentEndEvent ;
21
+ import org .snakeyaml .engine .v1 .events .DocumentStartEvent ;
22
+ import org .snakeyaml .engine .v1 .events .ImplicitTuple ;
23
+ import org .snakeyaml .engine .v1 .events .MappingEndEvent ;
24
+ import org .snakeyaml .engine .v1 .events .MappingStartEvent ;
25
+ import org .snakeyaml .engine .v1 .events .ScalarEvent ;
26
+ import org .snakeyaml .engine .v1 .events .SequenceEndEvent ;
27
+ import org .snakeyaml .engine .v1 .events .SequenceStartEvent ;
28
+ import org .snakeyaml .engine .v1 .events .StreamEndEvent ;
29
+ import org .snakeyaml .engine .v1 .events .StreamStartEvent ;
30
+ import org .snakeyaml .engine .v1 .nodes .Tag ;
16
31
17
32
import com .fasterxml .jackson .core .*;
18
33
import com .fasterxml .jackson .core .base .GeneratorBase ;
@@ -97,7 +112,12 @@ public enum Feature implements FormatFeature
97
112
* If disabled, Unix linefeed ({@code \n}) will be used.
98
113
* <p>
99
114
* Default value is `false` for backwards compatibility.
115
+ *
116
+ * This setting does not do anything. Regardless of its value, SnakeYAML Engine will use the line break defined
117
+ * in System.getProperty("line.separator")
118
+ * @deprecated
100
119
*/
120
+ @ Deprecated
101
121
USE_PLATFORM_LINE_BREAKS (false ),
102
122
103
123
/**
@@ -168,23 +188,23 @@ private Feature(boolean defaultState) {
168
188
169
189
protected Writer _writer ;
170
190
171
- protected DumperOptions _outputOptions ;
191
+ protected DumpSettings _outputOptions ;
172
192
173
193
// for field names, leave out quotes
174
- private final static DumperOptions . ScalarStyle STYLE_NAME = DumperOptions . ScalarStyle .PLAIN ;
194
+ private final static ScalarStyle STYLE_NAME = ScalarStyle .PLAIN ;
175
195
176
196
// numbers, booleans, should use implicit
177
- private final static DumperOptions . ScalarStyle STYLE_SCALAR = DumperOptions . ScalarStyle .PLAIN ;
197
+ private final static ScalarStyle STYLE_SCALAR = ScalarStyle .PLAIN ;
178
198
// Strings quoted for fun
179
- private final static DumperOptions . ScalarStyle STYLE_QUOTED = DumperOptions . ScalarStyle .DOUBLE_QUOTED ;
199
+ private final static ScalarStyle STYLE_QUOTED = ScalarStyle .DOUBLE_QUOTED ;
180
200
// Strings in literal (block) style
181
- private final static DumperOptions . ScalarStyle STYLE_LITERAL = DumperOptions . ScalarStyle .LITERAL ;
201
+ private final static ScalarStyle STYLE_LITERAL = ScalarStyle .LITERAL ;
182
202
183
203
// Which flow style to use for Base64? Maybe basic quoted?
184
204
// 29-Nov-2017, tatu: Actually SnakeYAML uses block style so:
185
- private final static DumperOptions . ScalarStyle STYLE_BASE64 = STYLE_LITERAL ;
205
+ private final static ScalarStyle STYLE_BASE64 = STYLE_LITERAL ;
186
206
187
- private final static DumperOptions . ScalarStyle STYLE_PLAIN = DumperOptions . ScalarStyle .PLAIN ;
207
+ private final static ScalarStyle STYLE_PLAIN = ScalarStyle .PLAIN ;
188
208
189
209
/*
190
210
/**********************************************************************
@@ -215,7 +235,7 @@ private Feature(boolean defaultState) {
215
235
public YAMLGenerator (ObjectWriteContext writeContext , IOContext ioCtxt ,
216
236
int streamWriteFeatures , int yamlFeatures ,
217
237
Writer out ,
218
- org . yaml . snakeyaml . DumperOptions . Version version )
238
+ SpecVersion version )
219
239
throws IOException
220
240
{
221
241
super (writeContext , streamWriteFeatures );
@@ -225,22 +245,22 @@ public YAMLGenerator(ObjectWriteContext writeContext, IOContext ioCtxt,
225
245
226
246
_outputOptions = buildDumperOptions (streamWriteFeatures , yamlFeatures , version );
227
247
228
- _emitter = new Emitter (_writer , _outputOptions );
248
+ _emitter = new Emitter ( _outputOptions , new WriterWrapper ( _writer ) );
229
249
// should we start output now, or try to defer?
230
- _emitter .emit (new StreamStartEvent (null , null ));
250
+ _emitter .emit (new StreamStartEvent ());
231
251
Map <String ,String > noTags = Collections .emptyMap ();
232
252
233
253
boolean startMarker = Feature .WRITE_DOC_START_MARKER .enabledIn (yamlFeatures );
234
254
235
- _emitter .emit (new DocumentStartEvent (null , null , startMarker ,
236
- version , // for 1.10 was: ((version == null) ? null : version.getArray()),
255
+ _emitter .emit (new DocumentStartEvent (startMarker , Optional . empty () ,
256
+ // for 1.10 was: ((version == null) ? null : version.getArray()),
237
257
noTags ));
238
258
}
239
259
240
- protected DumperOptions buildDumperOptions (int streamWriteFeatures , int yamlFeatures ,
241
- org . yaml . snakeyaml . DumperOptions . Version version )
260
+ protected DumpSettings buildDumperOptions (int streamWriteFeatures , int yamlFeatures ,
261
+ SpecVersion version )
242
262
{
243
- DumperOptions opt = new DumperOptions ();
263
+ DumpSettingsBuilder opt = new DumpSettingsBuilder ();
244
264
// would we want canonical?
245
265
if (Feature .CANONICAL_OUTPUT .enabledIn (_formatWriteFeatures )) {
246
266
opt .setCanonical (true );
@@ -260,11 +280,7 @@ protected DumperOptions buildDumperOptions(int streamWriteFeatures, int yamlFeat
260
280
opt .setIndicatorIndent (1 );
261
281
opt .setIndent (2 );
262
282
}
263
- // 14-May-2018: [dataformats-text#84] allow use of platform linefeed
264
- if (Feature .USE_PLATFORM_LINE_BREAKS .enabledIn (_formatWriteFeatures )) {
265
- opt .setLineBreak (DumperOptions .LineBreak .getPlatformLineBreak ());
266
- }
267
- return opt ;
283
+ return opt .build ();
268
284
}
269
285
270
286
/*
@@ -425,8 +441,8 @@ public final void flush() throws IOException
425
441
public void close () throws IOException
426
442
{
427
443
if (!isClosed ()) {
428
- _emitter .emit (new DocumentEndEvent (null , null , false ));
429
- _emitter .emit (new StreamEndEvent (null , null ));
444
+ _emitter .emit (new DocumentEndEvent ( false ));
445
+ _emitter .emit (new StreamEndEvent ());
430
446
super .close ();
431
447
432
448
/* 25-Nov-2008, tatus: As per [JACKSON-16] we are not to call close()
@@ -460,12 +476,12 @@ public final void writeStartArray() throws IOException
460
476
FlowStyle style = _outputOptions .getDefaultFlowStyle ();
461
477
String yamlTag = _typeId ;
462
478
boolean implicit = (yamlTag == null );
463
- String anchor = _objectId ;
464
- if (anchor != null ) {
479
+ Optional < Anchor > anchor = Optional . ofNullable ( _objectId ). map ( s -> new Anchor ( s )) ;
480
+ if (anchor . isPresent () ) {
465
481
_objectId = null ;
466
482
}
467
- _emitter .emit (new SequenceStartEvent (anchor , yamlTag ,
468
- implicit , null , null , style ));
483
+ _emitter .emit (new SequenceStartEvent (anchor , Optional . ofNullable ( yamlTag ) ,
484
+ implicit , style ));
469
485
}
470
486
471
487
@ Override
@@ -477,7 +493,7 @@ public final void writeEndArray() throws IOException
477
493
// just to make sure we don't "leak" type ids
478
494
_typeId = null ;
479
495
_outputContext = _outputContext .getParent ();
480
- _emitter .emit (new SequenceEndEvent (null , null ));
496
+ _emitter .emit (new SequenceEndEvent ());
481
497
}
482
498
483
499
@ Override
@@ -488,12 +504,11 @@ public final void writeStartObject() throws IOException
488
504
FlowStyle style = _outputOptions .getDefaultFlowStyle ();
489
505
String yamlTag = _typeId ;
490
506
boolean implicit = (yamlTag == null );
491
- String anchor = _objectId ;
492
- if (anchor != null ) {
507
+ Optional < Anchor > anchor = Optional . ofNullable ( _objectId ). map ( s -> new Anchor ( s )) ;
508
+ if (anchor . isPresent () ) {
493
509
_objectId = null ;
494
510
}
495
- _emitter .emit (new MappingStartEvent (anchor , yamlTag ,
496
- implicit , null , null , style ));
511
+ _emitter .emit (new MappingStartEvent (anchor , Optional .ofNullable (yamlTag ), implicit , style ));
497
512
}
498
513
499
514
@ Override
@@ -505,7 +520,7 @@ public final void writeEndObject() throws IOException
505
520
// just to make sure we don't "leak" type ids
506
521
_typeId = null ;
507
522
_outputContext = _outputContext .getParent ();
508
- _emitter .emit (new MappingEndEvent (null , null ));
523
+ _emitter .emit (new MappingEndEvent ());
509
524
}
510
525
511
526
/*
@@ -522,7 +537,7 @@ public void writeString(String text) throws IOException,JsonGenerationException
522
537
return ;
523
538
}
524
539
_verifyValueWrite ("write String value" );
525
- DumperOptions . ScalarStyle style = STYLE_QUOTED ;
540
+ ScalarStyle style = STYLE_QUOTED ;
526
541
if (Feature .MINIMIZE_QUOTES .enabledIn (_formatWriteFeatures ) && !isBooleanContent (text )) {
527
542
// If this string could be interpreted as a number, it must be quoted.
528
543
if (Feature .ALWAYS_QUOTE_NUMBERS_AS_STRINGS .enabledIn (_formatWriteFeatures )
@@ -752,7 +767,7 @@ public void writeObjectRef(Object id)
752
767
throws IOException
753
768
{
754
769
_verifyValueWrite ("write Object reference" );
755
- AliasEvent evt = new AliasEvent (String .valueOf (id ), null , null );
770
+ AliasEvent evt = new AliasEvent (Optional . of ( String .valueOf (id )). map ( s -> new Anchor ( s )) );
756
771
_emitter .emit (evt );
757
772
}
758
773
@@ -797,7 +812,7 @@ protected void _releaseBuffers() {
797
812
// ... and sometimes we specifically DO want explicit tag:
798
813
private final static ImplicitTuple EXPLICIT_TAGS = new ImplicitTuple (false , false );
799
814
800
- protected void _writeScalar (String value , String type , DumperOptions . ScalarStyle style ) throws IOException
815
+ protected void _writeScalar (String value , String type , ScalarStyle style ) throws IOException
801
816
{
802
817
_emitter .emit (_scalarEvent (value , style ));
803
818
}
@@ -812,27 +827,26 @@ private void _writeScalarBinary(Base64Variant b64variant,
812
827
}
813
828
final String lf = _lf ();
814
829
String encoded = b64variant .encode (data , false , lf );
815
- _emitter .emit (new ScalarEvent (null , TAG_BINARY , EXPLICIT_TAGS , encoded ,
816
- null , null , STYLE_BASE64 ));
830
+ _emitter .emit (new ScalarEvent (Optional . empty (), Optional . ofNullable ( TAG_BINARY ) , EXPLICIT_TAGS , encoded , STYLE_BASE64 ));
831
+
817
832
}
818
833
819
- protected ScalarEvent _scalarEvent (String value , DumperOptions . ScalarStyle style )
834
+ protected ScalarEvent _scalarEvent (String value , ScalarStyle style )
820
835
{
821
836
String yamlTag = _typeId ;
822
837
if (yamlTag != null ) {
823
838
_typeId = null ;
824
839
}
825
- String anchor = _objectId ;
826
- if (anchor != null ) {
840
+ Optional < Anchor > anchor = Optional . ofNullable ( _objectId ). map ( s -> new Anchor ( s )) ;
841
+ if (anchor . isPresent () ) {
827
842
_objectId = null ;
828
843
}
829
844
// 29-Nov-2017, tatu: Not 100% sure why we don't force explicit tags for
830
845
// type id, but trying to do so seems to double up tag output...
831
- return new ScalarEvent (anchor , yamlTag , NO_TAGS , value ,
832
- null , null , style );
846
+ return new ScalarEvent (anchor , Optional .ofNullable (yamlTag ), NO_TAGS , value , style );
833
847
}
834
848
835
849
protected String _lf () {
836
- return _outputOptions .getLineBreak (). getString ();
850
+ return _outputOptions .getBestLineBreak ();
837
851
}
838
852
}
0 commit comments