@@ -28,7 +28,7 @@ public enum Feature // implements FormatFeature // for 2.7
28
28
/**
29
29
* Whether we are to write an explicit document start marker ("---")
30
30
* or not.
31
- *
31
+ *
32
32
* @since 2.3
33
33
*/
34
34
WRITE_DOC_START_MARKER (true ),
@@ -38,17 +38,17 @@ public enum Feature // implements FormatFeature // for 2.7
38
38
* or "generic" Object Id mechanism (false). Former works better for systems that
39
39
* are YAML-centric; latter may be better choice for interoperability, when
40
40
* converting between formats or accepting other formats.
41
- *
41
+ *
42
42
* @since 2.5
43
43
*/
44
44
USE_NATIVE_OBJECT_ID (true ),
45
-
45
+
46
46
/**
47
47
* Whether to use YAML native Type Id construct for indicating type (true);
48
48
* or "generic" type property (false). Former works better for systems that
49
49
* are YAML-centric; latter may be better choice for interoperability, when
50
50
* converting between formats or accepting other formats.
51
- *
51
+ *
52
52
* @since 2.5
53
53
*/
54
54
USE_NATIVE_TYPE_ID (true ),
@@ -80,7 +80,7 @@ public enum Feature // implements FormatFeature // for 2.7
80
80
* @since 2.7
81
81
*/
82
82
MINIMIZE_QUOTES (false ),
83
-
83
+
84
84
/**
85
85
* Whether numbers stored as strings will be rendered with quotes (true) or
86
86
* without quotes (false, default) when MINIMIZE_QUOTES is enabled.
@@ -91,12 +91,21 @@ public enum Feature // implements FormatFeature // for 2.7
91
91
*
92
92
* @since 2.8.2
93
93
*/
94
- ALWAYS_QUOTE_NUMBERS_AS_STRINGS (false )
94
+ ALWAYS_QUOTE_NUMBERS_AS_STRINGS (false ),
95
+
96
+ /**
97
+ * Whether for string containing newlines a <a href="http://www.yaml.org/spec/1.2/spec.html#style/block/literal">literal block style</a>
98
+ * should be used. This automatically enabled when {@link #MINIMIZE_QUOTES} is set.
99
+ * <p>
100
+ * The content of such strinfs is limited to printable characters according to the rules of
101
+ * <a href="http://www.yaml.org/spec/1.2/spec.html#style/block/literal">literal block style</a>.
102
+ */
103
+ LITERAL_BLOCK_STYLE (false )
95
104
;
96
105
97
106
protected final boolean _defaultState ;
98
107
protected final int _mask ;
99
-
108
+
100
109
/**
101
110
* Method that calculates bit set (flags) of all features that
102
111
* are enabled by default.
@@ -111,14 +120,14 @@ public static int collectDefaults()
111
120
}
112
121
return flags ;
113
122
}
114
-
123
+
115
124
private Feature (boolean defaultState ) {
116
125
_defaultState = defaultState ;
117
126
_mask = (1 << ordinal ());
118
127
}
119
-
128
+
120
129
public boolean enabledByDefault () { return _defaultState ; }
121
- public boolean enabledIn (int flags ) { return (flags & _mask ) != 0 ; }
130
+ public boolean enabledIn (int flags ) { return (flags & _mask ) != 0 ; }
122
131
public int getMask () { return _mask ; }
123
132
}
124
133
@@ -127,7 +136,7 @@ private Feature(boolean defaultState) {
127
136
/* Internal constants
128
137
/**********************************************************
129
138
*/
130
-
139
+
131
140
protected final static long MIN_INT_AS_LONG = (long ) Integer .MIN_VALUE ;
132
141
protected final static long MAX_INT_AS_LONG = (long ) Integer .MAX_VALUE ;
133
142
protected final static Pattern PLAIN_NUMBER_P = Pattern .compile ("[0-9]*(\\ .[0-9]*)?" );
@@ -153,7 +162,7 @@ private Feature(boolean defaultState) {
153
162
154
163
// for field names, leave out quotes
155
164
private final static Character STYLE_NAME = null ;
156
-
165
+
157
166
// numbers, booleans, should use implicit
158
167
private final static Character STYLE_SCALAR = null ;
159
168
// Strings quoted for fun
@@ -185,7 +194,7 @@ private Feature(boolean defaultState) {
185
194
* need to output one.
186
195
*/
187
196
protected String _typeId ;
188
-
197
+
189
198
/*
190
199
/**********************************************************
191
200
/* Life-cycle
@@ -203,14 +212,14 @@ public YAMLGenerator(IOContext ctxt, int jsonFeatures, int yamlFeatures,
203
212
_writer = out ;
204
213
205
214
_outputOptions = buildDumperOptions (jsonFeatures , yamlFeatures , version );
206
-
215
+
207
216
_emitter = new Emitter (_writer , _outputOptions );
208
217
// should we start output now, or try to defer?
209
218
_emitter .emit (new StreamStartEvent (null , null ));
210
219
Map <String ,String > noTags = Collections .emptyMap ();
211
-
220
+
212
221
boolean startMarker = Feature .WRITE_DOC_START_MARKER .enabledIn (yamlFeatures );
213
-
222
+
214
223
_emitter .emit (new DocumentStartEvent (null , null , startMarker ,
215
224
version , // for 1.10 was: ((version == null) ? null : version.getArray()),
216
225
noTags ));
@@ -232,10 +241,10 @@ protected DumperOptions buildDumperOptions(int jsonFeatures, int yamlFeatures, o
232
241
return opt ;
233
242
}
234
243
235
- /*
236
- /**********************************************************
237
- /* Versioned
238
- /**********************************************************
244
+ /*
245
+ /**********************************************************
246
+ /* Versioned
247
+ /**********************************************************
239
248
*/
240
249
241
250
@ Override
@@ -291,7 +300,7 @@ public JsonGenerator overrideFormatFeatures(int values, int mask) {
291
300
_formatFeatures = (_formatFeatures & ~mask ) | (values & mask );
292
301
return this ;
293
302
}
294
-
303
+
295
304
@ Override
296
305
public boolean canUseSchema (FormatSchema schema ) {
297
306
return false ;
@@ -330,13 +339,13 @@ public YAMLGenerator configure(Feature f, boolean state) {
330
339
}
331
340
return this ;
332
341
}
333
-
342
+
334
343
/*
335
344
/**********************************************************************
336
345
/* Overridden methods; writing field names
337
346
/**********************************************************************
338
347
*/
339
-
348
+
340
349
/* And then methods overridden to make final, streamline some
341
350
* aspects...
342
351
*/
@@ -389,7 +398,7 @@ public final void flush() throws IOException
389
398
{
390
399
_writer .flush ();
391
400
}
392
-
401
+
393
402
@ Override
394
403
public void close () throws IOException
395
404
{
@@ -406,7 +415,7 @@ public void close() throws IOException
406
415
/* Public API: structural output
407
416
/**********************************************************
408
417
*/
409
-
418
+
410
419
@ Override
411
420
public final void writeStartArray () throws IOException
412
421
{
@@ -422,15 +431,15 @@ public final void writeStartArray() throws IOException
422
431
_emitter .emit (new SequenceStartEvent (anchor , yamlTag ,
423
432
implicit , null , null , style ));
424
433
}
425
-
434
+
426
435
@ Override
427
436
public final void writeEndArray () throws IOException
428
437
{
429
438
if (!_writeContext .inArray ()) {
430
439
_reportError ("Current context not Array but " +_writeContext .typeDesc ());
431
440
}
432
441
// just to make sure we don't "leak" type ids
433
- _typeId = null ;
442
+ _typeId = null ;
434
443
_writeContext = _writeContext .getParent ();
435
444
_emitter .emit (new SequenceEndEvent (null , null ));
436
445
}
@@ -458,7 +467,7 @@ public final void writeEndObject() throws IOException
458
467
_reportError ("Current context not Object but " +_writeContext .typeDesc ());
459
468
}
460
469
// just to make sure we don't "leak" type ids
461
- _typeId = null ;
470
+ _typeId = null ;
462
471
_writeContext = _writeContext .getParent ();
463
472
_emitter .emit (new MappingEndEvent (null , null ));
464
473
}
@@ -488,6 +497,8 @@ public void writeString(String text) throws IOException,JsonGenerationException
488
497
} else {
489
498
style = STYLE_PLAIN ;
490
499
}
500
+ } else if (Feature .LITERAL_BLOCK_STYLE .enabledIn (_formatFeatures ) && text .indexOf ('\n' ) >= 0 ) {
501
+ style = STYLE_LITERAL ;
491
502
}
492
503
_writeScalar (text , "string" , style );
493
504
}
@@ -565,7 +576,7 @@ public void writeRawValue(char[] text, int offset, int len) throws IOException {
565
576
/* Output method implementations, base64-encoded binary
566
577
/**********************************************************
567
578
*/
568
-
579
+
569
580
@ Override
570
581
public void writeBinary (Base64Variant b64variant , byte [] data , int offset , int len ) throws IOException
571
582
{
@@ -624,13 +635,13 @@ public void writeNumber(BigInteger v) throws IOException
624
635
_verifyValueWrite ("write number" );
625
636
_writeScalar (String .valueOf (v .toString ()), "java.math.BigInteger" , STYLE_SCALAR );
626
637
}
627
-
638
+
628
639
@ Override
629
640
public void writeNumber (double d ) throws IOException
630
641
{
631
642
_verifyValueWrite ("write number" );
632
643
_writeScalar (String .valueOf (d ), "double" , STYLE_SCALAR );
633
- }
644
+ }
634
645
635
646
@ Override
636
647
public void writeNumber (float f ) throws IOException
@@ -681,14 +692,14 @@ public boolean canWriteObjectId() {
681
692
// yes, YAML does support Native Type Ids!
682
693
// 10-Sep-2014, tatu: Except as per [#23] might not want to...
683
694
return Feature .USE_NATIVE_OBJECT_ID .enabledIn (_formatFeatures );
684
- }
695
+ }
685
696
686
697
@ Override
687
698
public boolean canWriteTypeId () {
688
699
// yes, YAML does support Native Type Ids!
689
700
// 10-Sep-2014, tatu: Except as per [#22] might not want to...
690
701
return Feature .USE_NATIVE_TYPE_ID .enabledIn (_formatFeatures );
691
- }
702
+ }
692
703
693
704
@ Override
694
705
public void writeTypeId (Object id )
@@ -706,7 +717,7 @@ public void writeObjectRef(Object id)
706
717
AliasEvent evt = new AliasEvent (String .valueOf (id ), null , null );
707
718
_emitter .emit (evt );
708
719
}
709
-
720
+
710
721
@ Override
711
722
public void writeObjectId (Object id )
712
723
throws IOException
@@ -749,7 +760,7 @@ protected void _writeScalar(String value, String type, Character style) throws I
749
760
{
750
761
_emitter .emit (_scalarEvent (value , style ));
751
762
}
752
-
763
+
753
764
protected ScalarEvent _scalarEvent (String value , Character style )
754
765
{
755
766
String yamlTag = _typeId ;
0 commit comments