Skip to content
This repository was archived by the owner on Dec 19, 2024. It is now read-only.

Commit e66f4b1

Browse files
committed
Add MINIMIZE_QUOTES generator feature
1 parent 4282aff commit e66f4b1

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLGenerator.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
public class YAMLGenerator extends GeneratorBase
2121
{
22+
23+
2224
/**
2325
* Enumeration that defines all togglable features for YAML generators
2426
*/
@@ -66,7 +68,18 @@ public enum Feature // implements FormatFeature // for 2.7
6668
*
6769
* @since 2.6
6870
*/
69-
SPLIT_LINES(true)
71+
SPLIT_LINES(true),
72+
73+
/**
74+
* Whether strings will be rendered without quotes (true) or
75+
* with quotes (false, default).
76+
*<p>
77+
* Minimized quote usage makes for more human readable output; however, content is
78+
* limited to printable characters according to the rules of
79+
* <a href="http://www.yaml.org/spec/1.2/spec.html#style/block/literal">literal block style</a>.
80+
* @since 2.6
81+
*/
82+
MINIMIZE_QUOTES(false)
7083

7184
;
7285

@@ -126,11 +139,15 @@ private Feature(boolean defaultState) {
126139
// numbers, booleans, should use implicit
127140
private final static Character STYLE_SCALAR = null;
128141
// Strings quoted for fun
129-
private final static Character STYLE_STRING = Character.valueOf('"');
130-
142+
private final static Character STYLE_QUOTED = Character.valueOf('"');
143+
// Strings in literal (block) style
144+
private final static Character STYLE_LITERAL = Character.valueOf('|');
145+
131146
// Which flow style to use for Base64? Maybe basic quoted?
132147
private final static Character STYLE_BASE64 = Character.valueOf('"');
133148

149+
private final static Character STYLE_PLAIN = null;
150+
134151
/*
135152
/**********************************************************
136153
/* Output state
@@ -439,7 +456,16 @@ public void writeString(String text) throws IOException,JsonGenerationException
439456
return;
440457
}
441458
_verifyValueWrite("write String value");
442-
_writeScalar(text, "string", STYLE_STRING);
459+
Character style = STYLE_QUOTED;
460+
if (Feature.MINIMIZE_QUOTES.enabledIn(_formatFeatures)) {
461+
if (text.contains("\n")) {
462+
style = STYLE_LITERAL;
463+
}
464+
else {
465+
style = STYLE_PLAIN;
466+
}
467+
}
468+
_writeScalar(text, "string", style);
443469
}
444470

445471
@Override

src/test/java/com/fasterxml/jackson/dataformat/yaml/SimpleGenerationTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,42 @@ public void testSplitLines() throws Exception
192192
yaml);
193193
}
194194

195+
public void testLiteralStringsSingleLine() throws Exception
196+
{
197+
YAMLFactory f = new YAMLFactory();
198+
// verify default settings
199+
assertFalse(f.isEnabled(YAMLGenerator.Feature.MINIMIZE_QUOTES));
200+
201+
f.configure(YAMLGenerator.Feature.MINIMIZE_QUOTES, true);
202+
203+
YAMLMapper mapper = new YAMLMapper(f);
204+
205+
Map<String, Object> content = new HashMap<String, Object>();
206+
content.put("key", "some value");
207+
String yaml = mapper.writeValueAsString(content).trim();
208+
209+
assertEquals("---\n" +
210+
"key: some value", yaml);
211+
}
212+
213+
public void testLiteralStringsMultiLine() throws Exception
214+
{
215+
YAMLFactory f = new YAMLFactory();
216+
// verify default settings
217+
assertFalse(f.isEnabled(YAMLGenerator.Feature.MINIMIZE_QUOTES));
218+
219+
f.configure(YAMLGenerator.Feature.MINIMIZE_QUOTES, true);
220+
221+
YAMLMapper mapper = new YAMLMapper(f);
222+
223+
Map<String, Object> content = new HashMap<String, Object>();
224+
content.put("key", "first\nsecond\nthird");
225+
String yaml = mapper.writeValueAsString(content).trim();
226+
227+
assertEquals("---\n" +
228+
"key: |-\n first\n second\n third", yaml);
229+
}
230+
195231
/*
196232
/**********************************************************************
197233
/* Helper methods

0 commit comments

Comments
 (0)