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

Commit b280169

Browse files
authored
Merge pull request #76 from rhuss/master
Add new feature LITERAL_BLOCK_STYLE
2 parents 114494f + 156bc36 commit b280169

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,16 @@ public enum Feature // implements FormatFeature // for 2.7
9191
*
9292
* @since 2.8.2
9393
*/
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 strings 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)
95104
;
96105

97106
protected final boolean _defaultState;
@@ -488,6 +497,8 @@ public void writeString(String text) throws IOException,JsonGenerationException
488497
} else {
489498
style = STYLE_PLAIN;
490499
}
500+
} else if (Feature.LITERAL_BLOCK_STYLE.enabledIn(_formatFeatures) && text.indexOf('\n') >= 0) {
501+
style = STYLE_LITERAL;
491502
}
492503
_writeScalar(text, "string", style);
493504
}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,31 @@ public void testNonQuoteNumberStoredAsString() throws Exception
332332
"key: 2.0.1.2.3", yaml);
333333
}
334334

335+
public void testLiteralBlockStyle() throws Exception
336+
{
337+
YAMLFactory f = new YAMLFactory();
338+
// verify default settings
339+
assertFalse(f.isEnabled(YAMLGenerator.Feature.LITERAL_BLOCK_STYLE));
340+
341+
f.configure(YAMLGenerator.Feature.LITERAL_BLOCK_STYLE, true);
342+
343+
YAMLMapper mapper = new YAMLMapper(f);
344+
345+
Map<String, Object> content = new HashMap<String, Object>();
346+
content.put("text", "Hello\nWorld");
347+
String yaml = mapper.writeValueAsString(content).trim();
348+
349+
assertEquals("---\n" +
350+
"text: |-\n Hello\n World", yaml);
351+
352+
content.clear();
353+
content.put("text", "Hello World");
354+
yaml = mapper.writeValueAsString(content).trim();
355+
356+
assertEquals("---\n" +
357+
"text: \"Hello World\"", yaml);
358+
}
359+
335360
/*
336361
/**********************************************************************
337362
/* Helper methods

0 commit comments

Comments
 (0)