Skip to content
This repository has been archived by the owner on Jan 22, 2019. It is now read-only.

Commit

Permalink
Merge pull request #76 from rhuss/master
Browse files Browse the repository at this point in the history
Add new feature LITERAL_BLOCK_STYLE
  • Loading branch information
cowtowncoder committed Oct 13, 2016
2 parents 114494f + 156bc36 commit b280169
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,16 @@ public enum Feature // implements FormatFeature // for 2.7
*
* @since 2.8.2
*/
ALWAYS_QUOTE_NUMBERS_AS_STRINGS(false)
ALWAYS_QUOTE_NUMBERS_AS_STRINGS(false),

/**
* Whether for string containing newlines a <a href="http://www.yaml.org/spec/1.2/spec.html#style/block/literal">literal block style</a>
* should be used. This automatically enabled when {@link #MINIMIZE_QUOTES} is set.
* <p>
* The content of such strings is limited to printable characters according to the rules of
* <a href="http://www.yaml.org/spec/1.2/spec.html#style/block/literal">literal block style</a>.
*/
LITERAL_BLOCK_STYLE(false)
;

protected final boolean _defaultState;
Expand Down Expand Up @@ -488,6 +497,8 @@ public void writeString(String text) throws IOException,JsonGenerationException
} else {
style = STYLE_PLAIN;
}
} else if (Feature.LITERAL_BLOCK_STYLE.enabledIn(_formatFeatures) && text.indexOf('\n') >= 0) {
style = STYLE_LITERAL;
}
_writeScalar(text, "string", style);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,31 @@ public void testNonQuoteNumberStoredAsString() throws Exception
"key: 2.0.1.2.3", yaml);
}

public void testLiteralBlockStyle() throws Exception
{
YAMLFactory f = new YAMLFactory();
// verify default settings
assertFalse(f.isEnabled(YAMLGenerator.Feature.LITERAL_BLOCK_STYLE));

f.configure(YAMLGenerator.Feature.LITERAL_BLOCK_STYLE, true);

YAMLMapper mapper = new YAMLMapper(f);

Map<String, Object> content = new HashMap<String, Object>();
content.put("text", "Hello\nWorld");
String yaml = mapper.writeValueAsString(content).trim();

assertEquals("---\n" +
"text: |-\n Hello\n World", yaml);

content.clear();
content.put("text", "Hello World");
yaml = mapper.writeValueAsString(content).trim();

assertEquals("---\n" +
"text: \"Hello World\"", yaml);
}

/*
/**********************************************************************
/* Helper methods
Expand Down

0 comments on commit b280169

Please sign in to comment.